Browse Source

Initial commit

Yann Weber 6 years ago
commit
b11ea65bf2
1 changed files with 502 additions and 0 deletions
  1. 502
    0
      check.sh

+ 502
- 0
check.sh View File

@@ -0,0 +1,502 @@
1
+#!/bin/sh
2
+
3
+alias echo="/bin/echo -e"
4
+
5
+color=1
6
+verbose=2
7
+exit_on_fail=0
8
+
9
+col_reset() {
10
+	if [ "$color" -gt 0 ]
11
+	then
12
+		tput sgr0
13
+	fi
14
+}
15
+
16
+col_set() {
17
+	if [ -z "$1" ]
18
+	then
19
+		return
20
+	fi
21
+	if [ "$color" -gt 0 ]
22
+	then
23
+		tput setaf $1
24
+	fi
25
+}
26
+
27
+log() {
28
+	echo "$(col_set $3)[$(printf "%7s" "$1")]$(col_reset) $2"
29
+}
30
+
31
+datetime() {
32
+	date -Iseconds
33
+}
34
+
35
+logdate() {
36
+	echo "$(date -Iseconds) $(col_set $3)[$(printf "%7s" "$1")]$(col_reset) $2"
37
+}
38
+
39
+fail() {
40
+	if [ "$verbose" -lt 2 ]
41
+	then
42
+		echo "" #for the dot printed with -n
43
+	fi
44
+	test_msg="$1"
45
+	test_status=1
46
+}
47
+
48
+err() {
49
+	msg="$(log ERR "$1" 1)"
50
+	test_msg="$1"
51
+	test_status=-1
52
+}
53
+
54
+success() {
55
+	msg="$(log OK "$1" 2)"
56
+	test_msg="$1"
57
+	test_status=0
58
+}
59
+
60
+#
61
+#	Tests & testcase functions
62
+#
63
+
64
+tc_name=""
65
+tc_infos=""
66
+tc_fail=0
67
+tc_run=0
68
+
69
+total_fail=0
70
+total_run=0
71
+
72
+test_msg=""
73
+test_status=0
74
+
75
+_test_setup() {
76
+	test_status=0
77
+	test_msg=""
78
+}
79
+
80
+TC_INIT() {
81
+	tc_name=$1
82
+	tc_infos=$2
83
+	tc_fail=0
84
+	tc_run=0
85
+
86
+	msg="Running testcase '$tc_name'"
87
+	if [ -n "$tc_infos" ]
88
+	then
89
+		msg="${msg} ($tc_infos)"
90
+	fi
91
+	if [ "$verbose" -gt 0 ]
92
+	then
93
+		logdate TESTCASE "$msg" 4
94
+	fi
95
+}
96
+
97
+TC_END() {
98
+	
99
+	if [ "$tc_fail" -gt 0 ]
100
+	then
101
+		if [ "$verbose" -gt 1 ]
102
+		then
103
+			logdate TESTCASE "-------------$tc_name END------------" 1
104
+		fi
105
+		logdate FAIL "Testcase '$tc_name' tests: ${tc_run} fails: $(col_set 1)${tc_fail}$(col_reset)" 1
106
+		if [ "$exit_on_fail" -ne 0 ]
107
+		then
108
+			CHECK_REPORT
109
+		fi
110
+	elif [ "$verbose" -eq 1 ]
111
+	then
112
+		echo "" #for the dot printed with -n
113
+		logdate TESTCASE "Testcase '$tc_name' tests:${tc_run} fails: $tc_fail" 2
114
+	elif [ "$verbose" -gt 1 ]
115
+	then
116
+		logdate TESTCASE "-------------$tc_name END------------" 4
117
+	fi
118
+	tc_name=""
119
+	tc_infos=""
120
+	tc_fail=0
121
+	tc_run=0
122
+}
123
+
124
+TC_RUN() {
125
+	_test_setup
126
+	cmd=$1
127
+	shift
128
+	args=""
129
+	case $#
130
+	in
131
+		1)$cmd "$1";;
132
+		2)$cmd "$1" "$2";;
133
+		3)$cmd "$1" "$2" "$3";;
134
+		4)$cmd "$1" "$2" "$3" "$4";;
135
+		*)fail "To many arguments for $cmd"
136
+	esac
137
+	#$cmd $args
138
+
139
+	tc_run=$(expr $tc_run + 1)
140
+	total_run=$(expr $total_run + 1)
141
+	if [ "$test_status" -ne 0 ]
142
+	then
143
+		err_type="FAIL"
144
+		if [ "$test_status" -lt 0 ]
145
+		then
146
+			err_type="ERR"
147
+		fi
148
+		tc_fail=$(expr $tc_fail + 1)
149
+		total_fail=$(expr $total_fail + 1)
150
+		logdate $err_type "$tc_name: $test_msg" 1 >&2
151
+	elif [ "$verbose" -gt 1 ]
152
+	then
153
+		logdate OK "$tc_name: $test_msg" 2
154
+	else
155
+		echo -n '.'
156
+	fi
157
+}
158
+
159
+CHECK_START() {
160
+	logdate STATUS "Starting tests" 3
161
+}
162
+
163
+CHECK_REPORT() {
164
+	if [ "$verbose" -eq 0 -a "$tc_fail" -eq "0" ]
165
+	then
166
+		echo "" # for dot printed with -n
167
+	fi
168
+	if [ "$verbose" -gt 0 ]
169
+	then
170
+		logdate STATUS "All tests done" 3
171
+	fi
172
+	if [ "$total_fail" -gt 0 ]
173
+	then
174
+		logdate FAIL "Summary : tests: ${total_run} fails: $(col_set 1)${total_fail}$(col_reset)" 1
175
+		exit 1
176
+	else
177
+		logdate OK "Summary : tests:${total_run} fails: $total_fail" 2
178
+		exit 0
179
+	fi
180
+}
181
+
182
+#
183
+#	HTTP/HTTPS tests
184
+#
185
+
186
+_check_http_status() {
187
+	# $1 url
188
+	# $2 status
189
+	# $* curl options
190
+	url=$1
191
+	shift 1
192
+	expt=$1
193
+	if [ -z "$1" ]
194
+	then
195
+		expt=200
196
+	fi
197
+	shift 1
198
+
199
+	status=$(curl -s -o /dev/null -w "%{http_code}" $* $url)
200
+	if [ "$status" -ne "$expt" ]
201
+	then
202
+		fail "Check http status $expt for $url : $status returned" 
203
+	elif [ "$verbose" -gt 0 ]
204
+	then
205
+		success "Check http status $status for $url"
206
+	fi
207
+}
208
+
209
+check_http_status() {
210
+	# $1 url
211
+	# $2 status
212
+	_check_http_status http://$1 $2
213
+}
214
+
215
+check_http_200() {
216
+	_check_http_status http://$1 200
217
+}
218
+
219
+check_https_status() {
220
+	# $1 url
221
+	# $2 status
222
+	_check_http_status https://$1 $2
223
+}
224
+
225
+check_https_200() {
226
+	_check_http_status https://$1 200
227
+}
228
+
229
+check_https_cert() {
230
+	# Check that SSL Cert is valid
231
+	# $1 URL
232
+	status=$(curl -s -o /dev/null -w "%{http_code}" https://$1)
233
+	rep=$?
234
+	if [ "$rep" -eq 0 ]
235
+	then
236
+		success "https://$1 cert verified"
237
+		return
238
+	fi
239
+	status=$(curl -k -s -o /dev/null -q "%{http_code}" https://$1)
240
+	rep=$?
241
+	if [ "$rep" -eq 0 ]
242
+	then
243
+		fail "https://$1 cert invalid"
244
+	else
245
+		err "Unable to curl https://$1"
246
+	fi
247
+}
248
+
249
+check_html_title() {
250
+	url="$1"
251
+	expt="$2"
252
+
253
+	tmpxsl=$(mktemp -t XSL.XXXXXXXXXXX)
254
+	echo '<?xml version="1.0"?>
255
+<xsl:stylesheet version="1.0"
256
+xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
257
+        <xsl:output method = "text"/>
258
+        <xsl:template match="/">
259
+                <xsl:value-of select="/html/head/title"/>
260
+        </xsl:template>
261
+</xsl:stylesheet>' > $tmpxsl
262
+
263
+	tmphtml=$(mktemp -t html.XXXXXXXXX)
264
+
265
+	curl --silent $url > $tmphtml
266
+	title=$(xsltproc --html --novalid $tmpxsl $tmphtml 2>/dev/null)
267
+	if [ "$?" -ne "0" ]
268
+	then
269
+		title=$(xsltproc --novalid $tmpxsl $tmphtml 2>/dev/null)
270
+	fi
271
+
272
+	if [ "$title" = "$expt" ]
273
+	then
274
+		success "$url HTML title is '$expt'"
275
+	else
276
+		fail "$url HTML title is '$title' but '$expt' expected"
277
+	fi
278
+
279
+	rm $tmpxsl $tmphtml 2>/dev/null
280
+}
281
+
282
+check_audiostream() {
283
+	# Uses mplayer to fetch 128Kb of stream
284
+	# $1 Stream URL
285
+	tmpfile=$(mktemp -t check_audiostream.XXXXXXXXX)
286
+	sz="128kb"
287
+	if [ "$verbose" -gt 1 ]
288
+	then
289
+		logdate INFO "$tc_name: Running mplayer on '$1' for $sz" 3
290
+	fi
291
+
292
+	mplayer -endpos $sz -ao pcm:file=$tmpfile "$1" 1>/dev/null 2>/dev/null
293
+	res=$?
294
+	bytes=$(du $tmpfile | cut -f1)
295
+	rm $tmpfile 2>/dev/null
296
+
297
+	if [ "$bytes" -lt 1024 ]
298
+	then
299
+		fail "mplayer retrieved ${bytes}B of stream instead of expected $sz"
300
+		return
301
+	fi
302
+	if [ "$res" -eq 0 ]
303
+	then
304
+		success "mplayer retrieved $sz of stream on $1"
305
+	else
306
+		fail "mplayer failed to retrieve stream on $1"
307
+	fi
308
+}
309
+
310
+check_ping() {
311
+	ns=$1
312
+	count=$2
313
+	if [ -z "$count" ]
314
+	then
315
+		count=5
316
+	fi
317
+	ping -i 0.2 -c $count $ns 2>/dev/null >/dev/null
318
+	res=$?
319
+	if [ "$res" -ne 0 ]
320
+	then
321
+		fail "unable to ping '$ns'"
322
+	else
323
+		success "successfully send $count ping to '$ns'"
324
+	fi
325
+}
326
+
327
+check_git_repo() {
328
+	tmpdir=$(mktemp -d -t check_git.XXXXXXXXX)
329
+	git clone $1 $tmpdir 2>/dev/null 1>/dev/null
330
+	res=$?
331
+	rm -Rf $tmpdir
332
+	if [ "$res" -ne 0 ]
333
+	then
334
+		fail "unable to clone git repo '$1'"
335
+	else
336
+		success "git repo '$1' cloned'"
337
+	fi
338
+}
339
+
340
+#
341
+# Jabber XMPP checks
342
+#
343
+
344
+__xmpp_probe() {
345
+	serv=$1
346
+	timeout=$2
347
+	type=$3
348
+
349
+	payload="<?xml version=\"1.0\"?>\n<stream:stream xmlns:stream=\"http://etherx.jabber.org/streams\" version=\"1.0\" xmlns=\"jabber:$type\" to=\"${1}\" xml:lang=\"en\" xmlns:xml=\"http://www.w3.org/XML/1998/namespace\">\n"
350
+	if [ "$verbose" -gt 2 ]
351
+	then
352
+		echo -e $payload | sed -e "s/^/$(datetime) [  DEBUG] Sent : /" >&2
353
+	fi
354
+	echo -e $payload
355
+	sleep $timeout
356
+}
357
+
358
+_xmpp_probe() {
359
+	serv=$1
360
+	port=$2
361
+	timeout=$3
362
+	type=$4
363
+
364
+	tmpres=$(mktemp -t xmpp_probe.XXXXXXXXX)
365
+
366
+	echo "$(__xmpp_probe $serv $timeout $type| nc -q2 $serv $port)</stream:stream>" | xmllint --format - > $tmpres
367
+	if [ "$verbose" -gt 2 ]
368
+	then
369
+		cat $tmpres | sed -e "s/^/$(datetime) [  DEBUG] Recv : /" >&2
370
+	fi
371
+	cat $tmpres
372
+	rm $tmpres
373
+}
374
+
375
+_check_xmpp_ns() {
376
+	ns1=$1
377
+	expt_ns=$2
378
+	expt_port=$3
379
+	dnsq="_xmpp-${4}._tcp.$1"
380
+	rep="$(dig $dnsq srv +short | cut -d" " -f3,4)"
381
+	if [ "$rep" = "$expt_port ${expt_ns}." ]
382
+	then
383
+		success "$dnsq = '$rep'"
384
+	else
385
+		fail "$dnsq = '$rep' but '$expt_port ${expt_ns}.' expected"
386
+	fi
387
+}
388
+
389
+check_xmpp_serv_ns() {
390
+	_check_xmpp_ns "$1" "$2" "$3" "server"
391
+}
392
+
393
+check_xmpp_client_ns() {
394
+	_check_xmpp_ns "$1" "$2" "$3" "client"
395
+}
396
+
397
+_check_xmpp_server() {
398
+	serv=$1
399
+	port=$2
400
+	timeout=$3
401
+	type=$4
402
+	
403
+	if [ -z "$port" ]
404
+	then
405
+		port=5222
406
+	fi
407
+	if [ -z "$timeout" ]
408
+	then
409
+		timeout=2
410
+	fi
411
+
412
+	if [ "$verbose" -gt 1 ]
413
+	then
414
+		tpe="client"
415
+		if [ "$type" = "server" ]
416
+		then
417
+			tpe="S2S"
418
+		fi
419
+		logdate INFO "$tc_name: Connecting to XMPP $serv $tpe port $port (timeout=${timeout}s)" 3
420
+	fi
421
+
422
+	stream=$(_xmpp_probe $serv $port $timeout $type| head -n2 | tail -n1)
423
+	if [ -z "$stream" ]
424
+	then
425
+		fail "Empty reply from $serv:$port"
426
+		return
427
+	fi
428
+
429
+	if [ "$type" = "client" ]
430
+	then
431
+		infos=$(echo $stream | sed -E 's/^<([^ ]+).* xmlns="([^"]+)".* from="([^"]+)" .*$/\1 \2 \3/')
432
+	else
433
+		infos="$(echo $stream | sed -E 's/^<([^ ]+).* xmlns="([^"]+)" .*$/\1 \2/') $serv"
434
+	fi
435
+
436
+	if [ "$(echo $infos | cut -d ' ' -f1,2)" = "stream:stream jabber:$type" ]
437
+	then
438
+		if [ "$(echo $infos | cut -d ' ' -f3)" = "$serv" ]
439
+		then
440
+			success "Successfully connected to XMPP $type $serv:$port"
441
+		else
442
+			fail "Server $serv:$port announce itself as $(echo $infos | cut -f3)"
443
+		fi
444
+	else
445
+		fail "Unexpected reply from $serv:$port : $infos"
446
+	fi
447
+}
448
+
449
+check_xmpp_server_client() {
450
+	_check_xmpp_server "$1" "$2" "$3" "client"
451
+}
452
+
453
+check_xmpp_server_s2s() {
454
+	_check_xmpp_server "$1" "$2" "$3" "server"
455
+}
456
+
457
+check_xmpp_ssl() {
458
+	serv=$1
459
+	port=$2
460
+	
461
+	if [ -z "$port" ]
462
+	then
463
+		port=5222
464
+	fi
465
+
466
+	openssl s_client -connect $serv:$port </dev/null -starttls xmpp >/dev/null 2>/dev/null
467
+	rep=$?
468
+	if [ "$rep" -eq 0 ]
469
+	then
470
+		success "Openssl successfully negociating XMPP ssl with $serv:$port"
471
+	else
472
+		fail "Openssl failed negociating XMPP ssl with $serv:$port"
473
+	fi
474
+}
475
+
476
+#
477
+#	SSH checks
478
+#
479
+
480
+check_ssh_nc() {
481
+	host=$1
482
+	port=$2
483
+	
484
+	if [ -z "$port" ]
485
+	then
486
+		port=22
487
+	fi
488
+
489
+	rep="$(nc -q1 z3.zered.net $port </dev/null)"
490
+	res=$?
491
+	if [ "$res" -ne "0" ]
492
+	then
493
+		fail "Netcat unable to connect to $host:$port"
494
+		return
495
+	fi
496
+	if echo $rep | grep "^SSH-2.0-OpenSSH" >/dev/null
497
+	then
498
+		success "OpenSSH replied on $host:$port"
499
+	else
500
+		fail "Bad replie from $host:$port : '$rep'"
501
+	fi
502
+}

Loading…
Cancel
Save