forked from vikshanker/sponge
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfsm_passive_close.cc
116 lines (79 loc) · 3.63 KB
/
fsm_passive_close.cc
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
#include "tcp_config.hh"
#include "tcp_expectation.hh"
#include "tcp_fsm_test_harness.hh"
#include "tcp_header.hh"
#include "tcp_segment.hh"
#include <cstdlib>
#include <exception>
#include <iostream>
#include <stdexcept>
#include <string>
using namespace std;
using State = TCPTestHarness::State;
int main() {
try {
TCPConfig cfg{};
// test #1: start in LAST_ACK, ack
{
TCPTestHarness test_1 = TCPTestHarness::in_last_ack(cfg);
test_1.execute(Tick(4 * cfg.rt_timeout));
test_1.execute(ExpectState{State::LAST_ACK});
test_1.send_ack(WrappingInt32{2}, WrappingInt32{2});
test_1.execute(Tick(1));
test_1.execute(ExpectState{State::CLOSED});
}
// test #2: start in CLOSE_WAIT, close(), throw away first FIN, ack re-tx FIN
{
TCPTestHarness test_2 = TCPTestHarness::in_close_wait(cfg);
test_2.execute(Tick(4 * cfg.rt_timeout));
test_2.execute(ExpectState{State::CLOSE_WAIT});
test_2.execute(Close{});
test_2.execute(Tick(1));
test_2.execute(ExpectState{State::LAST_ACK});
TCPSegment seg1 = test_2.expect_seg(ExpectOneSegment{}.with_fin(true), "test 2 falied: bad seg or no FIN");
test_2.execute(Tick(cfg.rt_timeout - 2));
test_2.execute(ExpectNoSegment{}, "test 2 failed: FIN re-tx was too fast");
test_2.execute(Tick(2));
TCPSegment seg2 = test_2.expect_seg(ExpectOneSegment{}.with_fin(true).with_seqno(seg1.header().seqno),
"test 2 failed: bad re-tx FIN");
const WrappingInt32 rx_seqno{2};
const auto ack_expect = rx_seqno;
test_2.send_ack(ack_expect, seg2.header().seqno - 1); // wrong ackno!
test_2.execute(Tick(1));
test_2.execute(ExpectState{State::LAST_ACK});
test_2.send_ack(ack_expect, seg2.header().seqno + 1);
test_2.execute(Tick(1));
test_2.execute(ExpectState{State::CLOSED});
}
// test #3: start in ESTABLSHED, send FIN, recv ACK, check for CLOSE_WAIT
{
TCPTestHarness test_3 = TCPTestHarness::in_established(cfg);
test_3.execute(Tick(4 * cfg.rt_timeout));
test_3.execute(ExpectState{State::ESTABLISHED});
const WrappingInt32 rx_seqno{1};
const auto ack_expect = rx_seqno + 1;
test_3.send_fin(rx_seqno, WrappingInt32{0});
test_3.execute(Tick(1));
test_3.execute(ExpectOneSegment{}.with_ack(true).with_ackno(ack_expect),
"test 3 failed: bad seg, no ACK, or wrong ackno");
test_3.execute(ExpectState{State::CLOSE_WAIT});
test_3.send_fin(rx_seqno, WrappingInt32{0});
test_3.execute(Tick(1));
test_3.execute(ExpectOneSegment{}.with_ack(true).with_ackno(ack_expect),
"test 3 falied: bad response to 2nd FIN");
test_3.execute(ExpectState{State::CLOSE_WAIT});
test_3.execute(Tick(1));
test_3.execute(Close{});
test_3.execute(Tick(1));
test_3.execute(ExpectState{State::LAST_ACK});
TCPSegment seg3 = test_3.expect_seg(ExpectOneSegment{}.with_fin(true), "test 3 failed: bad seg or no FIN");
test_3.send_ack(ack_expect, seg3.header().seqno + 1);
test_3.execute(Tick(1));
test_3.execute(ExpectState{State::CLOSED});
}
} catch (const exception &e) {
cerr << e.what() << endl;
return 1;
}
return EXIT_SUCCESS;
}