JsonTest.cc 14 KB

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