tests: Ensure that failed parsing returns invalid items

This commit is contained in:
Max Bruckner 2017-02-15 21:12:10 +01:00
parent c6e1a281f9
commit 5986edba1d
4 changed files with 29 additions and 3 deletions

View File

@ -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.")
@ -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

View File

@ -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)

View File

@ -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)
@ -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)

View File

@ -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)
{
@ -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)