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

https://gitlab.com/Ornim/obs-old · C · 307 lines · 210 code · 46 blank · 51 comment · 118 complexity · 364d7199f4613b92d4cdb50de9401d91 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. #ifdef HAVE_CONFIG_H
  9. #include <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. /* string and length (int) */
  75. value = json_pack("s#", "test asdf", 4);
  76. if(!json_is_string(value) || strcmp("test", json_string_value(value)))
  77. fail("json_pack string and length failed");
  78. if(value->refcount != (size_t)1)
  79. fail("json_pack string and length refcount failed");
  80. json_decref(value);
  81. /* string and length (size_t) */
  82. value = json_pack("s%", "test asdf", (size_t)4);
  83. if(!json_is_string(value) || strcmp("test", json_string_value(value)))
  84. fail("json_pack string and length failed");
  85. if(value->refcount != (size_t)1)
  86. fail("json_pack string and length refcount failed");
  87. json_decref(value);
  88. /* string and length (int), non-NUL terminated string */
  89. value = json_pack("s#", buffer, 4);
  90. if(!json_is_string(value) || strcmp("test", json_string_value(value)))
  91. fail("json_pack string and length (int) failed");
  92. if(value->refcount != (size_t)1)
  93. fail("json_pack string and length (int) refcount failed");
  94. json_decref(value);
  95. /* string and length (size_t), non-NUL terminated string */
  96. value = json_pack("s%", buffer, (size_t)4);
  97. if(!json_is_string(value) || strcmp("test", json_string_value(value)))
  98. fail("json_pack string and length (size_t) failed");
  99. if(value->refcount != (size_t)1)
  100. fail("json_pack string and length (size_t) refcount failed");
  101. json_decref(value);
  102. /* string concatenation */
  103. value = json_pack("s++", "te", "st", "ing");
  104. if(!json_is_string(value) || strcmp("testing", json_string_value(value)))
  105. fail("json_pack string concatenation failed");
  106. if(value->refcount != (size_t)1)
  107. fail("json_pack string concatenation refcount failed");
  108. json_decref(value);
  109. /* string concatenation and length (int) */
  110. value = json_pack("s#+#+", "test", 1, "test", 2, "test");
  111. if(!json_is_string(value) || strcmp("ttetest", json_string_value(value)))
  112. fail("json_pack string concatenation and length (int) failed");
  113. if(value->refcount != (size_t)1)
  114. fail("json_pack string concatenation and length (int) refcount failed");
  115. json_decref(value);
  116. /* string concatenation and length (size_t) */
  117. value = json_pack("s%+%+", "test", (size_t)1, "test", (size_t)2, "test");
  118. if(!json_is_string(value) || strcmp("ttetest", json_string_value(value)))
  119. fail("json_pack string concatenation and length (size_t) failed");
  120. if(value->refcount != (size_t)1)
  121. fail("json_pack string concatenation and length (size_t) refcount failed");
  122. json_decref(value);
  123. /* empty object */
  124. value = json_pack("{}", 1.0);
  125. if(!json_is_object(value) || json_object_size(value) != 0)
  126. fail("json_pack empty object failed");
  127. if(value->refcount != (size_t)1)
  128. fail("json_pack empty object refcount failed");
  129. json_decref(value);
  130. /* empty list */
  131. value = json_pack("[]", 1.0);
  132. if(!json_is_array(value) || json_array_size(value) != 0)
  133. fail("json_pack empty list failed");
  134. if(value->refcount != (size_t)1)
  135. fail("json_pack empty list failed");
  136. json_decref(value);
  137. /* non-incref'd object */
  138. value = json_pack("o", json_integer(1));
  139. if(!json_is_integer(value) || json_integer_value(value) != 1)
  140. fail("json_pack object failed");
  141. if(value->refcount != (size_t)1)
  142. fail("json_pack integer refcount failed");
  143. json_decref(value);
  144. /* incref'd object */
  145. value = json_pack("O", json_integer(1));
  146. if(!json_is_integer(value) || json_integer_value(value) != 1)
  147. fail("json_pack object failed");
  148. if(value->refcount != (size_t)2)
  149. fail("json_pack integer refcount failed");
  150. json_decref(value);
  151. json_decref(value);
  152. /* simple object */
  153. value = json_pack("{s:[]}", "foo");
  154. if(!json_is_object(value) || json_object_size(value) != 1)
  155. fail("json_pack array failed");
  156. if(!json_is_array(json_object_get(value, "foo")))
  157. fail("json_pack array failed");
  158. if(json_object_get(value, "foo")->refcount != (size_t)1)
  159. fail("json_pack object refcount failed");
  160. json_decref(value);
  161. /* object with complex key */
  162. value = json_pack("{s+#+: []}", "foo", "barbar", 3, "baz");
  163. if(!json_is_object(value) || json_object_size(value) != 1)
  164. fail("json_pack array failed");
  165. if(!json_is_array(json_object_get(value, "foobarbaz")))
  166. fail("json_pack array failed");
  167. if(json_object_get(value, "foobarbaz")->refcount != (size_t)1)
  168. fail("json_pack object refcount failed");
  169. json_decref(value);
  170. /* simple array */
  171. value = json_pack("[i,i,i]", 0, 1, 2);
  172. if(!json_is_array(value) || json_array_size(value) != 3)
  173. fail("json_pack object failed");
  174. for(i=0; i<3; i++)
  175. {
  176. if(!json_is_integer(json_array_get(value, i)) ||
  177. json_integer_value(json_array_get(value, i)) != i)
  178. fail("json_pack integer array failed");
  179. }
  180. json_decref(value);
  181. /* Whitespace; regular string */
  182. value = json_pack(" s ", "test");
  183. if(!json_is_string(value) || strcmp("test", json_string_value(value)))
  184. fail("json_pack string (with whitespace) failed");
  185. json_decref(value);
  186. /* Whitespace; empty array */
  187. value = json_pack("[ ]");
  188. if(!json_is_array(value) || json_array_size(value) != 0)
  189. fail("json_pack empty array (with whitespace) failed");
  190. json_decref(value);
  191. /* Whitespace; array */
  192. value = json_pack("[ i , i, i ] ", 1, 2, 3);
  193. if(!json_is_array(value) || json_array_size(value) != 3)
  194. fail("json_pack array (with whitespace) failed");
  195. json_decref(value);
  196. /*
  197. * Invalid cases
  198. */
  199. /* newline in format string */
  200. if(json_pack_ex(&error, 0, "{\n\n1"))
  201. fail("json_pack failed to catch invalid format '1'");
  202. check_error("Expected format 's', got '1'", "<format>", 3, 1, 4);
  203. /* mismatched open/close array/object */
  204. if(json_pack_ex(&error, 0, "[}"))
  205. fail("json_pack failed to catch mismatched '}'");
  206. check_error("Unexpected format character '}'", "<format>", 1, 2, 2);
  207. if(json_pack_ex(&error, 0, "{]"))
  208. fail("json_pack failed to catch mismatched ']'");
  209. check_error("Expected format 's', got ']'", "<format>", 1, 2, 2);
  210. /* missing close array */
  211. if(json_pack_ex(&error, 0, "["))
  212. fail("json_pack failed to catch missing ']'");
  213. check_error("Unexpected end of format string", "<format>", 1, 2, 2);
  214. /* missing close object */
  215. if(json_pack_ex(&error, 0, "{"))
  216. fail("json_pack failed to catch missing '}'");
  217. check_error("Unexpected end of format string", "<format>", 1, 2, 2);
  218. /* garbage after format string */
  219. if(json_pack_ex(&error, 0, "[i]a", 42))
  220. fail("json_pack failed to catch garbage after format string");
  221. check_error("Garbage after format string", "<format>", 1, 4, 4);
  222. if(json_pack_ex(&error, 0, "ia", 42))
  223. fail("json_pack failed to catch garbage after format string");
  224. check_error("Garbage after format string", "<format>", 1, 2, 2);
  225. /* NULL string */
  226. if(json_pack_ex(&error, 0, "s", NULL))
  227. fail("json_pack failed to catch null argument string");
  228. check_error("NULL string argument", "<args>", 1, 1, 1);
  229. /* + on its own */
  230. if(json_pack_ex(&error, 0, "+", NULL))
  231. fail("json_pack failed to a lone +");
  232. check_error("Unexpected format character '+'", "<format>", 1, 1, 1);
  233. /* NULL format */
  234. if(json_pack_ex(&error, 0, NULL))
  235. fail("json_pack failed to catch NULL format string");
  236. check_error("NULL or empty format string", "<format>", -1, -1, 0);
  237. /* NULL key */
  238. if(json_pack_ex(&error, 0, "{s:i}", NULL, 1))
  239. fail("json_pack failed to catch NULL key");
  240. check_error("NULL string argument", "<args>", 1, 2, 2);
  241. /* More complicated checks for row/columns */
  242. if(json_pack_ex(&error, 0, "{ {}: s }", "foo"))
  243. fail("json_pack failed to catch object as key");
  244. check_error("Expected format 's', got '{'", "<format>", 1, 3, 3);
  245. /* Complex object */
  246. if(json_pack_ex(&error, 0, "{ s: {}, s:[ii{} }", "foo", "bar", 12, 13))
  247. fail("json_pack failed to catch missing ]");
  248. check_error("Unexpected format character '}'", "<format>", 1, 19, 19);
  249. /* Complex array */
  250. if(json_pack_ex(&error, 0, "[[[[[ [[[[[ [[[[ }]]]] ]]]] ]]]]]"))
  251. fail("json_pack failed to catch extra }");
  252. check_error("Unexpected format character '}'", "<format>", 1, 21, 21);
  253. /* Invalid UTF-8 in object key */
  254. if(json_pack_ex(&error, 0, "{s:i}", "\xff\xff", 42))
  255. fail("json_pack failed to catch invalid UTF-8 in an object key");
  256. check_error("Invalid UTF-8 object key", "<args>", 1, 2, 2);
  257. /* Invalid UTF-8 in a string */
  258. if(json_pack_ex(&error, 0, "{s:s}", "foo", "\xff\xff"))
  259. fail("json_pack failed to catch invalid UTF-8 in a string");
  260. check_error("Invalid UTF-8 string", "<args>", 1, 4, 4);
  261. }