test_fixed_size.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. /*
  2. * Copyright (c) 2020 Petri Lehtinen <[email protected]>
  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 "util.h"
  8. #include <jansson.h>
  9. #include <string.h>
  10. static void test_keylen_iterator(json_t *object) {
  11. const char key1[] = {'t', 'e', 's', 't', '1'};
  12. const char key2[] = {'t', 'e', 's', 't'};
  13. const char key3[] = {'t', 'e', 's', '\0', 't'};
  14. const char key4[] = {'t', 'e', 's', 't', '\0'};
  15. const char *reference_keys[] = {key1, key2, key3, key4};
  16. const size_t reference_keys_len[] = {sizeof(key1), sizeof(key2), sizeof(key3),
  17. sizeof(key4)};
  18. size_t index = 0;
  19. json_t *value;
  20. const char *key;
  21. size_t keylen;
  22. json_object_keylen_foreach(object, key, keylen, value) {
  23. if (keylen != reference_keys_len[index])
  24. fail("invalid key len in iterator");
  25. if (memcmp(key, reference_keys[index], reference_keys_len[index]) != 0)
  26. fail("invalid key in iterator");
  27. index++;
  28. }
  29. }
  30. static void test_keylen(void) {
  31. json_t *obj = json_object();
  32. const char key[] = {'t', 'e', 's', 't', '1'};
  33. const char key2[] = {'t', 'e', 's', 't'};
  34. const char key3[] = {'t', 'e', 's', '\0', 't'};
  35. const char key4[] = {'t', 'e', 's', 't', '\0'};
  36. if (json_object_size(obj) != 0)
  37. fail("incorrect json");
  38. json_object_set_new_nocheck(obj, "test1", json_true());
  39. if (json_object_size(obj) != 1)
  40. fail("incorrect json");
  41. if (json_object_getn(obj, key, sizeof(key)) != json_true())
  42. fail("json_object_getn failed");
  43. if (json_object_getn(obj, key2, sizeof(key2)) != NULL)
  44. fail("false positive json_object_getn by key2");
  45. if (json_object_setn_nocheck(obj, key2, sizeof(key2), json_false()))
  46. fail("json_object_setn_nocheck for key2 failed");
  47. if (json_object_size(obj) != 2)
  48. fail("incorrect json");
  49. if (json_object_get(obj, "test") != json_false())
  50. fail("json_object_setn_nocheck for key2 failed");
  51. if (json_object_getn(obj, key2, sizeof(key2)) != json_false())
  52. fail("json_object_getn by key 2 failed");
  53. if (json_object_getn(obj, key3, sizeof(key3)) != NULL)
  54. fail("false positive json_object_getn by key3");
  55. if (json_object_setn_nocheck(obj, key3, sizeof(key3), json_false()))
  56. fail("json_object_setn_nocheck for key3 failed");
  57. if (json_object_size(obj) != 3)
  58. fail("incorrect json");
  59. if (json_object_getn(obj, key3, sizeof(key3)) != json_false())
  60. fail("json_object_getn by key 3 failed");
  61. if (json_object_getn(obj, key4, sizeof(key4)) != NULL)
  62. fail("false positive json_object_getn by key3");
  63. if (json_object_setn_nocheck(obj, key4, sizeof(key4), json_false()))
  64. fail("json_object_setn_nocheck for key3 failed");
  65. if (json_object_size(obj) != 4)
  66. fail("incorrect json");
  67. test_keylen_iterator(obj);
  68. if (json_object_getn(obj, key4, sizeof(key4)) != json_false())
  69. fail("json_object_getn by key 3 failed");
  70. if (json_object_size(obj) != 4)
  71. fail("incorrect json");
  72. if (json_object_deln(obj, key4, sizeof(key4)))
  73. fail("json_object_deln failed");
  74. if (json_object_getn(obj, key4, sizeof(key4)) != NULL)
  75. fail("json_object_deln failed");
  76. if (json_object_size(obj) != 3)
  77. fail("incorrect json");
  78. if (json_object_deln(obj, key3, sizeof(key3)))
  79. fail("json_object_deln failed");
  80. if (json_object_getn(obj, key3, sizeof(key3)) != NULL)
  81. fail("json_object_deln failed");
  82. if (json_object_size(obj) != 2)
  83. fail("incorrect json");
  84. if (json_object_deln(obj, key2, sizeof(key2)))
  85. fail("json_object_deln failed");
  86. if (json_object_getn(obj, key2, sizeof(key2)) != NULL)
  87. fail("json_object_deln failed");
  88. if (json_object_size(obj) != 1)
  89. fail("incorrect json");
  90. if (json_object_deln(obj, key, sizeof(key)))
  91. fail("json_object_deln failed");
  92. if (json_object_getn(obj, key, sizeof(key)) != NULL)
  93. fail("json_object_deln failed");
  94. if (json_object_size(obj) != 0)
  95. fail("incorrect json");
  96. json_decref(obj);
  97. }
  98. static void test_invalid_keylen(void) {
  99. json_t *obj = json_object();
  100. json_t *empty_obj = json_object();
  101. const char key[] = {'t', 'e', 's', 't', '1'};
  102. json_object_set_new_nocheck(obj, "test1", json_true());
  103. if (json_object_getn(NULL, key, sizeof(key)) != NULL)
  104. fail("json_object_getn on NULL failed");
  105. if (json_object_getn(obj, NULL, sizeof(key)) != NULL)
  106. fail("json_object_getn on NULL failed");
  107. if (json_object_getn(obj, key, 0) != NULL)
  108. fail("json_object_getn on NULL failed");
  109. if (!json_object_setn_new(obj, NULL, sizeof(key), json_true()))
  110. fail("json_object_setn_new with NULL key failed");
  111. if (!json_object_setn_new_nocheck(obj, NULL, sizeof(key), json_true()))
  112. fail("json_object_setn_new_nocheck with NULL key failed");
  113. if (!json_object_del(obj, NULL))
  114. fail("json_object_del with NULL failed");
  115. if (!json_object_deln(empty_obj, key, sizeof(key)))
  116. fail("json_object_deln with empty object failed");
  117. if (!json_object_deln(obj, key, sizeof(key) - 1))
  118. fail("json_object_deln with incomplete key failed");
  119. json_decref(obj);
  120. json_decref(empty_obj);
  121. }
  122. static void test_binary_keys(void) {
  123. json_t *obj = json_object();
  124. int key1 = 0;
  125. int key2 = 1;
  126. json_object_setn_nocheck(obj, (const char *)&key1, sizeof(key1), json_true());
  127. json_object_setn_nocheck(obj, (const char *)&key2, sizeof(key2), json_true());
  128. if (!json_is_true(json_object_getn(obj, (const char *)&key1, sizeof(key1))))
  129. fail("cannot get integer key1");
  130. if (!json_is_true(json_object_getn(obj, (const char *)&key1, sizeof(key2))))
  131. fail("cannot get integer key2");
  132. if (json_object_size(obj) != 2)
  133. fail("binary object size missmatch");
  134. if (json_object_deln(obj, (const char *)&key1, sizeof(key1)))
  135. fail("cannot del integer key1");
  136. if (json_object_size(obj) != 1)
  137. fail("binary object size missmatch");
  138. if (json_object_deln(obj, (const char *)&key2, sizeof(key2)))
  139. fail("cannot del integer key2");
  140. if (json_object_size(obj) != 0)
  141. fail("binary object size missmatch");
  142. json_decref(obj);
  143. }
  144. static void test_dump_order(void) {
  145. json_t *obj = json_object();
  146. char key1[] = {'k', '\0', '-', '2'};
  147. char key2[] = {'k', '\0', '-', '1'};
  148. const char expected_sorted_str[] =
  149. "{\"k\\u0000-1\": \"first\", \"k\\u0000-2\": \"second\"}";
  150. const char expected_nonsorted_str[] =
  151. "{\"k\\u0000-2\": \"second\", \"k\\u0000-1\": \"first\"}";
  152. char *out;
  153. json_object_setn_new_nocheck(obj, key1, sizeof(key1), json_string("second"));
  154. json_object_setn_new_nocheck(obj, key2, sizeof(key2), json_string("first"));
  155. out = malloc(512);
  156. json_dumpb(obj, out, 512, 0);
  157. if (memcmp(expected_nonsorted_str, out, sizeof(expected_nonsorted_str) - 1) != 0)
  158. fail("preserve order failed");
  159. json_dumpb(obj, out, 512, JSON_SORT_KEYS);
  160. if (memcmp(expected_sorted_str, out, sizeof(expected_sorted_str) - 1) != 0)
  161. fail("utf-8 sort failed");
  162. free(out);
  163. json_decref(obj);
  164. }
  165. static void run_tests() {
  166. test_keylen();
  167. test_invalid_keylen();
  168. test_binary_keys();
  169. test_dump_order();
  170. }