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