Browse Source

Merge branch 'tests' of https://github.com/RyDroid/json-c into RyDroid-tests

Conflicts:
	tests/test_util_file.c
tags/json-c-0.13-20171207
Eric Haszlakiewicz 9 years ago
parent
commit
61cd433131
7 changed files with 82 additions and 56 deletions
  1. +0
    -2
      tests/test-defs.sh
  2. +4
    -4
      tests/test1.c
  3. +3
    -2
      tests/test2.c
  4. +7
    -7
      tests/test4.c
  5. +11
    -11
      tests/test_null.c
  6. +16
    -16
      tests/test_parse.c
  7. +41
    -14
      tests/test_util_file.c

+ 0
- 2
tests/test-defs.sh View File

@@ -124,5 +124,3 @@ run_output_test()


return $err return $err
} }



+ 4
- 4
tests/test1.c View File

@@ -98,14 +98,14 @@ void test_array_del_idx()
rc, json_object_to_json_string(my_array)); rc, json_object_to_json_string(my_array));
} }


// One more time, with the empty array:
/* One more time, with the empty array: */
rc = json_object_array_del_idx(my_array, 0, 1); rc = json_object_array_del_idx(my_array, 0, 1);
printf("after del_idx(0,1)=%d, my_array.to_string()=%s\n", printf("after del_idx(0,1)=%d, my_array.to_string()=%s\n",
rc, json_object_to_json_string(my_array)); rc, json_object_to_json_string(my_array));


json_object_put(my_array); json_object_put(my_array);


// Delete all array indexes at once
/* Delete all array indexes at once */
my_array = make_array(); my_array = make_array();
rc = json_object_array_del_idx(my_array, 0, orig_array_len); rc = json_object_array_del_idx(my_array, 0, orig_array_len);
printf("after del_idx(0,%llu)=%d, my_array.to_string()=%s\n", printf("after del_idx(0,%llu)=%d, my_array.to_string()=%s\n",
@@ -113,7 +113,7 @@ void test_array_del_idx()


json_object_put(my_array); json_object_put(my_array);


// Delete *more* than all array indexes at once
/* Delete *more* than all array indexes at once */
my_array = make_array(); my_array = make_array();
rc = json_object_array_del_idx(my_array, 0, orig_array_len + 1); rc = json_object_array_del_idx(my_array, 0, orig_array_len + 1);
printf("after del_idx(0,%llu)=%d, my_array.to_string()=%s\n", printf("after del_idx(0,%llu)=%d, my_array.to_string()=%s\n",
@@ -233,5 +233,5 @@ int main(int argc, char **argv)
json_object_put(my_object); json_object_put(my_object);
json_object_put(my_array); json_object_put(my_array);


return 0;
return EXIT_SUCCESS;
} }

+ 3
- 2
tests/test2.c View File

@@ -26,9 +26,10 @@ int main(int argc, char **argv)
sflags = parse_flags(argc, argv); sflags = parse_flags(argc, argv);
#endif #endif


new_obj = json_tokener_parse("/* more difficult test case */ { \"glossary\": { \"title\": \"example glossary\", \"GlossDiv\": { \"title\": \"S\", \"GlossList\": [ { \"ID\": \"SGML\", \"SortAs\": \"SGML\", \"GlossTerm\": \"Standard Generalized Markup Language\", \"Acronym\": \"SGML\", \"Abbrev\": \"ISO 8879:1986\", \"GlossDef\": \"A meta-markup language, used to create markup languages such as DocBook.\", \"GlossSeeAlso\": [\"GML\", \"XML\", \"markup\"] } ] } } }");
new_obj = json_tokener_parse("/* more difficult test case */"
"{ \"glossary\": { \"title\": \"example glossary\", \"GlossDiv\": { \"title\": \"S\", \"GlossList\": [ { \"ID\": \"SGML\", \"SortAs\": \"SGML\", \"GlossTerm\": \"Standard Generalized Markup Language\", \"Acronym\": \"SGML\", \"Abbrev\": \"ISO 8879:1986\", \"GlossDef\": \"A meta-markup language, used to create markup languages such as DocBook.\", \"GlossSeeAlso\": [\"GML\", \"XML\", \"markup\"] } ] } } }");
printf("new_obj.to_string()=%s\n", json_object_to_json_string(new_obj)); printf("new_obj.to_string()=%s\n", json_object_to_json_string(new_obj));
json_object_put(new_obj); json_object_put(new_obj);


return 0;
return EXIT_SUCCESS;
} }

+ 7
- 7
tests/test4.c View File

@@ -10,7 +10,7 @@
#include "json_object.h" #include "json_object.h"
#include "json_tokener.h" #include "json_tokener.h"


