/deps/jansson/test/suites/api/test_unpack.c

https://gitlab.com/Ornim/obs-old · C · 381 lines · 271 code · 62 blank · 48 comment · 115 complexity · 3b80f4df31a22b03adc6723a49b3b3b5 MD5 · raw file

  1. /*
  2. * Copyright (c) 2009-2013 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}", "foo", "bar", "baz", 42);
  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. check_error("1 object item(s) left unpacked", "<validation>", 1, 10, 10);
  254. json_decref(j);
  255. j = json_pack("[i,{s:i,s:n},[i,i]]", 1, "foo", 2, "bar", 3, 4);
  256. if(json_unpack_ex(j, NULL, JSON_STRICT | JSON_VALIDATE_ONLY,
  257. "[i{sisn}[ii]]", "foo", "bar"))
  258. fail("json_unpack complex value with strict validation failed");
  259. json_decref(j);
  260. /* ! and * must be last */
  261. j = json_pack("[ii]", 1, 2);
  262. if(!json_unpack_ex(j, &error, 0, "[i!i]", &i1, &i2))
  263. fail("json_unpack failed to catch ! in the middle of an array");
  264. check_error("Expected ']' after '!', got 'i'", "<format>", 1, 4, 4);
  265. if(!json_unpack_ex(j, &error, 0, "[i*i]", &i1, &i2))
  266. fail("json_unpack failed to catch * in the middle of an array");
  267. check_error("Expected ']' after '*', got 'i'", "<format>", 1, 4, 4);
  268. json_decref(j);
  269. j = json_pack("{sssi}", "foo", "bar", "baz", 42);
  270. if(!json_unpack_ex(j, &error, 0, "{ss!si}", "foo", &s, "baz", &i1))
  271. fail("json_unpack failed to catch ! in the middle of an object");
  272. check_error("Expected '}' after '!', got 's'", "<format>", 1, 5, 5);
  273. if(!json_unpack_ex(j, &error, 0, "{ss*si}", "foo", &s, "baz", &i1))
  274. fail("json_unpack failed to catch ! in the middle of an object");
  275. check_error("Expected '}' after '*', got 's'", "<format>", 1, 5, 5);
  276. json_decref(j);
  277. /* Error in nested object */
  278. j = json_pack("{s{snsn}}", "foo", "bar", "baz");
  279. if(!json_unpack_ex(j, &error, 0, "{s{sn!}}", "foo", "bar"))
  280. fail("json_unpack nested object with strict validation failed");
  281. check_error("1 object item(s) left unpacked", "<validation>", 1, 7, 7);
  282. json_decref(j);
  283. /* Error in nested array */
  284. j = json_pack("[[ii]]", 1, 2);
  285. if(!json_unpack_ex(j, &error, 0, "[[i!]]", &i1))
  286. fail("json_unpack nested array with strict validation failed");
  287. check_error("1 array item(s) left unpacked", "<validation>", 1, 5, 5);
  288. json_decref(j);
  289. /* Optional values */
  290. j = json_object();
  291. i1 = 0;
  292. if(json_unpack(j, "{s?i}", "foo", &i1))
  293. fail("json_unpack failed for optional key");
  294. if(i1 != 0)
  295. fail("json_unpack unpacked an optional key");
  296. json_decref(j);
  297. i1 = 0;
  298. j = json_pack("{si}", "foo", 42);
  299. if(json_unpack(j, "{s?i}", "foo", &i1))
  300. fail("json_unpack failed for an optional value");
  301. if(i1 != 42)
  302. fail("json_unpack failed to unpack an optional value");
  303. json_decref(j);
  304. j = json_object();
  305. i1 = i2 = i3 = 0;
  306. if(json_unpack(j, "{s?[ii]s?{s{si}}}",
  307. "foo", &i1, &i2,
  308. "bar", "baz", "quux", &i3))
  309. fail("json_unpack failed for complex optional values");
  310. if(i1 != 0 || i2 != 0 || i3 != 0)
  311. fail("json_unpack unexpectedly unpacked something");
  312. json_decref(j);
  313. j = json_pack("{s{si}}", "foo", "bar", 42);
  314. if(json_unpack(j, "{s?{s?i}}", "foo", "bar", &i1))
  315. fail("json_unpack failed for complex optional values");
  316. if(i1 != 42)
  317. fail("json_unpack failed to unpack");
  318. json_decref(j);
  319. }