license.bot | f003cfe | 2008-08-24 09:55:55 +0900 | [diff] [blame^] | 1 | // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 4 | |
| 5 | #include "testing/gtest/include/gtest/gtest.h" |
| 6 | #include "base/json_reader.h" |
| 7 | #include "base/values.h" |
tc@google.com | d3bb16f | 2008-08-09 02:26:42 +0900 | [diff] [blame] | 8 | #include "build/build_config.h" |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 9 | |
| 10 | TEST(JSONReaderTest, Reading) { |
| 11 | // some whitespace checking |
| 12 | Value* root = NULL; |
tc@google.com | ce6a78d | 2008-07-29 09:01:31 +0900 | [diff] [blame] | 13 | ASSERT_TRUE(JSONReader::JsonToValue(" null ", &root, false, false)); |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 14 | ASSERT_TRUE(root); |
| 15 | ASSERT_TRUE(root->IsType(Value::TYPE_NULL)); |
| 16 | delete root; |
| 17 | |
| 18 | // Invalid JSON string |
| 19 | root = NULL; |
tc@google.com | ce6a78d | 2008-07-29 09:01:31 +0900 | [diff] [blame] | 20 | ASSERT_FALSE(JSONReader::JsonToValue("nu", &root, false, false)); |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 21 | ASSERT_FALSE(root); |
| 22 | |
| 23 | // Simple bool |
| 24 | root = NULL; |
tc@google.com | ce6a78d | 2008-07-29 09:01:31 +0900 | [diff] [blame] | 25 | ASSERT_TRUE(JSONReader::JsonToValue("true ", &root, false, false)); |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 26 | ASSERT_TRUE(root); |
| 27 | ASSERT_TRUE(root->IsType(Value::TYPE_BOOLEAN)); |
| 28 | delete root; |
| 29 | |
| 30 | // Test number formats |
| 31 | root = NULL; |
tc@google.com | ce6a78d | 2008-07-29 09:01:31 +0900 | [diff] [blame] | 32 | ASSERT_TRUE(JSONReader::JsonToValue("43", &root, false, false)); |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 33 | ASSERT_TRUE(root); |
| 34 | ASSERT_TRUE(root->IsType(Value::TYPE_INTEGER)); |
| 35 | int int_val = 0; |
| 36 | ASSERT_TRUE(root->GetAsInteger(&int_val)); |
| 37 | ASSERT_EQ(43, int_val); |
| 38 | delete root; |
| 39 | |
| 40 | // According to RFC4627, oct, hex, and leading zeros are invalid JSON. |
| 41 | root = NULL; |
tc@google.com | ce6a78d | 2008-07-29 09:01:31 +0900 | [diff] [blame] | 42 | ASSERT_FALSE(JSONReader::JsonToValue("043", &root, false, false)); |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 43 | ASSERT_FALSE(root); |
| 44 | root = NULL; |
tc@google.com | ce6a78d | 2008-07-29 09:01:31 +0900 | [diff] [blame] | 45 | ASSERT_FALSE(JSONReader::JsonToValue("0x43", &root, false, false)); |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 46 | ASSERT_FALSE(root); |
| 47 | root = NULL; |
tc@google.com | ce6a78d | 2008-07-29 09:01:31 +0900 | [diff] [blame] | 48 | ASSERT_FALSE(JSONReader::JsonToValue("00", &root, false, false)); |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 49 | ASSERT_FALSE(root); |
| 50 | |
| 51 | // Test 0 (which needs to be special cased because of the leading zero |
| 52 | // clause). |
| 53 | root = NULL; |
tc@google.com | ce6a78d | 2008-07-29 09:01:31 +0900 | [diff] [blame] | 54 | ASSERT_TRUE(JSONReader::JsonToValue("0", &root, false, false)); |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 55 | ASSERT_TRUE(root); |
| 56 | ASSERT_TRUE(root->IsType(Value::TYPE_INTEGER)); |
| 57 | int_val = 1; |
| 58 | ASSERT_TRUE(root->GetAsInteger(&int_val)); |
| 59 | ASSERT_EQ(0, int_val); |
| 60 | delete root; |
| 61 | |
mmentovai@google.com | 8dcf71c | 2008-08-08 02:15:41 +0900 | [diff] [blame] | 62 | // Numbers that overflow ints should succeed, being internally promoted to |
| 63 | // storage as doubles |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 64 | root = NULL; |
mmentovai@google.com | 8dcf71c | 2008-08-08 02:15:41 +0900 | [diff] [blame] | 65 | ASSERT_TRUE(JSONReader::JsonToValue("2147483648", &root, false, false)); |
| 66 | ASSERT_TRUE(root); |
tc@google.com | d3bb16f | 2008-08-09 02:26:42 +0900 | [diff] [blame] | 67 | double real_val; |
| 68 | #ifdef ARCH_CPU_32_BITS |
| 69 | ASSERT_TRUE(root->IsType(Value::TYPE_REAL)); |
| 70 | real_val = 0.0; |
| 71 | ASSERT_TRUE(root->GetAsReal(&real_val)); |
| 72 | ASSERT_DOUBLE_EQ(2147483648.0, real_val); |
| 73 | #else |
| 74 | ASSERT_TRUE(root->IsType(Value::TYPE_INTEGER)); |
| 75 | int_val = 0; |
| 76 | ASSERT_TRUE(root->GetAsInteger(&int_val)); |
| 77 | ASSERT_EQ(2147483648, int_val); |
| 78 | #endif |
| 79 | delete root; |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 80 | root = NULL; |
mmentovai@google.com | 8dcf71c | 2008-08-08 02:15:41 +0900 | [diff] [blame] | 81 | ASSERT_TRUE(JSONReader::JsonToValue("-2147483649", &root, false, false)); |
| 82 | ASSERT_TRUE(root); |
tc@google.com | d3bb16f | 2008-08-09 02:26:42 +0900 | [diff] [blame] | 83 | #ifdef ARCH_CPU_32_BITS |
| 84 | ASSERT_TRUE(root->IsType(Value::TYPE_REAL)); |
| 85 | real_val = 0.0; |
| 86 | ASSERT_TRUE(root->GetAsReal(&real_val)); |
| 87 | ASSERT_DOUBLE_EQ(-2147483649.0, real_val); |
| 88 | #else |
| 89 | ASSERT_TRUE(root->IsType(Value::TYPE_INTEGER)); |
| 90 | int_val = 0; |
| 91 | ASSERT_TRUE(root->GetAsInteger(&int_val)); |
| 92 | ASSERT_EQ(-2147483649, int_val); |
| 93 | #endif |
| 94 | delete root; |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 95 | |
| 96 | // Parse a double |
| 97 | root = NULL; |
tc@google.com | ce6a78d | 2008-07-29 09:01:31 +0900 | [diff] [blame] | 98 | ASSERT_TRUE(JSONReader::JsonToValue("43.1", &root, false, false)); |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 99 | ASSERT_TRUE(root); |
| 100 | ASSERT_TRUE(root->IsType(Value::TYPE_REAL)); |
tc@google.com | d3bb16f | 2008-08-09 02:26:42 +0900 | [diff] [blame] | 101 | real_val = 0.0; |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 102 | ASSERT_TRUE(root->GetAsReal(&real_val)); |
| 103 | ASSERT_DOUBLE_EQ(43.1, real_val); |
| 104 | delete root; |
| 105 | |
| 106 | root = NULL; |
tc@google.com | ce6a78d | 2008-07-29 09:01:31 +0900 | [diff] [blame] | 107 | ASSERT_TRUE(JSONReader::JsonToValue("4.3e-1", &root, false, false)); |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 108 | ASSERT_TRUE(root); |
| 109 | ASSERT_TRUE(root->IsType(Value::TYPE_REAL)); |
| 110 | real_val = 0.0; |
| 111 | ASSERT_TRUE(root->GetAsReal(&real_val)); |
| 112 | ASSERT_DOUBLE_EQ(.43, real_val); |
| 113 | delete root; |
| 114 | |
| 115 | root = NULL; |
tc@google.com | ce6a78d | 2008-07-29 09:01:31 +0900 | [diff] [blame] | 116 | ASSERT_TRUE(JSONReader::JsonToValue("2.1e0", &root, false, false)); |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 117 | ASSERT_TRUE(root); |
| 118 | ASSERT_TRUE(root->IsType(Value::TYPE_REAL)); |
| 119 | real_val = 0.0; |
| 120 | ASSERT_TRUE(root->GetAsReal(&real_val)); |
| 121 | ASSERT_DOUBLE_EQ(2.1, real_val); |
| 122 | delete root; |
| 123 | |
| 124 | root = NULL; |
tc@google.com | ce6a78d | 2008-07-29 09:01:31 +0900 | [diff] [blame] | 125 | ASSERT_TRUE(JSONReader::JsonToValue("2.1e+0001", &root, false, false)); |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 126 | ASSERT_TRUE(root); |
| 127 | ASSERT_TRUE(root->IsType(Value::TYPE_REAL)); |
| 128 | real_val = 0.0; |
| 129 | ASSERT_TRUE(root->GetAsReal(&real_val)); |
| 130 | ASSERT_DOUBLE_EQ(21.0, real_val); |
| 131 | delete root; |
| 132 | |
| 133 | root = NULL; |
tc@google.com | ce6a78d | 2008-07-29 09:01:31 +0900 | [diff] [blame] | 134 | ASSERT_TRUE(JSONReader::JsonToValue("0.01", &root, false, false)); |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 135 | ASSERT_TRUE(root); |
| 136 | ASSERT_TRUE(root->IsType(Value::TYPE_REAL)); |
| 137 | real_val = 0.0; |
| 138 | ASSERT_TRUE(root->GetAsReal(&real_val)); |
| 139 | ASSERT_DOUBLE_EQ(0.01, real_val); |
| 140 | delete root; |
| 141 | |
| 142 | root = NULL; |
tc@google.com | ce6a78d | 2008-07-29 09:01:31 +0900 | [diff] [blame] | 143 | ASSERT_TRUE(JSONReader::JsonToValue("1.00", &root, false, false)); |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 144 | ASSERT_TRUE(root); |
| 145 | ASSERT_TRUE(root->IsType(Value::TYPE_REAL)); |
| 146 | real_val = 0.0; |
| 147 | ASSERT_TRUE(root->GetAsReal(&real_val)); |
| 148 | ASSERT_DOUBLE_EQ(1.0, real_val); |
| 149 | delete root; |
| 150 | |
| 151 | // Fractional parts must have a digit before and after the decimal point. |
| 152 | root = NULL; |
tc@google.com | ce6a78d | 2008-07-29 09:01:31 +0900 | [diff] [blame] | 153 | ASSERT_FALSE(JSONReader::JsonToValue("1.", &root, false, false)); |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 154 | ASSERT_FALSE(root); |
| 155 | root = NULL; |
tc@google.com | ce6a78d | 2008-07-29 09:01:31 +0900 | [diff] [blame] | 156 | ASSERT_FALSE(JSONReader::JsonToValue(".1", &root, false, false)); |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 157 | ASSERT_FALSE(root); |
| 158 | root = NULL; |
tc@google.com | ce6a78d | 2008-07-29 09:01:31 +0900 | [diff] [blame] | 159 | ASSERT_FALSE(JSONReader::JsonToValue("1.e10", &root, false, false)); |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 160 | ASSERT_FALSE(root); |
| 161 | |
| 162 | // Exponent must have a digit following the 'e'. |
| 163 | root = NULL; |
tc@google.com | ce6a78d | 2008-07-29 09:01:31 +0900 | [diff] [blame] | 164 | ASSERT_FALSE(JSONReader::JsonToValue("1e", &root, false, false)); |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 165 | ASSERT_FALSE(root); |
| 166 | root = NULL; |
tc@google.com | ce6a78d | 2008-07-29 09:01:31 +0900 | [diff] [blame] | 167 | ASSERT_FALSE(JSONReader::JsonToValue("1E", &root, false, false)); |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 168 | ASSERT_FALSE(root); |
| 169 | root = NULL; |
tc@google.com | ce6a78d | 2008-07-29 09:01:31 +0900 | [diff] [blame] | 170 | ASSERT_FALSE(JSONReader::JsonToValue("1e1.", &root, false, false)); |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 171 | ASSERT_FALSE(root); |
| 172 | root = NULL; |
tc@google.com | ce6a78d | 2008-07-29 09:01:31 +0900 | [diff] [blame] | 173 | ASSERT_FALSE(JSONReader::JsonToValue("1e1.0", &root, false, false)); |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 174 | ASSERT_FALSE(root); |
| 175 | |
| 176 | // INF/-INF/NaN are not valid |
| 177 | root = NULL; |
tc@google.com | ce6a78d | 2008-07-29 09:01:31 +0900 | [diff] [blame] | 178 | ASSERT_FALSE(JSONReader::JsonToValue("1e1000", &root, false, false)); |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 179 | ASSERT_FALSE(root); |
| 180 | root = NULL; |
tc@google.com | ce6a78d | 2008-07-29 09:01:31 +0900 | [diff] [blame] | 181 | ASSERT_FALSE(JSONReader::JsonToValue("-1e1000", &root, false, false)); |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 182 | ASSERT_FALSE(root); |
| 183 | root = NULL; |
tc@google.com | ce6a78d | 2008-07-29 09:01:31 +0900 | [diff] [blame] | 184 | ASSERT_FALSE(JSONReader::JsonToValue("NaN", &root, false, false)); |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 185 | ASSERT_FALSE(root); |
| 186 | |
| 187 | // Invalid number formats |
| 188 | root = NULL; |
tc@google.com | ce6a78d | 2008-07-29 09:01:31 +0900 | [diff] [blame] | 189 | ASSERT_FALSE(JSONReader::JsonToValue("4.3.1", &root, false, false)); |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 190 | ASSERT_FALSE(root); |
| 191 | root = NULL; |
tc@google.com | ce6a78d | 2008-07-29 09:01:31 +0900 | [diff] [blame] | 192 | ASSERT_FALSE(JSONReader::JsonToValue("4e3.1", &root, false, false)); |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 193 | ASSERT_FALSE(root); |
| 194 | |
| 195 | // Test string parser |
| 196 | root = NULL; |
tc@google.com | ce6a78d | 2008-07-29 09:01:31 +0900 | [diff] [blame] | 197 | ASSERT_TRUE(JSONReader::JsonToValue("\"hello world\"", &root, false, false)); |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 198 | ASSERT_TRUE(root); |
| 199 | ASSERT_TRUE(root->IsType(Value::TYPE_STRING)); |
| 200 | std::wstring str_val; |
| 201 | ASSERT_TRUE(root->GetAsString(&str_val)); |
| 202 | ASSERT_EQ(L"hello world", str_val); |
| 203 | delete root; |
| 204 | |
| 205 | // Empty string |
| 206 | root = NULL; |
tc@google.com | ce6a78d | 2008-07-29 09:01:31 +0900 | [diff] [blame] | 207 | ASSERT_TRUE(JSONReader::JsonToValue("\"\"", &root, false, false)); |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 208 | ASSERT_TRUE(root); |
| 209 | ASSERT_TRUE(root->IsType(Value::TYPE_STRING)); |
| 210 | str_val.clear(); |
| 211 | ASSERT_TRUE(root->GetAsString(&str_val)); |
| 212 | ASSERT_EQ(L"", str_val); |
| 213 | delete root; |
| 214 | |
| 215 | // Test basic string escapes |
| 216 | root = NULL; |
| 217 | ASSERT_TRUE(JSONReader::JsonToValue("\" \\\"\\\\\\/\\b\\f\\n\\r\\t\"", &root, |
tc@google.com | ce6a78d | 2008-07-29 09:01:31 +0900 | [diff] [blame] | 218 | false, false)); |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 219 | ASSERT_TRUE(root); |
| 220 | ASSERT_TRUE(root->IsType(Value::TYPE_STRING)); |
| 221 | str_val.clear(); |
| 222 | ASSERT_TRUE(root->GetAsString(&str_val)); |
| 223 | ASSERT_EQ(L" \"\\/\b\f\n\r\t", str_val); |
| 224 | delete root; |
| 225 | |
| 226 | // Test hex and unicode escapes including the null character. |
| 227 | root = NULL; |
tc@google.com | ce6a78d | 2008-07-29 09:01:31 +0900 | [diff] [blame] | 228 | ASSERT_TRUE(JSONReader::JsonToValue("\"\\x41\\x00\\u1234\"", &root, false, |
| 229 | false)); |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 230 | ASSERT_TRUE(root); |
| 231 | ASSERT_TRUE(root->IsType(Value::TYPE_STRING)); |
| 232 | str_val.clear(); |
| 233 | ASSERT_TRUE(root->GetAsString(&str_val)); |
| 234 | ASSERT_EQ(std::wstring(L"A\0\x1234", 3), str_val); |
| 235 | delete root; |
| 236 | |
| 237 | // Test invalid strings |
| 238 | root = NULL; |
tc@google.com | ce6a78d | 2008-07-29 09:01:31 +0900 | [diff] [blame] | 239 | ASSERT_FALSE(JSONReader::JsonToValue("\"no closing quote", &root, false, |
| 240 | false)); |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 241 | ASSERT_FALSE(root); |
| 242 | root = NULL; |
| 243 | ASSERT_FALSE(JSONReader::JsonToValue("\"\\z invalid escape char\"", &root, |
tc@google.com | ce6a78d | 2008-07-29 09:01:31 +0900 | [diff] [blame] | 244 | false, false)); |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 245 | ASSERT_FALSE(root); |
| 246 | root = NULL; |
| 247 | ASSERT_FALSE(JSONReader::JsonToValue("\"\\xAQ invalid hex code\"", &root, |
tc@google.com | ce6a78d | 2008-07-29 09:01:31 +0900 | [diff] [blame] | 248 | false, false)); |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 249 | ASSERT_FALSE(root); |
| 250 | root = NULL; |
| 251 | ASSERT_FALSE(JSONReader::JsonToValue("not enough hex chars\\x1\"", &root, |
tc@google.com | ce6a78d | 2008-07-29 09:01:31 +0900 | [diff] [blame] | 252 | false, false)); |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 253 | ASSERT_FALSE(root); |
| 254 | root = NULL; |
| 255 | ASSERT_FALSE(JSONReader::JsonToValue("\"not enough escape chars\\u123\"", |
tc@google.com | ce6a78d | 2008-07-29 09:01:31 +0900 | [diff] [blame] | 256 | &root, false, false)); |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 257 | ASSERT_FALSE(root); |
| 258 | root = NULL; |
| 259 | ASSERT_FALSE(JSONReader::JsonToValue("\"extra backslash at end of input\\\"", |
tc@google.com | ce6a78d | 2008-07-29 09:01:31 +0900 | [diff] [blame] | 260 | &root, false, false)); |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 261 | ASSERT_FALSE(root); |
| 262 | |
| 263 | // Basic array |
| 264 | root = NULL; |
tc@google.com | ce6a78d | 2008-07-29 09:01:31 +0900 | [diff] [blame] | 265 | ASSERT_TRUE(JSONReader::Read("[true, false, null]", &root, false)); |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 266 | ASSERT_TRUE(root); |
| 267 | ASSERT_TRUE(root->IsType(Value::TYPE_LIST)); |
| 268 | ListValue* list = static_cast<ListValue*>(root); |
darin@google.com | f327280 | 2008-08-15 05:27:29 +0900 | [diff] [blame] | 269 | ASSERT_EQ(3U, list->GetSize()); |
tc@google.com | ce6a78d | 2008-07-29 09:01:31 +0900 | [diff] [blame] | 270 | |
| 271 | // Test with trailing comma. Should be parsed the same as above. |
| 272 | Value* root2 = NULL; |
| 273 | ASSERT_TRUE(JSONReader::Read("[true, false, null, ]", &root2, true)); |
| 274 | EXPECT_TRUE(root->Equals(root2)); |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 275 | delete root; |
tc@google.com | ce6a78d | 2008-07-29 09:01:31 +0900 | [diff] [blame] | 276 | delete root2; |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 277 | |
| 278 | // Empty array |
| 279 | root = NULL; |
tc@google.com | ce6a78d | 2008-07-29 09:01:31 +0900 | [diff] [blame] | 280 | ASSERT_TRUE(JSONReader::Read("[]", &root, false)); |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 281 | ASSERT_TRUE(root); |
| 282 | ASSERT_TRUE(root->IsType(Value::TYPE_LIST)); |
| 283 | list = static_cast<ListValue*>(root); |
darin@google.com | f327280 | 2008-08-15 05:27:29 +0900 | [diff] [blame] | 284 | ASSERT_EQ(0U, list->GetSize()); |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 285 | delete root; |
| 286 | |
| 287 | // Nested arrays |
| 288 | root = NULL; |
tc@google.com | ce6a78d | 2008-07-29 09:01:31 +0900 | [diff] [blame] | 289 | ASSERT_TRUE(JSONReader::Read("[[true], [], [false, [], [null]], null]", &root, |
| 290 | false)); |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 291 | ASSERT_TRUE(root); |
| 292 | ASSERT_TRUE(root->IsType(Value::TYPE_LIST)); |
| 293 | list = static_cast<ListValue*>(root); |
darin@google.com | f327280 | 2008-08-15 05:27:29 +0900 | [diff] [blame] | 294 | ASSERT_EQ(4U, list->GetSize()); |
tc@google.com | ce6a78d | 2008-07-29 09:01:31 +0900 | [diff] [blame] | 295 | |
| 296 | // Lots of trailing commas. |
| 297 | root2 = NULL; |
| 298 | ASSERT_TRUE(JSONReader::Read("[[true], [], [false, [], [null, ] , ], null,]", |
| 299 | &root2, true)); |
| 300 | EXPECT_TRUE(root->Equals(root2)); |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 301 | delete root; |
tc@google.com | ce6a78d | 2008-07-29 09:01:31 +0900 | [diff] [blame] | 302 | delete root2; |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 303 | |
| 304 | // Invalid, missing close brace. |
| 305 | root = NULL; |
tc@google.com | ce6a78d | 2008-07-29 09:01:31 +0900 | [diff] [blame] | 306 | ASSERT_FALSE(JSONReader::Read("[[true], [], [false, [], [null]], null", &root, |
| 307 | false)); |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 308 | ASSERT_FALSE(root); |
| 309 | |
| 310 | // Invalid, too many commas |
| 311 | root = NULL; |
tc@google.com | ce6a78d | 2008-07-29 09:01:31 +0900 | [diff] [blame] | 312 | ASSERT_FALSE(JSONReader::Read("[true,, null]", &root, false)); |
| 313 | ASSERT_FALSE(root); |
| 314 | ASSERT_FALSE(JSONReader::Read("[true,, null]", &root, true)); |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 315 | ASSERT_FALSE(root); |
| 316 | |
| 317 | // Invalid, no commas |
| 318 | root = NULL; |
tc@google.com | ce6a78d | 2008-07-29 09:01:31 +0900 | [diff] [blame] | 319 | ASSERT_FALSE(JSONReader::Read("[true null]", &root, false)); |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 320 | ASSERT_FALSE(root); |
| 321 | |
| 322 | // Invalid, trailing comma |
| 323 | root = NULL; |
tc@google.com | ce6a78d | 2008-07-29 09:01:31 +0900 | [diff] [blame] | 324 | ASSERT_FALSE(JSONReader::Read("[true,]", &root, false)); |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 325 | ASSERT_FALSE(root); |
| 326 | |
tc@google.com | ce6a78d | 2008-07-29 09:01:31 +0900 | [diff] [blame] | 327 | // Valid if we set |allow_trailing_comma| to true. |
| 328 | EXPECT_TRUE(JSONReader::Read("[true,]", &root, true)); |
| 329 | ASSERT_TRUE(root); |
| 330 | ASSERT_TRUE(root->IsType(Value::TYPE_LIST)); |
| 331 | list = static_cast<ListValue*>(root); |
darin@google.com | f327280 | 2008-08-15 05:27:29 +0900 | [diff] [blame] | 332 | EXPECT_EQ(1U, list->GetSize()); |
tc@google.com | ce6a78d | 2008-07-29 09:01:31 +0900 | [diff] [blame] | 333 | Value* tmp_value = NULL; |
| 334 | ASSERT_TRUE(list->Get(0, &tmp_value)); |
| 335 | EXPECT_TRUE(tmp_value->IsType(Value::TYPE_BOOLEAN)); |
| 336 | bool bool_value = false; |
| 337 | ASSERT_TRUE(tmp_value->GetAsBoolean(&bool_value)); |
| 338 | EXPECT_TRUE(bool_value); |
| 339 | delete root; |
| 340 | |
| 341 | // Don't allow empty elements, even if |allow_trailing_comma| is |
| 342 | // true. |
| 343 | root = NULL; |
| 344 | EXPECT_FALSE(JSONReader::Read("[,]", &root, true)); |
| 345 | EXPECT_FALSE(root); |
| 346 | EXPECT_FALSE(JSONReader::Read("[true,,]", &root, true)); |
| 347 | EXPECT_FALSE(root); |
| 348 | EXPECT_FALSE(JSONReader::Read("[,true,]", &root, true)); |
| 349 | EXPECT_FALSE(root); |
| 350 | EXPECT_FALSE(JSONReader::Read("[true,,false]", &root, true)); |
| 351 | EXPECT_FALSE(root); |
| 352 | |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 353 | // Test objects |
| 354 | root = NULL; |
tc@google.com | ce6a78d | 2008-07-29 09:01:31 +0900 | [diff] [blame] | 355 | ASSERT_TRUE(JSONReader::Read("{}", &root, false)); |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 356 | ASSERT_TRUE(root); |
| 357 | ASSERT_TRUE(root->IsType(Value::TYPE_DICTIONARY)); |
| 358 | delete root; |
| 359 | |
| 360 | root = NULL; |
| 361 | ASSERT_TRUE(JSONReader::Read( |
tc@google.com | ce6a78d | 2008-07-29 09:01:31 +0900 | [diff] [blame] | 362 | "{\"number\":9.87654321, \"null\":null , \"\\x53\" : \"str\" }", &root, |
| 363 | false)); |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 364 | ASSERT_TRUE(root); |
| 365 | ASSERT_TRUE(root->IsType(Value::TYPE_DICTIONARY)); |
| 366 | DictionaryValue* dict_val = static_cast<DictionaryValue*>(root); |
| 367 | real_val = 0.0; |
| 368 | ASSERT_TRUE(dict_val->GetReal(L"number", &real_val)); |
| 369 | ASSERT_DOUBLE_EQ(9.87654321, real_val); |
| 370 | Value* null_val = NULL; |
| 371 | ASSERT_TRUE(dict_val->Get(L"null", &null_val)); |
| 372 | ASSERT_TRUE(null_val->IsType(Value::TYPE_NULL)); |
| 373 | str_val.clear(); |
| 374 | ASSERT_TRUE(dict_val->GetString(L"S", &str_val)); |
| 375 | ASSERT_EQ(L"str", str_val); |
tc@google.com | ce6a78d | 2008-07-29 09:01:31 +0900 | [diff] [blame] | 376 | |
| 377 | root2 = NULL; |
| 378 | ASSERT_TRUE(JSONReader::Read( |
| 379 | "{\"number\":9.87654321, \"null\":null , \"\\x53\" : \"str\", }", &root2, |
| 380 | true)); |
| 381 | EXPECT_TRUE(root->Equals(root2)); |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 382 | delete root; |
tc@google.com | ce6a78d | 2008-07-29 09:01:31 +0900 | [diff] [blame] | 383 | delete root2; |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 384 | |
| 385 | // Test nesting |
| 386 | root = NULL; |
| 387 | ASSERT_TRUE(JSONReader::Read( |
tc@google.com | ce6a78d | 2008-07-29 09:01:31 +0900 | [diff] [blame] | 388 | "{\"inner\":{\"array\":[true]},\"false\":false,\"d\":{}}", &root, false)); |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 389 | ASSERT_TRUE(root); |
| 390 | ASSERT_TRUE(root->IsType(Value::TYPE_DICTIONARY)); |
| 391 | dict_val = static_cast<DictionaryValue*>(root); |
| 392 | DictionaryValue* inner_dict = NULL; |
| 393 | ASSERT_TRUE(dict_val->GetDictionary(L"inner", &inner_dict)); |
| 394 | ListValue* inner_array = NULL; |
| 395 | ASSERT_TRUE(inner_dict->GetList(L"array", &inner_array)); |
darin@google.com | f327280 | 2008-08-15 05:27:29 +0900 | [diff] [blame] | 396 | ASSERT_EQ(1U, inner_array->GetSize()); |
tc@google.com | ce6a78d | 2008-07-29 09:01:31 +0900 | [diff] [blame] | 397 | bool_value = true; |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 398 | ASSERT_TRUE(dict_val->GetBoolean(L"false", &bool_value)); |
| 399 | ASSERT_FALSE(bool_value); |
| 400 | inner_dict = NULL; |
| 401 | ASSERT_TRUE(dict_val->GetDictionary(L"d", &inner_dict)); |
tc@google.com | ce6a78d | 2008-07-29 09:01:31 +0900 | [diff] [blame] | 402 | |
| 403 | root2 = NULL; |
| 404 | ASSERT_TRUE(JSONReader::Read( |
| 405 | "{\"inner\": {\"array\":[true] , },\"false\":false,\"d\":{},}", &root2, |
| 406 | true)); |
| 407 | EXPECT_TRUE(root->Equals(root2)); |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 408 | delete root; |
tc@google.com | ce6a78d | 2008-07-29 09:01:31 +0900 | [diff] [blame] | 409 | delete root2; |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 410 | |
| 411 | // Invalid, no closing brace |
| 412 | root = NULL; |
tc@google.com | ce6a78d | 2008-07-29 09:01:31 +0900 | [diff] [blame] | 413 | ASSERT_FALSE(JSONReader::Read("{\"a\": true", &root, false)); |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 414 | ASSERT_FALSE(root); |
| 415 | |
| 416 | // Invalid, keys must be quoted |
| 417 | root = NULL; |
tc@google.com | ce6a78d | 2008-07-29 09:01:31 +0900 | [diff] [blame] | 418 | ASSERT_FALSE(JSONReader::Read("{foo:true}", &root, false)); |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 419 | ASSERT_FALSE(root); |
| 420 | |
| 421 | // Invalid, trailing comma |
| 422 | root = NULL; |
tc@google.com | ce6a78d | 2008-07-29 09:01:31 +0900 | [diff] [blame] | 423 | ASSERT_FALSE(JSONReader::Read("{\"a\":true,}", &root, false)); |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 424 | ASSERT_FALSE(root); |
| 425 | |
| 426 | // Invalid, too many commas |
| 427 | root = NULL; |
tc@google.com | ce6a78d | 2008-07-29 09:01:31 +0900 | [diff] [blame] | 428 | ASSERT_FALSE(JSONReader::Read("{\"a\":true,,\"b\":false}", &root, false)); |
| 429 | ASSERT_FALSE(root); |
| 430 | root = NULL; |
| 431 | ASSERT_FALSE(JSONReader::Read("{\"a\":true,,\"b\":false}", &root, true)); |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 432 | ASSERT_FALSE(root); |
| 433 | |
| 434 | // Invalid, no separator |
| 435 | root = NULL; |
tc@google.com | ce6a78d | 2008-07-29 09:01:31 +0900 | [diff] [blame] | 436 | ASSERT_FALSE(JSONReader::Read("{\"a\" \"b\"}", &root, false)); |
| 437 | ASSERT_FALSE(root); |
| 438 | |
| 439 | // Invalid, lone comma. |
| 440 | root = NULL; |
| 441 | ASSERT_FALSE(JSONReader::Read("{,}", &root, false)); |
| 442 | ASSERT_FALSE(root); |
| 443 | ASSERT_FALSE(JSONReader::Read("{,}", &root, true)); |
| 444 | ASSERT_FALSE(root); |
| 445 | ASSERT_FALSE(JSONReader::Read("{\"a\":true,,}", &root, true)); |
| 446 | ASSERT_FALSE(root); |
| 447 | ASSERT_FALSE(JSONReader::Read("{,\"a\":true}", &root, true)); |
| 448 | ASSERT_FALSE(root); |
| 449 | ASSERT_FALSE(JSONReader::Read("{\"a\":true,,\"b\":false}", &root, true)); |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 450 | ASSERT_FALSE(root); |
| 451 | |
| 452 | // Test stack overflow |
| 453 | root = NULL; |
| 454 | std::string evil(1000000, '['); |
| 455 | evil.append(std::string(1000000, ']')); |
tc@google.com | ce6a78d | 2008-07-29 09:01:31 +0900 | [diff] [blame] | 456 | ASSERT_FALSE(JSONReader::Read(evil, &root, false)); |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 457 | ASSERT_FALSE(root); |
| 458 | |
| 459 | // A few thousand adjacent lists is fine. |
| 460 | std::string not_evil("["); |
| 461 | not_evil.reserve(15010); |
| 462 | for (int i = 0; i < 5000; ++i) { |
| 463 | not_evil.append("[],"); |
| 464 | } |
| 465 | not_evil.append("[]]"); |
tc@google.com | ce6a78d | 2008-07-29 09:01:31 +0900 | [diff] [blame] | 466 | ASSERT_TRUE(JSONReader::Read(not_evil, &root, false)); |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 467 | ASSERT_TRUE(root); |
| 468 | ASSERT_TRUE(root->IsType(Value::TYPE_LIST)); |
| 469 | list = static_cast<ListValue*>(root); |
darin@google.com | f327280 | 2008-08-15 05:27:29 +0900 | [diff] [blame] | 470 | ASSERT_EQ(5001U, list->GetSize()); |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 471 | delete root; |
| 472 | |
| 473 | // Test utf8 encoded input |
| 474 | root = NULL; |
| 475 | ASSERT_TRUE(JSONReader::JsonToValue("\"\xe7\xbd\x91\xe9\xa1\xb5\"", &root, |
tc@google.com | ce6a78d | 2008-07-29 09:01:31 +0900 | [diff] [blame] | 476 | false, false)); |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 477 | ASSERT_TRUE(root); |
| 478 | ASSERT_TRUE(root->IsType(Value::TYPE_STRING)); |
| 479 | str_val.clear(); |
| 480 | ASSERT_TRUE(root->GetAsString(&str_val)); |
| 481 | ASSERT_EQ(L"\x7f51\x9875", str_val); |
| 482 | delete root; |
| 483 | |
| 484 | // Test invalid root objects. |
| 485 | root = NULL; |
tc@google.com | ce6a78d | 2008-07-29 09:01:31 +0900 | [diff] [blame] | 486 | ASSERT_FALSE(JSONReader::Read("null", &root, false)); |
| 487 | ASSERT_FALSE(JSONReader::Read("true", &root, false)); |
| 488 | ASSERT_FALSE(JSONReader::Read("10", &root, false)); |
| 489 | ASSERT_FALSE(JSONReader::Read("\"root\"", &root, false)); |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 490 | } |
license.bot | f003cfe | 2008-08-24 09:55:55 +0900 | [diff] [blame^] | 491 | |