forked from vikshanker/sponge
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfsm_active_close.cc
197 lines (136 loc) · 6.62 KB
/
fsm_active_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
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
#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 TIME_WAIT, timeout
{
TCPTestHarness test_1 = TCPTestHarness::in_time_wait(cfg);
test_1.execute(Tick(10 * cfg.rt_timeout - 1));
test_1.execute(ExpectState{State::TIME_WAIT});
test_1.execute(Tick(1));
test_1.execute(ExpectNotInState{State::TIME_WAIT});
test_1.execute(Tick(10 * cfg.rt_timeout));
test_1.execute(ExpectState{State::CLOSED});
}
// test #2: start in CLOSING, send ack, time out
{
TCPTestHarness test_2 = TCPTestHarness::in_closing(cfg);
test_2.execute(Tick(4 * cfg.rt_timeout));
test_2.execute(ExpectOneSegment{}.with_fin(true));
test_2.execute(ExpectState{State::CLOSING});
test_2.send_ack(WrappingInt32{2}, WrappingInt32{2});
test_2.execute(ExpectNoSegment{});
test_2.execute(ExpectState{State::TIME_WAIT});
test_2.execute(Tick(10 * cfg.rt_timeout - 1));
test_2.execute(ExpectState{State::TIME_WAIT});
test_2.execute(Tick(2));
test_2.execute(ExpectState{State::CLOSED});
}
// test #3: start in FIN_WAIT_2, send FIN, time out
{
TCPTestHarness test_3 = TCPTestHarness::in_fin_wait_2(cfg);
test_3.execute(Tick(4 * cfg.rt_timeout));
test_3.execute(ExpectState{State::FIN_WAIT_2});
const WrappingInt32 rx_seqno{1};
test_3.send_fin(rx_seqno, WrappingInt32{2});
const auto ack_expect = rx_seqno + 1;
test_3.execute(Tick(1));
test_3.execute(ExpectOneSegment{}.with_ack(true).with_ackno(ack_expect),
"test 3 failed: wrong ACK for FIN");
test_3.execute(ExpectState{State::TIME_WAIT});
test_3.execute(Tick(10 * cfg.rt_timeout));
test_3.execute(ExpectState{State::CLOSED});
}
// test #4: start in FIN_WAIT_1, ack, FIN, time out
{
TCPTestHarness test_4 = TCPTestHarness::in_fin_wait_1(cfg);
// Expect retransmission of FIN
test_4.execute(Tick(4 * cfg.rt_timeout));
test_4.execute(ExpectOneSegment{}.with_fin(true));
// ACK the FIN
const WrappingInt32 rx_seqno{1};
test_4.send_ack(rx_seqno, WrappingInt32{2});
test_4.execute(Tick(5));
// Send our own FIN
test_4.send_fin(rx_seqno, WrappingInt32{2});
const auto ack_expect = rx_seqno + 1;
test_4.execute(Tick(1));
test_4.execute(ExpectOneSegment{}.with_no_flags().with_ack(true).with_ackno(ack_expect));
test_4.execute(Tick(10 * cfg.rt_timeout));
test_4.execute(ExpectState{State::CLOSED});
}
// test 5: start in FIN_WAIT_1, ack, FIN, FIN again, time out
{
TCPTestHarness test_5 = TCPTestHarness::in_fin_wait_1(cfg);
// ACK the FIN
const WrappingInt32 rx_seqno{1};
test_5.send_ack(rx_seqno, WrappingInt32{2});
test_5.execute(ExpectState{State::FIN_WAIT_2});
test_5.execute(Tick(5));
test_5.send_fin(rx_seqno, WrappingInt32{2});
test_5.execute(ExpectState{State::TIME_WAIT});
test_5.execute(ExpectLingerTimer{0ul});
const auto ack_expect = rx_seqno + 1;
test_5.execute(Tick(1));
test_5.execute(ExpectLingerTimer{1ul});
test_5.execute(ExpectOneSegment{}.with_no_flags().with_ack(true).with_ackno(ack_expect));
test_5.execute(Tick(10 * cfg.rt_timeout - 10));
test_5.execute(ExpectLingerTimer{uint64_t(10 * cfg.rt_timeout - 9)});
test_5.send_fin(rx_seqno, WrappingInt32{2});
test_5.execute(ExpectLingerTimer{0ul});
test_5.execute(Tick(1));
test_5.execute(ExpectOneSegment{}.with_ack(true).with_ackno(ack_expect),
"test 5 failed: no ACK for 2nd FIN");
test_5.execute(ExpectState{State::TIME_WAIT});
// tick the timer and see what happens---a 2nd FIN in TIME_WAIT should reset the wait timer!
// (this is an edge case of "throw it away and send another ack" for out-of-window segs)
test_5.execute(Tick(10 * cfg.rt_timeout - 10));
test_5.execute(ExpectLingerTimer{uint64_t(10 * cfg.rt_timeout - 9)},
"test 5 failed: time_since_last_segment_received() should reset after 2nd FIN");
test_5.execute(ExpectNoSegment{});
test_5.execute(Tick(10));
test_5.execute(ExpectState{State::CLOSED});
}
// test 6: start in ESTABLISHED, get FIN, get FIN re-tx, send FIN, get ACK, send ACK, time out
{
TCPTestHarness test_6 = TCPTestHarness::in_established(cfg);
test_6.execute(Close{});
test_6.execute(Tick(1));
TCPSegment seg1 = test_6.expect_seg(ExpectOneSegment{}.with_fin(true),
"test 6 failed: bad FIN after close()");
auto &seg1_hdr = seg1.header();
test_6.execute(Tick(cfg.rt_timeout - 2));
test_6.execute(ExpectNoSegment{}, "test 6 failed: FIN re-tx was too fast");
test_6.execute(Tick(2));
TCPSegment seg2 = test_6.expect_seg(ExpectOneSegment{}.with_fin(true).with_seqno(seg1_hdr.seqno),
"test 6 failed: bad re-tx FIN");
auto &seg2_hdr = seg2.header();
const WrappingInt32 rx_seqno{1};
test_6.send_fin(rx_seqno, WrappingInt32{0});
const auto ack_expect = rx_seqno + 1;
test_6.execute(Tick(1));
test_6.execute(ExpectState{State::CLOSING});
test_6.execute(ExpectOneSegment{}.with_ack(true).with_ackno(ack_expect), "test 6 failed: bad ACK for FIN");
test_6.send_ack(ack_expect, seg2_hdr.seqno + 1);
test_6.execute(Tick(1));
test_6.execute(ExpectState{State::TIME_WAIT});
test_6.execute(Tick(10 * cfg.rt_timeout));
test_6.execute(ExpectState{State::CLOSED});
}
} catch (const exception &e) {
cerr << e.what() << endl;
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}