~ubuntu-branches/ubuntu/gutsy/poco/gutsy

« back to all changes in this revision

Viewing changes to Net/testsuite/src/MultipartReaderTest.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Krzysztof Burghardt
  • Date: 2007-04-27 18:33:48 UTC
  • Revision ID: james.westby@ubuntu.com-20070427183348-xgnpct0qd6a2ip34
Tags: upstream-1.2.9
ImportĀ upstreamĀ versionĀ 1.2.9

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
//
 
2
// MultipartReaderTest.cpp
 
3
//
 
4
// $Id: //poco/1.2/Net/testsuite/src/MultipartReaderTest.cpp#2 $
 
5
//
 
6
// Copyright (c) 2005-2006, Applied Informatics Software Engineering GmbH.
 
7
// and Contributors.
 
8
//
 
9
// Permission is hereby granted, free of charge, to any person or organization
 
10
// obtaining a copy of the software and accompanying documentation covered by
 
11
// this license (the "Software") to use, reproduce, display, distribute,
 
12
// execute, and transmit the Software, and to prepare derivative works of the
 
13
// Software, and to permit third-parties to whom the Software is furnished to
 
14
// do so, all subject to the following:
 
15
// 
 
16
// The copyright notices in the Software and this entire statement, including
 
17
// the above license grant, this restriction and the following disclaimer,
 
18
// must be included in all copies of the Software, in whole or in part, and
 
19
// all derivative works of the Software, unless such copies or derivative
 
20
// works are solely in the form of machine-executable object code generated by
 
21
// a source language processor.
 
22
// 
 
23
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 
24
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 
25
// FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
 
26
// SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
 
27
// FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
 
28
// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
 
29
// DEALINGS IN THE SOFTWARE.
 
30
//
 
31
 
 
32
 
 
33
#include "MultipartReaderTest.h"
 
34
#include "CppUnit/TestCaller.h"
 
35
#include "CppUnit/TestSuite.h"
 
36
#include "Poco/Net/MultipartReader.h"
 
37
#include "Poco/Net/MessageHeader.h"
 
38
#include "Poco/Net/NetException.h"
 
39
#include <sstream>
 
40
 
 
41
 
 
42
using Poco::Net::MultipartReader;
 
43
using Poco::Net::MessageHeader;
 
44
using Poco::Net::MultipartException;
 
45
 
 
46
 
 
47
MultipartReaderTest::MultipartReaderTest(const std::string& name): CppUnit::TestCase(name)
 
48
{
 
49
}
 
50
 
 
51
 
 
52
MultipartReaderTest::~MultipartReaderTest()
 
53
{
 
54
}
 
55
 
 
56
 
 
57
void MultipartReaderTest::testReadOnePart()
 
58
{
 
59
        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");
 
60
        std::istringstream istr(s);
 
61
        MultipartReader r(istr, "MIME_boundary_01234567");
 
62
        assert (r.boundary() == "MIME_boundary_01234567");
 
63
        assert (r.hasNextPart());
 
64
        MessageHeader h;
 
65
        r.nextPart(h);
 
66
        assert (h.size() == 1);
 
67
        assert (h["name1"] == "value1");
 
68
        std::istream& i = r.stream();
 
69
        int ch = i.get();
 
70
        std::string part;
 
71
        while (ch >= 0)
 
72
        {
 
73
                part += (char) ch;
 
74
                ch = i.get();
 
75
        }
 
76
        assert (part == "this is part 1");
 
77
        assert (!r.hasNextPart());
 
78
        try
 
79
        {
 
80
                r.nextPart(h);
 
81
                fail("no more parts - must throw");
 
82
        }
 
83
        catch (MultipartException&)
 
84
        {
 
85
        }
 
86
}
 
87
 
 
88
 
 
89
void MultipartReaderTest::testReadTwoParts()
 
90
{
 
91
        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");
 
92
        std::istringstream istr(s);
 
93
        MultipartReader r(istr, "MIME_boundary_01234567");
 
94
        assert (r.hasNextPart());
 
95
        MessageHeader h;
 
96
        r.nextPart(h);
 
97
        assert (h.size() == 1);
 
98
        assert (h["name1"] == "value1");
 
99
        std::istream& i = r.stream();
 
100
        int ch = i.get();
 
101
        std::string part;
 
102
        while (ch >= 0)
 
103
        {
 
104
                part += (char) ch;
 
105
                ch = i.get();
 
106
        }
 
107
        assert (part == "this is part 1");
 
108
        assert (r.hasNextPart());
 
109
        r.nextPart(h);
 
110
        assert (h.empty());
 
111
        std::istream& ii = r.stream();
 
112
        part.clear();
 
113
        ch = ii.get();
 
114
        while (ch >= 0)
 
115
        {
 
116
                part += (char) ch;
 
117
                ch = ii.get();
 
118
        }
 
119
        assert (part == "this is part 2\r\n");
 
120
 
 
121
        try
 
122
        {
 
123
                r.nextPart(h);
 
124
                fail("no more parts - must throw");
 
125
        }
 
126
        catch (MultipartException&)
 
127
        {
 
128
        }
 
129
}
 
