mirror of
https://github.com/DaveGamble/cJSON.git
synced 2023-08-10 21:13:26 +03:00
113 lines
4.1 KiB
C
113 lines
4.1 KiB
C
/*
|
|
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"
|
|
|
|
static cJSON item[1];
|
|
|
|
static const unsigned char *error_pointer = NULL;
|
|
|
|
static void assert_is_string(cJSON *string_item)
|
|
{
|
|
TEST_ASSERT_NOT_NULL_MESSAGE(string_item, "Item is NULL.");
|
|
|
|
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);
|
|
}
|
|
|
|
static void assert_parse_string(const char *string, const char *expected)
|
|
{
|
|
TEST_ASSERT_NOT_NULL_MESSAGE(parse_string(item, (const unsigned char*)string, &error_pointer), "Couldn't parse string.");
|
|
assert_is_string(item);
|
|
TEST_ASSERT_EQUAL_STRING_MESSAGE(expected, item->valuestring, "The parsed result isn't as expected.");
|
|
cJSON_free(item->valuestring);
|
|
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")
|
|
|
|
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€猫");
|
|
assert_parse_string("\"\b\f\n\r\t\"", "\b\f\n\r\t");
|
|
}
|
|
|
|
static void parse_string_should_parse_utf16_surrogate_pairs(void)
|
|
{
|
|
assert_parse_string("\"\\uD83D\\udc31\"", "🐱");
|
|
}
|
|
|
|
static void parse_string_should_not_parse_non_strings(void)
|
|
{
|
|
assert_not_parse_string("this\" is not a string\"");
|
|
assert_not_parse_string("");
|
|
}
|
|
|
|
static void parse_string_should_not_parse_invalid_backslash(void)
|
|
{
|
|
assert_not_parse_string("Abcdef\\123");
|
|
assert_not_parse_string("Abcdef\\e23");
|
|
}
|
|
|
|
static void parse_string_should_not_overflow_with_closing_backslash(void)
|
|
{
|
|
assert_not_parse_string("\"000000000000000000\\");
|
|
}
|
|
|
|
static void parse_string_should_parse_bug_94(void)
|
|
{
|
|
const char string[] = "\"~!@\\\\#$%^&*()\\\\\\\\-\\\\+{}[]:\\\\;\\\\\\\"\\\\<\\\\>?/.,DC=ad,DC=com\"";
|
|
assert_parse_string(string, "~!@\\#$%^&*()\\\\-\\+{}[]:\\;\\\"\\<\\>?/.,DC=ad,DC=com");
|
|
}
|
|
|
|
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();
|
|
}
|