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
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
|
#!/usr/bin/env bash
updir="./termsend-test/out"
data="./termsend-test/data"
pidfile="./termsend-test/termsend.pid"
. ./mtest.sh
os="$(uname)"
if [ "${os}" = "Linux" ]
then
server="127.$((RANDOM % 256)).$((RANDOM % 256)).$((RANDOM % 254 + 2))"
else
server="127.0.0.1"
fi
## ==========================================================================
# _ __ ____
# ____ _____ (_)_ __ ____ _ / /_ ___ / __/__ __ ____ _____ _____
# / __ \ / ___// /| | / // __ `// __// _ \ / /_ / / / // __ \ / ___// ___/
# / /_/ // / / / | |/ // /_/ // /_ / __/ / __// /_/ // / / // /__ (__ )
# / .___//_/ /_/ |___/ \__,_/ \__/ \___/ /_/ \__,_//_/ /_/ \___//____/
#/_/
## ==========================================================================
start_termsend()
{
common_opts="-D -l7 -c -i61337 -a61338 -s1024 -t3 -m3 -dlocalhost -utermsend \
-gtermsend -P"${pidfile}" -q./termsend-test/termsend-query.log \
-p./termsend-test/termsend.log -T0 \
-o./termsend-test/out -b${server}"
mkdir -p ./termsend-test/out
if [ ${ssl_test} = "openssl" ]
then
../src/termsend ${common_opts} -I61339 -A61340 -k./test-server.key.pem \
-C./test-server.cert.pem -f./test-server.key.pass ${args}
else
../src/termsend ${common_opts} ${args}
fi
# wait for termsend to start
tries=0
while true
do
if grep "n/server initialized and started" ./termsend-test/termsend.log \
>/dev/null
then
break
fi
tries=$(( tries + 1 ))
if [ ${tries} -eq 600 ]
then
echo "termsend failed to start" >> /tmp/termsend
exit 1
fi
sleep 0.1
done
}
## ==========================================================================
## ==========================================================================
mt_prepare_test()
{
start_termsend
}
## ==========================================================================
## ==========================================================================
mt_cleanup_test()
{
pid="$(cat "${pidfile}")"
kill -15 "${pid}"
tries=0
echo "killing termsend" >> /tmp/termsend
while true
do
if ! kill -s 0 "${pid}" 2>/dev/null
then
# termsend died
echo "it died" >> /tmp/termsend
break
fi
tries=$(( tries + 1))
if [ ${tries} -eq 5 ]
then
echo "could kill 5 times" >> /tmp/termsend
exit 1
break
fi
sleep 1
done
rm -rf ./termsend-test
}
## ==========================================================================
# Parses output from termsend server to get generated file name where data
# was stored in
## ==========================================================================
get_file()
{
tail -n1 | rev | cut -d/ -f-1 | rev
}
## ==========================================================================
# sends content of file from path $1, to termsend server and returns
# response from the server. Tries for up to 5 seconds
## ==========================================================================
termsend_nc()
{
port="${1}"
file="${2}"
append_termsend="${3}"
if [ -z "${append_termsend}" ]
then
append_termsend=1
fi
echo "sending file $1 append $2" >> /tmp/termsend
tries=0
while true
do
printf "" > "${file}.ncerr"
if [ ${append_termsend} -eq 1 ]
then
out="$(cat "${file}" | { cat -; echo 'termsend'; } | \
${nc} -v ${server} ${port} 2>"${file}.ncerr")"
else
out="$(cat "${file}" | ${nc} -v ${server} ${port} \
2>"${file}.ncerr")"
fi
if egrep -i "open|succe|received" "${file}.ncerr"
then
# nc was successfull
echo "nc allright" >> /tmp/termsend
echo $out >> /tmp/termsend
echo "${out}"
return 0
fi
if [ ${tries} -eq 5 ]
then
# 5 seconds passed, server did not start, something
# is wrong, abort, abort
echo "nc fucked 5 times" >> /tmp/termsend
return 1
fi
# nc failed, probably connection refused, let's try again
echo "nc fucked" >> /tmp/termsend
cat ${file}.ncerr >> /tmp/termsend
tries=$(( tries + 1 ))
sleep 1
done
}
termsend_socat()
{
port="${1}"
file="${2}"
append_termsend="${3}"
if [ -z "${append_termsend}" ]
then
append_termsend=1
fi
if [ "${ssl_test}" = "openssl" ]
then
socat_cmd="socat -t30 - OPENSSL:${server}:${port},verify=0"
else
socat_cmd="socat -t30 - TCP:${server}:${port}"
fi
echo "sending file $1 append $2" >> /tmp/termsend
tries=0
while true
do
if [ ${append_termsend} -eq 1 ]
then
out="$(cat "${file}" | { cat -; echo 'termsend'; } | \
${socat_cmd})"
else
out="$(cat "${file}" | ${socat_cmd})"
fi
if [ ${?} -eq 0 ]
then
# socat was successfull
echo "socat allright" >> /tmp/termsend
echo $out >> /tmp/termsend
echo "${out}"
return 0
fi
if [ ${tries} -eq 5 ]
then
# 5 seconds passed, server did not start, something
# is wrong, abort, abort
echo "socat fucked 5 times" >> /tmp/termsend
return 1
fi
# socat failed, probably connection refused, let's try again
echo "socat fucked" >> /tmp/termsend
tries=$(( tries + 1 ))
sleep 1
done
}
termsend()
{
case ${prog_test}-${ssl_test} in
nc-none)
port=61337
if [ ${timed_test} -eq 1 ]; then port=61338; fi
termsend_nc ${port} ${@}
;;
socat-none)
port=61337
if [ ${timed_test} -eq 1 ]; then port=61338; fi
termsend_socat ${port} ${@}
;;
socat-openssl)
port=61339
if [ ${timed_test} -eq 1 ]; then port=61340; fi
termsend_socat ${port} ${@}
;;
*)
echo "invalid termsend arguments: ${ssl_test}${have_nc}"
;;
esac
}
## ==========================================================================
# Generate random string. Function appends '\n' automatically, to generate
# string with 128 byte (including \n") pass 127 as first argument.
#
# $1 - number of characters to generate (excluding '\n'
## ==========================================================================
randstr()
{
cat /dev/urandom | tr -dc 'a-zA-Z0-9' | tr -d '\0' | fold -w $1 | head -n 1
}
## ==========================================================================
# Generates random binary data
#
# $1 - number of bytes to generate
## ==========================================================================
randbin()
{
dd if=/dev/urandom bs=1 count=${1} 2>/dev/null
}
## ==========================================================================
# verifyes whether data upload to server was successfull. This function is
# thread-safe.
## ==========================================================================
multi_thread_check()
{
fname="$(randstr 120)"
randstr 128 > "${data}.${fname}"
out="$(termsend "${data}.${fname}" | tail -n1)"
if [ "${out}" = "all upload slots are taken, try again later" ]
then
touch "${1}.test_check"
return 0
elif [[ "${out}" = "localhost/"* ]]
then
file="$(echo "${out}" | rev | cut -d/ -f-1 | rev)"
mt_fail "diff ${updir}/${file} ${data}.${fname}"
touch "${1}.test_check"
return $?
else
if [ -z "${out}" ]
then
touch "${1}.test_check"
return
fi
echo "something weird received: '${out}'"
mt_fail false
touch "${1}.test_check"
return 0
fi
}
## ==========================================================================
# __ __
# / /_ ___ _____ / /_ _____
# / __// _ \ / ___// __// ___/
# / /_ / __/(__ )/ /_ (__ )
# \__/ \___//____/ \__//____/
#
## ==========================================================================
test_is_running()
{
mt_fail "kill -s 0 `cat ./termsend-test/termsend.pid`"
# sleep for 1 second, since it is possible that we run the test
# so quickly, that in cleanup kill SIGTERM is emited to early
# and test fails. This is especially true when using compiler
# sanitizers.
sleep 1
}
## ==========================================================================
## ==========================================================================
test_send_string()
{
randstr 128 > ${data}
file="$(termsend "${data}" | get_file)"
mt_fail "diff ${updir}/${file} ${data}"
}
## ==========================================================================
## ==========================================================================
test_threaded()
{
max_wait=60
for i in $(seq 1 1 16)
do
multi_thread_check ${i} &
done
# we expect 16 files to be created, finish test once all 16 files
# are present
i=0
while true
do
num_files="$(ls -1 *.test_check | wc -l)"
if [ ${num_files} -eq 16 ]
then
# all files generated, we're good
break
fi
if [ ${i} -gt ${max_wait} ]
then
mt_fail "[ \"threaded test timedout\" = \"error force\" ]"
break
fi
i=$(( i + 1 ))
sleep 1
done
rm *.test_check
}
## ==========================================================================
## ==========================================================================
test_send_string_full()
{
randstr 1023 > ${data}
file="$(termsend "${data}" | get_file)"
mt_fail "diff ${updir}/${file} ${data}"
}
## ==========================================================================
## ==========================================================================
test_send_string_too_big()
{
randstr 1337 > ${data}
out="$(termsend "${data}" | tail -n1)"
mt_fail "[ \"${out}\" == \"file too big, max length is 1024 bytes\" ]"
}
## ==========================================================================
## ==========================================================================
test_send_bin()
{
randbin 128 > ${data}
file="$(termsend "${data}" | get_file)"
mt_fail "diff ${updir}/${file} ${data}"
}
## ==========================================================================
## ==========================================================================
test_send_bin_full()
{
randbin 1024 > ${data}
file="$(termsend ${data} | get_file)"
mt_fail "diff ${updir}/${file} ${data}"
}
## ==========================================================================
## ==========================================================================
test_send_bin_too_big()
{
randbin 1337 > "${data}"
out="$(termsend "${data}" | tail -n1)"
mt_fail "[ \"${out}\" == \"file too big, max length is 1024 bytes\" ]"
}
## ==========================================================================
## ==========================================================================
test_send_empty_with_termsend()
{
truncate -s0 "${data}"
out="$(termsend "${data}" | tail -n1)"
mt_fail "[ \"${out}\" == \"no data has been sent\" ]"
}
## ==========================================================================
## ==========================================================================
test_send_empty()
{
truncate -s0 "${data}"
out="$(termsend "${data}" 0 | ${tailn} | tr "\n" ".")"
if [ "${prog_test}" = "socat" ] || \
[ "${prog_test}" = "nc" -a "${nctype}" = "nmap" ]
then
mt_fail "[ \"${out}\" == \"no data has been sent.\" ]"
else
mt_fail "[[ \"$out\" == \"disconnected due to inactivity for 3 seconds, did you forget to append termination string\"* ]]"
fi
}
## ==========================================================================
## ==========================================================================
test_send_without_termsend()
{
randbin 128 > "${data}"
if [ "${prog_test}" = "socat" ] || \
[ "${prog_test}" = "nc" -a "${nctype}" = "nmap" ]
then
# socat and nmap version of nc nicely close connection when
# all data is sent, so error will not happen here
file="$(termsend ${data} | get_file)"
mt_fail "diff ${updir}/${file} ${data}"
else
out="$(termsend "${data}" 0 | ${tailn} | tr "\n" ".")"
mt_fail "[[ \"$out\" == \"disconnected due to inactivity for 3 seconds, did you forget to append termination string\"* ]]"
fi
}
## ==========================================================================
## ==========================================================================
test_timed_upload()
{
randbin 128 > "${data}"
file="$(termsend "${data}" 0 | get_file)"
mt_fail "diff ${updir}/${file} ${data}"
}
## ==========================================================================
## ==========================================================================
test_timed_upload_empty()
{
truncate -s0 "${data}"
out="$(termsend "${data}" 0 | ${tailn} | tr "\n" ".")"
mt_fail "echo \"${out}\" | grep \"no data has been sent.\""
}
## ==========================================================================
## ==========================================================================
test_timed_upload_full()
{
randbin 1024 > ${data}
file="$(termsend "${data}" 0 | get_file)"
mt_fail "diff ${updir}/${file} ${data}"
}
## ==========================================================================
## ==========================================================================
test_timed_upload_too_big()
{
randbin 1337 > ${data}
out="$(termsend "${data}" 0 | tail -n1)"
mt_fail "[ \"$out\" == \"file too big, max length is 1024 bytes\" ]"
}
## ==========================================================================
## ==========================================================================
test_timed_upload_with_termsend()
{
randbin 128 > "${data}"
file="$(termsend "${data}" | get_file)"
mt_fail "diff $updir/$file $data"
}
## ==========================================================================
## ==========================================================================
test_totally_random()
{
for i in `seq 1 1 128`
do
numbytes=$(((RANDOM % 2048) + 1))
finish=$((RANDOM % 32))
if [ $finish -eq 0 ]
then
# send data but don't send ending termsend\n
if [ $numbytes -gt 1024 ]
then
numbytes=512
fi
randbin $numbytes > $data
if [ "${prog_test}" = "socat" ] || \
[ "${prog_test}" = "nc" -a "${nctype}" = "nmap" ]
then
# socat and nmap version of nc nicely close connection when
# all data is sent, so error will not happen here
file="$(termsend ${data} | get_file)"
mt_fail "diff ${updir}/${file} ${data}"
else
out="$(termsend ${data} 0 | ${tailn} | tr "\n" ".")"
mt_fail "[[ \"$out\" == \"disconnected due to inactivity for 3 seconds, did you forget to append termination string\"* ]]"
fi
else
randbin $numbytes > $data
if [ $numbytes -gt 1024 ]
then
out="$(termsend "${data}" | tail -n1)"
if [ ! -z "$out" ]
then
mt_fail "[ \"$out\" == \"file too big, max length is 1024 bytes\" ]"
fi
else
file="$(termsend "${data}" | get_file)"
mt_fail "diff $updir/$file $data"
fi
fi
done
}
## ==========================================================================
## ==========================================================================
check_sanitizer_logs()
{
if grep "==ERROR: " ${0}.log
then
# succes means ERROR appears
mt_fail "[ \"sanitizer log contains ERROR\" = \"error force\" ]"
fi
}
## ==========================================================================
# _ ____ __ __
# (_)____ / __/____ ____ _ ____ _ / /_ / /_ ___ _____
# / // __ \ / /_ / __ \ / __ `// __ `// __// __ \ / _ \ / ___/
# / // / / // __// /_/ / / /_/ // /_/ // /_ / / / // __// /
# /_//_/ /_//_/ \____/ \__, / \__,_/ \__//_/ /_/ \___//_/
# /____/
## ==========================================================================
have_nc=1
if [ "${os}" = "SunOS" ]
then
nc="nc -F"
tailn="tail -n2"
else
nc="nc"
tailn="tail -n1"
fi
if nc -v 2>&1 | grep "nmap.org" > /dev/null
then
nctype=nmap
else
nctype=hobbit
fi
have_socat=0
if type socat > /dev/null
then
have_socat=1
fi
have_ssl=0
if ../src/termsend -v | grep +ssl > /dev/null
then
have_ssl=1
fi
## ==========================================================================
# __ __ __ _
# / /_ ___ _____ / /_ ___ _ __ ___ _____ __ __ / /_ (_)____ ____
# / __// _ \ / ___// __/ / _ \ | |/_// _ \ / ___// / / // __// // __ \ / __ \
#/ /_ / __/(__ )/ /_ / __/_> < / __// /__ / /_/ // /_ / // /_/ // / / /
#\__/ \___//____/ \__/ \___//_/|_| \___/ \___/ \__,_/ \__//_/ \____//_/ /_/
#
## ==========================================================================
run_tests()
{
timed_test=0
mt_run_named test_is_running "test_is_running-${prog_test}-${ssl_test}"
mt_run_named test_send_string "test_send_string-${prog_test}-${ssl_test}"
mt_run_named test_send_string_full "test_send_string_full-${prog_test}-${ssl_test}"
mt_run_named test_send_string_too_big "test_send_string_too_big-${prog_test}-${ssl_test}"
mt_run_named test_send_bin "test_send_bin-${prog_test}-${ssl_test}"
mt_run_named test_send_bin_full "test_send_bin_full-${prog_test}-${ssl_test}"
mt_run_named test_send_bin_too_big "test_send_bin_too_big-${prog_test}-${ssl_test}"
mt_run_named test_send_without_termsend "test_send_without_termsend-${prog_test}-${ssl_test}"
mt_run_named test_send_empty_with_termsend "test_send_empty_with_termsend-${prog_test}-${ssl_test}"
mt_run_named test_send_empty "test_send_empty-${prog_test}-${ssl_test}"
mt_run_named test_threaded "test_threaded-${prog_test}-${ssl_test}"
mt_run_named test_totally_random "test_totally_random-${prog_test}-${ssl_test}"
timed_test=1
mt_run_named test_timed_upload "test_timed_upload-${prog_test}-${ssl_test}"
mt_run_named test_timed_upload_empty "test_timed_upload_empty-${prog_test}-${ssl_test}"
mt_run_named test_timed_upload_full "test_timed_upload_full-${prog_test}-${ssl_test}"
mt_run_named test_timed_upload_too_big "test_timed_upload_too_big-${prog_test}-${ssl_test}"
mt_run_named test_timed_upload_with_termsend "test_timed_upload_with_termsend-${prog_test}-${ssl_test}"
}
rm -rf ./termsend-test
ssl_test=none
prog_test=nc
run_tests
if [ ${have_socat} -eq 1 ]
then
ssl_test=none
prog_test=socat
run_tests
fi
if [ ${have_socat} -eq 1 ] && [ ${have_ssl} -eq 1 ]
then
ssl_test=openssl
prog_test=socat
run_tests
fi
# last but not least, when running sanitizer tests, our test suite
# will succed even if sanitizer reports memory leak or write out of
# bound, because usually such errors does not crash app but are
# rather exploitable security issues. So we grep log file for
# sanitizer errors and test fail when there was any error.
unset mt_prepare_test
unset mt_cleanup_test
mt_run check_sanitizer_logs
mt_return
|