/3rdparty/jansson-2.1/test/suites/api/test_unpack.c

https://github.com/ezzymny/moai-beta · C · 341 lines · 237 code · 58 blank · 46 comment · 93 complexity · 60110dfd30bcdf8319ce800d6cf86a82 MD5 · raw file

  1. /*
  2. * Copyright (c) 2009-2011 Petri Lehtinen <petri@digip.org>
  3. * Copyright (c) 2010-2011 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. int main()
  13. {
  14. json_t *j, *j2;
  15. int i1, i2, i3;
  16. json_int_t I1;
  17. int rv;
  18. double f;
  19. char *s;
  20. json_error_t error;
  21. /*
  22. * Simple, valid json_pack cases
  23. */
  24. /* true */
  25. rv = json_unpack(json_true(), "b", &i1);
  26. if(rv || !i1)
  27. fail("json_unpack boolean failed");
  28. /* false */
  29. rv = json_unpack(json_false(), "b", &i1);
  30. if(rv || i1)
  31. fail("json_unpack boolean failed");
  32. /* null */
  33. if(json_unpack(json_null(), "n"))
  34. fail("json_unpack null failed");
  35. /* integer */
  36. j = json_integer(42);
  37. rv = json_unpack(j, "i", &i1);
  38. if(rv || i1 != 42)
  39. fail("json_unpack integer failed");
  40. json_decref(j);
  41. /* json_int_t */
  42. j = json_integer(5555555);
  43. rv = json_unpack(j, "I", &I1);
  44. if(rv || I1 != 5555555)
  45. fail("json_unpack json_int_t failed");
  46. json_decref(j);
  47. /* real */
  48. j = json_real(1.7);
  49. rv = json_unpack(j, "f", &f);
  50. if(rv || f != 1.7)
  51. fail("json_unpack real failed");
  52. json_decref(j);
  53. /* number */
  54. j = json_integer(12345);
  55. rv = json_unpack(j, "F", &f);
  56. if(rv || f != 12345.0)
  57. fail("json_unpack (real or) integer failed");
  58. json_decref(j);
  59. j = json_real(1.7);
  60. rv = json_unpack(j, "F", &f);
  61. if(rv || f != 1.7)
  62. fail("json_unpack real (or integer) failed");
  63. json_decref(j);
  64. /* string */
  65. j = json_string("foo");
  66. rv = json_unpack(j, "s", &s);
  67. if(rv || strcmp(s, "foo"))
  68. fail("json_unpack string failed");
  69. json_decref(j);
  70. /* empty object */
  71. j = json_object();
  72. if(json_unpack(j, "{}"))
  73. fail("json_unpack empty object failed");
  74. json_decref(j);
  75. /* empty list */
  76. j = json_array();
  77. if(json_unpack(j, "[]"))
  78. fail("json_unpack empty list failed");
  79. json_decref(j);
  80. /* non-incref'd object */
  81. j = json_object();
  82. rv = json_unpack(j, "o", &j2);
  83. if(j2 != j || j->refcount != 1)
  84. fail("json_unpack object failed");
  85. json_decref(j);
  86. /* incref'd object */
  87. j = json_object();
  88. rv = json_unpack(j, "O", &j2);
  89. if(j2 != j || j->refcount != 2)
  90. fail("json_unpack object failed");
  91. json_decref(j);
  92. json_decref(j);
  93. /* simple object */
  94. j = json_pack("{s:i}", "foo", 42);
  95. rv = json_unpack(j, "{s:i}", "foo", &i1);
  96. if(rv || i1 != 42)
  97. fail("json_unpack simple object failed");
  98. json_decref(j);
  99. /* simple array */
  100. j = json_pack("[iii]", 1, 2, 3);
  101. rv = json_unpack(j, "[i,i,i]", &i1, &i2, &i3);
  102. if(rv || i1 != 1 || i2 != 2 || i3 != 3)
  103. fail("json_unpack simple array failed");
  104. json_decref(j);
  105. /* object with many items & strict checking */
  106. j = json_pack("{s:i, s:i, s:i}", "a", 1, "b", 2, "c", 3);
  107. rv = json_unpack(j, "{s:i, s:i, s:i}", "a", &i1, "b", &i2, "c", &i3);
  108. if(rv || i1 != 1 || i2 != 2 || i3 != 3)
  109. fail("json_unpack object with many items failed");
  110. json_decref(j);
  111. /*
  112. * Invalid cases
  113. */
  114. j = json_integer(42);
  115. if(!json_unpack_ex(j, &error, 0, "z"))
  116. fail("json_unpack succeeded with invalid format character");
  117. check_error("Unexpected format character 'z'", "<format>", 1, 1, 1);
  118. if(!json_unpack_ex(NULL, &error, 0, "[i]"))
  119. fail("json_unpack succeeded with NULL root");
  120. check_error("NULL root value", "<root>", -1, -1, 0);
  121. json_decref(j);
  122. /* mismatched open/close array/object */
  123. j = json_pack("[]");
  124. if(!json_unpack_ex(j, &error, 0, "[}"))
  125. fail("json_unpack failed to catch mismatched ']'");
  126. check_error("Unexpected format character '}'", "<format>", 1, 2, 2);
  127. json_decref(j);
  128. j = json_pack("{}");
  129. if(!json_unpack_ex(j, &error, 0, "{]"))
  130. fail("json_unpack failed to catch mismatched '}'");
  131. check_error("Expected format 's', got ']'", "<format>", 1, 2, 2);
  132. json_decref(j);
  133. /* missing close array */
  134. j = json_pack("[]");
  135. if(!json_unpack_ex(j, &error, 0, "["))
  136. fail("json_unpack failed to catch missing ']'");
  137. check_error("Unexpected end of format string", "<format>", 1, 2, 2);
  138. json_decref(j);
  139. /* missing close object */
  140. j = json_pack("{}");
  141. if(!json_unpack_ex(j, &error, 0, "{"))
  142. fail("json_unpack failed to catch missing '}'");
  143. check_error("Unexpected end of format string", "<format>", 1, 2, 2);
  144. json_decref(j);
  145. /* garbage after format string */
  146. j = json_pack("[i]", 42);
  147. if(!json_unpack_ex(j, &error, 0, "[i]a", &i1))
  148. fail("json_unpack failed to catch garbage after format string");
  149. check_error("Garbage after format string", "<format>", 1, 4, 4);
  150. json_decref(j);
  151. j = json_integer(12345);
  152. if(!json_unpack_ex(j, &error, 0, "ia", &i1))
  153. fail("json_unpack failed to catch garbage after format string");
  154. check_error("Garbage after format string", "<format>", 1, 2, 2);
  155. json_decref(j);
  156. /* NULL format string */
  157. j = json_pack("[]");
  158. if(!json_unpack_ex(j, &error, 0, NULL))
  159. fail("json_unpack failed to catch null format string");
  160. check_error("NULL or empty format string", "<format>", -1, -1, 0);
  161. json_decref(j);
  162. /* NULL string pointer */
  163. j = json_string("foobie");
  164. if(!json_unpack_ex(j, &error, 0, "s", NULL))
  165. fail("json_unpack failed to catch null string pointer");
  166. check_error("NULL string argument", "<args>", 1, 1, 1);
  167. json_decref(j);
  168. /* invalid types */
  169. j = json_integer(42);
  170. j2 = json_string("foo");
  171. if(!json_unpack_ex(j, &error, 0, "s"))
  172. fail("json_unpack failed to catch invalid type");
  173. check_error("Expected string, got integer", "<validation>", 1, 1, 1);
  174. if(!json_unpack_ex(j, &error, 0, "n"))
  175. fail("json_unpack failed to catch invalid type");
  176. check_error("Expected null, got integer", "<validation>", 1, 1, 1);
  177. if(!json_unpack_ex(j, &error, 0, "b"))
  178. fail("json_unpack failed to catch invalid type");
  179. check_error("Expected true or false, got integer", "<validation>", 1, 1, 1);
  180. if(!json_unpack_ex(j2, &error, 0, "i"))
  181. fail("json_unpack failed to catch invalid type");
  182. check_error("Expected integer, got string", "<validation>", 1, 1, 1);
  183. if(!json_unpack_ex(j2, &error, 0, "I"))
  184. fail("json_unpack failed to catch invalid type");
  185. check_error("Expected integer, got string", "<validation>", 1, 1, 1);
  186. if(!json_unpack_ex(j, &error, 0, "f"))
  187. fail("json_unpack failed to catch invalid type");
  188. check_error("Expected real, got integer", "<validation>", 1, 1, 1);
  189. if(!json_unpack_ex(j2, &error, 0, "F"))
  190. fail("json_unpack failed to catch invalid type");
  191. check_error("Expected real or integer, got string", "<validation>", 1, 1, 1);
  192. if(!json_unpack_ex(j, &error, 0, "[i]"))
  193. fail("json_unpack failed to catch invalid type");
  194. check_error("Expected array, got integer", "<validation>", 1, 1, 1);
  195. if(!json_unpack_ex(j, &error, 0, "{si}", "foo"))
  196. fail("json_unpack failed to catch invalid type");
  197. check_error("Expected object, got integer", "<validation>", 1, 1, 1);
  198. json_decref(j);
  199. json_decref(j2);
  200. /* Array index out of range */
  201. j = json_pack("[i]", 1);
  202. if(!json_unpack_ex(j, &error, 0, "[ii]", &i1, &i2))
  203. fail("json_unpack failed to catch index out of array bounds");
  204. check_error("Array index 1 out of range", "<validation>", 1, 3, 3);
  205. json_decref(j);
  206. /* NULL object key */
  207. j = json_pack("{si}", "foo", 42);
  208. if(!json_unpack_ex(j, &error, 0, "{si}", NULL, &i1))
  209. fail("json_unpack failed to catch null string pointer");
  210. check_error("NULL object key", "<args>", 1, 2, 2);
  211. json_decref(j);
  212. /* Object key not found */
  213. j = json_pack("{si}", "foo", 42);
  214. if(!json_unpack_ex(j, &error, 0, "{si}", "baz", &i1))
  215. fail("json_unpack failed to catch null string pointer");
  216. check_error("Object item not found: baz", "<validation>", 1, 3, 3);
  217. json_decref(j);
  218. /*
  219. * Strict validation
  220. */
  221. j = json_pack("[iii]", 1, 2, 3);
  222. rv = json_unpack(j, "[iii!]", &i1, &i2, &i3);
  223. if(rv || i1 != 1 || i2 != 2 || i3 != 3)
  224. fail("json_unpack array with strict validation failed");
  225. json_decref(j);
  226. j = json_pack("[iii]", 1, 2, 3);
  227. if(!json_unpack_ex(j, &error, 0, "[ii!]", &i1, &i2))
  228. fail("json_unpack array with strict validation failed");
  229. check_error("1 array item(s) left unpacked", "<validation>", 1, 5, 5);
  230. json_decref(j);
  231. /* Like above, but with JSON_STRICT instead of '!' format */
  232. j = json_pack("[iii]", 1, 2, 3);
  233. if(!json_unpack_ex(j, &error, JSON_STRICT, "[ii]", &i1, &i2))
  234. fail("json_unpack array with strict validation failed");
  235. check_error("1 array item(s) left unpacked", "<validation>", 1, 4, 4);
  236. json_decref(j);
  237. j = json_pack("{s:s, s:i}", "foo", "bar", "baz", 42);
  238. rv = json_unpack(j, "{sssi!}", "foo", &s, "baz", &i1);
  239. if(rv || strcmp(s, "bar") != 0 || i1 != 42)
  240. fail("json_unpack object with strict validation failed");
  241. json_decref(j);
  242. /* Unpack the same item twice */
  243. j = json_pack("{s:s, s:i}", "foo", "bar", "baz", 42);
  244. if(!json_unpack_ex(j, &error, 0, "{s:s,s:s!}", "foo", &s, "foo", &s))
  245. fail("json_unpack object with strict validation failed");
  246. check_error("1 object item(s) left unpacked", "<validation>", 1, 10, 10);
  247. json_decref(j);
  248. j = json_pack("[i,{s:i,s:n},[i,i]]", 1, "foo", 2, "bar", 3, 4);
  249. if(json_unpack_ex(j, NULL, JSON_STRICT | JSON_VALIDATE_ONLY,
  250. "[i{sisn}[ii]]", "foo", "bar"))
  251. fail("json_unpack complex value with strict validation failed");
  252. json_decref(j);
  253. /* ! and * must be last */
  254. j = json_pack("[ii]", 1, 2);
  255. if(!json_unpack_ex(j, &error, 0, "[i!i]", &i1, &i2))
  256. fail("json_unpack failed to catch ! in the middle of an array");
  257. check_error("Expected ']' after '!', got 'i'", "<format>", 1, 4, 4);
  258. if(!json_unpack_ex(j, &error, 0, "[i*i]", &i1, &i2))
  259. fail("json_unpack failed to catch * in the middle of an array");
  260. check_error("Expected ']' after '*', got 'i'", "<format>", 1, 4, 4);
  261. json_decref(j);
  262. j = json_pack("{sssi}", "foo", "bar", "baz", 42);
  263. if(!json_unpack_ex(j, &error, 0, "{ss!si}", "foo", &s, "baz", &i1))
  264. fail("json_unpack failed to catch ! in the middle of an object");
  265. check_error("Expected '}' after '!', got 's'", "<format>", 1, 5, 5);
  266. if(!json_unpack_ex(j, &error, 0, "{ss*si}", "foo", &s, "baz", &i1))
  267. fail("json_unpack failed to catch ! in the middle of an object");
  268. check_error("Expected '}' after '*', got 's'", "<format>", 1, 5, 5);
  269. json_decref(j);
  270. /* Error in nested object */
  271. j = json_pack("{s{snsn}}", "foo", "bar", "baz");
  272. if(!json_unpack_ex(j, &error, 0, "{s{sn!}}", "foo", "bar"))
  273. fail("json_unpack nested object with strict validation failed");
  274. check_error("1 object item(s) left unpacked", "<validation>", 1, 7, 7);
  275. json_decref(j);
  276. /* Error in nested array */
  277. j = json_pack("[[ii]]", 1, 2);
  278. if(!json_unpack_ex(j, &error, 0, "[[i!]]", &i1))
  279. fail("json_unpack nested array with strict validation failed");
  280. check_error("1 array item(s) left unpacked", "<validation>", 1, 5, 5);
  281. json_decref(j);
  282. return 0;
  283. }