Skip to content

Commit

Permalink
tests: Ensure that failed parsing returns invalid items
Browse files Browse the repository at this point in the history
  • Loading branch information
FSMaxB committed Feb 15, 2017
1 parent c6e1a28 commit 5986edb
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 3 deletions.
7 changes: 7 additions & 0 deletions tests/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@

extern void reset(cJSON *item);
extern char *read_file(const char *filename);
extern cjbool assert_is_invalid(cJSON *item);

/* assertion helper macros */
#define assert_has_type(item, item_type) TEST_ASSERT_BITS_MESSAGE(0xFF, item_type, item->type, "Item doesn't have expected type.")
Expand All @@ -41,5 +42,11 @@ extern char *read_file(const char *filename);
TEST_ASSERT_NULL_MESSAGE(item->prev, "Linked list previous pointer is not NULL.")
#define assert_has_child(item) TEST_ASSERT_NOT_NULL_MESSAGE(item->child, "Item doesn't have a child.")
#define assert_has_no_child(item) TEST_ASSERT_NULL_MESSAGE(item->child, "Item has a child.")
#define assert_is_invalid(item) \
assert_has_type(item, cJSON_Invalid);\
assert_not_in_list(item);\
assert_has_no_child(item);\
assert_has_no_string(item);\
assert_has_no_valuestring(item)

#endif
1 change: 1 addition & 0 deletions tests/parse_array.c
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ static void assert_is_array(cJSON *array_item)
static void assert_not_array(const char *json)
{
TEST_ASSERT_NULL(parse_array(item, (const unsigned char*)json, &error_pointer));
assert_is_invalid(item);
}

static void assert_parse_array(const char *json)
Expand Down
9 changes: 7 additions & 2 deletions tests/parse_object.c
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ static void assert_is_child(cJSON *child_item, const char *name, int type)
static void assert_not_object(const char *json)
{
TEST_ASSERT_NULL(parse_object(item, (const unsigned char*)json, &error_pointer));
assert_is_invalid(item);
reset(item);
}

static void assert_parse_object(const char *json)
Expand All @@ -66,9 +68,12 @@ static void assert_parse_object(const char *json)
static void parse_object_should_parse_empty_objects(void)
{
assert_parse_object("{}");
TEST_ASSERT_NULL(item->child);
assert_has_no_child(item);
reset(item);

assert_parse_object("{\n\t}");
TEST_ASSERT_NULL(item->child);
assert_has_no_child(item);
reset(item);
}

static void parse_array_should_parse_arrays_with_one_element(void)
Expand Down
15 changes: 14 additions & 1 deletion tests/parse_string.c
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,11 @@ static void assert_parse_string(const char *string, const char *expected)
item->valuestring = NULL;
}

#define assert_not_parse_string(string) TEST_ASSERT_NULL_MESSAGE(parse_string(item, (const unsigned char*)string, &error_pointer), "Malformed string should not be accepted")
#define assert_not_parse_string(string) \
TEST_ASSERT_NULL_MESSAGE(parse_string(item, (const unsigned char*)string, &error_pointer), "Malformed string should not be accepted");\
assert_is_invalid(item)



static void parse_string_should_parse_strings(void)
{
Expand All @@ -65,35 +69,44 @@ static void parse_string_should_parse_strings(void)
assert_parse_string(
"\"\\\"\\\\\\/\\b\\f\\n\\r\\t\\u20AC\\u732b\"",
"\"\\/\b\f\n\r\t€猫");
reset(item);
assert_parse_string("\"\b\f\n\r\t\"", "\b\f\n\r\t");
reset(item);
}

static void parse_string_should_parse_utf16_surrogate_pairs(void)
{
assert_parse_string("\"\\uD83D\\udc31\"", "🐱");
reset(item);
}

static void parse_string_should_not_parse_non_strings(void)
{
assert_not_parse_string("this\" is not a string\"");
reset(item);
assert_not_parse_string("");
reset(item);
}

static void parse_string_should_not_parse_invalid_backslash(void)
{
assert_not_parse_string("Abcdef\\123");
reset(item);
assert_not_parse_string("Abcdef\\e23");
reset(item);
}

static void parse_string_should_not_overflow_with_closing_backslash(void)
{
assert_not_parse_string("\"000000000000000000\\");
reset(item);
}

static void parse_string_should_parse_bug_94(void)
{
const char string[] = "\"~!@\\\\#$%^&*()\\\\\\\\-\\\\+{}[]:\\\\;\\\\\\\"\\\\<\\\\>?/.,DC=ad,DC=com\"";
assert_parse_string(string, "~!@\\#$%^&*()\\\\-\\+{}[]:\\;\\\"\\<\\>?/.,DC=ad,DC=com");
reset(item);
}

int main(void)
Expand Down

0 comments on commit 5986edb

Please sign in to comment.