summaryrefslogtreecommitdiff
path: root/regress/usr.bin/nc/Makefile
blob: 61e9682dc8ade1ba80726b85aecae015ace85f41 (plain)
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
#	$OpenBSD: Makefile,v 1.6 2020/01/21 22:47:39 bluhm Exp $

# Copyright (c) 2020 Alexander Bluhm <bluhm@openbsd.org>
#
# Permission to use, copy, modify, and distribute this software for any
# purpose with or without fee is hereby granted, provided that the above
# copyright notice and this permission notice appear in all copies.
#
# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.

PROGS =			client-tcp server-tcp
SRCS_client-tcp =	client-tcp.c util.c
SRCS_server-tcp =	server-tcp.c util.c
WARNINGS =		yes

NC =			./netcat-regress

CLEANFILES =		${NC:T} {client,server}.{out,err,port,sock} ktrace.out

REGRESS_SETUP =		setup
setup:
	@echo '======== $@ ========'
	pkill ${NC:T} || true
	rm -f ${NC:T}
	# copying global netcat to local name allows to pkill it during cleanup
	cp /usr/bin/nc ${NC:T}
	chmod 755 ${NC:T}

REGRESS_CLEANUP =	cleanup
cleanup:
	@echo '======== $@ ========'
	-pkill ${NC:T} || true

REGRESS_TARGETS =

SERVER_NC = rm -f server.err; echo greeting | ${NC}
CLIENT_NC = rm -f client.err; echo command | ${NC}
SERVER_BG = 2>&1 >server.out | tee server.err &
CLIENT_BG = 2>&1 >client.out | tee client.err &
SERVER_LOG = >server.out 2>server.err
CLIENT_LOG = >client.out 2>client.err

PORT_GET = \
	sed -E -n 's/(Listening|Bound) on .* //p' server.err >server.port
PORT = `cat server.port`

LISTEN_WAIT = \
	let timeout=`date +%s`+5; \
	until grep -q 'Listening on ' server.err; \
	do [[ `date +%s` -lt $$timeout ]] || { echo timeout; exit 1; }; done

BIND_WAIT = \
	let timeout=`date +%s`+5; \
	until grep -q 'Bound on ' server.err; \
	do [[ `date +%s` -lt $$timeout ]] || { echo timeout; exit 1; }; done

BIND_CLIENT_WAIT = \
	let timeout=`date +%s`+5; \
	until grep -q 'Bound on ' client.err; \
	do [[ `date +%s` -lt $$timeout ]] || { echo timeout; exit 1; }; done

CONNECT_WAIT = \
	let timeout=`date +%s`+5; \
	until grep -q 'Connection to .* succeeded' client.err; \
	do [[ `date +%s` -lt $$timeout ]] || { echo timeout; exit 1; }; done

TLS_WAIT = \
	let timeout=`date +%s`+5; \
	until grep -q 'Cert Hash:' client.err; \
	do [[ `date +%s` -lt $$timeout ]] || { echo timeout; exit 1; }; done

TRANSFER_WAIT = \
	let timeout=`date +%s`+5; \
	until grep -q 'greeting' client.out && grep -q 'command' server.out; \
	do [[ `date +%s` -lt $$timeout ]] || { echo timeout; exit 1; }; done

TRANSFER_CLIENT_WAIT = \
	let timeout=`date +%s`+5; \
	until grep -q 'greeting' client.out; \
	do [[ `date +%s` -lt $$timeout ]] || { echo timeout; exit 1; }; done

TRANSFER_SERVER_WAIT = \
	let timeout=`date +%s`+5; \
	until grep -q 'command' server.out; \
	do [[ `date +%s` -lt $$timeout ]] || { echo timeout; exit 1; }; done

RUNNING_WAIT = \
	let timeout=`date +%s`+5; \
	while ps -xww -o comm,stat | grep -q '${NC:T} .*R'; \
	do [[ `date +%s` -lt $$timeout ]] || { echo timeout; exit 1; }; done

### TCP ####

REGRESS_TARGETS +=	run-tcp
run-tcp:
	@echo '======== $@ ========'
	${SERVER_NC} -n -v -l 127.0.0.1 0 ${SERVER_BG}
	${LISTEN_WAIT}
	${PORT_GET}
	${CLIENT_NC} -n -v 127.0.0.1 ${PORT} ${CLIENT_BG}
	${CONNECT_WAIT}
	${TRANSFER_WAIT}
	grep '^greeting$$' client.out
	grep '^command$$' server.out
	grep 'Listening on 127.0.0.1 ' server.err
	grep 'Connection received on 127.0.0.1 ' server.err
	grep 'Connection to 127.0.0.1 .* succeeded!' client.err

REGRESS_TARGETS +=	run-tcp6
run-tcp6:
	@echo '======== $@ ========'
	${SERVER_NC} -n -v -l ::1 0 ${SERVER_BG}
	${LISTEN_WAIT}
	${PORT_GET}
	${CLIENT_NC} -n -v ::1 ${PORT} ${CLIENT_BG}
	${CONNECT_WAIT}
	${TRANSFER_WAIT}
	grep '^greeting$$' client.out
	grep '^command$$' server.out
	grep 'Listening on ::1 ' server.err
	grep 'Connection received on ::1 ' server.err
	grep 'Connection to ::1 .* succeeded!' client.err

# TCP resolver

REGRESS_TARGETS +=	run-tcp-localhost-server
run-tcp-localhost-server:
	@echo '======== $@ ========'
	${SERVER_NC} -4 -v -l localhost 0 ${SERVER_BG}
	${LISTEN_WAIT}
	${PORT_GET}
	${CLIENT_NC} -n -v 127.0.0.1 ${PORT} ${CLIENT_BG}
	${CONNECT_WAIT}
	${TRANSFER_WAIT}
	grep '^greeting$$' client.out
	grep '^command$$' server.out
	grep 'Listening on localhost ' server.err
	grep 'Connection received on localhost ' server.err
	grep 'Connection to 127.0.0.1 .* succeeded!' client.err

REGRESS_TARGETS +=	run-tcp6-localhost-server
run-tcp6-localhost-server:
	@echo '======== $@ ========'
	${SERVER_NC} -6 -v -l localhost 0 ${SERVER_BG}
	${LISTEN_WAIT}
	${PORT_GET}
	${CLIENT_NC} -n -v ::1 ${PORT} ${CLIENT_BG}
	${CONNECT_WAIT}
	${TRANSFER_WAIT}
	grep '^greeting$$' client.out
	grep '^command$$' server.out
	grep 'Listening on localhost ' server.err
	grep 'Connection received on localhost ' server.err
	grep 'Connection to ::1 .* succeeded!' client.err

