forked from aria2/aria2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathChunkedDecodingStreamFilterTest.cc
273 lines (254 loc) · 8.44 KB
/
ChunkedDecodingStreamFilterTest.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
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
#include "ChunkedDecodingStreamFilter.h"
#include <cstdlib>
#include <iostream>
#include <cppunit/extensions/HelperMacros.h>
#include "DlAbortEx.h"
#include "Segment.h"
#include "ByteArrayDiskWriter.h"
#include "SinkStreamFilter.h"
#include "MockSegment.h"
#include "a2functional.h"
namespace aria2 {
class ChunkedDecodingStreamFilterTest : public CppUnit::TestFixture {
CPPUNIT_TEST_SUITE(ChunkedDecodingStreamFilterTest);
CPPUNIT_TEST(testTransform);
CPPUNIT_TEST(testTransform_withoutTrailer);
CPPUNIT_TEST(testTransform_with2Trailers);
CPPUNIT_TEST(testTransform_largeChunkSize);
CPPUNIT_TEST(testTransform_tooLargeChunkSize);
CPPUNIT_TEST(testTransform_chunkSizeMismatch);
CPPUNIT_TEST(testGetName);
CPPUNIT_TEST_SUITE_END();
std::unique_ptr<ChunkedDecodingStreamFilter> filter_;
std::shared_ptr<ByteArrayDiskWriter> writer_;
std::shared_ptr<Segment> segment_;
void clearWriter() { writer_->setString(""); }
public:
void setUp()
{
writer_ = std::make_shared<ByteArrayDiskWriter>();
auto sinkFilter = make_unique<SinkStreamFilter>();
sinkFilter->init();
filter_ = make_unique<ChunkedDecodingStreamFilter>(std::move(sinkFilter));
filter_->init();
segment_ = std::make_shared<MockSegment>();
}
void testTransform();
void testTransform_withoutTrailer();
void testTransform_with2Trailers();
void testTransform_largeChunkSize();
void testTransform_tooLargeChunkSize();
void testTransform_chunkSizeMismatch();
void testGetName();
};
CPPUNIT_TEST_SUITE_REGISTRATION(ChunkedDecodingStreamFilterTest);
void ChunkedDecodingStreamFilterTest::testTransform()
{
try {
std::basic_string<unsigned char> msg =
reinterpret_cast<const unsigned char*>("a\r\n1234567890\r\n");
ssize_t r = filter_->transform(writer_, segment_, msg.data(), msg.size());
CPPUNIT_ASSERT_EQUAL((ssize_t)10, r);
CPPUNIT_ASSERT_EQUAL(std::string("1234567890"), writer_->getString());
CPPUNIT_ASSERT_EQUAL((size_t)15, filter_->getBytesProcessed());
}
catch (DlAbortEx& e) {
CPPUNIT_FAIL(e.stackTrace());
}
clearWriter();
try {
// Feed extension; see it is ignored.
std::basic_string<unsigned char> msg =
reinterpret_cast<const unsigned char*>("3;extensionIgnored\r\n123\r\n");
ssize_t r = filter_->transform(writer_, segment_, msg.data(), msg.size());
CPPUNIT_ASSERT_EQUAL((ssize_t)3, r);
CPPUNIT_ASSERT_EQUAL(std::string("123"), writer_->getString());
CPPUNIT_ASSERT_EQUAL((size_t)25, filter_->getBytesProcessed());
}
catch (DlAbortEx& e) {
CPPUNIT_FAIL(e.stackTrace());
}
clearWriter();
// Feed 2extensions; see it is ignored.
try {
std::basic_string<unsigned char> msg =
reinterpret_cast<const unsigned char*>(
"3;extension1;extension2;\r\n123\r\n");
ssize_t r = filter_->transform(writer_, segment_, msg.data(), msg.size());
CPPUNIT_ASSERT_EQUAL((ssize_t)3, r);
CPPUNIT_ASSERT_EQUAL(std::string("123"), writer_->getString());
}
catch (DlAbortEx& e) {
CPPUNIT_FAIL(e.stackTrace());
}
clearWriter();
// Not all chunk size is available
try {
std::basic_string<unsigned char> msg =
reinterpret_cast<const unsigned char*>("1");
ssize_t r = filter_->transform(writer_, segment_, msg.data(), msg.size());
CPPUNIT_ASSERT_EQUAL((ssize_t)0, r);
}
catch (DlAbortEx& e) {
CPPUNIT_FAIL(e.stackTrace());
}
clearWriter();
try {
std::basic_string<unsigned char> msg =
reinterpret_cast<const unsigned char*>("0\r\n1234567890123456\r\n");
ssize_t r = filter_->transform(writer_, segment_, msg.data(), msg.size());
CPPUNIT_ASSERT_EQUAL((ssize_t)16, r);
CPPUNIT_ASSERT_EQUAL(std::string("1234567890123456"), writer_->getString());
}
catch (DlAbortEx& e) {
CPPUNIT_FAIL(e.stackTrace());
}
clearWriter();
// Not all chunk data is available
try {
std::basic_string<unsigned char> msg =
reinterpret_cast<const unsigned char*>("10\r\n1234567890");
ssize_t r = filter_->transform(writer_, segment_, msg.data(), msg.size());
CPPUNIT_ASSERT_EQUAL((ssize_t)10, r);
CPPUNIT_ASSERT_EQUAL(std::string("1234567890"), writer_->getString());
}
catch (DlAbortEx& e) {
CPPUNIT_FAIL(e.stackTrace());
}
clearWriter();
try {
std::basic_string<unsigned char> msg =
reinterpret_cast<const unsigned char*>("123456\r\n");
ssize_t r = filter_->transform(writer_, segment_, msg.data(), msg.size());
CPPUNIT_ASSERT_EQUAL((ssize_t)6, r);
CPPUNIT_ASSERT_EQUAL(std::string("123456"), writer_->getString());
}
catch (DlAbortEx& e) {
CPPUNIT_FAIL(e.stackTrace());
}
clearWriter();
// no trailing CR LF.
try {
std::basic_string<unsigned char> msg =
reinterpret_cast<const unsigned char*>("10\r\n1234567890123456");
ssize_t r = filter_->transform(writer_, segment_, msg.data(), msg.size());
CPPUNIT_ASSERT_EQUAL((ssize_t)16, r);
CPPUNIT_ASSERT_EQUAL(std::string("1234567890123456"), writer_->getString());
}
catch (DlAbortEx& e) {
CPPUNIT_FAIL(e.stackTrace());
}
clearWriter();
// feed only CR
try {
std::basic_string<unsigned char> msg =
reinterpret_cast<const unsigned char*>("\r");
ssize_t r = filter_->transform(writer_, segment_, msg.data(), msg.size());
CPPUNIT_ASSERT_EQUAL((ssize_t)0, r);
}
catch (DlAbortEx& e) {
CPPUNIT_FAIL(e.stackTrace());
}
// feed next LF
try {
std::basic_string<unsigned char> msg =
reinterpret_cast<const unsigned char*>("\n");
ssize_t r = filter_->transform(writer_, segment_, msg.data(), msg.size());
CPPUNIT_ASSERT_EQUAL((ssize_t)0, r);
CPPUNIT_ASSERT_EQUAL(std::string(""), writer_->getString());
}
catch (DlAbortEx& e) {
CPPUNIT_FAIL(e.stackTrace());
}
// feed 0 CR LF.
try {
std::basic_string<unsigned char> msg =
reinterpret_cast<const unsigned char*>("0\r\n");
ssize_t r = filter_->transform(writer_, segment_, msg.data(), msg.size());
CPPUNIT_ASSERT_EQUAL((ssize_t)0, r);
}
catch (DlAbortEx& e) {
CPPUNIT_FAIL(e.stackTrace());
}
// feed trailer
try {
std::basic_string<unsigned char> msg =
reinterpret_cast<const unsigned char*>("trailer\r\n");
ssize_t r = filter_->transform(writer_, segment_, msg.data(), msg.size());
CPPUNIT_ASSERT_EQUAL((ssize_t)0, r);
}
catch (DlAbortEx& e) {
CPPUNIT_FAIL(e.stackTrace());
}
// feed final CRLF
try {
std::basic_string<unsigned char> msg =
reinterpret_cast<const unsigned char*>("\r\n");
ssize_t r = filter_->transform(writer_, segment_, msg.data(), msg.size());
CPPUNIT_ASSERT_EQUAL((ssize_t)0, r);
}
catch (DlAbortEx& e) {
CPPUNIT_FAIL(e.stackTrace());
}
// input is over
CPPUNIT_ASSERT(filter_->finished());
}
void ChunkedDecodingStreamFilterTest::testTransform_withoutTrailer()
{
CPPUNIT_ASSERT_EQUAL(
(ssize_t)0, filter_->transform(
writer_, segment_,
reinterpret_cast<const unsigned char*>("0\r\n\r\n"), 5));
CPPUNIT_ASSERT(filter_->finished());
}
void ChunkedDecodingStreamFilterTest::testTransform_with2Trailers()
{
CPPUNIT_ASSERT_EQUAL(
(ssize_t)0,
filter_->transform(
writer_, segment_,
reinterpret_cast<const unsigned char*>("0\r\nt1\r\nt2\r\n\r\n"), 13));
CPPUNIT_ASSERT(filter_->finished());
}
void ChunkedDecodingStreamFilterTest::testTransform_largeChunkSize()
{
// chunkSize should be under 2^63-1
{
std::basic_string<unsigned char> msg =
reinterpret_cast<const unsigned char*>("7fffffffffffffff\r\n");
filter_->transform(writer_, segment_, msg.data(), msg.size());
}
}
void ChunkedDecodingStreamFilterTest::testTransform_tooLargeChunkSize()
{
// chunkSize 2^64 causes error
{
std::basic_string<unsigned char> msg =
reinterpret_cast<const unsigned char*>("ffffffffffffffff\r\n");
try {
filter_->transform(writer_, segment_, msg.data(), msg.size());
CPPUNIT_FAIL("exception must be thrown.");
}
catch (DlAbortEx& e) {
// success
}
}
}
void ChunkedDecodingStreamFilterTest::testTransform_chunkSizeMismatch()
{
std::basic_string<unsigned char> msg =
reinterpret_cast<const unsigned char*>("3\r\n1234\r\n");
try {
filter_->transform(writer_, segment_, msg.data(), msg.size());
CPPUNIT_FAIL("exception must be thrown.");
}
catch (DlAbortEx& e) {
// success
}
}
void ChunkedDecodingStreamFilterTest::testGetName()
{
CPPUNIT_ASSERT_EQUAL(std::string("ChunkedDecodingStreamFilter"),
filter_->getName());
}
} // namespace aria2