test_pack.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  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. #ifdef HAVE_CONFIG_H
  9. #include <jansson_private_config.h>
  10. #endif
  11. #include <jansson_config.h>
  12. #include <string.h>
  13. #include <jansson.h>
  14. #include <stdio.h>
  15. #include "util.h"
  16. static void run_tests()
  17. {
  18. json_t *value;
  19. int i;
  20. char buffer[4] = {'t', 'e', 's', 't'};
  21. json_error_t error;
  22. /*
  23. * Simple, valid json_pack cases
  24. */
  25. /* true */
  26. value = json_pack("b", 1);
  27. if(!json_is_true(value))
  28. fail("json_pack boolean failed");
  29. if(value->refcount != (size_t)-1)
  30. fail("json_pack boolean refcount failed");
  31. json_decref(value);
  32. /* false */
  33. value = json_pack("b", 0);
  34. if(!json_is_false(value))
  35. fail("json_pack boolean failed");
  36. if(value->refcount != (size_t)-1)
  37. fail("json_pack boolean refcount failed");
  38. json_decref(value);
  39. /* null */
  40. value = json_pack("n");
  41. if(!json_is_null(value))
  42. fail("json_pack null failed");
  43. if(value->refcount != (size_t)-1)
  44. fail("json_pack null refcount failed");
  45. json_decref(value);
  46. /* integer */
  47. value = json_pack("i", 1);
  48. if(!json_is_integer(value) || json_integer_value(value) != 1)
  49. fail("json_pack integer failed");
  50. if(value->refcount != (size_t)1)
  51. fail("json_pack integer refcount failed");
  52. json_decref(value);
  53. /* integer from json_int_t */
  54. value = json_pack("I", (json_int_t)555555);
  55. if(!json_is_integer(value) || json_integer_value(value) != 555555)
  56. fail("json_pack json_int_t failed");
  57. if(value->refcount != (size_t)1)
  58. fail("json_pack integer refcount failed");
  59. json_decref(value);
  60. /* real */
  61. value = json_pack("f", 1.0);
  62. if(!json_is_real(value) || json_real_value(value) != 1.0)
  63. fail("json_pack real failed");
  64. if(value->refcount != (size_t)1)
  65. fail("json_pack real refcount failed");
  66. json_decref(value);
  67. /* string */
  68. value = json_pack("s", "test");
  69. if(!json_is_string(value) || strcmp("test", json_string_value(value)))
  70. fail("json_pack string failed");
  71. if(value->refcount != (size_t)1)
  72. fail("json_pack string refcount failed");
  73. json_decref(value);
  74. /* nullable string (defined case) */
  75. value = json_pack("s?", "test");
  76. if(!json_is_string(value) || strcmp("test", json_string_value(value)))
  77. fail("json_pack nullable string (defined case) failed");
  78. if(value->refcount != (size_t)1)
  79. fail("json_pack nullable string (defined case) refcount failed");
  80. json_decref(value);
  81. /* nullable string (NULL case) */
  82. value = json_pack("s?", NULL);
  83. if(!json_is_null(value))
  84. fail("json_pack nullable string (NULL case) failed");
  85. if(value->refcount != (size_t)-1)
  86. fail("json_pack nullable string (NULL case) refcount failed");
  87. json_decref(value);
  88. /* string and length (int) */
  89. value = json_pack("s#", "test asdf", 4);
  90. if(!json_is_string(value) || strcmp("test", json_string_value(value)))
  91. fail("json_pack string and length failed");
  92. if(value->refcount != (size_t)1)
  93. fail("json_pack string and length refcount failed");
  94. json_decref(value);
  95. /* string and length (size_t) */
  96. value = json_pack("s%", "test asdf", (size_t)4);
  97. if(!json_is_string(value) || strcmp("test", json_string_value(value)))
  98. fail("json_pack string and length failed");
  99. if(value->refcount != (size_t)1)
  100. fail("json_pack string and length refcount failed");
  101. json_decref(value);
  102. /* string and length (int), non-NUL terminated string */
  103. value = json_pack("s#", buffer, 4);
  104. if(!json_is_string(value) || strcmp("test", json_string_value(value)))
  105. fail("json_pack string and length (int) failed");
  106. if(value->refcount != (size_t)1)
  107. fail("json_pack string and length (int) refcount failed");
  108. json_decref(value);
  109. /* string and length (size_t), non-NUL terminated string */
  110. value = json_pack("s%", buffer, (size_t)4);
  111. if(!json_is_string(value) || strcmp("test", json_string_value(value)))
  112. fail("json_pack string and length (size_t) failed");
  113. if(value->refcount != (size_t)1)
  114. fail("json_pack string and length (size_t) refcount failed");
  115. json_decref(value);
  116. /* string concatenation */
  117. value = json_pack("s++", "te", "st", "ing");
  118. if(!json_is_string(value) || strcmp("testing", json_string_value(value)))
  119. fail("json_pack string concatenation failed");
  120. if(value->refcount != (size_t)1)
  121. fail("json_pack string concatenation refcount failed");
  122. json_decref(value);
  123. /* string concatenation and length (int) */
  124. value = json_pack("s#+#+", "test", 1, "test", 2, "test");
  125. if(!json_is_string(value) || strcmp("ttetest", json_string_value(value)))
  126. fail("json_pack string concatenation and length (int) failed");
  127. if(value->refcount != (size_t)1)
  128. fail("json_pack string concatenation and length (int) refcount failed");
  129. json_decref(value);
  130. /* string concatenation and length (size_t) */
  131. value = json_pack("s%+%+", "test", (size_t)1, "test", (size_t)2, "test");
  132. if(!json_is_string(value) || strcmp("ttetest", json_string_value(value)))
  133. fail("json_pack string concatenation and length (size_t) failed");
  134. if(value->refcount != (size_t)1)
  135. fail("json_pack string concatenation and length (size_t) refcount failed");
  136. json_decref(value);
  137. /* empty object */
  138. value = json_pack("{}", 1.0);
  139. if(!json_is_object(value) || json_object_size(value) != 0)
  140. fail("json_pack empty object failed");
  141. if(value->refcount != (size_t)1)
  142. fail("json_pack empty object refcount failed");
  143. json_decref(value);
  144. /* empty list */
  145. value = json_pack("[]", 1.0);
  146. if(!json_is_array(value) || json_array_size(value) != 0)
  147. fail("json_pack empty list failed");
  148. if(value->refcount != (size_t)1)
  149. fail("json_pack empty list failed");
  150. json_decref(value);
  151. /* non-incref'd object */
  152. value = json_pack("o", json_integer(1));
  153. if(!json_is_integer(value) || json_integer_value(value) != 1)
  154. fail("json_pack object failed");
  155. if(value->refcount != (size_t)1)
  156. fail("json_pack integer refcount failed");
  157. json_decref(value);
  158. /* non-incref'd nullable object (defined case) */
  159. value = json_pack("o?", json_integer(1));
  160. if(!json_is_integer(value) || json_integer_value(value) != 1)
  161. fail("json_pack nullable object (defined case) failed");
  162. if(value->refcount != (size_t)1)
  163. fail("json_pack nullable object (defined case) refcount failed");
  164. json_decref(value);
  165. /* non-incref'd nullable object (NULL case) */
  166. value = json_pack("o?", NULL);
  167. if(!json_is_null(value))
  168. fail("json_pack nullable object (NULL case) failed");
  169. if(value->refcount != (size_t)-1)
  170. fail("json_pack nullable object (NULL case) refcount failed");
  171. json_decref(value);
  172. /* incref'd object */
  173. value = json_pack("O", json_integer(1));
  174. if(!json_is_integer(value) || json_integer_value(value) != 1)
  175. fail("json_pack object failed");
  176. if(value->refcount != (size_t)2)
  177. fail("json_pack integer refcount failed");
  178. json_decref(value);
  179. json_decref(value);
  180. /* incref'd nullable object (defined case) */
  181. value = json_pack("O?", json_integer(1));
  182. if(!json_is_integer(value) || json_integer_value(value) != 1)
  183. fail("json_pack incref'd nullable object (defined case) failed");
  184. if(value->refcount != (size_t)2)
  185. fail("json_pack incref'd nullable object (defined case) refcount failed");
  186. json_decref(value);
  187. json_decref(value);
  188. /* incref'd nullable object (NULL case) */
  189. value = json_pack("O?", NULL);
  190. if(!json_is_null(value))
  191. fail("json_pack incref'd nullable object (NULL case) failed");
  192. if(value->refcount != (size_t)-1)
  193. fail("json_pack incref'd nullable object (NULL case) refcount failed");
  194. /* simple object */
  195. value = json_pack("{s:[]}", "foo");
  196. if(!json_is_object(value) || json_object_size(value) != 1)
  197. fail("json_pack array failed");
  198. if(!json_is_array(json_object_get(value, "foo")))
  199. fail("json_pack array failed");
  200. if(json_object_get(value, "foo")->refcount != (size_t)1)
  201. fail("json_pack object refcount failed");
  202. json_decref(value);
  203. /* object with complex key */
  204. value = json_pack("{s+#+: []}", "foo", "barbar", 3, "baz");
  205. if(!json_is_object(value) || json_object_size(value) != 1)
  206. fail("json_pack array failed");
  207. if(!json_is_array(json_object_get(value, "foobarbaz")))
  208. fail("json_pack array failed");
  209. if(json_object_get(value, "foobarbaz")->refcount != (size_t)1)
  210. fail("json_pack object refcount failed");
  211. json_decref(value);
  212. /* simple array */
  213. value = json_pack("[i,i,i]", 0, 1, 2);
  214. if(!json_is_array(value) || json_array_size(value) != 3)
  215. fail("json_pack object failed");
  216. for(i=0; i<3; i++)
  217. {
  218. if(!json_is_integer(json_array_get(value, i)) ||
  219. json_integer_value(json_array_get(value, i)) != i)
  220. fail("json_pack integer array failed");
  221. }
  222. json_decref(value);
  223. /* Whitespace; regular string */
  224. value = json_pack(" s ", "test");
  225. if(!json_is_string(value) || strcmp("test", json_string_value(value)))
  226. fail("json_pack string (with whitespace) failed");
  227. json_decref(value);
  228. /* Whitespace; empty array */
  229. value = json_pack("[ ]");
  230. if(!json_is_array(value) || json_array_size(value) != 0)
  231. fail("json_pack empty array (with whitespace) failed");
  232. json_decref(value);
  233. /* Whitespace; array */
  234. value = json_pack("[ i , i, i ] ", 1, 2, 3);
  235. if(!json_is_array(value) || json_array_size(value) != 3)
  236. fail("json_pack array (with whitespace) failed");
  237. json_decref(value);
  238. /*
  239. * Invalid cases
  240. */
  241. /* newline in format string */
  242. if(json_pack_ex(&error, 0, "{\n\n1"))
  243. fail("json_pack failed to catch invalid format '1'");
  244. check_error("Expected format 's', got '1'", "<format>", 3, 1, 4);
  245. /* mismatched open/close array/object */
  246. if(json_pack_ex(&error, 0, "[}"))
  247. fail("json_pack failed to catch mismatched '}'");
  248. check_error("Unexpected format character '}'", "<format>", 1, 2, 2);
  249. if(json_pack_ex(&error, 0, "{]"))
  250. fail("json_pack failed to catch mismatched ']'");
  251. check_error("Expected format 's', got ']'", "<format>", 1, 2, 2);
  252. /* missing close array */
  253. if(json_pack_ex(&error, 0, "["))
  254. fail("json_pack failed to catch missing ']'");
  255. check_error("Unexpected end of format string", "<format>", 1, 2, 2);
  256. /* missing close object */
  257. if(json_pack_ex(&error, 0, "{"))
  258. fail("json_pack failed to catch missing '}'");
  259. check_error("Unexpected end of format string", "<format>", 1, 2, 2);
  260. /* garbage after format string */
  261. if(json_pack_ex(&error, 0, "[i]a", 42))
  262. fail("json_pack failed to catch garbage after format string");
  263. check_error("Garbage after format string", "<format>", 1, 4, 4);
  264. if(json_pack_ex(&error, 0, "ia", 42))
  265. fail("json_pack failed to catch garbage after format string");
  266. check_error("Garbage after format string", "<format>", 1, 2, 2);
  267. /* NULL string */
  268. if(json_pack_ex(&error, 0, "s", NULL))
  269. fail("json_pack failed to catch null argument string");
  270. check_error("NULL string argument", "<args>", 1, 1, 1);
  271. /* + on its own */
  272. if(json_pack_ex(&error, 0, "+", NULL))
  273. fail("json_pack failed to a lone +");
  274. check_error("Unexpected format character '+'", "<format>", 1, 1, 1);
  275. /* NULL format */
  276. if(json_pack_ex(&error, 0, NULL))
  277. fail("json_pack failed to catch NULL format string");
  278. check_error("NULL or empty format string", "<format>", -1, -1, 0);
  279. /* NULL key */
  280. if(json_pack_ex(&error, 0, "{s:i}", NULL, 1))
  281. fail("json_pack failed to catch NULL key");
  282. check_error("NULL string argument", "<args>", 1, 2, 2);
  283. /* More complicated checks for row/columns */
  284. if(json_pack_ex(&error, 0, "{ {}: s }", "foo"))
  285. fail("json_pack failed to catch object as key");
  286. check_error("Expected format 's', got '{'", "<format>", 1, 3, 3);
  287. /* Complex object */
  288. if(json_pack_ex(&error, 0, "{ s: {}, s:[ii{} }", "foo", "bar", 12, 13))
  289. fail("json_pack failed to catch missing ]");
  290. check_error("Unexpected format character '}'", "<format>", 1, 19, 19);
  291. /* Complex array */
  292. if(json_pack_ex(&error, 0, "[[[[[ [[[[[ [[[[ }]]]] ]]]] ]]]]]"))
  293. fail("json_pack failed to catch extra }");
  294. check_error("Unexpected format character '}'", "<format>", 1, 21, 21);
  295. /* Invalid UTF-8 in object key */
  296. if(json_pack_ex(&error, 0, "{s:i}", "\xff\xff", 42))
  297. fail("json_pack failed to catch invalid UTF-8 in an object key");
  298. check_error("Invalid UTF-8 object key", "<args>", 1, 2, 2);
  299. /* Invalid UTF-8 in a string */
  300. if(json_pack_ex(&error, 0, "{s:s}", "foo", "\xff\xff"))
  301. fail("json_pack failed to catch invalid UTF-8 in a string");
  302. check_error("Invalid UTF-8 string", "<args>", 1, 4, 4);
  303. }