-
Notifications
You must be signed in to change notification settings - Fork 12
/
tcp2_utils.ttcnpp
574 lines (447 loc) · 11.3 KB
/
tcp2_utils.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
/* -*- c-basic-offset: 4; tab-width: 4; -*- */
/*
* Copyright © 2018-2019, Intel Corporation.
*
* SPDX-License-Identifier: LGPL-2.1-only
*/
#include "tcp2_check.ttcnin"
module tcp2_utils {
import from libtypes all;
import from libnet all;
import from libutils all;
import from libtest all;
import from tcp2_check all;
modulepar {
boolean peer_assert_enabled := true; /* TODO: rename to tp_enabled */
}
/* Fuzzer component for incoming/outgoing drop, delay, reorder, corrupt */
/* Can also be used for transparent trace */
type component fuzzer_ct {
port port_tcp P_TCP_IN; /* from/to suite */
port port_tcp P_TCP_OUT; /* from/to system under test */
port port_udp P_UDP_IN; /* from/to suite */
port port_udp P_UDP_OUT; /* from/to system under test */
port port_cf P_CF; /* configuration port */
}
function conn_state(tcp_state_t new, event_t fail_on := EV_KEEP) runs on tcp_ct
{
state := new;
fail_on_events(fail_on);
}
function fuzzer() runs on fuzzer_ct
{
var charstring cmd;
var tcp_t tcp;
var udp_t udp;
var boolean recv := true;
while (true) {
alt {
[] P_CF.receive("incoming_pass") { recv := true; }
[] P_CF.receive("incoming_drop") { recv := false; }
[] P_TCP_IN.receive(?) -> value tcp { /* data from suite */
P_TCP_OUT.send(tcp);
}
[] P_TCP_OUT.receive(?) -> value tcp { /* data from peer */
if (recv) {
P_TCP_IN.send(tcp);
}
}
[] P_UDP_IN.receive(?) -> value udp { /* data from suite */
P_UDP_OUT.send(udp);
}
[] P_UDP_OUT.receive(?) -> value udp { /* data from peer */
P_UDP_IN.send(udp);
}
} /* end of alt */
}
}
type enumerated tp_msg_t {
TP_COMMAND,
TP_CONFIG_REQUEST,
TP_CONFIG_REPLY,
TP_INTROSPECT_REQUEST,
TP_INTROSPECT_REPLY,
TP_INTROSPECT_MEMORY_REQUEST,
TP_INTROSPECT_MEMORY_REPLY,
TP_INTROSPECT_PACKETS_REQUEST,
TP_INTROSPECT_PACKETS_RESPONSE,
TP_DEBUG_STOP,
TP_DEBUG_STEP,
TP_DEBUG_CONTINUE,
TP_DEBUG_RESPONSE,
TP_DEBUG_BREAKPOINT_ADD,
TP_DEBUG_BREAKPOINT_DELETE,
TP_TRACE_ADD,
TP_TRACE_DELETE
} with {
encode "TEXT";
}
external function tp_msg_to_string(in tp_msg_t x) return charstring
with { extension "prototype(convert) encode(TEXT)"; }
type record tp_t {
charstring msg,
charstring status optional,
charstring state optional,
integer seq optional,
integer ack optional,
charstring rcv optional,
charstring data optional,
charstring op optional
} with {
encode "JSON";
}
template tp_t ts_TP(tp_msg_t t,
template charstring d := omit,
template charstring o := omit) := {
msg := tp_msg_to_string(t),
status := omit,
state := omit,
seq := omit,
ack := omit,
rcv := omit,
data := d,
op := o
}
external function f_tp_enc(in tp_t x) return octetstring
with { extension "prototype(convert) encode(JSON)"; }
external function f_tp_dec(in octetstring x) return tp_t
with { extension "prototype(convert) decode(JSON)"; }
function tp_send(tp_msg_t msg_type, charstring o := "", charstring d := "",
integer isn := 0) runs on tcp_ct return tp_t
{
var tp_t tp := valueof(ts_TP(msg_type, d, o));
timer T_tp := T_default;
var udp_t udp;
if (false == peer_assert_enabled) {
goto exit;
}
tp.seq := isn;
P_UDP.send(ts_UDP(d := f_tp_enc(tp)));
T_tp.start;
alt {
[] P_UDP.receive(?) -> value udp {
T_tp.stop;
if (msg_type == TP_INTROSPECT_REQUEST or
(msg_type == TP_COMMAND and o == "RECV")) {
tp := f_tp_dec(udp.data);
}
}
[] T_tp.timeout {
log("Error: tp_send() timeout");
setverdict(fail);
}
} /* end of alt */
if (msg_type == TP_COMMAND and o == "CLOSE") {
test_ct_close();
}
label exit;
return tp;
}
function peer_assert(tcp_state_t expected_state) runs on tcp_ct
{
if (not peer_assert_enabled) { return; }
var tp_t tp := tp_send(TP_INTROSPECT_REQUEST);
if (tp.state != tcp_state_to_string(expected_state)) {
log("ASSERT: peer: ", tp.state, " != ",
tcp_state_to_string(expected_state));
setverdict(fail);
stop;
}
}
function fuzzer_on(octetstring data_to_send := ''O) runs on tcp_ct
{
ct_fuzzer := fuzzer_ct.create;
disconnect(self:P_TCP, eth_ct:P_TCP);
connect(self:P_TCP, ct_fuzzer:P_TCP_IN);
connect(ct_fuzzer:P_TCP_OUT, eth_ct:P_TCP);
disconnect(self:P_UDP, eth_ct:P_UDP);
connect(self:P_UDP, ct_fuzzer:P_UDP_IN);
connect(ct_fuzzer:P_UDP_OUT, eth_ct:P_UDP);
connect(self:P_FZ, ct_fuzzer:P_CF);
ct_fuzzer.start(fuzzer());
}
type record win_t {
integer len,
octetstring data
}
function win_init(inout win_t w)
{
w.len := 0;
w.data := ''O;
}
function win_push(inout win_t w, octetstring data)
{
assert(lengthof(data) > 0, "data=", data);
w.len := w.len + lengthof(data);
w.data := w.data & data;
}
function win_pop(inout win_t w, integer len) return octetstring
{
var octetstring data;
assert(len <= w.len, "len=", len, ", w.len=", w.len);
w.len := w.len - len;
data := substr(w.data, 0, len);
w.data := replace(w.data, 0, len, ''O);
return data;
}
function win_peek(inout win_t w, integer len) return octetstring
{
assert(len <= w.len, "len=", len, ", w.len=", w.len);
return substr(w.data, 0, len);
}
type record of integer vec_t;
/**
* Experimental test with the send window and data resend
*/
testcase tcp_window() runs on tcp_ct
{
var win_t to_send, unack;
var vec_t step := { 1, 2, 3, 4, 1, 2, 3, 4 };
var integer i := 0;
var octetstring data;
win_init(to_send);
win_init(unack);
tcp_connect(close := false);
tcp_config({{ "tcp_echo", "0" }});
win_push(to_send, '0102030405060708081011121314151617181920'O);
P_FZ.send("incoming_drop");
while (to_send.len > 0) {
data := win_pop(to_send, step[i]);
P_TCP.send(ts_TCP(fl := PSH,
seq := (seq + unack.len),
data := data));
win_push(unack, data);
pre_inc(i);
}
/* TODO: Timing shouldn't be hardcoded, find a method for adaptive timing */
T.start(T_default * 3.0); T.timeout;
P_FZ.send("incoming_pass");
i := 0;
label resend;
while (unack.len > 0) {
data := win_peek(unack, 1);
P_TCP.send(ts_TCP(fl := PSH,
seq := seq + lengthof(data),
data := data));
alt {
[] P_TCP.receive(tr_TCP(fl := ACK, seq := ack)) -> value tcp {
var integer acked := tcp.th_ack - seq;
assert(acked > 0);
assert(acked <= unack.len);
log("acked=", acked);
seq := seq + acked;
win_pop(unack, acked);
goto resend;
}
[] P_TCP.receive(?) -> value tcp {
assert(false, "tcp=", tcp);
}
} /* end of alt */
}
var tp_t peer := peer_assert_new(); /* get peer's received data */
log(peer.data);
/* TODO: Implement a check here */
setverdict(pass);
tp_send(TP_COMMAND, "CLOSE");
}
testcase test_u32_wrap() runs on tcp_ct
{
var integer x := 4294967295;
log(x);
u32_pre_inc(x);
log(x);
if (x != 0) {
setverdict(fail);
}
x := 4294967295;
log(x);
u32_pre_inc(x, 2);
log(x);
if (x != 1) {
setverdict(fail);
}
setverdict(pass);
}
/**
* This a fork of the retransmission test for an experimentation
* with what is the best approach here.
* Currently it checks peer's retransmission in TCP_SYN_RECEIVED
* TODO:
* - Check the retransmission in peer's TCP_SYN_SENT, TCP_ESTABLISHED,
* TCP_LAST_ACK
* - During peer's retransmission, input messages from the previous
* and next state
* - Reply before the peer's retransmission count is reached and
* verify that peer can recover
*/
testcase tcp_retransmit2() runs on tcp_ct
{
tcp_init();
var udp_t udp;
var tp_t tp;
var integer tcp_retries := 10;
var float tcp_rto := T_default/4.0;
var boolean peer_deleted := false;
ack := -1;
tcp_config({
{ "tcp_rto", int2str(TO_MSEC(tcp_rto)) },
{ "tcp_retries", int2str(tcp_retries) },
{ "tp_trace", int2str(1) }
});
P_TCP.send(ts_TCP(fl := SYN, seq := post_inc(seq)));
conn_state(TCP_SYN_SENT);
T.start;
tcp_retries := tcp_retries + 1; /* account non retransmit */
alt {
[tcp_retries > 0] P_TCP.receive(tr_TCP(fl := SYN or4b ACK,
ack := seq)) -> value tcp {
log("tcp_rto=", tcp_rto, " sec, actual=", T.read, " sec");
T.stop;
if (ack < 0) {
ack := tcp.th_seq;
}
assert(tcp.th_seq == ack);
if (tcp_retries == 0) {
peer_assert(TCP_SYN_SENT);
}
tcp_retries := tcp_retries - 1;
T.start;
repeat;
}
[tcp_retries <= 0] P_TCP.receive(?) -> value tcp {
log("Extra retransmit");
setverdict(fail);
}
[tcp_retries == 0] P_UDP.receive(?) -> value udp {
T.stop;
tp := f_tp_dec(udp.data);
peer_deleted := true;
setverdict(pass);
}
[] T.timeout { setverdict(fail); }
} /* end of alt */
//label end;
//tp_send(TP_COMMAND, "CLOSE");
}
/**
* Test TCP debug
*/
testcase tcp_debug() runs on tcp_ct
{
tcp_init();
tp_send(TP_DEBUG_STOP);
P_TCP.send(ts_TCP(fl := SYN, seq := post_inc(seq)));
T.start;
alt {
[] P_TCP.receive(tr_TCP(?)) {
T.stop;
setverdict(fail);
}
[] T.timeout {
tp_send(TP_DEBUG_STEP);
}
} /* end of alt */
T.start;
alt {
[] P_TCP.receive(tr_TCP(?)) {
T.stop;
setverdict(pass);
}
[] T.timeout {
setverdict(fail);
}
} /* end of alt */
tp_send(TP_COMMAND, "CLOSE");
}
type record map_entry_t {
charstring key,
charstring val /* "value" is a reserved word in TTCN-3 */
} with {
encode "JSON";
variant(val) "JSON: name as value";
}
type record of map_entry_t map_t with {
encode "JSON";
}
type record tp_new_t {
charstring msg,
map_t data
} with {
encode "JSON";
}
template tp_new_t ts_TP_new(tp_msg_t msg, map_t data) := {
msg := tp_msg_to_string(msg),
data := data
}
external function f_tp_new_enc(in tp_new_t x) return octetstring
with { extension "prototype(convert) encode(JSON) printing(pretty)"; }
external function f_tp_new_dec(in octetstring x) return tp_new_t
with { extension "prototype(convert) decode(JSON)"; }
function tp_send_new(tp_msg_t msg, map_t data) runs on tcp_ct
{
timer T_tp := T_default;
var tp_new_t tp := valueof(ts_TP_new(msg, data));
P_UDP.send(ts_UDP(d := f_tp_new_enc(tp)));
T_tp.start;
alt {
[] P_UDP.receive(?) {
T_tp.stop;
}
[] T_tp.timeout {
log("Error: T_tp timeout");
setverdict(fail);
}
} /* end of alt */
}
external function f_enc_map(in map_t x) return octetstring
with { extension "prototype(convert) encode(JSON)" }
function peer_assert_new(integer exp_seq := -1, integer exp_ack := -1)
runs on tcp_ct return tp_t
{
var tp_t peer := tp_send(TP_INTROSPECT_REQUEST);
if (exp_seq >= 0) {
tcp_assert(peer.seq == exp_seq,
"peer.seq=", peer.seq, ", exp_seq=", exp_seq);
}
if (exp_ack >= 0) {
tcp_assert(peer.ack == exp_ack,
"peer.ack=", peer.ack, ", exp_ack=", exp_ack);
}
return peer;
}
function tcp_send(octetstring data := ''O) runs on tcp_ct
{
/* This check might be too restrictive for out of sync tests */
peer_assert_new(exp_seq := ack, exp_ack := seq);
P_TCP.send(ts_TCP(fl := PSH, seq := post_inc(seq, lengthof(data)),
ack := ack, data := data));
T.start;
alt {
[] P_TCP.receive(tr_TCP(fl := ACK, seq := ack, ack := seq)) {
T.stop;
}
[] P_TCP.receive(?) { T.stop; setverdict(fail); }
[] T.timeout { setverdict(fail); }
} /* end of alt */
peer_assert_new(exp_seq := ack, exp_ack := seq);
}
function tcp_config(map_t cfg) runs on tcp_ct
{
if (not peer_assert_enabled) { return; }
tp_send_new(TP_CONFIG_REQUEST, cfg);
}
function tcp_init(boolean config := true) runs on tcp_ct
{
var map_t cfg := {
{ "tcp_echo", "1" },
{ "tp_tcp_conn_delete", "0" }
};
if (T_peer_default > 0.0) {
cfg[lengthof(cfg)] := { "tcp_rto", int2str(TO_MSEC(T_peer_default)) };
}
test_ct_init();
if (config) {
tcp_config(cfg);
}
}
} /* end of module */