3
#include <cppunit/extensions/HelperMacros.h>
5
#include "RecoverableException.h"
12
class JsonTest:public CppUnit::TestFixture {
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();
24
void testDecode_error();
26
void testDecodeGetParams();
29
CPPUNIT_TEST_SUITE_REGISTRATION( JsonTest );
31
void JsonTest::testDecode()
35
SharedHandle<ValueBase> r = json::decode("{}");
36
const Dict* dict = asDict(r);
41
SharedHandle<ValueBase> r = json::decode("{ }");
42
const Dict* dict = asDict(r);
47
SharedHandle<ValueBase> r = json::decode("[]");
48
const List* list = asList(r);
53
SharedHandle<ValueBase> r = json::decode("[ ]");
54
const List* list = asList(r);
59
SharedHandle<ValueBase> r = json::decode("[\"\"]");
60
const List* list = asList(r);
62
const String* s = asString(list->get(0));
63
CPPUNIT_ASSERT_EQUAL(std::string(), s->s());
67
SharedHandle<ValueBase> r = json::decode("[\"foobar\"]");
68
const List* list = asList(r);
70
const String* s = asString(list->get(0));
71
CPPUNIT_ASSERT_EQUAL(std::string("foobar"), s->s());
75
SharedHandle<ValueBase> r = json::decode("[\"\\\\foo\\\"\\\"bar\"]");
76
const List* list = asList(r);
78
const String* s = asString(list->get(0));
79
CPPUNIT_ASSERT_EQUAL(std::string("\\foo\"\"bar"), s->s());
83
SharedHandle<ValueBase> r = json::decode("[\"foo\\\"\"]");
84
const List* list = asList(r);
86
const String* s = asString(list->get(0));
87
CPPUNIT_ASSERT_EQUAL(std::string("foo\""), s->s());
90
// string: utf-8 1 to 3 bytes.
91
SharedHandle<ValueBase> r = json::decode("[\"\\u0024\\u00A2\\u20AC\"]");
92
const List* list = asList(r);
94
const String* s = asString(list->get(0));
95
CPPUNIT_ASSERT_EQUAL(std::string("$¢€"), s->s());
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());
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));
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());
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());
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());
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);
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());
177
void JsonTest::testDecode_error()
182
SharedHandle<ValueBase> r = json::decode("{");
183
CPPUNIT_FAIL("exception must be thrown.");
184
} catch(RecoverableException& e) {
191
SharedHandle<ValueBase> r = json::decode("}");
192
CPPUNIT_FAIL("exception must be thrown.");
193
} catch(RecoverableException& e) {
200
SharedHandle<ValueBase> r = json::decode("{\"\":");
201
CPPUNIT_FAIL("exception must be thrown.");
202
} catch(RecoverableException& e) {
209
SharedHandle<ValueBase> r = json::decode("{\"\":\"\",");
210
CPPUNIT_FAIL("exception must be thrown.");
211
} catch(RecoverableException& e) {
218
SharedHandle<ValueBase> r = json::decode("[");
219
CPPUNIT_FAIL("exception must be thrown.");
220
} catch(RecoverableException& e) {
227
SharedHandle<ValueBase> r = json::decode("]");
228
CPPUNIT_FAIL("exception must be thrown.");
229
} catch(RecoverableException& e) {
236
SharedHandle<ValueBase> r = json::decode("[\"\"");
237
CPPUNIT_FAIL("exception must be thrown.");
238
} catch(RecoverableException& e) {
245
SharedHandle<ValueBase> r = json::decode("[\"\",");
246
CPPUNIT_FAIL("exception must be thrown.");
247
} catch(RecoverableException& e) {
254
SharedHandle<ValueBase> r = json::decode("[\"foo]");
255
CPPUNIT_FAIL("exception must be thrown.");
256
} catch(RecoverableException& e) {
263
SharedHandle<ValueBase> r = json::decode("[\"\\u\"]");
264
CPPUNIT_FAIL("exception must be thrown.");
265
} catch(RecoverableException& e) {
272
SharedHandle<ValueBase> r = json::decode("[\"\\u");
273
CPPUNIT_FAIL("exception must be thrown.");
274
} catch(RecoverableException& e) {
281
SharedHandle<ValueBase> r = json::decode("[\"\\u000\"]");
282
CPPUNIT_FAIL("exception must be thrown.");
283
} catch(RecoverableException& e) {
290
SharedHandle<ValueBase> r = json::decode("[\"\\u000");
291
CPPUNIT_FAIL("exception must be thrown.");
292
} catch(RecoverableException& e) {
299
SharedHandle<ValueBase> r = json::decode("[\"\\uD852foo\"]");
300
CPPUNIT_FAIL("exception must be thrown.");
301
} catch(RecoverableException& e) {
308
SharedHandle<ValueBase> r = json::decode("[\"\\uD852");
309
CPPUNIT_FAIL("exception must be thrown.");
310
} catch(RecoverableException& e) {
317
SharedHandle<ValueBase> r = json::decode("[\"\\uD852\\u\"]");
318
CPPUNIT_FAIL("exception must be thrown.");
319
} catch(RecoverableException& e) {
326
SharedHandle<ValueBase> r = json::decode("[\"\\uD852\\u");
327
CPPUNIT_FAIL("exception must be thrown.");
328
} catch(RecoverableException& e) {
335
SharedHandle<ValueBase> r = json::decode("[\"\\uD852\\u0000\"]");
336
CPPUNIT_FAIL("exception must be thrown.");
337
} catch(RecoverableException& e) {
344
SharedHandle<ValueBase> r = json::decode("[\"\\uD852\\uDF62");
345
CPPUNIT_FAIL("exception must be thrown.");
346
} catch(RecoverableException& e) {
353
SharedHandle<ValueBase> r = json::decode("{:\"\"}");
354
CPPUNIT_FAIL("exception must be thrown.");
355
} catch(RecoverableException& e) {
362
SharedHandle<ValueBase> r = json::decode("{\"foo\":}");
363
CPPUNIT_FAIL("exception must be thrown.");
364
} catch(RecoverableException& e) {
371
SharedHandle<ValueBase> r = json::decode("{00}");
372
CPPUNIT_FAIL("exception must be thrown.");
373
} catch(RecoverableException& e) {
380
SharedHandle<ValueBase> r = json::decode("{1.}");
381
CPPUNIT_FAIL("exception must be thrown.");
382
} catch(RecoverableException& e) {
389
SharedHandle<ValueBase> r = json::decode("{1.1e}");
390
CPPUNIT_FAIL("exception must be thrown.");
391
} catch(RecoverableException& e) {
398
SharedHandle<ValueBase> r = json::decode("{t");
399
CPPUNIT_FAIL("exception must be thrown.");
400
} catch(RecoverableException& e) {
406
void JsonTest::testEncode()
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;
419
CPPUNIT_ASSERT_EQUAL(std::string("{\"attrs\":{\"license\":\"GPL\"},"
420
"\"files\":[\"aria2c\"],"
422
"\"name\":\"aria2\"}"),
423
json::encode(&dict));
427
list.append("\"\\/\b\f\n\r\t");
428
CPPUNIT_ASSERT_EQUAL(std::string("[\"\\\"\\\\\\/\\b\\f\\n\\r\\t\"]"),
429
json::encode(&list));
436
CPPUNIT_ASSERT_EQUAL(std::string("[\"\\u001F\"]"),
437
json::encode(&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));
449
void JsonTest::testDecodeGetParams()
452
std::string param = util::percentEncode(Base64::encode("[1,2,3]"));
453
std::string query = "?params=";
456
query += "method=sum&";
458
query += "jsoncallback=cb";
459
json::JsonGetParam gparam = json::decodeGetParams(query);
460
CPPUNIT_ASSERT_EQUAL(std::string("{\"method\":\"sum\","
462
"\"params\":[1,2,3]}"),
464
CPPUNIT_ASSERT_EQUAL(std::string("cb"), gparam.callback);
467
std::string query = "?params=";
468
query += util::percentEncode(Base64::encode("[{}]"));
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);
476
std::string query = "?method=sum&id=300";
477
json::JsonGetParam gparam = json::decodeGetParams(query);
478
CPPUNIT_ASSERT_EQUAL(std::string("{\"method\":\"sum\","
481
CPPUNIT_ASSERT_EQUAL(std::string(), gparam.callback);