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_get.c 3.7 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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\\u0000bar\": \"qwerty\\u0000asdf\" }";
  15. const char *expected_key = "foo\0bar";
  16. const int expected_key_len = 7;
  17. const char *expected_key_printable = "foo\\0bar";
  18. const char *expected_value = "qwerty\0asdf";
  19. const int expected_value_len = 11;
  20. const char *expected_value_printable = "qwerty\\0asdf";
  21. struct json_object *parsed = json_tokener_parse(input);
  22. printf("Parsed input: %s\n", input);
  23. printf("Result is ");
  24. if (parsed == NULL)
  25. {
  26. printf("NULL (error!)\n");
  27. return 1; // Error somewhere
  28. }
  29. else if (!json_object_is_type(parsed, json_type_object))
  30. {
  31. printf("not `json_type_object` (error!)\n");
  32. return 1; // Error somewhere
  33. }
  34. else
  35. {
  36. printf("`json_type_object`\n");
  37. }
  38. // Check nothing odd happened in parsing
  39. if (json_object_object_length(parsed) != 1)
  40. {
  41. printf("Should contain only one field (has %d) (error!)",
  42. json_object_object_length(parsed));
  43. return 1;
  44. }
  45. json_bool key_present = json_object_object_get_ex(parsed, expected_key, NULL);
  46. if (key_present)
  47. {
  48. printf("The key \"%s\" should not be present when calling "
  49. "`json_object_object_get_ex` "
  50. "(the real key contains a NUL character). (error!)\n",
  51. expected_key);
  52. return 1;
  53. }
  54. // Check the key is present
  55. struct json_object *string;
  56. key_present = json_object_object_get_ex_len(parsed, expected_key, 7, &string);
  57. if (!key_present)
  58. {
  59. printf("The key \"%s\" should be present when calling "
  60. "`json_object_object_get_ex_len` (error!)\n",
  61. expected_key_printable);
  62. return 1;
  63. }
  64. else if (string == NULL)
  65. {
  66. printf("The key \"%s\" was present when calling "
  67. "`json_object_object_get_ex_len`, but got NULL (error!)\n",
  68. expected_key_printable);
  69. return 1;
  70. }
  71. struct json_object *from_get_len = json_object_object_get_len(parsed, expected_key, 7);
  72. if (string != from_get_len)
  73. {
  74. printf("The value returned from `json_object_object_get_len` should be the "
  75. "same as the one from `json_object_object_get_ex_len` (error!)\n"
  76. "Got %p from `json_object_object_get_ex_len` but %p from "
  77. "`json_object_object_get_len`\n",
  78. string, from_get_len);
  79. return 1;
  80. }
  81. else
  82. {
  83. printf("Key was present and same for "
  84. "`json_object_object_get_ex_len` and `json_object_object_get_len`\n");
  85. }
  86. // Check the value is right
  87. if (!json_object_is_type(string, json_type_string))
  88. {
  89. printf(
  90. "Value is wrong type, expected `json_type_string` (%d) but got %d (error!)\n",
  91. json_type_string, json_object_get_type(string));
  92. return 1;
  93. }
  94. else
  95. {
  96. printf("Value is right type, `json_type_string`\n");
  97. }
  98. const int actual_value_len = json_object_get_string_len(string);
  99. const char *actual_value = json_object_get_string(string);
  100. if (actual_value_len != expected_value_len)
  101. {
  102. printf("Value is wrong length, expected %d but got %d (error!)\n",
  103. expected_value_len, actual_value_len);
  104. return 1;
  105. }
  106. else if (memcmp(expected_value, actual_value, actual_value_len) != 0)
  107. {
  108. printf("Expected \"%s\" but got \"", expected_value_printable);
  109. for (int i = 0; i < actual_value_len; ++i)
  110. {
  111. if (actual_value[i] == '\0')
  112. {
  113. puts("\\0");
  114. }
  115. else
  116. {
  117. putchar(actual_value[i]);
  118. }
  119. }
  120. printf("\"\n");
  121. return 1;
  122. }
  123. else
  124. {
  125. printf("Expected value matches actual\n");
  126. }
  127. json_object_put(parsed);
  128. printf("PASS\n");
  129. return 0;
  130. }