void print_hex( const char* s)
void print_hex(const char* s)
{ {
const char *iter = s; const char *iter = s;
unsigned char ch; unsigned char ch;
@@ -21,10 +21,10 @@ void print_hex( const char* s)
else else
printf( ","); printf( ",");
} }
printf("\n");
putchar('\n');
} }


int main()
int main(void)
{ {
const char *input = "\"\\ud840\\udd26,\\ud840\\udd27,\\ud800\\udd26,\\ud800\\udd27\""; const char *input = "\"\\ud840\\udd26,\\ud840\\udd27,\\ud800\\udd26,\\ud800\\udd27\"";
const char *expected = "\xF0\xA0\x84\xA6,\xF0\xA0\x84\xA7,\xF0\x90\x84\xA6,\xF0\x90\x84\xA7"; const char *expected = "\xF0\xA0\x84\xA6,\xF0\xA0\x84\xA7,\xF0\x90\x84\xA6,\xF0\x90\x84\xA7";
@@ -38,14 +38,14 @@ int main()
if (strings_match) if (strings_match)
{ {
printf("JSON parse result is correct: %s\n", unjson); printf("JSON parse result is correct: %s\n", unjson);
printf("PASS\n");
puts("PASS");
} else { } else {
printf("JSON parse result doesn't match expected string\n"); printf("JSON parse result doesn't match expected string\n");
printf("expected string bytes: "); printf("expected string bytes: ");
print_hex( expected);
print_hex(expected);
printf("parsed string bytes: "); printf("parsed string bytes: ");
print_hex( unjson);
printf("FAIL\n");
print_hex(unjson);
puts("FAIL");
retval = 1; retval = 1;
} }
json_object_put(parse_result); json_object_put(parse_result);


+ 11
- 11
tests/test_null.c View File

@@ -1,6 +1,6 @@
/* /*
* Tests if binary strings are supported.
*/
* Tests if binary strings are supported.
*/


#include <stdio.h> #include <stdio.h>
#include <string.h> #include <string.h>
@@ -10,9 +10,9 @@
#include "json_object.h" #include "json_object.h"
#include "json_tokener.h" #include "json_tokener.h"


int main()
int main(void)
{ {
// this test has a space after the null character. check that it's still included
/* this test has a space after the null character. check that it's still included */
const char *input = " \0 "; const char *input = " \0 ";
const char *expected = "\" \\u0000 \""; const char *expected = "\" \\u0000 \"";
struct json_object *string = json_object_new_string_len(input, 3); struct json_object *string = json_object_new_string_len(input, 3);
@@ -23,14 +23,14 @@ int main()
if (strings_match) if (strings_match)
{ {
printf("JSON write result is correct: %s\n", json); printf("JSON write result is correct: %s\n", json);
printf("PASS\n");
puts("PASS");
} else { } else {
printf("JSON write result doesn't match expected string\n");
puts("JSON write result doesn't match expected string");
printf("expected string: "); printf("expected string: ");
printf("%s\n", expected);
puts(expected);
printf("parsed string: "); printf("parsed string: ");
printf("%s\n", json);
printf("FAIL\n");
puts(json);
puts("FAIL");
retval=1; retval=1;
} }
json_object_put(string); json_object_put(string);
@@ -46,12 +46,12 @@ int main()
{ {
printf("%s%d", (ii ? ", " : ""), (int)parsed_cstr[ii]); printf("%s%d", (ii ? ", " : ""), (int)parsed_cstr[ii]);
} }
printf("]\n");
puts("]");
json_object_put(parsed_str); json_object_put(parsed_str);
} }
else else
{ {
printf("ERROR: failed to parse\n");
puts("ERROR: failed to parse");
} }
return retval; return retval;
} }

+ 16
- 16
tests/test_parse.c View File

@@ -11,16 +11,17 @@ static void test_basic_parse(void);
static void test_verbose_parse(void); static void test_verbose_parse(void);
static void test_incremental_parse(void); static void test_incremental_parse(void);


int main(int argc, char **argv)
int main(void)
{ {
MC_SET_DEBUG(1); MC_SET_DEBUG(1);


static const char separator[] = "==================================";
test_basic_parse(); test_basic_parse();
printf("==================================\n");
puts(separator);
test_verbose_parse(); test_verbose_parse();
printf("==================================\n");
puts(separator);
test_incremental_parse(); test_incremental_parse();
printf("==================================\n");
puts(separator);
} }


static void test_basic_parse() static void test_basic_parse()
@@ -173,7 +174,7 @@ static void test_verbose_parse()
/* b/c the string starts with 'f' parsing return a boolean error */ /* b/c the string starts with 'f' parsing return a boolean error */
assert (error == json_tokener_error_parse_boolean); assert (error == json_tokener_error_parse_boolean);


