Debian Codegraph @

src/tls/test-socket-activation-helper.sh

raw
1 #!/bin/sh
2 # SPDX-License-Identifier: LGPL-2.1-or-later
3 set -eu
4
5 if ! type curl >/dev/null 2>&1; then
6 echo "1..0 # SKIP: curl not installed"
7 exit 0
8 fi
9
10 echo "1..5"
11
12 # start activation helper
13 SOCKET_DIR=$(mktemp -d --tmpdir socks.XXXXXX)
14 COCKPIT_WS_PROCESS_IDLE=1 ./socket-activation-helper ./cockpit-ws "$SOCKET_DIR" &
15 HELPER_PID=$!
16 trap "kill $HELPER_PID; rm -r '$SOCKET_DIR'" EXIT INT QUIT PIPE
17
18 # wait until it is ready
19 for timeout in `seq 50`; do
20 curl --silent --head --unix-socket "$SOCKET_DIR/http.sock" http://dummy/cockpit/login >/dev/null && break
21 sleep 0.2
22 done
23
24 # expected results; we can't really login, but it should get sufficiently far
25 SUCCESS="HTTP/1.1 [45]0"
26 REDIRECT="HTTP/1.1 301"
27
28 # args: <socketname> <expected output>
29 expect_curl() {
30 OUT=$(curl --silent --show-error --head --unix-socket "$SOCKET_DIR/$1" http://dummy/cockpit/login)
31 if ! echo "$OUT" | grep -q "$2"; then
32 echo "FAIL: output does not contain $2" >&2
33 echo "$OUT" >&2
34 exit 1
35 fi
36 }
37
38 # args: <instance> <expected output>
39 expect_start() {
40 OUT=$(./wsinstance-start "$1" "$SOCKET_DIR")
41 if ! echo "$OUT" | grep -q "$2"; then
42 echo "FAIL: output does not contain $2" >&2
43 echo "$OUT" >&2
44 exit 1
45 fi
46 }
47
48 SHA256_CERT=$(sed -n '/CLIENT_CERT_FINGERPRINT/ { s/^[^"]*//; s/"//g; p }' $srcdir/src/tls/testing.h)
49 SHA256_NIL="$(sha256sum < /dev/null | cut -c1-64)"
50
51 expect_curl http.sock "$SUCCESS"
52 # second call to existing instance
53 expect_curl http.sock "$SUCCESS"
54 # wait for idle timeout
55 sleep 2
56 expect_curl http.sock "$SUCCESS"
57 echo "ok 1 http.sock"
58
59
60 expect_start $SHA256_NIL "^done$"
61 echo "ok 2 https-factory/success"
62
63 expect_start "junk" "^fail$"
64 echo "ok 3 https-factory/fail"
65
66 expect_curl https@$SHA256_NIL.sock "$SUCCESS"
67 # second call to existing instance
68 expect_curl https@$SHA256_NIL.sock "$SUCCESS"
69 # wait for idle timeout
70 sleep 2
71 expect_curl https@$SHA256_NIL.sock "$SUCCESS"
72 echo "ok 4 https@$SHA256_NIL.sock"
73
74 expect_curl https@$SHA256_CERT.sock "$SUCCESS"
75 echo "ok 5 https@$SHA256_CERT.sock"