forked from vikshanker/sponge
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrecv_special.cc
149 lines (138 loc) · 7.03 KB
/
recv_special.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 "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();
/* segment before SYN */
{
uint32_t isn = uniform_int_distribution<uint32_t>{0, UINT32_MAX}(rd);
TCPReceiverTestHarness test{4000};
test.execute(ExpectState{TCPReceiverStateSummary::LISTEN});
test.execute(
SegmentArrives{}.with_seqno(isn + 1).with_data("hello").with_result(SegmentArrives::Result::NOT_SYN));
test.execute(ExpectState{TCPReceiverStateSummary::LISTEN});
test.execute(ExpectUnassembledBytes{0});
test.execute(ExpectBytes{""});
test.execute(ExpectTotalAssembledBytes{0});
test.execute(SegmentArrives{}.with_syn().with_seqno(isn).with_result(SegmentArrives::Result::OK));
test.execute(ExpectState{TCPReceiverStateSummary::SYN_RECV});
test.execute(ExpectAckno{WrappingInt32{isn + 1}});
}
/* segment with SYN + data */
{
uint32_t isn = uniform_int_distribution<uint32_t>{0, UINT32_MAX}(rd);
TCPReceiverTestHarness test{4000};
test.execute(ExpectState{TCPReceiverStateSummary::LISTEN});
test.execute(SegmentArrives{}
.with_syn()
.with_seqno(isn)
.with_data("Hello, CS144!")
.with_result(SegmentArrives::Result::OK));
test.execute(ExpectState{TCPReceiverStateSummary::SYN_RECV});
test.execute(ExpectAckno{WrappingInt32{isn + 14}});
test.execute(ExpectUnassembledBytes{0});
test.execute(ExpectBytes{"Hello, CS144!"});
}
/* empty segment */
{
uint32_t isn = uniform_int_distribution<uint32_t>{0, UINT32_MAX}(rd);
TCPReceiverTestHarness test{4000};
test.execute(ExpectState{TCPReceiverStateSummary::LISTEN});
test.execute(SegmentArrives{}.with_syn().with_seqno(isn).with_result(SegmentArrives::Result::OK));
test.execute(ExpectState{TCPReceiverStateSummary::SYN_RECV});
test.execute(ExpectAckno{WrappingInt32{isn + 1}});
test.execute(ExpectUnassembledBytes{0});
test.execute(SegmentArrives{}.with_syn().with_seqno(isn + 1).with_result(SegmentArrives::Result::OK));
test.execute(ExpectUnassembledBytes{0});
test.execute(ExpectTotalAssembledBytes{0});
test.execute(ExpectInputNotEnded{});
test.execute(SegmentArrives{}.with_syn().with_seqno(isn + 5).with_result(SegmentArrives::Result::OK));
test.execute(ExpectUnassembledBytes{0});
test.execute(ExpectTotalAssembledBytes{0});
test.execute(ExpectInputNotEnded{});
}
/* segment with null byte */
{
uint32_t isn = uniform_int_distribution<uint32_t>{0, UINT32_MAX}(rd);
TCPReceiverTestHarness test{4000};
const string text = "Here's a null byte:"s + '\0' + "and it's gone."s;
test.execute(ExpectState{TCPReceiverStateSummary::LISTEN});
test.execute(SegmentArrives{}.with_syn().with_seqno(isn).with_result(SegmentArrives::Result::OK));
test.execute(ExpectState{TCPReceiverStateSummary::SYN_RECV});
test.execute(ExpectUnassembledBytes{0});
test.execute(ExpectTotalAssembledBytes{0});
test.execute(SegmentArrives{}.with_seqno(isn + 1).with_data(text).with_result(SegmentArrives::Result::OK));
test.execute(ExpectBytes{string(text)});
test.execute(ExpectAckno{WrappingInt32{isn + 35}});
test.execute(ExpectInputNotEnded{});
}
/* segment with data + FIN */
{
uint32_t isn = uniform_int_distribution<uint32_t>{0, UINT32_MAX}(rd);
TCPReceiverTestHarness test{4000};
test.execute(ExpectState{TCPReceiverStateSummary::LISTEN});
test.execute(SegmentArrives{}.with_syn().with_seqno(isn).with_result(SegmentArrives::Result::OK));
test.execute(ExpectState{TCPReceiverStateSummary::SYN_RECV});
test.execute(SegmentArrives{}
.with_fin()
.with_data("Goodbye, CS144!")
.with_seqno(isn + 1)
.with_result(SegmentArrives::Result::OK));
test.execute(ExpectState{TCPReceiverStateSummary::FIN_RECV});
test.execute(ExpectBytes{"Goodbye, CS144!"});
test.execute(ExpectAckno{WrappingInt32{isn + 17}});
test.execute(ExpectEof{});
}
/* segment with FIN (but can't be assembled yet) */
{
uint32_t isn = uniform_int_distribution<uint32_t>{0, UINT32_MAX}(rd);
TCPReceiverTestHarness test{4000};
test.execute(ExpectState{TCPReceiverStateSummary::LISTEN});
test.execute(SegmentArrives{}.with_syn().with_seqno(isn).with_result(SegmentArrives::Result::OK));
test.execute(ExpectState{TCPReceiverStateSummary::SYN_RECV});
test.execute(SegmentArrives{}
.with_fin()
.with_data("oodbye, CS144!")
.with_seqno(isn + 2)
.with_result(SegmentArrives::Result::OK));
test.execute(ExpectState{TCPReceiverStateSummary::SYN_RECV});
test.execute(ExpectBytes{""});
test.execute(ExpectAckno{WrappingInt32{isn + 1}});
test.execute(ExpectInputNotEnded{});
test.execute(SegmentArrives{}.with_data("G").with_seqno(isn + 1).with_result(SegmentArrives::Result::OK));
test.execute(ExpectState{TCPReceiverStateSummary::FIN_RECV});
test.execute(ExpectBytes{"Goodbye, CS144!"});
test.execute(ExpectAckno{WrappingInt32{isn + 17}});
test.execute(ExpectEof{});
}
/* segment with SYN + data + FIN */
{
uint32_t isn = uniform_int_distribution<uint32_t>{0, UINT32_MAX}(rd);
TCPReceiverTestHarness test{4000};
test.execute(ExpectState{TCPReceiverStateSummary::LISTEN});
test.execute(SegmentArrives{}
.with_syn()
.with_seqno(isn)
.with_data("Hello and goodbye, CS144!")
.with_fin()
.with_result(SegmentArrives::Result::OK));
test.execute(ExpectState{TCPReceiverStateSummary::FIN_RECV});
test.execute(ExpectAckno{WrappingInt32{isn + 27}});
test.execute(ExpectUnassembledBytes{0});
test.execute(ExpectBytes{"Hello and goodbye, CS144!"});
test.execute(ExpectEof{});
}
} catch (const exception &e) {
cerr << e.what() << endl;
return 1;
}
return EXIT_SUCCESS;
}