Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(493)

Side by Side Diff: base/json/json_parser_unittest.cc

Issue 1180693002: Update from https://crrev.com/333737 (Closed) Base URL: https://github.com/domokit/mojo.git@master
Patch Set: rebased Created 5 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « base/ios/crb_protocol_observers_unittest.mm ('k') | base/json/json_reader.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 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 "base/json/json_parser.h" 5 #include "base/json/json_parser.h"
6 6
7 #include "base/json/json_reader.h" 7 #include "base/json/json_reader.h"
8 #include "base/memory/scoped_ptr.h" 8 #include "base/memory/scoped_ptr.h"
9 #include "base/values.h" 9 #include "base/values.h"
10 #include "testing/gtest/include/gtest/gtest.h" 10 #include "testing/gtest/include/gtest/gtest.h"
(...skipping 186 matching lines...) Expand 10 before | Expand all | Expand 10 after
197 ASSERT_TRUE(value.get()); 197 ASSERT_TRUE(value.get());
198 EXPECT_TRUE(value->GetAsDouble(&number_d)); 198 EXPECT_TRUE(value->GetAsDouble(&number_d));
199 EXPECT_EQ(420, number_d); 199 EXPECT_EQ(420, number_d);
200 } 200 }
201 201
202 TEST_F(JSONParserTest, ErrorMessages) { 202 TEST_F(JSONParserTest, ErrorMessages) {
203 // Error strings should not be modified in case of success. 203 // Error strings should not be modified in case of success.
204 std::string error_message; 204 std::string error_message;
205 int error_code = 0; 205 int error_code = 0;
206 scoped_ptr<Value> root; 206 scoped_ptr<Value> root;
207 root.reset(JSONReader::ReadAndReturnError("[42]", JSON_PARSE_RFC, 207 root.reset(JSONReader::DeprecatedReadAndReturnError(
208 &error_code, &error_message)); 208 "[42]", JSON_PARSE_RFC, &error_code, &error_message));
209 EXPECT_TRUE(error_message.empty()); 209 EXPECT_TRUE(error_message.empty());
210 EXPECT_EQ(0, error_code); 210 EXPECT_EQ(0, error_code);
211 211
212 // Test line and column counting 212 // Test line and column counting
213 const char big_json[] = "[\n0,\n1,\n2,\n3,4,5,6 7,\n8,\n9\n]"; 213 const char big_json[] = "[\n0,\n1,\n2,\n3,4,5,6 7,\n8,\n9\n]";
214 // error here ----------------------------------^ 214 // error here ----------------------------------^
215 root.reset(JSONReader::ReadAndReturnError(big_json, JSON_PARSE_RFC, 215 root.reset(JSONReader::DeprecatedReadAndReturnError(
216 &error_code, &error_message)); 216 big_json, JSON_PARSE_RFC, &error_code, &error_message));
217 EXPECT_FALSE(root.get()); 217 EXPECT_FALSE(root.get());
218 EXPECT_EQ(JSONParser::FormatErrorMessage(5, 10, JSONReader::kSyntaxError), 218 EXPECT_EQ(JSONParser::FormatErrorMessage(5, 10, JSONReader::kSyntaxError),
219 error_message); 219 error_message);
220 EXPECT_EQ(JSONReader::JSON_SYNTAX_ERROR, error_code); 220 EXPECT_EQ(JSONReader::JSON_SYNTAX_ERROR, error_code);
221 221
222 error_code = 0; 222 error_code = 0;
223 error_message = ""; 223 error_message = "";
224 // Test line and column counting with "\r\n" line ending 224 // Test line and column counting with "\r\n" line ending
225 const char big_json_crlf[] = 225 const char big_json_crlf[] =
226 "[\r\n0,\r\n1,\r\n2,\r\n3,4,5,6 7,\r\n8,\r\n9\r\n]"; 226 "[\r\n0,\r\n1,\r\n2,\r\n3,4,5,6 7,\r\n8,\r\n9\r\n]";
227 // error here ----------------------^ 227 // error here ----------------------^
228 root.reset(JSONReader::ReadAndReturnError(big_json_crlf, JSON_PARSE_RFC, 228 root.reset(JSONReader::DeprecatedReadAndReturnError(
229 &error_code, &error_message)); 229 big_json_crlf, JSON_PARSE_RFC, &error_code, &error_message));
230 EXPECT_FALSE(root.get()); 230 EXPECT_FALSE(root.get());
231 EXPECT_EQ(JSONParser::FormatErrorMessage(5, 10, JSONReader::kSyntaxError), 231 EXPECT_EQ(JSONParser::FormatErrorMessage(5, 10, JSONReader::kSyntaxError),
232 error_message); 232 error_message);
233 EXPECT_EQ(JSONReader::JSON_SYNTAX_ERROR, error_code); 233 EXPECT_EQ(JSONReader::JSON_SYNTAX_ERROR, error_code);
234 234
235 // Test each of the error conditions 235 // Test each of the error conditions
236 root.reset(JSONReader::ReadAndReturnError("{},{}", JSON_PARSE_RFC, 236 root.reset(JSONReader::DeprecatedReadAndReturnError(
237 &error_code, &error_message)); 237 "{},{}", JSON_PARSE_RFC, &error_code, &error_message));
238 EXPECT_FALSE(root.get()); 238 EXPECT_FALSE(root.get());
239 EXPECT_EQ(JSONParser::FormatErrorMessage(1, 3, 239 EXPECT_EQ(JSONParser::FormatErrorMessage(1, 3,
240 JSONReader::kUnexpectedDataAfterRoot), error_message); 240 JSONReader::kUnexpectedDataAfterRoot), error_message);
241 EXPECT_EQ(JSONReader::JSON_UNEXPECTED_DATA_AFTER_ROOT, error_code); 241 EXPECT_EQ(JSONReader::JSON_UNEXPECTED_DATA_AFTER_ROOT, error_code);
242 242
243 std::string nested_json; 243 std::string nested_json;
244 for (int i = 0; i < 101; ++i) { 244 for (int i = 0; i < 101; ++i) {
245 nested_json.insert(nested_json.begin(), '['); 245 nested_json.insert(nested_json.begin(), '[');
246 nested_json.append(1, ']'); 246 nested_json.append(1, ']');
247 } 247 }
248 root.reset(JSONReader::ReadAndReturnError(nested_json, JSON_PARSE_RFC, 248 root.reset(JSONReader::DeprecatedReadAndReturnError(
249 &error_code, &error_message)); 249 nested_json, JSON_PARSE_RFC, &error_code, &error_message));
250 EXPECT_FALSE(root.get()); 250 EXPECT_FALSE(root.get());
251 EXPECT_EQ(JSONParser::FormatErrorMessage(1, 100, JSONReader::kTooMuchNesting), 251 EXPECT_EQ(JSONParser::FormatErrorMessage(1, 100, JSONReader::kTooMuchNesting),
252 error_message); 252 error_message);
253 EXPECT_EQ(JSONReader::JSON_TOO_MUCH_NESTING, error_code); 253 EXPECT_EQ(JSONReader::JSON_TOO_MUCH_NESTING, error_code);
254 254
255 root.reset(JSONReader::ReadAndReturnError("[1,]", JSON_PARSE_RFC, 255 root.reset(JSONReader::DeprecatedReadAndReturnError(
256 &error_code, &error_message)); 256 "[1,]", JSON_PARSE_RFC, &error_code, &error_message));
257 EXPECT_FALSE(root.get()); 257 EXPECT_FALSE(root.get());
258 EXPECT_EQ(JSONParser::FormatErrorMessage(1, 4, JSONReader::kTrailingComma), 258 EXPECT_EQ(JSONParser::FormatErrorMessage(1, 4, JSONReader::kTrailingComma),
259 error_message); 259 error_message);
260 EXPECT_EQ(JSONReader::JSON_TRAILING_COMMA, error_code); 260 EXPECT_EQ(JSONReader::JSON_TRAILING_COMMA, error_code);
261 261
262 root.reset(JSONReader::ReadAndReturnError("{foo:\"bar\"}", JSON_PARSE_RFC, 262 root.reset(JSONReader::DeprecatedReadAndReturnError(
263 &error_code, &error_message)); 263 "{foo:\"bar\"}", JSON_PARSE_RFC, &error_code, &error_message));
264 EXPECT_FALSE(root.get()); 264 EXPECT_FALSE(root.get());
265 EXPECT_EQ(JSONParser::FormatErrorMessage(1, 2, 265 EXPECT_EQ(JSONParser::FormatErrorMessage(1, 2,
266 JSONReader::kUnquotedDictionaryKey), error_message); 266 JSONReader::kUnquotedDictionaryKey), error_message);
267 EXPECT_EQ(JSONReader::JSON_UNQUOTED_DICTIONARY_KEY, error_code); 267 EXPECT_EQ(JSONReader::JSON_UNQUOTED_DICTIONARY_KEY, error_code);
268 268
269 root.reset(JSONReader::ReadAndReturnError("{\"foo\":\"bar\",}", 269 root.reset(JSONReader::DeprecatedReadAndReturnError(
270 JSON_PARSE_RFC, 270 "{\"foo\":\"bar\",}", JSON_PARSE_RFC, &error_code, &error_message));
271 &error_code, &error_message));
272 EXPECT_FALSE(root.get()); 271 EXPECT_FALSE(root.get());
273 EXPECT_EQ(JSONParser::FormatErrorMessage(1, 14, JSONReader::kTrailingComma), 272 EXPECT_EQ(JSONParser::FormatErrorMessage(1, 14, JSONReader::kTrailingComma),
274 error_message); 273 error_message);
275 274
276 root.reset(JSONReader::ReadAndReturnError("[nu]", JSON_PARSE_RFC, 275 root.reset(JSONReader::DeprecatedReadAndReturnError(
277 &error_code, &error_message)); 276 "[nu]", JSON_PARSE_RFC, &error_code, &error_message));
278 EXPECT_FALSE(root.get()); 277 EXPECT_FALSE(root.get());
279 EXPECT_EQ(JSONParser::FormatErrorMessage(1, 2, JSONReader::kSyntaxError), 278 EXPECT_EQ(JSONParser::FormatErrorMessage(1, 2, JSONReader::kSyntaxError),
280 error_message); 279 error_message);
281 EXPECT_EQ(JSONReader::JSON_SYNTAX_ERROR, error_code); 280 EXPECT_EQ(JSONReader::JSON_SYNTAX_ERROR, error_code);
282 281
283 root.reset(JSONReader::ReadAndReturnError("[\"xxx\\xq\"]", JSON_PARSE_RFC, 282 root.reset(JSONReader::DeprecatedReadAndReturnError(
284 &error_code, &error_message)); 283 "[\"xxx\\xq\"]", JSON_PARSE_RFC, &error_code, &error_message));
285 EXPECT_FALSE(root.get()); 284 EXPECT_FALSE(root.get());
286 EXPECT_EQ(JSONParser::FormatErrorMessage(1, 7, JSONReader::kInvalidEscape), 285 EXPECT_EQ(JSONParser::FormatErrorMessage(1, 7, JSONReader::kInvalidEscape),
287 error_message); 286 error_message);
288 EXPECT_EQ(JSONReader::JSON_INVALID_ESCAPE, error_code); 287 EXPECT_EQ(JSONReader::JSON_INVALID_ESCAPE, error_code);
289 288
290 root.reset(JSONReader::ReadAndReturnError("[\"xxx\\uq\"]", JSON_PARSE_RFC, 289 root.reset(JSONReader::DeprecatedReadAndReturnError(
291 &error_code, &error_message)); 290 "[\"xxx\\uq\"]", JSON_PARSE_RFC, &error_code, &error_message));
292 EXPECT_FALSE(root.get()); 291 EXPECT_FALSE(root.get());
293 EXPECT_EQ(JSONParser::FormatErrorMessage(1, 7, JSONReader::kInvalidEscape), 292 EXPECT_EQ(JSONParser::FormatErrorMessage(1, 7, JSONReader::kInvalidEscape),
294 error_message); 293 error_message);
295 EXPECT_EQ(JSONReader::JSON_INVALID_ESCAPE, error_code); 294 EXPECT_EQ(JSONReader::JSON_INVALID_ESCAPE, error_code);
296 295
297 root.reset(JSONReader::ReadAndReturnError("[\"xxx\\q\"]", JSON_PARSE_RFC, 296 root.reset(JSONReader::DeprecatedReadAndReturnError(
298 &error_code, &error_message)); 297 "[\"xxx\\q\"]", JSON_PARSE_RFC, &error_code, &error_message));
299 EXPECT_FALSE(root.get()); 298 EXPECT_FALSE(root.get());
300 EXPECT_EQ(JSONParser::FormatErrorMessage(1, 7, JSONReader::kInvalidEscape), 299 EXPECT_EQ(JSONParser::FormatErrorMessage(1, 7, JSONReader::kInvalidEscape),
301 error_message); 300 error_message);
302 EXPECT_EQ(JSONReader::JSON_INVALID_ESCAPE, error_code); 301 EXPECT_EQ(JSONReader::JSON_INVALID_ESCAPE, error_code);
303 } 302 }
304 303
305 TEST_F(JSONParserTest, Decode4ByteUtf8Char) { 304 TEST_F(JSONParserTest, Decode4ByteUtf8Char) {
306 // This test strings contains a 4 byte unicode character (a smiley!) that the 305 // This test strings contains a 4 byte unicode character (a smiley!) that the
307 // reader should be able to handle (the character is \xf0\x9f\x98\x87). 306 // reader should be able to handle (the character is \xf0\x9f\x98\x87).
308 const char kUtf8Data[] = 307 const char kUtf8Data[] =
309 "[\"😇\",[],[],[],{\"google:suggesttype\":[]}]"; 308 "[\"😇\",[],[],[],{\"google:suggesttype\":[]}]";
310 std::string error_message; 309 std::string error_message;
311 int error_code = 0; 310 int error_code = 0;
312 scoped_ptr<Value> root( 311 scoped_ptr<Value> root(JSONReader::DeprecatedReadAndReturnError(
313 JSONReader::ReadAndReturnError(kUtf8Data, JSON_PARSE_RFC, &error_code, 312 kUtf8Data, JSON_PARSE_RFC, &error_code, &error_message));
314 &error_message));
315 EXPECT_TRUE(root.get()) << error_message; 313 EXPECT_TRUE(root.get()) << error_message;
316 } 314 }
317 315
318 } // namespace internal 316 } // namespace internal
319 } // namespace base 317 } // namespace base
OLDNEW
« no previous file with comments | « base/ios/crb_protocol_observers_unittest.mm ('k') | base/json/json_reader.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698