~ubuntu-branches/ubuntu/quantal/aria2/quantal

« back to all changes in this revision

Viewing changes to test/JsonTest.cc

  • Committer: Bazaar Package Importer
  • Author(s): Kartik Mistry
  • Date: 2011-04-02 12:38:55 UTC
  • mfrom: (2.5.2 sid)
  • Revision ID: james.westby@ubuntu.com-20110402123855-znkslovhf5qvkjut
Tags: 1.11.1-1
New upstream release

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#include "json.h"
 
2
 
 
3
#include <cppunit/extensions/HelperMacros.h>
 
4
 
 
5
#include "RecoverableException.h"
 
6
#include "util.h"
 
7
#include "array_fun.h"
 
8
#include "Base64.h"
 
9
 
 
10
namespace aria2 {
 
11
 
 
12
class JsonTest:public CppUnit::TestFixture {
 
13
 
 
14
  CPPUNIT_TEST_SUITE(JsonTest);
 
15
  CPPUNIT_TEST(testDecode);
 
16
  CPPUNIT_TEST(testDecode_error);
 
17
  CPPUNIT_TEST(testEncode);
 
18
  CPPUNIT_TEST(testDecodeGetParams);
 
19
  CPPUNIT_TEST_SUITE_END();
 
20
private:
 
21
 
 
22
public:
 
23
  void testDecode();
 
24
  void testDecode_error();
 
25
  void testEncode();
 
26
  void testDecodeGetParams();
 
27
};
 
28
 
 
29
CPPUNIT_TEST_SUITE_REGISTRATION( JsonTest );
 
30
 
 
31
void JsonTest::testDecode()
 
32
{
 
33
  {
 
34
    // empty object
 
35
    SharedHandle<ValueBase> r = json::decode("{}");
 
36
    const Dict* dict = asDict(r);
 
37
    CPPUNIT_ASSERT(dict);
 
38
  }
 
39
  {
 
40
    // empty object
 
41
    SharedHandle<ValueBase> r = json::decode("{  }");
 
42
    const Dict* dict = asDict(r);
 
43
    CPPUNIT_ASSERT(dict);
 
44
  }
 
45
  {
 
46
    // empty array
 
47
    SharedHandle<ValueBase> r = json::decode("[]");
 
48
    const List* list = asList(r);
 
49
    CPPUNIT_ASSERT(list);
 
50
  }
 
51
  {
 
52
    // empty array
 
53
    SharedHandle<ValueBase> r = json::decode("[ ]");
 
54
    const List* list = asList(r);
 
55
    CPPUNIT_ASSERT(list);
 
56
  }
 
57
  {
 
58
    // empty string
 
59
    SharedHandle<ValueBase> r = json::decode("[\"\"]");
 
60
    const List* list = asList(r);
 
61
    CPPUNIT_ASSERT(list);
 
62
    const String* s = asString(list->get(0));
 
63
    CPPUNIT_ASSERT_EQUAL(std::string(), s->s());
 
64
  }
 
65
  {
 
66
    // string
 
67
    SharedHandle<ValueBase> r = json::decode("[\"foobar\"]");
 
68
    const List* list = asList(r);
 
69
    CPPUNIT_ASSERT(list);
 
70
    const String* s = asString(list->get(0));
 
71
    CPPUNIT_ASSERT_EQUAL(std::string("foobar"), s->s());
 
72
  }
 
73
  {
 
74
    // string with escape
 
75
    SharedHandle<ValueBase> r = json::decode("[\"\\\\foo\\\"\\\"bar\"]");
 
76
    const List* list = asList(r);
 
77
    CPPUNIT_ASSERT(list);
 
78
    const String* s = asString(list->get(0));
 
79
    CPPUNIT_ASSERT_EQUAL(std::string("\\foo\"\"bar"), s->s());
 
80
  }
 
81
  {
 
82
    // string with escape
 
83
    SharedHandle<ValueBase> r = json::decode("[\"foo\\\"\"]");
 
84
    const List* list = asList(r);
 
85
    CPPUNIT_ASSERT(list);
 
86
    const String* s = asString(list->get(0));
 
87
    CPPUNIT_ASSERT_EQUAL(std::string("foo\""), s->s());
 
88
  }
 
89
  {
 
90
    // string: utf-8 1 to 3 bytes.
 
91
    SharedHandle<ValueBase> r = json::decode("[\"\\u0024\\u00A2\\u20AC\"]");
 
92
    const List* list = asList(r);
 
93
    CPPUNIT_ASSERT(list);
 
94
    const String* s = asString(list->get(0));
 
95
    CPPUNIT_ASSERT_EQUAL(std::string("$¢€"), s->s());
 
96
  }
 
97
  {
 
98
    // string: utf-8 4 bytes
 
99
    SharedHandle<ValueBase> r = json::decode("[\"\\uD852\\uDF62\"]");
 
100
    const List* list = asList(r);
 
101
    CPPUNIT_ASSERT(list);
 
102
    const String* s = asString(list->get(0));
 
103
    const char arr[] = { 0xF0u, 0xA4u, 0xADu, 0xA2u };
 
104
    CPPUNIT_ASSERT_EQUAL(std::string(vbegin(arr), vend(arr)), s->s());
 
105
  }
 
106
  {
 
107
    // null
 
108
    SharedHandle<ValueBase> r = json::decode("[null]");
 
109
    const List* list = asList(r);
 
110
    CPPUNIT_ASSERT(list);
 
111
    const Null* s = asNull(list->get(0));
 
112
    CPPUNIT_ASSERT(s);
 
113
  }
 
114
  {
 
115
    // true, false
 
116
    SharedHandle<ValueBase> r = json::decode("[true, false]");
 
117
    const List* list = asList(r);
 
118
    CPPUNIT_ASSERT(list);
 
119
    const Bool* trueValue = asBool(list->get(0));
 
120
    CPPUNIT_ASSERT(trueValue);
 
121
    CPPUNIT_ASSERT(trueValue->val());
 
122
    const Bool* falseValue = asBool(list->get(1));
 
123
    CPPUNIT_ASSERT(falseValue);
 
124
    CPPUNIT_ASSERT(!falseValue->val());
 
125
  }
 
126
  {
 
127
    // object: 1 member
 
128
    SharedHandle<ValueBase> r = json::decode("{\"foo\":[\"bar\"]}");
 
129
    const Dict* dict = asDict(r);
 
130
    CPPUNIT_ASSERT(dict);
 
131
    const List* list = asList(dict->get("foo"));
 
132
    CPPUNIT_ASSERT(list);
 
133
    const String* s = asString(list->get(0));
 
134
    CPPUNIT_ASSERT_EQUAL(std::string("bar"), s->s());
 
135
  }
 
136
  {
 
137
    // object: 2 members
 
138
    SharedHandle<ValueBase> r = json::decode("{\"\":[\"bar\"], "
 
139
                                             "\"alpha\" : \"bravo\"}");
 
140
    const Dict* dict = asDict(r);
 
141
    CPPUNIT_ASSERT(dict);
 
142
    const List* list = asList(dict->get(""));
 
143
    CPPUNIT_ASSERT(list);
 
144
    const String* s = asString(list->get(0));
 
145
    CPPUNIT_ASSERT_EQUAL(std::string("bar"), s->s());
 
146
    const String* str = asString(dict->get("alpha"));
 
147
    CPPUNIT_ASSERT_EQUAL(std::string("bravo"), str->s());
 
148
  }
 
149
  {
 
150
    // array: 2 values
 
151
    SharedHandle<ValueBase> r = json::decode("[\"foo\", {}]");
 
152
    const List* list = asList(r);
 
153
    CPPUNIT_ASSERT(list);
 
154
    const String* s = asString(list->get(0));
 
155
    CPPUNIT_ASSERT_EQUAL(std::string("foo"), s->s());
 
156
    const Dict* dict = asDict(list->get(1));
 
157
    CPPUNIT_ASSERT(dict);
 
158
  }
 
159
  {
 
160
    // Number: currently we handle floating point number as string
 
161
    SharedHandle<ValueBase> r = json::decode("[0,-1,1.2,-1.2e-10,-1e10]");
 
162
    const List* list = asList(r);
 
163
    CPPUNIT_ASSERT(list);
 
164
    const Integer* i = asInteger(list->get(0));
 
165
    CPPUNIT_ASSERT_EQUAL((Integer::ValueType)0, i->i());
 
166
    const Integer* i1 = asInteger(list->get(1));
 
167
    CPPUNIT_ASSERT_EQUAL((Integer::ValueType)-1, i1->i());
 
168
    const String* s2 = asString(list->get(2));
 
169
    CPPUNIT_ASSERT_EQUAL(std::string("1.2"), s2->s());
 
170
    const String* s3 = asString(list->get(3));
 
171
    CPPUNIT_ASSERT_EQUAL(std::string("-1.2e-10"), s3->s());
 
172
    const String* s4 = asString(list->get(4));
 
173
    CPPUNIT_ASSERT_EQUAL(std::string("-1e10"), s4->s());
 
174
  }
 
175
}
 
176
 
 
177
void JsonTest::testDecode_error()
 
178
{
 
179
  {
 
180
    try {
 
181
      // object
 
182
      SharedHandle<ValueBase> r = json::decode("{");
 
183
      CPPUNIT_FAIL("exception must be thrown.");
 
184
    } catch(RecoverableException& e) {
 
185
      // success
 
186
    }
 
187
  }
 
188
  {
 
189
    try {
 
190
      // object
 
191
      SharedHandle<ValueBase> r = json::decode("}");
 
192
      CPPUNIT_FAIL("exception must be thrown.");
 
193
    } catch(RecoverableException& e) {
 
194
      // success
 
195
    }
 
196
  }
 
197
  {
 
198
    try {
 
199
      // object
 
200
      SharedHandle<ValueBase> r = json::decode("{\"\":");
 
201
      CPPUNIT_FAIL("exception must be thrown.");
 
202
    } catch(RecoverableException& e) {
 
203
      // success
 
204
    }
 
205
  }
 
206
  {
 
207
    try {
 
208
      // object
 
209
      SharedHandle<ValueBase> r = json::decode("{\"\":\"\",");
 
210
      CPPUNIT_FAIL("exception must be thrown.");
 
211
    } catch(RecoverableException& e) {
 
212
      // success
 
213
    }
 
214
  }
 
215
  {
 
216
    try {
 
217
      // array
 
218
      SharedHandle<ValueBase> r = json::decode("[");
 
219
      CPPUNIT_FAIL("exception must be thrown.");
 
220
    } catch(RecoverableException& e) {
 
221
      // success
 
222
    }
 
223
  }
 
224
  {
 
225
    try {
 
226
      // array
 
227
      SharedHandle<ValueBase> r = json::decode("]");
 
228
      CPPUNIT_FAIL("exception must be thrown.");
 
229
    } catch(RecoverableException& e) {
 
230
      // success
 
231
    }
 
232
  }
 
233
  {
 
234
    try {
 
235
      // array
 
236
      SharedHandle<ValueBase> r = json::decode("[\"\"");
 
237
      CPPUNIT_FAIL("exception must be thrown.");
 
238
    } catch(RecoverableException& e) {
 
239
      // success
 
240
    }
 
241
  }
 
242
  {
 
243
    try {
 
244
      // array
 
245
      SharedHandle<ValueBase> r = json::decode("[\"\",");
 
246
      CPPUNIT_FAIL("exception must be thrown.");
 
247
    } catch(RecoverableException& e) {
 
248
      // success
 
249
    }
 
250
  }
 
251
  {
 
252
    try {
 
253
      // string
 
254
      SharedHandle<ValueBase> r = json::decode("[\"foo]");
 
255
      CPPUNIT_FAIL("exception must be thrown.");
 
256
    } catch(RecoverableException& e) {
 
257
      // success
 
258
    }
 
259
  }
 
260
  {
 
261
    try {
 
262
      // string
 
263
      SharedHandle<ValueBase> r = json::decode("[\"\\u\"]");
 
264
      CPPUNIT_FAIL("exception must be thrown.");
 
265
    } catch(RecoverableException& e) {
 
266
      // success
 
267
    }
 
268
  }
 
269
  {
 
270
    try {
 
271
      // string
 
272
      SharedHandle<ValueBase> r = json::decode("[\"\\u");
 
273
      CPPUNIT_FAIL("exception must be thrown.");
 
274
    } catch(RecoverableException& e) {
 
275
      // success
 
276
    }
 
277
  }
 
278
  {
 
279
    try {
 
280
      // string
 
281
      SharedHandle<ValueBase> r = json::decode("[\"\\u000\"]");
 
282
      CPPUNIT_FAIL("exception must be thrown.");
 
283
    } catch(RecoverableException& e) {
 
284
      // success
 
285
    }
 
286
  }
 
287
  {
 
288
    try {
 
289
      // string
 
290
      SharedHandle<ValueBase> r = json::decode("[\"\\u000");
 
291
      CPPUNIT_FAIL("exception must be thrown.");
 
292
    } catch(RecoverableException& e) {
 
293
      // success
 
294
    }
 
295
  }
 
296
  {
 
297
    try {
 
298
      // string
 
299
      SharedHandle<ValueBase> r = json::decode("[\"\\uD852foo\"]");
 
300
      CPPUNIT_FAIL("exception must be thrown.");
 
301
    } catch(RecoverableException& e) {
 
302
      // success
 
303
    }
 
304
  }
 
305
  {
 
306
    try {
 
307
      // string
 
308
      SharedHandle<ValueBase> r = json::decode("[\"\\uD852");
 
309
      CPPUNIT_FAIL("exception must be thrown.");
 
310
    } catch(RecoverableException& e) {
 
311
      // success
 
312
    }
 
313
  }
 
314
  {
 
315
    try {
 
316
      // string
 
317
      SharedHandle<ValueBase> r = json::decode("[\"\\uD852\\u\"]");
 
318
      CPPUNIT_FAIL("exception must be thrown.");
 
319
    } catch(RecoverableException& e) {
 
320
      // success
 
321
    }
 
322
  }
 
323
  {
 
324
    try {
 
325
      // string
 
326
      SharedHandle<ValueBase> r = json::decode("[\"\\uD852\\u");
 
327
      CPPUNIT_FAIL("exception must be thrown.");
 
328
    } catch(RecoverableException& e) {
 
329
      // success
 
330
    }
 
331
  }
 
332
  {
 
333
    try {
 
334
      // string
 
335
      SharedHandle<ValueBase> r = json::decode("[\"\\uD852\\u0000\"]");
 
336
      CPPUNIT_FAIL("exception must be thrown.");
 
337
    } catch(RecoverableException& e) {
 
338
      // success
 
339
    }
 
340
  }
 
341
  {
 
342
    try {
 
343
      // string
 
344
      SharedHandle<ValueBase> r = json::decode("[\"\\uD852\\uDF62");
 
345
      CPPUNIT_FAIL("exception must be thrown.");
 
346
    } catch(RecoverableException& e) {
 
347
      // success
 
348
    }
 
349
  }
 
350
  {
 
351
    try {
 
352
      // object
 
353
      SharedHandle<ValueBase> r = json::decode("{:\"\"}");
 
354
      CPPUNIT_FAIL("exception must be thrown.");
 
355
    } catch(RecoverableException& e) {
 
356
      // success
 
357
    }
 
358
  }
 
359
  {
 
360
    try {
 
361
      // object
 
362
      SharedHandle<ValueBase> r = json::decode("{\"foo\":}");
 
363
      CPPUNIT_FAIL("exception must be thrown.");
 
364
    } catch(RecoverableException& e) {
 
365
      // success
 
366
    }
 
367
  }
 
368
  {
 
369
    try {
 
370
      // number
 
371
      SharedHandle<ValueBase> r = json::decode("{00}");
 
372
      CPPUNIT_FAIL("exception must be thrown.");
 
373
    } catch(RecoverableException& e) {
 
374
      // success
 
375
    }
 
376
  }
 
377
  {
 
378
    try {
 
379
      // number
 
380
      SharedHandle<ValueBase> r = json::decode("{1.}");
 
381
      CPPUNIT_FAIL("exception must be thrown.");
 
382
    } catch(RecoverableException& e) {
 
383
      // success
 
384
    }
 
385
  }
 
386
  {
 
387
    try {
 
388
      // number
 
389
      SharedHandle<ValueBase> r = json::decode("{1.1e}");
 
390
      CPPUNIT_FAIL("exception must be thrown.");
 
391
    } catch(RecoverableException& e) {
 
392
      // success
 
393
    }
 
394
  }
 
395
  {
 
396
    try {
 
397
      // bool
 
398
      SharedHandle<ValueBase> r = json::decode("{t");
 
399
      CPPUNIT_FAIL("exception must be thrown.");
 
400
    } catch(RecoverableException& e) {
 
401
      // success
 
402
    }
 
403
  }
 
404
}
 
405
 
 
406
void JsonTest::testEncode()
 
407
{
 
408
  {
 
409
    Dict dict;
 
410
    dict["name"] = String::g("aria2");
 
411
    dict["loc"] = Integer::g(80000);
 
412
    SharedHandle<List> files = List::g();
 
413
    files->append(String::g("aria2c"));
 
414
    dict["files"] = files;
 
415
    SharedHandle<Dict> attrs = Dict::g();
 
416
    attrs->put("license", String::g("GPL"));
 
417
    dict["attrs"] = attrs;
 
418
 
 
419
    CPPUNIT_ASSERT_EQUAL(std::string("{\"attrs\":{\"license\":\"GPL\"},"
 
420
                                     "\"files\":[\"aria2c\"],"
 
421
                                     "\"loc\":80000,"
 
422
                                     "\"name\":\"aria2\"}"),
 
423
                         json::encode(&dict));
 
424
  }
 
425
  {
 
426
    List list;
 
427
    list.append("\"\\/\b\f\n\r\t");
 
428
    CPPUNIT_ASSERT_EQUAL(std::string("[\"\\\"\\\\\\/\\b\\f\\n\\r\\t\"]"),
 
429
                         json::encode(&list));
 
430
  }
 
431
  {
 
432
    List list;
 
433
    std::string s;
 
434
    s += 0x1Fu;
 
435
    list.append(s);
 
436
    CPPUNIT_ASSERT_EQUAL(std::string("[\"\\u001F\"]"),
 
437
                         json::encode(&list));
 
438
  }
 
439
  {
 
440
    List list;
 
441
    list.append(Bool::gTrue());
 
442
    list.append(Bool::gFalse());
 
443
    list.append(Null::g());
 
444
    CPPUNIT_ASSERT_EQUAL(std::string("[true,false,null]"),
 
445
                         json::encode(&list));
 
446
  }
 
447
}
 
448
 
 
449
void JsonTest::testDecodeGetParams()
 
450
{
 
451
  {
 
452
    std::string param = util::percentEncode(Base64::encode("[1,2,3]"));
 
453
    std::string query = "?params=";
 
454
    query += param;
 
455
    query += '&';
 
456
    query += "method=sum&";
 
457
    query += "id=300&";
 
458
    query += "jsoncallback=cb";
 
459
    json::JsonGetParam gparam = json::decodeGetParams(query);
 
460
    CPPUNIT_ASSERT_EQUAL(std::string("{\"method\":\"sum\","
 
461
                                     "\"id\":\"300\","
 
462
                                     "\"params\":[1,2,3]}"),
 
463
                         gparam.request);
 
464
    CPPUNIT_ASSERT_EQUAL(std::string("cb"), gparam.callback);
 
465
  }
 
466
  {
 
467
    std::string query = "?params=";
 
468
    query += util::percentEncode(Base64::encode("[{}]"));
 
469
    query += '&';
 
470
    query += "jsoncallback=cb";
 
471
    json::JsonGetParam gparam = json::decodeGetParams(query);
 
472
    CPPUNIT_ASSERT_EQUAL(std::string("[{}]"), gparam.request);
 
473
    CPPUNIT_ASSERT_EQUAL(std::string("cb"), gparam.callback);
 
474
  }
 
475
  {
 
476
    std::string query = "?method=sum&id=300";
 
477
    json::JsonGetParam gparam = json::decodeGetParams(query);
 
478
    CPPUNIT_ASSERT_EQUAL(std::string("{\"method\":\"sum\","
 
479
                                     "\"id\":\"300\"}"),
 
480
                         gparam.request);
 
481
    CPPUNIT_ASSERT_EQUAL(std::string(), gparam.callback);
 
482
  }
 
483
}
 
484
 
 
485
} // namespace aria2