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_del.c 2.9 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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 = "{ \"biff\\u0000\": null, \"\\u0000zxcvbnm\": 178 }";
  15. const char *expected_biff = "biff\0";
  16. const int expected_biff_len = 5;
  17. const char *expected_biff_printable = "biff\\u0000";
  18. const char *expected_zxcv = "\0zxcvbnm";
  19. const int expected_zxcv_len = 8;
  20. const char *expected_zxcv_printable = "\\u0000zxcvbnm";
  21. struct json_object *parsed = json_tokener_parse(input);
  22. struct json_object *val;
  23. json_bool key_present;
  24. printf("Parsed input: %s\n", input);
  25. printf("Result is ");
  26. if (parsed == NULL)
  27. {
  28. printf("NULL (error!)\n");
  29. return 1; // Error somewhere
  30. }
  31. else if (!json_object_is_type(parsed, json_type_object))
  32. {
  33. printf("not `json_type_object` (error!)\n");
  34. return 1; // Error somewhere
  35. }
  36. else
  37. {
  38. printf("`json_type_object`\n");
  39. }
  40. // Check nothing odd happened in parsing
  41. if (json_object_object_length(parsed) != 2)
  42. {
  43. printf("Should contain two fields (has %d) (error!)",
  44. json_object_object_length(parsed));
  45. return 1;
  46. }
  47. key_present = json_object_object_get_ex_len(parsed, expected_biff, expected_biff_len, &val);
  48. if (!key_present || !json_object_is_type(val, json_type_null))
  49. {
  50. printf("The key \"%s\" should be present and should be NULL (error!)\n",
  51. expected_biff_printable);
  52. return 1;
  53. }
  54. key_present = json_object_object_get_ex_len(parsed, expected_zxcv, expected_zxcv_len, &val);
  55. if (!key_present || !json_object_is_type(val, json_type_int) ||
  56. json_object_get_int(val) != 178)
  57. {
  58. printf("The key \"%s\" should be present and should be 178 (error!)\n",
  59. expected_zxcv_printable);
  60. return 1;
  61. }
  62. printf("Expected keys (\"%s\" and \"%s\") present with expected values\n",
  63. expected_biff_printable, expected_zxcv_printable);
  64. // Delete one key
  65. json_object_object_del_len(parsed, expected_zxcv, expected_zxcv_len);
  66. // Check it is gone
  67. if (json_object_object_length(parsed) != 1)
  68. {
  69. printf("Should contain only one field (has %d) (error!)",
  70. json_object_object_length(parsed));
  71. return 1;
  72. }
  73. key_present = json_object_object_get_ex_len(parsed, expected_biff, expected_biff_len, &val);
  74. if (!key_present || !json_object_is_type(val, json_type_null))
  75. {
  76. printf("The key \"%s\" should be present and should be NULL (error!)\n",
  77. expected_biff_printable);
  78. return 1;
  79. }
  80. key_present = json_object_object_get_ex_len(parsed, expected_zxcv, expected_zxcv_len, &val);
  81. if (key_present)
  82. {
  83. printf("The key \"%s\" should not be present (error!)\n", expected_zxcv_printable);
  84. return 1;
  85. }
  86. printf("Key deleted properly\n");
  87. json_object_put(parsed);
  88. printf("PASS\n");
  89. return 0;
  90. }