forked from vikshanker/sponge
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrecv_window.cc
188 lines (172 loc) · 8.58 KB
/
recv_window.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
#include "receiver_harness.hh"
#include "wrapping_integers.hh"
#include <cstdint>
#include <cstdlib>
#include <exception>
#include <iostream>
#include <optional>
#include <stdexcept>
#include <string>
using namespace std;
int main() {
try {
// auto rd = get_random_generator();
{
// Window size decreases appropriately
size_t cap = 4000;
uint32_t isn = 23452;
TCPReceiverTestHarness test{cap};
test.execute(SegmentArrives{}.with_syn().with_seqno(isn).with_result(SegmentArrives::Result::OK));
test.execute(ExpectAckno{WrappingInt32{isn + 1}});
test.execute(ExpectWindow{cap});
test.execute(
SegmentArrives{}.with_seqno(isn + 1).with_data("abcd").with_result(SegmentArrives::Result::OK));
test.execute(ExpectAckno{WrappingInt32{isn + 5}});
test.execute(ExpectWindow{cap - 4});
test.execute(
SegmentArrives{}.with_seqno(isn + 9).with_data("ijkl").with_result(SegmentArrives::Result::OK));
test.execute(ExpectAckno{WrappingInt32{isn + 5}});
test.execute(ExpectWindow{cap - 4});
test.execute(
SegmentArrives{}.with_seqno(isn + 5).with_data("efgh").with_result(SegmentArrives::Result::OK));
test.execute(ExpectAckno{WrappingInt32{isn + 13}});
test.execute(ExpectWindow{cap - 12});
}
{
// Window size expands upon read
size_t cap = 4000;
uint32_t isn = 23452;
TCPReceiverTestHarness test{cap};
test.execute(SegmentArrives{}.with_syn().with_seqno(isn).with_result(SegmentArrives::Result::OK));
test.execute(ExpectAckno{WrappingInt32{isn + 1}});
test.execute(ExpectWindow{cap});
test.execute(
SegmentArrives{}.with_seqno(isn + 1).with_data("abcd").with_result(SegmentArrives::Result::OK));
test.execute(ExpectAckno{WrappingInt32{isn + 5}});
test.execute(ExpectWindow{cap - 4});
test.execute(ExpectBytes{"abcd"});
test.execute(ExpectAckno{WrappingInt32{isn + 5}});
test.execute(ExpectWindow{cap});
}
/* remove requirement for corrective ACK on out-of-window segment
{
// high-seqno segment is rejected
size_t cap = 2;
uint32_t isn = 23452;
TCPReceiverTestHarness test{cap};
test.execute(SegmentArrives{}.with_syn().with_seqno(isn).with_result(SegmentArrives::Result::OK));
test.execute(ExpectAckno{WrappingInt32{isn + 1}});
test.execute(ExpectWindow{cap});
test.execute(ExpectTotalAssembledBytes{0});
test.execute(SegmentArrives{}.with_seqno(isn + 3).with_data("cd").with_result(
SegmentArrives::Result::OUT_OF_WINDOW));
test.execute(ExpectAckno{WrappingInt32{isn + 1}});
test.execute(ExpectWindow{cap});
test.execute(ExpectTotalAssembledBytes{0});
}
*/
{
// almost-high-seqno segment is accepted, but only some bytes are kept
size_t cap = 2;
uint32_t isn = 23452;
TCPReceiverTestHarness test{cap};
test.execute(SegmentArrives{}.with_syn().with_seqno(isn).with_result(SegmentArrives::Result::OK));
test.execute(SegmentArrives{}.with_seqno(isn + 2).with_data("bc").with_result(SegmentArrives::Result::OK));
test.execute(ExpectTotalAssembledBytes{0});
test.execute(SegmentArrives{}.with_seqno(isn + 1).with_data("a").with_result(SegmentArrives::Result::OK));
test.execute(ExpectAckno{WrappingInt32{isn + 3}});
test.execute(ExpectWindow{0});
test.execute(ExpectTotalAssembledBytes{2});
test.execute(ExpectBytes{"ab"});
test.execute(ExpectWindow{2});
}
/* remove requirement for corrective ACK on out-of-window segment
{
// low-seqno segment is rejected
size_t cap = 4;
uint32_t isn = 294058;
TCPReceiverTestHarness test{cap};
test.execute(SegmentArrives{}.with_syn().with_seqno(isn).with_result(SegmentArrives::Result::OK));
test.execute(SegmentArrives{}.with_data("ab").with_seqno(isn +
1).with_result(SegmentArrives::Result::OK)); test.execute(ExpectTotalAssembledBytes{2});
test.execute(ExpectWindow{cap - 2});
test.execute(SegmentArrives{}.with_data("ab").with_seqno(isn + 1).with_result(
SegmentArrives::Result::OUT_OF_WINDOW));
test.execute(ExpectTotalAssembledBytes{2});
}
*/
{
// almost-low-seqno segment is accepted
size_t cap = 4;
uint32_t isn = 294058;
TCPReceiverTestHarness test{cap};
test.execute(SegmentArrives{}.with_syn().with_seqno(isn).with_result(SegmentArrives::Result::OK));
test.execute(SegmentArrives{}.with_data("ab").with_seqno(isn + 1).with_result(SegmentArrives::Result::OK));
test.execute(ExpectTotalAssembledBytes{2});
test.execute(ExpectWindow{cap - 2});
test.execute(SegmentArrives{}.with_data("abc").with_seqno(isn + 1).with_result(SegmentArrives::Result::OK));
test.execute(ExpectTotalAssembledBytes{3});
test.execute(ExpectWindow{cap - 3});
}
/* remove requirement for corrective ACK on out-of-window segment
{
// second SYN is rejected
size_t cap = 2;
uint32_t isn = 23452;
TCPReceiverTestHarness test{cap};
test.execute(SegmentArrives{}.with_syn().with_seqno(isn).with_result(SegmentArrives::Result::OK));
test.execute(
SegmentArrives{}.with_syn().with_seqno(isn).with_result(SegmentArrives::Result::OUT_OF_WINDOW));
test.execute(ExpectWindow{cap});
test.execute(ExpectTotalAssembledBytes{0});
}
*/
/* remove requirement for corrective ACK on out-of-window segment
{
// second FIN is rejected
size_t cap = 2;
uint32_t isn = 23452;
TCPReceiverTestHarness test{cap};
test.execute(SegmentArrives{}.with_syn().with_seqno(isn).with_result(SegmentArrives::Result::OK));
test.execute(SegmentArrives{}.with_fin().with_seqno(isn + 1).with_result(SegmentArrives::Result::OK));
test.execute(
SegmentArrives{}.with_fin().with_seqno(isn + 1).with_result(SegmentArrives::Result::OUT_OF_WINDOW));
test.execute(ExpectWindow{cap});
test.execute(ExpectTotalAssembledBytes{0});
}
*/
{
// Segment overflowing the window on left side is acceptable.
size_t cap = 4;
uint32_t isn = 23452;
TCPReceiverTestHarness test{cap};
test.execute(SegmentArrives{}.with_syn().with_seqno(isn).with_result(SegmentArrives::Result::OK));
test.execute(SegmentArrives{}.with_seqno(isn + 1).with_data("ab").with_result(SegmentArrives::Result::OK));
test.execute(
SegmentArrives{}.with_seqno(isn + 3).with_data("cdef").with_result(SegmentArrives::Result::OK));
}
{
// Segment matching the window is acceptable.
size_t cap = 4;
uint32_t isn = 23452;
TCPReceiverTestHarness test{cap};
test.execute(SegmentArrives{}.with_syn().with_seqno(isn).with_result(SegmentArrives::Result::OK));
test.execute(SegmentArrives{}.with_seqno(isn + 1).with_data("ab").with_result(SegmentArrives::Result::OK));
test.execute(SegmentArrives{}.with_seqno(isn + 3).with_data("cd").with_result(SegmentArrives::Result::OK));
}
// credit for test: Jared Wasserman
{
// A byte with invalid stream index should be ignored
size_t cap = 4;
uint32_t isn = 23452;
TCPReceiverTestHarness test{cap};
test.execute(SegmentArrives{}.with_syn().with_seqno(isn).with_result(SegmentArrives::Result::OK));
test.execute(SegmentArrives{}.with_seqno(isn).with_data("a").with_result(SegmentArrives::Result::OK));
test.execute(ExpectTotalAssembledBytes{0});
}
} catch (const exception &e) {
cerr << e.what() << endl;
return 1;
}
return EXIT_SUCCESS;
}