forked from vampire1996/sponge
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrecv_reorder.cc
149 lines (139 loc) · 7.52 KB
/
recv_reorder.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
#include "receiver_harness.hh"
#include "util.hh"
#include "wrapping_integers.hh"
#include <cstdint>
#include <cstdlib>
#include <exception>
#include <iostream>
#include <optional>
#include <random>
#include <stdexcept>
#include <string>
using namespace std;
int main() {
try {
auto rd = get_random_generator();
// An in-window, but later segment
{
uint32_t isn = uniform_int_distribution<uint32_t>{0, UINT32_MAX}(rd);
TCPReceiverTestHarness test{2358};
test.execute(SegmentArrives{}.with_syn().with_seqno(isn).with_result(SegmentArrives::Result::OK));
test.execute(ExpectAckno{WrappingInt32{isn + 1}});
test.execute(
SegmentArrives{}.with_seqno(isn + 10).with_data("abcd").with_result(SegmentArrives::Result::OK));
test.execute(ExpectAckno{WrappingInt32{isn + 1}});
test.execute(ExpectBytes{""});
test.execute(ExpectUnassembledBytes{4});
test.execute(ExpectTotalAssembledBytes{0});
}
// An in-window, but later segment, then the hole is filled
{
uint32_t isn = uniform_int_distribution<uint32_t>{0, UINT32_MAX}(rd);
TCPReceiverTestHarness test{2358};
test.execute(SegmentArrives{}.with_syn().with_seqno(isn).with_result(SegmentArrives::Result::OK));
test.execute(ExpectAckno{WrappingInt32{isn + 1}});
test.execute(
SegmentArrives{}.with_seqno(isn + 5).with_data("efgh").with_result(SegmentArrives::Result::OK));
test.execute(ExpectAckno{WrappingInt32{isn + 1}});
test.execute(ExpectBytes{""});
test.execute(ExpectUnassembledBytes{4});
test.execute(ExpectTotalAssembledBytes{0});
test.execute(
SegmentArrives{}.with_seqno(isn + 1).with_data("abcd").with_result(SegmentArrives::Result::OK));
test.execute(ExpectAckno{WrappingInt32{isn + 9}});
test.execute(ExpectBytes{"abcdefgh"});
test.execute(ExpectUnassembledBytes{0});
test.execute(ExpectTotalAssembledBytes{8});
}
// An in-window, but later segment, then the hole is filled, bit by bit
{
uint32_t isn = uniform_int_distribution<uint32_t>{0, UINT32_MAX}(rd);
TCPReceiverTestHarness test{2358};
test.execute(SegmentArrives{}.with_syn().with_seqno(isn).with_result(SegmentArrives::Result::OK));
test.execute(ExpectAckno{WrappingInt32{isn + 1}});
test.execute(
SegmentArrives{}.with_seqno(isn + 5).with_data("efgh").with_result(SegmentArrives::Result::OK));
test.execute(ExpectAckno{WrappingInt32{isn + 1}});
test.execute(ExpectBytes{""});
test.execute(ExpectUnassembledBytes{4});
test.execute(ExpectTotalAssembledBytes{0});
test.execute(SegmentArrives{}.with_seqno(isn + 1).with_data("ab").with_result(SegmentArrives::Result::OK));
test.execute(ExpectAckno{WrappingInt32{isn + 3}});
test.execute(ExpectBytes{"ab"});
test.execute(ExpectUnassembledBytes{4});
test.execute(ExpectTotalAssembledBytes{2});
test.execute(SegmentArrives{}.with_seqno(isn + 3).with_data("cd").with_result(SegmentArrives::Result::OK));
test.execute(ExpectAckno{WrappingInt32{isn + 9}});
test.execute(ExpectBytes{"cdefgh"});
test.execute(ExpectUnassembledBytes{0});
test.execute(ExpectTotalAssembledBytes{8});
}
// Many gaps, then filled bit by bit.
{
uint32_t isn = uniform_int_distribution<uint32_t>{0, UINT32_MAX}(rd);
TCPReceiverTestHarness test{2358};
test.execute(SegmentArrives{}.with_syn().with_seqno(isn).with_result(SegmentArrives::Result::OK));
test.execute(ExpectAckno{WrappingInt32{isn + 1}});
test.execute(SegmentArrives{}.with_seqno(isn + 5).with_data("e").with_result(SegmentArrives::Result::OK));
test.execute(ExpectAckno{WrappingInt32{isn + 1}});
test.execute(ExpectBytes{""});
test.execute(ExpectUnassembledBytes{1});
test.execute(ExpectTotalAssembledBytes{0});
test.execute(SegmentArrives{}.with_seqno(isn + 7).with_data("g").with_result(SegmentArrives::Result::OK));
test.execute(ExpectAckno{WrappingInt32{isn + 1}});
test.execute(ExpectBytes{""});
test.execute(ExpectUnassembledBytes{2});
test.execute(ExpectTotalAssembledBytes{0});
test.execute(SegmentArrives{}.with_seqno(isn + 3).with_data("c").with_result(SegmentArrives::Result::OK));
test.execute(ExpectAckno{WrappingInt32{isn + 1}});
test.execute(ExpectBytes{""});
test.execute(ExpectUnassembledBytes{3});
test.execute(ExpectTotalAssembledBytes{0});
test.execute(SegmentArrives{}.with_seqno(isn + 1).with_data("ab").with_result(SegmentArrives::Result::OK));
test.execute(ExpectAckno{WrappingInt32{isn + 4}});
test.execute(ExpectBytes{"abc"});
test.execute(ExpectUnassembledBytes{2});
test.execute(ExpectTotalAssembledBytes{3});
test.execute(SegmentArrives{}.with_seqno(isn + 6).with_data("f").with_result(SegmentArrives::Result::OK));
test.execute(ExpectUnassembledBytes{3});
test.execute(ExpectTotalAssembledBytes{3});
test.execute(ExpectBytes{""});
test.execute(SegmentArrives{}.with_seqno(isn + 4).with_data("d").with_result(SegmentArrives::Result::OK));
test.execute(ExpectUnassembledBytes{0});
test.execute(ExpectTotalAssembledBytes{7});
test.execute(ExpectBytes{"defg"});
}
// Many gaps, then subsumed
{
uint32_t isn = uniform_int_distribution<uint32_t>{0, UINT32_MAX}(rd);
TCPReceiverTestHarness test{2358};
test.execute(SegmentArrives{}.with_syn().with_seqno(isn).with_result(SegmentArrives::Result::OK));
test.execute(ExpectAckno{WrappingInt32{isn + 1}});
test.execute(SegmentArrives{}.with_seqno(isn + 5).with_data("e").with_result(SegmentArrives::Result::OK));
test.execute(ExpectAckno{WrappingInt32{isn + 1}});
test.execute(ExpectBytes{""});
test.execute(ExpectUnassembledBytes{1});
test.execute(ExpectTotalAssembledBytes{0});
test.execute(SegmentArrives{}.with_seqno(isn + 7).with_data("g").with_result(SegmentArrives::Result::OK));
test.execute(ExpectAckno{WrappingInt32{isn + 1}});
test.execute(ExpectBytes{""});
test.execute(ExpectUnassembledBytes{2});
test.execute(ExpectTotalAssembledBytes{0});
test.execute(SegmentArrives{}.with_seqno(isn + 3).with_data("c").with_result(SegmentArrives::Result::OK));
test.execute(ExpectAckno{WrappingInt32{isn + 1}});
test.execute(ExpectBytes{""});
test.execute(ExpectUnassembledBytes{3});
test.execute(ExpectTotalAssembledBytes{0});
test.execute(
SegmentArrives{}.with_seqno(isn + 1).with_data("abcdefgh").with_result(SegmentArrives::Result::OK));
test.execute(ExpectAckno{WrappingInt32{isn + 9}});
test.execute(ExpectBytes{"abcdefgh"});
test.execute(ExpectUnassembledBytes{0});
test.execute(ExpectTotalAssembledBytes{8});
}
} catch (const exception &e) {
cerr << e.what() << endl;
return 1;
}
return EXIT_SUCCESS;
}