REGRESS_TARGETS +=	run-tcp-localhost-client
run-tcp-localhost-client:
	@echo '======== $@ ========'
	${SERVER_NC} -n -v -l 127.0.0.1 0 ${SERVER_BG}
	${LISTEN_WAIT}
	${PORT_GET}
	${CLIENT_NC} -4 -v localhost ${PORT} ${CLIENT_BG}
	${CONNECT_WAIT}
	${TRANSFER_WAIT}
	grep '^greeting$$' client.out
	grep '^command$$' server.out
	grep 'Listening on 127.0.0.1 ' server.err
	grep 'Connection received on 127.0.0.1 ' server.err
	grep 'Connection to localhost .* succeeded!' client.err

REGRESS_TARGETS +=	run-tcp6-localhost-client
run-tcp6-localhost-client:
	@echo '======== $@ ========'
	${SERVER_NC} -n -v -l ::1 0 ${SERVER_BG}
	${LISTEN_WAIT}
	${PORT_GET}
	${CLIENT_NC} -6 -v localhost ${PORT} ${CLIENT_BG}
	${CONNECT_WAIT}
	${TRANSFER_WAIT}
	grep '^greeting$$' client.out
	grep '^command$$' server.out
	grep 'Listening on ::1 ' server.err
	grep 'Connection received on ::1 ' server.err
	grep 'Connection to localhost .* succeeded!' client.err

REGRESS_TARGETS +=	run-tcp-bad-localhost-server
run-tcp-bad-localhost-server:
	@echo '======== $@ ========'
	! ${NC} -4 -v -l ::1 0 ${SERVER_LOG}
	grep 'no address associated with name' server.err

REGRESS_TARGETS +=	run-tcp6-bad-localhost-server
run-tcp6-bad-localhost-server:
	@echo '======== $@ ========'
	! ${NC} -6 -v -l 127.0.0.0 0 ${SERVER_LOG}
	grep 'no address associated with name' server.err

REGRESS_TARGETS +=	run-tcp-bad-localhost-client
run-tcp-bad-localhost-client:
	@echo '======== $@ ========'
	${SERVER_NC} -n -v -l 127.0.0.1 0 ${SERVER_BG}
	${LISTEN_WAIT}
	${PORT_GET}
	! ${NC} -4 -v ::1 ${PORT} ${CLIENT_LOG}
	grep 'no address associated with name' client.err

REGRESS_TARGETS +=	run-tcp6-bad-localhost-client
run-tcp6-bad-localhost-client:
	@echo '======== $@ ========'
	${SERVER_NC} -n -v -l 127.0.0.1 0 ${SERVER_BG}
	${LISTEN_WAIT}
	${PORT_GET}
	! ${NC} -6 -v 127.0.0.1 ${PORT} ${CLIENT_LOG}
	grep 'no address associated with name' client.err

REGRESS_TARGETS +=	run-tcp-sleep
run-tcp-sleep:
	@echo '======== $@ ========'
	${SERVER_NC} -n -v -l 127.0.0.1 0 ${SERVER_BG}
	${LISTEN_WAIT}
	${PORT_GET}
	${CLIENT_NC} -n -v 127.0.0.1 ${PORT} ${CLIENT_BG}
	${CONNECT_WAIT}
	${TRANSFER_WAIT}
	grep '^greeting$$' client.out
	grep '^command$$' server.out
	grep 'Listening on 127.0.0.1 ' server.err
	grep 'Connection received on 127.0.0.1 ' server.err
	grep 'Connection to 127.0.0.1 .* succeeded!' client.err
	# netcat waits for the other side to terminate, check it is sleeping
	${RUNNING_WAIT}
	ps -xww -o comm,stat,args | grep '^${NC:T} .*S.* -v -l 127'
	ps -xww -o comm,stat,args | grep '^${NC:T} .*S.* -v 127'

# TCP keep

REGRESS_TARGETS +=	run-tcp-keep
run-tcp-keep:
	@echo '======== $@ ========'
	${SERVER_NC} -k -n -v -l 127.0.0.1 0 ${SERVER_BG}
	${LISTEN_WAIT}
	${PORT_GET}
	${CLIENT_NC} -n -v 127.0.0.1 ${PORT} ${CLIENT_BG}
	${CONNECT_WAIT}
	${TRANSFER_WAIT}
	grep '^greeting$$' client.out
	grep '^command$$' server.out
	grep 'Listening on 127.0.0.1 ' server.err
	grep 'Connection received on 127.0.0.1 ' server.err
	grep 'Connection to 127.0.0.1 .* succeeded!' client.err
	# kill client and reconnect with a new one
	:> server.err
	pkill -l -f "^${NC} .* 127.0.0.1 ${PORT}$$"
	rm -f client.{out,err}
	:> server.out
	# server closes the listen socket and binds a new one with new port
	${LISTEN_WAIT}
	${PORT_GET}
	${CLIENT_NC} -n -v 127.0.0.1 ${PORT} ${CLIENT_BG}
	${CONNECT_WAIT}
	# server sends only one greeting, do not wait for a second one
	${TRANSFER_SERVER_WAIT}
	! grep 'greeting' client.out
	# truncation of log results in NUL bytes, do not match ^
	grep 'command$$' server.out
	grep 'Listening on 127.0.0.1 ' server.err
	grep 'Connection received on 127.0.0.1 ' server.err
	grep 'Connection to 127.0.0.1 .* succeeded!' client.err

### TLS ###

REGRESS_TARGETS +=	run-tls
run-tls: 127.0.0.1.crt
	@echo '======== $@ ========'
	${SERVER_NC} -c -C 127.0.0.1.crt -K 127.0.0.1.key -n -v -l 127.0.0.1 0 \
	    ${SERVER_BG}
	${LISTEN_WAIT}
	${PORT_GET}
	${CLIENT_NC} -c -R 127.0.0.1.crt -n -v 127.0.0.1 ${PORT} ${CLIENT_BG}
	${CONNECT_WAIT}
	${TLS_WAIT}
	${TRANSFER_WAIT}
	grep '^greeting$$' client.out
	grep '^command$$' server.out
	grep 'Listening on 127.0.0.1 ' server.err
	grep 'Connection received on 127.0.0.1 ' server.err
	# XXX success message should be issued after TLS handshake
	grep 'Connection to 127.0.0.1 .* succeeded!' client.err
	grep 'Subject: .*/OU=server/CN=127.0.0.1' client.err
	grep 'Issuer: .*/OU=server/CN=127.0.0.1' client.err

