test_null.c 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. /*
  2. * Tests if binary strings are supported.
  3. */
  4. #include <stdio.h>
  5. #include <string.h>
  6. #include "config.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 a space after the null character. check that it's still included */
  13. const char *input = " \0 ";
  14. const char *expected = "\" \\u0000 \"";
  15. struct json_object *string = json_object_new_string_len(input, 3);
  16. const char *json = json_object_to_json_string(string);
  17. int strings_match = !strcmp( expected, json);
  18. int retval = 0;
  19. if (strings_match)
  20. {
  21. printf("JSON write result is correct: %s\n", json);
  22. puts("PASS");
  23. } else {
  24. puts("JSON write result doesn't match expected string");
  25. printf("expected string: ");
  26. puts(expected);
  27. printf("parsed string: ");
  28. puts(json);
  29. puts("FAIL");
  30. retval=1;
  31. }
  32. json_object_put(string);
  33. struct json_object *parsed_str = json_tokener_parse(expected);
  34. if (parsed_str)
  35. {
  36. int parsed_len = json_object_get_string_len(parsed_str);
  37. const char *parsed_cstr = json_object_get_string(parsed_str);
  38. int ii;
  39. printf("Re-parsed object string len=%d, chars=[", parsed_len);
  40. for (ii = 0; ii < parsed_len ; ii++)
  41. {
  42. printf("%s%d", (ii ? ", " : ""), (int)parsed_cstr[ii]);
  43. }
  44. puts("]");
  45. json_object_put(parsed_str);
  46. }
  47. else
  48. {
  49. puts("ERROR: failed to parse");
  50. }
  51. return retval;
  52. }