test_unpack.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406
  1. /*
  2. * Copyright (c) 2009-2016 Petri Lehtinen <petri@digip.org>
  3. * Copyright (c) 2010-2012 Graeme Smecher <graeme.smecher@mail.mcgill.ca>
  4. *
  5. * Jansson is free software; you can redistribute it and/or modify
  6. * it under the terms of the MIT license. See LICENSE for details.
  7. */
  8. #include <string.h>
  9. #include <jansson.h>
  10. #include <stdio.h>
  11. #include "util.h"
  12. static void run_tests()
  13. {
  14. json_t *j, *j2;
  15. int i1, i2, i3;
  16. json_int_t I1;
  17. int rv;
  18. size_t z;
  19. double f;
  20. char *s;
  21. json_error_t error;
  22. /*
  23. * Simple, valid json_pack cases
  24. */
  25. /* true */
  26. rv = json_unpack(json_true(), "b", &i1);
  27. if(rv || !i1)
  28. fail("json_unpack boolean failed");
  29. /* false */
  30. rv = json_unpack(json_false(), "b", &i1);
  31. if(rv || i1)
  32. fail("json_unpack boolean failed");
  33. /* null */
  34. if(json_unpack(json_null(), "n"))
  35. fail("json_unpack null failed");
  36. /* integer */
  37. j = json_integer(42);
  38. rv = json_unpack(j, "i", &i1);
  39. if(rv || i1 != 42)
  40. fail("json_unpack integer failed");
  41. json_decref(j);
  42. /* json_int_t */
  43. j = json_integer(5555555);
  44. rv = json_unpack(j, "I", &I1);
  45. if(rv || I1 != 5555555)
  46. fail("json_unpack json_int_t failed");
  47. json_decref(j);
  48. /* real */
  49. j = json_real(1.7);
  50. rv = json_unpack(j, "f", &f);
  51. if(rv || f != 1.7)
  52. fail("json_unpack real failed");
  53. json_decref(j);
  54. /* number */
  55. j = json_integer(12345);
  56. rv = json_unpack(j, "F", &f);
  57. if(rv || f != 12345.0)
  58. fail("json_unpack (real or) integer failed");
  59. json_decref(j);
  60. j = json_real(1.7);
  61. rv = json_unpack(j, "F", &f);
  62. if(rv || f != 1.7)
  63. fail("json_unpack real (or integer) failed");
  64. json_decref(j);
  65. /* string */
  66. j = json_string("foo");
  67. rv = json_unpack(j, "s", &s);
  68. if(rv || strcmp(s, "foo"))
  69. fail("json_unpack string failed");
  70. json_decref(j);
  71. /* string with length (size_t) */
  72. j = json_string("foo");
  73. rv = json_unpack(j, "s%", &s, &z);
  74. if(rv || strcmp(s, "foo") || z != 3)
  75. fail("json_unpack string with length (size_t) failed");
  76. json_decref(j);
  77. /* empty object */
  78. j = json_object();
  79. if(json_unpack(j, "{}"))
  80. fail("json_unpack empty object failed");
  81. json_decref(j);
  82. /* empty list */
  83. j = json_array();
  84. if(json_unpack(j, "[]"))
  85. fail("json_unpack empty list failed");
  86. json_decref(j);
  87. /* non-incref'd object */
  88. j = json_object();
  89. rv = json_unpack(j, "o", &j2);
  90. if(rv || j2 != j || j->refcount != 1)
  91. fail("json_unpack object failed");
  92. json_decref(j);
  93. /* incref'd object */
  94. j = json_object();
  95. rv = json_unpack(j, "O", &j2);
  96. if(rv || j2 != j || j->refcount != 2)
  97. fail("json_unpack object failed");
  98. json_decref(j);
  99. json_decref(j);
  100. /* simple object */
  101. j = json_pack("{s:i}", "foo", 42);
  102. rv = json_unpack(j, "{s:i}", "foo", &i1);
  103. if(rv || i1 != 42)
  104. fail("json_unpack simple object failed");
  105. json_decref(j);
  106. /* simple array */
  107. j = json_pack("[iii]", 1, 2, 3);
  108. rv = json_unpack(j, "[i,i,i]", &i1, &i2, &i3);
  109. if(rv || i1 != 1 || i2 != 2 || i3 != 3)
  110. fail("json_unpack simple array failed");
  111. json_decref(j);
  112. /* object with many items & strict checking */
  113. j = json_pack("{s:i, s:i, s:i}", "a", 1, "b", 2, "c", 3);
  114. rv = json_unpack(j, "{s:i, s:i, s:i}", "a", &i1, "b", &i2, "c", &i3);
  115. if(rv || i1 != 1 || i2 != 2 || i3 != 3)
  116. fail("json_unpack object with many items failed");
  117. json_decref(j);
  118. /*
  119. * Invalid cases
  120. */
  121. j = json_integer(42);
  122. if(!json_unpack_ex(j, &error, 0, "z"))
  123. fail("json_unpack succeeded with invalid format character");
  124. check_error("Unexpected format character 'z'", "<format>", 1, 1, 1);
  125. if(!json_unpack_ex(NULL, &error, 0, "[i]"))
  126. fail("json_unpack succeeded with NULL root");
  127. check_error("NULL root value", "<root>", -1, -1, 0);
  128. json_decref(j);
  129. /* mismatched open/close array/object */
  130. j = json_pack("[]");
  131. if(!json_unpack_ex(j, &error, 0, "[}"))
  132. fail("json_unpack failed to catch mismatched ']'");
  133. check_error("Unexpected format character '}'", "<format>", 1, 2, 2);
  134. json_decref(j);
  135. j = json_pack("{}");
  136. if(!json_unpack_ex(j, &error, 0, "{]"))
  137. fail("json_unpack failed to catch mismatched '}'");
  138. check_error("Expected format 's', got ']'", "<format>", 1, 2, 2);
  139. json_decref(j);
  140. /* missing close array */
  141. j = json_pack("[]");
  142. if(!json_unpack_ex(j, &error, 0, "["))
  143. fail("json_unpack failed to catch missing ']'");
  144. check_error("Unexpected end of format string", "<format>", 1, 2, 2);
  145. json_decref(j);
  146. /* missing close object */
  147. j = json_pack("{}");
  148. if(!json_unpack_ex(j, &error, 0, "{"))
  149. fail("json_unpack failed to catch missing '}'");
  150. check_error("Unexpected end of format string", "<format>", 1, 2, 2);
  151. json_decref(j);
  152. /* garbage after format string */
  153. j = json_pack("[i]", 42);
  154. if(!json_unpack_ex(j, &error, 0, "[i]a", &i1))
  155. fail("json_unpack failed to catch garbage after format string");
  156. check_error("Garbage after format string", "<format>", 1, 4, 4);
  157. json_decref(j);
  158. j = json_integer(12345);
  159. if(!json_unpack_ex(j, &error, 0, "ia", &i1))
  160. fail("json_unpack failed to catch garbage after format string");
  161. check_error("Garbage after format string", "<format>", 1, 2, 2);
  162. json_decref(j);
  163. /* NULL format string */
  164. j = json_pack("[]");
  165. if(!json_unpack_ex(j, &error, 0, NULL))
  166. fail("json_unpack failed to catch null format string");
  167. check_error("NULL or empty format string", "<format>", -1, -1, 0);
  168. json_decref(j);
  169. /* NULL string pointer */
  170. j = json_string("foobie");
  171. if(!json_unpack_ex(j, &error, 0, "s", NULL))
  172. fail("json_unpack failed to catch null string pointer");
  173. check_error("NULL string argument", "<args>", 1, 1, 1);
  174. json_decref(j);
  175. /* invalid types */
  176. j = json_integer(42);
  177. j2 = json_string("foo");
  178. if(!json_unpack_ex(j, &error, 0, "s"))
  179. fail("json_unpack failed to catch invalid type");
  180. check_error("Expected string, got integer", "<validation>", 1, 1, 1);
  181. if(!json_unpack_ex(j, &error, 0, "n"))
  182. fail("json_unpack failed to catch invalid type");
  183. check_error("Expected null, got integer", "<validation>", 1, 1, 1);
  184. if(!json_unpack_ex(j, &error, 0, "b"))
  185. fail("json_unpack failed to catch invalid type");
  186. check_error("Expected true or false, got integer", "<validation>", 1, 1, 1);
  187. if(!json_unpack_ex(j2, &error, 0, "i"))
  188. fail("json_unpack failed to catch invalid type");
  189. check_error("Expected integer, got string", "<validation>", 1, 1, 1);
  190. if(!json_unpack_ex(j2, &error, 0, "I"))
  191. fail("json_unpack failed to catch invalid type");
  192. check_error("Expected integer, got string", "<validation>", 1, 1, 1);
  193. if(!json_unpack_ex(j, &error, 0, "f"))
  194. fail("json_unpack failed to catch invalid type");
  195. check_error("Expected real, got integer", "<validation>", 1, 1, 1);
  196. if(!json_unpack_ex(j2, &error, 0, "F"))
  197. fail("json_unpack failed to catch invalid type");
  198. check_error("Expected real or integer, got string", "<validation>", 1, 1, 1);
  199. if(!json_unpack_ex(j, &error, 0, "[i]"))
  200. fail("json_unpack failed to catch invalid type");
  201. check_error("Expected array, got integer", "<validation>", 1, 1, 1);
  202. if(!json_unpack_ex(j, &error, 0, "{si}", "foo"))
  203. fail("json_unpack failed to catch invalid type");
  204. check_error("Expected object, got integer", "<validation>", 1, 1, 1);
  205. json_decref(j);
  206. json_decref(j2);
  207. /* Array index out of range */
  208. j = json_pack("[i]", 1);
  209. if(!json_unpack_ex(j, &error, 0, "[ii]", &i1, &i2))
  210. fail("json_unpack failed to catch index out of array bounds");
  211. check_error("Array index 1 out of range", "<validation>", 1, 3, 3);
  212. json_decref(j);
  213. /* NULL object key */
  214. j = json_pack("{si}", "foo", 42);
  215. if(!json_unpack_ex(j, &error, 0, "{si}", NULL, &i1))
  216. fail("json_unpack failed to catch null string pointer");
  217. check_error("NULL object key", "<args>", 1, 2, 2);
  218. json_decref(j);
  219. /* Object key not found */
  220. j = json_pack("{si}", "foo", 42);
  221. if(!json_unpack_ex(j, &error, 0, "{si}", "baz", &i1))
  222. fail("json_unpack failed to catch null string pointer");
  223. check_error("Object item not found: baz", "<validation>", 1, 3, 3);
  224. json_decref(j);
  225. /*
  226. * Strict validation
  227. */
  228. j = json_pack("[iii]", 1, 2, 3);
  229. rv = json_unpack(j, "[iii!]", &i1, &i2, &i3);
  230. if(rv || i1 != 1 || i2 != 2 || i3 != 3)
  231. fail("json_unpack array with strict validation failed");
  232. json_decref(j);
  233. j = json_pack("[iii]", 1, 2, 3);
  234. if(!json_unpack_ex(j, &error, 0, "[ii!]", &i1, &i2))
  235. fail("json_unpack array with strict validation failed");
  236. check_error("1 array item(s) left unpacked", "<validation>", 1, 5, 5);
  237. json_decref(j);
  238. /* Like above, but with JSON_STRICT instead of '!' format */
  239. j = json_pack("[iii]", 1, 2, 3);
  240. if(!json_unpack_ex(j, &error, JSON_STRICT, "[ii]", &i1, &i2))
  241. fail("json_unpack array with strict validation failed");
  242. check_error("1 array item(s) left unpacked", "<validation>", 1, 4, 4);
  243. json_decref(j);
  244. j = json_pack("{s:s, s:i}", "foo", "bar", "baz", 42);
  245. rv = json_unpack(j, "{sssi!}", "foo", &s, "baz", &i1);
  246. if(rv || strcmp(s, "bar") != 0 || i1 != 42)
  247. fail("json_unpack object with strict validation failed");
  248. json_decref(j);
  249. /* Unpack the same item twice */
  250. j = json_pack("{s:s, s:i, s:b}", "foo", "bar", "baz", 42, "quux", 1);
  251. if(!json_unpack_ex(j, &error, 0, "{s:s,s:s!}", "foo", &s, "foo", &s))
  252. fail("json_unpack object with strict validation failed");
  253. {
  254. const char *possible_errors[] = {
  255. "2 object item(s) left unpacked: baz, quux",
  256. "2 object item(s) left unpacked: quux, baz"
  257. };
  258. check_errors(possible_errors, 2, "<validation>", 1, 10, 10);
  259. }
  260. json_decref(j);
  261. j = json_pack("[i,{s:i,s:n},[i,i]]", 1, "foo", 2, "bar", 3, 4);
  262. if(json_unpack_ex(j, NULL, JSON_STRICT | JSON_VALIDATE_ONLY,
  263. "[i{sisn}[ii]]", "foo", "bar"))
  264. fail("json_unpack complex value with strict validation failed");
  265. json_decref(j);
  266. /* ! and * must be last */
  267. j = json_pack("[ii]", 1, 2);
  268. if(!json_unpack_ex(j, &error, 0, "[i!i]", &i1, &i2))
  269. fail("json_unpack failed to catch ! in the middle of an array");
  270. check_error("Expected ']' after '!', got 'i'", "<format>", 1, 4, 4);
  271. if(!json_unpack_ex(j, &error, 0, "[i*i]", &i1, &i2))
  272. fail("json_unpack failed to catch * in the middle of an array");
  273. check_error("Expected ']' after '*', got 'i'", "<format>", 1, 4, 4);
  274. json_decref(j);
  275. j = json_pack("{sssi}", "foo", "bar", "baz", 42);
  276. if(!json_unpack_ex(j, &error, 0, "{ss!si}", "foo", &s, "baz", &i1))
  277. fail("json_unpack failed to catch ! in the middle of an object");
  278. check_error("Expected '}' after '!', got 's'", "<format>", 1, 5, 5);
  279. if(!json_unpack_ex(j, &error, 0, "{ss*si}", "foo", &s, "baz", &i1))
  280. fail("json_unpack failed to catch ! in the middle of an object");
  281. check_error("Expected '}' after '*', got 's'", "<format>", 1, 5, 5);
  282. json_decref(j);
  283. /* Error in nested object */
  284. j = json_pack("{s{snsn}}", "foo", "bar", "baz");
  285. if(!json_unpack_ex(j, &error, 0, "{s{sn!}}", "foo", "bar"))
  286. fail("json_unpack nested object with strict validation failed");
  287. check_error("1 object item(s) left unpacked: baz", "<validation>", 1, 7, 7);
  288. json_decref(j);
  289. /* Error in nested array */
  290. j = json_pack("[[ii]]", 1, 2);
  291. if(!json_unpack_ex(j, &error, 0, "[[i!]]", &i1))
  292. fail("json_unpack nested array with strict validation failed");
  293. check_error("1 array item(s) left unpacked", "<validation>", 1, 5, 5);
  294. json_decref(j);
  295. /* Optional values */
  296. j = json_object();
  297. i1 = 0;
  298. if(json_unpack(j, "{s?i}", "foo", &i1))
  299. fail("json_unpack failed for optional key");
  300. if(i1 != 0)
  301. fail("json_unpack unpacked an optional key");
  302. json_decref(j);
  303. i1 = 0;
  304. j = json_pack("{si}", "foo", 42);
  305. if(json_unpack(j, "{s?i}", "foo", &i1))
  306. fail("json_unpack failed for an optional value");
  307. if(i1 != 42)
  308. fail("json_unpack failed to unpack an optional value");
  309. json_decref(j);
  310. j = json_object();
  311. i1 = i2 = i3 = 0;
  312. if(json_unpack(j, "{s?[ii]s?{s{si}}}",
  313. "foo", &i1, &i2,
  314. "bar", "baz", "quux", &i3))
  315. fail("json_unpack failed for complex optional values");
  316. if(i1 != 0 || i2 != 0 || i3 != 0)
  317. fail("json_unpack unexpectedly unpacked something");
  318. json_decref(j);
  319. j = json_pack("{s{si}}", "foo", "bar", 42);
  320. if(json_unpack(j, "{s?{s?i}}", "foo", "bar", &i1))
  321. fail("json_unpack failed for complex optional values");
  322. if(i1 != 42)
  323. fail("json_unpack failed to unpack");
  324. json_decref(j);
  325. /* Combine ? and ! */
  326. j = json_pack("{si}", "foo", 42);
  327. i1 = i2 = 0;
  328. if(json_unpack(j, "{sis?i!}", "foo", &i1, "bar", &i2))
  329. fail("json_unpack failed for optional values with strict mode");
  330. if(i1 != 42)
  331. fail("json_unpack failed to unpack");
  332. if(i2 != 0)
  333. fail("json_unpack failed to unpack");
  334. json_decref(j);
  335. /* But don't compensate a missing key with an optional one. */
  336. j = json_pack("{sisi}", "foo", 42, "baz", 43);
  337. i1 = i2 = i3 = 0;
  338. if(!json_unpack_ex(j, &error, 0, "{sis?i!}", "foo", &i1, "bar", &i2))
  339. fail("json_unpack failed for optional values with strict mode and compensation");
  340. check_error("1 object item(s) left unpacked: baz", "<validation>", 1, 8, 8);
  341. json_decref(j);
  342. }