| 1 | /* Copyright (c) 2014, 2026, Oracle and/or its affiliates. |
| 2 | |
| 3 | This program is free software; you can redistribute it and/or modify |
| 4 | it under the terms of the GNU General Public License, version 2.0, |
| 5 | as published by the Free Software Foundation. |
| 6 | |
| 7 | This program is designed to work with certain software (including |
| 8 | but not limited to OpenSSL) that is licensed under separate terms, |
| 9 | as designated in a particular file or component or in included license |
| 10 | documentation. The authors of MySQL hereby grant you an additional |
| 11 | permission to link the program and your derivative works with the |
| 12 | separately licensed software that they have either included with |
| 13 | the program or referenced in the documentation. |
| 14 | |
| 15 | This program is distributed in the hope that it will be useful, |
| 16 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 17 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 18 | GNU General Public License, version 2.0, for more details. |
| 19 | |
| 20 | You should have received a copy of the GNU General Public License |
| 21 | along with this program; if not, write to the Free Software |
| 22 | Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */ |
| 23 | |
| 24 | #include <gmock/gmock.h> |
| 25 | #include <gtest/gtest.h> |
| 26 | #include <sys/types.h> |
| 27 | #include <cstring> |
| 28 | #include <fstream> |
| 29 | #include <iostream> |
| 30 | #include <string> |
| 31 | #include <utility> |
| 32 | |
| 33 | #include "m_ctype.h" |
| 34 | #include "sql-common/json_dom.h" |
| 35 | #include "sql-common/json_path.h" |
| 36 | #include "sql_string.h" |
| 37 | #include "unittest/gunit/test_utils.h" |
| 38 | |
| 39 | /** |
| 40 | Test json path abstraction. |
| 41 | */ |
| 42 | namespace json_path_unittest { |
| 43 | |
| 44 | class JsonPathTest : public ::testing::Test { |
| 45 | protected: |
| 46 | void SetUp() override { initializer.SetUp(); } |
| 47 | void TearDown() override { initializer.TearDown(); } |
| 48 | THD *thd() const { return initializer.thd(); } |
| 49 | my_testing::Server_initializer initializer; |
| 50 | void vet_wrapper_seek(Json_wrapper *wrapper, const Json_path &path, |
| 51 | const std::string &expected, bool expected_null) const; |
| 52 | void vet_wrapper_seek(const char *json_text, const char *path_text, |
| 53 | const std::string &expected, bool expected_null) const; |
| 54 | }; |
| 55 | |
| 56 | /** |
| 57 | Struct that defines input and expected result for negative testing |
| 58 | of path parsing. |
| 59 | */ |
| 60 | struct Bad_path { |
| 61 | const char *m_path_expression; ///< the path to parse |
| 62 | const size_t m_expected_index; ///< the offset of the syntax error |
| 63 | }; |
| 64 | |
| 65 | /** |
| 66 | Class that contains parameterized test cases for bad paths. |
| 67 | */ |
| 68 | class JsonBadPathTestP : public ::testing::TestWithParam<Bad_path> {}; |
| 69 | |
| 70 | /** |
| 71 | Struct that defines input and expected result for positive testing |
| 72 | of path parsing. |
| 73 | */ |
| 74 | struct Good_path { |
| 75 | const char *m_path_expression; ///< the path to parse |
| 76 | const char *m_expected_path; ///< expected canonical path |
| 77 | }; |
| 78 | |
| 79 | /** |
| 80 | Struct that defines input and expected result for testing |
| 81 | Json_dom.get_location(). |
| 82 | */ |
| 83 | struct Location_tuple { |
| 84 | const char *m_json_text; // the document text |
| 85 | const char *m_path_expression; // the path to parse |
| 86 | }; |
| 87 | |
| 88 | /** |
| 89 | Struct that defines input and expected result for testing |
| 90 | the only_needs_one argument of Json_wrapper.seek(). |
| 91 | */ |
| 92 | struct Ono_tuple { |
| 93 | const char *m_json_text; // the document text |
| 94 | const char *m_path_expression; // the path to parse |
| 95 | const uint m_expected_hits; // total number of matches |
| 96 | }; |
| 97 | |
| 98 | /** |
| 99 | Struct that defines input for cloning test cases. |
| 100 | */ |
| 101 | struct Clone_tuple { |
| 102 | const char *m_path_expression_1; // the first path to parse |
| 103 | const char *m_path_expression_2; // the second path to parse |
| 104 | }; |
| 105 | |
| 106 | /** |
| 107 | Class that contains parameterized test cases for good paths. |
| 108 | */ |
| 109 | class JsonGoodPathTestP : public ::testing::TestWithParam<Good_path> {}; |
| 110 | |
| 111 | /** |
| 112 | Class that contains parameterized test cases for dom locations. |
| 113 | */ |
| 114 | class JsonGoodLocationTestP : public ::testing::TestWithParam<Location_tuple> { |
| 115 | }; |
| 116 | |
| 117 | /** |
| 118 | Class that contains parameterized test cases for the only_needs_one |
| 119 | arg of Json_wrapper.seek(). |
| 120 | */ |
| 121 | class JsonGoodOnoTestP : public ::testing::TestWithParam<Ono_tuple> { |
| 122 | void SetUp() override { initializer.SetUp(); } |
| 123 | void TearDown() override { initializer.TearDown(); } |
| 124 | my_testing::Server_initializer initializer; |
| 125 | |
| 126 | protected: |
| 127 | THD *thd() const { return initializer.thd(); } |
| 128 | }; |
| 129 | |
| 130 | /** |
| 131 | Class that contains parameterized test cases for cloning tests. |
| 132 | */ |
| 133 | class JsonGoodCloneTestP : public ::testing::TestWithParam<Clone_tuple> { |
| 134 | void SetUp() override { initializer.SetUp(); } |
| 135 | void TearDown() override { initializer.TearDown(); } |
| 136 | my_testing::Server_initializer initializer; |
| 137 | }; |
| 138 | |
| 139 | /* |
| 140 | Helper functions. |
| 141 | */ |
| 142 | |
| 143 | /* Concatenate the left and right strings and write the result into dest */ |
| 144 | char *concat(char *dest, const char *left, const char *right) { |
| 145 | dest[0] = 0; |
| 146 | std::strcat(dest, left); |
| 147 | std::strcat(dest, right); |
| 148 | |
| 149 | return dest; |
| 150 | } |
| 151 | |
| 152 | /** Code common to good_path() and good_leg_types() */ |
| 153 | void good_path_common(const char *path_expression, Json_path *json_path) { |
| 154 | size_t bad_idx = 0; |
| 155 | EXPECT_FALSE(parse_path(strlen(path_expression), path_expression, json_path, |
| 156 | &bad_idx, [] { ASSERT_TRUE(false); })); |
| 157 | |
| 158 | EXPECT_EQ(0U, bad_idx) << "bad_idx != 0 for " << path_expression; |
| 159 | } |
| 160 | |
| 161 | /** Verify that a good path parses correctly */ |
| 162 | void good_path(bool check_path, const char *path_expression, |
| 163 | std::string expected_path) { |
| 164 | Json_path json_path(key_memory_JSON); |
| 165 | good_path_common(path_expression, &json_path); |
| 166 | if (check_path) { |
| 167 | String str; |
| 168 | EXPECT_FALSE(json_path.to_string(&str)); |
| 169 | EXPECT_EQ(expected_path, std::string(str.ptr(), str.length())); |
| 170 | |
| 171 | // Move-construct a new path and verify that it's the same. |
| 172 | Json_path path2{std::move(json_path)}; |
| 173 | str.length(0); |
| 174 | EXPECT_FALSE(path2.to_string(&str)); |
| 175 | EXPECT_EQ(expected_path, std::string(str.ptr(), str.length())); |
| 176 | |
| 177 | // Move-assign to a new path and verify that it's the same. |
| 178 | Json_path path3(key_memory_JSON); |
| 179 | path3 = std::move(path2); |
| 180 | str.length(0); |
| 181 | EXPECT_FALSE(path3.to_string(&str)); |
| 182 | EXPECT_EQ(expected_path, std::string(str.ptr(), str.length())); |
| 183 | } |
| 184 | } |
| 185 | |
| 186 | void good_path(const char *path_expression, std::string expected_path) { |
| 187 | good_path(true, path_expression, expected_path); |
| 188 | } |
| 189 | |
| 190 | /** Shorter form of good_path() */ |
| 191 | void good_path(const char *path_expression) { |
| 192 | good_path(false, path_expression, ""); |
| 193 | } |
| 194 | |
| 195 | /** Verify whether the path contains a wildcard, ellipsis or range token. */ |
| 196 | void contains_wildcard(const char *path_expression, bool expected_answer) { |
| 197 | Json_path json_path(key_memory_JSON); |
| 198 | good_path_common(path_expression, &json_path); |
| 199 | EXPECT_EQ(expected_answer, json_path.can_match_many()); |
| 200 | } |
| 201 | |
| 202 | /** Verify that the leg at the given offset looks good */ |
| 203 | void good_leg_at(const char *path_expression, int leg_index, |
| 204 | const std::string &expected_leg, |
| 205 | enum_json_path_leg_type expected_leg_type) { |
| 206 | Json_path json_path(key_memory_JSON); |
| 207 | good_path_common(path_expression, &json_path); |
| 208 | |
| 209 | const Json_path_leg *actual_leg = *(json_path.begin() + leg_index); |
| 210 | String str; |
| 211 | EXPECT_FALSE(actual_leg->to_string(&str)); |
| 212 | EXPECT_EQ(expected_leg, std::string(str.ptr(), str.length())); |
| 213 | EXPECT_EQ(expected_leg_type, actual_leg->get_type()); |
| 214 | } |
| 215 | |
| 216 | /* compare two path legs */ |
| 217 | void compare_legs(const Json_path_leg *left, const Json_path_leg *right) { |
| 218 | String left_str; |
| 219 | String right_str; |
| 220 | EXPECT_EQ(0, left->to_string(&left_str)); |
| 221 | EXPECT_EQ(0, right->to_string(&right_str)); |
| 222 | EXPECT_EQ(std::string(left_str.ptr(), left_str.length()), |
| 223 | std::string(right_str.ptr(), right_str.length())); |
| 224 | } |
| 225 | |
| 226 | /** Compare two paths */ |
| 227 | void compare_paths(Json_path &left, Json_path_clone &right) { |
| 228 | EXPECT_EQ(left.leg_count(), right.leg_count()); |
| 229 | |
| 230 | Json_path_iterator right_it = right.begin(); |
| 231 | for (const Json_path_leg *left_leg : left) { |
| 232 | compare_legs(left_leg, *right_it++); |
| 233 | } |
| 234 | } |
| 235 | |
| 236 | /** Verify that clones look alike */ |
| 237 | void verify_clone(const char *path_expression_1, |
| 238 | const char *path_expression_2) { |
| 239 | Json_path real_path1(key_memory_JSON); |
| 240 | good_path_common(path_expression_1, &real_path1); |
| 241 | |
| 242 | Json_path_clone cloned_path(key_memory_JSON); |
| 243 | for (const Json_path_leg *leg : real_path1) cloned_path.append(leg); |
| 244 | compare_paths(real_path1, cloned_path); |
| 245 | |
| 246 | Json_path real_path2(key_memory_JSON); |
| 247 | good_path_common(path_expression_2, &real_path2); |
| 248 | cloned_path.clear(); |
| 249 | for (const Json_path_leg *leg : real_path2) cloned_path.append(leg); |
| 250 | compare_paths(real_path2, cloned_path); |
| 251 | } |
| 252 | |
| 253 | /** |
| 254 | Verify that a good path has the expected sequence of leg types. |
| 255 | */ |
| 256 | void good_leg_types(const char *path_expression, |
| 257 | enum_json_path_leg_type *expected_leg_types, |
| 258 | size_t length) { |
| 259 | Json_path json_path(key_memory_JSON); |
| 260 | good_path_common(path_expression, &json_path); |
| 261 | |
| 262 | EXPECT_EQ(length, json_path.leg_count()); |
| 263 | Json_path_iterator it = json_path.begin(); |
| 264 | for (size_t idx = 0; idx < length; idx++) { |
| 265 | EXPECT_EQ(expected_leg_types[idx], (*it++)->get_type()); |
| 266 | } |
| 267 | } |
| 268 | |
| 269 | /** Verify that a bad path fails as expected */ |
| 270 | void bad_path(const char *path_expression, size_t expected_index) { |
| 271 | size_t actual_index = 0; |
| 272 | Json_path json_path(key_memory_JSON); |
| 273 | EXPECT_TRUE(parse_path(strlen(path_expression), path_expression, &json_path, |
| 274 | &actual_index, [] { ASSERT_TRUE(false); })) |
| 275 | << "Unexpectedly parsed " << path_expression; |
| 276 | EXPECT_EQ(expected_index, actual_index) |
| 277 | << "Unexpected index for " << path_expression; |
| 278 | } |
| 279 | |
| 280 | /** Bad identifiers are ok as member names if they are double-quoted */ |
| 281 | void bad_identifier(const char *identifier, size_t expected_index) { |
| 282 | char dummy1[30]; |
| 283 | char dummy2[30]; |
| 284 | char *path_expression; |
| 285 | |
| 286 | path_expression = concat(dummy1, "$.", identifier); |
| 287 | bad_path(path_expression, expected_index); |
| 288 | |
| 289 | path_expression = concat(dummy1, "$.\"", identifier); |
| 290 | path_expression = concat(dummy2, path_expression, "\""); |
| 291 | good_path(path_expression); |
| 292 | } |
| 293 | |
| 294 | /* |
| 295 | Helper functions for Json_wrapper tests. |
| 296 | */ |
| 297 | |
| 298 | void JsonPathTest::vet_wrapper_seek(Json_wrapper *wrapper, |
| 299 | const Json_path &path, |
| 300 | const std::string &expected, |
| 301 | bool expected_null) const { |
| 302 | Json_wrapper_vector hits(PSI_NOT_INSTRUMENTED); |
| 303 | wrapper->seek(path, path.leg_count(), &hits, true, false); |
| 304 | String result_buffer; |
| 305 | |
| 306 | if (hits.size() == 1) { |
| 307 | EXPECT_FALSE(hits[0].to_string(&result_buffer, true, "test", |
| 308 | [] { ASSERT_TRUE(false); })); |
| 309 | } else { |
| 310 | Json_array *a = new (std::nothrow) Json_array(); |
| 311 | for (uint i = 0; i < hits.size(); ++i) { |
| 312 | a->append_clone(hits[i].to_dom()); |
| 313 | } |
| 314 | Json_wrapper w(a); |
| 315 | EXPECT_FALSE( |
| 316 | w.to_string(&result_buffer, true, "test", [] { ASSERT_TRUE(false); })); |
| 317 | } |
| 318 | |
| 319 | std::string actual = std::string(result_buffer.ptr(), result_buffer.length()); |
| 320 | |
| 321 | if (expected_null) { |
| 322 | const char *source_output = ""; |
| 323 | const char *result_output = ""; |
| 324 | |
| 325 | if (hits.size() > 0) { |
| 326 | String source_buffer; |
| 327 | EXPECT_FALSE(wrapper->to_string(&source_buffer, true, "test", |
| 328 | [] { ASSERT_TRUE(false); })); |
| 329 | source_output = source_buffer.ptr(); |
| 330 | result_output = actual.c_str(); |
| 331 | } |
| 332 | EXPECT_TRUE(hits.size() == 0) |
| 333 | << "Unexpected result wrapper for " << source_output |
| 334 | << ". The output is " << result_output << "\n"; |
| 335 | } else { |
| 336 | EXPECT_EQ(expected, actual); |
| 337 | } |
| 338 | } |
| 339 | |
| 340 | void JsonPathTest::vet_wrapper_seek(const char *json_text, |
| 341 | const char *path_text, |
| 342 | const std::string &expected, |
| 343 | bool expected_null) const { |
| 344 | Json_dom_ptr dom = Json_dom::parse( |
| 345 | json_text, std::strlen(json_text), [](const char *, size_t) {}, |
| 346 | [] { ASSERT_TRUE(false); }); |
| 347 | |
| 348 | String serialized_form; |
| 349 | EXPECT_FALSE(json_binary::serialize(thd(), dom.get(), &serialized_form)); |
| 350 | json_binary::Value binary = json_binary::parse_binary( |
| 351 | serialized_form.ptr(), serialized_form.length()); |
| 352 | |
| 353 | Json_wrapper dom_wrapper(std::move(dom)); |
| 354 | Json_wrapper binary_wrapper(binary); |
| 355 | |
| 356 | Json_path path(key_memory_JSON); |
| 357 | good_path_common(path_text, &path); |
| 358 | vet_wrapper_seek(&dom_wrapper, path, expected, expected_null); |
| 359 | vet_wrapper_seek(&binary_wrapper, path, expected, expected_null); |
| 360 | } |
| 361 | |
| 362 | void vet_dom_location(const char *json_text, const char *path_text) { |
| 363 | Json_dom_ptr dom = Json_dom::parse( |
| 364 | json_text, std::strlen(json_text), [](const char *, size_t) {}, |
| 365 | [] { ASSERT_TRUE(false); }); |
| 366 | Json_path path(key_memory_JSON); |
| 367 | good_path_common(path_text, &path); |
| 368 | Json_dom_vector hits(PSI_NOT_INSTRUMENTED); |
| 369 | |
| 370 | dom->seek(path, path.leg_count(), &hits, true, false); |
| 371 | EXPECT_EQ(1U, hits.size()); |
| 372 | if (hits.size() > 0) { |
| 373 | Json_dom *child = hits[0]; |
| 374 | Json_path location = child->get_location(); |
| 375 | String str; |
| 376 | EXPECT_EQ(0, location.to_string(&str)); |
| 377 | EXPECT_EQ(path_text, std::string(str.ptr(), str.length())); |
| 378 | } |
| 379 | } |
| 380 | |
| 381 | /** |
| 382 | Vet the short-circuiting effects of the only_needs_one argument |
| 383 | of Json_wrapper.seek(). |
| 384 | |
| 385 | @param[in] wrapper A wrapped JSON document. |
| 386 | @param[in] path A path to search for. |
| 387 | @param[in] expected_hits Total number of expected matches. |
| 388 | */ |
| 389 | void vet_only_needs_one(Json_wrapper &wrapper, const Json_path &path, |
| 390 | uint expected_hits) { |
| 391 | Json_wrapper_vector all_hits(PSI_NOT_INSTRUMENTED); |
| 392 | wrapper.seek(path, path.leg_count(), &all_hits, true, false); |
| 393 | |
| 394 | EXPECT_EQ(expected_hits, all_hits.size()); |
| 395 | |
| 396 | Json_wrapper_vector only_needs_one_hits(PSI_NOT_INSTRUMENTED); |
| 397 | wrapper.seek(path, path.leg_count(), &only_needs_one_hits, true, true); |
| 398 | uint expected_onoh_hits = (expected_hits == 0) ? 0 : 1; |
| 399 | EXPECT_EQ(expected_onoh_hits, only_needs_one_hits.size()); |
| 400 | } |
| 401 | |
| 402 | /** |
| 403 | Vet the short-circuiting effects of the only_needs_one argument |
| 404 | of Json_wrapper.seek(). |
| 405 | |
| 406 | @param[in] json_text Text of the json document to search. |
| 407 | @param[in] path_text Text of the path expression to use. |
| 408 | @param[in] expected_hits Total number of expected matches. |
| 409 | @param[in] thd THD handle |
| 410 | */ |
| 411 | void vet_only_needs_one(const char *json_text, const char *path_text, |
| 412 | uint expected_hits, const THD *thd) { |
| 413 | Json_dom_ptr dom = Json_dom::parse( |
| 414 | json_text, std::strlen(json_text), [](const char *, size_t) {}, |
| 415 | [] { ASSERT_TRUE(false); }); |
| 416 | |
| 417 | String serialized_form; |
| 418 | EXPECT_FALSE(json_binary::serialize(thd, dom.get(), &serialized_form)); |
| 419 | json_binary::Value binary = json_binary::parse_binary( |
| 420 | serialized_form.ptr(), serialized_form.length()); |
| 421 | |
| 422 | Json_wrapper dom_wrapper(std::move(dom)); |
| 423 | Json_wrapper binary_wrapper(binary); |
| 424 | |
| 425 | Json_path path(key_memory_JSON); |
| 426 | good_path_common(path_text, &path); |
| 427 | vet_only_needs_one(dom_wrapper, path, expected_hits); |
| 428 | vet_only_needs_one(binary_wrapper, path, expected_hits); |
| 429 | } |
| 430 | |
| 431 | /* |
| 432 | |
| 433 | Helper functions for testing Json_object.remove() |
| 434 | and Json_array.remove(). |
| 435 | */ |
| 436 | |
| 437 | /** |
| 438 | Format a Json_dom object to JSON text using Json_wrapper's |
| 439 | to_string functionality. |
| 440 | |
| 441 | @param dom The DOM object to be formatted |
| 442 | */ |
| 443 | std::string format(Json_dom *dom) { |
| 444 | String buffer; |
| 445 | Json_wrapper wrapper(dom->clone()); |
| 446 | EXPECT_FALSE( |
| 447 | wrapper.to_string(&buffer, true, "format", [] { ASSERT_TRUE(false); })); |
| 448 | |
| 449 | return std::string(buffer.ptr(), buffer.length()); |
| 450 | } |
| 451 | |
| 452 | /* |
| 453 | Tests |
| 454 | */ |
| 455 | |
| 456 | // Good paths with no column scope. |
| 457 | static const Good_path good_paths_no_column_scope[] = { |
| 458 | {"$", "$"}, |
| 459 | {" $", "$"}, |
| 460 | {"$ ", "$"}, |
| 461 | {" $ ", "$"}, |
| 462 | |
| 463 | {"$[5]", "$[5]"}, |
| 464 | {"$[ 5 ]", "$[5]"}, |
| 465 | {" $[ 5 ] ", "$[5]"}, |
| 466 | {" $ [ 5 ] ", "$[5]"}, |
| 467 | |
| 468 | {"$[456]", "$[456]"}, |
| 469 | {"$[ 456 ]", "$[456]"}, |
| 470 | {" $[ 456 ] ", "$[456]"}, |
| 471 | {" $ [ 456 ] ", "$[456]"}, |
| 472 | |
| 473 | {"$[last]", "$[last]"}, |
| 474 | {"$[ last]", "$[last]"}, |
| 475 | {"$[last ]", "$[last]"}, |
| 476 | {"$[last-1]", "$[last-1]"}, |
| 477 | {"$[last -1]", "$[last-1]"}, |
| 478 | {"$[last- 1]", "$[last-1]"}, |
| 479 | |
| 480 | {"$[4294967295]", "$[4294967295]"}, |
| 481 | {"$[last-4294967295]", "$[last-4294967295]"}, |
| 482 | |
| 483 | {"$.a", "$.a"}, |
| 484 | {"$ .a", "$.a"}, |
| 485 | {"$. a", "$.a"}, |
| 486 | {" $ . a ", "$.a"}, |
| 487 | |
| 488 | {" $. abc", "$.abc"}, |
| 489 | {" $ . abc", "$.abc"}, |
| 490 | {" $ . abc ", "$.abc"}, |
| 491 | {" $ . abc ", "$.abc"}, |
| 492 | |
| 493 | {"$.a[7]", "$.a[7]"}, |
| 494 | {" $ . a [ 7 ] ", "$.a[7]"}, |
| 495 | |
| 496 | {"$[7].a", "$[7].a"}, |
| 497 | {" $ [ 7 ] . a ", "$[7].a"}, |
| 498 | |
| 499 | {"$.*", "$.*"}, |
| 500 | {" $ . * ", "$.*"}, |
| 501 | |
| 502 | {"$.*.b", "$.*.b"}, |
| 503 | {" $ . * . b ", "$.*.b"}, |
| 504 | |
| 505 | {"$.*[4]", "$.*[4]"}, |
| 506 | {" $ . * [ 4 ] ", "$.*[4]"}, |
| 507 | |
| 508 | {"$[*]", "$[*]"}, |
| 509 | {" $ [ * ] ", "$[*]"}, |
| 510 | |
| 511 | {"$[*].a", "$[*].a"}, |
| 512 | {" $ [ * ] . a ", "$[*].a"}, |
| 513 | |
| 514 | {"$[*][31]", "$[*][31]"}, |
| 515 | {" $ [ * ] [ 31 ] ", "$[*][31]"}, |
| 516 | |
| 517 | {"$**.abc", "$**.abc"}, |
| 518 | {" $ ** . abc ", "$**.abc"}, |
| 519 | |
| 520 | {"$**[0]", "$**[0]"}, |
| 521 | {" $ ** [ 0 ] ", "$**[0]"}, |
| 522 | |
| 523 | {"$**.a", "$**.a"}, |
| 524 | {" $ ** . a ", "$**.a"}, |
| 525 | |
| 526 | // backslash in front of a quote |
| 527 | {"$.\"\\\\\"", "$.\"\\\\\""}, |
| 528 | |
| 529 | // 0-length member names must be quoted |
| 530 | {"$.\"\"", "$.\"\""}, |
| 531 | {"$.\"\".\"\"", "$.\"\".\"\""}, |
| 532 | {"$.\"\".a.\"\"", "$.\"\".a.\"\""}, |
| 533 | {"$.abc.\"\"", "$.abc.\"\""}, |
| 534 | {"$.abc.\"\".def", "$.abc.\"\".def"}, |
| 535 | {"$.\"abc\".\"\".def", "$.abc.\"\".def"}, |
| 536 | |
| 537 | {"$[0 to 0]", "$[0 to 0]"}, |
| 538 | {"$[1 to 1]", "$[1 to 1]"}, |
| 539 | {"$[1 to 3]", "$[1 to 3]"}, |
| 540 | {"$[ 1 to 3 ]", "$[1 to 3]"}, |
| 541 | {"$[0 to 4294967295]", "$[0 to 4294967295]"}, |
| 542 | {"$[last to last]", "$[last to last]"}, |
| 543 | {"$[last-0 to last - 0]", "$[last to last]"}, |
| 544 | {"$[last-1 to last-1]", "$[last-1 to last-1]"}, |
| 545 | {"$[last to 1]", "$[last to 1]"}, |
| 546 | {"$[1 to last]", "$[1 to last]"}, |
| 547 | }; |
| 548 | |
| 549 | /** Test good paths without column scope */ |
| 550 | TEST_P(JsonGoodPathTestP, GoodPaths) { |
| 551 | Good_path param = GetParam(); |
| 552 | good_path(param.m_path_expression, param.m_expected_path); |
| 553 | } |
| 554 | |
| 555 | INSTANTIATE_TEST_SUITE_P(PositiveNoColumnScope, JsonGoodPathTestP, |
| 556 | ::testing::ValuesIn(good_paths_no_column_scope)); |
| 557 | |
| 558 | /** Test that path leg types look correct. */ |
| 559 | TEST_F(JsonPathTest, LegTypes) { |
| 560 | { |
| 561 | SCOPED_TRACE(""); |
| 562 | enum_json_path_leg_type leg_types1[] = {jpl_member}; |
| 563 | good_leg_types("$.a", leg_types1, 1); |
| 564 | } |
| 565 | |
| 566 | { |
| 567 | SCOPED_TRACE(""); |
| 568 | enum_json_path_leg_type leg_types2[] = {jpl_array_cell}; |
| 569 | good_leg_types("$[3456]", leg_types2, 1); |
| 570 | } |
| 571 | |
| 572 | { |
| 573 | SCOPED_TRACE(""); |
| 574 | enum_json_path_leg_type leg_types3[] = {jpl_member_wildcard}; |
| 575 | good_leg_types("$.*", leg_types3, 1); |
| 576 | } |
| 577 | |
| 578 | { |
| 579 | SCOPED_TRACE(""); |
| 580 | enum_json_path_leg_type leg_types4[] = {jpl_array_cell_wildcard}; |
| 581 | good_leg_types("$[*]", leg_types4, 1); |
| 582 | } |
| 583 | |
| 584 | { |
| 585 | SCOPED_TRACE(""); |
| 586 | enum_json_path_leg_type leg_types5[] = {jpl_member, jpl_member}; |
| 587 | good_leg_types("$.foo.bar", leg_types5, 2); |
| 588 | } |
| 589 | |
| 590 | { |
| 591 | SCOPED_TRACE(""); |
| 592 | enum_json_path_leg_type leg_types6[] = {jpl_member, jpl_array_cell}; |
| 593 | good_leg_types("$.foo[987654321]", leg_types6, 2); |
| 594 | } |
| 595 | |
| 596 | { |
| 597 | SCOPED_TRACE(""); |
| 598 | enum_json_path_leg_type leg_types7[] = {jpl_member, jpl_member_wildcard}; |
| 599 | good_leg_types("$.foo.*", leg_types7, 2); |
| 600 | } |
| 601 | |
| 602 | { |
| 603 | SCOPED_TRACE(""); |
| 604 | enum_json_path_leg_type leg_types8[] = {jpl_member, |
| 605 | jpl_array_cell_wildcard}; |
| 606 | good_leg_types("$.foo[*]", leg_types8, 2); |
| 607 | } |
| 608 | |
| 609 | { |
| 610 | SCOPED_TRACE(""); |
| 611 | enum_json_path_leg_type leg_types9[] = {jpl_ellipsis, jpl_member}; |
| 612 | good_leg_types("$**.foo", leg_types9, 2); |
| 613 | } |
| 614 | |
| 615 | { |
| 616 | SCOPED_TRACE(""); |
| 617 | good_leg_types(" $ ", nullptr, 0); |
| 618 | } |
| 619 | } |
| 620 | |
| 621 | /** Test accessors. */ |
| 622 | TEST_F(JsonPathTest, Accessors) { |
| 623 | { |
| 624 | SCOPED_TRACE(""); |
| 625 | good_leg_at("$[*][31]", 0, "[*]", jpl_array_cell_wildcard); |
| 626 | } |
| 627 | { |
| 628 | SCOPED_TRACE(""); |
| 629 | good_leg_at("$.abc[ 3 ].def", 2, ".def", jpl_member); |
| 630 | } |
| 631 | { |
| 632 | SCOPED_TRACE(""); |
| 633 | good_leg_at("$.abc**.def", 1, "**", jpl_ellipsis); |
| 634 | } |
| 635 | } |
| 636 | |
| 637 | /** Test detection of wildcard/ellipsis tokens. */ |
| 638 | TEST_F(JsonPathTest, WildcardDetection) { |
| 639 | { |
| 640 | SCOPED_TRACE(""); |
| 641 | contains_wildcard("$", false); |
| 642 | } |
| 643 | { |
| 644 | SCOPED_TRACE(""); |
| 645 | contains_wildcard("$.foo", false); |
| 646 | } |
| 647 | { |
| 648 | SCOPED_TRACE(""); |
| 649 | contains_wildcard("$[3]", false); |
| 650 | } |
| 651 | { |
| 652 | SCOPED_TRACE(""); |
| 653 | contains_wildcard("$.foo.bar", false); |
| 654 | } |
| 655 | { |
| 656 | SCOPED_TRACE(""); |
| 657 | contains_wildcard("$[3].foo", false); |
| 658 | } |
| 659 | { |
| 660 | SCOPED_TRACE(""); |
| 661 | contains_wildcard("$[3][5]", false); |
| 662 | } |
| 663 | { |
| 664 | SCOPED_TRACE(""); |
| 665 | contains_wildcard("$.*", true); |
| 666 | } |
| 667 | { |
| 668 | SCOPED_TRACE(""); |
| 669 | contains_wildcard("$[*]", true); |
| 670 | } |
| 671 | { |
| 672 | SCOPED_TRACE(""); |
| 673 | contains_wildcard("$.*.bar", true); |
| 674 | } |
| 675 | { |
| 676 | SCOPED_TRACE(""); |
| 677 | contains_wildcard("$**.bar", true); |
| 678 | } |
| 679 | { |
| 680 | SCOPED_TRACE(""); |
| 681 | contains_wildcard("$[*].foo", true); |
| 682 | } |
| 683 | { |
| 684 | SCOPED_TRACE(""); |
| 685 | contains_wildcard("$**.foo", true); |
| 686 | } |
| 687 | { |
| 688 | SCOPED_TRACE(""); |
| 689 | contains_wildcard("$[3].*", true); |
| 690 | } |
| 691 | { |
| 692 | SCOPED_TRACE(""); |
| 693 | contains_wildcard("$[*][5]", true); |
| 694 | } |
| 695 | { |
| 696 | SCOPED_TRACE(""); |
| 697 | contains_wildcard("$**[5]", true); |
| 698 | } |
| 699 | { |
| 700 | SCOPED_TRACE(""); |
| 701 | contains_wildcard("$[1 to 2]", true); |
| 702 | } |
| 703 | { |
| 704 | SCOPED_TRACE(""); |
| 705 | contains_wildcard("$.a[1 to 2].b", true); |
| 706 | } |
| 707 | } |
| 708 | |
| 709 | TEST_P(JsonBadPathTestP, BadPaths) { |
| 710 | Bad_path param = GetParam(); |
| 711 | bad_path(param.m_path_expression, param.m_expected_index); |
| 712 | } |
| 713 | |
| 714 | // Bad paths with no column scope. |
| 715 | static const Bad_path bad_paths_no_column_scope[] = { |
| 716 | // no leading $ |
| 717 | {"foo", 1}, |
| 718 | {"[5]", 1}, |
| 719 | |
| 720 | // no period before key name |
| 721 | {"$foo", 1}, |
| 722 | {"$[5]foo", 4}, |
| 723 | |
| 724 | // array index not a number or a valid range |
| 725 | {"$[a]", 2}, |
| 726 | {"$[5].foo[b]", 9}, |
| 727 | {"$[]", 2}, |
| 728 | {"$[1.2]", 4}, |
| 729 | {"$[1,2]", 4}, |
| 730 | {"$[1,]", 4}, |
| 731 | {"$[1 TO 3]", 5}, |
| 732 | {"$[1 tO 3]", 5}, |
| 733 | {"$[1 To 3]", 5}, |
| 734 | {"$[1 to]", 5}, |
| 735 | {"$[1to]", 4}, |
| 736 | {"$[1to 2]", 4}, |
| 737 | {"$[1 to2]", 5}, |
| 738 | {"$[1 ti 2]", 5}, |
| 739 | {"$[1 t", 5}, |
| 740 | {"$[1 to ", 5}, |
| 741 | {"$[1 to 2,]", 9}, |
| 742 | {"$[4 to 3]", 8}, |
| 743 | {"$[0 tolast]", 5}, |
| 744 | {"$[lastto 2]", 7}, |
| 745 | {"$[lastto to 2]", 7}, |
| 746 | {"$[last+0]", 7}, |
| 747 | {"$[last+1]", 7}, |
| 748 | {"$[LAST]", 2}, |
| 749 | |
| 750 | // absurdly large array index, largest supported array index is 2^32-1 |
| 751 | {"$[9999999999999999999999999999999999999999" |
| 752 | "999999999999999999999999999]", |
| 753 | 2}, |
| 754 | {"$[4294967296]", 2}, |
| 755 | {"$[18446744073709551616]", 2}, |
| 756 | {"$[9223372036854775808]", 2}, |
| 757 | {"$[4294967296 to 2]", 2}, |
| 758 | {"$[0 to 4294967296]", 7}, |
| 759 | {"$[0 to 4294967297]", 7}, |
| 760 | {"$[last-4294967296]", 7}, |
| 761 | |
| 762 | // period not followed by member name |
| 763 | {"$.", 2}, |
| 764 | {"$.foo.", 6}, |
| 765 | {"$[3].", 5}, |
| 766 | {"$.[3]", 2}, |
| 767 | {"$.foo[4].", 9}, |
| 768 | |
| 769 | // array index not terminated by ] |
| 770 | {"$[4", 3}, |
| 771 | {"$[4a]", 4}, |
| 772 | {"$[4abc]", 4}, |
| 773 | |
| 774 | // ends in ellipsis |
| 775 | {"$**", 3}, |
| 776 | {"$.foo**", 7}, |
| 777 | |
| 778 | // paths shouldn't have column scopes if the caller says |
| 779 | // they don't |
| 780 | {"a.b.c$", 1}, |
| 781 | {"b.c$", 1}, |
| 782 | {"c$", 1}, |
| 783 | {"a.b.c$.e", 1}, |
| 784 | {"b.c$.e", 1}, |
| 785 | {"c$.e", 1}, |
| 786 | |
| 787 | // unterminated double-quoted name |
| 788 | {"$.\"bar", 6}, |
| 789 | |
| 790 | // 0-length member names must be quoted |
| 791 | {"$..ab", 2}, |
| 792 | {"$.", 2}, |
| 793 | {"$. ", 3}, |
| 794 | {"$.abc.", 6}, |
| 795 | {"$.abc..def", 6}, |
| 796 | {"$.\"abc\"..def", 8}, |
| 797 | |
| 798 | // backslash in front of a quote, and no end quote |
| 799 | {"$.\"\\\"", 5}, |
| 800 | |
| 801 | // reject plus in front of array index |
| 802 | {"$[+1]", 2}, |
| 803 | |
| 804 | // negative array indexes are rejected |
| 805 | {"$[-0]", 2}, |
| 806 | {"$[-1]", 2}, |
| 807 | {"$[0 to -1]", 7}, |
| 808 | {"$[-1 to 0]", 2}, |
| 809 | {"$[- 1]", 2}, |
| 810 | {"$[-]", 2}, |
| 811 | }; |
| 812 | |
| 813 | INSTANTIATE_TEST_SUITE_P(NegativeNoColumnScope, JsonBadPathTestP, |
| 814 | ::testing::ValuesIn(bad_paths_no_column_scope)); |
| 815 | |
| 816 | /** Good paths with column scope not supported yet */ |
| 817 | TEST_F(JsonPathTest, PositiveColumnScope) { |
| 818 | // |
| 819 | // Test good path syntax |
| 820 | // |
| 821 | bad_path("a.b.c$", 1); |
| 822 | } |
| 823 | |
| 824 | /** Test good quoted key names */ |
| 825 | static const Good_path good_quoted_key_names[] = { |
| 826 | {"$.\"a\"", "$.a"}, |
| 827 | {"$ .\"a\"", "$.a"}, |
| 828 | {"$. \"a\"", "$.a"}, |
| 829 | {" $ . \"a\" ", "$.a"}, |
| 830 | |
| 831 | {" $. \"abc\"", "$.abc"}, |
| 832 | {" $ . \"abc\"", "$.abc"}, |
| 833 | {" $ . \"abc\" ", "$.abc"}, |
| 834 | {" $ . \"abc\" ", "$.abc"}, |
| 835 | |
| 836 | {"$.\"a\"[7]", "$.a[7]"}, |
| 837 | {" $ . \"a\" [ 7 ] ", "$.a[7]"}, |
| 838 | |
| 839 | {"$[7].\"a\"", "$[7].a"}, |
| 840 | {" $ [ 7 ] . \"a\" ", "$[7].a"}, |
| 841 | |
| 842 | {"$.*.\"b\"", "$.*.b"}, |
| 843 | {" $ . * . \"b\" ", "$.*.b"}, |
| 844 | |
| 845 | {"$[*].\"a\"", "$[*].a"}, |
| 846 | {" $ [ * ] . \"a\" ", "$[*].a"}, |
| 847 | |
| 848 | {"$**.\"abc\"", "$**.abc"}, |
| 849 | {" $ ** . \"abc\" ", "$**.abc"}, |
| 850 | |
| 851 | {"$**.\"a\"", "$**.a"}, |
| 852 | {" $ ** . \"a\" ", "$**.a"}, |
| 853 | |
| 854 | // embedded spaces |
| 855 | {"$.\" c d \"", "$.\" c d \""}, |
| 856 | {"$.\" c d \".\"a b\"", "$.\" c d \".\"a b\""}, |
| 857 | {"$.\"a b\".\" c d \"", "$.\"a b\".\" c d \""}, |
| 858 | }; |
| 859 | |
| 860 | INSTANTIATE_TEST_SUITE_P(QuotedKeyNamesPositive, JsonGoodPathTestP, |
| 861 | ::testing::ValuesIn(good_quoted_key_names)); |
| 862 | |
| 863 | /** Test bad quoted key names */ |
| 864 | static const Bad_path bad_quoted_key_names[] = { |
| 865 | // no closing quote |
| 866 | {"$.a.\"bcd", 8}, |
| 867 | {"$.a.\"", 5}, |
| 868 | {"$.\"a\".\"bcd", 10}, |
| 869 | |
| 870 | // not followed by a member or array cell |
| 871 | {"$.abc.\"def\"ghi", 11}, |
| 872 | {"$.abc.\"def\"5", 11}, |
| 873 | |
| 874 | // unrecognized escape character |
| 875 | {"$.abc.\"def\\aghi\"", 16}, |
| 876 | |
| 877 | // unrecognized unicode escape |
| 878 | {"$.abcd.\"ef\\u01kfmno\"", 20}, |
| 879 | |
| 880 | // not preceded by a period |
| 881 | {"$\"abcd\"", 1}, |
| 882 | //{ false, "$.ghi\"abcd\"", 5 }, |
| 883 | }; |
| 884 | |
| 885 | INSTANTIATE_TEST_SUITE_P(QuotedKeyNamesNegative, JsonBadPathTestP, |
| 886 | ::testing::ValuesIn(bad_quoted_key_names)); |
| 887 | |
| 888 | /* Test that unquoted key names may not be ECMAScript identifiers */ |
| 889 | |
| 890 | static const Good_path good_ecmascript_identifiers[] = { |
| 891 | // keywords, however, are allowed |
| 892 | {"$.if.break.return", "$.if.break.return"}, |
| 893 | |
| 894 | // member name can start with $ and _ |
| 895 | {"$.$abc", "$.$abc"}, |
| 896 | {"$.$abc", "$.$abc"}, |
| 897 | |
| 898 | // internal digits are ok |
| 899 | {"$.a1_$bc", "$.a1_$bc"}, |
| 900 | |
| 901 | // and so are internal <ZWNJ> and <ZWJ> characters |
| 902 | {"$.a\\u200Cbc", |
| 903 | "$.a\xE2\x80\x8C" |
| 904 | "bc"}, |
| 905 | {"$.a\\u200Dbc", |
| 906 | "$.a\xE2\x80\x8D" |
| 907 | "bc"}, |
| 908 | |
| 909 | // and so are internal unicode combining marks |
| 910 | {"$.a\\u0300bc", |
| 911 | "$.a\xCC\x80" |
| 912 | "bc"}, |
| 913 | {"$.a\\u030Fbc", |
| 914 | "$.a\xCC\x8F" |
| 915 | "bc"}, |
| 916 | {"$.a\\u036Fbc", |
| 917 | "$.a\xCD\xAF" |
| 918 | "bc"}, |
| 919 | |
| 920 | // and so are internal unicode connector punctuation codepoints |
| 921 | {"$.a\\uFE33bc", |
| 922 | "$.a\xEF\xB8\xB3" |
| 923 | "bc"}, |
| 924 | }; |
| 925 | |
| 926 | INSTANTIATE_TEST_SUITE_P(GoodECMAScriptIdentifiers, JsonGoodPathTestP, |
| 927 | ::testing::ValuesIn(good_ecmascript_identifiers)); |
| 928 | |
| 929 | TEST_F(JsonPathTest, BadECMAScriptIdentifiers) { |
| 930 | // key names may not contain embedded quotes |
| 931 | { |
| 932 | SCOPED_TRACE(""); |
| 933 | bad_path("$.a\"bc", 6); |
| 934 | } |
| 935 | |
| 936 | // key names may not start with a digit or punctuation |
| 937 | { |
| 938 | SCOPED_TRACE(""); |
| 939 | bad_identifier("1abc", 6); |
| 940 | } |
| 941 | { |
| 942 | SCOPED_TRACE(""); |
| 943 | bad_identifier(";abc", 6); |
| 944 | } |
| 945 | |
| 946 | // and not with the <ZWNJ> and <ZWJ> characters |
| 947 | { |
| 948 | SCOPED_TRACE(""); |
| 949 | bad_identifier("\\u200Cabc", 11); |
| 950 | } |
| 951 | |
| 952 | // and not with a unicode combining mark |
| 953 | { |
| 954 | SCOPED_TRACE(""); |
| 955 | bad_identifier("\\u0300abc", 11); |
| 956 | } |
| 957 | { |
| 958 | SCOPED_TRACE(""); |
| 959 | bad_identifier("\\u030Fabc", 11); |
| 960 | } |
| 961 | { |
| 962 | SCOPED_TRACE(""); |
| 963 | bad_identifier("\\u036Fabc", 11); |
| 964 | } |
| 965 | |
| 966 | // and not with unicode connector punctuation |
| 967 | { |
| 968 | SCOPED_TRACE(""); |
| 969 | bad_identifier("\\uFE33abc", 11); |
| 970 | } |
| 971 | } |
| 972 | |
| 973 | TEST_F(JsonPathTest, WrapperSeekTest) { |
| 974 | // vacuous path |
| 975 | { |
| 976 | SCOPED_TRACE(""); |
| 977 | vet_wrapper_seek("false", "$", "false", false); |
| 978 | } |
| 979 | { |
| 980 | SCOPED_TRACE(""); |
| 981 | vet_wrapper_seek("[ false, true, 1 ]", "$", "[false, true, 1]", false); |
| 982 | } |
| 983 | |
| 984 | // no match |
| 985 | { |
| 986 | SCOPED_TRACE(""); |
| 987 | vet_wrapper_seek("false", "$.a", "", true); |
| 988 | } |
| 989 | { |
| 990 | SCOPED_TRACE(""); |
| 991 | vet_wrapper_seek("[ false, true, 1 ]", "$[3]", "", true); |
| 992 | } |
| 993 | |
| 994 | // first level retrieval |
| 995 | { |
| 996 | SCOPED_TRACE(""); |
| 997 | vet_wrapper_seek("[ false, true, 1 ]", "$[2]", "1", false); |
| 998 | } |
| 999 | { |
| 1000 | SCOPED_TRACE(""); |
| 1001 | vet_wrapper_seek( |
| 1002 | "{ \"a\" : 1, \"b\" : { \"c\" : [ 1, 2, 3 ] }, " |
| 1003 | "\"d\" : 4 }", |
| 1004 | "$.b", "{\"c\": [1, 2, 3]}", false); |
| 1005 | } |
| 1006 | |
| 1007 | // second level retrieval |
| 1008 | { |
| 1009 | SCOPED_TRACE(""); |
| 1010 | vet_wrapper_seek("[ false, true, [ 1, null, 200, 300 ], 400 ]", "$[2][3]", |
| 1011 | "300", false); |
| 1012 | } |
| 1013 | { |
| 1014 | SCOPED_TRACE(""); |
| 1015 | vet_wrapper_seek( |
| 1016 | "{ \"a\" : 1, \"b\" : { \"c\" : [ 1, 2, 3 ] }, " |
| 1017 | "\"d\" : 4 }", |
| 1018 | "$.b.c", "[1, 2, 3]", false); |
| 1019 | } |
| 1020 | { |
| 1021 | SCOPED_TRACE(""); |
| 1022 | vet_wrapper_seek( |
| 1023 | "[ false, {\"abc\": 500}, " |
| 1024 | "[ 1, null, 200, 300 ], 400 ]", |
| 1025 | "$[1].abc", "500", false); |
| 1026 | } |
| 1027 | { |
| 1028 | SCOPED_TRACE(""); |
| 1029 | vet_wrapper_seek( |
| 1030 | "{ \"a\" : 1, \"b\" : [ 100, 200, 300 ], " |
| 1031 | "\"d\" : 4 }", |
| 1032 | "$.b[2]", "300", false); |
| 1033 | } |
| 1034 | |
| 1035 | // wildcards |
| 1036 | { |
| 1037 | SCOPED_TRACE(""); |
| 1038 | vet_wrapper_seek( |
| 1039 | "{ \"a\" : 1, \"b\" : [ 100, 200, 300 ], " |
| 1040 | "\"d\" : 4 }", |
| 1041 | "$.*", "[1, [100, 200, 300], 4]", false); |
| 1042 | } |
| 1043 | { |
| 1044 | SCOPED_TRACE(""); |
| 1045 | vet_wrapper_seek( |
| 1046 | "[ false, {\"a\": true}, {\"b\": 200}, " |
| 1047 | "{\"a\": 300} ]", |
| 1048 | "$[*].a", "[true, 300]", false); |
| 1049 | } |
| 1050 | { |
| 1051 | SCOPED_TRACE(""); |
| 1052 | vet_wrapper_seek( |
| 1053 | "{ \"b\": {\"c\": 100}, \"d\": {\"a\": 200}, " |
| 1054 | "\"e\": {\"a\": 300}}", |
| 1055 | "$.*.a", "[200, 300]", false); |
| 1056 | } |
| 1057 | |
| 1058 | // |
| 1059 | // ellipsis |
| 1060 | // |
| 1061 | { |
| 1062 | SCOPED_TRACE(""); |
| 1063 | vet_wrapper_seek( |
| 1064 | "{ \"b\": {\"c\": 100}, \"d\": {\"a\": 200}, " |
| 1065 | "\"e\": {\"a\": 300}, \"f\": {\"g\": {\"a\": 500} } }", |
| 1066 | "$**.a", "[200, 300, 500]", false); |
| 1067 | } |
| 1068 | |
| 1069 | // ellipsis with array recursing into object |
| 1070 | { |
| 1071 | SCOPED_TRACE(""); |
| 1072 | vet_wrapper_seek( |
| 1073 | "{ \"a\": 100, " |
| 1074 | "\"d\": [ {\"a\": 200}, " |
| 1075 | "{ \"e\": {\"a\": 300, \"f\": 500} }, " |
| 1076 | " { \"g\" : true, \"a\": 600 } ] }", |
| 1077 | "$.d**.a", "[200, 300, 600]", false); |
| 1078 | } |
| 1079 | |
| 1080 | // ellipsis with object recursing into arrays |
| 1081 | { |
| 1082 | SCOPED_TRACE(""); |
| 1083 | vet_wrapper_seek( |
| 1084 | "{ \"a\": true, " |
| 1085 | " \"b\": { " |
| 1086 | " \"a\": 100," |
| 1087 | " \"c\": [ " |
| 1088 | "200, { \"a\": 300 }, " |
| 1089 | "{ \"d\": { \"e\": { \"a\": 400 } }, \"f\": true }, " |
| 1090 | "500, [ { \"a\": 600 } ]" |
| 1091 | "]" |
| 1092 | "}, " |
| 1093 | " \"g\": { \"a\": 700 } }", |
| 1094 | "$.b**.a", "[100, 300, 400, 600]", false); |
| 1095 | } |
| 1096 | |
| 1097 | // daisy-chained ellipses |
| 1098 | { |
| 1099 | SCOPED_TRACE(""); |
| 1100 | vet_wrapper_seek( |
| 1101 | "{ \"a\": { \"x\" : { \"b\": { \"y\": { \"b\": " |
| 1102 | "{ \"z\": { \"c\": 100 } } } } } } }", |
| 1103 | "$.a**.b**.c", "100", false); |
| 1104 | } |
| 1105 | { |
| 1106 | SCOPED_TRACE(""); |
| 1107 | vet_wrapper_seek( |
| 1108 | "{ " |
| 1109 | " \"c\": true" |
| 1110 | ", \"a\": { " |
| 1111 | " \"d\": [ " |
| 1112 | " { " |
| 1113 | " \"b\" : { " |
| 1114 | " \"e\": [" |
| 1115 | "{ \"c\": 100 " |
| 1116 | ", \"f\": { \"a\": 200, \"b\": { \"g\" : { \"h\": " |
| 1117 | "{ \"c\": 300 } } } }" |
| 1118 | " }" |
| 1119 | " ]" |
| 1120 | " }" |
| 1121 | " }" |
| 1122 | " ]" |
| 1123 | " }" |
| 1124 | ", \"b\": true" |
| 1125 | " }", |
| 1126 | "$.a**.b**.c", "[100, 300]", false); |
| 1127 | } |
| 1128 | { |
| 1129 | SCOPED_TRACE(""); |
| 1130 | vet_wrapper_seek( |
| 1131 | "[" |
| 1132 | " 100," |
| 1133 | " [" |
| 1134 | " true," |
| 1135 | " false," |
| 1136 | " true," |
| 1137 | " false," |
| 1138 | " { \"a\": [" |
| 1139 | " 300," |
| 1140 | " 400," |
| 1141 | " [" |
| 1142 | " 1, 2, 3, 4, 5," |
| 1143 | " {" |
| 1144 | " \"b\": [ 500, 600, 700, 800, 900 ]" |
| 1145 | " }" |
| 1146 | " ]" |
| 1147 | " ]" |
| 1148 | " }" |
| 1149 | " ]," |
| 1150 | " 200" |
| 1151 | "]", |
| 1152 | "$[1]**[2]**[3]", "[4, 800]", false); |
| 1153 | } |
| 1154 | |
| 1155 | // $[1][2][3].b[3] is a match for $[1]**[2]**[3] |
| 1156 | { |
| 1157 | SCOPED_TRACE(""); |
| 1158 | vet_wrapper_seek( |
| 1159 | "[" |
| 1160 | " 100," |
| 1161 | " [" |
| 1162 | " 300," |
| 1163 | " 400," |
| 1164 | " [" |
| 1165 | " 1, 2, 3, 4, 5," |
| 1166 | " {" |
| 1167 | " \"b\": [ 500, 600, 700, 800, 900 ]" |
| 1168 | " }" |
| 1169 | " ]" |
| 1170 | " ]," |
| 1171 | " 200" |
| 1172 | "]", |
| 1173 | "$[1]**[2]**[3]", "[4, 800]", false); |
| 1174 | } |
| 1175 | |
| 1176 | /* |
| 1177 | $**[2]**.c matches |
| 1178 | |
| 1179 | $.a[ 2 ][ 1 ].c |
| 1180 | $.c.d[2][5].c |
| 1181 | $.d[2][4].d.c |
| 1182 | |
| 1183 | but not |
| 1184 | |
| 1185 | $.b[ 1 ][ 1 ].c |
| 1186 | $.e[2].c |
| 1187 | */ |
| 1188 | { |
| 1189 | SCOPED_TRACE(""); |
| 1190 | vet_wrapper_seek( |
| 1191 | "{" |
| 1192 | " \"a\": [ 0, 1, [ 0, { \"c\": 100 } ] ]," |
| 1193 | " \"b\": [ 0, [ 0, { \"c\": 200 } ] ]," |
| 1194 | " \"c\": { \"d\": [ 0, 1, [ 0, 1, 2, 3, 4, " |
| 1195 | "{ \"c\": 300 } ] ] }," |
| 1196 | " \"d\": [ 0, 1, [ 0, 1, 2, 3, { \"d\": " |
| 1197 | "{ \"c\": 400 } } ] ]," |
| 1198 | " \"e\": [ 0, 1, { \"c\": 500 } ]" |
| 1199 | "}", |
| 1200 | "$**[2]**.c", "[100, 300, 400, 500]", false); |
| 1201 | } |
| 1202 | |
| 1203 | // auto-wrapping |
| 1204 | { |
| 1205 | SCOPED_TRACE(""); |
| 1206 | vet_wrapper_seek("{ \"a\": 100 }", "$.a[ 0 ]", "100", false); |
| 1207 | } |
| 1208 | { |
| 1209 | SCOPED_TRACE(""); |
| 1210 | vet_wrapper_seek("[ [ 100, 200, 300 ], 400, { \"c\": 500 } ]", "$[*][ 0 ]", |
| 1211 | "[100, 400, {\"c\": 500}]", false); |
| 1212 | } |
| 1213 | |
| 1214 | // auto-wrapping only works for the 0th index |
| 1215 | { |
| 1216 | SCOPED_TRACE(""); |
| 1217 | vet_wrapper_seek("[ [ 100, 200, 300 ], 400, { \"c\": 500 } ]", "$[*][ 1 ]", |
| 1218 | "200", false); |
| 1219 | } |
| 1220 | |
| 1221 | // verify more ellipsis and autowrapping cases. |
| 1222 | |
| 1223 | // these two should have the same result. |
| 1224 | { |
| 1225 | SCOPED_TRACE(""); |
| 1226 | vet_wrapper_seek("[1]", "$[0][0]", "1", false); |
| 1227 | SCOPED_TRACE(""); |
| 1228 | } |
| 1229 | { |
| 1230 | SCOPED_TRACE(""); |
| 1231 | vet_wrapper_seek("[1]", "$**[0]", "1", false); |
| 1232 | } |
| 1233 | |
| 1234 | // these two should have the same result. |
| 1235 | { |
| 1236 | SCOPED_TRACE(""); |
| 1237 | vet_wrapper_seek("{ \"a\": 1 }", "$.a[0]", "1", false); |
| 1238 | SCOPED_TRACE(""); |
| 1239 | } |
| 1240 | { |
| 1241 | SCOPED_TRACE(""); |
| 1242 | vet_wrapper_seek("{ \"a\": 1 }", "$**[0]", "[{\"a\": 1}, 1]", false); |
| 1243 | } |
| 1244 | |
| 1245 | // these two should have the same result. |
| 1246 | { |
| 1247 | SCOPED_TRACE(""); |
| 1248 | vet_wrapper_seek("{ \"a\": 1 }", "$[0].a", "1", false); |
| 1249 | SCOPED_TRACE(""); |
| 1250 | } |
| 1251 | { |
| 1252 | SCOPED_TRACE(""); |
| 1253 | vet_wrapper_seek("{ \"a\": 1 }", "$**.a", "1", false); |
| 1254 | } |
| 1255 | } |
| 1256 | |
| 1257 | TEST_F(JsonPathTest, RemoveDomTest) { |
| 1258 | { |
| 1259 | SCOPED_TRACE(""); |
| 1260 | std::string json_text = "[100, 200, 300]"; |
| 1261 | Json_dom_ptr dom = Json_dom::parse( |
| 1262 | json_text.data(), json_text.length(), [](const char *, size_t) {}, |
| 1263 | [] { ASSERT_TRUE(false); }); |
| 1264 | auto array = static_cast<Json_array *>(dom.get()); |
| 1265 | EXPECT_TRUE(array->remove(1)); |
| 1266 | EXPECT_EQ("[100, 300]", format(array)); |
| 1267 | EXPECT_FALSE(array->remove(2)); |
| 1268 | EXPECT_EQ("[100, 300]", format(array)); |
| 1269 | |
| 1270 | json_text = "{\"a\": 100, \"b\": 200, \"c\": 300}"; |
| 1271 | dom = Json_dom::parse( |
| 1272 | json_text.data(), json_text.length(), [](const char *, size_t) {}, |
| 1273 | [] { ASSERT_TRUE(false); }); |
| 1274 | auto object = static_cast<Json_object *>(dom.get()); |
| 1275 | EXPECT_TRUE(object->remove("b")); |
| 1276 | EXPECT_EQ("{\"a\": 100, \"c\": 300}", format(object)); |
| 1277 | EXPECT_FALSE(object->remove("d")); |
| 1278 | EXPECT_EQ("{\"a\": 100, \"c\": 300}", format(object)); |
| 1279 | } |
| 1280 | |
| 1281 | /* |
| 1282 | test the adding of parent pointers |
| 1283 | */ |
| 1284 | |
| 1285 | // Json_dom.add_alias() |
| 1286 | |
| 1287 | Json_object object1; |
| 1288 | Json_boolean true_literal1(true); |
| 1289 | Json_boolean false_literal1(false); |
| 1290 | Json_null *null_literal1 = new (std::nothrow) Json_null(); |
| 1291 | EXPECT_EQ(nullptr, null_literal1->parent()); |
| 1292 | object1.add_clone(std::string("a"), &true_literal1); |
| 1293 | object1.add_clone(std::string("b"), &false_literal1); |
| 1294 | object1.add_alias(std::string("c"), null_literal1); |
| 1295 | EXPECT_EQ(&object1, null_literal1->parent()); |
| 1296 | EXPECT_EQ("{\"a\": true, \"b\": false, \"c\": null}", format(&object1)); |
| 1297 | SCOPED_TRACE(""); |
| 1298 | EXPECT_TRUE(object1.remove("c")); |
| 1299 | EXPECT_EQ("{\"a\": true, \"b\": false}", format(&object1)); |
| 1300 | EXPECT_FALSE(object1.remove("c")); |
| 1301 | EXPECT_EQ("{\"a\": true, \"b\": false}", format(&object1)); |
| 1302 | |
| 1303 | // Json_dom.add_clone() |
| 1304 | |
| 1305 | Json_null null_literal2; |
| 1306 | EXPECT_EQ(nullptr, null_literal2.parent()); |
| 1307 | std::string key("d"); |
| 1308 | object1.add_clone(key, &null_literal2); |
| 1309 | Json_dom *clone = object1.get(key); |
| 1310 | EXPECT_EQ(&object1, clone->parent()); |
| 1311 | |
| 1312 | // Json_array.append_clone() |
| 1313 | |
| 1314 | Json_array array; |
| 1315 | Json_boolean true_literal2(true); |
| 1316 | Json_boolean false_literal2(false); |
| 1317 | Json_null null_literal3; |
| 1318 | array.append_clone(&true_literal2); |
| 1319 | array.append_clone(&false_literal2); |
| 1320 | array.append_clone(&null_literal3); |
| 1321 | EXPECT_EQ("[true, false, null]", format(&array)); |
| 1322 | Json_dom *cell = array[2]; |
| 1323 | EXPECT_EQ(&array, cell->parent()); |
| 1324 | |
| 1325 | // Json_array.append_alias() |
| 1326 | |
| 1327 | Json_boolean *true_literal3 = new (std::nothrow) Json_boolean(true); |
| 1328 | array.append_alias(true_literal3); |
| 1329 | EXPECT_EQ("[true, false, null, true]", format(&array)); |
| 1330 | EXPECT_EQ(&array, true_literal3->parent()); |
| 1331 | EXPECT_TRUE(array.remove(3)); |
| 1332 | EXPECT_EQ("[true, false, null]", format(&array)); |
| 1333 | EXPECT_FALSE(array.remove(3)); |
| 1334 | EXPECT_EQ("[true, false, null]", format(&array)); |
| 1335 | |
| 1336 | // Json_array.insert_clone() |
| 1337 | |
| 1338 | Json_boolean true_literal4(true); |
| 1339 | array.insert_clone(2, &true_literal4); |
| 1340 | EXPECT_EQ("[true, false, true, null]", format(&array)); |
| 1341 | cell = array[2]; |
| 1342 | EXPECT_EQ(&array, cell->parent()); |
| 1343 | |
| 1344 | // Json_array.insert_alias() |
| 1345 | |
| 1346 | Json_boolean *false_literal3 = new (std::nothrow) Json_boolean(false); |
| 1347 | array.insert_alias(3, Json_dom_ptr(false_literal3)); |
| 1348 | EXPECT_EQ("[true, false, true, false, null]", format(&array)); |
| 1349 | EXPECT_EQ(&array, false_literal3->parent()); |
| 1350 | EXPECT_TRUE(array.remove(3)); |
| 1351 | EXPECT_EQ("[true, false, true, null]", format(&array)); |
| 1352 | EXPECT_FALSE(array.remove(4)); |
| 1353 | EXPECT_EQ("[true, false, true, null]", format(&array)); |
| 1354 | |
| 1355 | // Json_array.insert_clone() |
| 1356 | Json_boolean true_literal5(true); |
| 1357 | array.insert_clone(5, &true_literal5); |
| 1358 | EXPECT_EQ("[true, false, true, null, true]", format(&array)); |
| 1359 | EXPECT_EQ(&array, array[4]->parent()); |
| 1360 | |
| 1361 | // Json_array.insert_alias() |
| 1362 | Json_boolean *false_literal4 = new (std::nothrow) Json_boolean(false); |
| 1363 | array.insert_alias(7, Json_dom_ptr(false_literal4)); |
| 1364 | EXPECT_EQ("[true, false, true, null, true, false]", format(&array)); |
| 1365 | EXPECT_EQ(&array, false_literal4->parent()); |
| 1366 | EXPECT_EQ(&array, array[5]->parent()); |
| 1367 | EXPECT_TRUE(array.remove(5)); |
| 1368 | EXPECT_EQ("[true, false, true, null, true]", format(&array)); |
| 1369 | EXPECT_FALSE(array.remove(5)); |
| 1370 | EXPECT_EQ("[true, false, true, null, true]", format(&array)); |
| 1371 | } |
| 1372 | |
| 1373 | // Tuples for the test of Json_dom.get_location() |
| 1374 | static const Location_tuple location_tuples[] = { |
| 1375 | {"true", "$"}, |
| 1376 | {"[true, false, null]", "$"}, |
| 1377 | {"[true, false, null]", "$[1]"}, |
| 1378 | {"{ \"a\": true}", "$"}, |
| 1379 | {"{ \"a\": true}", "$.a"}, |
| 1380 | {"{ \"a\": true, \"b\": [1, 2, 3] }", "$.b[2]"}, |
| 1381 | {"[ 0, 1, { \"a\": true, \"b\": [1, 2, 3] } ]", "$[2].b[0]"}, |
| 1382 | }; |
| 1383 | |
| 1384 | /** Test good paths without column scope */ |
| 1385 | TEST_P(JsonGoodLocationTestP, GoodLocations) { |
| 1386 | Location_tuple param = GetParam(); |
| 1387 | vet_dom_location(param.m_json_text, param.m_path_expression); |
| 1388 | } |
| 1389 | |
| 1390 | INSTANTIATE_TEST_SUITE_P(LocationTesting, JsonGoodLocationTestP, |
| 1391 | ::testing::ValuesIn(location_tuples)); |
| 1392 | |
| 1393 | // Tuples for the test of the only_needs_one arg of Json_wrapper.seek() |
| 1394 | static const Ono_tuple ono_tuples[] = { |
| 1395 | {"[ { \"a\": 1 }, { \"a\": 2 } ]", "$[*].a", 2}, |
| 1396 | {"[ { \"a\": 1 }, { \"a\": 2 } ]", "$**.a", 2}, |
| 1397 | {"{ \"a\": { \"x\" : { \"b\": { \"y\": { \"b\": " |
| 1398 | "{ \"z\": { \"c\": 100 }, \"c\": 200 } } } } } }", |
| 1399 | "$.a**.b**.c", 2}, |
| 1400 | }; |
| 1401 | |
| 1402 | /** Test good paths without column scope */ |
| 1403 | TEST_P(JsonGoodOnoTestP, GoodOno) { |
| 1404 | Ono_tuple param = GetParam(); |
| 1405 | vet_only_needs_one(param.m_json_text, param.m_path_expression, |
| 1406 | param.m_expected_hits, thd()); |
| 1407 | } |
| 1408 | |
| 1409 | INSTANTIATE_TEST_SUITE_P(OnoTesting, JsonGoodOnoTestP, |
| 1410 | ::testing::ValuesIn(ono_tuples)); |
| 1411 | |
| 1412 | // Tuples for tests of cloning |
| 1413 | static const Clone_tuple clone_tuples[] = { |
| 1414 | {"$", "$[33]"}, |
| 1415 | {"$[*].a", "$.a.b.c.d.e"}, |
| 1416 | {"$.a.b.c[73]", "$**.abc.d.e.f.g"}, |
| 1417 | }; |
| 1418 | |
| 1419 | /** Test cloning. */ |
| 1420 | TEST_P(JsonGoodCloneTestP, GoodClone) { |
| 1421 | Clone_tuple param = GetParam(); |
| 1422 | verify_clone(param.m_path_expression_1, param.m_path_expression_2); |
| 1423 | } |
| 1424 | |
| 1425 | INSTANTIATE_TEST_SUITE_P(CloneTesting, JsonGoodCloneTestP, |
| 1426 | ::testing::ValuesIn(clone_tuples)); |
| 1427 | |
| 1428 | /** |
| 1429 | A class used for parameterized test cases for the |
| 1430 | Json_path_leg::is_autowrap() function. |
| 1431 | */ |
| 1432 | class JsonPathLegAutowrapP |
| 1433 | : public ::testing::TestWithParam<std::pair<std::string, bool>> {}; |
| 1434 | |
| 1435 | TEST_P(JsonPathLegAutowrapP, Autowrap) { |
| 1436 | const auto param = GetParam(); |
| 1437 | const std::string path_text = "$" + param.first + ".a"; |
| 1438 | const bool expected_result = param.second; |
| 1439 | |
| 1440 | Json_path path(key_memory_JSON); |
| 1441 | size_t idx = 0; |
| 1442 | EXPECT_FALSE(parse_path(path_text.length(), path_text.data(), &path, &idx, |
| 1443 | [] { ASSERT_TRUE(false); })); |
| 1444 | EXPECT_EQ(0U, idx); |
| 1445 | EXPECT_EQ(2U, path.leg_count()); |
| 1446 | EXPECT_EQ(expected_result, (*path.begin())->is_autowrap()); |
| 1447 | } |
| 1448 | |
| 1449 | static const std::pair<std::string, bool> autowrap_tuples[] = { |
| 1450 | // These should match non-arrays due to auto-wrapping. |
| 1451 | {"[0]", true}, |
| 1452 | {"[last]", true}, |
| 1453 | {"[last-0]", true}, |
| 1454 | {"[0 to last]", true}, |
| 1455 | {"[0 to last-0]", true}, |
| 1456 | {"[0 to 0]", true}, |
| 1457 | {"[0 to 1]", true}, |
| 1458 | {"[0 to 100]", true}, |
| 1459 | {"[last to 0]", true}, |
| 1460 | {"[last to 1]", true}, |
| 1461 | {"[last-0 to 1]", true}, |
| 1462 | {"[last to 100]", true}, |
| 1463 | {"[last to last]", true}, |
| 1464 | {"[last-1 to last]", true}, |
| 1465 | {"[last-100 to last]", true}, |
| 1466 | {"[last-1 to 0]", true}, |
| 1467 | {"[last-1 to 1]", true}, |
| 1468 | |
| 1469 | // These should not match non-arrays. |
| 1470 | {"[*]", false}, |
| 1471 | {".*", false}, |
| 1472 | {"**", false}, |
| 1473 | {".name", false}, |
| 1474 | {".\"0\"", false}, |
| 1475 | {"[1]", false}, |
| 1476 | {"[100]", false}, |
| 1477 | {"[last-1]", false}, |
| 1478 | {"[last-100]", false}, |
| 1479 | {"[0 to last-1]", false}, |
| 1480 | {"[1 to last]", false}, |
| 1481 | {"[last-2 to last-1]", false}, |
| 1482 | }; |
| 1483 | |
| 1484 | INSTANTIATE_TEST_SUITE_P(AutowrapTesting, JsonPathLegAutowrapP, |
| 1485 | ::testing::ValuesIn(autowrap_tuples)); |
| 1486 | |
| 1487 | } // end namespace json_path_unittest |