-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.sh
executable file
·19365 lines (18543 loc) · 604 KB
/
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
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
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#! /usr/bin/env bash
# source: test.sh
# Copyright Gerhard Rieger and contributors (see file CHANGES)
# Published under the GNU General Public License V.2, see file COPYING
# perform lots of tests on socat
# this script uses functions; you need a shell that supports them
# you can pass general options to socat: export OPTS="-d -d -d -d -lu"
# you can eg strace socat with: export TRACE="strace -v -tt -ff -D -x -s 1024 -o /tmp/$USER/socat.strace"
#set -vx
#TODO: Add options for interface, broadcast-interface
[ -z "$USER" ] && USER="$LOGNAME" # HP-UX
if [ -z "$TMPDIR" ]; then
if [ -z "$TMP" ]; then
TMP=/tmp
fi
TMPDIR="$TMP"
fi
#E=-e # Linux
if [ $(echo "x\c") = "x" ]; then E=""
elif [ $(echo -e "x\c") = "x" ]; then E="-e"
else
echo "cannot suppress trailing newline on echo" >&2
exit 1
fi
ECHO="echo $E"
PRINTF="printf"
usage() {
$ECHO "Usage: $0 <options> [<test-spec> ...]"
$ECHO "options:"
$ECHO "\t-h \t\tShow this help"
$ECHO "\t-t <sec> \tBase for timeouts in seconds, default: 0.1"
$ECHO "\t-v \t\tBe more verbose, show failed commands"
$ECHO "\t-n <num> \tOnly perform test with given number"
$ECHO "\t-N <num> \tOnly perform tests starting with given number"
$ECHO "\t-C \t\tClear/remove left over certificates from previous runs"
$ECHO "\t-x \t\tShow commands executed, even when test succeeded"
#$ECHO "\t-d \t\tShow log output of commands, even when they did not fail"
$ECHO "\t--internet \tAllow tests that send packets to Internet"
$ECHO "\t--expect-fail N1,N2,... \tIgnore failure of these tests"
$ECHO "\ttest-spec \Number of test or name of test"
$ECHO "Contents of environment variable OPTS are passed to Socat invocations, e.'g:"
$ECHO "OPTS=\"-d -d -d -d -lu\" ./test.sh"
$ECHO "TRACE=\"strace -tt -v\" Use trace,valgrind etc.on socat"
$ECHO "SOCAT=/path/to/socat \tselect socat executable for test"
$ECHO "FILAN=... PROCAN=..."
$ECHO "Find the tests' stdout,stderr,diff in $TMPDIR/$USER/\$PID"
}
val_t=0.1
NUMCOND=true
#NUMCOND="test \$N -gt 70"
VERBOSE=
DEBUG=
INTERNET=
OPT_EXPECT_FAIL= EXPECT_FAIL=
while [ "$1" ]; do
case "X$1" in
X-h) usage; exit 0 ;;
X-d) DEBUG="1" ;;
X-t?*) val_t="${1#-t}" ;;
X-t) shift; val_t="$1" ;;
X-v) VERBOSE=1 ;; # show commands
X-n?*) NUMCOND="test \$N -eq ${1#-n}" ;;
X-n) shift; NUMCOND="test \$N -eq $1" ;;
X-N?*) NUMCOND="test \$N -gt ${1#-N}" ;;
X-N) shift; NUMCOND="test \$N -ge $1" ;;
X-C) rm -f testcert*.conf testcert.dh testcli*.* testsrv*.* ;;
X--internet|X-internet) INTERNET=1 ;; # allow access to 3rd party Internet hosts
X--expect-fail|X-expect-fail) OPT_EXPECT_FAIL=1; shift; EXPECT_FAIL="$1" ;;
X-*) echo "Unknown option \"$1\"" >&2
usage >&2
exit 1 ;;
*) break;
esac
shift
done
debug=$DEBUG
opt_t="-t $val_t"
UNAME=`uname`
UNAME_R=`uname -r`
#MICROS=100000
case "X$val_t" in
X*.???????*) S="${val_t%.*}"; uS="${val_t#*.}"; uS="${uS:0:6}" ;;
X*.*) S="${val_t%.*}"; uS="${val_t#*.}"; uS="${uS}000000"; uS="${uS:0:6}" ;;
X*) S="${val_t}"; uS="000000" ;;
esac
MICROS=${S}${uS}
MICROS=${MICROS##0000}; MICROS=${MICROS##00}; MICROS=${MICROS##0}
#echo MICROS=$MICROS >&2
#
_MICROS=$((MICROS+999999)); SECONDs="${_MICROS%??????}"
[ -z "$SECONDs" ] && SECONDs=0
withroot=0 # perform privileged tests even if not run by root
[ -z "$SOCAT" ] && SOCAT="./socat"
if ! [ -x "$SOCAT" ] && ! type $SOCAT >/dev/null 2>&1; then
echo "$SOCAT does not exist" >&2; exit 1;
fi
if [ "$SOCAT" = socat ]; then
SOCAT=$(type -p socat) || SOCAT=$(which socat)
fi
#echo $SOCAT
if [ -z "$PROCAN" ]; then if test -x ./procan; then PROCAN="./procan"; elif type procan >/dev/null 2>&1; then PROCAN=procan; elif test -x ${SOCAT%/*}/procan; then PROCAN=${SOCAT%/*}/procan; else PROCAN=false; fi; fi
if [ -z "$FILAN" ]; then if test -x ./filan; then FILAN="./filan"; elif ! type filan >/dev/null 2>&1; then FILAN=filan; elif test -x ${SOCAT%/*}/filan; then FILAN=${SOCAT%/*}/filan; else FILAN=false; fi; fi
#PATH=$PATH:/opt/freeware/bin
#PATH=$PATH:/usr/local/ssl/bin
PATH=$PATH:/sbin # RHEL6:ip
case "$0" in
*/*) PATH="${0%/*}:$PATH"
esac
#OPENSSL_RAND="-rand /dev/egd-pool"
#SOCAT_EGD="egd=/dev/egd-pool"
MISCDELAY=1
OPTS="$opt_t $OPTS"
opts="$OPTS"
TESTS="$*"; export TESTS
if ! SOCAT_MAIN_WAIT= $SOCAT -V >/dev/null 2>&1; then
echo "Failed to execute $SOCAT, exiting" >&2
exit 1
fi
SOCAT_VERSION=$(SOCAT_MAIN_WAIT= $SOCAT -V |head -n 2 |tail -n 1 |sed 's/.* \([0-9][1-9]*\.[0-9][0-9]*\.[0-9][^[:space:]]*\).*/\1/')
if [ -z "$SOCAT_VERSION" ]; then
echo "Warning: failed to retrieve Socat version" >&2
fi
if type ip >/dev/null 2>&1; then
if ip -V |grep -q -i -e "^ip utility, iproute2-" -e BusyBox; then
IP=$(type -p ip)
else
unset IP
fi
fi
if type ss >/dev/null 2>&1; then
# on Ubuntu-10 ss has differing output format
if ss -V |grep -q "^ss utility, iproute2-[2-6]"; then
SS=$(type -p ss)
else
unset SS
fi
fi
# for some tests we need a network interface
if type ip >/dev/null 2>&1; then
INTERFACE=$(ip r get 8.8.8.8 |grep ' dev ' |head -n 1 |sed "s/.*dev[[:space:]][[:space:]]*\([^[:space:]][^[:space:]]*\).*/\1/")
else
case "$UNAME" in
Linux)
if [ "$IP" ]; then
INTERFACE="$($IP route get 8.8.8.8 |grep ' dev ' |sed -e 's/.* dev //' -e 's/ .*//')"
else
INTERFACE="$(netstat -rn |grep -e "^default" -e "^0\.0\.0\.0" |awk '{print($8);}')"
fi ;;
FreeBSD) INTERFACE="$(netstat -rn |grep -e "^default" -e "^0\.0\.0\.0" |awk '{print($4);}')" ;;
*) INTERFACE="$(netstat -rn |grep -e "^default" -e "^0\.0\.0\.0" |awk '{print($4);}')" ;;
esac
fi
MCINTERFACE=$INTERFACE
[ -z "$MCINTERFACE" ] && MCINTERFACE=lo # !!! Linux only - and not always
#LOCALHOST=192.168.58.1
LOCALHOST=localhost # attention: on FreeBSD-10 localhost resolves primarily to IPv6
LOCALHOST4=127.0.0.1
LOCALHOST6="[::1]"
#PROTO=$(awk '{print($2);}' /etc/protocols |sort -n |tail -n 1)
#PROTO=$(($PROTO+1))
PROTO=$((144+RANDOM/2048))
_PORT=12001
SOURCEPORT=2002
REUSEADDR=reuseaddr # use this with LISTEN addresses and bind options
# get some system constants for use in tests
SOCK_DGRAM="$($PROCAN -c |grep "^#define[[:space:]]*SOCK_DGRAM[[:space:]]" |cut -d' ' -f3)"
FOPEN_MAX=$($PROCAN -c 2>/dev/null |grep '^#define[ ][ ]*FOPEN_MAX' |awk '{print($3);}')
PF_INET6="$($PROCAN -c |grep "^#define[[:space:]]*PF_INET6[[:space:]]" |cut -d' ' -f3)"
TIOCEXCL="$($PROCAN -c |grep "^#define[[:space:]]*TIOCEXCL[[:space:]]" |cut -d' ' -f3)"
SOL_SOCKET="$($PROCAN -c |grep "^#define[[:space:]]*SOL_SOCKET[[:space:]]" |cut -d' ' -f3)"
SO_REUSEADDR="$($PROCAN -c |grep "^#define[[:space:]]*SO_REUSEADDR[[:space:]]" |cut -d' ' -f3)"
TCP_MAXSEG="$($PROCAN -c |grep "^#define[[:space:]]*TCP_MAXSEG[[:space:]]" |cut -d' ' -f3)"
SIZE_T=$($PROCAN |grep "^[^[:space:]]*size_t" |awk '{print($3);}')
#AI_ADDRCONFIG=; if [ "$($SOCAT -hhh |grep ai-addrconfig)" ]; then AI_ADDRCONFIG="ai-addrconfig=0"; fi
# SSL certificate contents
TESTCERT_CONF=testcert.conf
TESTCERT6_CONF=testcert6.conf
TESTALT_CONF=testalt.conf
#
TESTCERT_COMMONNAME="$LOCALHOST"
TESTCERT_COMMONNAME6="$LOCALHOST6"
TESTCERT_COUNTRYNAME="XY"
TESTCERT_LOCALITYNAME="Lunar Base"
TESTCERT_ORGANIZATIONALUNITNAME="socat"
TESTCERT_ORGANIZATIONNAME="dest-unreach"
TESTCERT_SUBJECT="C = $TESTCERT_COUNTRYNAME, CN = $TESTCERT_COMMONNAME, O = $TESTCERT_ORGANIZATIONNAME, OU = $TESTCERT_ORGANIZATIONALUNITNAME, L = $TESTCERT_LOCALITYNAME"
TESTCERT_ISSUER="C = $TESTCERT_COUNTRYNAME, CN = $TESTCERT_COMMONNAME, O = $TESTCERT_ORGANIZATIONNAME, OU = $TESTCERT_ORGANIZATIONALUNITNAME, L = $TESTCERT_LOCALITYNAME"
RSABITS=2048 # Ubuntu-20.04 with OpenSSL-1.1.1f does not work with 1024 nor 1536
DSABITS=2048
cat >$TESTCERT_CONF <<EOF
prompt=no
[ req ]
default_bits = $RSABITS
distinguished_name=Test
[ Test ]
countryName=$TESTCERT_COUNTRYNAME
commonName=$TESTCERT_COMMONNAME
O=$TESTCERT_ORGANIZATIONNAME
OU=$TESTCERT_ORGANIZATIONALUNITNAME
L=$TESTCERT_LOCALITYNAME
EOF
cat >$TESTCERT6_CONF <<EOF
prompt=no
[ req ]
default_bits = $RESBITS
distinguished_name=Test
[ Test ]
countryName=$TESTCERT_COUNTRYNAME
commonName=$TESTCERT_COMMONNAME6
O=$TESTCERT_ORGANIZATIONNAME
OU=$TESTCERT_ORGANIZATIONALUNITNAME
L=$TESTCERT_LOCALITYNAME
EOF
cat >$TESTALT_CONF <<EOF
# config for generation of self signed certificate with IP addresses in
# SubjectAltNames
prompt=no
[ req ]
default_bits = $RSABITS
distinguished_name = subject
x509_extensions = x509_ext
[ subject ]
countryName=$TESTCERT_COUNTRYNAME
commonName=servername
O=$TESTCERT_ORGANIZATIONNAME
OU=$TESTCERT_ORGANIZATIONALUNITNAME
L=$TESTCERT_LOCALITYNAME
[ x509_ext ]
subjectAltName = @alternate_names
[ alternate_names ]
DNS.1 = localhost
DNS.2 = localhost4
DNS.3 = localhost6
IP.1 = 127.0.0.1
IP.2 = ::1
EOF
# clean up from previous runs
rm -f testcli.{crt,key,pem}
rm -f testsrv.{crt,key,pem}
rm -f testcli6.{crt,key,pem}
rm -f testsrv6.{crt,key,pem}
rm -f testalt.{crt,key,pem}
CAT="cat"
OD_C="od -c"
toupper () {
case ${BASH_VERSION:0:1} in
[1-3]) echo "$@" |tr a-z A-Z ;;
[4-9]) echo "${@^^*}" ;;
esac
}
tolower () {
case ${BASH_VERSION:0:1} in
[1-3]) echo "$@" |tr A-Z a-z ;;
[4-9]) echo "${@,,*}" ;;
esac
}
# precision sleep; takes seconds with fractional part
psleep () {
local T="$1"
[ "$T" = 0 ] && T=0.000002
$SOCAT -T "$T" pipe pipe 2>/dev/null
}
# time in microseconds to wait in some situations
if ! type usleep >/dev/null 2>&1 ||
usleep 0 2>&1 |grep -q deprecated; then
usleep () {
local n="$1"
case "$n" in
*???????) S="${n%??????}"; uS="${n:${#n}-6}" ;;
*) S=0; uS="00000$n"; uS="${uS:${#uS}-6}" ;;
esac
$SOCAT -T "$S.$uS" pipe pipe 2>/dev/null
}
fi
#USLEEP=usleep
# A sleep with configurable clocking ($vat_t)
# val_t should be at least the time that a Socat invocation, no action, and
# termination takes
relsleep () {
usleep $(($1*MICROS))
}
if type ping6 >/dev/null 2>&1; then
PING6=ping6
else
PING6="ping -6"
fi
F_n="%3d" # format string for test numbers
export LC_ALL=C # for timestamps format...
export LANG=C
export LANGUAGE=C # knoppix
case "$UNAME" in
HP-UX|OSF1)
echo "$SOCAT -u stdin stdout" >cat.sh
chmod a+x cat.sh
CAT=./cat.sh
;;
SunOS)
# /usr/bin/tr doesn't handle the a-z range syntax (needs [a-z]), use
# /usr/xpg4/bin/tr instead
alias tr=/usr/xpg4/bin/tr
;;
*)
CAT="cat"
;;
esac
# some OSes need special options
case "$UNAME" in
#HP-UX)
# # on HP-UX, the default options (below) hang some tests (former 14, 15)
# PTYOPTS=
# PTYOPTS2=
# ;;
SunOS)
PTYOPTS="perm=600"
PTYOPTS2="echo=0,opost=0"
;;
*)
PTYOPTS="echo=0,opost=0"
#PTYOPTS2="raw,echo=0"
PTYOPTS2="cfmakeraw"
#PTYOPTS2="rawer"
;;
esac
# for some tests we need an unprivileged user id to su to
if [ "$SUDO_USER" ]; then
SUBSTUSER="$SUDO_USER"
else
SUBSTUSER="$(grep -v '^[^:]*:^[^:]*:0:' /etc/passwd |tail -n 1 |cut -d: -f1)"
fi
if [ -z "$SS" ]; then
# non-root users might miss ifconfig in their path
case "$UNAME" in
AIX) IFCONFIG=/usr/sbin/ifconfig ;;
FreeBSD) IFCONFIG=/sbin/ifconfig ;;
HP-UX) IFCONFIG=/usr/sbin/ifconfig ;;
Linux) IFCONFIG=/sbin/ifconfig ;;
NetBSD)IFCONFIG=/sbin/ifconfig ;;
OpenBSD)IFCONFIG=/sbin/ifconfig ;;
OSF1) IFCONFIG=/sbin/ifconfig ;;
SunOS) IFCONFIG=/sbin/ifconfig ;;
Darwin)IFCONFIG=/sbin/ifconfig ;;
DragonFly) IFCONFIG=/sbin/ifconfig ;;
*) IFCONFIG=/sbin/ifconfig ;;
esac
fi
# need output like "644"
case "$UNAME" in
Linux) fileperms() { stat -L --print "%a\n" "$1" 2>/dev/null; } ;;
FreeBSD) fileperms() { stat -L -x "$1" |grep ' Mode:' |sed 's/.* Mode:[[:space:]]*([0-9]\([0-7][0-7][0-7]\).*/\1/'; } ;;
*) fileperms() {
local p s=0 c
p="$(ls -l -L "$1" |awk '{print($1);}')"
p="${p:1:9}"
while [ "$p" ]; do c=${p:0:1}; p=${p:1}; [ "x$c" == x- ]; let "s=2*s+$?"; done
printf "%03o\n" $s;
} ;;
esac
# need user (owner) of filesystem entry
case "$UNAME" in
Linux) fileuser() { stat -L --print "%U\n" "$1" 2>/dev/null; } ;;
FreeBSD) fileuser() { ls -l "$1" |awk '{print($3);}'; } ;;
*) fileuser() { ls -l "$1" |awk '{print($3);}'; } ;;
esac
if2addr4() {
local IF="$1"
if [ "$IP" ]; then
$IP address show dev "$IF" |grep "inet " |sed -e "s/.*inet //" -e "s/ .*//"
else
$IFCONFIG "$BROADCASTIF" |grep 'inet ' |awk '{print($2);}' |cut -d: -f2
fi
}
if2bc4() {
local IF="$1"
if [ "$IP" ]; then
$IP address show dev "$IF" |grep ' inet .* brd ' |awk '{print($4);}'
else
$IFCONFIG "$IF" |grep 'broadcast ' |sed 's/.*broadcast/broadcast/' |awk '{print($2);}'
fi
}
# for some tests we need a second local IPv4 address
case "$UNAME" in
Linux)
if [ "$IP" ]; then
BROADCASTIF=$($IP r get 8.8.8.8 |grep ' dev ' |sed 's/.*\<dev[[:space:]][[:space:]]*\([a-z0-9][a-z0-9]*\).*/\1/')
else
BROADCASTIF=$(route -n |grep '^0.0.0.0 ' |awk '{print($8);}')
fi
[ -z "$BROADCASTIF" ] && BROADCASTIF=eth0
SECONDADDR=127.1.0.1
SECONDMASK=255.255.0.0
BCADDR=127.255.255.255
BCIFADDR=$(if2addr4 $BROADCASTIF) ;;
FreeBSD|NetBSD|OpenBSD)
MAINIF=$($IFCONFIG -a |grep '^[a-z]' |grep -v '^lo0: ' |head -1 |cut -d: -f1)
BROADCASTIF="$MAINIF"
SECONDADDR=$($IFCONFIG "$BROADCASTIF" |grep 'inet ' |awk '{print($2);}')
BCIFADDR="$SECONDADDR"
BCADDR=$($IFCONFIG "$BROADCASTIF" |grep 'broadcast ' |sed 's/.*broadcast/broadcast/' |awk '{print($2);}') ;;
HP-UX)
MAINIF=lan0 # might use "netstat -ni" for this
BROADCASTIF="$MAINIF"
SECONDADDR=$($IFCONFIG $MAINIF |tail -n 1 |awk '{print($2);}')
BCADDR=$($IFCONFIG $BROADCASTIF |grep 'broadcast ' |sed 's/.*broadcast/broadcast/' |awk '{print($2);}') ;;
SunOS)
MAINIF=$($IFCONFIG -a |grep '^[a-z]' |grep -v '^lo0: ' |head -1 |cut -d: -f1)
BROADCASTIF="$MAINIF"
#BROADCASTIF=hme0
#BROADCASTIF=eri0
#SECONDADDR=$($IFCONFIG $BROADCASTIF |grep 'inet ' |awk '{print($2);}')
SECONDADDR=$(expr "$($IFCONFIG -a |grep 'inet ' |fgrep -v ' 127.0.0.1 '| head -n 1)" : '.*inet \([0-9.]*\) .*')
#BCIFADDR="$SECONDADDR"
#BCADDR=$($IFCONFIG $BROADCASTIF |grep 'broadcast ' |sed 's/.*broadcast/broadcast/' |awk '{print($2);}')
;;
DragonFly)
MAINIF=$($IFCONFIG -a |grep -v ^lp |grep '^[a-z]' |grep -v '^lo0: ' |head -1 |cut -d: -f1)
BROADCASTIF="$MAINIF"
SECONDADDR=$($IFCONFIG "$BROADCASTIF" |grep 'inet ' |awk '{print($2);}')
BCIFADDR="$SECONDADDR"
BCADDR=$($IFCONFIG "$BROADCASTIF" |grep 'broadcast ' |sed 's/.*broadcast/broadcast/' |awk '{print($2);}') ;;
#AIX|FreeBSD|Solaris)
*)
SECONDADDR=$(expr "$($IFCONFIG -a |grep 'inet ' |fgrep -v ' 127.0.0.1 ' |head -n 1)" : '.*inet \([0-9.]*\) .*')
;;
esac
# for generic sockets we need this address in hex form
if [ "$SECONDADDR" ]; then
SECONDADDRHEX="$(printf "%02x%02x%02x%02x\n" $(echo "$SECONDADDR" |tr '.' ' '))"
fi
# for some tests we need a second local IPv6 address
case "$UNAME" in
Linux) if [ "$IP" ]; then
SECONDIP6ADDR=$(expr "$($IP address |grep 'inet6 ' |fgrep -v ' ::1/128 '| head -n 1)" : '.*inet6 \([0-9a-f:][0-9a-f:]*\)/.*')
else
SECONDIP6ADDR=$(expr "$($IFCONFIG -a |grep 'inet6 ' |fgrep -v ' ::1/128 '| head -n 1)" : '.*inet \([0-9.]*\) .*')
fi ;;
*)
SECONDIP6ADDR=$(expr "$($IFCONFIG -a |grep 'inet6 ' |fgrep -v ' ::1/128 '| head -n 1)" : '.*inet \([0-9.]*\) .*')
;;
esac
if [ -z "$SECONDIP6ADDR" ]; then
# case "$TESTS" in
# *%root2%*) $IFCONFIG eth0 ::2/128
# esac
SECONDIP6ADDR="$LOCALHOST6"
else
SECONDIP6ADDR="[$SECONDIP6ADDR]"
fi
TRUE=$(which true)
#E=-e # Linux
if [ $(echo "x\c") = "x" ]; then E=""
elif [ $(echo -e "x\c") = "x" ]; then E="-e"
else
echo "cannot suppress trailing newline on echo" >&2
exit 1
fi
ECHO="echo $E"
PRINTF="printf"
case "$TERM" in
vt100|vt320|linux|xterm|cons25|dtterm|aixterm|sun-color|xterm-color|xterm-256color|screen)
# there are different behaviours of printf (and echo)
# on some systems, echo behaves different than printf...
if [ "$($PRINTF "\0101")" = "A" ]; then
RED="\0033[31m"
GREEN="\0033[32m"
YELLOW="\0033[33m"
# if [ "$UNAME" = SunOS ]; then
# NORMAL="\0033[30m"
# else
NORMAL="\0033[39m"
# fi
else
RED="\033[31m"
GREEN="\033[32m"
YELLOW="\033[33m"
# if [ "$UNAME" = SunOS ]; then
# NORMAL="\033[30m"
# else
NORMAL="\033[39m"
# fi
fi
OK="${GREEN}OK${NORMAL}"
FAILED="${RED}FAILED${NORMAL}"
NO_RESULT="${YELLOW}NO RESULT${NORMAL}"
CANT="$NO_RESULT"
;;
*) OK="OK"
FAILED="FAILED"
NO_RESULT="NO RESULT"
CANT="$NO_RESULT"
;;
esac
if [ -x /usr/xpg4/bin/id ]; then
# SunOS has rather useless tools in its default path
PATH="/usr/xpg4/bin:$PATH"
fi
OPENSSL_S_CLIENT_4=
OPENSSL_S_CLIENT_DTLS=
init_openssl_s_client () {
if openssl s_client -help 2>&1 |grep -q ' -4 '; then
OPENSSL_S_CLIENT_4="-4"
else
OPENSSL_S_CLIENT_4=" "
fi
if openssl s_client -help 2>&1 | grep -q ' -dtls '; then
OPENSSL_S_CLIENT_DTLS=-dtls
else
OPENSSL_S_CLIENT_DTLS=-dtls1
fi
}
OPENSSL_S_SERVER_4=
OPENSSL_S_SERVER_DTLS=
OPENSSL_S_SERVER_NO_IGN_EOF=
init_openssl_s_server () {
if openssl s_server -help 2>&1 |grep -q ' -4 '; then
OPENSSL_S_SERVER_4="-4"
else
OPENSSL_S_SERVER_4=" "
fi
if openssl s_server -help 2>&1 | grep -q ' -dtls '; then
OPENSSL_S_SERVER_DTLS="-dtls"
else
OPENSSL_S_SERVER_DTLS="-dtls1"
fi
if openssl s_server -help 2>&1 | grep -q ' -no-ign_eof '; then
OPENSSL_S_SERVER_NO_IGN_EOF="-no-ign_eof"
else
OPENSSL_S_SERVER_NO_IGN_EOF=" "
fi
}
[ -z "$TESTS" ] && TESTS="consistency functions filan"
# use '%' as separation char
TESTS="%$(echo " $TESTS " |tr ' ' '%')%"
[ -z "$USER" ] && USER="$LOGNAME" # HP-UX
if [ -z "$TMPDIR" ]; then
if [ -z "$TMP" ]; then
TMP=/tmp
fi
TMPDIR="$TMP"
fi
TD="$TMPDIR/$USER/$$"; td="$TD"
rm -rf "$TD" || (echo "cannot rm $TD" >&2; exit 1)
mkdir -p "$TD"
#trap "rm -r $TD" 0 3
echo "Using temp directory $TD"
case "$TESTS" in
*%consistency%*)
# test if addresses are sorted alphabetically:
$ECHO "testing if address array is sorted...\c"
TF="$TD/socat-q"
IFS="$($ECHO ' \n\t')"
if ! $SOCAT -hhh >/dev/null; then
echo "Failed: $SOCAT -hhh" >&2
exit -1
fi
$SOCAT -hhh |sed -n '/^ address-head:/,/^ opts:/ p' |grep -v -e "^ address-head:" -e "^ opts:" |sed -e 's/^[[:space:]]*//' -e 's/[: ].*//' |grep -v '^<' >"$TF"
$SOCAT -hhh |sed -n '/^ address-head:/,/^ opts:/ p' |grep -v -e "^ address-head:" -e "^ opts:" |sed -e 's/^[[:space:]]*//' -e 's/[: ].*//' |grep -v '^<' |LC_ALL=C sort |diff "$TF" - >"$TF-diff"
if [ -s "$TF-diff" ]; then
$ECHO "\n*** address array is not sorted. Wrong entries:" >&2
cat "$TD/socat-q-diff" >&2
exit 1
else
echo " ok"
fi
#/bin/rm "$TF"
#/bin/rm "$TF-diff"
esac
case "$TESTS" in
*%consistency%*)
# test if address options array ("optionnames") is sorted alphabetically:
$ECHO "testing if address options are sorted...\c"
TF="$TD/socat-qq"
$SOCAT -hhh |sed '1,/opt:/ d' |awk '{print($1);}' >"$TF"
LC_ALL=C sort "$TF" |diff "$TF" - >"$TF-diff"
if [ -s "$TF-diff" ]; then
$ECHO "\n*** option array is not sorted. Wrong entries:" >&2
cat "$TD/socat-qq-diff" >&2
exit 1
else
echo " ok"
fi
/bin/rm "$TF"
/bin/rm "$TF-diff"
esac
#==============================================================================
N=1
numOK=0
numFAIL=0
numCANT=0
listFAIL=
listCANT=
namesFAIL=
#==============================================================================
# test if selected socat features work ("FUNCTIONS")
testecho () {
local N="$1"
local title="$2"
local arg1="$3"; [ -z "$arg1" ] && arg1="-"
local arg2="$4"; [ -z "$arg2" ] && arg2="echo"
local opts="$5"
local T="$6"; [ -z "$T" ] && T=0
local tf="$td/test$N.stdout"
local te="$td/test$N.stderr"
local tdiff="$td/test$N.diff"
local da="test$N $(date) $RANDOM"
if ! eval $NUMCOND; then :; else
#local cmd="$TRACE $SOCAT $opts $arg1 $arg2"
#$ECHO "testing $title (test $N)... \c"
$PRINTF "test $F_n %s... " $N "$title"
#echo "$da" |$cmd >"$tf" 2>"$te"
(psleep $T; echo "$da"; psleep $T) |($TRACE $SOCAT $opts "$arg1" "$arg2" >"$tf" 2>"$te"; echo $? >"$td/test$N.rc") &
export rc1=$!
#sleep 5 && kill $rc1 2>/dev/null &
# rc2=$!
wait $rc1
# kill $rc2 2>/dev/null
if [ "$(cat "$td/test$N.rc")" != 0 ]; then
$PRINTF "$FAILED: $TRACE $SOCAT:\n"
echo "$TRACE $SOCAT $opts $arg1 $arg2" >&2
cat "$te" >&2
numFAIL=$((numFAIL+1))
listFAIL="$listFAIL $N"
elif echo "$da" |diff - "$tf" >"$tdiff" 2>&1; then
$PRINTF "$OK\n"
if [ "$VERBOSE" ]; then echo "$SOCAT $opts $arg1 $arg2" >&2; fi
if [ -n "$debug" ]; then cat $te >&2; fi
numOK=$((numOK+1))
else
$PRINTF "$FAILED:\n"
echo "$TRACE $SOCAT $opts $arg1 $arg2" >&2
cat "$te" >&2
echo diff: >&2
cat "$tdiff" >&2
numFAIL=$((numFAIL+1))
listFAIL="$listFAIL $N"
fi
fi # NUMCOND
}
# test if call to od and throughput of data works - with graceful shutdown and
# flush of od buffers
testod () {
local num="$1"
local title="$2"
local arg1="$3"; [ -z "$arg1" ] && arg1="-"
local arg2="$4"; [ -z "$arg2" ] && arg2="echo"
local opts="$5"
local T="$6"; [ -z "$T" ] && T=0
local tf="$td/test$N.stdout"
local te="$td/test$N.stderr"
local tr="$td/test$N.ref"
local tdiff="$td/test$N.diff"
local dain="$(date) $RANDOM"
if ! eval $NUMCOND; then :; else
echo "$dain" |$OD_C >"$tr"
# local daout="$(echo "$dain" |$OD_C)"
$PRINTF "test $F_n %s... " $num "$title"
(psleep $T; echo "$dain"; psleep $T) |$TRACE $SOCAT $opts "$arg1" "$arg2" >"$tf" 2>"$te"
if [ "$?" != 0 ]; then
$PRINTF "$FAILED: $TRACE $SOCAT:\n"
echo "$TRACE $SOCAT $opts $arg1 $arg2"
cat "$te"
numFAIL=$((numFAIL+1))
listFAIL="$listFAIL $num"
# elif echo "$daout" |diff - "$tf" >"$tdiff" 2>&1; then
elif diff "$tr" "$tf" >"$tdiff" 2>&1; then
$PRINTF "$OK\n"
if [ -n "$debug" ]; then cat $te; fi
numOK=$((numOK+1))
else
$PRINTF "$FAILED: diff:\n"
echo "$TRACE $SOCAT $opts $arg1 $arg2"
cat "$te"
cat "$tdiff"
numFAIL=$((numFAIL+1))
listFAIL="$listFAIL $num"
fi
fi # NUMCOND
}
# test if the socat executable has these features compiled in
# print the first missing address type
testfeats () {
local a A;
for a in $@; do
A=$(echo "$a" |tr 'a-z-' 'A-Z_')
if SOCAT_MAIN_WAIT= $SOCAT -V |grep "#define WITH_$A 1\$" >/dev/null; then
if [[ "$A" =~ OPENSSL.* ]]; then
gentestcert testsrv
gentestcert testcli
fi
shift
continue
fi
echo "$a"
return 1
done
return 0
}
# test if the socat executable has these address types compiled in
# print the first missing address type
testaddrs () {
local a A;
for a in $@; do
A=$(echo "$a" |tr 'a-z' 'A-Z')
# the ::::: forces syntax errer and prevents the address from doing anything
if ! $SOCAT $A::::: /dev/null 2>&1 </dev/null |grep -q "E unknown device/address"; then
shift
continue
fi
echo "$a"
return 1
done
return 0
}
# test if the socat executable has these options compiled in
# print the first missing option
testoptions () {
local a A;
for a in $@; do
A=$(echo "$a" |tr 'a-z' 'A-Z')
if $SOCAT -hhh |grep "[^a-z0-9-]$a[^a-z0-9-]" >/dev/null; then
shift
continue
fi
echo "$a"
return 1
done
return 0
}
# check if the given pid exists and has child processes
# if yes: prints child process lines to stdout, returns 0
# if not: prints ev.message to stderr, returns 1
childprocess () {
local l
case "$UNAME" in
AIX) l="$(ps -fade |grep "^........ ...... $(printf %6u $1)")" ;;
FreeBSD) l="$(ps -faje |grep "^........ ..... $(printf %5u $1)")" ;;
HP-UX) l="$(ps -fade |grep "^........ ..... $(printf %5u $1)")" ;;
Linux) l="$(ps -fade |grep "^........ ..... $(printf %5u $1)")" ;;
# NetBSD) l="$(ps -aj |grep "^........ ..... $(printf %4u $1)")" ;;
NetBSD) l="$(ps -aj |grep "^[^ ][^ ]*[ ][ ]*..... $(printf %5u $1)")" ;;
OpenBSD) l="$(ps -aj |grep "^........ ..... $(printf %5u $1)")" ;;
SunOS) l="$(ps -fade |grep "^........ ..... $(printf %5u $1)")" ;;
DragonFly)l="$(ps -faje |grep "^[^ ][^ ]*[ ][ ]*..... $(printf %5u $1)")" ;;
CYGWIN*) l="$(ps -pafe |grep "^[^ ]*[ ][ ]*[^ ][^ ]*[ ][ ]*$1[ ]")" ;;
*) l="$(ps -fade |grep "^[^ ][^ ]*[ ][ ]*[0-9][0-9]**[ ][ ]*$(printf %5u $1) ")" ;; esac
if [ -z "$l" ]; then
return 1;
fi
echo "$l"
return 0
}
# return a list of child process pids [killchild]
childpids () {
case "$UNAME" in
AIX) l="$(ps -fade |grep "^........ ...... $(printf %6u $1)" |awk '{print($2);}')" ;;
FreeBSD) l="$(ps -fl |grep "^[^ ][^ ]*[ ][ ]*[0-9][0-9]*[ ][ ]*$1[ ]" |awk '{print($2);}')" ;;
HP-UX) l="$(ps -fade |grep "^........ ..... $(printf %5u $1)" |awk '{print($2);}')" ;;
# Linux) l="$(ps -fade |grep "^........ ..... $(printf %5u $1)" |awk '{print($2);}')" ;;
Linux) l="$(ps -fade |grep "^[^[:space:]][^[:space:]]*[[:space:]][[:space:]]*[^[:space:]][^[:space:]]*[[:space:]][[:space:]]*$1 " |awk '{print($2);}')" ;;
# NetBSD) l="$(ps -aj |grep "^........ ..... $(printf %4u $1)" |awk '{print($2);}')" ;;
NetBSD) l="$(ps -aj |grep "^[^ ][^ ]*[ ][ ]*..... $(printf %5u $1)" |awk '{print($2);}')" ;;
OpenBSD) l="$(ps -aj |grep "^........ ..... $(printf %5u $1)" |awk '{print($2);}')" ;;
SunOS) l="$(ps -fade |grep "^........ ..... $(printf %5u $1)" |awk '{print($2);}')" ;;
DragonFly)l="$(ps -faje |grep "^[^ ][^ ]*[ ][ ]*..... $(printf %5u $1)" |awk '{print($2);}')" ;;
CYGWIN*) l="$(ps -pafe |grep "^[^ ]*[ ][ ]*[^ ][^ ]*[ ][ ]*$1[ ]" |awk '{print($2);}')" ;;
*) l="$(ps -fade |grep "^[^ ][^ ]*[ ][ ]*[0-9][0-9]*[ ][ ]*$(printf %5u $1) " |awk '{print($2);}')" ;; esac
if [ -z "$l" ]; then
return 1;
fi
echo "$l"
return 0
}
# check if the given process line refers to a defunct (zombie) process
# yes: returns 0
# no: returns 1
isdefunct () {
local l
case "$UNAME" in
AIX) l="$(echo "$1" |grep ' <defunct>$')" ;;
FreeBSD) l="$(echo "$1" |grep ' <defunct>$')" ;;
HP-UX) l="$(echo "$1" |grep ' <defunct>$')" ;;
Linux) l="$(echo "$1" |grep ' <defunct>$')" ;;
SunOS) l="$(echo "$1" |grep ' <defunct>$')" ;;
DragonFly)l="$(echo "$1" |grep ' <defunct>$')" ;;
*) l="$(echo "$1" |grep ' <defunct>$')" ;;
esac
[ -n "$l" ];
}
# check if UNIX socket protocol is available on host
runsunix () {
return 0;
$TRACE $SOCAT /dev/null UNIX-LISTEN:"$td/unix.socket" 2>"$td/unix.stderr" &
pid=$!
usleep $MICROS
kill "$pid" 2>/dev/null
test ! -s "$td/unix.stderr"
}
unset HAVENOT_IP4
# check if an IP4 loopback interface exists
runsip4 () {
[ -n "$HAVENOT_IP4" ] && return $HAVENOT_IP4
local l
case "$UNAME" in
AIX) l=$($IFCONFIG lo0 |fgrep 'inet 127.0.0.1 ') ;;
FreeBSD) l=$($IFCONFIG lo0 |fgrep 'inet 127.0.0.1 ') ;;
HP-UX) l=$($IFCONFIG lo0 |fgrep 'inet 127.0.0.1 ') ;;
Linux) if [ "$IP" ]; then
l=$($IP address |egrep ' inet 127.0.0.1/')
else
l=$($IFCONFIG |egrep 'inet (addr:)?127\.0\.0\.1 ')
fi ;;
NetBSD)l=$($IFCONFIG -a |fgrep 'inet 127.0.0.1 ');;
OpenBSD)l=$($IFCONFIG -a |fgrep 'inet 127.0.0.1 ');;
OSF1) l=$($IFCONFIG -a |grep ' inet ') ;;
SunOS) l=$($IFCONFIG -a |grep 'inet ') ;;
Darwin)l=$($IFCONFIG lo0 |fgrep 'inet 127.0.0.1 ') ;;
DragonFly)l=$($IFCONFIG -a |fgrep 'inet 127.0.0.1 ');;
CYGWIN*) l=$(ipconfig |grep IPv4);;
*) l=$($IFCONFIG -a |grep ' ::1[^:0-9A-Fa-f]') ;;
esac
[ -z "$l" ] && return 1
# existence of interface might not suffice, check for routeability:
case "$UNAME" in
Darwin) ping -c 1 127.0.0.1 >/dev/null 2>&1; l="$?" ;;
Linux) ping -c 1 127.0.0.1 >/dev/null 2>&1; l="$?" ;;
*) if [ -n "$l" ]; then l=0; else l=1; fi ;;
esac
HAVENOT_IP4=$l
if [ "$HAVENOT_IP4" -ne 0 ]; then
echo IP4
fi
return $l;
}
unset HAVENOT_IP6
# check if an IP6 loopback interface exists
runsip6 () {
[ -n "$HAVENOT_IP6" ] && return $HAVENOT_IP6
local l
case "$UNAME" in
AIX) l=$($IFCONFIG lo0 |grep 'inet6 ::1[/%]') ;;
HP-UX) l=$($IFCONFIG lo0 |grep ' inet6 ') ;;
Linux) if [ "$IP" ]; then
l=$($IP address |egrep 'inet6 ::1/128')
else
l=$($IFCONFIG |egrep 'inet6 (addr: )?::1/?')
fi ;;
NetBSD)l=$($IFCONFIG -a |grep 'inet6 ::1 ');;
OSF1) l=$($IFCONFIG -a |grep ' inet6 ') ;;
SunOS) l=$($IFCONFIG -a |grep 'inet6 ') ;;
Darwin)l=$($IFCONFIG lo0 |grep 'inet6 ::1 ') ;;
CYGWIN*) l=$(ipconfig |grep IPv6);;
*) l=$($IFCONFIG -a |grep ' ::1[^:0-9A-Fa-f]') ;;
esac
[ -z "$l" ] && return 1
# existence of interface might not suffice, check for routeability:
case "$UNAME" in
Darwin) $PING6 -c 1 ::1 >/dev/null 2>&1; l="$?" ;;
Linux) $PING6 -c 1 ::1 >/dev/null 2>&1; l="$?" ;;
*) if [ -n "$l" ]; then l=0; else l=1; fi ;;
esac
HAVENOT_IP6=$l
if [ "$HAVENOT_IP6" -ne 0 ]; then
echo IP6
fi
return "$HAVENOT_IP6"
}
# check if TCP on IPv4 is available on host
runstcp4 () {
runsip4 >/dev/null || { echo TCP4; return 1; }
$SOCAT -h |grep -i ' TCP4-' >/dev/null || return 1
return 0;
}
# check if TCP on IPv6 is available on host
runstcp6 () {
runsip6 >/dev/null || { echo TCP6; return 1; }
$SOCAT -h |grep -i ' TCP6-' >/dev/null || return 1
return 0;
}
# check if UDP on IPv4 is available on host
runsudp4 () {
runsip4 >/dev/null || { echo UDP4; return 1; }
$SOCAT -h |grep -i ' UDP4-' >/dev/null || return 1
return 0;
}
# check if UDP on IPv6 is available on host
runsudp6 () {
runsip6 >/dev/null || { echo UDP6; return 1; }
$SOCAT -h |grep -i ' UDP6-' >/dev/null || return 1
return 0;
}
# check if SCTP on IPv4 is available on host
runssctp4 () {
runsip4 >/dev/null || { echo SCTP4; return 1; }
$SOCAT -h |grep -i ' SCTP4-' >/dev/null || return 1
$SOCAT /dev/null SCTP4-L:0,accept-timeout=0.001 2>/dev/null || return 1;
return 0;
}
# check if SCTP on IPv6 is available on host
runssctp6 () {
runsip6 >/dev/null || { echo SCTP6; return 1; }
$SOCAT -h |grep -i ' SCTP6-' >/dev/null || return 1
$SOCAT /dev/null SCTP6-L:0,accept-timeout=0.001 2>/dev/null || return 1;
return 0;
}
# check if DCCP on IPv4 is available on host
runsdccp4 () {
runsip4 >/dev/null || { echo DCCP4; return 1; }
$SOCAT -h |grep -i ' DCCP4-' >/dev/null || return 1
$SOCAT /dev/null DCCP4-L:0,accept-timeout=0.001 2>/dev/null || return 1;
return 0;
}
# check if DCCP on IPv6 is available on host
runsdccp6 () {
runsip6 >/dev/null || { echo DCCP6; return 1; }
$SOCAT -h |grep -i ' DCCP6-' >/dev/null || return 1
$SOCAT /dev/null DCCP6-L:0,accept-timeout=0.001 2>/dev/null || return 1;
return 0;