summaryrefslogtreecommitdiff
path: root/lib9p/tests/runtest
blob: c2f6c41595a97b1adf7fd5924689c1f6075c4ba3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#!/usr/bin/env bash
# lib9p/tests/runtest - Simple tests for the 9P `test_server`
#
# Copyright (C) 2025  Luke T. Shumaker <lukeshu@lukeshu.com>
# SPDX-License-Identifier: AGPL-3.0-or-later

set -euE -o pipefail
set -x

valgrind --error-exitcode=2 ./tests/test_server/test_server &
server_pid=$!
# shellcheck disable=SC2064
trap "kill $server_pid || true; wait $server_pid || true" EXIT
server_addr='localhost:9000'

client=(9p -a "$server_addr")

expect_lines() (
	{ set +x; } &>/dev/null
	printf >&2 '+ diff -u expected.txt actual.txt\n'
	diff -u <(printf '%s\n' "$@") <(printf '%s\n' "$out")
)

while [[ -d /proc/$server_pid && "$(readlink /proc/$server_pid/fd/4 2>/dev/null)" != socket:* ]]; do sleep 0.1; done

out=$("${client[@]}" ls -l '')
expect_lines \
	'd-r-xr-xr-x M 0 root root   0 Oct  7 15:51 Documentation' \
	'--r--r--r-- M 0 root root 166 Oct  7 15:51 README.md' \
	'---w--w--w- M 0 root root   0 Oct  7 15:51 shutdown'

out=$("${client[@]}" ls -l 'Documentation/')
expect_lines \
	'--r--r--r-- M 0 root root 162 Oct  7 15:51 x'

out=$("${client[@]}" read 'README.md')
expect_lines \
	'<!--' \
	'  README.md - test static file' \
	'' \
	'  Copyright (C) 2024-2025  Luke T. Shumaker <lukeshu@lukeshu.com>' \
	'  SPDX-License-Identifier: AGPL-3.0-or-later' \
	'-->' \
	'Hello, world!'

out=$("${client[@]}" read 'Documentation/x')
expect_lines \
	'<!--' \
	'  Documentation/x - test static file' \
	'' \
	'  Copyright (C) 2024-2025  Luke T. Shumaker <lukeshu@lukeshu.com>' \
	'  SPDX-License-Identifier: AGPL-3.0-or-later' \
	'-->' \
	'foo'

out=$("${client[@]}" stat 'Documentation/x')
expect_lines \
	"'x' 'root' 'root' 'root' q (0000000000000001 1 ) m 0444 at 1728337905 mt 1728337904 l 162 t 0 d 0"

out=$("${client[@]}" write 'shutdown' <<<1)
expect_lines ''

wait "$server_pid"
trap - EXIT