test_dump.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. /*
  2. * Copyright (c) 2009-2016 Petri Lehtinen <petri@digip.org>
  3. *
  4. * Jansson is free software; you can redistribute it and/or modify
  5. * it under the terms of the MIT license. See LICENSE for details.
  6. */
  7. #include <jansson.h>
  8. #include <string.h>
  9. #include "util.h"
  10. static int encode_null_callback(const char *buffer, size_t size, void *data)
  11. {
  12. (void)buffer;
  13. (void)size;
  14. (void)data;
  15. return 0;
  16. }
  17. static void encode_null()
  18. {
  19. if(json_dumps(NULL, JSON_ENCODE_ANY) != NULL)
  20. fail("json_dumps didn't fail for NULL");
  21. if(json_dumpf(NULL, stderr, JSON_ENCODE_ANY) != -1)
  22. fail("json_dumpf didn't fail for NULL");
  23. /* Don't test json_dump_file to avoid creating a file */
  24. if(json_dump_callback(NULL, encode_null_callback, NULL, JSON_ENCODE_ANY) != -1)
  25. fail("json_dump_callback didn't fail for NULL");
  26. }
  27. static void encode_twice()
  28. {
  29. /* Encode an empty object/array, add an item, encode again */
  30. json_t *json;
  31. char *result;
  32. json = json_object();
  33. result = json_dumps(json, 0);
  34. if(!result || strcmp(result, "{}"))
  35. fail("json_dumps failed");
  36. free(result);
  37. json_object_set_new(json, "foo", json_integer(5));
  38. result = json_dumps(json, 0);
  39. if(!result || strcmp(result, "{\"foo\": 5}"))
  40. fail("json_dumps failed");
  41. free(result);
  42. json_decref(json);
  43. json = json_array();
  44. result = json_dumps(json, 0);
  45. if(!result || strcmp(result, "[]"))
  46. fail("json_dumps failed");
  47. free(result);
  48. json_array_append_new(json, json_integer(5));
  49. result = json_dumps(json, 0);
  50. if(!result || strcmp(result, "[5]"))
  51. fail("json_dumps failed");
  52. free(result);
  53. json_decref(json);
  54. }
  55. static void circular_references()
  56. {
  57. /* Construct a JSON object/array with a circular reference:
  58. object: {"a": {"b": {"c": <circular reference to $.a>}}}
  59. array: [[[<circular reference to the $[0] array>]]]
  60. Encode it, remove the circular reference and encode again.
  61. */
  62. json_t *json;
  63. char *result;
  64. json = json_object();
  65. json_object_set_new(json, "a", json_object());
  66. json_object_set_new(json_object_get(json, "a"), "b", json_object());
  67. json_object_set(json_object_get(json_object_get(json, "a"), "b"), "c",
  68. json_object_get(json, "a"));
  69. if(json_dumps(json, 0))
  70. fail("json_dumps encoded a circular reference!");
  71. json_object_del(json_object_get(json_object_get(json, "a"), "b"), "c");
  72. result = json_dumps(json, 0);
  73. if(!result || strcmp(result, "{\"a\": {\"b\": {}}}"))
  74. fail("json_dumps failed!");
  75. free(result);
  76. json_decref(json);
  77. json = json_array();
  78. json_array_append_new(json, json_array());
  79. json_array_append_new(json_array_get(json, 0), json_array());
  80. json_array_append(json_array_get(json_array_get(json, 0), 0),
  81. json_array_get(json, 0));
  82. if(json_dumps(json, 0))
  83. fail("json_dumps encoded a circular reference!");
  84. json_array_remove(json_array_get(json_array_get(json, 0), 0), 0);
  85. result = json_dumps(json, 0);
  86. if(!result || strcmp(result, "[[[]]]"))
  87. fail("json_dumps failed!");
  88. free(result);
  89. json_decref(json);
  90. }
  91. static void encode_other_than_array_or_object()
  92. {
  93. /* Encoding anything other than array or object should only
  94. * succeed if the JSON_ENCODE_ANY flag is used */
  95. json_t *json;
  96. FILE *fp = NULL;
  97. char *result;
  98. json = json_string("foo");
  99. if(json_dumps(json, 0) != NULL)
  100. fail("json_dumps encoded a string!");
  101. if(json_dumpf(json, fp, 0) == 0)
  102. fail("json_dumpf encoded a string!");
  103. result = json_dumps(json, JSON_ENCODE_ANY);
  104. if(!result || strcmp(result, "\"foo\"") != 0)
  105. fail("json_dumps failed to encode a string with JSON_ENCODE_ANY");
  106. free(result);
  107. json_decref(json);
  108. json = json_integer(42);
  109. if(json_dumps(json, 0) != NULL)
  110. fail("json_dumps encoded an integer!");
  111. if(json_dumpf(json, fp, 0) == 0)
  112. fail("json_dumpf encoded an integer!");
  113. result = json_dumps(json, JSON_ENCODE_ANY);
  114. if(!result || strcmp(result, "42") != 0)
  115. fail("json_dumps failed to encode an integer with JSON_ENCODE_ANY");
  116. free(result);
  117. json_decref(json);
  118. }
  119. static void escape_slashes()
  120. {
  121. /* Test dump escaping slashes */
  122. json_t *json;
  123. char *result;
  124. json = json_object();
  125. json_object_set_new(json, "url", json_string("https://github.com/akheron/jansson"));
  126. result = json_dumps(json, 0);
  127. if(!result || strcmp(result, "{\"url\": \"https://github.com/akheron/jansson\"}"))
  128. fail("json_dumps failed to not escape slashes");
  129. free(result);
  130. result = json_dumps(json, JSON_ESCAPE_SLASH);
  131. if(!result || strcmp(result, "{\"url\": \"https:\\/\\/github.com\\/akheron\\/jansson\"}"))
  132. fail("json_dumps failed to escape slashes");
  133. free(result);
  134. json_decref(json);
  135. }
  136. static void encode_nul_byte()
  137. {
  138. json_t *json;
  139. char *result;
  140. json = json_stringn("nul byte \0 in string", 20);
  141. result = json_dumps(json, JSON_ENCODE_ANY);
  142. if(!result || memcmp(result, "\"nul byte \\u0000 in string\"", 27))
  143. fail("json_dumps failed to dump an embedded NUL byte");
  144. free(result);
  145. json_decref(json);
  146. }
  147. static void dump_file()
  148. {
  149. json_t *json;
  150. int result;
  151. result = json_dump_file(NULL, "", 0);
  152. if (result != -1)
  153. fail("json_dump_file succeeded with invalid args");
  154. json = json_object();
  155. result = json_dump_file(json, "json_dump_file.json", 0);
  156. if (result != 0)
  157. fail("json_dump_file failed");
  158. json_decref(json);
  159. remove("json_dump_file.json");
  160. }
  161. static void run_tests()
  162. {
  163. encode_null();
  164. encode_twice();
  165. circular_references();
  166. encode_other_than_array_or_object();
  167. escape_slashes();
  168. encode_nul_byte();
  169. dump_file();
  170. }