printf("json_tokener_parse_versbose() OK\n");
puts("json_tokener_parse_versbose() OK");
} }


struct incremental_step { struct incremental_step {
@@ -263,7 +264,7 @@ static void test_incremental_parse()
string_to_parse = "{ \"foo"; /* } */ string_to_parse = "{ \"foo"; /* } */
printf("json_tokener_parse(%s) ... ", string_to_parse); printf("json_tokener_parse(%s) ... ", string_to_parse);
new_obj = json_tokener_parse(string_to_parse); new_obj = json_tokener_parse(string_to_parse);
if (new_obj == NULL) printf("got error as expected\n");
if (new_obj == NULL) puts("got error as expected");


/* test incremental parsing in various forms */ /* test incremental parsing in various forms */
tok = json_tokener_new(); tok = json_tokener_new();
@@ -293,17 +294,18 @@ static void test_incremental_parse()
{ {
if (new_obj != NULL) if (new_obj != NULL)
printf("ERROR: invalid object returned: %s\n", printf("ERROR: invalid object returned: %s\n",
json_object_to_json_string(new_obj));
json_object_to_json_string(new_obj));
else if (jerr != step->expected_error) else if (jerr != step->expected_error)
printf("ERROR: got wrong error: %s\n", printf("ERROR: got wrong error: %s\n",
json_tokener_error_desc(jerr));
json_tokener_error_desc(jerr));
else if (tok->char_offset != expected_char_offset) else if (tok->char_offset != expected_char_offset)
printf("ERROR: wrong char_offset %d != expected %d\n", printf("ERROR: wrong char_offset %d != expected %d\n",
tok->char_offset,
expected_char_offset);
tok->char_offset,
expected_char_offset);
else else
{ {
printf("OK: got correct error: %s\n", json_tokener_error_desc(jerr));
printf("OK: got correct error: %s\n",
json_tokener_error_desc(jerr));
this_step_ok = 1; this_step_ok = 1;
} }
} }
@@ -311,11 +313,11 @@ static void test_incremental_parse()
{ {
if (new_obj == NULL) if (new_obj == NULL)
printf("ERROR: expected valid object, instead: %s\n", printf("ERROR: expected valid object, instead: %s\n",
json_tokener_error_desc(jerr));
json_tokener_error_desc(jerr));
else if (tok->char_offset != expected_char_offset) else if (tok->char_offset != expected_char_offset)
printf("ERROR: wrong char_offset %d != expected %d\n", printf("ERROR: wrong char_offset %d != expected %d\n",
tok->char_offset,
expected_char_offset);
tok->char_offset,
expected_char_offset);
else else
{ {
printf("OK: got object of type [%s]: %s\n", printf("OK: got object of type [%s]: %s\n",
@@ -340,6 +342,4 @@ static void test_incremental_parse()
json_tokener_free(tok); json_tokener_free(tok);


printf("End Incremental Tests OK=%d ERROR=%d\n", num_ok, num_error); printf("End Incremental Tests OK=%d ERROR=%d\n", num_ok, num_error);

return;
} }

+ 41
- 14
tests/test_util_file.c View File

@@ -41,12 +41,12 @@ static void test_write_to_file()
if (rv == 0) if (rv == 0)
stat_and_cat(outfile); stat_and_cat(outfile);


printf("\n");
putchar('\n');


const char *outfile2 = "json2.out"; const char *outfile2 = "json2.out";
rv = json_object_to_file_ext(outfile2, jso, JSON_C_TO_STRING_PRETTY); rv = json_object_to_file_ext(outfile2, jso, JSON_C_TO_STRING_PRETTY);
printf("%s: json_object_to_file_ext(%s, jso, JSON_C_TO_STRING_PRETTY)=%d\n", printf("%s: json_object_to_file_ext(%s, jso, JSON_C_TO_STRING_PRETTY)=%d\n",
(rv == 0) ? "OK" : "FAIL", outfile2, rv);
(rv == 0) ? "OK" : "FAIL", outfile2, rv);
if (rv == 0) if (rv == 0)
stat_and_cat(outfile2); stat_and_cat(outfile2);
} }
@@ -57,19 +57,28 @@ static void stat_and_cat(const char *file)
int d = open(file, O_RDONLY, 0600); int d = open(file, O_RDONLY, 0600);
if (d < 0) if (d < 0)
{ {
printf("FAIL: unable to open %s: %s\n", file, strerror(errno));
printf("FAIL: unable to open %s: %s\n",
file, strerror(errno));
return; return;
} }
if (fstat(d, &sb) < 0) if (fstat(d, &sb) < 0)
{ {
printf("FAIL: unable to stat %s: %s\n", file, strerror(errno));
printf("FAIL: unable to stat %s: %s\n",
file, strerror(errno));
close(d); close(d);
return; return;
} }
char *buf = malloc(sb.st_size + 1); char *buf = malloc(sb.st_size + 1);
if(!buf)
{
printf("FAIL: unable to allocate memory\n");
close(d);
return;
}
if (read(d, buf, sb.st_size) < sb.st_size) if (read(d, buf, sb.st_size) < sb.st_size)
{ {
printf("FAIL: unable to read all of %s: %s\n", file, strerror(errno));
printf("FAIL: unable to read all of %s: %s\n",
file, strerror(errno));
free(buf); free(buf);
close(d); close(d);
return; return;
@@ -87,8 +96,11 @@ int main(int argc, char **argv)
const char *testdir; const char *testdir;
if (argc < 2) if (argc < 2)
{ {
fprintf(stderr, "Usage: %s <testdir>\n <testdir> is the location of input files\n", argv[0]);
exit(1);
fprintf(stderr,
"Usage: %s <testdir>\n"
" <testdir> is the location of input files\n",
argv[0]);
return EXIT_FAILURE;
} }
testdir = argv[1]; testdir = argv[1];


@@ -96,6 +108,7 @@ int main(int argc, char **argv)
test_read_nonexistant(); test_read_nonexistant();
test_read_closed(); test_read_closed();
test_write_to_file(); test_write_to_file();
return EXIT_SUCCESS;
} }


static void test_read_valid_with_fd(const char *testdir) static void test_read_valid_with_fd(const char *testdir)
@@ -105,18 +118,23 @@ static void test_read_valid_with_fd(const char *testdir)
int d = open(filename, O_RDONLY, 0); int d = open(filename, O_RDONLY, 0);
if (d < 0) if (d < 0)
{ {
fprintf(stderr, "FAIL: unable to open %s: %s\n", filename, strerror(errno));
exit(1);
fprintf(stderr,
"FAIL: unable to open %s: %s\n",
filename, strerror(errno));
exit(EXIT_FAILURE);
} }
json_object *jso = json_object_from_fd(d); json_object *jso = json_object_from_fd(d);
if (jso != NULL) if (jso != NULL)
{ {
printf("OK: json_object_from_fd(%s)=%s\n", filename, json_object_to_json_string(jso));
printf("OK: json_object_from_fd(%s)=%s\n",
filename, json_object_to_json_string(jso));
json_object_put(jso); json_object_put(jso);
} }
else else
{ {
fprintf(stderr, "FAIL: unable to parse contents of %s: %s\n", filename, json_util_get_last_err());
fprintf(stderr,
"FAIL: unable to parse contents of %s: %s\n",
filename, json_util_get_last_err());
} }
close(d); close(d);
} }
@@ -128,7 +146,8 @@ static void test_read_nonexistant()
json_object *jso = json_object_from_file(filename); json_object *jso = json_object_from_file(filename);
if (jso != NULL) if (jso != NULL)
{ {
printf("FAIL: json_object_from_file(%s) returned 0x%lx when NULL expected\n", filename, (unsigned long)jso);
printf("FAIL: json_object_from_file(%s) returned 0x%lx when NULL expected\n",
filename, (unsigned long)jso);
json_object_put(jso); json_object_put(jso);
} }
else else
@@ -142,15 +161,23 @@ static void test_read_closed()
{ {
// Test reading from a closed fd // Test reading from a closed fd
int d = open("/dev/null", O_RDONLY, 0); int d = open("/dev/null", O_RDONLY, 0);
if(d < 0)
{
puts("FAIL: unable to open");
}
close(d); close(d);

json_object *jso = json_object_from_fd(d); json_object *jso = json_object_from_fd(d);
if (jso != NULL) if (jso != NULL)
{ {
printf("FAIL: read from closed fd returning non-NULL: 0x%lx\n", (unsigned long)jso);
printf("FAIL: read from closed fd returning non-NULL: 0x%lx\n",
(unsigned long) jso);
fflush(stdout); fflush(stdout);
printf(" jso=%s\n", json_object_to_json_string(jso)); printf(" jso=%s\n", json_object_to_json_string(jso));
json_object_put(jso); json_object_put(jso);
return; return;
} }
printf("OK: json_object_from_fd(closed_fd), expecting NULL, EBADF, got:0x%lx, %s\n", (unsigned long)jso, json_util_get_last_err());
printf("OK: json_object_from_fd(closed_fd), "
"expecting NULL, EBADF, got:0x%lx, %s\n",
(unsigned long)jso, json_util_get_last_err());
} }

Loading…
Cancel
Save