1 | // |
2 | // MultipartReaderTest.cpp |
3 | // |
4 | // Copyright (c) 2005-2006, Applied Informatics Software Engineering GmbH. |
5 | // and Contributors. |
6 | // |
7 | // SPDX-License-Identifier: BSL-1.0 |
8 | // |
9 | |
10 | |
11 | #include "MultipartReaderTest.h" |
12 | #include "Poco/CppUnit/TestCaller.h" |
13 | #include "Poco/CppUnit/TestSuite.h" |
14 | #include "Poco/Net/MultipartReader.h" |
15 | #include "Poco/Net/MessageHeader.h" |
16 | #include "Poco/Net/NetException.h" |
17 | #include <sstream> |
18 | |
19 | |
20 | using Poco::Net::MultipartReader; |
21 | using Poco::Net::MessageHeader; |
22 | using Poco::Net::MultipartException; |
23 | |
24 | |
25 | MultipartReaderTest::MultipartReaderTest(const std::string& name): CppUnit::TestCase(name) |
26 | { |
27 | } |
28 | |
29 | |
30 | MultipartReaderTest::~MultipartReaderTest() |
31 | { |
32 | } |
33 | |
34 | |
35 | void MultipartReaderTest::testReadOnePart() |
36 | { |
37 | std::string s("\r\n--MIME_boundary_01234567\r\nname1: value1\r\n\r\nthis is part 1\r\n--MIME_boundary_01234567--\r\n" ); |
38 | std::istringstream istr(s); |
39 | MultipartReader r(istr, "MIME_boundary_01234567" ); |
40 | assertTrue (r.boundary() == "MIME_boundary_01234567" ); |
41 | assertTrue (r.hasNextPart()); |
42 | MessageHeader h; |
43 | r.nextPart(h); |
44 | assertTrue (h.size() == 1); |
45 | assertTrue (h["name1" ] == "value1" ); |
46 | std::istream& i = r.stream(); |
47 | int ch = i.get(); |
48 | std::string part; |
49 | while (ch >= 0) |
50 | { |
51 | part += (char) ch; |
52 | ch = i.get(); |
53 | } |
54 | assertTrue (part == "this is part 1" ); |
55 | assertTrue (!r.hasNextPart()); |
56 | try |
57 | { |
58 | r.nextPart(h); |
59 | fail("no more parts - must throw" ); |
60 | } |
61 | catch (MultipartException&) |
62 | { |
63 | } |
64 | } |
65 | |
66 | |
67 | void MultipartReaderTest::testReadTwoParts() |
68 | { |
69 | std::string s("\r\n--MIME_boundary_01234567\r\nname1: value1\r\n\r\nthis is part 1\r\n--MIME_boundary_01234567\r\n\r\nthis is part 2\r\n\r\n--MIME_boundary_01234567--\r\n" ); |
70 | std::istringstream istr(s); |
71 | MultipartReader r(istr, "MIME_boundary_01234567" ); |
72 | assertTrue (r.hasNextPart()); |
73 | MessageHeader h; |
74 | r.nextPart(h); |
75 | assertTrue (h.size() == 1); |
76 | assertTrue (h["name1" ] == "value1" ); |
77 | std::istream& i = r.stream(); |
78 | int ch = i.get(); |
79 | std::string part; |
80 | while (ch >= 0) |
81 | { |
82 | part += (char) ch; |
83 | ch = i.get(); |
84 | } |
85 | assertTrue (part == "this is part 1" ); |
86 | assertTrue (r.hasNextPart()); |
87 | r.nextPart(h); |
88 | assertTrue (h.empty()); |
89 | std::istream& ii = r.stream(); |
90 | part.clear(); |
91 | ch = ii.get(); |
92 | while (ch >= 0) |
93 | { |
94 | part += (char) ch; |
95 | ch = ii.get(); |
96 | } |
97 | assertTrue (part == "this is part 2\r\n" ); |
98 | |
99 | try |
100 | { |
101 | r.nextPart(h); |
102 | fail("no more parts - must throw" ); |
103 | } |
104 | catch (MultipartException&) |
105 | { |
106 | } |
107 | } |
108 | |
109 | |
110 | void MultipartReaderTest::testReadEmptyLines() |
111 | { |
112 | std::string s("\r\n--MIME_boundary_01234567\r\nname1: value1\r\n\r\nthis is\r\npart 1\r\n\r\n--MIME_boundary_01234567\r\n\r\nthis\r\n\r\nis part 2\r\n\r\n\r\n--MIME_boundary_01234567--\r\n" ); |
113 | std::istringstream istr(s); |
114 | MultipartReader r(istr, "MIME_boundary_01234567" ); |
115 | assertTrue (r.hasNextPart()); |
116 | MessageHeader h; |
117 | r.nextPart(h); |
118 | assertTrue (h.size() == 1); |
119 | assertTrue (h["name1" ] == "value1" ); |
120 | std::istream& i = r.stream(); |
121 | int ch = i.get(); |
122 | std::string part; |
123 | while (ch >= 0) |
124 | { |
125 | part += (char) ch; |
126 | ch = i.get(); |
127 | } |
128 | assertTrue (part == "this is\r\npart 1\r\n" ); |
129 | assertTrue (r.hasNextPart()); |
130 | r.nextPart(h); |
131 | assertTrue (h.empty()); |
132 | std::istream& ii = r.stream(); |
133 | part.clear(); |
134 | ch = ii.get(); |
135 | while (ch >= 0) |
136 | { |
137 | part += (char) ch; |
138 | ch = ii.get(); |
139 | } |
140 | assertTrue (part == "this\r\n\r\nis part 2\r\n\r\n" ); |
141 | |
142 | try |
143 | { |
144 | r.nextPart(h); |
145 | fail("no more parts - must throw" ); |
146 | } |
147 | catch (MultipartException&) |
148 | { |
149 | } |
150 | } |
151 | |
152 | |
153 | void MultipartReaderTest::testReadLongPart() |
154 | { |
155 | std::string longPart(3000, 'X'); |
156 | std::string s("\r\n--MIME_boundary_01234567\r\nname1: value1\r\n\r\n" ); |
157 | s.append(longPart); |
158 | s.append("\r\n--MIME_boundary_01234567\r\n\r\nthis is part 2\r\n--MIME_boundary_01234567--\r\n" ); |
159 | std::istringstream istr(s); |
160 | MultipartReader r(istr, "MIME_boundary_01234567" ); |
161 | assertTrue (r.hasNextPart()); |
162 | MessageHeader h; |
163 | r.nextPart(h); |
164 | assertTrue (h.size() == 1); |
165 | assertTrue (h["name1" ] == "value1" ); |
166 | std::istream& i = r.stream(); |
167 | int ch = i.get(); |
168 | std::string part; |
169 | while (ch >= 0) |
170 | { |
171 | part += (char) ch; |
172 | ch = i.get(); |
173 | } |
174 | assertTrue (part == longPart); |
175 | assertTrue (r.hasNextPart()); |
176 | r.nextPart(h); |
177 | assertTrue (h.empty()); |
178 | std::istream& ii = r.stream(); |
179 | part.clear(); |
180 | ch = ii.get(); |
181 | while (ch >= 0) |
182 | { |
183 | part += (char) ch; |
184 | ch = ii.get(); |
185 | } |
186 | assertTrue (part == "this is part 2" ); |
187 | |
188 | try |
189 | { |
190 | r.nextPart(h); |
191 | fail("no more parts - must throw" ); |
192 | } |
193 | catch (MultipartException&) |
194 | { |
195 | } |
196 | } |
197 | |
198 | |
199 | void MultipartReaderTest::testGuessBoundary() |
200 | { |
201 | std::string s("\r\n--MIME_boundary_01234567\r\nname1: value1\r\n\r\nthis is part 1\r\n--MIME_boundary_01234567--\r\n" ); |
202 | std::istringstream istr(s); |
203 | MultipartReader r(istr); |
204 | assertTrue (r.hasNextPart()); |
205 | MessageHeader h; |
206 | r.nextPart(h); |
207 | assertTrue (r.boundary() == "MIME_boundary_01234567" ); |
208 | assertTrue (h.size() == 1); |
209 | assertTrue (h["name1" ] == "value1" ); |
210 | std::istream& i = r.stream(); |
211 | int ch = i.get(); |
212 | std::string part; |
213 | while (ch >= 0) |
214 | { |
215 | part += (char) ch; |
216 | ch = i.get(); |
217 | } |
218 | assertTrue (part == "this is part 1" ); |
219 | assertTrue (!r.hasNextPart()); |
220 | try |
221 | { |
222 | r.nextPart(h); |
223 | fail("no more parts - must throw" ); |
224 | } |
225 | catch (MultipartException&) |
226 | { |
227 | } |
228 | } |
229 | |
230 | |
231 | void MultipartReaderTest::testPreamble() |
232 | { |
233 | std::string s("this is the\r\npreamble\r\n--MIME_boundary_01234567\r\nname1: value1\r\n\r\nthis is part 1\r\n--MIME_boundary_01234567--\r\n" ); |
234 | std::istringstream istr(s); |
235 | MultipartReader r(istr, "MIME_boundary_01234567" ); |
236 | assertTrue (r.hasNextPart()); |
237 | MessageHeader h; |
238 | r.nextPart(h); |
239 | assertTrue (h.size() == 1); |
240 | assertTrue (h["name1" ] == "value1" ); |
241 | std::istream& i = r.stream(); |
242 | int ch = i.get(); |
243 | std::string part; |
244 | while (ch >= 0) |
245 | { |
246 | part += (char) ch; |
247 | ch = i.get(); |
248 | } |
249 | assertTrue (part == "this is part 1" ); |
250 | assertTrue (!r.hasNextPart()); |
251 | try |
252 | { |
253 | r.nextPart(h); |
254 | fail("no more parts - must throw" ); |
255 | } |
256 | catch (MultipartException&) |
257 | { |
258 | } |
259 | } |
260 | |
261 | |
262 | void MultipartReaderTest::testBadBoundary() |
263 | { |
264 | std::string s("\r\n--MIME_boundary_01234567\r\nname1: value1\r\n\r\nthis is part 1\r\n--MIME_boundary_01234567--\r\n" ); |
265 | std::istringstream istr(s); |
266 | MultipartReader r(istr, "MIME_boundary_7654321" ); |
267 | assertTrue (r.hasNextPart()); |
268 | MessageHeader h; |
269 | try |
270 | { |
271 | r.nextPart(h); |
272 | } |
273 | catch (MultipartException&) |
274 | { |
275 | } |
276 | } |
277 | |
278 | |
279 | void MultipartReaderTest::testRobustness() |
280 | { |
281 | std::string s("--MIME_boundary_01234567\rname1: value1\r\n\nthis is part 1\n--MIME_boundary_01234567--" ); |
282 | std::istringstream istr(s); |
283 | MultipartReader r(istr, "MIME_boundary_01234567" ); |
284 | assertTrue (r.hasNextPart()); |
285 | MessageHeader h; |
286 | r.nextPart(h); |
287 | assertTrue (h.size() == 1); |
288 | assertTrue (h["name1" ] == "value1" ); |
289 | std::istream& i = r.stream(); |
290 | int ch = i.get(); |
291 | std::string part; |
292 | while (ch >= 0) |
293 | { |
294 | part += (char) ch; |
295 | ch = i.get(); |
296 | } |
297 | assertTrue (part == "this is part 1" ); |
298 | assertTrue (!r.hasNextPart()); |
299 | try |
300 | { |
301 | r.nextPart(h); |
302 | fail("no more parts - must throw" ); |
303 | } |
304 | catch (MultipartException&) |
305 | { |
306 | } |
307 | } |
308 | |
309 | |
310 | void MultipartReaderTest::testUnixLineEnds() |
311 | { |
312 | std::string s("\n--MIME_boundary_01234567\nname1: value1\n\nthis is part 1\n--MIME_boundary_01234567\n\nthis is part 2\n\n--MIME_boundary_01234567--\n" ); |
313 | std::istringstream istr(s); |
314 | MultipartReader r(istr, "MIME_boundary_01234567" ); |
315 | assertTrue (r.hasNextPart()); |
316 | MessageHeader h; |
317 | r.nextPart(h); |
318 | assertTrue (h.size() == 1); |
319 | assertTrue (h["name1" ] == "value1" ); |
320 | std::istream& i = r.stream(); |
321 | int ch = i.get(); |
322 | std::string part; |
323 | while (ch >= 0) |
324 | { |
325 | part += (char) ch; |
326 | ch = i.get(); |
327 | } |
328 | assertTrue (part == "this is part 1" ); |
329 | assertTrue (r.hasNextPart()); |
330 | r.nextPart(h); |
331 | assertTrue (h.empty()); |
332 | std::istream& ii = r.stream(); |
333 | part.clear(); |
334 | ch = ii.get(); |
335 | while (ch >= 0) |
336 | { |
337 | part += (char) ch; |
338 | ch = ii.get(); |
339 | } |
340 | assertTrue (part == "this is part 2\n" ); |
341 | |
342 | try |
343 | { |
344 | r.nextPart(h); |
345 | fail("no more parts - must throw" ); |
346 | } |
347 | catch (MultipartException&) |
348 | { |
349 | } |
350 | } |
351 | |
352 | |
353 | void MultipartReaderTest::setUp() |
354 | { |
355 | } |
356 | |
357 | |
358 | void MultipartReaderTest::tearDown() |
359 | { |
360 | } |
361 | |
362 | |
363 | CppUnit::Test* MultipartReaderTest::suite() |
364 | { |
365 | CppUnit::TestSuite* pSuite = new CppUnit::TestSuite("MultipartReaderTest" ); |
366 | |
367 | CppUnit_addTest(pSuite, MultipartReaderTest, testReadOnePart); |
368 | CppUnit_addTest(pSuite, MultipartReaderTest, testReadTwoParts); |
369 | CppUnit_addTest(pSuite, MultipartReaderTest, testReadEmptyLines); |
370 | CppUnit_addTest(pSuite, MultipartReaderTest, testReadLongPart); |
371 | CppUnit_addTest(pSuite, MultipartReaderTest, testGuessBoundary); |
372 | CppUnit_addTest(pSuite, MultipartReaderTest, testPreamble); |
373 | CppUnit_addTest(pSuite, MultipartReaderTest, testBadBoundary); |
374 | CppUnit_addTest(pSuite, MultipartReaderTest, testRobustness); |
375 | CppUnit_addTest(pSuite, MultipartReaderTest, testUnixLineEnds); |
376 | |
377 | return pSuite; |
378 | } |
379 | |