-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
Copy pathop-test.sh
356 lines (314 loc) · 8.01 KB
/
op-test.sh
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
#!/bin/sh
#
# Downloads the run_unit_tests.sh file from easyrsa-unit-tests repo
# and executes that - allows for disconnected testing from the easy-rsa
# repo with TravisCI.
# log
log () {
[ "$disable_log" ] && return
if printf '%s\n' "* $*"; then
return
else
echo "printf failed"
exit 9
fi
} # => log ()
# clean up
clean_up () {
if [ "$no_delete" ]; then
log "saved final state.."
else
if [ "$EASYRSA_NIX" ]; then
[ "$keep_eut" ] || rm -f "$utest_bin"
[ "$keep_sc" ] || rm -f "$sc_bin"
[ "$keep_ssl" ] || rm -f "$ssl_bin"
fi
fi
} # => clean_up ()
# curl download and openssl hash
# wing it ..
curl_it () {
#log "BEGIN: curl_it"
if [ "$#" -eq 2 ]; then
file="$1"
hash="$2"
else
log "> Usage: <file> <hash>"
return 1
fi
if [ "$enable_curl" ]; then
: # ok
else
log "> curl disabled"
return 0
fi
# valid target
case "$file" in
easyrsa-unit-tests.sh)
unset -v require_hash
;;
shellcheck|openssl)
require_hash=1
;;
*)
log "> invalid target: $file"
return 1
esac
# download
if [ "$enable_curl" ]; then
log "> download: ${gh_url}/${file}"
curl -SO "${gh_url}/${file}" || \
log "> download failed: ${file}"
else
log "> curl disabled"
fi
# hash download
if [ "${require_hash}" ]; then
if [ -e "${file}" ]; then
log "> hash ${file}"
temp_hash="$(openssl sha256 "${file}")"
log "temp_hash: $temp_hash"
log "hash : $hash"
if [ "$temp_hash" = "$hash" ]; then
: # OK - hash is good
else
log "> hash failed: ${file}"
return 1
fi
else
log "> file missing: ${file}"
return 1
fi
else
if [ -e "${file}" ]; then
: # ok - file is here
else
log "> file missing: ${file}"
return 1
fi
fi
} # => curl_it ()
################################################################################
# RUN unit test
run_unit_test ()
{
if [ "${utest_bin_ok}" ] && [ "${ssl_bin_ok}" ]; then
# Start unit tests
log ">>> BEGIN unit tests:"
if [ "${dry_run}" ]; then
log "<<dry-run>> sh ${utest_bin} ${verb}"
estat=1
else
log ">>>>>>: sh ${utest_bin} ${verb}"
if sh "${utest_bin}" "${verb}"; then
log "OK"
estat=0
else
log "FAIL"
estat=1
fi
fi
log "<<< END unit tests:"
else
log "unit-test abandoned"
estat=1
fi
} # => run_unit_test ()
########################################
## DOWNLOAD unit-test
download_unit_test () {
# if not present then download unit-test
target_file="${utest_file}"
target_hash="${utest_hash}"
if [ "$enable_unit_test" ]; then
if [ -e "${ERSA_UT}/${target_file}" ]; then
keep_eut=1
[ -x "${ERSA_UT}/${target_file}" ] || \
chmod +x "${ERSA_UT}/${target_file}"
# version check
if "${ERSA_UT}/${target_file}" version; then
utest_bin="${ERSA_UT}/${target_file}"
utest_bin_ok=1
else
log "version check failed: ${ERSA_UT}/${target_file}"
fi
else
# download and basic check
log "curl_it ${target_file}"
if curl_it "${target_file}" "${target_hash}"; then
[ -x "${ERSA_UT}/${target_file}" ] || \
chmod +x "${ERSA_UT}/${target_file}"
# functional check - version check
if "${ERSA_UT}/${target_file}" version; then
utest_bin="${ERSA_UT}/${target_file}"
utest_bin_ok=1
else
log "version check failed: ${target_file}"
fi
else
log "curl_it ${target_file} - failed"
fi
fi
[ "$utest_bin_ok" ] || log "undefined: utest_bin_ok"
log "setup unit-test - ok"
else
log "unit-test disabled"
fi # => shellcheck
}
## DOWNLOAD unit-test
################################################################################
## USE shellcheck
# Run shellcheck
run_shellcheck () {
if [ "$enable_shellcheck" ] && [ "$sc_bin_ok" ] && [ "$EASYRSA_NIX" ]; then
if [ -e easyrsa3/easyrsa ]; then
if "${sc_bin}" -s sh -S warning -x easyrsa3/easyrsa; then
log "shellcheck completed - ok"
else
log "shellcheck completed - *easyrsa* FAILED"
fi
else
log "easyrsa binary not present, not using shellcheck"
fi
else
log "shellcheck abandoned"
fi
}
## USE shellcheck
########################################
## DOWNLOAD shellcheck
download_shellcheck () {
# if not present then download shellcheck
target_file="${sc_file}"
target_hash="${sc_hash}"
if [ "$enable_shellcheck" ] && [ "$EASYRSA_NIX" ]; then
log "setup shellcheck"
if [ -e "${ERSA_UT}/${target_file}" ]; then
keep_sc=1
[ -x "${ERSA_UT}/${target_file}" ] || \
chmod +x "${ERSA_UT}/${target_file}"
"${ERSA_UT}/${target_file}" -V || \
log "version check failed: ${ERSA_UT}/${target_file}"
sc_bin="${ERSA_UT}/${target_file}"
sc_bin_ok=1
else
# download and basic check
log "curl_it ${target_file}"
if curl_it "${target_file}" "${target_hash}"; then
log "curl_it ${target_file} - ok"
[ -x "${ERSA_UT}/${target_file}" ] || \
chmod +x "${ERSA_UT}/${target_file}"
# functional check
if "${ERSA_UT}/${target_file}" -V; then
sc_bin="${ERSA_UT}/${target_file}"
sc_bin_ok=1
else
log "version check failed: ${ERSA_UT}/${target_file}"
fi
else
log "curl_it ${target_file} - failed"
fi
fi
fi
## DOWNLOAD shellcheck
}
################################################################################
## DOWNLOAD openssl-3
download_opensslv3 () {
# if not present then download and then use openssl3
target_file="${ssl_file}"
target_hash="${ssl_hash}"
if [ "$enable_openssl3" ] && [ "$EASYRSA_NIX" ]; then
if [ -e "${ERSA_UT}/${target_file}" ]; then
keep_ssl=1
[ -x "${ERSA_UT}/${target_file}" ] || \
chmod +x "${ERSA_UT}/${target_file}"
# version check 'openssl version'
"${ERSA_UT}/${target_file}" version || \
log "version check failed: ${ERSA_UT}/${target_file}"
ssl_bin="${ERSA_UT}/${target_file}"
ssl_bin_ok=1
else
# download and basic check
log "curl_it ${target_file}"
if curl_it "${target_file}" "${target_hash}"; then
log "curl_it ${target_file} - ok"
[ -x "${ERSA_UT}/${target_file}" ] || \
chmod +x "${ERSA_UT}/${target_file}"
# functional check - version check 'openssl version'
if "${ERSA_UT}/${target_file}" version; then
ssl_bin="${ERSA_UT}/${target_file}"
ssl_bin_ok=1
# Set up Easy-RSA Unit-Test for OpenSSL-v3
export EASYRSA_OPENSSL="${ssl_bin}"
else
log "version check failed: ${ERSA_UT}/${target_file}"
fi
else
log "curl_it ${target_file} - failed"
fi
fi
log "setup openssl3 - hey hokey-dokey-lopey"
log "OpenSSL-v3 ENabled"
else
if [ "$EASYRSA_NIX" ]; then
log "System SSL enabled"
ssl_bin="openssl"
ssl_bin_ok=1
else
log "Windows, no OpenSSL-v3"
fi
fi
} # => ## DOWNLOAD openssl-3
################################################################################
unset -v disable_log verb enable_unit_test enable_shellcheck enable_openssl3 \
keep_sc keep_ssl keep_eut no_delete
# Set by default
enable_unit_test=1
enable_curl=1
while [ -n "$1" ]; do
case "$1" in
--no-log) disable_log=1 ;;
'') verb='-v' ;;
-v) verb='-v' ;;
-vv) verb='-vv' ;;
-sc) enable_shellcheck=1 ;;
-o3) enable_openssl3=1 ;;
-dr) dry_run=1 ;;
-nt|--no-test) unset -v enable_unit_test ;;
-nc|--no-curl) unset -v enable_curl ;;
-nd|--no-delete) no_delete=1 ;;
*)
log "Unknown option: $1"
exit 9
esac
shift
done
log "Easy-RSA Unit Tests:"
# Layout
ERSA_UT="${PWD}"
# Sources
gh_url='https://raw.githubusercontent.com/OpenVPN/easyrsa-unit-tests/master'
utest_file='easyrsa-unit-tests.sh'
unset -v utest_bin utest_bin_ok
utest_hash='no-hash'
sc_file='shellcheck'
unset -v sc_bin sc_bin_ok
sc_hash='SHA256(shellcheck)= f4bce23c11c3919c1b20bcb0f206f6b44c44e26f2bc95f8aa708716095fa0651'
ssl_file='openssl'
unset -v ssl_bin ssl_bin_ok
ssl_hash='SHA256(openssl)= bc4a5882bad4f51e6d04c25877e1e85ad86f14c5f6e078dd9c02f9d38f8791be'
# Here we go ..
# allow shellcheck to fail
download_shellcheck
run_shellcheck
# if this fails then fly system ssl
download_opensslv3
# The test which matters!
download_unit_test
run_unit_test
clean_up
################################################################################
log "estat: $estat ${dry_run:+<<dry run>>}"
exit $estat
# vim: no