REGRESS_TARGETS +=	run-tls6
run-tls6: 1.crt
	@echo '======== $@ ========'
	${SERVER_NC} -c -C 1.crt -K 1.key -n -v -l ::1 0 ${SERVER_BG}
	${LISTEN_WAIT}
	${PORT_GET}
	${CLIENT_NC} -c -R 1.crt -n -v ::1 ${PORT} ${CLIENT_BG}
	${CONNECT_WAIT}
	${TLS_WAIT}
	${TRANSFER_WAIT}
	grep '^greeting$$' client.out
	grep '^command$$' server.out
	grep 'Listening on ::1 ' server.err
	grep 'Connection received on ::1 ' server.err
	grep 'Connection to ::1 .* succeeded!' client.err
	grep 'Subject: .*/OU=server/CN=::1' client.err
	grep 'Issuer: .*/OU=server/CN=::1' client.err

REGRESS_TARGETS +=	run-tls-localhost
run-tls-localhost: server.crt ca.crt
	@echo '======== $@ ========'
	${SERVER_NC} -c -C server.crt -K server.key -v -l localhost 0 \
	    ${SERVER_BG}
	${LISTEN_WAIT}
	${PORT_GET}
	${CLIENT_NC} -c -R ca.crt -v localhost ${PORT} ${CLIENT_BG}
	${CONNECT_WAIT}
	${TLS_WAIT}
	${TRANSFER_WAIT}
	grep '^greeting$$' client.out
	grep '^command$$' server.out
	grep 'Listening on localhost ' server.err
	grep 'Connection received on localhost ' server.err
	grep 'Connection to localhost .* succeeded!' client.err
	grep 'Subject: .*/OU=server/CN=localhost' client.err
	grep 'Issuer: .*/OU=ca/CN=root' client.err

REGRESS_TARGETS +=	run-tls-bad-ca
run-tls-bad-ca: server.crt fake-ca.crt
	@echo '======== $@ ========'
	${SERVER_NC} -c -C server.crt -K server.key -v -l localhost 0 \
	    ${SERVER_BG}
	${LISTEN_WAIT}
	${PORT_GET}
	# the client uses the wrong root ca to verify the server cert
	! ${NC} -c -R fake-ca.crt -v localhost ${PORT} ${CLIENT_LOG}
	${CONNECT_WAIT}
	grep 'Listening on localhost ' server.err
	grep 'Connection received on localhost ' server.err
	grep 'certificate signature failure' client.err
	! grep 'greeting' client.out
	! grep 'command' server.out

REGRESS_TARGETS +=	run-tls-name
run-tls-name: server.crt ca.crt
	@echo '======== $@ ========'
	${SERVER_NC} -c -C server.crt -K server.key -n -v -l 127.0.0.1 0 \
	    ${SERVER_BG}
	${LISTEN_WAIT}
	${PORT_GET}
	${CLIENT_NC} -c -e localhost -R ca.crt -n -v 127.0.0.1 ${PORT} \
	    ${CLIENT_BG}
	${CONNECT_WAIT}
	${TLS_WAIT}
	${TRANSFER_WAIT}
	grep '^greeting$$' client.out
	grep '^command$$' server.out
	grep 'Listening on 127.0.0.1 ' server.err
	grep 'Connection received on 127.0.0.1 ' server.err
	grep 'Connection to 127.0.0.1 .* succeeded!' client.err
	grep 'Subject: .*/OU=server/CN=localhost' client.err
	grep 'Issuer: .*/OU=ca/CN=root' client.err

REGRESS_TARGETS +=	run-tls-bad-name
run-tls-bad-name: server.crt ca.crt
	@echo '======== $@ ========'
	${SERVER_NC} -c -C server.crt -K server.key -n -v -l 127.0.0.1 0 \
	    ${SERVER_BG}
	${LISTEN_WAIT}
	${PORT_GET}
	# the common name in server.crt is localhost, not 127.0.0.1
	! ${NC} -c -e 127.0.0.1 -R ca.crt -n -v 127.0.0.1 ${PORT} ${CLIENT_LOG}
	${CONNECT_WAIT}
	grep 'Listening on 127.0.0.1 ' server.err
	grep 'Connection received on 127.0.0.1 ' server.err
	grep 'Connection to 127.0.0.1 .* succeeded!' client.err
	grep "name \`127.0.0.1\' not present in server certificate" client.err
	! grep 'greeting' client.out
	! grep 'command' server.out

REGRESS_TARGETS +=	run-tls-hash
run-tls-hash: server.crt ca.crt server.hash
	@echo '======== $@ ========'
	${SERVER_NC} -c -C server.crt -K server.key -v -l localhost 0 \
	    ${SERVER_BG}
	${LISTEN_WAIT}
	${PORT_GET}
	# check that the server presents certificate with correct hash
	${CLIENT_NC} -c -H `cat server.hash` -R ca.crt -v localhost ${PORT} \
	    ${CLIENT_BG}
	${CONNECT_WAIT}
	${TLS_WAIT}
	${TRANSFER_WAIT}
	grep '^greeting$$' client.out
	grep '^command$$' server.out
	grep 'Listening on localhost ' server.err
	grep 'Connection received on localhost ' server.err
	grep 'Connection to localhost .* succeeded!' client.err
	grep 'Subject: .*/OU=server/CN=localhost' client.err
	grep 'Issuer: .*/OU=ca/CN=root' client.err
	grep 'Cert Hash: SHA256:' client.err

REGRESS_TARGETS +=	run-tls-bad-hash
run-tls-bad-hash: server.crt ca.crt ca.hash
	@echo '======== $@ ========'
	${SERVER_NC} -c -C server.crt -K server.key -v -l localhost 0 \
	    ${SERVER_BG}
	${LISTEN_WAIT}
	${PORT_GET}
	# server presents certificate with server.hash, ca.hash is wrong
	! ${NC} -c -H `cat ca.hash` -R ca.crt -v localhost ${PORT} \
	    ${CLIENT_LOG}
	${CONNECT_WAIT}
	${TLS_WAIT}
	grep 'Listening on localhost ' server.err
	grep 'Connection received on localhost ' server.err
	grep 'Connection to localhost .* succeeded!' client.err
	grep 'peer certificate is not SHA256:' client.err
	! grep 'greeting' client.out
	! grep 'command' server.out

# TLS client certificate

REGRESS_TARGETS +=	run-tls-client
run-tls-client: client.crt server.crt ca.crt
	@echo '======== $@ ========'
	# use client certificate and validate at server
	${SERVER_NC} -c -R ca.crt -C server.crt -K server.key -v -l \
	    localhost 0 ${SERVER_BG}
	${LISTEN_WAIT}
	${PORT_GET}
	${CLIENT_NC} -c -R ca.crt -C client.crt -K client.key -v \
	    localhost ${PORT} ${CLIENT_BG}
	${CONNECT_WAIT}
	${TLS_WAIT}
	${TRANSFER_WAIT}
	grep '^greeting$$' client.out
	grep '^command$$' server.out
	grep 'Listening on localhost ' server.err
	grep 'Connection received on localhost ' server.err
	grep 'Connection to localhost .* succeeded!' client.err
	grep 'Subject: .*/OU=server/CN=localhost' client.err
	grep 'Issuer: .*/OU=ca/CN=root' client.err
	grep 'Subject: .*/OU=client/CN=localhost' server.err
	grep 'Issuer: .*/OU=ca/CN=root' server.err

