aboutsummaryrefslogtreecommitdiff
path: root/regress/lib.sh
blob: 3018b999d7e2e7ab1554dbbeaa96cad05dcff447 (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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
failed=

gg="./../gg"
gmid="./../gmid"
current_test=

run_test() {
	ggflags=
	port=10965
	config_common="
ipv6 off
port $port
"
	hdr=
	body=
	dont_check_server_alive=no

	current_test=$1
	rm -f reg.conf

	if ! $1; then
		echo "$1 failed"
		failed="$failed $1"
	else
		echo "$1 passed"
	fi

	if [ "$dont_check_server_alive" != 'no' ]; then
		return
	fi

	if ! check; then
		echo "gmid crashed?"
		failed="$failed $1"
	fi
}

tests_done() {
	if [ "$failed" != "" ]; then
		echo
		echo "failed tests:$failed"
		exit 1
	fi
	exit 0
}

# usage: gen_config <global config> <server config>
# generates a configuration file reg.conf
gen_config() {
	cat <<EOF > reg.conf
$config_common
$1
server "localhost" {
	cert "$PWD/cert.pem"
	key  "$PWD/key.pem"
	root "$PWD/testdata"
	$2
}
EOF
	if ! checkconf; then
		echo "failed to parse the config" >&2
		return 1
	fi
}

checkconf() {
	if ! $gmid -n -c reg.conf >/dev/null 2>&1; then
		$gmid -n -c reg.conf
	fi
}

# usage: setup_simple_test <global config> <server config>
# generates a configuration file with `gen_config', validates it and
# launches the daemon
setup_simple_test() {
	gen_config "$1" "$2"
	run
}

# usage: get <path>
# return the body of the request on stdout
get() {
	$gg -T10 $ggflags "gemini://localhost:10965/$1" || true
}

# usage: head <path>
# return the meta response line on stdout
head() {
	$gg -T10 -d header $ggflags "gemini://localhost:10965/$1" || true
}

# usage: raw <path>
# return both header and body
raw() {
	$gg -T10 -dwhole $ggflags "gemini://localhost:10965/$1" || true
}

# usage: fetch <path>
# fetches the header and the body.  They're returned in $hdr and
# $body.
fetch() {
	if ! hdr="$(head $1)" || ! body="$(get $1)"; then
		return 1
	fi
}

# usage: fetch_hdr <path>
# fetches the header into $hdr
fetch_hdr() {
	hdr="$(head $1)"
	body=""
}

# usage: check_reply header body
# checks that $hdr and $body are equal to the given strings
check_reply() {
	if [ "$hdr" != "$1" ]; then
		echo "Header mismatch" >&2
		echo "wants : $1"      >&2
		echo "got   : $hdr"    >&2
		return 1
	fi

	if [ "$body" != "$2" ]; then
		echo "Body mismatch" >&2
		echo "wants : $2"    >&2
		echo "got   : $body" >&2
		return 1
	fi
}

run() {
	if check; then
		kill -HUP "$(cat gmid.pid)"
		sleep 1
		return
	fi

	$gmid -P gmid.pid -c reg.conf

	# give gmid time to bind the port, otherwise we end up
	# executing gg when gmid isn't ready yet.
	sleep 1
}

check() {
	if [ ! -f gmid.pid ]; then
		return 1
	fi

	pid="$(cat gmid.pid || true)"
	if [ "$pid" == "" ]; then
		return 1
	fi

	# remember: we're running under ``set -e''
	if ps $pid >/dev/null; then
		return 0
	fi

	return 1
}

count() {
	wc -l | xargs
}

quit() {
	pid="$(cat gmid.pid || true)"
	if [ "$pid" != "" ]; then
		kill $pid || true
		wait || true
	fi
}

onexit() {
	rm -f bigfile bigfile.sha
	quit
}