forked from huangrt01/TCP-Lab
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfsm_stream_reassembler_many.cc
125 lines (102 loc) · 4.55 KB
/
fsm_stream_reassembler_many.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
#include "byte_stream.hh"
#include "stream_reassembler.hh"
#include "util.hh"
#include <algorithm>
#include <cstdint>
#include <cstdlib>
#include <exception>
#include <iostream>
#include <stdexcept>
#include <tuple>
#include <utility>
#include <vector>
using namespace std;
static constexpr unsigned NREPS = 32;
static constexpr unsigned NSEGS = 128;
static constexpr unsigned MAX_SEG_LEN = 2048;
string read(StreamReassembler &reassembler) {
return reassembler.stream_out().read(reassembler.stream_out().buffer_size());
}
int main() {
try {
auto rd = get_random_generator();
// buffer a bunch of bytes, make sure we can empty and re-fill before calling close()
for (unsigned rep_no = 0; rep_no < NREPS; ++rep_no) {
StreamReassembler buf{MAX_SEG_LEN * NSEGS};
vector<tuple<size_t, size_t>> seq_size;
size_t offset = 0;
for (unsigned i = 0; i < NSEGS; ++i) {
const size_t size = 1 + (rd() % (MAX_SEG_LEN - 1));
seq_size.emplace_back(offset, size);
offset += size;
}
shuffle(seq_size.begin(), seq_size.end(), rd);
string d(offset, 0);
generate(d.begin(), d.end(), [&] { return rd(); });
for (auto [off, sz] : seq_size) {
string dd(d.cbegin() + off, d.cbegin() + off + sz);
buf.push_substring(move(dd), off, off + sz == offset);
}
auto result = read(buf);
if (buf.stream_out().bytes_written() != offset) { // read bytes
throw runtime_error("test 1 - number of bytes RX is incorrect");
}
if (!equal(result.cbegin(), result.cend(), d.cbegin())) {
throw runtime_error("test 1 - content of RX bytes is incorrect");
}
}
// insert EOF into a hole in the buffer
for (unsigned rep_no = 0; rep_no < NREPS; ++rep_no) {
StreamReassembler buf{65'000};
const size_t size = 1024;
string d(size, 0);
generate(d.begin(), d.end(), [&] { return rd(); });
buf.push_substring(d, 0, false);
buf.push_substring(d.substr(10), size + 10, false);
auto res1 = read(buf);
if (buf.stream_out().bytes_written() != size) {
throw runtime_error("test 3 - number of RX bytes is incorrect");
}
if (!equal(res1.cbegin(), res1.cend(), d.cbegin())) {
throw runtime_error("test 3 - content of RX bytes is incorrect");
}
buf.push_substring(string(d.cbegin(), d.cbegin() + 7), size, false);
buf.push_substring(string(d.cbegin() + 7, d.cbegin() + 8), size + 7, true);
auto res2 = read(buf);
if (buf.stream_out().bytes_written() != size + 8) { // rx bytes
throw runtime_error("test 3 - number of RX bytes is incorrect after 2nd read");
}
if (!equal(res2.cbegin(), res2.cend(), d.cbegin())) {
throw runtime_error("test 3 - content of RX bytes is incorrect after 2nd read");
}
}
// insert EOF over previously queued data, require one of two possible correct actions
for (unsigned rep_no = 0; rep_no < NREPS; ++rep_no) {
StreamReassembler buf{65'000};
const size_t size = 1024;
string d(size, 0);
generate(d.begin(), d.end(), [&] { return rd(); });
buf.push_substring(d, 0, false);
buf.push_substring(d.substr(10), size + 10, false);
auto res1 = read(buf);
if (buf.stream_out().bytes_written() != size) {
throw runtime_error("test 4 - number of RX bytes is incorrect");
}
if (!equal(res1.cbegin(), res1.cend(), d.cbegin())) {
throw runtime_error("test 4 - content of RX bytes is incorrect");
}
buf.push_substring(string(d.cbegin(), d.cbegin() + 15), size, true);
auto res2 = read(buf);
if (buf.stream_out().bytes_written() != 2 * size && buf.stream_out().bytes_written() != size + 15) {
throw runtime_error("test 4 - number of RX bytes is incorrect after 2nd read");
}
if (!equal(res2.cbegin(), res2.cend(), d.cbegin())) {
throw runtime_error("test 4 - content of RX bytes is incorrect after 2nd read");
}
}
} catch (const exception &e) {
cerr << "Exception: " << e.what() << endl;
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}