REGRESS_TARGETS +=	run-tls-bad-client
run-tls-bad-client: client.crt server.crt ca.crt
	@echo '======== $@ ========'
	# require client certificate at server
	${SERVER_NC} -c -T clientcert -R ca.crt -C server.crt -K server.key \
	    -v -l localhost 0 ${SERVER_BG}
	${LISTEN_WAIT}
	${PORT_GET}
	# client does not provide certificate
	${CLIENT_NC} -c -R ca.crt -v localhost ${PORT} ${CLIENT_BG}
	${CONNECT_WAIT}
	${TLS_WAIT}
	grep 'Listening on localhost ' server.err
	grep 'Connection received on localhost ' server.err
	grep 'Connection to localhost .* succeeded!' client.err
	grep 'Subject: .*/OU=server/CN=localhost' client.err
	grep 'Issuer: .*/OU=ca/CN=root' client.err
	grep 'No client certificate provided' server.err
	! grep 'greeting' client.out
	! grep 'command' server.out

REGRESS_TARGETS +=	run-tls-client-bad-ca
run-tls-client-bad-ca: client.crt server.crt ca.crt fake-ca.crt
	@echo '======== $@ ========'
	# the server uses the wrong root ca to verify the client cert
	${SERVER_NC} -c -R fake-ca.crt -C server.crt -K server.key -v -l \
	    localhost 0 ${SERVER_BG}
	${LISTEN_WAIT}
	${PORT_GET}
	! ${NC} -c -R ca.crt -C client.crt -K client.key -v \
	    localhost ${PORT} ${CLIENT_LOG}
	${CONNECT_WAIT}
	grep 'Listening on localhost ' server.err
	grep 'Connection received on localhost ' server.err
	grep 'Connection to localhost .* succeeded!' client.err
	# XXX no specific error message for bogus ca
	egrep \
	    'CRYPTO_internal:(block type is not 01|data too large for modulus)'\
	    server.err
	! grep 'greeting' client.out
	! grep 'command' server.out

REGRESS_TARGETS +=	run-tls-client-name
run-tls-client-name: client.crt server.crt ca.crt
	@echo '======== $@ ========'
	# check client certificate name at server
	${SERVER_NC} -c -e localhost -R ca.crt -C server.crt -K server.key \
	    -n -v -l 127.0.0.1 0 ${SERVER_BG}
	${LISTEN_WAIT}
	${PORT_GET}
	${CLIENT_NC} -4 -c -R ca.crt -C client.crt -K client.key -v \
	    localhost ${PORT} ${CLIENT_BG}
	${CONNECT_WAIT}
	${TLS_WAIT}
	${TRANSFER_WAIT}
	grep '^greeting$$' client.out
	grep '^command$$' server.out
	grep 'Listening on 127.0.0.1 ' server.err
	grep 'Connection received on 127.0.0.1 ' server.err
	grep 'Connection to localhost .* succeeded!' client.err
	grep 'Subject: .*/OU=server/CN=localhost' client.err
	grep 'Issuer: .*/OU=ca/CN=root' client.err
	grep 'Subject: .*/OU=client/CN=localhost' server.err
	grep 'Issuer: .*/OU=ca/CN=root' server.err

REGRESS_TARGETS +=	run-tls-client-bad-name
run-tls-client-bad-name: client.crt server.crt ca.crt
	@echo '======== $@ ========'
	# client certificate is for localhost, check with 127.0.0.1 should fail
	${SERVER_NC} -c -e 127.0.0.1 -R ca.crt -C server.crt -K server.key \
	    -n -v -l 127.0.0.1 0 ${SERVER_BG}
	${LISTEN_WAIT}
	${PORT_GET}
	# client does not see any problem, TLS handshake works, wait for exit
	${CLIENT_NC} -4 -c -R ca.crt -C client.crt -K client.key -v \
	    localhost ${PORT} ${CLIENT_BG}
	${CONNECT_WAIT}
	${TLS_WAIT}
	grep 'Listening on 127.0.0.1 ' server.err
	grep 'Connection received on 127.0.0.1 ' server.err
	grep 'Connection to localhost .* succeeded!' client.err
	grep 'Subject: .*/OU=server/CN=localhost' client.err
	grep 'Issuer: .*/OU=ca/CN=root' client.err
	grep 'Subject: .*/OU=client/CN=localhost' server.err
	grep 'Issuer: .*/OU=ca/CN=root' server.err
	grep 'name (127.0.0.1) not found in client cert' server.err
	! grep 'greeting' client.out
	! grep 'command' server.out

REGRESS_TARGETS +=	run-tls-client-hash
run-tls-client-hash: client.crt server.crt ca.crt client.hash
	@echo '======== $@ ========'
	# check client certificate hash at server
	${SERVER_NC} -c -H `cat client.hash` -R ca.crt \
	    -C server.crt -K server.key -v -l localhost 0 ${SERVER_BG}
	${LISTEN_WAIT}
	${PORT_GET}
	${CLIENT_NC} -c -R ca.crt -C client.crt -K client.key -v \
	    localhost ${PORT} ${CLIENT_BG}
	${CONNECT_WAIT}
	${TLS_WAIT}
	${TRANSFER_WAIT}
	grep '^greeting$$' client.out
	grep '^command$$' server.out
	grep 'Listening on localhost ' server.err
	grep 'Connection received on localhost ' server.err
	grep 'Connection to localhost .* succeeded!' client.err
	grep 'Subject: .*/OU=server/CN=localhost' client.err
	grep 'Issuer: .*/OU=ca/CN=root' client.err
	grep 'Subject: .*/OU=client/CN=localhost' server.err
	grep 'Issuer: .*/OU=ca/CN=root' server.err

REGRESS_TARGETS +=	run-tls-client-bad-hash
run-tls-client-bad-hash: client.crt server.crt ca.crt ca.hash
	@echo '======== $@ ========'
	# client presents certificate with client.hash, ca.hash is wrong
	${SERVER_NC} -c -H `cat ca.hash` -R ca.crt \
	    -C server.crt -K server.key -v -l localhost 0 ${SERVER_BG}
	${LISTEN_WAIT}
	${PORT_GET}
	# client does not see any problem, TLS handshake works, wait for exit
	${CLIENT_NC} -c -R ca.crt -C client.crt -K client.key -v \
	    localhost ${PORT} ${CLIENT_BG}
	${CONNECT_WAIT}
	${TLS_WAIT}
	grep 'Listening on localhost ' server.err
	grep 'Connection received on localhost ' server.err
	grep 'Connection to localhost .* succeeded!' client.err
	grep 'Subject: .*/OU=server/CN=localhost' client.err
	grep 'Issuer: .*/OU=ca/CN=root' client.err
	grep 'Subject: .*/OU=client/CN=localhost' server.err
	grep 'Issuer: .*/OU=ca/CN=root' server.err
	grep 'peer certificate is not SHA256:' server.err
	! grep 'greeting' client.out
	! grep 'command' server.out

