1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346
|
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "base/json/json_parser.h"
#include <stddef.h>
#include <memory>
#include "base/json/json_reader.h"
#include "base/values.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace base {
namespace internal {
class JSONParserTest : public testing::Test {
public:
JSONParser* NewTestParser(const std::string& input,
int options = JSON_PARSE_RFC) {
JSONParser* parser = new JSONParser(options);
parser->start_pos_ = input.data();
parser->pos_ = parser->start_pos_;
parser->end_pos_ = parser->start_pos_ + input.length();
return parser;
}
void TestLastThree(JSONParser* parser) {
EXPECT_EQ(',', *parser->NextChar());
EXPECT_EQ('|', *parser->NextChar());
EXPECT_EQ('\0', *parser->NextChar());
EXPECT_EQ(parser->end_pos_, parser->pos_);
}
};
TEST_F(JSONParserTest, NextChar) {
std::string input("Hello world");
std::unique_ptr<JSONParser> parser(NewTestParser(input));
EXPECT_EQ('H', *parser->pos_);
for (size_t i = 1; i < input.length(); ++i) {
EXPECT_EQ(input[i], *parser->NextChar());
}
EXPECT_EQ(parser->end_pos_, parser->NextChar());
}
TEST_F(JSONParserTest, ConsumeString) {
std::string input("\"test\",|");
std::unique_ptr<JSONParser> parser(NewTestParser(input));
std::unique_ptr<Value> value(parser->ConsumeString());
EXPECT_EQ('"', *parser->pos_);
TestLastThree(parser.get());
ASSERT_TRUE(value.get());
std::string str;
EXPECT_TRUE(value->GetAsString(&str));
EXPECT_EQ("test", str);
}
TEST_F(JSONParserTest, ConsumeList) {
std::string input("[true, false],|");
std::unique_ptr<JSONParser> parser(NewTestParser(input));
std::unique_ptr<Value> value(parser->ConsumeList());
EXPECT_EQ(']', *parser->pos_);
TestLastThree(parser.get());
ASSERT_TRUE(value.get());
base::ListValue* list;
EXPECT_TRUE(value->GetAsList(&list));
EXPECT_EQ(2u, list->GetSize());
}
TEST_F(JSONParserTest, ConsumeDictionary) {
std::string input("{\"abc\":\"def\"},|");
std::unique_ptr<JSONParser> parser(NewTestParser(input));
std::unique_ptr<Value> value(parser->ConsumeDictionary());
EXPECT_EQ('}', *parser->pos_);
TestLastThree(parser.get());
ASSERT_TRUE(value.get());
base::DictionaryValue* dict;
EXPECT_TRUE(value->GetAsDictionary(&dict));
std::string str;
EXPECT_TRUE(dict->GetString("abc", &str));
EXPECT_EQ("def", str);
}
TEST_F(JSONParserTest, ConsumeLiterals) {
// Literal |true|.
std::string input("true,|");
std::unique_ptr<JSONParser> parser(NewTestParser(input));
std::unique_ptr<Value> value(parser->ConsumeLiteral());
EXPECT_EQ('e', *parser->pos_);
TestLastThree(parser.get());
ASSERT_TRUE(value.get());
bool bool_value = false;
EXPECT_TRUE(value->GetAsBoolean(&bool_value));
EXPECT_TRUE(bool_value);
// Literal |false|.
input = "false,|";
parser.reset(NewTestParser(input));
value = parser->ConsumeLiteral();
EXPECT_EQ('e', *parser->pos_);
TestLastThree(parser.get());
ASSERT_TRUE(value.get());
EXPECT_TRUE(value->GetAsBoolean(&bool_value));
EXPECT_FALSE(bool_value);
// Literal |null|.
input = "null,|";
parser.reset(NewTestParser(input));
value = parser->ConsumeLiteral();
EXPECT_EQ('l', *parser->pos_);
TestLastThree(parser.get());
ASSERT_TRUE(value.get());
EXPECT_TRUE(value->IsType(Value::Type::NONE));
}
TEST_F(JSONParserTest, ConsumeNumbers) {
// Integer.
std::string input("1234,|");
std::unique_ptr<JSONParser> parser(NewTestParser(input));
std::unique_ptr<Value> value(parser->ConsumeNumber());
EXPECT_EQ('4', *parser->pos_);
TestLastThree(parser.get());
ASSERT_TRUE(value.get());
int number_i;
EXPECT_TRUE(value->GetAsInteger(&number_i));
EXPECT_EQ(1234, number_i);
// Negative integer.
input = "-1234,|";
parser.reset(NewTestParser(input));
value = parser->ConsumeNumber();
EXPECT_EQ('4', *parser->pos_);
TestLastThree(parser.get());
ASSERT_TRUE(value.get());
EXPECT_TRUE(value->GetAsInteger(&number_i));
EXPECT_EQ(-1234, number_i);
// Double.
input = "12.34,|";
parser.reset(NewTestParser(input));
value = parser->ConsumeNumber();
EXPECT_EQ('4', *parser->pos_);
TestLastThree(parser.get());
ASSERT_TRUE(value.get());
double number_d;
EXPECT_TRUE(value->GetAsDouble(&number_d));
EXPECT_EQ(12.34, number_d);
// Scientific.
input = "42e3,|";
parser.reset(NewTestParser(input));
value = parser->ConsumeNumber();
EXPECT_EQ('3', *parser->pos_);
TestLastThree(parser.get());
ASSERT_TRUE(value.get());
EXPECT_TRUE(value->GetAsDouble(&number_d));
EXPECT_EQ(42000, number_d);
// Negative scientific.
input = "314159e-5,|";
parser.reset(NewTestParser(input));
value = parser->ConsumeNumber();
EXPECT_EQ('5', *parser->pos_);
TestLastThree(parser.get());
ASSERT_TRUE(value.get());
EXPECT_TRUE(value->GetAsDouble(&number_d));
EXPECT_EQ(3.14159, number_d);
// Positive scientific.
input = "0.42e+3,|";
parser.reset(NewTestParser(input));
value = parser->ConsumeNumber();
EXPECT_EQ('3', *parser->pos_);
TestLastThree(parser.get());
ASSERT_TRUE(value.get());
EXPECT_TRUE(value->GetAsDouble(&number_d));
EXPECT_EQ(420, number_d);
}
TEST_F(JSONParserTest, ErrorMessages) {
// Error strings should not be modified in case of success.
std::string error_message;
int error_code = 0;
std::unique_ptr<Value> root = JSONReader::ReadAndReturnError(
"[42]", JSON_PARSE_RFC, &error_code, &error_message);
EXPECT_TRUE(error_message.empty());
EXPECT_EQ(0, error_code);
// Test line and column counting
const char big_json[] = "[\n0,\n1,\n2,\n3,4,5,6 7,\n8,\n9\n]";
// error here ----------------------------------^
root = JSONReader::ReadAndReturnError(big_json, JSON_PARSE_RFC, &error_code,
&error_message);
EXPECT_FALSE(root.get());
EXPECT_EQ(JSONParser::FormatErrorMessage(5, 10, JSONReader::kSyntaxError),
error_message);
EXPECT_EQ(JSONReader::JSON_SYNTAX_ERROR, error_code);
error_code = 0;
error_message = "";
// Test line and column counting with "\r\n" line ending
const char big_json_crlf[] =
"[\r\n0,\r\n1,\r\n2,\r\n3,4,5,6 7,\r\n8,\r\n9\r\n]";
// error here ----------------------^
root = JSONReader::ReadAndReturnError(big_json_crlf, JSON_PARSE_RFC,
&error_code, &error_message);
EXPECT_FALSE(root.get());
EXPECT_EQ(JSONParser::FormatErrorMessage(5, 10, JSONReader::kSyntaxError),
error_message);
EXPECT_EQ(JSONReader::JSON_SYNTAX_ERROR, error_code);
// Test each of the error conditions
root = JSONReader::ReadAndReturnError("{},{}", JSON_PARSE_RFC, &error_code,
&error_message);
EXPECT_FALSE(root.get());
EXPECT_EQ(JSONParser::FormatErrorMessage(1, 3,
JSONReader::kUnexpectedDataAfterRoot), error_message);
EXPECT_EQ(JSONReader::JSON_UNEXPECTED_DATA_AFTER_ROOT, error_code);
std::string nested_json;
for (int i = 0; i < 201; ++i) {
nested_json.insert(nested_json.begin(), '[');
nested_json.append(1, ']');
}
root = JSONReader::ReadAndReturnError(nested_json, JSON_PARSE_RFC,
&error_code, &error_message);
EXPECT_FALSE(root.get());
EXPECT_EQ(JSONParser::FormatErrorMessage(1, 200, JSONReader::kTooMuchNesting),
error_message);
EXPECT_EQ(JSONReader::JSON_TOO_MUCH_NESTING, error_code);
root = JSONReader::ReadAndReturnError("[1,]", JSON_PARSE_RFC, &error_code,
&error_message);
EXPECT_FALSE(root.get());
EXPECT_EQ(JSONParser::FormatErrorMessage(1, 4, JSONReader::kTrailingComma),
error_message);
EXPECT_EQ(JSONReader::JSON_TRAILING_COMMA, error_code);
root = JSONReader::ReadAndReturnError("{foo:\"bar\"}", JSON_PARSE_RFC,
&error_code, &error_message);
EXPECT_FALSE(root.get());
EXPECT_EQ(JSONParser::FormatErrorMessage(1, 2,
JSONReader::kUnquotedDictionaryKey), error_message);
EXPECT_EQ(JSONReader::JSON_UNQUOTED_DICTIONARY_KEY, error_code);
root = JSONReader::ReadAndReturnError("{\"foo\":\"bar\",}", JSON_PARSE_RFC,
&error_code, &error_message);
EXPECT_FALSE(root.get());
EXPECT_EQ(JSONParser::FormatErrorMessage(1, 14, JSONReader::kTrailingComma),
error_message);
root = JSONReader::ReadAndReturnError("[nu]", JSON_PARSE_RFC, &error_code,
&error_message);
EXPECT_FALSE(root.get());
EXPECT_EQ(JSONParser::FormatErrorMessage(1, 2, JSONReader::kSyntaxError),
error_message);
EXPECT_EQ(JSONReader::JSON_SYNTAX_ERROR, error_code);
root = JSONReader::ReadAndReturnError("[\"xxx\\xq\"]", JSON_PARSE_RFC,
&error_code, &error_message);
EXPECT_FALSE(root.get());
EXPECT_EQ(JSONParser::FormatErrorMessage(1, 7, JSONReader::kInvalidEscape),
error_message);
EXPECT_EQ(JSONReader::JSON_INVALID_ESCAPE, error_code);
root = JSONReader::ReadAndReturnError("[\"xxx\\uq\"]", JSON_PARSE_RFC,
&error_code, &error_message);
EXPECT_FALSE(root.get());
EXPECT_EQ(JSONParser::FormatErrorMessage(1, 7, JSONReader::kInvalidEscape),
error_message);
EXPECT_EQ(JSONReader::JSON_INVALID_ESCAPE, error_code);
root = JSONReader::ReadAndReturnError("[\"xxx\\q\"]", JSON_PARSE_RFC,
&error_code, &error_message);
EXPECT_FALSE(root.get());
EXPECT_EQ(JSONParser::FormatErrorMessage(1, 7, JSONReader::kInvalidEscape),
error_message);
EXPECT_EQ(JSONReader::JSON_INVALID_ESCAPE, error_code);
}
TEST_F(JSONParserTest, Decode4ByteUtf8Char) {
// This test strings contains a 4 byte unicode character (a smiley!) that the
// reader should be able to handle (the character is \xf0\x9f\x98\x87).
const char kUtf8Data[] =
"[\"😇\",[],[],[],{\"google:suggesttype\":[]}]";
std::string error_message;
int error_code = 0;
std::unique_ptr<Value> root = JSONReader::ReadAndReturnError(
kUtf8Data, JSON_PARSE_RFC, &error_code, &error_message);
EXPECT_TRUE(root.get()) << error_message;
}
TEST_F(JSONParserTest, DecodeUnicodeNonCharacter) {
// Tests Unicode code points (encoded as escaped UTF-16) that are not valid
// characters.
EXPECT_FALSE(JSONReader::Read("[\"\\ufdd0\"]"));
EXPECT_FALSE(JSONReader::Read("[\"\\ufffe\"]"));
EXPECT_FALSE(JSONReader::Read("[\"\\ud83f\\udffe\"]"));
}
TEST_F(JSONParserTest, DecodeNegativeEscapeSequence) {
EXPECT_FALSE(JSONReader::Read("[\"\\x-A\"]"));
EXPECT_FALSE(JSONReader::Read("[\"\\u-00A\"]"));
}
// Verifies invalid utf-8 characters are replaced.
TEST_F(JSONParserTest, ReplaceInvalidCharacters) {
const std::string bogus_char = "ó¿¿¿";
const std::string quoted_bogus_char = "\"" + bogus_char + "\"";
std::unique_ptr<JSONParser> parser(
NewTestParser(quoted_bogus_char, JSON_REPLACE_INVALID_CHARACTERS));
std::unique_ptr<Value> value(parser->ConsumeString());
ASSERT_TRUE(value.get());
std::string str;
EXPECT_TRUE(value->GetAsString(&str));
EXPECT_EQ(kUnicodeReplacementString, str);
}
} // namespace internal
} // namespace base
|