1//
2// MessageHeaderTest.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 "MessageHeaderTest.h"
12#include "Poco/CppUnit/TestCaller.h"
13#include "Poco/CppUnit/TestSuite.h"
14#include "Poco/Net/MessageHeader.h"
15#include "Poco/Net/NetException.h"
16#include <sstream>
17
18
19using Poco::Net::MessageHeader;
20using Poco::Net::NameValueCollection;
21using Poco::Net::MessageException;
22
23
24MessageHeaderTest::MessageHeaderTest(const std::string& name): CppUnit::TestCase(name)
25{
26}
27
28
29MessageHeaderTest::~MessageHeaderTest()
30{
31}
32
33
34void MessageHeaderTest::testWrite()
35{
36 MessageHeader mh;
37 mh.set("name1", "value1");
38 mh.set("name2", "value2");
39 mh.set("name3", "value3");
40
41 std::ostringstream ostr;
42 mh.write(ostr);
43 std::string s = ostr.str();
44 assertTrue (s == "name1: value1\r\nname2: value2\r\nname3: value3\r\n");
45}
46
47
48void MessageHeaderTest::testRead1()
49{
50 std::string s("name1: value1\r\nname2: value2\r\nname3: value3\r\n");
51 std::istringstream istr(s);
52 MessageHeader mh;
53 mh.read(istr);
54 assertTrue (mh.size() == 3);
55 assertTrue (mh["name1"] == "value1");
56 assertTrue (mh["name2"] == "value2");
57 assertTrue (mh["name3"] == "value3");
58}
59
60
61void MessageHeaderTest::testRead2()
62{
63 std::string s("name1: value1\nname2: value2\nname3: value3\n");
64 std::istringstream istr(s);
65 MessageHeader mh;
66 mh.read(istr);
67 assertTrue (mh.size() == 3);
68 assertTrue (mh["name1"] == "value1");
69 assertTrue (mh["name2"] == "value2");
70 assertTrue (mh["name3"] == "value3");
71}
72
73
74void MessageHeaderTest::testRead3()
75{
76 std::string s("name1: value1\r\n");
77 std::istringstream istr(s);
78 MessageHeader mh;
79 mh.read(istr);
80 assertTrue (mh.size() == 1);
81 assertTrue (mh["name1"] == "value1");
82}
83
84
85
86void MessageHeaderTest::testRead4()
87{
88 std::string s("name1: value1\r\nname2: value2\r\n\r\nsomedata");
89 std::istringstream istr(s);
90 MessageHeader mh;
91 mh.read(istr);
92 assertTrue (mh.size() == 2);
93 assertTrue (mh["name1"] == "value1");
94 assertTrue (mh["name2"] == "value2");
95 int ch = istr.get();
96 assertTrue (ch == '\r');
97 ch = istr.get();
98 assertTrue (ch == '\n');
99 ch = istr.get();
100 assertTrue (ch == 's');
101}
102
103
104void MessageHeaderTest::testRead5()
105{
106 std::string s("name1:\r\nname2: value2\r\nname3: value3 \r\n");
107 std::istringstream istr(s);
108 MessageHeader mh;
109 mh.read(istr);
110 assertTrue (mh.size() == 3);
111 assertTrue (mh["name1"] == "");
112 assertTrue (mh["name2"] == "value2");
113 assertTrue (mh["name3"] == "value3");
114}
115
116
117void MessageHeaderTest::testReadFolding1()
118{
119 std::string s("name1: value1\r\nname2: value21\r\n value22\r\nname3: value3\r\n");
120 std::istringstream istr(s);
121 MessageHeader mh;
122 mh.read(istr);
123 assertTrue (mh.size() == 3);
124 assertTrue (mh["name1"] == "value1");
125 assertTrue (mh["name2"] == "value21 value22");
126 assertTrue (mh["name3"] == "value3");
127}
128
129
130void MessageHeaderTest::testReadFolding2()
131{
132 std::string s("name1: value1\nname2: value21\n\tvalue22\nname3: value3\n");
133 std::istringstream istr(s);
134 MessageHeader mh;
135 mh.read(istr);
136 assertTrue (mh.size() == 3);
137 assertTrue (mh["name1"] == "value1");
138 assertTrue (mh["name2"] == "value21\tvalue22");
139 assertTrue (mh["name3"] == "value3");
140}
141
142
143void MessageHeaderTest::testReadFolding3()
144{
145 std::string s("name1: value1\r\nname2: value21\r\n value22\r\n");
146 std::istringstream istr(s);
147 MessageHeader mh;
148 mh.read(istr);
149 assertTrue (mh.size() == 2);
150 assertTrue (mh["name1"] == "value1");
151 assertTrue (mh["name2"] == "value21 value22");
152}
153
154
155void MessageHeaderTest::testReadFolding4()
156{
157 std::string s("name1: value1\r\nname2: value21\r\n value22\r\n value23");
158 std::istringstream istr(s);
159 MessageHeader mh;
160 mh.read(istr);
161 assertTrue (mh.size() == 2);
162 assertTrue (mh["name1"] == "value1");
163 assertTrue (mh["name2"] == "value21 value22 value23");
164}
165
166
167void MessageHeaderTest::testReadFolding5()
168{
169 std::string s("name1: value1\r\nname2: value21\r\n value22\r\n value23\r\nname3: value3");
170 std::istringstream istr(s);
171 MessageHeader mh;
172 mh.read(istr);
173 assertTrue (mh.size() == 3);
174 assertTrue (mh["name1"] == "value1");
175 assertTrue (mh["name2"] == "value21 value22 value23");
176 assertTrue (mh["name3"] == "value3");
177}
178
179
180void MessageHeaderTest::testReadInvalid1()
181{
182 std::string s("name1: value1\r\nname2: value21\r\n value22\r\n value23\r\n");
183 s.append(300, 'x');
184 std::istringstream istr(s);
185 MessageHeader mh;
186 try
187 {
188 mh.read(istr);
189 fail("malformed message - must throw");
190 }
191 catch (MessageException&)
192 {
193 }
194}
195
196
197void MessageHeaderTest::testReadInvalid2()
198{
199 std::string s("name1: value1\r\nname2: ");
200 s.append(9000, 'x');
201 std::istringstream istr(s);
202 MessageHeader mh;
203 try
204 {
205 mh.read(istr);
206 fail("malformed message - must throw");
207 }
208 catch (MessageException&)
209 {
210 }
211}
212
213
214void MessageHeaderTest::testSplitElements()
215{
216 std::string s;
217 std::vector<std::string> v;
218 MessageHeader::splitElements(s, v);
219 assertTrue (v.empty());
220
221 s = "foo";
222 MessageHeader::splitElements(s, v);
223 assertTrue (v.size() == 1);
224 assertTrue (v[0] == "foo");
225
226 s = " foo ";
227 MessageHeader::splitElements(s, v);
228 assertTrue (v.size() == 1);
229 assertTrue (v[0] == "foo");
230
231 s = "foo,bar";
232 MessageHeader::splitElements(s, v);
233 assertTrue (v.size() == 2);
234 assertTrue (v[0] == "foo");
235 assertTrue (v[1] == "bar");
236
237 s = "foo,,bar";
238 MessageHeader::splitElements(s, v);
239 assertTrue (v.size() == 2);
240 assertTrue (v[0] == "foo");
241 assertTrue (v[1] == "bar");
242
243 MessageHeader::splitElements(s, v, false);
244 assertTrue (v.size() == 3);
245 assertTrue (v[0] == "foo");
246 assertTrue (v[1] == "");
247 assertTrue (v[2] == "bar");
248
249 s = "foo;param=\"a,b\",bar;param=\"c,d\"";
250 MessageHeader::splitElements(s, v);
251 assertTrue (v.size() == 2);
252 assertTrue (v[0] == "foo;param=\"a,b\"");
253 assertTrue (v[1] == "bar;param=\"c,d\"");
254
255 s = "foo; param=\"a,b\", bar; param=\"c,d\"";
256 MessageHeader::splitElements(s, v);
257 assertTrue (v.size() == 2);
258 assertTrue (v[0] == "foo; param=\"a,b\"");
259 assertTrue (v[1] == "bar; param=\"c,d\"");
260
261 s = "foo, bar, f00, baz";
262 MessageHeader::splitElements(s, v);
263 assertTrue (v.size() == 4);
264 assertTrue (v[0] == "foo");
265 assertTrue (v[1] == "bar");
266 assertTrue (v[2] == "f00");
267 assertTrue (v[3] == "baz");
268
269 s = "a,b,c";
270 MessageHeader::splitElements(s, v);
271 assertTrue (v.size() == 3);
272 assertTrue (v[0] == "a");
273 assertTrue (v[1] == "b");
274 assertTrue (v[2] == "c");
275
276 s = "a=\"value=\\\\\\\"foo, bar\\\\\\\"\",b=foo";
277 MessageHeader::splitElements(s, v);
278 assertTrue (v.size() == 2);
279 assertTrue (v[0] == "a=\"value=\\\"foo, bar\\\"\"");
280 assertTrue (v[1] == "b=foo");
281
282 s = "a=\\\",b=\\\"";
283 MessageHeader::splitElements(s, v);
284 assertTrue (v.size() == 2);
285 assertTrue (v[0] == "a=\"");
286 assertTrue (v[1] == "b=\"");
287
288}
289
290
291void MessageHeaderTest::testSplitParameters()
292{
293 std::string s;
294 std::string v;
295 NameValueCollection p;
296
297 MessageHeader::splitParameters(s, v, p);
298 assertTrue (v.empty());
299 assertTrue (p.empty());
300
301 s = "multipart/related";
302 MessageHeader::splitParameters(s, v, p);
303 assertTrue (v == "multipart/related");
304 assertTrue (p.empty());
305
306 s = "multipart/related; boundary=MIME_boundary_01234567";
307 MessageHeader::splitParameters(s, v, p);
308 assertTrue (v == "multipart/related");
309 assertTrue (p.size() == 1);
310 assertTrue (p["boundary"] == "MIME_boundary_01234567");
311
312 s = "multipart/related; boundary=\"MIME_boundary_76543210\"";
313 MessageHeader::splitParameters(s, v, p);
314 assertTrue (v == "multipart/related");
315 assertTrue (p.size() == 1);
316 assertTrue (p["boundary"] == "MIME_boundary_76543210");
317
318 s = "text/plain; charset=us-ascii";
319 MessageHeader::splitParameters(s, v, p);
320 assertTrue (v == "text/plain");
321 assertTrue (p.size() == 1);
322 assertTrue (p["charset"] == "us-ascii");
323
324 s = "value; p1=foo; p2=bar";
325 MessageHeader::splitParameters(s, v, p);
326 assertTrue (v == "value");
327 assertTrue (p.size() == 2);
328 assertTrue (p["p1"] == "foo");
329 assertTrue (p["p2"] == "bar");
330
331 s = "value; p1=\"foo; bar\"";
332 MessageHeader::splitParameters(s, v, p);
333 assertTrue (v == "value");
334 assertTrue (p.size() == 1);
335 assertTrue (p["p1"] == "foo; bar");
336
337 s = "value ; p1=foo ; p2=bar ";
338 MessageHeader::splitParameters(s, v, p);
339 assertTrue (v == "value");
340 assertTrue (p.size() == 2);
341 assertTrue (p["p1"] == "foo");
342 assertTrue (p["p2"] == "bar");
343}
344
345
346void MessageHeaderTest::testFieldLimit()
347{
348 std::string s("name1: value1\r\nname2: value2\r\nname3: value3\r\n");
349 std::istringstream istr(s);
350 MessageHeader mh;
351 mh.setFieldLimit(2);
352 try
353 {
354 mh.read(istr);
355 fail("Field limit exceeded - must throw");
356 }
357 catch (MessageException&)
358 {
359 }
360}
361
362
363void MessageHeaderTest::testDecodeWord()
364{
365 std::string coded("this is pure ASCII");
366 std::string decoded = MessageHeader::decodeWord(coded, "ISO-8859-1");
367 assertTrue (decoded == coded);
368
369 coded = "(=?ISO-8859-1?Q?a?= =?ISO-8859-1?Q?b?=)";
370 decoded = MessageHeader::decodeWord(coded, "ISO-8859-1");
371 assertTrue (decoded == "(a b)");
372
373 coded = "Hello =?UTF-8?B?RnJhbmNpcw==?=, good bye";
374 decoded = MessageHeader::decodeWord(coded, "ISO-8859-1");
375 assertTrue (decoded == "Hello Francis, good bye");
376}
377
378
379
380void MessageHeaderTest::setUp()
381{
382}
383
384
385void MessageHeaderTest::tearDown()
386{
387}
388
389
390CppUnit::Test* MessageHeaderTest::suite()
391{
392 CppUnit::TestSuite* pSuite = new CppUnit::TestSuite("MessageHeaderTest");
393
394 CppUnit_addTest(pSuite, MessageHeaderTest, testWrite);
395 CppUnit_addTest(pSuite, MessageHeaderTest, testRead1);
396 CppUnit_addTest(pSuite, MessageHeaderTest, testRead2);
397 CppUnit_addTest(pSuite, MessageHeaderTest, testRead3);
398 CppUnit_addTest(pSuite, MessageHeaderTest, testRead4);
399 CppUnit_addTest(pSuite, MessageHeaderTest, testRead5);
400 CppUnit_addTest(pSuite, MessageHeaderTest, testReadFolding1);
401 CppUnit_addTest(pSuite, MessageHeaderTest, testReadFolding2);
402 CppUnit_addTest(pSuite, MessageHeaderTest, testReadFolding3);
403 CppUnit_addTest(pSuite, MessageHeaderTest, testReadFolding4);
404 CppUnit_addTest(pSuite, MessageHeaderTest, testReadFolding5);
405 CppUnit_addTest(pSuite, MessageHeaderTest, testReadInvalid1);
406 CppUnit_addTest(pSuite, MessageHeaderTest, testReadInvalid2);
407 CppUnit_addTest(pSuite, MessageHeaderTest, testSplitElements);
408 CppUnit_addTest(pSuite, MessageHeaderTest, testSplitParameters);
409 CppUnit_addTest(pSuite, MessageHeaderTest, testFieldLimit);
410 CppUnit_addTest(pSuite, MessageHeaderTest, testDecodeWord);
411
412 return pSuite;
413}
414