130
 
 
131
 
 
132
void MultipartReaderTest::testReadEmptyLines()
 
133
{
 
134
        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");
 
135
        std::istringstream istr(s);
 
136
        MultipartReader r(istr, "MIME_boundary_01234567");
 
137
        assert (r.hasNextPart());
 
138
        MessageHeader h;
 
139
        r.nextPart(h);
 
140
        assert (h.size() == 1);
 
141
        assert (h["name1"] == "value1");
 
142
        std::istream& i = r.stream();
 
143
        int ch = i.get();
 
144
        std::string part;
 
145
        while (ch >= 0)
 
146
        {
 
147
                part += (char) ch;
 
148
                ch = i.get();
 
149
        }
 
150
        assert (part == "this is\r\npart 1\r\n");
 
151
        assert (r.hasNextPart());
 
152
        r.nextPart(h);
 
153
        assert (h.empty());
 
154
        std::istream& ii = r.stream();
 
155
        part.clear();
 
156
        ch = ii.get();
 
157
        while (ch >= 0)
 
158
        {
 
159
                part += (char) ch;
 
160
                ch = ii.get();
 
161
        }
 
162
        assert (part == "this\r\n\r\nis part 2\r\n\r\n");
 
163
 
 
164
        try
 
165
        {
 
166
                r.nextPart(h);
 
167
                fail("no more parts - must throw");
 
168
        }
 
169
        catch (MultipartException&)
 
170
        {
 
171
        }
 
172
}
 
173
 
 
174
 
 
175
void MultipartReaderTest::testReadLongPart()
 
176
{
 
177
        std::string longPart(3000, 'X');
 
178
        std::string s("\r\n--MIME_boundary_01234567\r\nname1: value1\r\n\r\n");
 
179
        s.append(longPart);
 
180
        s.append("\r\n--MIME_boundary_01234567\r\n\r\nthis is part 2\r\n--MIME_boundary_01234567--\r\n");
 
181
        std::istringstream istr(s);
 
182
        MultipartReader r(istr, "MIME_boundary_01234567");
 
183
        assert (r.hasNextPart());
 
184
        MessageHeader h;
 
185
        r.nextPart(h);
 
186
        assert (h.size() == 1);
 
187
        assert (h["name1"] == "value1");
 
188
        std::istream& i = r.stream();
 
189
        int ch = i.get();
 
190
        std::string part;
 
191
        while (ch >= 0)
 
192
        {
 
193
                part += (char) ch;
 
194
                ch = i.get();
 
195
        }
 
196
        assert (part == longPart);
 
197
        assert (r.hasNextPart());
 
198
        r.nextPart(h);
 
199
        assert (h.empty());
 
200
        std::istream& ii = r.stream();
 
201
        part.clear();
 
202
        ch = ii.get();
 
203
        while (ch >= 0)
 
204
        {
 
205
                part += (char) ch;
 
206
                ch = ii.get();
 
207
        }
 
208
        assert (part == "this is part 2");
 
209
 
 
210
        try
 
211
        {
 
212
                r.nextPart(h);
 
213
                fail("no more parts - must throw");
 
214
        }
 
215
        catch (MultipartException&)
 
216
        {
 
217
        }
 
218
}
 
219
 
 
220
 
 
221
void MultipartReaderTest::testGuessBoundary()
 
222
{
 
223
        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");
 
224
        std::istringstream istr(s);
 
225
        MultipartReader r(istr);
 
226
        assert (r.hasNextPart());
 
227
        MessageHeader h;
 
228
        r.nextPart(h);
 
229
        assert (r.boundary() == "MIME_boundary_01234567");
 
230
        assert (h.size() == 1);
 
231
        assert (h["name1"] == "value1");
 
232
        std::istream& i = r.stream();
 
233
        int ch = i.get();
 
234
        std::string part;
 
235
        while (ch >= 0)
 
236
        {
 
237
                part += (char) ch;
 
238
                ch = i.get();
 
239
        }
 
240
        assert (part == "this is part 1");
 
241
        assert (!r.hasNextPart());
 
242
        try
 
243
        {
 
244
                r.nextPart(h);
 
245
                fail("no more parts - must throw");
 
246
        }
 
247
        catch (MultipartException&)
 
248
        {
 
249
        }
 
250
}
 
251
 
 
252
 
 
253
void MultipartReaderTest::testPreamble()
 
