Debian Codegraph @

src/pins/tang/tests/tang-validate-adv

raw symbols SCIP
1 #!/bin/bash -xe
2 # vim: set ts=8 shiftwidth=4 softtabstop=4 expandtab smarttab colorcolumn=80:
3 #
4 # Copyright (c) 2020 Red Hat, Inc.
5 # Author: Sergio Correia <[email protected]>
6 #
7 # This program is free software: you can redistribute it and/or modify
8 # it under the terms of the GNU General Public License as published by
9 # the Free Software Foundation, either version 3 of the License, or
10 # (at your option) any later version.
11 #
12 # This program is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
16 #
17 # You should have received a copy of the GNU General Public License
18 # along with this program. If not, see <http://www.gnu.org/licenses/>.
19 #
20
21 . tang-common-test-functions
22
23 on_exit() {
24 local exit_status=$?
25 for d in "${TMP}" "${TMP2}"; do
26 [ ! -d "${d}" ] && continue
27 tang_stop "${d}"
28 rm -rf "${d}"
29 done
30 exit "${exit_status}"
31 }
32
33 do_test() {
34 local response="${1}"
35 local stderr="${2:-/dev/stderr}"
36 local port
37
38 run_test_server "${TMP2}" "${response}"
39 port=$(tang_get_port "${TMP2}")
40 cfg="$(printf '{"url":"localhost:%d"}' "${port}")"
41 if ! echo foo | clevis encrypt tang "${cfg}" -y 2>"${stderr}"; then
42 echo "Error (do_test) response: ${response}" >&2
43 [ -r "${stderr}" ] && cat "${stderr}" >&2
44 return 1
45 fi
46 }
47
48 do_test_with_adv() {
49 local adv="${1}"
50 local stderr="${2:-/dev/stderr}"
51
52 cfg="$(printf '{"url":"127.1.2.3:1234","adv":"%s"}' "${adv}")"
53 if ! echo foo-adv | clevis encrypt tang "${cfg}" 2>"${stderr}"; then
54 echo "Error (do_test_with_adv) adv: ${adv} response: ${response}" >&2
55 [ -r "${stderr}" ] && cat "${stderr}" >&2
56 return 1
57 fi
58 }
59
60 validate_output() {
61 local output="${1}"
62 if grep -Fq jose "${output}"; then
63 error "'jose' is not expected to appear in the error output"
64 fi
65 }
66
67 trap 'on_exit' EXIT
68
69 TMP="$(mktemp -d)"
70 TMP2="$(mktemp -d)"
71
72 CASES="${TMP}/cases"
73 mkdir -p "${CASES}"
74
75 # Let's test server responses.
76 # Case 1 - regular advertisement - PASS.
77 RESP="${CASES}"/good-01
78 cat << EOF > "${RESP}"
79 HTTP/1.0 200 OK
80
81 $(tang_create_adv "${TMP}" /dev/stdout)
82 EOF
83
84 # Case 2 - bad advertisement.
85 RESP="${CASES}"/bad-01
86 adv='{'
87 cat << EOF > "${RESP}"
88 HTTP/1.0 200 OK
89
90 ${adv}
91 EOF
92
93 # Case 3 - returning 404.
94 RESP="${CASES}"/bad-02
95 cat << EOF > "${RESP}"
96 HTTP/1.0 404 Not Found
97
98 EOF
99
100 # case 4 - returning 301.
101 RESP="${CASES}"/bad-03
102 cat << EOF > "${RESP}"
103 HTTP/1.0 301 Moved Permanently
104
105 EOF
106
107 # case 5 - returning 500.
108 RESP="${CASES}"/bad-04
109 cat << EOF > "${RESP}"
110 HTTP/1.0 500 Internal Server Error
111
112 EOF
113
114 for c in "${CASES}"/good-*; do
115 STDERR="${c}".stderr
116 do_test "${c}" "${STDERR}"
117 validate_output "${STDERR}"
118 done
119
120 # Tests where bind is expected to fail (validate is still expected to succeed).
121 for c in "${CASES}"/bad-*; do
122 STDERR="${c}".stderr
123 ! do_test "${c}" "${STDERR}"
124 validate_output "${STDERR}"
125 done
126
127 # Now let's do some tests passing "adv" in the configuration.
128 STDERR="${CASES}"/stderr
129 for adv in "[]" "]" "" "{}"; do
130 ! do_test_with_adv "${adv}" "${STDERR}"
131 validate_output "${STDERR}"
132 done
133
134 # Now let's use existing files as well.
135 tang_run "${TMP}"
136 port=$(tang_get_port "${TMP}")
137
138 touch "${CASES}"/adv-bad-01
139 echo '{' > "${CASES}"/adv-bad-02
140 echo "foobar" > "${CASES}"/adv-bad-03
141 tang_get_adv "${port}" "${CASES}"/adv-good-01
142
143 # Tests where bind is expected to pass.
144 for adv in "${CASES}"/adv-good-*; do
145 STDERR="${adv}".stderr
146 do_test_with_adv "${adv}" "${STDERR}"
147 validate_output "${STDERR}"
148 done
149
150 # Tests where bind is expected to fail. validate still should pass.
151 for adv in "${CASES}"/adv-bad-*; do
152 STDERR="${adv}".stderr
153 ! do_test_with_adv "${adv}" "${STDERR}"
154 validate_output "${STDERR}"
155 done