Files
json-c/tests/test_null.c

60 lines
1.3 KiB
C
Raw Permalink Normal View History

/*
2016-08-08 15:11:19 +02:00
* Tests if binary strings are supported.
*/
2020-03-28 10:25:00 +08:00
#include "config.h"
#include <stdio.h>
#include <string.h>
#include "json_inttypes.h"
#include "json_object.h"
#include "json_tokener.h"
2016-08-08 15:11:19 +02:00
int main(void)
{
2016-08-08 15:11:19 +02:00
/* this test has a space after the null character. check that it's still included */
const char *input = " \0 ";
const char *expected = "\" \\u0000 \"";
struct json_object *string = json_object_new_string_len(input, 3);
const char *json = json_object_to_json_string(string);
2020-03-28 10:25:00 +08:00
int strings_match = !strcmp(expected, json);
int retval = 0;
if (strings_match)
{
printf("JSON write result is correct: %s\n", json);
2016-08-08 15:11:19 +02:00
puts("PASS");
2020-03-28 10:25:00 +08:00
}
else
{
2016-08-08 15:11:19 +02:00
puts("JSON write result doesn't match expected string");
printf("expected string: ");
2016-08-08 15:11:19 +02:00
puts(expected);
printf("parsed string: ");
2016-08-08 15:11:19 +02:00
puts(json);
puts("FAIL");
2020-03-28 10:25:00 +08:00
retval = 1;
}
json_object_put(string);
struct json_object *parsed_str = json_tokener_parse(expected);
if (parsed_str)
{
int parsed_len = json_object_get_string_len(parsed_str);
const char *parsed_cstr = json_object_get_string(parsed_str);
int ii;
printf("Re-parsed object string len=%d, chars=[", parsed_len);
2020-03-28 10:25:00 +08:00
for (ii = 0; ii < parsed_len; ii++)
{
printf("%s%d", (ii ? ", " : ""), (int)parsed_cstr[ii]);
}
2016-08-08 15:11:19 +02:00
puts("]");
json_object_put(parsed_str);
}
else
{
2016-08-08 15:11:19 +02:00
puts("ERROR: failed to parse");
}
return retval;
}