JsonTest.cc 11 KB

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