REGRESS_TARGETS +=	run-tls-client-no-hash
run-tls-client-no-hash: client.crt server.crt ca.crt client.hash
	@echo '======== $@ ========'
	# check client certificate hash at server if available
	${SERVER_NC} -c -H `cat client.hash` -R ca.crt \
	    -C server.crt -K server.key -v -l localhost 0 ${SERVER_BG}
	${LISTEN_WAIT}
	${PORT_GET}
	# client provides no certificate
	${CLIENT_NC} -c -R ca.crt -v localhost ${PORT} ${CLIENT_BG}
	${CONNECT_WAIT}
	${TLS_WAIT}
	${TRANSFER_WAIT}
	# client certificate and hash is optional, transfer is successful
	grep '^greeting$$' client.out
	grep '^command$$' server.out
	grep 'Listening on localhost ' server.err
	grep 'Connection received on localhost ' server.err
	grep 'Connection to localhost .* succeeded!' client.err
	grep 'Subject: .*/OU=server/CN=localhost' client.err
	grep 'Issuer: .*/OU=ca/CN=root' client.err
	# non existing hash is not checked
	! grep 'Cert Hash: SHA256:' server.err

REGRESS_TARGETS +=	run-tls-sleep
run-tls-sleep: 127.0.0.1.crt
	@echo '======== $@ ========'
	${SERVER_NC} -c -C 127.0.0.1.crt -K 127.0.0.1.key -n -v -l 127.0.0.1 0 \
	    ${SERVER_BG}
	${LISTEN_WAIT}
	${PORT_GET}
	${CLIENT_NC} -c -R 127.0.0.1.crt -n -v 127.0.0.1 ${PORT} ${CLIENT_BG}
	${CONNECT_WAIT}
	${TLS_WAIT}
	${TRANSFER_WAIT}
	grep '^greeting$$' client.out
	grep '^command$$' server.out
	grep 'Listening on 127.0.0.1 ' server.err
	grep 'Connection received on 127.0.0.1 ' server.err
	# XXX success message should be issued after TLS handshake
	grep 'Connection to 127.0.0.1 .* succeeded!' client.err
	grep 'Subject: .*/OU=server/CN=127.0.0.1' client.err
	grep 'Issuer: .*/OU=server/CN=127.0.0.1' client.err
	# netcat waits for the other side to terminate, check it is sleeping
	${RUNNING_WAIT}
	ps -xww -o comm,stat,args | grep '^${NC:T} .*S.* -v -l 127'
	ps -xww -o comm,stat,args | grep '^${NC:T} .*S.* -v 127'

# TLS keep

REGRESS_TARGETS +=	run-tls-keep
run-tls-keep: 127.0.0.1.crt
	@echo '======== $@ ========'
	${SERVER_NC} -k -c -C 127.0.0.1.crt -K 127.0.0.1.key -n -v -l \
	    127.0.0.1 0 ${SERVER_BG}
	${LISTEN_WAIT}
	${PORT_GET}
	${CLIENT_NC} -c -R 127.0.0.1.crt -n -v 127.0.0.1 ${PORT} ${CLIENT_BG}
	${CONNECT_WAIT}
	${TLS_WAIT}
	${TRANSFER_WAIT}
	grep '^greeting$$' client.out
	grep '^command$$' server.out
	grep 'Listening on 127.0.0.1 ' server.err
	grep 'Connection received on 127.0.0.1 ' server.err
	grep 'Connection to 127.0.0.1 .* succeeded!' client.err
	grep 'Subject: .*/OU=server/CN=127.0.0.1' client.err
	grep 'Issuer: .*/OU=server/CN=127.0.0.1' client.err
	# kill client and reconnect with a new one
	:> server.err
	pkill -l -f "^${NC} .* 127.0.0.1 ${PORT}$$"
	rm -f client.{out,err}
	:> server.out
	# server closes the listen socket and binds a new one with new port
	${LISTEN_WAIT}
	${PORT_GET}
	${CLIENT_NC} -c -R 127.0.0.1.crt -n -v 127.0.0.1 ${PORT} ${CLIENT_BG}
	${CONNECT_WAIT}
	${TLS_WAIT}
	# server sends only one greeting, do not wait for a second one
	${TRANSFER_SERVER_WAIT}
	! grep 'greeting' client.out
	# truncation of log results in NUL bytes, do not match ^
	grep 'command$$' server.out
	grep 'Listening on 127.0.0.1 ' server.err
	grep 'Connection received on 127.0.0.1 ' server.err
	grep 'Connection to 127.0.0.1 .* succeeded!' client.err
	grep 'Subject: .*/OU=server/CN=127.0.0.1' client.err
	grep 'Issuer: .*/OU=server/CN=127.0.0.1' client.err

### UDP ####

REGRESS_TARGETS +=	run-udp
run-udp:
	@echo '======== $@ ========'
	${SERVER_NC} -u -n -v -l 127.0.0.1 0 ${SERVER_BG}
	${BIND_WAIT}
	${PORT_GET}
	# the -v option would cause udptest() to write additional X
	${CLIENT_NC} -u -n 127.0.0.1 ${PORT} ${CLIENT_BG}
	${TRANSFER_WAIT}
	grep '^greeting$$' client.out
	grep '^command$$' server.out
	grep 'Bound on 127.0.0.1 ' server.err
	grep 'Connection received on 127.0.0.1 ' server.err

REGRESS_TARGETS +=	run-udp6
run-udp6:
	@echo '======== $@ ========'
	${SERVER_NC} -u -n -v -l ::1 0 ${SERVER_BG}
	${BIND_WAIT}
	${PORT_GET}
	# the -v option would cause udptest() to write additional X
	${CLIENT_NC} -u -n ::1 ${PORT} ${CLIENT_BG}
	${TRANSFER_WAIT}
	grep '^greeting$$' client.out
	grep '^command$$' server.out
	grep 'Bound on ::1 ' server.err
	grep 'Connection received on ::1 ' server.err

