OLD | NEW |
1 // Copyright (c) 2016 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2016 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "platform/inspector_protocol/Parser.h" | 5 #include "platform/JSONParser.h" |
6 | 6 |
7 #include "platform/inspector_protocol/String16.h" | 7 #include "platform/JSONValues.h" |
8 #include "platform/inspector_protocol/Values.h" | |
9 #include "testing/gtest/include/gtest/gtest.h" | 8 #include "testing/gtest/include/gtest/gtest.h" |
| 9 #include "wtf/text/StringBuilder.h" |
10 | 10 |
11 namespace blink { | 11 namespace blink { |
12 namespace protocol { | |
13 | 12 |
14 TEST(ParserTest, Reading) | 13 TEST(JSONParserTest, Reading) |
15 { | 14 { |
16 protocol::Value* tmpValue; | 15 JSONValue* tmpValue; |
17 std::unique_ptr<protocol::Value> root; | 16 std::unique_ptr<JSONValue> root; |
18 std::unique_ptr<protocol::Value> root2; | 17 std::unique_ptr<JSONValue> root2; |
19 String16 strVal; | 18 String strVal; |
20 int intVal = 0; | 19 int intVal = 0; |
21 | 20 |
22 // some whitespace checking | 21 // some whitespace checking |
23 root = parseJSON(" null "); | 22 root = parseJSON(" null "); |
24 ASSERT_TRUE(root.get()); | 23 ASSERT_TRUE(root.get()); |
25 EXPECT_EQ(Value::TypeNull, root->type()); | 24 EXPECT_EQ(JSONValue::TypeNull, root->getType()); |
26 | 25 |
27 // Invalid JSON string | 26 // Invalid JSON string |
28 root = parseJSON("nu"); | 27 root = parseJSON("nu"); |
29 EXPECT_FALSE(root.get()); | 28 EXPECT_FALSE(root.get()); |
30 | 29 |
31 // Simple bool | 30 // Simple bool |
32 root = parseJSON("true "); | 31 root = parseJSON("true "); |
33 ASSERT_TRUE(root.get()); | 32 ASSERT_TRUE(root.get()); |
34 EXPECT_EQ(Value::TypeBoolean, root->type()); | 33 EXPECT_EQ(JSONValue::TypeBoolean, root->getType()); |
35 | 34 |
36 // Embedded comment | 35 // Embedded comment |
37 root = parseJSON("40 /*/"); | 36 root = parseJSON("40 /*/"); |
38 EXPECT_FALSE(root.get()); | 37 EXPECT_FALSE(root.get()); |
39 root = parseJSON("/* comment */null"); | 38 root = parseJSON("/* comment */null"); |
40 ASSERT_TRUE(root.get()); | 39 ASSERT_TRUE(root.get()); |
41 EXPECT_EQ(Value::TypeNull, root->type()); | 40 EXPECT_EQ(JSONValue::TypeNull, root->getType()); |
42 root = parseJSON("40 /* comment */"); | 41 root = parseJSON("40 /* comment */"); |
43 ASSERT_TRUE(root.get()); | 42 ASSERT_TRUE(root.get()); |
44 EXPECT_EQ(Value::TypeInteger, root->type()); | 43 EXPECT_EQ(JSONValue::TypeNumber, root->getType()); |
45 EXPECT_TRUE(root->asInteger(&intVal)); | 44 EXPECT_TRUE(root->asNumber(&intVal)); |
46 EXPECT_EQ(40, intVal); | 45 EXPECT_EQ(40, intVal); |
47 root = parseJSON("/**/ 40 /* multi-line\n comment */ // more comment"); | 46 root = parseJSON("/**/ 40 /* multi-line\n comment */ // more comment"); |
48 ASSERT_TRUE(root.get()); | 47 ASSERT_TRUE(root.get()); |
49 EXPECT_EQ(Value::TypeInteger, root->type()); | 48 EXPECT_EQ(JSONValue::TypeNumber, root->getType()); |
50 EXPECT_TRUE(root->asInteger(&intVal)); | 49 EXPECT_TRUE(root->asNumber(&intVal)); |
51 EXPECT_EQ(40, intVal); | 50 EXPECT_EQ(40, intVal); |
52 root = parseJSON("true // comment"); | 51 root = parseJSON("true // comment"); |
53 ASSERT_TRUE(root.get()); | 52 ASSERT_TRUE(root.get()); |
54 EXPECT_EQ(Value::TypeBoolean, root->type()); | 53 EXPECT_EQ(JSONValue::TypeBoolean, root->getType()); |
55 root = parseJSON("/* comment */\"sample string\""); | 54 root = parseJSON("/* comment */\"sample string\""); |
56 ASSERT_TRUE(root.get()); | 55 ASSERT_TRUE(root.get()); |
57 EXPECT_TRUE(root->asString(&strVal)); | 56 EXPECT_TRUE(root->asString(&strVal)); |
58 EXPECT_EQ("sample string", strVal); | 57 EXPECT_EQ("sample string", strVal); |
59 root = parseJSON("[1, /* comment, 2 ] */ \n 3]"); | 58 root = parseJSON("[1, /* comment, 2 ] */ \n 3]"); |
60 ASSERT_TRUE(root.get()); | 59 ASSERT_TRUE(root.get()); |
61 protocol::ListValue* list = ListValue::cast(root.get()); | 60 JSONArray* list = JSONArray::cast(root.get()); |
62 ASSERT_TRUE(list); | 61 ASSERT_TRUE(list); |
63 EXPECT_EQ(2u, list->size()); | 62 EXPECT_EQ(2u, list->length()); |
64 tmpValue = list->at(0); | 63 tmpValue = list->get(0); |
65 ASSERT_TRUE(tmpValue); | 64 ASSERT_TRUE(tmpValue); |
66 EXPECT_TRUE(tmpValue->asInteger(&intVal)); | 65 EXPECT_TRUE(tmpValue->asNumber(&intVal)); |
67 EXPECT_EQ(1, intVal); | 66 EXPECT_EQ(1, intVal); |
68 tmpValue = list->at(1); | 67 tmpValue = list->get(1); |
69 ASSERT_TRUE(tmpValue); | 68 ASSERT_TRUE(tmpValue); |
70 EXPECT_TRUE(tmpValue->asInteger(&intVal)); | 69 EXPECT_TRUE(tmpValue->asNumber(&intVal)); |
71 EXPECT_EQ(3, intVal); | 70 EXPECT_EQ(3, intVal); |
72 root = parseJSON("[1, /*a*/2, 3]"); | 71 root = parseJSON("[1, /*a*/2, 3]"); |
73 ASSERT_TRUE(root.get()); | 72 ASSERT_TRUE(root.get()); |
74 list = ListValue::cast(root.get()); | 73 list = JSONArray::cast(root.get()); |
75 ASSERT_TRUE(list); | 74 ASSERT_TRUE(list); |
76 EXPECT_EQ(3u, list->size()); | 75 EXPECT_EQ(3u, list->length()); |
77 root = parseJSON("/* comment **/42"); | 76 root = parseJSON("/* comment **/42"); |
78 ASSERT_TRUE(root.get()); | 77 ASSERT_TRUE(root.get()); |
79 EXPECT_EQ(Value::TypeInteger, root->type()); | 78 EXPECT_EQ(JSONValue::TypeNumber, root->getType()); |
80 EXPECT_TRUE(root->asInteger(&intVal)); | 79 EXPECT_TRUE(root->asNumber(&intVal)); |
81 EXPECT_EQ(42, intVal); | 80 EXPECT_EQ(42, intVal); |
82 root = parseJSON( | 81 root = parseJSON( |
83 "/* comment **/\n" | 82 "/* comment **/\n" |
84 "// */ 43\n" | 83 "// */ 43\n" |
85 "44"); | 84 "44"); |
86 ASSERT_TRUE(root.get()); | 85 ASSERT_TRUE(root.get()); |
87 EXPECT_EQ(Value::TypeInteger, root->type()); | 86 EXPECT_EQ(JSONValue::TypeNumber, root->getType()); |
88 EXPECT_TRUE(root->asInteger(&intVal)); | 87 EXPECT_TRUE(root->asNumber(&intVal)); |
89 EXPECT_EQ(44, intVal); | 88 EXPECT_EQ(44, intVal); |
90 | 89 |
91 // Test number formats | 90 // Test number formats |
92 root = parseJSON("43"); | 91 root = parseJSON("43"); |
93 ASSERT_TRUE(root.get()); | 92 ASSERT_TRUE(root.get()); |
94 EXPECT_EQ(Value::TypeInteger, root->type()); | 93 EXPECT_EQ(JSONValue::TypeNumber, root->getType()); |
95 EXPECT_TRUE(root->asInteger(&intVal)); | 94 EXPECT_TRUE(root->asNumber(&intVal)); |
96 EXPECT_EQ(43, intVal); | 95 EXPECT_EQ(43, intVal); |
97 | 96 |
98 // According to RFC4627, oct, hex, and leading zeros are invalid JSON. | 97 // According to RFC4627, oct, hex, and leading zeros are invalid JSON. |
99 root = parseJSON("043"); | 98 root = parseJSON("043"); |
100 EXPECT_FALSE(root.get()); | 99 EXPECT_FALSE(root.get()); |
101 root = parseJSON("0x43"); | 100 root = parseJSON("0x43"); |
102 EXPECT_FALSE(root.get()); | 101 EXPECT_FALSE(root.get()); |
103 root = parseJSON("00"); | 102 root = parseJSON("00"); |
104 EXPECT_FALSE(root.get()); | 103 EXPECT_FALSE(root.get()); |
105 | 104 |
106 // Test 0 (which needs to be special cased because of the leading zero | 105 // Test 0 (which needs to be special cased because of the leading zero |
107 // clause). | 106 // clause). |
108 root = parseJSON("0"); | 107 root = parseJSON("0"); |
109 ASSERT_TRUE(root.get()); | 108 ASSERT_TRUE(root.get()); |
110 EXPECT_EQ(Value::TypeInteger, root->type()); | 109 EXPECT_EQ(JSONValue::TypeNumber, root->getType()); |
111 intVal = 1; | 110 intVal = 1; |
112 EXPECT_TRUE(root->asInteger(&intVal)); | 111 EXPECT_TRUE(root->asNumber(&intVal)); |
113 EXPECT_EQ(0, intVal); | 112 EXPECT_EQ(0, intVal); |
114 | 113 |
115 // Numbers that overflow ints should succeed, being internally promoted to | 114 // Numbers that overflow ints should succeed, being internally promoted to |
116 // storage as doubles | 115 // storage as doubles |
117 root = parseJSON("2147483648"); | 116 root = parseJSON("2147483648"); |
118 ASSERT_TRUE(root.get()); | 117 ASSERT_TRUE(root.get()); |
119 double doubleVal; | 118 double doubleVal; |
120 EXPECT_EQ(Value::TypeDouble, root->type()); | 119 EXPECT_EQ(JSONValue::TypeNumber, root->getType()); |
121 doubleVal = 0.0; | 120 doubleVal = 0.0; |
122 EXPECT_TRUE(root->asDouble(&doubleVal)); | 121 EXPECT_TRUE(root->asNumber(&doubleVal)); |
123 EXPECT_DOUBLE_EQ(2147483648.0, doubleVal); | 122 EXPECT_DOUBLE_EQ(2147483648.0, doubleVal); |
124 root = parseJSON("-2147483649"); | 123 root = parseJSON("-2147483649"); |
125 ASSERT_TRUE(root.get()); | 124 ASSERT_TRUE(root.get()); |
126 EXPECT_EQ(Value::TypeDouble, root->type()); | 125 EXPECT_EQ(JSONValue::TypeNumber, root->getType()); |
127 doubleVal = 0.0; | 126 doubleVal = 0.0; |
128 EXPECT_TRUE(root->asDouble(&doubleVal)); | 127 EXPECT_TRUE(root->asNumber(&doubleVal)); |
129 EXPECT_DOUBLE_EQ(-2147483649.0, doubleVal); | 128 EXPECT_DOUBLE_EQ(-2147483649.0, doubleVal); |
130 | 129 |
131 // Parse a double | 130 // Parse a double |
132 root = parseJSON("43.1"); | 131 root = parseJSON("43.1"); |
133 ASSERT_TRUE(root.get()); | 132 ASSERT_TRUE(root.get()); |
134 EXPECT_EQ(Value::TypeDouble, root->type()); | 133 EXPECT_EQ(JSONValue::TypeNumber, root->getType()); |
135 doubleVal = 0.0; | 134 doubleVal = 0.0; |
136 EXPECT_TRUE(root->asDouble(&doubleVal)); | 135 EXPECT_TRUE(root->asNumber(&doubleVal)); |
137 EXPECT_DOUBLE_EQ(43.1, doubleVal); | 136 EXPECT_DOUBLE_EQ(43.1, doubleVal); |
138 | 137 |
139 root = parseJSON("4.3e-1"); | 138 root = parseJSON("4.3e-1"); |
140 ASSERT_TRUE(root.get()); | 139 ASSERT_TRUE(root.get()); |
141 EXPECT_EQ(Value::TypeDouble, root->type()); | 140 EXPECT_EQ(JSONValue::TypeNumber, root->getType()); |
142 doubleVal = 0.0; | 141 doubleVal = 0.0; |
143 EXPECT_TRUE(root->asDouble(&doubleVal)); | 142 EXPECT_TRUE(root->asNumber(&doubleVal)); |
144 EXPECT_DOUBLE_EQ(.43, doubleVal); | 143 EXPECT_DOUBLE_EQ(.43, doubleVal); |
145 | 144 |
146 root = parseJSON("2.1e0"); | 145 root = parseJSON("2.1e0"); |
147 ASSERT_TRUE(root.get()); | 146 ASSERT_TRUE(root.get()); |
148 EXPECT_EQ(Value::TypeDouble, root->type()); | 147 EXPECT_EQ(JSONValue::TypeNumber, root->getType()); |
149 doubleVal = 0.0; | 148 doubleVal = 0.0; |
150 EXPECT_TRUE(root->asDouble(&doubleVal)); | 149 EXPECT_TRUE(root->asNumber(&doubleVal)); |
151 EXPECT_DOUBLE_EQ(2.1, doubleVal); | 150 EXPECT_DOUBLE_EQ(2.1, doubleVal); |
152 | 151 |
153 root = parseJSON("2.1e+0001"); | 152 root = parseJSON("2.1e+0001"); |
154 ASSERT_TRUE(root.get()); | 153 ASSERT_TRUE(root.get()); |
155 EXPECT_EQ(Value::TypeInteger, root->type()); | 154 EXPECT_EQ(JSONValue::TypeNumber, root->getType()); |
156 doubleVal = 0.0; | 155 doubleVal = 0.0; |
157 EXPECT_TRUE(root->asDouble(&doubleVal)); | 156 EXPECT_TRUE(root->asNumber(&doubleVal)); |
158 EXPECT_DOUBLE_EQ(21.0, doubleVal); | 157 EXPECT_DOUBLE_EQ(21.0, doubleVal); |
159 | 158 |
160 root = parseJSON("0.01"); | 159 root = parseJSON("0.01"); |
161 ASSERT_TRUE(root.get()); | 160 ASSERT_TRUE(root.get()); |
162 EXPECT_EQ(Value::TypeDouble, root->type()); | 161 EXPECT_EQ(JSONValue::TypeNumber, root->getType()); |
163 doubleVal = 0.0; | 162 doubleVal = 0.0; |
164 EXPECT_TRUE(root->asDouble(&doubleVal)); | 163 EXPECT_TRUE(root->asNumber(&doubleVal)); |
165 EXPECT_DOUBLE_EQ(0.01, doubleVal); | 164 EXPECT_DOUBLE_EQ(0.01, doubleVal); |
166 | 165 |
167 root = parseJSON("1.00"); | 166 root = parseJSON("1.00"); |
168 ASSERT_TRUE(root.get()); | 167 ASSERT_TRUE(root.get()); |
169 EXPECT_EQ(Value::TypeInteger, root->type()); | 168 EXPECT_EQ(JSONValue::TypeNumber, root->getType()); |
170 doubleVal = 0.0; | 169 doubleVal = 0.0; |
171 EXPECT_TRUE(root->asDouble(&doubleVal)); | 170 EXPECT_TRUE(root->asNumber(&doubleVal)); |
172 EXPECT_DOUBLE_EQ(1.0, doubleVal); | 171 EXPECT_DOUBLE_EQ(1.0, doubleVal); |
173 | 172 |
174 // Fractional parts must have a digit before and after the decimal point. | 173 // Fractional parts must have a digit before and after the decimal point. |
175 root = parseJSON("1."); | 174 root = parseJSON("1."); |
176 EXPECT_FALSE(root.get()); | 175 EXPECT_FALSE(root.get()); |
177 root = parseJSON(".1"); | 176 root = parseJSON(".1"); |
178 EXPECT_FALSE(root.get()); | 177 EXPECT_FALSE(root.get()); |
179 root = parseJSON("1.e10"); | 178 root = parseJSON("1.e10"); |
180 EXPECT_FALSE(root.get()); | 179 EXPECT_FALSE(root.get()); |
181 | 180 |
(...skipping 17 matching lines...) Expand all Loading... |
199 | 198 |
200 // Invalid number formats | 199 // Invalid number formats |
201 root = parseJSON("4.3.1"); | 200 root = parseJSON("4.3.1"); |
202 EXPECT_FALSE(root.get()); | 201 EXPECT_FALSE(root.get()); |
203 root = parseJSON("4e3.1"); | 202 root = parseJSON("4e3.1"); |
204 EXPECT_FALSE(root.get()); | 203 EXPECT_FALSE(root.get()); |
205 | 204 |
206 // Test string parser | 205 // Test string parser |
207 root = parseJSON("\"hello world\""); | 206 root = parseJSON("\"hello world\""); |
208 ASSERT_TRUE(root.get()); | 207 ASSERT_TRUE(root.get()); |
209 EXPECT_EQ(Value::TypeString, root->type()); | 208 EXPECT_EQ(JSONValue::TypeString, root->getType()); |
210 EXPECT_TRUE(root->asString(&strVal)); | 209 EXPECT_TRUE(root->asString(&strVal)); |
211 EXPECT_EQ("hello world", strVal); | 210 EXPECT_EQ("hello world", strVal); |
212 | 211 |
213 // Empty string | 212 // Empty string |
214 root = parseJSON("\"\""); | 213 root = parseJSON("\"\""); |
215 ASSERT_TRUE(root.get()); | 214 ASSERT_TRUE(root.get()); |
216 EXPECT_EQ(Value::TypeString, root->type()); | 215 EXPECT_EQ(JSONValue::TypeString, root->getType()); |
217 EXPECT_TRUE(root->asString(&strVal)); | 216 EXPECT_TRUE(root->asString(&strVal)); |
218 EXPECT_EQ("", strVal); | 217 EXPECT_EQ("", strVal); |
219 | 218 |
220 // Test basic string escapes | 219 // Test basic string escapes |
221 root = parseJSON("\" \\\"\\\\\\/\\b\\f\\n\\r\\t\\v\""); | 220 root = parseJSON("\" \\\"\\\\\\/\\b\\f\\n\\r\\t\\v\""); |
222 ASSERT_TRUE(root.get()); | 221 ASSERT_TRUE(root.get()); |
223 EXPECT_EQ(Value::TypeString, root->type()); | 222 EXPECT_EQ(JSONValue::TypeString, root->getType()); |
224 EXPECT_TRUE(root->asString(&strVal)); | 223 EXPECT_TRUE(root->asString(&strVal)); |
225 EXPECT_EQ(" \"\\/\b\f\n\r\t\v", strVal); | 224 EXPECT_EQ(" \"\\/\b\f\n\r\t\v", strVal); |
226 | 225 |
227 // Test hex and unicode escapes including the null character. | 226 // Test hex and unicode escapes including the null character. |
228 root = parseJSON("\"\\x41\\x00\\u1234\""); | 227 root = parseJSON("\"\\x41\\x00\\u1234\""); |
229 EXPECT_FALSE(root.get()); | 228 EXPECT_FALSE(root.get()); |
230 | 229 |
231 // Test invalid strings | 230 // Test invalid strings |
232 root = parseJSON("\"no closing quote"); | 231 root = parseJSON("\"no closing quote"); |
233 EXPECT_FALSE(root.get()); | 232 EXPECT_FALSE(root.get()); |
234 root = parseJSON("\"\\z invalid escape char\""); | 233 root = parseJSON("\"\\z invalid escape char\""); |
235 EXPECT_FALSE(root.get()); | 234 EXPECT_FALSE(root.get()); |
236 root = parseJSON("\"not enough escape chars\\u123\""); | 235 root = parseJSON("\"not enough escape chars\\u123\""); |
237 EXPECT_FALSE(root.get()); | 236 EXPECT_FALSE(root.get()); |
238 root = parseJSON("\"extra backslash at end of input\\\""); | 237 root = parseJSON("\"extra backslash at end of input\\\""); |
239 EXPECT_FALSE(root.get()); | 238 EXPECT_FALSE(root.get()); |
240 | 239 |
241 // Basic array | 240 // Basic array |
242 root = parseJSON("[true, false, null]"); | 241 root = parseJSON("[true, false, null]"); |
243 ASSERT_TRUE(root.get()); | 242 ASSERT_TRUE(root.get()); |
244 EXPECT_EQ(Value::TypeArray, root->type()); | 243 EXPECT_EQ(JSONValue::TypeArray, root->getType()); |
245 list = ListValue::cast(root.get()); | 244 list = JSONArray::cast(root.get()); |
246 ASSERT_TRUE(list); | 245 ASSERT_TRUE(list); |
247 EXPECT_EQ(3U, list->size()); | 246 EXPECT_EQ(3U, list->length()); |
248 | 247 |
249 // Empty array | 248 // Empty array |
250 root = parseJSON("[]"); | 249 root = parseJSON("[]"); |
251 ASSERT_TRUE(root.get()); | 250 ASSERT_TRUE(root.get()); |
252 EXPECT_EQ(Value::TypeArray, root->type()); | 251 EXPECT_EQ(JSONValue::TypeArray, root->getType()); |
253 list = ListValue::cast(root.get()); | 252 list = JSONArray::cast(root.get()); |
254 ASSERT_TRUE(list); | 253 ASSERT_TRUE(list); |
255 EXPECT_EQ(0U, list->size()); | 254 EXPECT_EQ(0U, list->length()); |
256 | 255 |
257 // Nested arrays | 256 // Nested arrays |
258 root = parseJSON("[[true], [], [false, [], [null]], null]"); | 257 root = parseJSON("[[true], [], [false, [], [null]], null]"); |
259 ASSERT_TRUE(root.get()); | 258 ASSERT_TRUE(root.get()); |
260 EXPECT_EQ(Value::TypeArray, root->type()); | 259 EXPECT_EQ(JSONValue::TypeArray, root->getType()); |
261 list = ListValue::cast(root.get()); | 260 list = JSONArray::cast(root.get()); |
262 ASSERT_TRUE(list); | 261 ASSERT_TRUE(list); |
263 EXPECT_EQ(4U, list->size()); | 262 EXPECT_EQ(4U, list->length()); |
264 | 263 |
265 // Invalid, missing close brace. | 264 // Invalid, missing close brace. |
266 root = parseJSON("[[true], [], [false, [], [null]], null"); | 265 root = parseJSON("[[true], [], [false, [], [null]], null"); |
267 EXPECT_FALSE(root.get()); | 266 EXPECT_FALSE(root.get()); |
268 | 267 |
269 // Invalid, too many commas | 268 // Invalid, too many commas |
270 root = parseJSON("[true,, null]"); | 269 root = parseJSON("[true,, null]"); |
271 EXPECT_FALSE(root.get()); | 270 EXPECT_FALSE(root.get()); |
272 | 271 |
273 // Invalid, no commas | 272 // Invalid, no commas |
274 root = parseJSON("[true null]"); | 273 root = parseJSON("[true null]"); |
275 EXPECT_FALSE(root.get()); | 274 EXPECT_FALSE(root.get()); |
276 | 275 |
277 // Invalid, trailing comma | 276 // Invalid, trailing comma |
278 root = parseJSON("[true,]"); | 277 root = parseJSON("[true,]"); |
279 EXPECT_FALSE(root.get()); | 278 EXPECT_FALSE(root.get()); |
280 | 279 |
281 root = parseJSON("[true]"); | 280 root = parseJSON("[true]"); |
282 ASSERT_TRUE(root.get()); | 281 ASSERT_TRUE(root.get()); |
283 EXPECT_EQ(Value::TypeArray, root->type()); | 282 EXPECT_EQ(JSONValue::TypeArray, root->getType()); |
284 list = ListValue::cast(root.get()); | 283 list = JSONArray::cast(root.get()); |
285 ASSERT_TRUE(list); | 284 ASSERT_TRUE(list); |
286 EXPECT_EQ(1U, list->size()); | 285 EXPECT_EQ(1U, list->length()); |
287 tmpValue = list->at(0); | 286 tmpValue = list->get(0); |
288 ASSERT_TRUE(tmpValue); | 287 ASSERT_TRUE(tmpValue); |
289 EXPECT_EQ(Value::TypeBoolean, tmpValue->type()); | 288 EXPECT_EQ(JSONValue::TypeBoolean, tmpValue->getType()); |
290 bool boolValue = false; | 289 bool boolValue = false; |
291 EXPECT_TRUE(tmpValue->asBoolean(&boolValue)); | 290 EXPECT_TRUE(tmpValue->asBoolean(&boolValue)); |
292 EXPECT_TRUE(boolValue); | 291 EXPECT_TRUE(boolValue); |
293 | 292 |
294 // Don't allow empty elements. | 293 // Don't allow empty elements. |
295 root = parseJSON("[,]"); | 294 root = parseJSON("[,]"); |
296 EXPECT_FALSE(root.get()); | 295 EXPECT_FALSE(root.get()); |
297 root = parseJSON("[true,,]"); | 296 root = parseJSON("[true,,]"); |
298 EXPECT_FALSE(root.get()); | 297 EXPECT_FALSE(root.get()); |
299 root = parseJSON("[,true,]"); | 298 root = parseJSON("[,true,]"); |
300 EXPECT_FALSE(root.get()); | 299 EXPECT_FALSE(root.get()); |
301 root = parseJSON("[true,,false]"); | 300 root = parseJSON("[true,,false]"); |
302 EXPECT_FALSE(root.get()); | 301 EXPECT_FALSE(root.get()); |
303 | 302 |
304 // Test objects | 303 // Test objects |
305 root = parseJSON("{}"); | 304 root = parseJSON("{}"); |
306 ASSERT_TRUE(root.get()); | 305 ASSERT_TRUE(root.get()); |
307 EXPECT_EQ(Value::TypeObject, root->type()); | 306 EXPECT_EQ(JSONValue::TypeObject, root->getType()); |
308 | 307 |
309 root = parseJSON("{\"number\":9.87654321, \"null\":null , \"S\" : \"str\" }"
); | 308 root = parseJSON("{\"number\":9.87654321, \"null\":null , \"S\" : \"str\" }"
); |
310 ASSERT_TRUE(root.get()); | 309 ASSERT_TRUE(root.get()); |
311 EXPECT_EQ(Value::TypeObject, root->type()); | 310 EXPECT_EQ(JSONValue::TypeObject, root->getType()); |
312 protocol::DictionaryValue* objectVal = DictionaryValue::cast(root.get()); | 311 JSONObject* objectVal = JSONObject::cast(root.get()); |
313 ASSERT_TRUE(objectVal); | 312 ASSERT_TRUE(objectVal); |
314 doubleVal = 0.0; | 313 doubleVal = 0.0; |
315 EXPECT_TRUE(objectVal->getDouble("number", &doubleVal)); | 314 EXPECT_TRUE(objectVal->getNumber("number", &doubleVal)); |
316 EXPECT_DOUBLE_EQ(9.87654321, doubleVal); | 315 EXPECT_DOUBLE_EQ(9.87654321, doubleVal); |
317 protocol::Value* nullVal = objectVal->get("null"); | 316 JSONValue* nullVal = objectVal->get("null"); |
318 ASSERT_TRUE(nullVal); | 317 ASSERT_TRUE(nullVal); |
319 EXPECT_EQ(Value::TypeNull, nullVal->type()); | 318 EXPECT_EQ(JSONValue::TypeNull, nullVal->getType()); |
320 EXPECT_TRUE(objectVal->getString("S", &strVal)); | 319 EXPECT_TRUE(objectVal->getString("S", &strVal)); |
321 EXPECT_EQ("str", strVal); | 320 EXPECT_EQ("str", strVal); |
322 | 321 |
323 // Test newline equivalence. | 322 // Test newline equivalence. |
324 root2 = parseJSON( | 323 root2 = parseJSON( |
325 "{\n" | 324 "{\n" |
326 " \"number\":9.87654321,\n" | 325 " \"number\":9.87654321,\n" |
327 " \"null\":null,\n" | 326 " \"null\":null,\n" |
328 " \"S\":\"str\"\n" | 327 " \"S\":\"str\"\n" |
329 "}\n"); | 328 "}\n"); |
330 ASSERT_TRUE(root2.get()); | 329 ASSERT_TRUE(root2.get()); |
331 EXPECT_EQ(root->toJSONString(), root2->toJSONString()); | 330 EXPECT_EQ(root->toJSONString(), root2->toJSONString()); |
332 | 331 |
333 root2 = parseJSON( | 332 root2 = parseJSON( |
334 "{\r\n" | 333 "{\r\n" |
335 " \"number\":9.87654321,\r\n" | 334 " \"number\":9.87654321,\r\n" |
336 " \"null\":null,\r\n" | 335 " \"null\":null,\r\n" |
337 " \"S\":\"str\"\r\n" | 336 " \"S\":\"str\"\r\n" |
338 "}\r\n"); | 337 "}\r\n"); |
339 ASSERT_TRUE(root2.get()); | 338 ASSERT_TRUE(root2.get()); |
340 EXPECT_EQ(root->toJSONString(), root2->toJSONString()); | 339 EXPECT_EQ(root->toJSONString(), root2->toJSONString()); |
341 | 340 |
342 // Test nesting | 341 // Test nesting |
343 root = parseJSON("{\"inner\":{\"array\":[true]},\"false\":false,\"d\":{}}"); | 342 root = parseJSON("{\"inner\":{\"array\":[true]},\"false\":false,\"d\":{}}"); |
344 ASSERT_TRUE(root.get()); | 343 ASSERT_TRUE(root.get()); |
345 EXPECT_EQ(Value::TypeObject, root->type()); | 344 EXPECT_EQ(JSONValue::TypeObject, root->getType()); |
346 objectVal = DictionaryValue::cast(root.get()); | 345 objectVal = JSONObject::cast(root.get()); |
347 ASSERT_TRUE(objectVal); | 346 ASSERT_TRUE(objectVal); |
348 protocol::DictionaryValue* innerObject = objectVal->getObject("inner"); | 347 JSONObject* innerObject = objectVal->getObject("inner"); |
349 ASSERT_TRUE(innerObject); | 348 ASSERT_TRUE(innerObject); |
350 protocol::ListValue* innerArray = innerObject->getArray("array"); | 349 JSONArray* innerArray = innerObject->getArray("array"); |
351 ASSERT_TRUE(innerArray); | 350 ASSERT_TRUE(innerArray); |
352 EXPECT_EQ(1U, innerArray->size()); | 351 EXPECT_EQ(1U, innerArray->length()); |
353 boolValue = true; | 352 boolValue = true; |
354 EXPECT_TRUE(objectVal->getBoolean("false", &boolValue)); | 353 EXPECT_TRUE(objectVal->getBoolean("false", &boolValue)); |
355 EXPECT_FALSE(boolValue); | 354 EXPECT_FALSE(boolValue); |
356 innerObject = objectVal->getObject("d"); | 355 innerObject = objectVal->getObject("d"); |
357 EXPECT_TRUE(innerObject); | 356 EXPECT_TRUE(innerObject); |
358 | 357 |
359 // Test keys with periods | 358 // Test keys with periods |
360 root = parseJSON("{\"a.b\":3,\"c\":2,\"d.e.f\":{\"g.h.i.j\":1}}"); | 359 root = parseJSON("{\"a.b\":3,\"c\":2,\"d.e.f\":{\"g.h.i.j\":1}}"); |
361 ASSERT_TRUE(root.get()); | 360 ASSERT_TRUE(root.get()); |
362 EXPECT_EQ(Value::TypeObject, root->type()); | 361 EXPECT_EQ(JSONValue::TypeObject, root->getType()); |
363 objectVal = DictionaryValue::cast(root.get()); | 362 objectVal = JSONObject::cast(root.get()); |
364 ASSERT_TRUE(objectVal); | 363 ASSERT_TRUE(objectVal); |
365 int integerValue = 0; | 364 int integerValue = 0; |
366 EXPECT_TRUE(objectVal->getInteger("a.b", &integerValue)); | 365 EXPECT_TRUE(objectVal->getNumber("a.b", &integerValue)); |
367 EXPECT_EQ(3, integerValue); | 366 EXPECT_EQ(3, integerValue); |
368 EXPECT_TRUE(objectVal->getInteger("c", &integerValue)); | 367 EXPECT_TRUE(objectVal->getNumber("c", &integerValue)); |
369 EXPECT_EQ(2, integerValue); | 368 EXPECT_EQ(2, integerValue); |
370 innerObject = objectVal->getObject("d.e.f"); | 369 innerObject = objectVal->getObject("d.e.f"); |
371 ASSERT_TRUE(innerObject); | 370 ASSERT_TRUE(innerObject); |
372 EXPECT_EQ(1U, innerObject->size()); | 371 EXPECT_EQ(1, innerObject->size()); |
373 EXPECT_TRUE(innerObject->getInteger("g.h.i.j", &integerValue)); | 372 EXPECT_TRUE(innerObject->getNumber("g.h.i.j", &integerValue)); |
374 EXPECT_EQ(1, integerValue); | 373 EXPECT_EQ(1, integerValue); |
375 | 374 |
376 root = parseJSON("{\"a\":{\"b\":2},\"a.b\":1}"); | 375 root = parseJSON("{\"a\":{\"b\":2},\"a.b\":1}"); |
377 ASSERT_TRUE(root.get()); | 376 ASSERT_TRUE(root.get()); |
378 EXPECT_EQ(Value::TypeObject, root->type()); | 377 EXPECT_EQ(JSONValue::TypeObject, root->getType()); |
379 objectVal = DictionaryValue::cast(root.get()); | 378 objectVal = JSONObject::cast(root.get()); |
380 ASSERT_TRUE(objectVal); | 379 ASSERT_TRUE(objectVal); |
381 innerObject = objectVal->getObject("a"); | 380 innerObject = objectVal->getObject("a"); |
382 ASSERT_TRUE(innerObject); | 381 ASSERT_TRUE(innerObject); |
383 EXPECT_TRUE(innerObject->getInteger("b", &integerValue)); | 382 EXPECT_TRUE(innerObject->getNumber("b", &integerValue)); |
384 EXPECT_EQ(2, integerValue); | 383 EXPECT_EQ(2, integerValue); |
385 EXPECT_TRUE(objectVal->getInteger("a.b", &integerValue)); | 384 EXPECT_TRUE(objectVal->getNumber("a.b", &integerValue)); |
386 EXPECT_EQ(1, integerValue); | 385 EXPECT_EQ(1, integerValue); |
387 | 386 |
388 // Invalid, no closing brace | 387 // Invalid, no closing brace |
389 root = parseJSON("{\"a\": true"); | 388 root = parseJSON("{\"a\": true"); |
390 EXPECT_FALSE(root.get()); | 389 EXPECT_FALSE(root.get()); |
391 | 390 |
392 // Invalid, keys must be quoted | 391 // Invalid, keys must be quoted |
393 root = parseJSON("{foo:true}"); | 392 root = parseJSON("{foo:true}"); |
394 EXPECT_FALSE(root.get()); | 393 EXPECT_FALSE(root.get()); |
395 | 394 |
(...skipping 13 matching lines...) Expand all Loading... |
409 root = parseJSON("{,}"); | 408 root = parseJSON("{,}"); |
410 EXPECT_FALSE(root.get()); | 409 EXPECT_FALSE(root.get()); |
411 root = parseJSON("{\"a\":true,,}"); | 410 root = parseJSON("{\"a\":true,,}"); |
412 EXPECT_FALSE(root.get()); | 411 EXPECT_FALSE(root.get()); |
413 root = parseJSON("{,\"a\":true}"); | 412 root = parseJSON("{,\"a\":true}"); |
414 EXPECT_FALSE(root.get()); | 413 EXPECT_FALSE(root.get()); |
415 root = parseJSON("{\"a\":true,,\"b\":false}"); | 414 root = parseJSON("{\"a\":true,,\"b\":false}"); |
416 EXPECT_FALSE(root.get()); | 415 EXPECT_FALSE(root.get()); |
417 | 416 |
418 // Test stack overflow | 417 // Test stack overflow |
419 String16Builder evil; | 418 StringBuilder evil; |
420 evil.reserveCapacity(2000000); | 419 evil.reserveCapacity(2000000); |
421 for (int i = 0; i < 1000000; ++i) | 420 for (int i = 0; i < 1000000; ++i) |
422 evil.append('['); | 421 evil.append('['); |
423 for (int i = 0; i < 1000000; ++i) | 422 for (int i = 0; i < 1000000; ++i) |
424 evil.append(']'); | 423 evil.append(']'); |
425 root = parseJSON(evil.toString()); | 424 root = parseJSON(evil.toString()); |
426 EXPECT_FALSE(root.get()); | 425 EXPECT_FALSE(root.get()); |
427 | 426 |
428 // A few thousand adjacent lists is fine. | 427 // A few thousand adjacent lists is fine. |
429 String16Builder notEvil; | 428 StringBuilder notEvil; |
430 notEvil.reserveCapacity(15010); | 429 notEvil.reserveCapacity(15010); |
431 notEvil.append('['); | 430 notEvil.append('['); |
432 for (int i = 0; i < 5000; ++i) | 431 for (int i = 0; i < 5000; ++i) |
433 notEvil.append("[],"); | 432 notEvil.append("[],"); |
434 notEvil.append("[]]"); | 433 notEvil.append("[]]"); |
435 root = parseJSON(notEvil.toString()); | 434 root = parseJSON(notEvil.toString()); |
436 ASSERT_TRUE(root.get()); | 435 ASSERT_TRUE(root.get()); |
437 EXPECT_EQ(Value::TypeArray, root->type()); | 436 EXPECT_EQ(JSONValue::TypeArray, root->getType()); |
438 list = ListValue::cast(root.get()); | 437 list = JSONArray::cast(root.get()); |
439 ASSERT_TRUE(list); | 438 ASSERT_TRUE(list); |
440 EXPECT_EQ(5001U, list->size()); | 439 EXPECT_EQ(5001U, list->length()); |
441 | 440 |
442 // Test utf8 encoded input | 441 // Test utf8 encoded input |
443 root = parseJSON("\"\\xe7\\xbd\\x91\\xe9\\xa1\\xb5\""); | 442 root = parseJSON("\"\\xe7\\xbd\\x91\\xe9\\xa1\\xb5\""); |
444 ASSERT_FALSE(root.get()); | 443 ASSERT_FALSE(root.get()); |
445 | 444 |
446 // Test utf16 encoded strings. | 445 // Test utf16 encoded strings. |
447 root = parseJSON("\"\\u20ac3,14\""); | 446 root = parseJSON("\"\\u20ac3,14\""); |
448 ASSERT_TRUE(root.get()); | 447 ASSERT_TRUE(root.get()); |
449 EXPECT_EQ(Value::TypeString, root->type()); | 448 EXPECT_EQ(JSONValue::TypeString, root->getType()); |
450 EXPECT_TRUE(root->asString(&strVal)); | 449 EXPECT_TRUE(root->asString(&strVal)); |
451 UChar tmp2[] = {0x20ac, 0x33, 0x2c, 0x31, 0x34}; | 450 UChar tmp2[] = {0x20ac, 0x33, 0x2c, 0x31, 0x34}; |
452 EXPECT_EQ(String16(tmp2, 5), strVal); | 451 EXPECT_EQ(String(tmp2, WTF_ARRAY_LENGTH(tmp2)), strVal); |
453 | 452 |
454 root = parseJSON("\"\\ud83d\\udca9\\ud83d\\udc6c\""); | 453 root = parseJSON("\"\\ud83d\\udca9\\ud83d\\udc6c\""); |
455 ASSERT_TRUE(root.get()); | 454 ASSERT_TRUE(root.get()); |
456 EXPECT_EQ(Value::TypeString, root->type()); | 455 EXPECT_EQ(JSONValue::TypeString, root->getType()); |
457 EXPECT_TRUE(root->asString(&strVal)); | 456 EXPECT_TRUE(root->asString(&strVal)); |
458 UChar tmp3[] = {0xd83d, 0xdca9, 0xd83d, 0xdc6c}; | 457 UChar tmp3[] = {0xd83d, 0xdca9, 0xd83d, 0xdc6c}; |
459 EXPECT_EQ(String16(tmp3, 4), strVal); | 458 EXPECT_EQ(String(tmp3, WTF_ARRAY_LENGTH(tmp3)), strVal); |
460 | 459 |
461 // Test literal root objects. | 460 // Test literal root objects. |
462 root = parseJSON("null"); | 461 root = parseJSON("null"); |
463 EXPECT_EQ(Value::TypeNull, root->type()); | 462 EXPECT_EQ(JSONValue::TypeNull, root->getType()); |
464 | 463 |
465 root = parseJSON("true"); | 464 root = parseJSON("true"); |
466 ASSERT_TRUE(root.get()); | 465 ASSERT_TRUE(root.get()); |
467 EXPECT_TRUE(root->asBoolean(&boolValue)); | 466 EXPECT_TRUE(root->asBoolean(&boolValue)); |
468 EXPECT_TRUE(boolValue); | 467 EXPECT_TRUE(boolValue); |
469 | 468 |
470 root = parseJSON("10"); | 469 root = parseJSON("10"); |
471 ASSERT_TRUE(root.get()); | 470 ASSERT_TRUE(root.get()); |
472 EXPECT_TRUE(root->asInteger(&integerValue)); | 471 EXPECT_TRUE(root->asNumber(&integerValue)); |
473 EXPECT_EQ(10, integerValue); | 472 EXPECT_EQ(10, integerValue); |
474 | 473 |
475 root = parseJSON("\"root\""); | 474 root = parseJSON("\"root\""); |
476 ASSERT_TRUE(root.get()); | 475 ASSERT_TRUE(root.get()); |
477 EXPECT_TRUE(root->asString(&strVal)); | 476 EXPECT_TRUE(root->asString(&strVal)); |
478 EXPECT_EQ("root", strVal); | 477 EXPECT_EQ("root", strVal); |
479 } | 478 } |
480 | 479 |
481 TEST(ParserTest, InvalidSanity) | 480 TEST(JSONParserTest, InvalidSanity) |
482 { | 481 { |
483 const char* const invalidJson[] = { | 482 const char* const invalidJson[] = { |
484 "/* test *", | 483 "/* test *", |
485 "{\"foo\"", | 484 "{\"foo\"", |
486 "{\"foo\":", | 485 "{\"foo\":", |
487 " [", | 486 " [", |
488 "\"\\u123g\"", | 487 "\"\\u123g\"", |
489 "{\n\"eh:\n}", | 488 "{\n\"eh:\n}", |
490 "////", | 489 "////", |
491 "*/**/", | 490 "*/**/", |
492 "/**/", | 491 "/**/", |
493 "/*/", | 492 "/*/", |
494 "//**/" | 493 "//**/" |
495 }; | 494 }; |
496 | 495 |
497 for (size_t i = 0; i < 11; ++i) { | 496 for (size_t i = 0; i < WTF_ARRAY_LENGTH(invalidJson); ++i) { |
498 std::unique_ptr<protocol::Value> result = parseJSON(invalidJson[i]); | 497 std::unique_ptr<JSONValue> result = parseJSON(invalidJson[i]); |
499 EXPECT_FALSE(result.get()); | 498 EXPECT_FALSE(result.get()); |
500 } | 499 } |
501 } | 500 } |
502 | 501 |
503 } // namespace protocol | |
504 } // namespace blink | 502 } // namespace blink |
OLD | NEW |