-
Notifications
You must be signed in to change notification settings - Fork 12
/
tcp_suite.ttcn
143 lines (125 loc) · 3.23 KB
/
tcp_suite.ttcn
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
/* -*- c-basic-offset: 4; tab-width: 4; -*- */
/*
* Copyright © 2018-2022, Intel Corporation.
* SPDX-License-Identifier: LGPL-2.1-only
*/
module tcp_suite {
import from libtypes all;
import from libnet all;
import from libtest all;
import from libutils all;
import from port_data_types all;
modulepar {
float T_default := 3.0;
}
type component tcp_ct extends test_ct {
var tcp_t tcp;
var tcp_state_t s := TCP_LISTEN;
var u32 seq := 0;
var u32 ack := 0;
}
/**
* Send TCP SYN, expect SYN+ACK
*/
testcase tc_tcp_003() runs on tcp_ct {
test_ct_init();
P_TCP.send(ts_TCP(fl := SYN));
T.start;
alt {
[] P_TCP.receive(tr_TCP(fl := SYN or4b ACK)) {
T.stop;
setverdict(pass);
}
[] T.timeout {
setverdict(fail);
}
}
/* Terminate half open connection */
P_TCP.send(ts_TCP(fl := RST, seq := pre_inc(seq)));
}
/**
* Test TCP connection establishment, data transfer
* (data echoing expected) and connection close.
*/
testcase tc_tcp_conn(octetstring data := ''O) runs on tcp_ct
{
test_ct_init();
var octetstring data_to_send := data;
var octetstring data_unack := ''O;
var integer data_step := 1;
d_as_timeout := activate(as_timeout());
d_as_unexpected_msg := activate(as_unexpected_msg());
P_TCP.send(ts_TCP(fl := SYN, seq := post_inc(seq)));
s := TCP_SYN_SENT;
T.start;
alt {
[s == TCP_SYN_SENT] P_TCP.receive(tr_TCP(fl := SYN or4b ACK,
ack := seq)) -> value tcp {
T.stop;
s := TCP_SYN_RECEIVED;
ack := tcp.th_seq;
P_TCP.send(ts_TCP(fl := ACK, seq := seq, ack := pre_inc(ack)));
T.start; repeat;
}
[s == TCP_SYN_RECEIVED] T.timeout {
s := TCP_ESTABLISHED;
deactivate(d_as_timeout); deactivate(d_as_unexpected_msg);
T.start; repeat;
}
[s == TCP_ESTABLISHED] P_TCP.receive(tr_TCP) -> value tcp {
T.stop;
var tcp_t tcp2 := valueof(ts_TCP);
if (th_is_set(tcp, PSH)) {
th_set(tcp2, ACK);
pre_inc(ack, lengthof(tcp.data));
}
if (th_is_set(tcp, ACK)) {
if (tcp.th_ack > seq - lengthof(data_unack)) {
var integer diff := tcp.th_ack - (seq - lengthof(data_unack));
byte_array_remove_range(data_unack, index := 0, len := diff);
}
}
if (tcp2.th_flags != TH_NONE) {
tcp2.th_seq := seq;
tcp2.th_ack := ack;
P_TCP.send(tcp2);
}
T.start; repeat;
}
[s == TCP_ESTABLISHED] T.timeout {
if (lengthof(data_unack) > 0) { goto wait_for_data; }
if (lengthof(data_to_send) > 0) {
data_unack := byte_array_remove_range(data_to_send, index := 0,
len := data_step);
P_TCP.send(ts_TCP(fl := PSH,
seq := post_inc(seq, lengthof(data_unack)),
ack := ack, data := data_unack));
goto wait_for_data;
}
/* Everything is sent and aknowledged, close the connection */
s := TCP_CLOSE_WAIT;
d_as_timeout := activate(as_timeout());
d_as_unexpected_msg := activate(as_unexpected_msg());
P_TCP.send(ts_TCP(fl := FIN or4b ACK,
seq := post_inc(seq), ack := ack));
label wait_for_data;
T.start; repeat;
}
[s == TCP_CLOSE_WAIT] P_TCP.receive(tr_TCP(fl := FIN or4b ACK,
ack := seq)) {
T.stop;
s := TCP_CLOSED;
goto test_end;
}
}
label test_end;
if (s == TCP_CLOSED and lengthof(data_to_send) == 0
and lengthof(data_unack) == 0) {
setverdict(pass);
}
}
control {
execute(tc_tcp_003());
execute(tc_tcp_conn(data := '01'O));
}
}