REGRESS_TARGETS +=	run-udp-probe
run-udp-probe:
	@echo '======== $@ ========'
	${SERVER_NC} -u -n -v -l 127.0.0.1 0 ${SERVER_BG}
	${BIND_WAIT}
	${PORT_GET}
	${CLIENT_NC} -u -v -n 127.0.0.1 ${PORT} ${CLIENT_BG}
	${TRANSFER_WAIT}
	grep '^greeting$$' client.out
	# client sends 4 X UDP packets to check connection
	grep '^XXXXcommand$$' server.out
	grep 'Bound on 127.0.0.1 ' server.err
	grep 'Connection received on 127.0.0.1 ' server.err
	grep 'Connection to 127.0.0.1 .* succeeded!' client.err

# UDP resolver

REGRESS_TARGETS +=	run-udp-localhost
run-udp-localhost:
	@echo '======== $@ ========'
	${SERVER_NC} -u -4 -v -l localhost 0 ${SERVER_BG}
	${BIND_WAIT}
	${PORT_GET}
	# the -v option would cause udptest() to write additional X
	${CLIENT_NC} -u -4 localhost ${PORT} ${CLIENT_BG}
	${TRANSFER_WAIT}
	grep '^greeting$$' client.out
	grep '^command$$' server.out
	grep 'Bound on localhost ' server.err
	grep 'Connection received on localhost ' server.err

REGRESS_TARGETS +=	run-udp6-localhost
run-udp6-localhost:
	@echo '======== $@ ========'
	${SERVER_NC} -u -6 -v -l localhost 0 ${SERVER_BG}
	${BIND_WAIT}
	${PORT_GET}
	# the -v option would cause udptest() to write additional X
	${CLIENT_NC} -u -6 localhost ${PORT} ${CLIENT_BG}
	${TRANSFER_WAIT}
	grep '^greeting$$' client.out
	grep '^command$$' server.out
	grep 'Bound on localhost ' server.err
	grep 'Connection received on localhost ' server.err

# UDP keep

REGRESS_TARGETS +=	run-udp-keep
run-udp-keep:
	@echo '======== $@ ========'
	${SERVER_NC} -k -u -n -v -l 127.0.0.1 0 ${SERVER_BG}
	${BIND_WAIT}
	${PORT_GET}
	# the -v option causes udptest() to write additional X
	${CLIENT_NC} -u -n -v 127.0.0.1 ${PORT} ${CLIENT_BG}
	# server does not connect, nothing reaches the client
	${TRANSFER_SERVER_WAIT}
	! grep 'greeting' client.out
	grep '^XXXXcommand$$' server.out
	grep 'Bound on 127.0.0.1 ' server.err
	# client does not connect
	! grep 'Connection received on ' server.err
	grep 'Connection to 127.0.0.1 .* succeeded!' client.err
	# kill client and reconnect with a new one
	:> server.err
	pkill -l -f "^${NC} .* 127.0.0.1 ${PORT}$$"
	rm -f client.{out,err}
	:> server.out
	${CLIENT_NC} -u -n -v 127.0.0.1 ${PORT} ${CLIENT_BG}
	${TRANSFER_SERVER_WAIT}
	! grep 'greeting' client.out
	# truncation of log results in NUL bytes, do not match ^
	grep 'XXXXcommand$$' server.out
	# server keeps socket and does not bind again
	! grep 'Bound on ' server.err
	# client does not connect
	! grep 'Connection received on ' server.err
	grep 'Connection to 127.0.0.1 .* succeeded!' client.err

REGRESS_TARGETS +=	run-udp-sleep
run-udp-sleep:
	@echo '======== $@ ========'
	${SERVER_NC} -u -n -v -l 127.0.0.1 0 ${SERVER_BG}
	${BIND_WAIT}
	${PORT_GET}
	# the -v option would cause udptest() to write additional X
	${CLIENT_NC} -u -n 127.0.0.1 ${PORT} ${CLIENT_BG}
	${TRANSFER_WAIT}
	grep '^greeting$$' client.out
	grep '^command$$' server.out
	grep 'Bound on 127.0.0.1 ' server.err
	grep 'Connection received on 127.0.0.1 ' server.err
	# netcat waits for the other side to terminate, check it is sleeping
	${RUNNING_WAIT}
	ps -xww -o comm,stat,args | grep '^${NC:T} .*S.* -v -l 127'
	ps -xww -o comm,stat,args | grep '^${NC:T} .*S.* -n 127'

### UNIX ####

REGRESS_TARGETS +=	run-unix
run-unix:
	@echo '======== $@ ========'
	rm -f server.sock
	${SERVER_NC} -U -n -v -l server.sock ${SERVER_BG}
	${LISTEN_WAIT}
	${CLIENT_NC} -U -n -v server.sock ${CLIENT_BG}
	${TRANSFER_WAIT}
	grep '^greeting$$' client.out
	grep '^command$$' server.out
	# XXX message Bound and Listening is redundant
	grep 'Bound on server.sock$$' server.err
	grep 'Listening on server.sock$$' server.err
	grep 'Connection received on server.sock$$' server.err
	# XXX message succeeded is missing
	! grep 'Connection to server.sock .* succeeded!' client.err

REGRESS_TARGETS +=	run-unix-namelookup
run-unix-namelookup:
	@echo '======== $@ ========'
	rm -f server.sock
	${SERVER_NC} -U -v -l server.sock ${SERVER_BG}
	${LISTEN_WAIT}
	${CLIENT_NC} -U -v server.sock ${CLIENT_BG}
	${TRANSFER_WAIT}
	grep '^greeting$$' client.out
	grep '^command$$' server.out
	# XXX message Bound and Listening is redundant
	grep 'Bound on server.sock$$' server.err
	grep 'Listening on server.sock$$' server.err
	grep 'Connection received on server.sock$$' server.err
	# XXX message succeeded is missing
	! grep 'Connection to server.sock .* succeeded!' client.err

# UNIX keep

REGRESS_TARGETS +=	run-unix-keep
run-unix-keep:
	@echo '======== $@ ========'
	rm -f server.sock
	${SERVER_NC} -k -U -n -v -l server.sock ${SERVER_BG}
	${LISTEN_WAIT}
	${CLIENT_NC} -U -n -v server.sock ${CLIENT_BG}
	${TRANSFER_WAIT}
	grep '^greeting$$' client.out
	grep '^command$$' server.out
	# XXX message Bound and Listening is redundant
	grep 'Bound on server.sock$$' server.err
	grep 'Listening on server.sock$$' server.err
	grep 'Connection received on server.sock$$' server.err
	# XXX message succeeded is missing
	! grep 'Connection to server.sock .* succeeded!' client.err
	# kill client and reconnect with a new one
	:> server.err
	pkill -l -f "^${NC} .* -v server.sock$$"
	rm -f client.{out,err}
	:> server.out
	${CLIENT_NC} -U -n -v server.sock ${CLIENT_BG}
	# server sends only one greeting, do not wait for a second one
	${TRANSFER_SERVER_WAIT}
	! grep 'greeting' client.out
	# truncation of log results in NUL bytes, do not match ^
	grep 'command$$' server.out
	grep 'Connection received on server.sock$$' server.err
	# XXX message succeeded is missing
	! grep 'Connection to server.sock .* succeeded!' client.err