254
{
 
255
        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");
 
256
        std::istringstream istr(s);
 
257
        MultipartReader r(istr, "MIME_boundary_01234567");
 
258
        assert (r.hasNextPart());
 
259
        MessageHeader h;
 
260
        r.nextPart(h);
 
261
        assert (h.size() == 1);
 
262
        assert (h["name1"] == "value1");
 
263
        std::istream& i = r.stream();
 
264
        int ch = i.get();
 
265
        std::string part;
 
266
        while (ch >= 0)
 
267
        {
 
268
                part += (char) ch;
 
269
                ch = i.get();
 
270
        }
 
271
        assert (part == "this is part 1");
 
272
        assert (!r.hasNextPart());
 
273
        try
 
274
        {
 
275
                r.nextPart(h);
 
276
                fail("no more parts - must throw");
 
277
        }
 
278
        catch (MultipartException&)
 
279
        {
 
280
        }
 
281
}
 
282
 
 
283
 
 
284
void MultipartReaderTest::testBadBoundary()
 
285
{
 
286
        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");
 
287
        std::istringstream istr(s);
 
288
        MultipartReader r(istr, "MIME_boundary_7654321");
 
289
        assert (r.hasNextPart());
 
290
        MessageHeader h;
 
291
        try
 
292
        {
 
293
                r.nextPart(h);
 
294
        }
 
295
        catch (MultipartException&)
 
296
        {
 
297
        }
 
298
}
 
299
 
 
300
 
 
301
void MultipartReaderTest::testRobustness()
 
302
{
 
303
        std::string s("--MIME_boundary_01234567\rname1: value1\r\n\nthis is part 1\n--MIME_boundary_01234567--");
 
304
        std::istringstream istr(s);
 
305
        MultipartReader r(istr, "MIME_boundary_01234567");
 
306
        assert (r.hasNextPart());
 
307
        MessageHeader h;
 
308
        r.nextPart(h);
 
309
        assert (h.size() == 1);
 
310
        assert (h["name1"] == "value1");
 
311
        std::istream& i = r.stream();
 
312
        int ch = i.get();
 
313
        std::string part;
 
314
        while (ch >= 0)
 
315
        {
 
316
                part += (char) ch;
 
317
                ch = i.get();
 
318
        }
 
319
        assert (part == "this is part 1");
 
320
        assert (!r.hasNextPart());
 
321
        try
 
322
        {
 
323
                r.nextPart(h);
 
324
                fail("no more parts - must throw");
 
325
        }
 
326
        catch (MultipartException&)
 
327
        {
 
328
        }
 
329
}
 
330
 
 
331
 
 
332
void MultipartReaderTest::testUnixLineEnds()
 
333
{
 
334
        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");
 
335
        std::istringstream istr(s);
 
336
        MultipartReader r(istr, "MIME_boundary_01234567");
 
337
        assert (r.hasNextPart());
 
338
        MessageHeader h;
 
339
        r.nextPart(h);
 
340
        assert (h.size() == 1);
 
341
        assert (h["name1"] == "value1");
 
342
        std::istream& i = r.stream();
 
343
        int ch = i.get();
 
344
        std::string part;
 
345
        while (ch >= 0)
 
346
        {
 
347
                part += (char) ch;
 
348
                ch = i.get();
 
349
        }
 
350
        assert (part == "this is part 1");
 
351
        assert (r.hasNextPart());
 
352
        r.nextPart(h);
 
353
        assert (h.empty());
 
354
        std::istream& ii = r.stream();
 
355
        part.clear();
 
356
        ch = ii.get();
 
357
        while (ch >= 0)
 
358
        {
 
359
                part += (char) ch;
 
360
                ch = ii.get();
 
361
        }
 
362
        assert (part == "this is part 2\n");
 
363
 
 
364
        try
 
365
        {
 
366
                r.nextPart(h);
 
367
                fail("no more parts - must throw");
 
368
        }
 
369
        catch (MultipartException&)
 
370
        {
 
371
        }
 
372
}
 
373
 
 
374
 
 
375
void MultipartReaderTest::setUp()
 
376
{
 
377
}
 
378
 
 
379
 
 
380
void MultipartReaderTest::tearDown()
 
381
{
 
382
}
 
383
 
 
384
 
 
385
CppUnit::Test* MultipartReaderTest::suite()
 
386
{
 
387
        CppUnit::TestSuite* pSuite = new CppUnit::TestSuite("MultipartReaderTest");
 
388
 
 
389
        CppUnit_addTest(pSuite, MultipartReaderTest, testReadOnePart);
 
390
        CppUnit_addTest(pSuite, MultipartReaderTest, testReadTwoParts);
 
391
        CppUnit_addTest(pSuite, MultipartReaderTest, testReadEmptyLines);
 
392
        CppUnit_addTest(pSuite, MultipartReaderTest, testReadLongPart);
 
393
        CppUnit_addTest(pSuite, MultipartReaderTest, testGuessBoundary);
 
394
        CppUnit_addTest(pSuite, MultipartReaderTest, testPreamble);
 
395
        CppUnit_addTest(pSuite, MultipartReaderTest, testBadBoundary);
 
396
        CppUnit_addTest(pSuite, MultipartReaderTest, testRobustness);
 
397
        CppUnit_addTest(pSuite, MultipartReaderTest, testUnixLineEnds);
 
398
 
 
399
        return pSuite;
 
400
}