| OLD | NEW |
| 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_reader.h" | 5 #include "base/json/json_reader.h" |
| 6 | 6 |
| 7 #include "base/base_paths.h" | 7 #include "base/base_paths.h" |
| 8 #include "base/files/file_util.h" | 8 #include "base/files/file_util.h" |
| 9 #include "base/logging.h" | 9 #include "base/logging.h" |
| 10 #include "base/memory/scoped_ptr.h" | 10 #include "base/memory/scoped_ptr.h" |
| 11 #include "base/path_service.h" | 11 #include "base/path_service.h" |
| 12 #include "base/strings/string_piece.h" | 12 #include "base/strings/string_piece.h" |
| 13 #include "base/strings/utf_string_conversions.h" | 13 #include "base/strings/utf_string_conversions.h" |
| 14 #include "base/values.h" | 14 #include "base/values.h" |
| 15 #include "build/build_config.h" | 15 #include "build/build_config.h" |
| 16 #include "testing/gtest/include/gtest/gtest.h" | 16 #include "testing/gtest/include/gtest/gtest.h" |
| 17 | 17 |
| 18 namespace base { | 18 namespace base { |
| 19 | 19 |
| 20 TEST(JSONReaderTest, Reading) { | 20 TEST(JSONReaderTest, Reading) { |
| 21 // some whitespace checking | 21 // some whitespace checking |
| 22 scoped_ptr<Value> root; | 22 scoped_ptr<Value> root; |
| 23 root.reset(JSONReader().ReadToValue(" null ")); | 23 root = JSONReader().ReadToValue(" null "); |
| 24 ASSERT_TRUE(root.get()); | 24 ASSERT_TRUE(root.get()); |
| 25 EXPECT_TRUE(root->IsType(Value::TYPE_NULL)); | 25 EXPECT_TRUE(root->IsType(Value::TYPE_NULL)); |
| 26 | 26 |
| 27 // Invalid JSON string | 27 // Invalid JSON string |
| 28 root.reset(JSONReader().ReadToValue("nu")); | 28 root = JSONReader().ReadToValue("nu"); |
| 29 EXPECT_FALSE(root.get()); | 29 EXPECT_FALSE(root.get()); |
| 30 | 30 |
| 31 // Simple bool | 31 // Simple bool |
| 32 root.reset(JSONReader().ReadToValue("true ")); | 32 root = JSONReader().ReadToValue("true "); |
| 33 ASSERT_TRUE(root.get()); | 33 ASSERT_TRUE(root.get()); |
| 34 EXPECT_TRUE(root->IsType(Value::TYPE_BOOLEAN)); | 34 EXPECT_TRUE(root->IsType(Value::TYPE_BOOLEAN)); |
| 35 | 35 |
| 36 // Embedded comment | 36 // Embedded comment |
| 37 root.reset(JSONReader().ReadToValue("/* comment */null")); | 37 root = JSONReader().ReadToValue("/* comment */null"); |
| 38 ASSERT_TRUE(root.get()); | 38 ASSERT_TRUE(root.get()); |
| 39 EXPECT_TRUE(root->IsType(Value::TYPE_NULL)); | 39 EXPECT_TRUE(root->IsType(Value::TYPE_NULL)); |
| 40 root.reset(JSONReader().ReadToValue("40 /* comment */")); | 40 root = JSONReader().ReadToValue("40 /* comment */"); |
| 41 ASSERT_TRUE(root.get()); | 41 ASSERT_TRUE(root.get()); |
| 42 EXPECT_TRUE(root->IsType(Value::TYPE_INTEGER)); | 42 EXPECT_TRUE(root->IsType(Value::TYPE_INTEGER)); |
| 43 root.reset(JSONReader().ReadToValue("true // comment")); | 43 root = JSONReader().ReadToValue("true // comment"); |
| 44 ASSERT_TRUE(root.get()); | 44 ASSERT_TRUE(root.get()); |
| 45 EXPECT_TRUE(root->IsType(Value::TYPE_BOOLEAN)); | 45 EXPECT_TRUE(root->IsType(Value::TYPE_BOOLEAN)); |
| 46 root.reset(JSONReader().ReadToValue("/* comment */\"sample string\"")); | 46 root = JSONReader().ReadToValue("/* comment */\"sample string\""); |
| 47 ASSERT_TRUE(root.get()); | 47 ASSERT_TRUE(root.get()); |
| 48 EXPECT_TRUE(root->IsType(Value::TYPE_STRING)); | 48 EXPECT_TRUE(root->IsType(Value::TYPE_STRING)); |
| 49 std::string value; | 49 std::string value; |
| 50 EXPECT_TRUE(root->GetAsString(&value)); | 50 EXPECT_TRUE(root->GetAsString(&value)); |
| 51 EXPECT_EQ("sample string", value); | 51 EXPECT_EQ("sample string", value); |
| 52 root.reset(JSONReader().ReadToValue("[1, /* comment, 2 ] */ \n 3]")); | 52 root = JSONReader().ReadToValue("[1, /* comment, 2 ] */ \n 3]"); |
| 53 ASSERT_TRUE(root.get()); | 53 ASSERT_TRUE(root.get()); |
| 54 ListValue* list = static_cast<ListValue*>(root.get()); | 54 ListValue* list = static_cast<ListValue*>(root.get()); |
| 55 EXPECT_EQ(2u, list->GetSize()); | 55 EXPECT_EQ(2u, list->GetSize()); |
| 56 int int_val = 0; | 56 int int_val = 0; |
| 57 EXPECT_TRUE(list->GetInteger(0, &int_val)); | 57 EXPECT_TRUE(list->GetInteger(0, &int_val)); |
| 58 EXPECT_EQ(1, int_val); | 58 EXPECT_EQ(1, int_val); |
| 59 EXPECT_TRUE(list->GetInteger(1, &int_val)); | 59 EXPECT_TRUE(list->GetInteger(1, &int_val)); |
| 60 EXPECT_EQ(3, int_val); | 60 EXPECT_EQ(3, int_val); |
| 61 root.reset(JSONReader().ReadToValue("[1, /*a*/2, 3]")); | 61 root = JSONReader().ReadToValue("[1, /*a*/2, 3]"); |
| 62 ASSERT_TRUE(root.get()); | 62 ASSERT_TRUE(root.get()); |
| 63 list = static_cast<ListValue*>(root.get()); | 63 list = static_cast<ListValue*>(root.get()); |
| 64 EXPECT_EQ(3u, list->GetSize()); | 64 EXPECT_EQ(3u, list->GetSize()); |
| 65 root.reset(JSONReader().ReadToValue("/* comment **/42")); | 65 root = JSONReader().ReadToValue("/* comment **/42"); |
| 66 ASSERT_TRUE(root.get()); | 66 ASSERT_TRUE(root.get()); |
| 67 EXPECT_TRUE(root->IsType(Value::TYPE_INTEGER)); | 67 EXPECT_TRUE(root->IsType(Value::TYPE_INTEGER)); |
| 68 EXPECT_TRUE(root->GetAsInteger(&int_val)); | 68 EXPECT_TRUE(root->GetAsInteger(&int_val)); |
| 69 EXPECT_EQ(42, int_val); | 69 EXPECT_EQ(42, int_val); |
| 70 root.reset(JSONReader().ReadToValue( | 70 root = JSONReader().ReadToValue( |
| 71 "/* comment **/\n" | 71 "/* comment **/\n" |
| 72 "// */ 43\n" | 72 "// */ 43\n" |
| 73 "44")); | 73 "44"); |
| 74 ASSERT_TRUE(root.get()); | 74 ASSERT_TRUE(root.get()); |
| 75 EXPECT_TRUE(root->IsType(Value::TYPE_INTEGER)); | 75 EXPECT_TRUE(root->IsType(Value::TYPE_INTEGER)); |
| 76 EXPECT_TRUE(root->GetAsInteger(&int_val)); | 76 EXPECT_TRUE(root->GetAsInteger(&int_val)); |
| 77 EXPECT_EQ(44, int_val); | 77 EXPECT_EQ(44, int_val); |
| 78 | 78 |
| 79 // Test number formats | 79 // Test number formats |
| 80 root.reset(JSONReader().ReadToValue("43")); | 80 root = JSONReader().ReadToValue("43"); |
| 81 ASSERT_TRUE(root.get()); | 81 ASSERT_TRUE(root.get()); |
| 82 EXPECT_TRUE(root->IsType(Value::TYPE_INTEGER)); | 82 EXPECT_TRUE(root->IsType(Value::TYPE_INTEGER)); |
| 83 EXPECT_TRUE(root->GetAsInteger(&int_val)); | 83 EXPECT_TRUE(root->GetAsInteger(&int_val)); |
| 84 EXPECT_EQ(43, int_val); | 84 EXPECT_EQ(43, int_val); |
| 85 | 85 |
| 86 // According to RFC4627, oct, hex, and leading zeros are invalid JSON. | 86 // According to RFC4627, oct, hex, and leading zeros are invalid JSON. |
| 87 root.reset(JSONReader().ReadToValue("043")); | 87 root = JSONReader().ReadToValue("043"); |
| 88 EXPECT_FALSE(root.get()); | 88 EXPECT_FALSE(root.get()); |
| 89 root.reset(JSONReader().ReadToValue("0x43")); | 89 root = JSONReader().ReadToValue("0x43"); |
| 90 EXPECT_FALSE(root.get()); | 90 EXPECT_FALSE(root.get()); |
| 91 root.reset(JSONReader().ReadToValue("00")); | 91 root = JSONReader().ReadToValue("00"); |
| 92 EXPECT_FALSE(root.get()); | 92 EXPECT_FALSE(root.get()); |
| 93 | 93 |
| 94 // Test 0 (which needs to be special cased because of the leading zero | 94 // Test 0 (which needs to be special cased because of the leading zero |
| 95 // clause). | 95 // clause). |
| 96 root.reset(JSONReader().ReadToValue("0")); | 96 root = JSONReader().ReadToValue("0"); |
| 97 ASSERT_TRUE(root.get()); | 97 ASSERT_TRUE(root.get()); |
| 98 EXPECT_TRUE(root->IsType(Value::TYPE_INTEGER)); | 98 EXPECT_TRUE(root->IsType(Value::TYPE_INTEGER)); |
| 99 int_val = 1; | 99 int_val = 1; |
| 100 EXPECT_TRUE(root->GetAsInteger(&int_val)); | 100 EXPECT_TRUE(root->GetAsInteger(&int_val)); |
| 101 EXPECT_EQ(0, int_val); | 101 EXPECT_EQ(0, int_val); |
| 102 | 102 |
| 103 // Numbers that overflow ints should succeed, being internally promoted to | 103 // Numbers that overflow ints should succeed, being internally promoted to |
| 104 // storage as doubles | 104 // storage as doubles |
| 105 root.reset(JSONReader().ReadToValue("2147483648")); | 105 root = JSONReader().ReadToValue("2147483648"); |
| 106 ASSERT_TRUE(root.get()); | 106 ASSERT_TRUE(root.get()); |
| 107 double double_val; | 107 double double_val; |
| 108 EXPECT_TRUE(root->IsType(Value::TYPE_DOUBLE)); | 108 EXPECT_TRUE(root->IsType(Value::TYPE_DOUBLE)); |
| 109 double_val = 0.0; | 109 double_val = 0.0; |
| 110 EXPECT_TRUE(root->GetAsDouble(&double_val)); | 110 EXPECT_TRUE(root->GetAsDouble(&double_val)); |
| 111 EXPECT_DOUBLE_EQ(2147483648.0, double_val); | 111 EXPECT_DOUBLE_EQ(2147483648.0, double_val); |
| 112 root.reset(JSONReader().ReadToValue("-2147483649")); | 112 root = JSONReader().ReadToValue("-2147483649"); |
| 113 ASSERT_TRUE(root.get()); | 113 ASSERT_TRUE(root.get()); |
| 114 EXPECT_TRUE(root->IsType(Value::TYPE_DOUBLE)); | 114 EXPECT_TRUE(root->IsType(Value::TYPE_DOUBLE)); |
| 115 double_val = 0.0; | 115 double_val = 0.0; |
| 116 EXPECT_TRUE(root->GetAsDouble(&double_val)); | 116 EXPECT_TRUE(root->GetAsDouble(&double_val)); |
| 117 EXPECT_DOUBLE_EQ(-2147483649.0, double_val); | 117 EXPECT_DOUBLE_EQ(-2147483649.0, double_val); |
| 118 | 118 |
| 119 // Parse a double | 119 // Parse a double |
| 120 root.reset(JSONReader().ReadToValue("43.1")); | 120 root = JSONReader().ReadToValue("43.1"); |
| 121 ASSERT_TRUE(root.get()); | 121 ASSERT_TRUE(root.get()); |
| 122 EXPECT_TRUE(root->IsType(Value::TYPE_DOUBLE)); | 122 EXPECT_TRUE(root->IsType(Value::TYPE_DOUBLE)); |
| 123 double_val = 0.0; | 123 double_val = 0.0; |
| 124 EXPECT_TRUE(root->GetAsDouble(&double_val)); | 124 EXPECT_TRUE(root->GetAsDouble(&double_val)); |
| 125 EXPECT_DOUBLE_EQ(43.1, double_val); | 125 EXPECT_DOUBLE_EQ(43.1, double_val); |
| 126 | 126 |
| 127 root.reset(JSONReader().ReadToValue("4.3e-1")); | 127 root = JSONReader().ReadToValue("4.3e-1"); |
| 128 ASSERT_TRUE(root.get()); | 128 ASSERT_TRUE(root.get()); |
| 129 EXPECT_TRUE(root->IsType(Value::TYPE_DOUBLE)); | 129 EXPECT_TRUE(root->IsType(Value::TYPE_DOUBLE)); |
| 130 double_val = 0.0; | 130 double_val = 0.0; |
| 131 EXPECT_TRUE(root->GetAsDouble(&double_val)); | 131 EXPECT_TRUE(root->GetAsDouble(&double_val)); |
| 132 EXPECT_DOUBLE_EQ(.43, double_val); | 132 EXPECT_DOUBLE_EQ(.43, double_val); |
| 133 | 133 |
| 134 root.reset(JSONReader().ReadToValue("2.1e0")); | 134 root = JSONReader().ReadToValue("2.1e0"); |
| 135 ASSERT_TRUE(root.get()); | 135 ASSERT_TRUE(root.get()); |
| 136 EXPECT_TRUE(root->IsType(Value::TYPE_DOUBLE)); | 136 EXPECT_TRUE(root->IsType(Value::TYPE_DOUBLE)); |
| 137 double_val = 0.0; | 137 double_val = 0.0; |
| 138 EXPECT_TRUE(root->GetAsDouble(&double_val)); | 138 EXPECT_TRUE(root->GetAsDouble(&double_val)); |
| 139 EXPECT_DOUBLE_EQ(2.1, double_val); | 139 EXPECT_DOUBLE_EQ(2.1, double_val); |
| 140 | 140 |
| 141 root.reset(JSONReader().ReadToValue("2.1e+0001")); | 141 root = JSONReader().ReadToValue("2.1e+0001"); |
| 142 ASSERT_TRUE(root.get()); | 142 ASSERT_TRUE(root.get()); |
| 143 EXPECT_TRUE(root->IsType(Value::TYPE_DOUBLE)); | 143 EXPECT_TRUE(root->IsType(Value::TYPE_DOUBLE)); |
| 144 double_val = 0.0; | 144 double_val = 0.0; |
| 145 EXPECT_TRUE(root->GetAsDouble(&double_val)); | 145 EXPECT_TRUE(root->GetAsDouble(&double_val)); |
| 146 EXPECT_DOUBLE_EQ(21.0, double_val); | 146 EXPECT_DOUBLE_EQ(21.0, double_val); |
| 147 | 147 |
| 148 root.reset(JSONReader().ReadToValue("0.01")); | 148 root = JSONReader().ReadToValue("0.01"); |
| 149 ASSERT_TRUE(root.get()); | 149 ASSERT_TRUE(root.get()); |
| 150 EXPECT_TRUE(root->IsType(Value::TYPE_DOUBLE)); | 150 EXPECT_TRUE(root->IsType(Value::TYPE_DOUBLE)); |
| 151 double_val = 0.0; | 151 double_val = 0.0; |
| 152 EXPECT_TRUE(root->GetAsDouble(&double_val)); | 152 EXPECT_TRUE(root->GetAsDouble(&double_val)); |
| 153 EXPECT_DOUBLE_EQ(0.01, double_val); | 153 EXPECT_DOUBLE_EQ(0.01, double_val); |
| 154 | 154 |
| 155 root.reset(JSONReader().ReadToValue("1.00")); | 155 root = JSONReader().ReadToValue("1.00"); |
| 156 ASSERT_TRUE(root.get()); | 156 ASSERT_TRUE(root.get()); |
| 157 EXPECT_TRUE(root->IsType(Value::TYPE_DOUBLE)); | 157 EXPECT_TRUE(root->IsType(Value::TYPE_DOUBLE)); |
| 158 double_val = 0.0; | 158 double_val = 0.0; |
| 159 EXPECT_TRUE(root->GetAsDouble(&double_val)); | 159 EXPECT_TRUE(root->GetAsDouble(&double_val)); |
| 160 EXPECT_DOUBLE_EQ(1.0, double_val); | 160 EXPECT_DOUBLE_EQ(1.0, double_val); |
| 161 | 161 |
| 162 // Fractional parts must have a digit before and after the decimal point. | 162 // Fractional parts must have a digit before and after the decimal point. |
| 163 root.reset(JSONReader().ReadToValue("1.")); | 163 root = JSONReader().ReadToValue("1."); |
| 164 EXPECT_FALSE(root.get()); | 164 EXPECT_FALSE(root.get()); |
| 165 root.reset(JSONReader().ReadToValue(".1")); | 165 root = JSONReader().ReadToValue(".1"); |
| 166 EXPECT_FALSE(root.get()); | 166 EXPECT_FALSE(root.get()); |
| 167 root.reset(JSONReader().ReadToValue("1.e10")); | 167 root = JSONReader().ReadToValue("1.e10"); |
| 168 EXPECT_FALSE(root.get()); | 168 EXPECT_FALSE(root.get()); |
| 169 | 169 |
| 170 // Exponent must have a digit following the 'e'. | 170 // Exponent must have a digit following the 'e'. |
| 171 root.reset(JSONReader().ReadToValue("1e")); | 171 root = JSONReader().ReadToValue("1e"); |
| 172 EXPECT_FALSE(root.get()); | 172 EXPECT_FALSE(root.get()); |
| 173 root.reset(JSONReader().ReadToValue("1E")); | 173 root = JSONReader().ReadToValue("1E"); |
| 174 EXPECT_FALSE(root.get()); | 174 EXPECT_FALSE(root.get()); |
| 175 root.reset(JSONReader().ReadToValue("1e1.")); | 175 root = JSONReader().ReadToValue("1e1."); |
| 176 EXPECT_FALSE(root.get()); | 176 EXPECT_FALSE(root.get()); |
| 177 root.reset(JSONReader().ReadToValue("1e1.0")); | 177 root = JSONReader().ReadToValue("1e1.0"); |
| 178 EXPECT_FALSE(root.get()); | 178 EXPECT_FALSE(root.get()); |
| 179 | 179 |
| 180 // INF/-INF/NaN are not valid | 180 // INF/-INF/NaN are not valid |
| 181 root.reset(JSONReader().ReadToValue("1e1000")); | 181 root = JSONReader().ReadToValue("1e1000"); |
| 182 EXPECT_FALSE(root.get()); | 182 EXPECT_FALSE(root.get()); |
| 183 root.reset(JSONReader().ReadToValue("-1e1000")); | 183 root = JSONReader().ReadToValue("-1e1000"); |
| 184 EXPECT_FALSE(root.get()); | 184 EXPECT_FALSE(root.get()); |
| 185 root.reset(JSONReader().ReadToValue("NaN")); | 185 root = JSONReader().ReadToValue("NaN"); |
| 186 EXPECT_FALSE(root.get()); | 186 EXPECT_FALSE(root.get()); |
| 187 root.reset(JSONReader().ReadToValue("nan")); | 187 root = JSONReader().ReadToValue("nan"); |
| 188 EXPECT_FALSE(root.get()); | 188 EXPECT_FALSE(root.get()); |
| 189 root.reset(JSONReader().ReadToValue("inf")); | 189 root = JSONReader().ReadToValue("inf"); |
| 190 EXPECT_FALSE(root.get()); | 190 EXPECT_FALSE(root.get()); |
| 191 | 191 |
| 192 // Invalid number formats | 192 // Invalid number formats |
| 193 root.reset(JSONReader().ReadToValue("4.3.1")); | 193 root = JSONReader().ReadToValue("4.3.1"); |
| 194 EXPECT_FALSE(root.get()); | 194 EXPECT_FALSE(root.get()); |
| 195 root.reset(JSONReader().ReadToValue("4e3.1")); | 195 root = JSONReader().ReadToValue("4e3.1"); |
| 196 EXPECT_FALSE(root.get()); | 196 EXPECT_FALSE(root.get()); |
| 197 | 197 |
| 198 // Test string parser | 198 // Test string parser |
| 199 root.reset(JSONReader().ReadToValue("\"hello world\"")); | 199 root = JSONReader().ReadToValue("\"hello world\""); |
| 200 ASSERT_TRUE(root.get()); | 200 ASSERT_TRUE(root.get()); |
| 201 EXPECT_TRUE(root->IsType(Value::TYPE_STRING)); | 201 EXPECT_TRUE(root->IsType(Value::TYPE_STRING)); |
| 202 std::string str_val; | 202 std::string str_val; |
| 203 EXPECT_TRUE(root->GetAsString(&str_val)); | 203 EXPECT_TRUE(root->GetAsString(&str_val)); |
| 204 EXPECT_EQ("hello world", str_val); | 204 EXPECT_EQ("hello world", str_val); |
| 205 | 205 |
| 206 // Empty string | 206 // Empty string |
| 207 root.reset(JSONReader().ReadToValue("\"\"")); | 207 root = JSONReader().ReadToValue("\"\""); |
| 208 ASSERT_TRUE(root.get()); | 208 ASSERT_TRUE(root.get()); |
| 209 EXPECT_TRUE(root->IsType(Value::TYPE_STRING)); | 209 EXPECT_TRUE(root->IsType(Value::TYPE_STRING)); |
| 210 str_val.clear(); | 210 str_val.clear(); |
| 211 EXPECT_TRUE(root->GetAsString(&str_val)); | 211 EXPECT_TRUE(root->GetAsString(&str_val)); |
| 212 EXPECT_EQ("", str_val); | 212 EXPECT_EQ("", str_val); |
| 213 | 213 |
| 214 // Test basic string escapes | 214 // Test basic string escapes |
| 215 root.reset(JSONReader().ReadToValue("\" \\\"\\\\\\/\\b\\f\\n\\r\\t\\v\"")); | 215 root = JSONReader().ReadToValue("\" \\\"\\\\\\/\\b\\f\\n\\r\\t\\v\""); |
| 216 ASSERT_TRUE(root.get()); | 216 ASSERT_TRUE(root.get()); |
| 217 EXPECT_TRUE(root->IsType(Value::TYPE_STRING)); | 217 EXPECT_TRUE(root->IsType(Value::TYPE_STRING)); |
| 218 str_val.clear(); | 218 str_val.clear(); |
| 219 EXPECT_TRUE(root->GetAsString(&str_val)); | 219 EXPECT_TRUE(root->GetAsString(&str_val)); |
| 220 EXPECT_EQ(" \"\\/\b\f\n\r\t\v", str_val); | 220 EXPECT_EQ(" \"\\/\b\f\n\r\t\v", str_val); |
| 221 | 221 |
| 222 // Test hex and unicode escapes including the null character. | 222 // Test hex and unicode escapes including the null character. |
| 223 root.reset(JSONReader().ReadToValue("\"\\x41\\x00\\u1234\"")); | 223 root = JSONReader().ReadToValue("\"\\x41\\x00\\u1234\""); |
| 224 ASSERT_TRUE(root.get()); | 224 ASSERT_TRUE(root.get()); |
| 225 EXPECT_TRUE(root->IsType(Value::TYPE_STRING)); | 225 EXPECT_TRUE(root->IsType(Value::TYPE_STRING)); |
| 226 str_val.clear(); | 226 str_val.clear(); |
| 227 EXPECT_TRUE(root->GetAsString(&str_val)); | 227 EXPECT_TRUE(root->GetAsString(&str_val)); |
| 228 EXPECT_EQ(std::wstring(L"A\0\x1234", 3), UTF8ToWide(str_val)); | 228 EXPECT_EQ(std::wstring(L"A\0\x1234", 3), UTF8ToWide(str_val)); |
| 229 | 229 |
| 230 // Test invalid strings | 230 // Test invalid strings |
| 231 root.reset(JSONReader().ReadToValue("\"no closing quote")); | 231 root = JSONReader().ReadToValue("\"no closing quote"); |
| 232 EXPECT_FALSE(root.get()); | 232 EXPECT_FALSE(root.get()); |
| 233 root.reset(JSONReader().ReadToValue("\"\\z invalid escape char\"")); | 233 root = JSONReader().ReadToValue("\"\\z invalid escape char\""); |
| 234 EXPECT_FALSE(root.get()); | 234 EXPECT_FALSE(root.get()); |
| 235 root.reset(JSONReader().ReadToValue("\"\\xAQ invalid hex code\"")); | 235 root = JSONReader().ReadToValue("\"\\xAQ invalid hex code\""); |
| 236 EXPECT_FALSE(root.get()); | 236 EXPECT_FALSE(root.get()); |
| 237 root.reset(JSONReader().ReadToValue("not enough hex chars\\x1\"")); | 237 root = JSONReader().ReadToValue("not enough hex chars\\x1\""); |
| 238 EXPECT_FALSE(root.get()); | 238 EXPECT_FALSE(root.get()); |
| 239 root.reset(JSONReader().ReadToValue("\"not enough escape chars\\u123\"")); | 239 root = JSONReader().ReadToValue("\"not enough escape chars\\u123\""); |
| 240 EXPECT_FALSE(root.get()); | 240 EXPECT_FALSE(root.get()); |
| 241 root.reset(JSONReader().ReadToValue("\"extra backslash at end of input\\\"")); | 241 root = JSONReader().ReadToValue("\"extra backslash at end of input\\\""); |
| 242 EXPECT_FALSE(root.get()); | 242 EXPECT_FALSE(root.get()); |
| 243 | 243 |
| 244 // Basic array | 244 // Basic array |
| 245 root.reset(JSONReader::Read("[true, false, null]")); | 245 root.reset(JSONReader::Read("[true, false, null]")); |
| 246 ASSERT_TRUE(root.get()); | 246 ASSERT_TRUE(root.get()); |
| 247 EXPECT_TRUE(root->IsType(Value::TYPE_LIST)); | 247 EXPECT_TRUE(root->IsType(Value::TYPE_LIST)); |
| 248 list = static_cast<ListValue*>(root.get()); | 248 list = static_cast<ListValue*>(root.get()); |
| 249 EXPECT_EQ(3U, list->GetSize()); | 249 EXPECT_EQ(3U, list->GetSize()); |
| 250 | 250 |
| 251 // Test with trailing comma. Should be parsed the same as above. | 251 // Test with trailing comma. Should be parsed the same as above. |
| (...skipping 207 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 459 not_evil.append("[],"); | 459 not_evil.append("[],"); |
| 460 } | 460 } |
| 461 not_evil.append("[]]"); | 461 not_evil.append("[]]"); |
| 462 root.reset(JSONReader::Read(not_evil)); | 462 root.reset(JSONReader::Read(not_evil)); |
| 463 ASSERT_TRUE(root.get()); | 463 ASSERT_TRUE(root.get()); |
| 464 EXPECT_TRUE(root->IsType(Value::TYPE_LIST)); | 464 EXPECT_TRUE(root->IsType(Value::TYPE_LIST)); |
| 465 list = static_cast<ListValue*>(root.get()); | 465 list = static_cast<ListValue*>(root.get()); |
| 466 EXPECT_EQ(5001U, list->GetSize()); | 466 EXPECT_EQ(5001U, list->GetSize()); |
| 467 | 467 |
| 468 // Test utf8 encoded input | 468 // Test utf8 encoded input |
| 469 root.reset(JSONReader().ReadToValue("\"\xe7\xbd\x91\xe9\xa1\xb5\"")); | 469 root = JSONReader().ReadToValue("\"\xe7\xbd\x91\xe9\xa1\xb5\""); |
| 470 ASSERT_TRUE(root.get()); | 470 ASSERT_TRUE(root.get()); |
| 471 EXPECT_TRUE(root->IsType(Value::TYPE_STRING)); | 471 EXPECT_TRUE(root->IsType(Value::TYPE_STRING)); |
| 472 str_val.clear(); | 472 str_val.clear(); |
| 473 EXPECT_TRUE(root->GetAsString(&str_val)); | 473 EXPECT_TRUE(root->GetAsString(&str_val)); |
| 474 EXPECT_EQ(L"\x7f51\x9875", UTF8ToWide(str_val)); | 474 EXPECT_EQ(L"\x7f51\x9875", UTF8ToWide(str_val)); |
| 475 | 475 |
| 476 root.reset(JSONReader().ReadToValue( | 476 root = JSONReader().ReadToValue( |
| 477 "{\"path\": \"/tmp/\xc3\xa0\xc3\xa8\xc3\xb2.png\"}")); | 477 "{\"path\": \"/tmp/\xc3\xa0\xc3\xa8\xc3\xb2.png\"}"); |
| 478 ASSERT_TRUE(root.get()); | 478 ASSERT_TRUE(root.get()); |
| 479 EXPECT_TRUE(root->IsType(Value::TYPE_DICTIONARY)); | 479 EXPECT_TRUE(root->IsType(Value::TYPE_DICTIONARY)); |
| 480 EXPECT_TRUE(root->GetAsDictionary(&dict_val)); | 480 EXPECT_TRUE(root->GetAsDictionary(&dict_val)); |
| 481 EXPECT_TRUE(dict_val->GetString("path", &str_val)); | 481 EXPECT_TRUE(dict_val->GetString("path", &str_val)); |
| 482 EXPECT_EQ("/tmp/\xC3\xA0\xC3\xA8\xC3\xB2.png", str_val); | 482 EXPECT_EQ("/tmp/\xC3\xA0\xC3\xA8\xC3\xB2.png", str_val); |
| 483 | 483 |
| 484 // Test invalid utf8 encoded input | 484 // Test invalid utf8 encoded input |
| 485 root.reset(JSONReader().ReadToValue("\"345\xb0\xa1\xb0\xa2\"")); | 485 root = JSONReader().ReadToValue("\"345\xb0\xa1\xb0\xa2\""); |
| 486 EXPECT_FALSE(root.get()); | 486 EXPECT_FALSE(root.get()); |
| 487 root.reset(JSONReader().ReadToValue("\"123\xc0\x81\"")); | 487 root = JSONReader().ReadToValue("\"123\xc0\x81\""); |
| 488 EXPECT_FALSE(root.get()); | 488 EXPECT_FALSE(root.get()); |
| 489 root.reset(JSONReader().ReadToValue("\"abc\xc0\xae\"")); | 489 root = JSONReader().ReadToValue("\"abc\xc0\xae\""); |
| 490 EXPECT_FALSE(root.get()); | 490 EXPECT_FALSE(root.get()); |
| 491 | 491 |
| 492 // Test utf16 encoded strings. | 492 // Test utf16 encoded strings. |
| 493 root.reset(JSONReader().ReadToValue("\"\\u20ac3,14\"")); | 493 root = JSONReader().ReadToValue("\"\\u20ac3,14\""); |
| 494 ASSERT_TRUE(root.get()); | 494 ASSERT_TRUE(root.get()); |
| 495 EXPECT_TRUE(root->IsType(Value::TYPE_STRING)); | 495 EXPECT_TRUE(root->IsType(Value::TYPE_STRING)); |
| 496 str_val.clear(); | 496 str_val.clear(); |
| 497 EXPECT_TRUE(root->GetAsString(&str_val)); | 497 EXPECT_TRUE(root->GetAsString(&str_val)); |
| 498 EXPECT_EQ("\xe2\x82\xac""3,14", str_val); | 498 EXPECT_EQ("\xe2\x82\xac""3,14", str_val); |
| 499 | 499 |
| 500 root.reset(JSONReader().ReadToValue("\"\\ud83d\\udca9\\ud83d\\udc6c\"")); | 500 root = JSONReader().ReadToValue("\"\\ud83d\\udca9\\ud83d\\udc6c\""); |
| 501 ASSERT_TRUE(root.get()); | 501 ASSERT_TRUE(root.get()); |
| 502 EXPECT_TRUE(root->IsType(Value::TYPE_STRING)); | 502 EXPECT_TRUE(root->IsType(Value::TYPE_STRING)); |
| 503 str_val.clear(); | 503 str_val.clear(); |
| 504 EXPECT_TRUE(root->GetAsString(&str_val)); | 504 EXPECT_TRUE(root->GetAsString(&str_val)); |
| 505 EXPECT_EQ("\xf0\x9f\x92\xa9\xf0\x9f\x91\xac", str_val); | 505 EXPECT_EQ("\xf0\x9f\x92\xa9\xf0\x9f\x91\xac", str_val); |
| 506 | 506 |
| 507 // Test invalid utf16 strings. | 507 // Test invalid utf16 strings. |
| 508 const char* const cases[] = { | 508 const char* const cases[] = { |
| 509 "\"\\u123\"", // Invalid scalar. | 509 "\"\\u123\"", // Invalid scalar. |
| 510 "\"\\ud83d\"", // Invalid scalar. | 510 "\"\\ud83d\"", // Invalid scalar. |
| 511 "\"\\u$%@!\"", // Invalid scalar. | 511 "\"\\u$%@!\"", // Invalid scalar. |
| 512 "\"\\uzz89\"", // Invalid scalar. | 512 "\"\\uzz89\"", // Invalid scalar. |
| 513 "\"\\ud83d\\udca\"", // Invalid lower surrogate. | 513 "\"\\ud83d\\udca\"", // Invalid lower surrogate. |
| 514 "\"\\ud83d\\ud83d\"", // Invalid lower surrogate. | 514 "\"\\ud83d\\ud83d\"", // Invalid lower surrogate. |
| 515 "\"\\ud83foo\"", // No lower surrogate. | 515 "\"\\ud83foo\"", // No lower surrogate. |
| 516 "\"\\ud83\\foo\"" // No lower surrogate. | 516 "\"\\ud83\\foo\"" // No lower surrogate. |
| 517 }; | 517 }; |
| 518 for (size_t i = 0; i < arraysize(cases); ++i) { | 518 for (size_t i = 0; i < arraysize(cases); ++i) { |
| 519 root.reset(JSONReader().ReadToValue(cases[i])); | 519 root = JSONReader().ReadToValue(cases[i]); |
| 520 EXPECT_FALSE(root.get()) << cases[i]; | 520 EXPECT_FALSE(root.get()) << cases[i]; |
| 521 } | 521 } |
| 522 | 522 |
| 523 // Test literal root objects. | 523 // Test literal root objects. |
| 524 root.reset(JSONReader::Read("null")); | 524 root.reset(JSONReader::Read("null")); |
| 525 EXPECT_TRUE(root->IsType(Value::TYPE_NULL)); | 525 EXPECT_TRUE(root->IsType(Value::TYPE_NULL)); |
| 526 | 526 |
| 527 root.reset(JSONReader::Read("true")); | 527 root.reset(JSONReader::Read("true")); |
| 528 ASSERT_TRUE(root.get()); | 528 ASSERT_TRUE(root.get()); |
| 529 EXPECT_TRUE(root->GetAsBoolean(&bool_value)); | 529 EXPECT_TRUE(root->GetAsBoolean(&bool_value)); |
| (...skipping 117 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 647 | 647 |
| 648 TEST(JSONReaderTest, IllegalTrailingNull) { | 648 TEST(JSONReaderTest, IllegalTrailingNull) { |
| 649 const char json[] = { '"', 'n', 'u', 'l', 'l', '"', '\0' }; | 649 const char json[] = { '"', 'n', 'u', 'l', 'l', '"', '\0' }; |
| 650 std::string json_string(json, sizeof(json)); | 650 std::string json_string(json, sizeof(json)); |
| 651 JSONReader reader; | 651 JSONReader reader; |
| 652 EXPECT_FALSE(reader.ReadToValue(json_string)); | 652 EXPECT_FALSE(reader.ReadToValue(json_string)); |
| 653 EXPECT_EQ(JSONReader::JSON_UNEXPECTED_DATA_AFTER_ROOT, reader.error_code()); | 653 EXPECT_EQ(JSONReader::JSON_UNEXPECTED_DATA_AFTER_ROOT, reader.error_code()); |
| 654 } | 654 } |
| 655 | 655 |
| 656 } // namespace base | 656 } // namespace base |
| OLD | NEW |