# UNIX dgram

REGRESS_TARGETS +=	run-unix-dgram
run-unix-dgram:
	@echo '======== $@ ========'
	rm -f {client,server}.sock
	${SERVER_NC} -U -u -n -v -l server.sock ${SERVER_BG}
	${BIND_WAIT}
	${CLIENT_NC} -U -u -n -v server.sock ${CLIENT_BG}
	${TRANSFER_WAIT}
	${BIND_CLIENT_WAIT}
	grep '^greeting$$' client.out
	grep '^command$$' server.out
	grep 'Bound on server.sock$$' server.err
	grep 'Connection received on server.sock$$' server.err
	# XXX message succeeded is missing
	! grep 'Connection to server.sock .* succeeded!' client.err

REGRESS_TARGETS +=	run-unix-dgram-namelookup
run-unix-dgram-namelookup:
	@echo '======== $@ ========'
	rm -f {client,server}.sock
	${SERVER_NC} -U -u -v -l server.sock ${SERVER_BG}
	${BIND_WAIT}
	${CLIENT_NC} -U -u -v server.sock ${CLIENT_BG}
	${TRANSFER_WAIT}
	${BIND_CLIENT_WAIT}
	grep '^greeting$$' client.out
	grep '^command$$' server.out
	grep 'Bound on server.sock$$' server.err
	grep 'Connection received on server.sock$$' server.err
	# XXX message succeeded is missing
	! grep 'Connection to server.sock .* succeeded!' client.err

REGRESS_TARGETS +=	run-unix-dgram-clientsock
run-unix-dgram-clientsock:
	@echo '======== $@ ========'
	rm -f {client,server}.sock
	${SERVER_NC} -U -u -n -v -l server.sock ${SERVER_BG}
	${BIND_WAIT}
	${CLIENT_NC} -U -u -n -v -s client.sock server.sock ${CLIENT_BG}
	${TRANSFER_WAIT}
	grep '^greeting$$' client.out
	grep '^command$$' server.out
	grep 'Bound on server.sock$$' server.err
	grep 'Connection received on server.sock$$' server.err
	# XXX message succeeded is missing
	! grep 'Connection to server.sock .* succeeded!' client.err

# UNIX dgram keep

REGRESS_TARGETS +=	run-unix-dgram-keep
run-unix-dgram-keep:
	@echo '======== $@ ========'
	rm -f {client,server}.sock
	${SERVER_NC} -k -U -u -n -v -l server.sock ${SERVER_BG}
	${BIND_WAIT}
	${CLIENT_NC} -U -u -n -v server.sock ${CLIENT_BG}
	# server does not connect, nothing reaches the client
	${TRANSFER_SERVER_WAIT}
	${BIND_CLIENT_WAIT}
	! grep 'greeting' client.out
	grep '^command$$' server.out
	grep 'Bound on server.sock$$' server.err
	# client does not connect
	! grep 'Connection received on ' server.err
	# XXX message succeeded is missing
	! grep 'Connection to server.sock .* succeeded!' client.err
	# kill client and reconnect with a new one
	:> server.err
	pkill -l -f "^${NC} .* -v server.sock$$"
	rm -f client.{out,err}
	:> server.out
	${CLIENT_NC} -U -u -n -v server.sock ${CLIENT_BG}
	${TRANSFER_SERVER_WAIT}
	${BIND_CLIENT_WAIT}
	! grep 'greeting' client.out
	# truncation of log results in NUL bytes, do not match ^
	grep 'command$$' server.out
	# server keeps socket and does not bind again
	! grep 'Bound on ' server.err
	# client does not connect
	! grep 'Connection received on ' server.err
	# XXX message succeeded is missing
	! grep 'Connection to 127.0.0.1 .* succeeded!' client.err

### TCP with test peer

REGRESS_TARGETS +=	run-tcp-test
run-tcp-test: server-tcp client-tcp
	@echo '======== $@ ========'
	# test the test tools
	./server-tcp -s greeting -r command 127.0.0.1 0 >server.port
	./client-tcp -r greeting -s command 127.0.0.1 ${PORT} >client.port

REGRESS_TARGETS +=	run-tcp-test-shutdown
run-tcp-test-shutdown: server-tcp client-tcp
	@echo '======== $@ ========'
	# test the test tools
	./server-tcp -s greeting -N -r command -E 127.0.0.1 0 >server.port
	./client-tcp -r greeting -E -s command -N 127.0.0.1 ${PORT} >client.port

# TCP netcat server with test client

REGRESS_TARGETS +=	run-tcp-server
run-tcp-server: client-tcp
	@echo '======== $@ ========'
	${SERVER_NC} -n -v -l 127.0.0.1 0 ${SERVER_BG}
	${LISTEN_WAIT}
	${PORT_GET}
	# test client read from netcat, then send line and exit
	./client-tcp -r greeting -s command 127.0.0.1 ${PORT} >client.port
	${TRANSFER_SERVER_WAIT}
	grep '^command$$' server.out
	grep 'Listening on 127.0.0.1 ' server.err
	grep 'Connection received on 127.0.0.1 ' server.err

REGRESS_TARGETS +=	run-tcp-server-eof
run-tcp-server-eof: client-tcp
	@echo '======== $@ ========'
	${SERVER_NC} -n -v -l 127.0.0.1 0 ${SERVER_BG}
	${LISTEN_WAIT}
	${PORT_GET}
	# test client read from netcat, then send line, shutdown, wait for eof
	./client-tcp -r greeting -s command -N -E 127.0.0.1 ${PORT} >client.port
	${TRANSFER_SERVER_WAIT}
	grep '^command$$' server.out
	grep 'Listening on 127.0.0.1 ' server.err
	grep 'Connection received on 127.0.0.1 ' server.err

REGRESS_TARGETS +=	run-tcp-server-reverse-eof
run-tcp-server-reverse-eof: client-tcp
	@echo '======== $@ ========'
	${SERVER_NC} -n -v -l 127.0.0.1 0 ${SERVER_BG}
	${LISTEN_WAIT}
	${PORT_GET}
	# test client send to netcat, shutdown, then read line, wait for eof
	./client-tcp -s command -N -r greeting -E 127.0.0.1 ${PORT} >client.port
	${TRANSFER_SERVER_WAIT}
	grep '^command$$' server.out
	grep 'Listening on 127.0.0.1 ' server.err
	grep 'Connection received on 127.0.0.1 ' server.err

