forked from vikshanker/sponge
-
Notifications
You must be signed in to change notification settings - Fork 0
/
receiver_harness.hh
324 lines (274 loc) · 10.7 KB
/
receiver_harness.hh
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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
#ifndef SPONGE_RECEIVER_HARNESS_HH
#define SPONGE_RECEIVER_HARNESS_HH
#include "byte_stream.hh"
#include "tcp_receiver.hh"
#include "tcp_state.hh"
#include "util.hh"
#include "wrapping_integers.hh"
#include <algorithm>
#include <exception>
#include <iostream>
#include <optional>
#include <sstream>
#include <string>
struct ReceiverTestStep {
virtual std::string to_string() const { return "ReceiverTestStep"; }
virtual void execute(TCPReceiver &) const {}
virtual ~ReceiverTestStep() {}
};
class ReceiverExpectationViolation : public std::runtime_error {
public:
ReceiverExpectationViolation(const std::string msg) : std::runtime_error(msg) {}
};
struct ReceiverExpectation : public ReceiverTestStep {
std::string to_string() const { return "Expectation: " + description(); }
virtual std::string description() const { return "description missing"; }
virtual void execute(TCPReceiver &) const {}
virtual ~ReceiverExpectation() {}
};
struct ExpectState : public ReceiverExpectation {
std::string _state;
ExpectState(const std::string &state) : _state(state) {}
std::string description() const { return "in state `" + _state + "`"; }
void execute(TCPReceiver &receiver) const {
if (TCPState::state_summary(receiver) != _state) {
throw ReceiverExpectationViolation("The TCPReceiver was in state `" + TCPState::state_summary(receiver) +
"`, but it was expected to be in state `" + _state + "`");
}
}
};
struct ExpectAckno : public ReceiverExpectation {
std::optional<WrappingInt32> _ackno;
ExpectAckno(std::optional<WrappingInt32> ackno) : _ackno(ackno) {}
std::string description() const {
if (_ackno.has_value()) {
return "ackno " + std::to_string(_ackno.value().raw_value());
} else {
return "no ackno available";
}
}
void execute(TCPReceiver &receiver) const {
if (receiver.ackno() != _ackno) {
std::string reported =
receiver.ackno().has_value() ? std::to_string(receiver.ackno().value().raw_value()) : "none";
std::string expected = _ackno.has_value() ? std::to_string(_ackno.value().raw_value()) : "none";
throw ReceiverExpectationViolation("The TCPReceiver reported ackno `" + reported +
"`, but it was expected to be `" + expected + "`");
}
}
};
struct ExpectWindow : public ReceiverExpectation {
size_t _window;
ExpectWindow(const size_t window) : _window(window) {}
std::string description() const { return "window " + std::to_string(_window); }
void execute(TCPReceiver &receiver) const {
if (receiver.window_size() != _window) {
std::string reported = std::to_string(receiver.window_size());
std::string expected = std::to_string(_window);
throw ReceiverExpectationViolation("The TCPReceiver reported window `" + reported +
"`, but it was expected to be `" + expected + "`");
}
}
};
struct ExpectUnassembledBytes : public ReceiverExpectation {
size_t _n_bytes;
ExpectUnassembledBytes(size_t n_bytes) : _n_bytes(n_bytes) {}
std::string description() const { return std::to_string(_n_bytes) + " unassembled bytes"; }
void execute(TCPReceiver &receiver) const {
if (receiver.unassembled_bytes() != _n_bytes) {
std::ostringstream ss;
ss << "The TCPReceiver reported `" << receiver.unassembled_bytes()
<< "` unassembled bytes, but there was expected to be `" << _n_bytes << "` unassembled bytes";
throw ReceiverExpectationViolation(ss.str());
}
}
};
struct ExpectTotalAssembledBytes : public ReceiverExpectation {
size_t _n_bytes;
ExpectTotalAssembledBytes(size_t n_bytes) : _n_bytes(n_bytes) {}
std::string description() const { return std::to_string(_n_bytes) + " assembled bytes, in total"; }
void execute(TCPReceiver &receiver) const {
if (receiver.stream_out().bytes_written() != _n_bytes) {
std::ostringstream ss;
ss << "The TCPReceiver stream reported `" << receiver.stream_out().bytes_written()
<< "` bytes written, but there was expected to be `" << _n_bytes << "` bytes written (in total)";
throw ReceiverExpectationViolation(ss.str());
}
}
};
struct ExpectEof : public ReceiverExpectation {
ExpectEof() {}
std::string description() const { return "receiver.stream_out().eof() == true"; }
void execute(TCPReceiver &receiver) const {
if (not receiver.stream_out().eof()) {
throw ReceiverExpectationViolation(
"The TCPReceiver stream reported eof() == false, but was expected to be true");
}
}
};
struct ExpectInputNotEnded : public ReceiverExpectation {
ExpectInputNotEnded() {}
std::string description() const { return "receiver.stream_out().input_ended() == false"; }
void execute(TCPReceiver &receiver) const {
if (receiver.stream_out().input_ended()) {
throw ReceiverExpectationViolation(
"The TCPReceiver stream reported input_ended() == true, but was expected to be false");
}
}
};
struct ExpectBytes : public ReceiverExpectation {
std::string _bytes;
ExpectBytes(std::string &&bytes) : _bytes(std::move(bytes)) {}
std::string description() const {
std::ostringstream ss;
ss << "bytes available: \"" << _bytes << "\"";
return ss.str();
}
void execute(TCPReceiver &receiver) const {
ByteStream &stream = receiver.stream_out();
if (stream.buffer_size() != _bytes.size()) {
std::ostringstream ss;
ss << "The TCPReceiver reported `" << stream.buffer_size()
<< "` bytes available, but there were expected to be `" << _bytes.size() << "` bytes available";
throw ReceiverExpectationViolation(ss.str());
}
std::string bytes = stream.read(_bytes.size());
if (not std::equal(bytes.begin(), bytes.end(), _bytes.begin(), _bytes.end())) {
std::ostringstream ss;
ss << "the TCPReceiver assembled \"" << bytes << "\", but was expected to assemble \"" << _bytes << "\".";
throw ReceiverExpectationViolation(ss.str());
}
}
};
struct ReceiverAction : public ReceiverTestStep {
std::string to_string() const { return "Action: " + description(); }
virtual std::string description() const { return "description missing"; }
virtual void execute(TCPReceiver &) const {}
virtual ~ReceiverAction() {}
};
struct SegmentArrives : public ReceiverAction {
enum class Result { NOT_SYN, OK };
static std::string result_name(Result res) {
switch (res) {
case Result::NOT_SYN:
return "(no SYN received, so no ackno available)";
case Result::OK:
return "(SYN received, so ackno available)";
default:
return "unknown";
}
}
bool ack{};
bool rst{};
bool syn{};
bool fin{};
WrappingInt32 seqno{0};
WrappingInt32 ackno{0};
uint16_t win{};
std::string data{};
std::optional<Result> result{};
SegmentArrives &with_ack(WrappingInt32 ackno_) {
ack = true;
ackno = ackno_;
return *this;
}
SegmentArrives &with_ack(uint32_t ackno_) { return with_ack(WrappingInt32{ackno_}); }
SegmentArrives &with_rst() {
rst = true;
return *this;
}
SegmentArrives &with_syn() {
syn = true;
return *this;
}
SegmentArrives &with_fin() {
fin = true;
return *this;
}
SegmentArrives &with_seqno(WrappingInt32 seqno_) {
seqno = seqno_;
return *this;
}
SegmentArrives &with_seqno(uint32_t seqno_) { return with_seqno(WrappingInt32{seqno_}); }
SegmentArrives &with_win(uint16_t win_) {
win = win_;
return *this;
}
SegmentArrives &with_data(std::string data_) {
data = data_;
return *this;
}
SegmentArrives &with_result(Result result_) {
result = result_;
return *this;
}
TCPSegment build_segment() const {
TCPSegment seg;
seg.payload() = std::string(data);
seg.header().ack = ack;
seg.header().fin = fin;
seg.header().syn = syn;
seg.header().rst = rst;
seg.header().ackno = ackno;
seg.header().seqno = seqno;
seg.header().win = win;
return seg;
}
std::string description() const override {
TCPSegment seg = build_segment();
std::ostringstream o;
o << "segment arrives ";
o << seg.header().summary();
if (data.size() > 0) {
o << " with data \"" << data << "\"";
}
return o.str();
}
void execute(TCPReceiver &receiver) const override {
TCPSegment seg = build_segment();
std::ostringstream o;
o << seg.header().summary();
if (data.size() > 0) {
o << " with data \"" << data << "\"";
}
receiver.segment_received(std::move(seg));
Result res;
if (not receiver.ackno().has_value()) {
res = Result::NOT_SYN;
} else {
res = Result::OK;
}
if (result.has_value() and result.value() != res) {
throw ReceiverExpectationViolation("TCPReceiver::segment_received() reported `" + result_name(res) +
"` in response to `" + o.str() + "`, but it was expected to report `" +
result_name(result.value()) + "`");
}
}
};
class TCPReceiverTestHarness {
TCPReceiver receiver;
std::vector<std::string> steps_executed;
public:
TCPReceiverTestHarness(size_t capacity) : receiver(capacity), steps_executed() {
std::ostringstream ss;
ss << "Initialized with ("
<< "capacity=" << capacity << ")";
steps_executed.emplace_back(ss.str());
}
void execute(const ReceiverTestStep &step) {
try {
step.execute(receiver);
steps_executed.emplace_back(step.to_string());
} catch (const ReceiverExpectationViolation &e) {
std::cerr << "Test Failure on expectation:\n\t" << step.to_string();
std::cerr << "\n\nFailure message:\n\t" << e.what();
std::cerr << "\n\nList of steps that executed successfully:";
for (const std::string &s : steps_executed) {
std::cerr << "\n\t" << s;
}
std::cerr << std::endl << std::endl;
throw e;
}
}
};
#endif // SPONGE_RECEIVER_HARNESS_HH