You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

test_null_keys_add.c 4.3 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. /*
  2. * Tests if binary strings are supported.
  3. */
  4. #include "config.h"
  5. #include <stdio.h>
  6. #include <string.h>
  7. #include "json_inttypes.h"
  8. #include "json_object.h"
  9. #include "json_tokener.h"
  10. #include "linkhash.h"
  11. int main(void)
  12. {
  13. /* this test has embedded null characters in the key and value.
  14. * check that it's still included after deserializing. */
  15. const char *input = "{ \"foo\": 14.5, \"bar\": [] }";
  16. const char *foo_key = "foo";
  17. const char *foo_value = "14.5";
  18. const char *bar_key = "bar";
  19. const char *toadd_key = "foo\0bar";
  20. const int toadd_key_len = 7;
  21. const char *toadd_key_printable = "foo\\0bar";
  22. const char *toadd_value = "qwerty\0asdf";
  23. const int toadd_value_len = 12;
  24. const char *toadd_value_printable = "qwerty\\0asdf";
  25. struct json_object *parsed = json_tokener_parse(input);
  26. printf("Parsed input: %s\n", input);
  27. printf("Result is ");
  28. if (parsed == NULL)
  29. {
  30. printf("NULL (error!)\n");
  31. return 1; // Error somewhere
  32. }
  33. else if (!json_object_is_type(parsed, json_type_object))
  34. {
  35. printf("not `json_type_object` (error!)\n");
  36. return 1; // Error somewhere
  37. }
  38. else
  39. {
  40. printf("`json_type_object`\n");
  41. }
  42. // Check nothing odd happened in parsing
  43. if (json_object_object_length(parsed) != 2)
  44. {
  45. printf("Should contain two fields (has %d) (error!)",
  46. json_object_object_length(parsed));
  47. return 1;
  48. }
  49. json_bool key_present = json_object_object_get_ex(parsed, foo_key, NULL);
  50. if (!key_present)
  51. {
  52. printf("Should contain key \"%s\", but does not (error!)", foo_key);
  53. return 1;
  54. }
  55. key_present = json_object_object_get_ex(parsed, bar_key, NULL);
  56. if (!key_present)
  57. {
  58. printf("Should contain key \"%s\", but does not (error!)", bar_key);
  59. return 1;
  60. }
  61. // Check the previous keys are still the same present
  62. struct json_object *foo = json_object_object_get(parsed, foo_key);
  63. if (!json_object_is_type(foo, json_type_double))
  64. {
  65. printf("Key \"%s\" should be `json_type_double` (%d) but was %d (error!)\n",
  66. foo_key, (int)json_type_double, json_object_get_type(foo));
  67. return 1;
  68. }
  69. else
  70. {
  71. printf("Key \"%s\" parsed as right type\n", foo_key);
  72. }
  73. struct json_object *bar = json_object_object_get(parsed, bar_key);
  74. if (!json_object_is_type(bar, json_type_array))
  75. {
  76. printf("Key \"%s\" should be `json_type_array` (%d) but was %d (error!)\n", bar_key,
  77. (int)json_type_array, json_object_get_type(bar));
  78. return 1;
  79. }
  80. else
  81. {
  82. printf("Key \"%s\" parsed as right type\n", bar_key);
  83. }
  84. // Add the new key
  85. struct json_object *new_str = json_object_new_string_len(toadd_value, toadd_value_len);
  86. if (json_object_object_add_ex_len(parsed, toadd_key, toadd_key_len, new_str,
  87. JSON_C_OBJECT_KEY_IS_CONSTANT) != 0)
  88. {
  89. printf("An error occured adding the key \"%s\" (error!)\n", toadd_key_printable);
  90. return 1;
  91. }
  92. // Check the new key was actually added
  93. if (json_object_object_length(parsed) != 3)
  94. {
  95. printf("Should contain three fields after adding new key (has %d) (error!)",
  96. json_object_object_length(parsed));
  97. return 1;
  98. }
  99. else if (json_object_object_get_len(parsed, toadd_key, toadd_key_len) != new_str)
  100. {
  101. printf("Have three keys, but don't have the right value in \"%s\" (error!)\n",
  102. toadd_key_printable);
  103. printf("Keys :\n");
  104. json_object_object_foreach(parsed, key, val)
  105. {
  106. putchar('\"');
  107. fwrite(json_key_data(key), json_key_size(key), 1, stdout);
  108. printf("\" (%zd)\n", json_key_size(key));
  109. }
  110. return 1;
  111. }
  112. else
  113. {
  114. printf("Added the key \"%s\" successfully\n", toadd_key_printable);
  115. }
  116. // Check the previous keys are still the same present
  117. foo = json_object_object_get(parsed, foo_key);
  118. if (!json_object_is_type(foo, json_type_double))
  119. {
  120. printf("Key \"%s\" should be `json_type_double` (%d) but was %d (error!)\n",
  121. foo_key, (int)json_type_double, json_object_get_type(foo));
  122. return 1;
  123. }
  124. else
  125. {
  126. printf("Key \"%s\" is still the same\n", foo_key);
  127. }
  128. bar = json_object_object_get(parsed, bar_key);
  129. if (!json_object_is_type(bar, json_type_array))
  130. {
  131. printf("Key \"%s\" should be `json_type_array` (%d) but was %d (error!)\n", bar_key,
  132. (int)json_type_array, json_object_get_type(bar));
  133. return 1;
  134. }
  135. else
  136. {
  137. printf("Key \"%s\" is still the same\n", bar_key);
  138. }
  139. json_object_put(parsed);
  140. printf("PASS\n");
  141. return 0;
  142. }