-
Notifications
You must be signed in to change notification settings - Fork 12
/
tcp2_check.ttcnpp
1299 lines (1063 loc) · 27.1 KB
/
tcp2_check.ttcnpp
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
/* -*- c-basic-offset: 4; tab-width: 4; -*- */
/*
* Copyright © 2018-2020, Intel Corporation.
*
* SPDX-License-Identifier: LGPL-2.1-only
*/
/*
* This is a sanity check for experimental TCP for Zephyr OS:
* https://github.com/ozhuraki/zephyr/blob/tcp2-dev/README.md
*/
#include "tcp2_check.ttcnin"
module tcp2_check {
import from libtypes all;
import from libnet all;
import from libtest all;
import from libutils all;
import from tcp2_utils all;
type component tcp_ct extends test_ct {
var tcp_state_t state := TCP_LISTEN;
var u32 seq := 0;
var u32 ack := 0;
var octetstring snd := ''O;
var octetstring rcv := ''O;
var fuzzer_ct ct_fuzzer;
port port_cf P_FZ;
var tcp_t tcp; /* TODO: Drop */
}
/**
* Suite's active connection establishment, optional data, and active close
*/
function tcp_connect(octetstring data_to_send := ''O,
octetstring data_to_receive := ''O,
boolean close := true,
u32 isn := 0,
template tcp_options_t options := omit) runs on tcp_ct
{
seq := isn;
tcp_init();
P_TCP.send(ts_TCP(fl := SYN, seq := u32_post_inc(conn(seq)),
options := options));
conn_state(TCP_SYN_SENT, fail_on := EV_TIMEOUT or4b EV_UNEXPECTED_MSG);
T.start;
P_TCP.receive(tr_TCP(fl := SYN or4b ACK, ack := conn(seq))) -> value tcp {
T.stop;
peer_assert(TCP_SYN_RECEIVED);
conn_state(TCP_SYN_RECEIVED);
ack := tcp.th_seq;
P_TCP.send(ts_TCP(fl := ACK, seq := conn(seq),
ack := u32_pre_inc(conn(ack))));
conn_state(TCP_ESTABLISHED, fail_on := EV_NONE);
if (false == close) {
goto end;
}
}
label est;
peer_assert(TCP_ESTABLISHED);
if (lengthof(snd) > 0) { goto wait_for_data; }
if (lengthof(data_to_send) > 0) {
snd := byte_array_remove_range(data_to_send, index := 0, len := 1);
P_TCP.send(ts_TCP(fl := PSH,
seq := u32_post_inc(conn(seq), lengthof(snd)),
data := snd));
goto wait_for_data;
}
/* Everything is sent and aknowledged, do active close */
P_TCP.send(ts_TCP(fl := FIN or4b ACK, seq := u32_post_inc(conn(seq)),
ack := conn(ack)));
conn_state(TCP_FIN_WAIT_1, fail_on := EV_TIMEOUT or4b EV_UNEXPECTED_MSG);
/* TODO: peer_assert() here is tricky, since peer in active close
sends ack and fin+ack immedeately */
label wait_for_data;
T.start;
alt {
[TCP_ESTABLISHED == state] P_TCP.receive(tr_TCP) -> value tcp {
T.stop;
peer_assert(TCP_ESTABLISHED);
var tcp_t tcp2 := valueof(ts_TCP);
if (th_is_set(tcp, PSH)) {
byte_array_append(rcv, tcp.data);
th_set(tcp2, ACK);
u32_pre_inc(conn(ack), lengthof(tcp.data));
}
if (th_is_set(tcp, ACK)) {
if (tcp.th_ack > seq - lengthof(snd)) {
var integer diff := tcp.th_ack - (seq - lengthof(snd));
byte_array_remove_range(snd, index := 0, len := diff);
}
}
if (tcp2.th_flags != TH_NONE) {
tcp2.th_seq := conn(seq);
tcp2.th_ack := conn(ack);
P_TCP.send(tcp2);
}
T.start; repeat;
}
[TCP_ESTABLISHED == state] T.timeout {
goto est;
}
[TCP_FIN_WAIT_1 == state] P_TCP.receive(tr_TCP(fl := FIN or4b ACK,
seq := conn(ack),
ack := conn(seq))) {
T.stop;
peer_assert(TCP_LAST_ACK);
conn_state(TCP_CLOSED, fail_on := EV_NONE);
P_TCP.send(ts_TCP(fl := ACK,
seq := conn(seq),
ack := u32_pre_inc(conn(ack))));
}
} /* end of alt */
peer_assert(TCP_CLOSED);
if (TCP_CLOSED == state and 0 == lengthof(data_to_send) and
0 == lengthof(snd) and rcv == data_to_receive) {
setverdict(pass);
} else {
log("Error: data_to_receive: ", data_to_receive, " rcv: ", rcv);
setverdict(fail);
}
tp_send(TP_COMMAND, "CLOSE");
label end;
}
/**
* Suite's passive connection establishment, optional data and close
*/
function tcp_listen(octetstring data_to_send := ''O,
octetstring data_to_receive := ''O,
boolean close := true,
u32 isn := 0) runs on tcp_ct
{
tcp_init();
tp_send(TP_COMMAND, "CONNECT", d := oct2str(data_to_receive), isn := isn);
conn_state(TCP_LISTEN, fail_on := EV_TIMEOUT or4b EV_UNEXPECTED_MSG);
T.start;
alt {
[TCP_LISTEN == state] P_TCP.receive(tr_TCP(fl := SYN)) -> value tcp {
T.stop;
peer_assert(TCP_SYN_SENT);
conn_state(TCP_SYN_RECEIVED);
ack := tcp.th_seq;
P_TCP.send(ts_TCP(fl := SYN or4b ACK, seq := u32_post_inc(conn(seq)),
ack := u32_pre_inc(conn(ack))));
conn_state(TCP_SYN_SENT);
T.start; repeat;
}
[TCP_SYN_SENT == state] P_TCP.receive(tr_TCP(fl := ACK,
ack := conn(seq))) -> value tcp {
T.stop;
conn_state(TCP_SYN_RECEIVED);
ack := tcp.th_seq;
conn_state(TCP_ESTABLISHED, fail_on := EV_NONE);
if (lengthof(data_to_send) > 0) {
tp_send(TP_COMMAND, "SEND", oct2str(data_to_send));
}
if (close == false) {
goto end;
}
if (lengthof(data_to_receive) > 0) {
T.start; repeat;
}
}
[TCP_ESTABLISHED == state] P_TCP.receive(tr_TCP(data := ?)) -> value tcp {
if (th_is_set(tcp, PSH) or th_is_set(tcp, ACK)) {
T.stop;
peer_assert(TCP_ESTABLISHED);
P_TCP.send(ts_TCP(fl := ACK, seq := conn(seq),
ack := u32_pre_inc(conn(ack),
lengthof(tcp.data))));
byte_array_append(rcv, tcp.data);
if (rcv != data_to_receive) {
T.start; repeat;
}
}
}
} /* end of alt */
peer_assert(TCP_ESTABLISHED);
P_TCP.send(ts_TCP(fl := FIN or4b ACK, seq := u32_post_inc(conn(seq)),
ack := conn(ack)));
conn_state(TCP_FIN_WAIT_1, fail_on := EV_TIMEOUT or4b EV_UNEXPECTED_MSG);
T.start;
alt {
[TCP_FIN_WAIT_1 == state] P_TCP.receive(tr_TCP(fl := FIN or4b ACK,
seq := conn(ack),
ack := conn(seq))) {
T.stop;
peer_assert(TCP_LAST_ACK);
conn_state(TCP_CLOSED, fail_on := EV_NONE);
P_TCP.send(ts_TCP(fl := ACK,
seq := conn(seq),
ack := u32_pre_inc(conn(ack))));
}
} /* end of alt */
peer_assert(TCP_CLOSED);
if (state == TCP_CLOSED and rcv == data_to_receive) {
setverdict(pass);
} else {
log("Error: data_to_receive: ", data_to_receive, " rcv: ", rcv);
setverdict(fail);
}
tp_send(TP_COMMAND, "CLOSE");
label end;
}
/**
* Send an IPv4 with IPPROTO_TCP but empty IP payload
*/
testcase tcp_undersized() runs on tcp_ct
{
tcp_init();
P_IP.send(valueof(ts_IP(IPPROTO_TCP, ''O)));
if (do_ping() == true) { /* Ternary operator isn't supported */
setverdict(pass);
} else {
setverdict(fail);
}
}
/**
* Test a reaction on SYN with incorrect th_off
*/
testcase tcp_incorrect_offset() runs on tcp_ct
{
tcp_init();
P_TCP2.send(ts_TCP2(fl := SYN, seq := u32_post_inc(conn(seq)), off := 3));
T.start;
alt {
[] P_TCP.receive(tr_TCP(fl := RST)) {
T.stop;
setverdict(pass);
}
[] T.timeout {
setverdict(fail);
}
} /* end of alt */
tp_send(TP_COMMAND, "CLOSE");
}
testcase tcp_no_flags() runs on tcp_ct
{
tcp_init();
P_TCP.send(ts_TCP(fl := TH_NONE));
if (do_ping() == true) {
setverdict(pass);
} else {
setverdict(fail);
}
}
/**
* Test active TCP connection establishment and active close
*/
testcase tcp_connect_no_data_close() runs on tcp_ct
{
tcp_connect(data_to_send := ''O);
}
/**
* Test that RST works in ESTABLISHED
*/
testcase tcp_connect_no_data_reset() runs on tcp_ct
{
var udp_t udp;
var tp_new_t tp;
tcp_connect(close := false);
tcp_config({{ "tp_trace", "1" }});
P_TCP.send(ts_TCP(fl := RST, seq := conn(seq)));
T.start;
alt {
[] P_UDP.receive(?) -> value udp {
T.stop;
tp := f_tp_new_dec(udp.data);
if (tp.data[0].val == "CONN_DELETE") {
setverdict(pass);
}
}
[] T.timeout { setverdict(fail); }
} /* end of alt */
tp_send(TP_COMMAND, "CLOSE");
}
/**
* Test suite's active TCP connection establishment,
* data transfer (echo expected) and suite's active close
*/
testcase tcp_connect_data_close() runs on tcp_ct
{
tcp_connect(data_to_send := '01'O, data_to_receive := '01'O);
}
testcase tcp_connect_data_reset() runs on tcp_ct
{
var octetstring data := '01'O;
tcp_connect(data_to_send := data, data_to_receive := data, close := false);
P_TCP.send(ts_TCP(fl := RST));
T.start;
T.timeout;
peer_assert(TCP_CLOSED);
setverdict(pass); /* this won't override the failure of peer_assert() */
tp_send(TP_COMMAND, "CLOSE");
}
/**
* Test active TCP connection establishment, "more data" and active close
*/
testcase tcp_connect_more_data_close() runs on tcp_ct
{
tcp_connect(data_to_send := '010203'O, data_to_receive := '010203'O);
}
/**
* Test that peer sends RST on missing data in ESTABLISHED
*/
testcase tcp_connect_data_gap() runs on tcp_ct
{
var udp_t udp;
var tp_new_t tp;
var boolean reset_received := false;
tcp_connect(close := false);
tcp_config({{ "tp_trace", "1" }});
P_TCP.send(ts_TCP(fl := PSH, seq := conn(seq) + 42, data := '01'O));
T.start;
alt {
[] P_TCP.receive(tr_TCP(fl := RST)) {
T.stop;
reset_received := true;
T.start; repeat;
}
[reset_received] P_UDP.receive(?) -> value udp {
T.stop;
tp := f_tp_new_dec(udp.data);
if (tp.data[0].val == "CONN_DELETE") {
setverdict(pass);
}
}
[] T.timeout { setverdict(fail); }
} /* end of alt */
tp_send(TP_COMMAND, "CLOSE");
}
/**
* Test passive TCP connection establishment and active close
*/
testcase tcp_listen_no_data_close() runs on tcp_ct
{
tcp_listen(data_to_receive := ''O);
}
/**
* Test that RST works in ESTABLISHED
*/
testcase tcp_listen_no_data_reset() runs on tcp_ct
{
var udp_t udp;
var tp_new_t tp;
tcp_listen(close := false);
tcp_config({{ "tp_trace", "1" }});
P_TCP.send(ts_TCP(fl := RST, seq := conn(seq)));
T.start;
alt {
[] P_UDP.receive(?) -> value udp {
T.stop;
tp := f_tp_new_dec(udp.data);
if (tp.data[0].val == "CONN_DELETE") {
setverdict(pass);
}
}
[] T.timeout { setverdict(fail); }
} /* end of alt */
tp_send(TP_COMMAND, "CLOSE");
}
/**
* Test passive TCP connection establishment, data and active close
*/
testcase tcp_listen_data_close() runs on tcp_ct
{
tcp_listen(data_to_send := '01'O, data_to_receive := '01'O);
}
testcase tcp_listen_data_reset() runs on tcp_ct
{
tcp_listen(data_to_send := '01'O, data_to_receive := '01'O, close := false);
P_TCP.send(ts_TCP(fl := RST));
T.start;
T.timeout;
peer_assert(TCP_CLOSED);
setverdict(pass); /* this won't override the failure of peer_assert() */
tp_send(TP_COMMAND, "CLOSE");
}
/**
* Test passive TCP connection establishment, data and active close
*/
testcase tcp_listen_more_data_close() runs on tcp_ct
{
tcp_listen(data_to_send := '010203'O, data_to_receive := '010203'O);
}
/**
* Test sequence wraps/overflows
*/
testcase tcp_connect_seq_wrap() runs on tcp_ct
{
tcp_connect(data_to_send := '01'O, data_to_receive := '01'O,
isn := hex2int('FFFFFFFE'H));
}
testcase tcp_listen_seq_wrap() runs on tcp_ct
{
tcp_listen(data_to_send := '01'O, data_to_receive := '01'O,
isn := hex2int('FFFFFFFE'H));
}
/**
* Test peer's retransmission
*/
testcase tcp_retransmit() runs on tcp_ct
{
tcp_init();
fuzzer_on();
P_FZ.send("incoming_drop");
P_TCP.send(ts_TCP(fl := SYN, seq := post_inc(conn(seq))));
conn_state(TCP_SYN_SENT);
T.start(T_default * 3.0);
T.timeout;
P_FZ.send("incoming_pass");
T.start(T_default * 3.0);
alt {
[] P_TCP.receive(tr_TCP(fl := SYN or4b ACK,
ack := conn(seq))) -> value tcp {
T.stop;
ack := tcp.th_seq;
P_TCP.send(ts_TCP(fl := ACK, seq := conn(seq), ack := pre_inc(conn(ack))));
setverdict(pass);
}
[] T.timeout {
setverdict(fail);
}
}
T.start(T_default);
T.timeout;
conn_state(TCP_ESTABLISHED, fail_on := EV_NONE);
tp_send(TP_COMMAND, "CLOSE");
}
type record of octetstring tcp_test_data_t;
/**
* Test the reception of data with variable chunks, verify peer's seq and ack,
* and check what the peer has received with the TP_COMMAND/RECV
*/
/* TODO: Enhance this one with out of order data */
testcase tcp_peer_recv() runs on tcp_ct
{
var tcp_test_data_t data := { '01'O, '0203'O, '040506'O };
var octetstring data_total := ''O;
tcp_connect(close := false);
tcp_config({{ "tcp_echo", "0" }});
for (var integer i := 0; i < sizeof(data); i := i + 1) {
data_total := data_total & data[i];
tcp_send(data[i]);
}
var tp_t peer := tp_send(TP_COMMAND, "RECV");
if (str2oct(peer.data) == data_total) {
setverdict(pass);
}
tp_send(TP_COMMAND, "CLOSE");
}
/**
* Test peer's tcp_send() functionality
*/
testcase tcp_peer_send() runs on tcp_ct
{
tcp_connect(close := false);
var octetstring data := '01'O;
tp_send(TP_COMMAND, "SEND", oct2str(data));
T.start;
alt {
[state == TCP_ESTABLISHED] P_TCP.receive(tr_TCP(fl := PSH, seq := conn(seq),
data := data)) {
T.stop;
setverdict(pass);
}
[state == TCP_ESTABLISHED] T.timeout {
setverdict(fail);
}
} /* end of alt */
peer_assert(TCP_ESTABLISHED);
tp_send(TP_COMMAND, "CLOSE");
}
/**
* Send an undersized data in ESTABLISHED
*/
testcase tcp_data_undersized() runs on tcp_ct
{
tcp_connect(close := false);
P_TCP.send(ts_TCP(fl := PSH, seq := conn(seq), data := ''O));
T.start;
alt {
[state == TCP_ESTABLISHED] P_TCP.receive(tr_TCP(fl := RST)) {
T.stop;
peer_assert(TCP_CLOSED);
setverdict(pass);
}
[state == TCP_ESTABLISHED] T.timeout {
setverdict(fail);
}
} /* end of alt */
tp_send(TP_COMMAND, "CLOSE");
}
/**
* Test a reaction on TCP data packets with options
*/
testcase tcp_data_options() runs on tcp_ct
{
tcp_connect(close := false);
var octetstring data_to_send := '010203'O;
tcp := valueof(ts_TCP(fl := PSH,
seq := u32_post_inc(conn(seq), lengthof(data_to_send)),
data := data_to_send));
tcp.options := {
{ TO_MSS, 4, int2oct(1460, 2) }
}
P_TCP.send(tcp);
T.start;
alt {
[state == TCP_ESTABLISHED] P_TCP.receive(tr_TCP(fl := ACK, ack := conn(seq))) {
T.stop;
T.start; repeat;
}
[state == TCP_ESTABLISHED] P_TCP.receive(tr_TCP) -> value tcp {
T.stop;
if (th_is_set(tcp, PSH) and tcp.data == data_to_send) {
setverdict(pass);
goto end;
}
}
[state == TCP_ESTABLISHED] T.timeout {
setverdict(fail);
goto end;
}
} /* end of alt */
peer_assert(TCP_ESTABLISHED);
label end;
tp_send(TP_COMMAND, "CLOSE");
}
/**
* Test a reaction on TCP data packets with incorrect options
*/
testcase tcp_data_incorrect_options() runs on tcp_ct
{
tcp_connect(close := false);
var octetstring data_to_send := '010203'O;
tcp := valueof(ts_TCP(fl := PSH,
seq := u32_post_inc(conn(seq), lengthof(data_to_send)),
data := data_to_send));
tcp.options := {
{ TO_MSS, 3, '00'O } /* Incorrect option length */
}
P_TCP.send(tcp);
T.start;
alt {
[state == TCP_ESTABLISHED] P_TCP.receive(tr_TCP(fl := RST)) {
T.stop;
setverdict(pass);
}
[state == TCP_ESTABLISHED] T.timeout {
setverdict(fail);
goto end;
}
} /* end of alt */
peer_assert(TCP_CLOSED);
label end;
tp_send(TP_COMMAND, "CLOSE");
}
/**
* Test a reaction on TCP data with incorrect offset
*/
testcase tcp_data_incorrect_offset() runs on tcp_ct
{
var octetstring data_to_send := '010203'O;
tcp_connect(close := false);
var tcp2_t tcp2 := valueof(ts_TCP2(fl := PSH,
seq := u32_post_inc(conn(seq), lengthof(data_to_send)),
off := 3,
data := data_to_send));
P_TCP2.send(tcp2);
T.start;
alt {
[state == TCP_ESTABLISHED] P_TCP.receive(tr_TCP(fl := RST)) {
T.stop;
peer_assert(TCP_CLOSED);
setverdict(pass);
}
[state == TCP_ESTABLISHED] T.timeout {
setverdict(fail);
}
} /* end of alt */
tp_send(TP_COMMAND, "CLOSE");
}
testcase tcp_connect_isn_no_data() runs on tcp_ct
{
tcp_connect(isn := 333);
}
testcase tcp_connect_isn_data() runs on tcp_ct
{
var octetstring data := '01'O;
tcp_connect(data_to_send := data, data_to_receive := data, isn := 333);
}
testcase tcp_connect_isn_more_data() runs on tcp_ct
{
var octetstring data := '010203'O;
tcp_connect(data_to_send := data, data_to_receive := data, isn := 333);
}
testcase tcp_listen_isn_no_data() runs on tcp_ct
{
tcp_listen(isn := 333);
}
testcase tcp_listen_isn_data() runs on tcp_ct
{
tcp_listen(data_to_send := '01'O, data_to_receive := '01'O, isn := 333);
}
testcase tcp_listen_isn_more_data() runs on tcp_ct
{
tcp_listen(data_to_send := '010203'O, data_to_receive := '010203'O, isn := 333);
}
/*
* Test peer's active close:
*
* - Check that peer is in FIN_WAIT_1 after it sends FIN+ACK
* - Check that peer is in TIME_WAIT after the reception of FIN+ACK
*/
testcase tcp_peer_close() runs on tcp_ct
{
tcp_connect(close := false);
tp_send(TP_COMMAND, "CLOSE2");
T.start;
alt {
[state == TCP_ESTABLISHED] P_TCP.receive(tr_TCP(fl := FIN or4b ACK,
seq := conn(ack),
ack := conn(seq))) {
T.stop;
peer_assert(TCP_FIN_WAIT_1);
conn_state(TCP_CLOSING);
P_TCP.send(ts_TCP(fl := FIN or4b ACK,
seq := conn(seq),
ack := u32_pre_inc(conn(ack))));
T.start(T_default/4.0); repeat;
}
[state == TCP_CLOSING] T.timeout {
peer_assert(TCP_TIME_WAIT);
setverdict(pass);
}
}
tp_send(TP_COMMAND, "CLOSE");
}
/*
* Test peer's retransmission in active close:
*
* - Check that peer retransmits in FIN_WAIT_1 state
* - Check that peer deletes the connection on no response
*/
testcase tcp_peer_close_retransmit() runs on tcp_ct
{
#define TCP_RETRIES 1
var udp_t udp;
var tp_new_t tp;
var integer replies := 0;
tcp_connect(close := false);
tcp_config({
{ "tp_trace", "1" },
{ "tcp_rto", int2str(TO_MSEC(T_default/4.0)) },
{ "tcp_retries", int2str(TCP_RETRIES) }
});
tp_send(TP_COMMAND, "CLOSE2");
T.start;
alt {
[state == TCP_ESTABLISHED] P_TCP.receive(tr_TCP(fl := FIN or4b ACK,
seq := conn(ack),
ack := conn(seq))) {
T.stop;
replies := replies + 1;
T.start; repeat;
}
[] P_UDP.receive(?) -> value udp {
T.stop;
tp := f_tp_new_dec(udp.data);
if (tp.data[0].val == "CONN_DELETE" and replies == (TCP_RETRIES + 1)) {
setverdict(pass);
}
}
}
tp_send(TP_COMMAND, "CLOSE");
#undef TCP_RETRIES
}
testcase tcp_close2() runs on tcp_ct
{
tcp_connect(close := false);
tp_send(TP_COMMAND, "CLOSE2");
P_TCP.receive(tr_TCP(fl := FIN or4b ACK, seq := conn(ack),
ack := conn(seq)));
P_TCP.send(ts_TCP(fl := ACK, seq := conn(seq),
ack := u32_pre_inc(conn(ack))));
}
/**
* This sketch is used to complete egress TCP data path at the peer.
*/
testcase tcp_send2() runs on tcp_ct
{
tcp_connect(close := false);
fuzzer_on();
P_FZ.send("incoming_drop");
var octetstring data := '010203'O;
while (lengthof(data) > 0) {
tp_send(TP_COMMAND, "SEND",
oct2str(byte_array_remove_range(data, 0, 1)));
}
peer_assert(TCP_ESTABLISHED);
setverdict(pass);
tp_send(TP_COMMAND, "CLOSE");
}
testcase tcp_rtt() runs on tcp_ct
{
tcp_init();
const integer N := 20;
var integer i := N;
var float total := 0.0, min := 1000.0, max := 0.0, rtt;
while (post_dec(i) > 0) {
T.start;
if (do_ping(t:= 1000.0) == false) {
setverdict(fail);
}
rtt := T.read;
T.stop;
//log("rtt: ", rtt);
total := total + rtt;
if (rtt > max) {
max := rtt;
}
if (rtt < min) {
min := rtt;
}
}
log("avg: ", total / int2float(N));
log("min: ", min);
log("max: ", max);
i := N;
total := 0.0;
min := 1000.0;
max := 0.0;
while (post_dec(i) > 0) {
T.start;
tcp_config({{ "tcp_echo", "0" }});
alt {
[] P_UDP.receive(?) {
rtt := T.read;
T.stop;
//log("rtt: ", rtt);
total := total + rtt;
if (rtt > max) {
max := rtt;
}
if (rtt < min) {
min := rtt;
}
}
[] T.timeout {
setverdict(fail);
}
} /* end of alt */
}
log("avg: ", total / int2float(N));
log("min: ", min);
log("max: ", max);
}
testcase tcp_connect_early_data() runs on tcp_ct
{
var octetstring data := '01'O;
tcp_init();
tcp_config({{ "tcp_echo", "0" }});
P_TCP.send(ts_TCP(fl := SYN, seq := u32_post_inc(conn(seq))));
conn_state(TCP_SYN_SENT, fail_on := EV_TIMEOUT or4b EV_UNEXPECTED_MSG);
T.start;
P_TCP.receive(tr_TCP(fl := SYN or4b ACK, ack := conn(seq))) -> value tcp {
T.stop;
ack := tcp.th_seq;
P_TCP.send(ts_TCP(fl := ACK or4b PSH, seq := conn(seq),
ack := u32_pre_inc(conn(ack)), data := data));
}
T.start;
T.timeout;
var tp_t peer := tp_send(TP_COMMAND, "RECV");
if (str2oct(peer.data) == data) {
setverdict(pass);
}
tp_send(TP_COMMAND, "CLOSE");
}
/**
* This is just a convenience for development/debug purposes
*/
testcase tp_close() runs on tcp_ct
{
tcp_init(config := false);
tp_send(TP_COMMAND, "CLOSE");
}
function tcp_close() runs on tcp_ct
{
P_TCP.send(ts_TCP(fl := RST, seq := conn(seq)));
}
/**
* This sketch is used to complete ingress TCP data path at the peer.
*/
testcase tcp_connect2() runs on tcp_ct
{
tcp_connect(close := false);
P_TCP.send(ts_TCP(fl := PSH, seq := u32_post_inc(conn(seq)),
data := '01'O));
T.start(1.0);
T.timeout;
tcp_close();
T.start(1.0);
T.timeout;
}
/**
* This is just an example of how the receive window can be adjusted
* with the test protocol v1.
*/
testcase tcp_window() runs on tcp_ct
{
test_ct_init();
tcp_config({{ "tcp_window", "64" }});
}
/**
* Send a data without PSH, the data should be placed into the receive window.