cJSON/tests/parse_string.c

136 lines
4.5 KiB
C
Raw Normal View History

2017-02-04 04:12:10 +03:00
/*
Copyright (c) 2009-2017 Dave Gamble and cJSON contributors
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "unity/examples/unity_config.h"
#include "unity/src/unity.h"
#include "common.h"
2017-02-04 04:12:10 +03:00
static cJSON item[1];
static void assert_is_string(cJSON *string_item)
{
TEST_ASSERT_NOT_NULL_MESSAGE(string_item, "Item is NULL.");
2017-02-15 21:57:54 +03:00
assert_not_in_list(string_item);
assert_has_no_child(string_item);
assert_has_type(string_item, cJSON_String);
assert_has_no_reference(string_item);
assert_has_no_const_string(string_item);
assert_has_valuestring(string_item);
assert_has_no_string(string_item);
2017-02-04 04:12:10 +03:00
}
static void assert_parse_string(const char *string, const char *expected)
{
2017-03-12 23:44:28 +03:00
parse_buffer buffer;
buffer.content = (const unsigned char*)string;
buffer.length = strlen(string) + sizeof("");
buffer.offset = 0;
TEST_ASSERT_TRUE_MESSAGE(parse_string(item, &buffer, &global_hooks), "Couldn't parse string.");
2017-02-04 04:12:10 +03:00
assert_is_string(item);
TEST_ASSERT_EQUAL_STRING_MESSAGE(expected, item->valuestring, "The parsed result isn't as expected.");
global_hooks.deallocate(item->valuestring);
2017-02-04 04:12:10 +03:00
item->valuestring = NULL;
}
2017-03-12 23:44:28 +03:00
static void assert_not_parse_string(const char * const string)
{
parse_buffer buffer;
buffer.content = (const unsigned char*)string;
buffer.length = strlen(string) + sizeof("");
buffer.offset = 0;
TEST_ASSERT_FALSE_MESSAGE(parse_string(item, &buffer, &global_hooks), "Malformed string should not be accepted.");
2017-03-12 23:44:28 +03:00
assert_is_invalid(item);
}
2017-02-15 21:57:54 +03:00
2017-02-04 04:12:10 +03:00
static void parse_string_should_parse_strings(void)
{
assert_parse_string("\"\"", "");
assert_parse_string(
"\" !\\\"#$%&'()*+,-./\\/0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\\\]^_'abcdefghijklmnopqrstuvwxyz{|}~\"",
" !\"#$%&'()*+,-.//0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_'abcdefghijklmnopqrstuvwxyz{|}~");
assert_parse_string(
"\"\\\"\\\\\\/\\b\\f\\n\\r\\t\\u20AC\\u732b\"",
"\"\\/\b\f\n\r\t€猫");
reset(item);
2017-02-04 04:12:10 +03:00
assert_parse_string("\"\b\f\n\r\t\"", "\b\f\n\r\t");
reset(item);
2017-02-04 04:12:10 +03:00
}
static void parse_string_should_parse_utf16_surrogate_pairs(void)
{
assert_parse_string("\"\\uD83D\\udc31\"", "🐱");
reset(item);
2017-02-04 04:12:10 +03:00
}
static void parse_string_should_not_parse_non_strings(void)
{
2017-02-15 21:57:54 +03:00
assert_not_parse_string("this\" is not a string\"");
reset(item);
2017-02-15 21:57:54 +03:00
assert_not_parse_string("");
reset(item);
2017-02-04 04:12:10 +03:00
}
static void parse_string_should_not_parse_invalid_backslash(void)
{
2017-02-15 21:57:54 +03:00
assert_not_parse_string("Abcdef\\123");
reset(item);
2017-02-15 21:57:54 +03:00
assert_not_parse_string("Abcdef\\e23");
reset(item);
2017-02-04 04:12:10 +03:00
}
static void parse_string_should_not_overflow_with_closing_backslash(void)
{
2017-02-15 21:57:54 +03:00
assert_not_parse_string("\"000000000000000000\\");
reset(item);
2017-02-04 04:12:10 +03:00
}
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);
2017-02-04 04:12:10 +03:00
}
int main(void)
{
/* initialize cJSON item and error pointer */
memset(item, 0, sizeof(cJSON));
UNITY_BEGIN();
RUN_TEST(parse_string_should_parse_strings);
RUN_TEST(parse_string_should_parse_utf16_surrogate_pairs);
RUN_TEST(parse_string_should_not_parse_non_strings);
RUN_TEST(parse_string_should_not_parse_invalid_backslash);
RUN_TEST(parse_string_should_parse_bug_94);
RUN_TEST(parse_string_should_not_overflow_with_closing_backslash);
return UNITY_END();
}