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 3.4 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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. int main(void)
  11. {
  12. /* this test has embedded null characters in the key and value.
  13. * check that it's still included after deserializing. */
  14. const char *input = "{ \"foo\": 14.5, \"bar\": [] }";
  15. const char *foo_key = "foo";
  16. const char *foo_value = "14.5";
  17. const char *bar_key = "bar";
  18. const char *toadd_key = "foo\0bar";
  19. const int toadd_key_len = 7;
  20. const char *toadd_key_printable = "foo\\0bar";
  21. const char *toadd_value = "qwerty\0asdf";
  22. const int toadd_value_len = 12;
  23. const char *toadd_value_printable = "qwerty\\0asdf";
  24. struct json_object *parsed = json_tokener_parse(input);
  25. printf("Parsed input: %s\n", input);
  26. printf("Result is ");
  27. if (parsed == NULL)
  28. {
  29. printf("NULL (error!)\n");
  30. return 1; // Error somewhere
  31. }
  32. else if (!json_object_is_type(parsed, json_type_object))
  33. {
  34. printf("not `json_type_object` (error!)\n");
  35. return 1; // Error somewhere
  36. }
  37. else
  38. {
  39. printf("`json_type_object`\n");
  40. }
  41. // Check nothing odd happened in parsing
  42. if (json_object_object_length(parsed) != 2)
  43. {
  44. printf("Should contain two fields (has %d) (error!)",
  45. json_object_object_length(parsed));
  46. return 1;
  47. }
  48. json_bool key_present = json_object_object_get_ex(parsed, foo_key, NULL);
  49. if (!key_present)
  50. {
  51. printf("Should contain key \"%s\", but does not (error!)", foo_key);
  52. return 1;
  53. }
  54. key_present = json_object_object_get_ex(parsed, bar_key, NULL);
  55. if (!key_present)
  56. {
  57. printf("Should contain key \"%s\", but does not (error!)", bar_key);
  58. return 1;
  59. }
  60. // Add the new key
  61. struct json_object *new_str = json_object_new_string_len(toadd_value, toadd_value_len);
  62. if (json_object_object_add_ex(parsed, toadd_key, new_str,
  63. JSON_C_OBJECT_ADD_KEY_IS_NEW |
  64. JSON_C_OBJECT_KEY_IS_CONSTANT) != 0)
  65. {
  66. printf("An error occured adding the key \"%s\" (error!)\n", toadd_key_printable);
  67. return 1;
  68. }
  69. // Check the new key was actually added
  70. if (json_object_object_length(parsed) != 3)
  71. {
  72. printf("Should contain three fields after adding new key (has %d) (error!)",
  73. json_object_object_length(parsed));
  74. return 1;
  75. }
  76. else if (json_object_object_get_len(parsed, toadd_key, toadd_key_len) != new_str)
  77. {
  78. printf("Have three keys, but don't have the right value in \"%s\" (error!)\n",
  79. toadd_key_printable);
  80. return 1;
  81. }
  82. else
  83. {
  84. printf("Added the key \"%s\" successfully\n", toadd_key_printable);
  85. }
  86. // Check the previous keys are still the same present
  87. struct json_object *foo = json_object_object_get(parsed, foo_key);
  88. if (!json_object_is_type(foo, json_type_double))
  89. {
  90. printf("Key \"%s\" should be `json_type_double` (%d) but was %d (error!)\n",
  91. foo_key, (int)json_type_double, json_object_get_type(foo));
  92. return 1;
  93. }
  94. else
  95. {
  96. printf("Key \"%s\" is still the same\n", foo_key);
  97. }
  98. struct json_object *bar = json_object_object_get(parsed, bar_key);
  99. if (!json_object_is_type(foo, json_type_array))
  100. {
  101. printf("Key \"%s\" should be `json_type_array` (%d) but was %d (error!)\n", bar_key,
  102. (int)json_type_array, json_object_get_type(foo));
  103. return 1;
  104. }
  105. else
  106. {
  107. printf("Key \"%s\" is still the same\n", bar_key);
  108. }
  109. json_object_put(parsed);
  110. printf("PASS\n");
  111. return 0;
  112. }