REGRESS_TARGETS +=	run-tcp-server-shutdown-eof
run-tcp-server-shutdown-eof: client-tcp
	@echo '======== $@ ========'
	# netcat calls shutdown on output after EOF on input
	${SERVER_NC} -N -n -v -l 127.0.0.1 0 ${SERVER_BG}
	${LISTEN_WAIT}
	${PORT_GET}
	# test client read from netcat, then send line, wait for eof, shutdown
	./client-tcp -r greeting -s command -E -N 127.0.0.1 ${PORT} >client.port
	${TRANSFER_SERVER_WAIT}
	grep '^command$$' server.out
	grep 'Listening on 127.0.0.1 ' server.err
	grep 'Connection received on 127.0.0.1 ' server.err

REGRESS_TARGETS +=	run-tcp-server-shutdown-reverse-eof
run-tcp-server-shutdown-reverse-eof: client-tcp
	@echo '======== $@ ========'
	# netcat calls shutdown on output after EOF on input
	${SERVER_NC} -N -n -v -l 127.0.0.1 0 ${SERVER_BG}
	${LISTEN_WAIT}
	${PORT_GET}
	# test client send to netcat, shutdown, then read line, wait for eof
	./client-tcp -s command -N -r greeting -E 127.0.0.1 ${PORT} >client.port
	${TRANSFER_SERVER_WAIT}
	grep '^command$$' server.out
	grep 'Listening on 127.0.0.1 ' server.err
	grep 'Connection received on 127.0.0.1 ' server.err

# TCP netcat client with test server

REGRESS_TARGETS +=	run-tcp-client
run-tcp-client: server-tcp
	@echo '======== $@ ========'
	# test server send to netcat, then read line and exit
	./server-tcp -s greeting -r command 127.0.0.1 0 >server.port
	${CLIENT_NC} -n -v 127.0.0.1 ${PORT} ${CLIENT_BG}
	${CONNECT_WAIT}
	${TRANSFER_CLIENT_WAIT}
	grep '^greeting$$' client.out
	grep 'Connection to 127.0.0.1 .* succeeded!' client.err

REGRESS_TARGETS +=	run-tcp-client-eof
run-tcp-client-eof: server-tcp
	@echo '======== $@ ========'
	# test server send to netcat, shutdown, then read line, wait for eof
	./server-tcp -s greeting -N -r command -E 127.0.0.1 0 >server.port
	${CLIENT_NC} -n -v 127.0.0.1 ${PORT} ${CLIENT_BG}
	${CONNECT_WAIT}
	${TRANSFER_CLIENT_WAIT}
	grep '^greeting$$' client.out
	grep 'Connection to 127.0.0.1 .* succeeded!' client.err

REGRESS_TARGETS +=	run-tcp-client-reverse-eof
run-tcp-client-reverse-eof: server-tcp
	@echo '======== $@ ========'
	# test server read from netcat, then read line, wait for eof, shutdown
	./server-tcp -r command -s greeting -E -N 127.0.0.1 0 >server.port
	${CLIENT_NC} -n -v 127.0.0.1 ${PORT} ${CLIENT_BG}
	${CONNECT_WAIT}
	${TRANSFER_CLIENT_WAIT}
	grep '^greeting$$' client.out
	grep 'Connection to 127.0.0.1 .* succeeded!' client.err

REGRESS_TARGETS +=	run-tcp-client-shutdown-eof
run-tcp-client-shutdown-eof: server-tcp
	@echo '======== $@ ========'
	# test server send to netcat, shutdown, then read line, wait for eof
	./server-tcp -s greeting -N -r command -E 127.0.0.1 0 >server.port
	# netcat calls shutdown on output after EOF on input
	${CLIENT_NC} -N -n -v 127.0.0.1 ${PORT} ${CLIENT_BG}
	${CONNECT_WAIT}
	${TRANSFER_CLIENT_WAIT}
	grep '^greeting$$' client.out
	grep 'Connection to 127.0.0.1 .* succeeded!' client.err

REGRESS_TARGETS +=	run-tcp-client-shutdown-reverse-eof
run-tcp-client-shutdown-reverse-eof: server-tcp
	@echo '======== $@ ========'
	# test server read from netcat, wait for eof, then read line, shutdown
	./server-tcp -r command -E -s greeting -N 127.0.0.1 0 >server.port
	# netcat calls shutdown on output after EOF on input
	${CLIENT_NC} -N -n -v 127.0.0.1 ${PORT} ${CLIENT_BG}
	${CONNECT_WAIT}
	${TRANSFER_CLIENT_WAIT}
	grep '^greeting$$' client.out
	grep 'Connection to 127.0.0.1 .* succeeded!' client.err

REGRESS_EXPECTED_FAILURES = \
	run-tcp-server-shutdown-eof \
	run-tcp-server-shutdown-reverse-eof \
	run-tcp-client-shutdown-eof \
	run-tcp-client-shutdown-reverse-eof \
	run-tcp-sleep \
	run-tls-sleep \
	run-udp-sleep

.PHONY: ${REGRESS_SETUP} ${REGRESS_CLEANUP} ${REGRESS_TARGETS}

### create certificates for TLS

CLEANFILES +=		{127.0.0.1,1}.{crt,key} \
			ca.{crt,key,srl,hash} fake-ca.{crt,key,hash} \
			{client,server}.{req,crt,key,hash}

127.0.0.1.crt:
	openssl req -batch -new \
	    -subj /L=OpenBSD/O=netcat-regress/OU=server/CN=${@:R}/ \
	    -nodes -newkey rsa -keyout ${@:R}.key -x509 -out $@

1.crt:
	openssl req -batch -new \
	    -subj /L=OpenBSD/O=netcat-regress/OU=server/CN=::1/ \
	    -nodes -newkey rsa -keyout 1.key -x509 -out $@

ca.crt fake-ca.crt:
	openssl req -batch -new \
	    -subj /L=OpenBSD/O=netcat-regress/OU=ca/CN=root/ \
	    -nodes -newkey rsa -keyout ${@:R}.key -x509 -out $@

client.req server.req:
	openssl req -batch -new \
	    -subj /L=OpenBSD/O=netcat-regress/OU=${@:R}/CN=localhost/ \
	    -nodes -newkey rsa -keyout ${@:R}.key -out $@

client.crt server.crt: ca.crt ${@:R}.req
	openssl x509 -CAcreateserial -CAkey ca.key -CA ca.crt \
	    -req -in ${@:R}.req -out $@

client.hash server.hash ca.hash: ${@:R}.crt
	openssl x509 -in ${@:R}.crt -outform der | sha256 | sed s/^/SHA256:/ >$@

.include <bsd.regress.mk>