1 /*
2 Copyright (c) 2009-2017 Dave Gamble and cJSON contributors
3
4 Permission is hereby granted, free of charge, to any person obtaining a copy
5 of this software and associated documentation files (the "Software"), to deal
6 in the Software without restriction, including without limitation the rights
7 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 copies of the Software, and to permit persons to whom the Software is
9 furnished to do so, subject to the following conditions:
10
11 The above copyright notice and this permission notice shall be included in
12 all copies or substantial portions of the Software.
13
14 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20 THE SOFTWARE.
21 */
22
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <string.h>
26
27 #include "unity/examples/unity_config.h"
28 #include "unity/src/unity.h"
29 #include "common.h"
30
31 static cJSON item[1];
32
assert_is_string(cJSON * string_item)33 static void assert_is_string(cJSON *string_item)
34 {
35 TEST_ASSERT_NOT_NULL_MESSAGE(string_item, "Item is NULL.");
36
37 assert_not_in_list(string_item);
38 assert_has_no_child(string_item);
39 assert_has_type(string_item, cJSON_String);
40 assert_has_no_reference(string_item);
41 assert_has_no_const_string(string_item);
42 assert_has_valuestring(string_item);
43 assert_has_no_string(string_item);
44 }
45
assert_parse_string(const char * string,const char * expected)46 static void assert_parse_string(const char *string, const char *expected)
47 {
48 parse_buffer buffer = { 0, 0, 0, 0, { 0, 0, 0 } };
49 buffer.content = (const unsigned char*)string;
50 buffer.length = strlen(string) + sizeof("");
51 buffer.hooks = global_hooks;
52
53 TEST_ASSERT_TRUE_MESSAGE(parse_string(item, &buffer), "Couldn't parse string.");
54 assert_is_string(item);
55 TEST_ASSERT_EQUAL_STRING_MESSAGE(expected, item->valuestring, "The parsed result isn't as expected.");
56 global_hooks.deallocate(item->valuestring);
57 item->valuestring = NULL;
58 }
59
assert_not_parse_string(const char * const string)60 static void assert_not_parse_string(const char * const string)
61 {
62 parse_buffer buffer = { 0, 0, 0, 0, { 0, 0, 0 } };
63 buffer.content = (const unsigned char*)string;
64 buffer.length = strlen(string) + sizeof("");
65 buffer.hooks = global_hooks;
66
67 TEST_ASSERT_FALSE_MESSAGE(parse_string(item, &buffer), "Malformed string should not be accepted.");
68 assert_is_invalid(item);
69 }
70
71
72
parse_string_should_parse_strings(void)73 static void parse_string_should_parse_strings(void)
74 {
75 assert_parse_string("\"\"", "");
76 assert_parse_string(
77 "\" !\\\"#$%&'()*+,-./\\/0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\\\]^_'abcdefghijklmnopqrstuvwxyz{|}~\"",
78 " !\"#$%&'()*+,-.//0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_'abcdefghijklmnopqrstuvwxyz{|}~");
79 assert_parse_string(
80 "\"\\\"\\\\\\/\\b\\f\\n\\r\\t\\u20AC\\u732b\"",
81 "\"\\/\b\f\n\r\t€猫");
82 reset(item);
83 assert_parse_string("\"\b\f\n\r\t\"", "\b\f\n\r\t");
84 reset(item);
85 }
86
parse_string_should_parse_utf16_surrogate_pairs(void)87 static void parse_string_should_parse_utf16_surrogate_pairs(void)
88 {
89 assert_parse_string("\"\\uD83D\\udc31\"", "");
90 reset(item);
91 }
92
parse_string_should_not_parse_non_strings(void)93 static void parse_string_should_not_parse_non_strings(void)
94 {
95 assert_not_parse_string("this\" is not a string\"");
96 reset(item);
97 assert_not_parse_string("");
98 reset(item);
99 }
100
parse_string_should_not_parse_invalid_backslash(void)101 static void parse_string_should_not_parse_invalid_backslash(void)
102 {
103 assert_not_parse_string("Abcdef\\123");
104 reset(item);
105 assert_not_parse_string("Abcdef\\e23");
106 reset(item);
107 }
108
parse_string_should_not_overflow_with_closing_backslash(void)109 static void parse_string_should_not_overflow_with_closing_backslash(void)
110 {
111 assert_not_parse_string("\"000000000000000000\\");
112 reset(item);
113 }
114
parse_string_should_parse_bug_94(void)115 static void parse_string_should_parse_bug_94(void)
116 {
117 const char string[] = "\"~!@\\\\#$%^&*()\\\\\\\\-\\\\+{}[]:\\\\;\\\\\\\"\\\\<\\\\>?/.,DC=ad,DC=com\"";
118 assert_parse_string(string, "~!@\\#$%^&*()\\\\-\\+{}[]:\\;\\\"\\<\\>?/.,DC=ad,DC=com");
119 reset(item);
120 }
121
main(void)122 int CJSON_CDECL main(void)
123 {
124 /* initialize cJSON item and error pointer */
125 memset(item, 0, sizeof(cJSON));
126
127 UNITY_BEGIN();
128 RUN_TEST(parse_string_should_parse_strings);
129 RUN_TEST(parse_string_should_parse_utf16_surrogate_pairs);
130 RUN_TEST(parse_string_should_not_parse_non_strings);
131 RUN_TEST(parse_string_should_not_parse_invalid_backslash);
132 RUN_TEST(parse_string_should_parse_bug_94);
133 RUN_TEST(parse_string_should_not_overflow_with_closing_backslash);
134 return UNITY_END();
135 }
136