Browse Source

Add additional tests and fix `test_null_keys_get`

pull/715/head
HexTheDragon 4 years ago
parent
commit
0a8f719eda
8 changed files with 129 additions and 1 deletions
  1. +2
    -0
      tests/CMakeLists.txt
  2. +100
    -0
      tests/test_null_keys_del.c
  3. +5
    -0
      tests/test_null_keys_del.expected
  4. +1
    -0
      tests/test_null_keys_del.test
  5. +1
    -1
      tests/test_null_keys_get.c
  6. +18
    -0
      tests/test_null_keys_print.c
  7. +1
    -0
      tests/test_null_keys_print.expected
  8. +1
    -0
      tests/test_null_keys_print.test

+ 2
- 0
tests/CMakeLists.txt View File

@@ -28,6 +28,8 @@ set(ALL_TEST_NAMES
test_null test_null
test_null_keys_add test_null_keys_add
test_null_keys_get test_null_keys_get
test_null_keys_del
test_null_keys_print
test_parse test_parse
test_parse_int64 test_parse_int64
test_printbuf test_printbuf


+ 100
- 0
tests/test_null_keys_del.c View File

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

#include "config.h"
#include <stdio.h>
#include <string.h>

#include "json_inttypes.h"
#include "json_object.h"
#include "json_tokener.h"

int main(void)
{
/* this test has embedded null characters in the key and value.
* check that it's still included after deserializing. */
const char *input = "{ \"biff\\u0000\": null, \"\\u0000zxcvbnm\": 178 }";
const char *expected_biff = "biff\0";
const int expected_biff_len = 5;
const char *expected_biff_printable = "biff\\u0000";
const char *expected_zxcv = "\0zxcvbnm";
const int expected_zxcv_len = 8;
const char *expected_zxcv_printable = "\\u0000zxcvbnm";

struct json_object *parsed = json_tokener_parse(input);
struct json_object *val;
json_bool key_present;

printf("Parsed input: %s\n", input);
printf("Result is ");
if (parsed == NULL)
{
printf("NULL (error!)\n");
return 1; // Error somewhere
}
else if (!json_object_is_type(parsed, json_type_object))
{
printf("not `json_type_object` (error!)\n");
return 1; // Error somewhere
}
else
{
printf("`json_type_object`\n");
}

// Check nothing odd happened in parsing
if (json_object_object_length(parsed) != 2)
{
printf("Should contain two fields (has %d) (error!)",
json_object_object_length(parsed));
return 1;
}

key_present = json_object_object_get_ex_len(parsed, expected_biff, expected_biff_len, &val);
if (!key_present || !json_object_is_type(val, json_type_null))
{
printf("The key \"%s\" should be present and should be NULL (error!)\n",
expected_biff_printable);
return 1;
}
key_present = json_object_object_get_ex_len(parsed, expected_zxcv, expected_zxcv_len, &val);
if (!key_present || !json_object_is_type(val, json_type_int) ||
json_object_get_int(val) != 178)
{
printf("The key \"%s\" should be present and should be 178 (error!)\n",
expected_zxcv_printable);
return 1;
}
printf("Expected keys (\"%s\" and \"%s\") present with expected values\n",
expected_biff_printable, expected_zxcv_printable);

// Delete one key
json_object_object_del_len(parsed, expected_zxcv, expected_zxcv_len);

// Check it is gone
if (json_object_object_length(parsed) != 1)
{
printf("Should contain only one field (has %d) (error!)",
json_object_object_length(parsed));
return 1;
}
key_present = json_object_object_get_ex_len(parsed, expected_biff, expected_biff_len, &val);
if (!key_present || !json_object_is_type(val, json_type_null))
{
printf("The key \"%s\" should be present and should be NULL (error!)\n",
expected_biff_printable);
return 1;
}
key_present = json_object_object_get_ex_len(parsed, expected_zxcv, expected_zxcv_len, &val);
if (key_present)
{
printf("The key \"%s\" should not be present (error!)\n", expected_zxcv_printable);
return 1;
}
printf("Key deleted properly\n");

json_object_put(parsed);
printf("PASS\n");
return 0;
}

+ 5
- 0
tests/test_null_keys_del.expected View File

@@ -0,0 +1,5 @@
Parsed input: { "biff\u0000": null, "\u0000zxcvbnm": 178 }
Result is `json_type_object`
Expected keys ("biff\0" and "\0zxcvbnm") present with expected values
Key deleted properly
PASS

+ 1
- 0
tests/test_null_keys_del.test View File

@@ -0,0 +1 @@
test_basic.test

+ 1
- 1
tests/test_null_keys_get.c View File

@@ -19,7 +19,7 @@ int main(void)
const int expected_key_len = 7; const int expected_key_len = 7;
const char *expected_key_printable = "foo\\0bar"; const char *expected_key_printable = "foo\\0bar";
const char *expected_value = "qwerty\0asdf"; const char *expected_value = "qwerty\0asdf";
const int expected_value_len = 12;
const int expected_value_len = 11;
const char *expected_value_printable = "qwerty\\0asdf"; const char *expected_value_printable = "qwerty\\0asdf";


struct json_object *parsed = json_tokener_parse(input); struct json_object *parsed = json_tokener_parse(input);


+ 18
- 0
tests/test_null_keys_print.c View File

@@ -0,0 +1,18 @@
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "json.h"
#include "parse_flags.h"

int main(int argc, char **argv)
{
struct json_object *new_obj;

new_obj = json_tokener_parse("{ \"foo\\u0000bar\": \"qwerty\\u0000asdf\" }");
printf("new_obj.to_string()=%s\n", json_object_to_json_string(new_obj));
json_object_put(new_obj);

return EXIT_SUCCESS;
}

+ 1
- 0
tests/test_null_keys_print.expected View File

@@ -0,0 +1 @@
new_obj.to_string()={ "foo\u0000bar": "qwerty\u0000asdf" }

+ 1
- 0
tests/test_null_keys_print.test View File

@@ -0,0 +1 @@
test_basic.test

Loading…
Cancel
Save