| 1 | /* Copyright (c) 2011, 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 | /** |
| 25 | @file |
| 26 | Unit test of the Optimizer trace API (WL#5257) |
| 27 | */ |
| 28 | |
| 29 | #include <gtest/gtest.h> |
| 30 | #include <limits.h> |
| 31 | #include <string.h> |
| 32 | #include <sys/types.h> |
| 33 | #include <cmath> |
| 34 | #include <limits> |
| 35 | #include <regex> |
| 36 | #include <string> |
| 37 | |
| 38 | #include "m_ctype.h" |
| 39 | #include "m_string.h" // llstr |
| 40 | #include "my_dbug.h" |
| 41 | #include "my_inttypes.h" |
| 42 | #include "my_macros.h" |
| 43 | #include "my_sys.h" |
| 44 | #include "mysys_err.h" // for testing of OOM |
| 45 | #include "sql-common/json_dom.h" |
| 46 | #include "sql/opt_trace.h" |
| 47 | #include "sql/opt_trace_context.h" |
| 48 | |
| 49 | namespace opt_trace_unittest { |
| 50 | |
| 51 | const ulonglong all_features = Opt_trace_context::default_features; |
| 52 | |
| 53 | /** |
| 54 | @note It is a macro, for proper reporting of line numbers in case of |
| 55 | assertion failure. SCOPED_TRACE will report line number at the |
| 56 | macro expansion site. |
| 57 | */ |
| 58 | #define check_json_compliance(str, length) \ |
| 59 | { \ |
| 60 | SCOPED_TRACE(""); \ |
| 61 | do_check_json_compliance(str, length); \ |
| 62 | } |
| 63 | |
| 64 | /** |
| 65 | Checks compliance of a trace with JSON syntax rules. |
| 66 | @param str pointer to trace |
| 67 | @param length trace's length |
| 68 | */ |
| 69 | static void do_check_json_compliance(const char *str, size_t length) { |
| 70 | // There is an option for outputting end markers as comments in the optimizer |
| 71 | // trace. Comments are not understood by our JSON parser, so eliminate them |
| 72 | // before validating the trace. |
| 73 | std::string json_document(str, length); |
| 74 | std::regex comment_re("/\\*[ A-Za-z_]* \\*/"); |
| 75 | json_document = std::regex_replace(json_document, comment_re, ""); |
| 76 | |
| 77 | const char *errmsg = nullptr; |
| 78 | size_t errpos = 0; |
| 79 | Json_dom_ptr dom = Json_dom::parse( |
| 80 | json_document.data(), json_document.size(), [](const char *, size_t) {}, |
| 81 | [] { ASSERT_TRUE(false); }); |
| 82 | ASSERT_NE(nullptr, dom) << "Parse error: " << errmsg |
| 83 | << "\nError position: " << errpos << "\nDocument:\n" |
| 84 | << json_document; |
| 85 | } |
| 86 | |
| 87 | extern "C" void my_error_handler(uint error, const char *str, myf MyFlags); |
| 88 | |
| 89 | class TraceContentTest : public ::testing::Test { |
| 90 | public: |
| 91 | Opt_trace_context trace; |
| 92 | static bool oom; ///< whether we got an OOM error from opt trace |
| 93 | protected: |
| 94 | void SetUp() override { |
| 95 | /* Save original and install our custom error hook. */ |
| 96 | m_old_error_handler_hook = error_handler_hook; |
| 97 | error_handler_hook = my_error_handler; |
| 98 | oom = false; |
| 99 | // Setting debug flags triggers enter/exit trace, so redirect to /dev/null |
| 100 | DBUG_SET("o," IF_WIN("NUL", "/dev/null")); |
| 101 | } |
| 102 | void TearDown() override { error_handler_hook = m_old_error_handler_hook; } |
| 103 | |
| 104 | static ErrorHandlerFunctionPointer m_old_error_handler_hook; |
| 105 | }; |
| 106 | bool TraceContentTest::oom; |
| 107 | ErrorHandlerFunctionPointer TraceContentTest::m_old_error_handler_hook; |
| 108 | |
| 109 | void my_error_handler(uint error, const char *, myf) { |
| 110 | const uint EE = static_cast<uint>(EE_OUTOFMEMORY); |
| 111 | EXPECT_EQ(EE, error); |
| 112 | if (error == EE) TraceContentTest::oom = true; |
| 113 | } |
| 114 | |
| 115 | TEST_F(TraceContentTest, ConstructAndDestruct) {} |
| 116 | |
| 117 | /** Test empty trace */ |
| 118 | TEST_F(TraceContentTest, Empty) { |
| 119 | ASSERT_FALSE( |
| 120 | trace.start(true, false, false, false, -1, 1, ULONG_MAX, all_features)); |
| 121 | EXPECT_TRUE(trace.is_started()); |
| 122 | EXPECT_TRUE(trace.support_I_S()); |
| 123 | /* |
| 124 | Add at least an object to it. A really empty trace ("") is not |
| 125 | JSON-compliant. |
| 126 | */ |
| 127 | { Opt_trace_object oto(&trace); } |
| 128 | /* End trace */ |
| 129 | trace.end(); |
| 130 | /* And verify trace's content */ |
| 131 | Opt_trace_iterator it(&trace); |
| 132 | /* |
| 133 | ASSERT here, because a failing EXPECT_FALSE would continue into |
| 134 | it.get_value() and segfault. |
| 135 | */ |
| 136 | ASSERT_FALSE(it.at_end()); |
| 137 | Opt_trace_info info; |
| 138 | it.get_value(&info); |
| 139 | const char expected[] = "{\n}"; |
| 140 | EXPECT_STREQ(expected, info.trace_ptr); |
| 141 | EXPECT_EQ(sizeof(expected) - 1, info.trace_length); |
| 142 | check_json_compliance(info.trace_ptr, info.trace_length); |
| 143 | EXPECT_EQ(0U, info.missing_bytes); |
| 144 | EXPECT_FALSE(info.missing_priv); |
| 145 | EXPECT_FALSE(oom); |
| 146 | /* Should be no more traces */ |
| 147 | it.next(); |
| 148 | ASSERT_TRUE(it.at_end()); |
| 149 | } |
| 150 | |
| 151 | /** Test normal usage */ |
| 152 | TEST_F(TraceContentTest, NormalUsage) { |
| 153 | ASSERT_FALSE( |
| 154 | trace.start(true, false, true, false, -1, 1, ULONG_MAX, all_features)); |
| 155 | { |
| 156 | Opt_trace_object oto(&trace); |
| 157 | oto.add_select_number(123456); |
| 158 | { |
| 159 | Opt_trace_array ota(&trace, "one array"); |
| 160 | ota.add(200.4); |
| 161 | { |
| 162 | Opt_trace_object oto1(&trace); |
| 163 | oto1.add_alnum("one key", "one value").add("another key", 100U); |
| 164 | } |
| 165 | ota.add_alnum("one string element"); |
| 166 | ota.add(true); |
| 167 | } |
| 168 | oto.add("yet another key", -1000LL); |
| 169 | { |
| 170 | Opt_trace_array ota(&trace, "another array"); |
| 171 | ota.add(1LL).add(2).add(3LL).add(4LL); |
| 172 | } |
| 173 | } |
| 174 | trace.end(); |
| 175 | Opt_trace_iterator it(&trace); |
| 176 | ASSERT_FALSE(it.at_end()); |
| 177 | Opt_trace_info info; |
| 178 | it.get_value(&info); |
| 179 | const char expected[] = |
| 180 | "{\n" |
| 181 | " \"select#\": 123456,\n" |
| 182 | " \"one array\": [\n" |
| 183 | " 200.4,\n" |
| 184 | " {\n" |
| 185 | " \"one key\": \"one value\",\n" |
| 186 | " \"another key\": 100\n" |
| 187 | " },\n" |
| 188 | " \"one string element\",\n" |
| 189 | " true\n" |
| 190 | " ] /* one array */,\n" |
| 191 | " \"yet another key\": -1000,\n" |
| 192 | " \"another array\": [\n" |
| 193 | " 1,\n" |
| 194 | " 2,\n" |
| 195 | " 3,\n" |
| 196 | " 4\n" |
| 197 | " ] /* another array */\n" |
| 198 | "}"; |
| 199 | EXPECT_STREQ(expected, info.trace_ptr); |
| 200 | EXPECT_EQ(sizeof(expected) - 1, info.trace_length); |
| 201 | check_json_compliance(info.trace_ptr, info.trace_length); |
| 202 | EXPECT_EQ(0U, info.missing_bytes); |
| 203 | EXPECT_FALSE(info.missing_priv); |
| 204 | EXPECT_FALSE(oom); |
| 205 | it.next(); |
| 206 | ASSERT_TRUE(it.at_end()); |
| 207 | } |
| 208 | |
| 209 | /** Test reaction to malformed JSON (object with value without key) */ |
| 210 | TEST_F(TraceContentTest, BuggyObject) { |
| 211 | ASSERT_FALSE( |
| 212 | trace.start(true, false, true, false, -1, 1, ULONG_MAX, all_features)); |
| 213 | { |
| 214 | Opt_trace_object oto(&trace); |
| 215 | { |
| 216 | Opt_trace_array ota(&trace, "one array"); |
| 217 | ota.add(200.4); |
| 218 | { |
| 219 | Opt_trace_object oto1(&trace); |
| 220 | oto1.add_alnum("one value"); // no key, which is wrong |
| 221 | oto1.add(326); // same |
| 222 | Opt_trace_object oto2(&trace); // same |
| 223 | } |
| 224 | ota.add_alnum("one string element"); |
| 225 | ota.add(true); |
| 226 | } |
| 227 | oto.add("yet another key", -1000LL); |
| 228 | { |
| 229 | Opt_trace_array ota(&trace, "another array"); |
| 230 | ota.add(1LL).add(2LL).add(3LL).add(4LL); |
| 231 | } |
| 232 | } |
| 233 | trace.end(); |
| 234 | Opt_trace_iterator it(&trace); |
| 235 | ASSERT_FALSE(it.at_end()); |
| 236 | Opt_trace_info info; |
| 237 | it.get_value(&info); |
| 238 | const char expected[] = |
| 239 | "{\n" |
| 240 | " \"one array\": [\n" |
| 241 | " 200.4,\n" |
| 242 | " {\n" |
| 243 | " \"unknown_key_1\": \"one value\",\n" |
| 244 | " \"unknown_key_2\": 326,\n" |
| 245 | " \"unknown_key_3\": {\n" |
| 246 | " }\n" |
| 247 | " },\n" |
| 248 | " \"one string element\",\n" |
| 249 | " true\n" |
| 250 | " ] /* one array */,\n" |
| 251 | " \"yet another key\": -1000,\n" |
| 252 | " \"another array\": [\n" |
| 253 | " 1,\n" |
| 254 | " 2,\n" |
| 255 | " 3,\n" |
| 256 | " 4\n" |
| 257 | " ] /* another array */\n" |
| 258 | "}"; |
| 259 | EXPECT_STREQ(expected, info.trace_ptr); |
| 260 | EXPECT_EQ(sizeof(expected) - 1, info.trace_length); |
| 261 | check_json_compliance(info.trace_ptr, info.trace_length); |
| 262 | EXPECT_EQ(0U, info.missing_bytes); |
| 263 | EXPECT_FALSE(info.missing_priv); |
| 264 | EXPECT_FALSE(oom); |
| 265 | it.next(); |
| 266 | ASSERT_TRUE(it.at_end()); |
| 267 | } |
| 268 | |
| 269 | /** Test reaction to malformed JSON (array with value with key) */ |
| 270 | TEST_F(TraceContentTest, BuggyArray) { |
| 271 | ASSERT_FALSE( |
| 272 | trace.start(true, false, true, false, -1, 1, ULONG_MAX, all_features)); |
| 273 | { |
| 274 | Opt_trace_object oto(&trace); |
| 275 | { |
| 276 | Opt_trace_array ota(&trace, "one array"); |
| 277 | ota.add("superfluous key", 200.4); // key, which is wrong |
| 278 | ota.add("not necessary", 326); // same |
| 279 | Opt_trace_object oto2(&trace, "not needed"); // same |
| 280 | } |
| 281 | oto.add("yet another key", -1000LL); |
| 282 | { |
| 283 | Opt_trace_array ota(&trace, "another array"); |
| 284 | ota.add(1LL).add(2LL).add(3LL).add(4LL); |
| 285 | } |
| 286 | } |
| 287 | trace.end(); |
| 288 | Opt_trace_iterator it(&trace); |
| 289 | ASSERT_FALSE(it.at_end()); |
| 290 | Opt_trace_info info; |
| 291 | it.get_value(&info); |
| 292 | const char expected[] = |
| 293 | "{\n" |
| 294 | " \"one array\": [\n" |
| 295 | " 200.4,\n" |
| 296 | " 326,\n" |
| 297 | " {\n" |
| 298 | " } /* not needed */\n" |
| 299 | " ] /* one array */,\n" |
| 300 | " \"yet another key\": -1000,\n" |
| 301 | " \"another array\": [\n" |
| 302 | " 1,\n" |
| 303 | " 2,\n" |
| 304 | " 3,\n" |
| 305 | " 4\n" |
| 306 | " ] /* another array */\n" |
| 307 | "}"; |
| 308 | EXPECT_STREQ(expected, info.trace_ptr); |
| 309 | EXPECT_EQ(sizeof(expected) - 1, info.trace_length); |
| 310 | check_json_compliance(info.trace_ptr, info.trace_length); |
| 311 | EXPECT_EQ(0U, info.missing_bytes); |
| 312 | EXPECT_FALSE(info.missing_priv); |
| 313 | EXPECT_FALSE(oom); |
| 314 | it.next(); |
| 315 | ASSERT_TRUE(it.at_end()); |
| 316 | } |
| 317 | |
| 318 | /** Test Opt_trace_disable_I_S */ |
| 319 | TEST_F(TraceContentTest, DisableISWithObject) { |
| 320 | ASSERT_FALSE( |
| 321 | trace.start(true, false, true, false, -1, 1, ULONG_MAX, all_features)); |
| 322 | { |
| 323 | Opt_trace_object oto(&trace); |
| 324 | { |
| 325 | Opt_trace_array ota(&trace, "one array"); |
| 326 | ota.add(200.4); |
| 327 | { |
| 328 | Opt_trace_object oto1(&trace); |
| 329 | oto1.add_alnum("one key", "one value").add("another key", 100LL); |
| 330 | Opt_trace_disable_I_S otd(&trace, true); |
| 331 | oto1.add("a third key", false); |
| 332 | Opt_trace_object oto2(&trace, "a fourth key"); |
| 333 | oto2.add("key inside", 1LL); |
| 334 | /* don't disable... but above layer is stronger */ |
| 335 | Opt_trace_disable_I_S otd2(&trace, false); |
| 336 | oto2.add("another key inside", 5LL); |
| 337 | // disabling should apply to substatements too: |
| 338 | ASSERT_FALSE(trace.start(true, false, true, false, -1, 1, ULONG_MAX, |
| 339 | all_features)); |
| 340 | { Opt_trace_object oto3(&trace); } |
| 341 | trace.end(); |
| 342 | } |
| 343 | ota.add_alnum("one string element"); |
| 344 | ota.add(true); |
| 345 | } |
| 346 | Opt_trace_disable_I_S otd2(&trace, false); // don't disable |
| 347 | oto.add("yet another key", -1000LL); |
| 348 | { |
| 349 | Opt_trace_array ota(&trace, "another array"); |
| 350 | ota.add(1LL).add(2LL).add(3LL).add(4LL); |
| 351 | } |
| 352 | } |
| 353 | trace.end(); |
| 354 | Opt_trace_iterator it(&trace); |
| 355 | ASSERT_FALSE(it.at_end()); |
| 356 | Opt_trace_info info; |
| 357 | it.get_value(&info); |
| 358 | const char expected[] = |
| 359 | "{\n" |
| 360 | " \"one array\": [\n" |
| 361 | " 200.4,\n" |
| 362 | " {\n" |
| 363 | " \"one key\": \"one value\",\n" |
| 364 | " \"another key\": 100\n" |
| 365 | " },\n" |
| 366 | " \"one string element\",\n" |
| 367 | " true\n" |
| 368 | " ] /* one array */,\n" |
| 369 | " \"yet another key\": -1000,\n" |
| 370 | " \"another array\": [\n" |
| 371 | " 1,\n" |
| 372 | " 2,\n" |
| 373 | " 3,\n" |
| 374 | " 4\n" |
| 375 | " ] /* another array */\n" |
| 376 | "}"; |
| 377 | EXPECT_STREQ(expected, info.trace_ptr); |
| 378 | EXPECT_EQ(sizeof(expected) - 1, info.trace_length); |
| 379 | check_json_compliance(info.trace_ptr, info.trace_length); |
| 380 | EXPECT_EQ(0U, info.missing_bytes); |
| 381 | EXPECT_FALSE(info.missing_priv); |
| 382 | EXPECT_FALSE(oom); |
| 383 | it.next(); |
| 384 | ASSERT_TRUE(it.at_end()); |
| 385 | } |
| 386 | |
| 387 | /** Test Opt_trace_context::disable_I_S_for_this_and_children */ |
| 388 | TEST_F(TraceContentTest, DisableISWithCall) { |
| 389 | // Test that it disables even before any start() |
| 390 | trace.disable_I_S_for_this_and_children(); |
| 391 | ASSERT_FALSE( |
| 392 | trace.start(true, false, true, false, -1, 1, ULONG_MAX, all_features)); |
| 393 | { |
| 394 | Opt_trace_object oto(&trace); |
| 395 | { |
| 396 | Opt_trace_array ota(&trace, "one array"); |
| 397 | ota.add(200.4); |
| 398 | { |
| 399 | Opt_trace_object oto1(&trace); |
| 400 | oto1.add_alnum("one key", "one value").add("another key", 100LL); |
| 401 | oto1.add("a third key", false); |
| 402 | Opt_trace_object oto2(&trace, "a fourth key"); |
| 403 | oto2.add("key inside", 1LL); |
| 404 | // disabling should apply to substatements too: |
| 405 | ASSERT_FALSE(trace.start(true, false, true, false, -1, 1, ULONG_MAX, |
| 406 | all_features)); |
| 407 | { Opt_trace_object oto3(&trace); } |
| 408 | trace.end(); |
| 409 | /* don't disable... but above layer is stronger */ |
| 410 | Opt_trace_disable_I_S otd2(&trace, false); |
| 411 | oto2.add("another key inside", 5LL); |
| 412 | // disabling should apply to substatements too: |
| 413 | ASSERT_FALSE(trace.start(true, false, true, false, -1, 1, ULONG_MAX, |
| 414 | all_features)); |
| 415 | { Opt_trace_object oto4(&trace); } |
| 416 | trace.end(); |
| 417 | } |
| 418 | ota.add_alnum("one string element"); |
| 419 | ota.add(true); |
| 420 | } |
| 421 | oto.add("yet another key", -1000LL); |
| 422 | { |
| 423 | Opt_trace_array ota(&trace, "another array"); |
| 424 | ota.add(1LL).add(2LL).add(3LL).add(4LL); |
| 425 | } |
| 426 | } |
| 427 | trace.end(); |
| 428 | trace.restore_I_S(); |
| 429 | Opt_trace_iterator it(&trace); |
| 430 | ASSERT_TRUE(it.at_end()); |
| 431 | } |
| 432 | |
| 433 | /** Helper for Trace_settings_test.offset */ |
| 434 | void make_one_trace(Opt_trace_context *trace, const char *name, long offset, |
| 435 | long limit) { |
| 436 | ASSERT_FALSE(trace->start(true, false, true, false, offset, limit, ULONG_MAX, |
| 437 | all_features)); |
| 438 | { |
| 439 | Opt_trace_object oto(trace); |
| 440 | oto.add(name, 0LL); |
| 441 | } |
| 442 | trace->end(); |
| 443 | } |
| 444 | |
| 445 | /** |
| 446 | Helper for Trace_settings_test.offset |
| 447 | |
| 448 | @param trace The trace context. |
| 449 | @param names A NULL-terminated array of "names". |
| 450 | |
| 451 | Checks that the list of traces is as expected. |
| 452 | This macro checks that the first trace contains names[0], that the second |
| 453 | trace contains names[1], etc. That the number of traces is the same as |
| 454 | the number of elements in "names". |
| 455 | |
| 456 | @note It is a macro, for proper reporting of line numbers in case of |
| 457 | assertion failure. SCOPED_TRACE will report line number at the |
| 458 | macro expansion site. |
| 459 | */ |
| 460 | #define check(trace, names) \ |
| 461 | { \ |
| 462 | SCOPED_TRACE(""); \ |
| 463 | do_check(&trace, names); \ |
| 464 | } |
| 465 | |
| 466 | void do_check(Opt_trace_context *trace, const char **names) { |
| 467 | Opt_trace_iterator it(trace); |
| 468 | Opt_trace_info info; |
| 469 | for (const char **name = names; *name != nullptr; name++) { |
| 470 | ASSERT_FALSE(it.at_end()); |
| 471 | it.get_value(&info); |
| 472 | const size_t name_len = strlen(*name); |
| 473 | EXPECT_EQ(name_len + 11, info.trace_length); |
| 474 | EXPECT_EQ(0, strncmp(info.trace_ptr + 5, *name, name_len)); |
| 475 | EXPECT_EQ(0U, info.missing_bytes); |
| 476 | it.next(); |
| 477 | } |
| 478 | ASSERT_TRUE(it.at_end()); |
| 479 | } |
| 480 | |
| 481 | /** Test offset/limit variables */ |
| 482 | TEST_F(TraceContentTest, Offset) { |
| 483 | make_one_trace(&trace, "100", -1 /* offset */, 1 /* limit */); |
| 484 | const char *expected_traces0[] = {"100", nullptr}; |
| 485 | check(trace, expected_traces0); |
| 486 | make_one_trace(&trace, "101", -1, 1); |
| 487 | /* 101 should have overwritten 100 */ |
| 488 | const char *expected_traces1[] = {"101", nullptr}; |
| 489 | check(trace, expected_traces1); |
| 490 | make_one_trace(&trace, "102", -1, 1); |
| 491 | const char *expected_traces2[] = {"102", nullptr}; |
| 492 | check(trace, expected_traces2); |
| 493 | trace.reset(); |
| 494 | const char *expected_traces_empty[] = {nullptr}; |
| 495 | check(trace, expected_traces_empty); |
| 496 | make_one_trace(&trace, "103", -3, 2); |
| 497 | make_one_trace(&trace, "104", -3, 2); |
| 498 | make_one_trace(&trace, "105", -3, 2); |
| 499 | make_one_trace(&trace, "106", -3, 2); |
| 500 | make_one_trace(&trace, "107", -3, 2); |
| 501 | make_one_trace(&trace, "108", -3, 2); |
| 502 | make_one_trace(&trace, "109", -3, 2); |
| 503 | const char *expected_traces3[] = {"107", "108", nullptr}; |
| 504 | check(trace, expected_traces3); |
| 505 | trace.reset(); |
| 506 | check(trace, expected_traces_empty); |
| 507 | make_one_trace(&trace, "110", 3, 2); |
| 508 | make_one_trace(&trace, "111", 3, 2); |
| 509 | make_one_trace(&trace, "112", 3, 2); |
| 510 | make_one_trace(&trace, "113", 3, 2); |
| 511 | make_one_trace(&trace, "114", 3, 2); |
| 512 | make_one_trace(&trace, "115", 3, 2); |
| 513 | make_one_trace(&trace, "116", 3, 2); |
| 514 | const char *expected_traces10[] = {"113", "114", nullptr}; |
| 515 | check(trace, expected_traces10); |
| 516 | trace.reset(); |
| 517 | check(trace, expected_traces_empty); |
| 518 | make_one_trace(&trace, "117", 0, 1); |
| 519 | make_one_trace(&trace, "118", 0, 1); |
| 520 | make_one_trace(&trace, "119", 0, 1); |
| 521 | const char *expected_traces17[] = {"117", nullptr}; |
| 522 | check(trace, expected_traces17); |
| 523 | trace.reset(); |
| 524 | make_one_trace(&trace, "120", 0, 0); |
| 525 | make_one_trace(&trace, "121", 0, 0); |
| 526 | make_one_trace(&trace, "122", 0, 0); |
| 527 | const char *expected_traces20[] = {nullptr}; |
| 528 | check(trace, expected_traces20); |
| 529 | EXPECT_FALSE(oom); |
| 530 | } |
| 531 | |
| 532 | /** Test truncation by max_mem_size */ |
| 533 | TEST_F(TraceContentTest, MaxMemSize) { |
| 534 | ASSERT_FALSE(trace.start(true, false, false, false, -1, 1, |
| 535 | 1000 /* max_mem_size */, all_features)); |
| 536 | /* make a "long" trace */ |
| 537 | { |
| 538 | Opt_trace_object oto(&trace); |
| 539 | Opt_trace_array ota(&trace, "one array"); |
| 540 | for (int i = 0; i < 100; i++) { |
| 541 | ota.add_alnum("make it long"); |
| 542 | } |
| 543 | } |
| 544 | trace.end(); |
| 545 | Opt_trace_iterator it(&trace); |
| 546 | ASSERT_FALSE(it.at_end()); |
| 547 | Opt_trace_info info; |
| 548 | it.get_value(&info); |
| 549 | const char expected[] = |
| 550 | "{\n" |
| 551 | " \"one array\": [\n" |
| 552 | " \"make it long\",\n" |
| 553 | " \"make it long\",\n"; |
| 554 | /* |
| 555 | Without truncation the trace would take: |
| 556 | 2+17+3+1+20*100 = 2023 |
| 557 | */ |
| 558 | EXPECT_EQ(996U, info.trace_length); |
| 559 | EXPECT_EQ(1027U, info.missing_bytes); // 996+1027=2023 |
| 560 | EXPECT_FALSE(info.missing_priv); |
| 561 | EXPECT_FALSE(oom); |
| 562 | EXPECT_EQ(0, strncmp(expected, info.trace_ptr, sizeof(expected) - 1)); |
| 563 | it.next(); |
| 564 | ASSERT_TRUE(it.at_end()); |
| 565 | } |
| 566 | |
| 567 | /** Test how truncation by max_mem_size affects next traces */ |
| 568 | TEST_F(TraceContentTest, MaxMemSize2) { |
| 569 | ASSERT_FALSE(trace.start(true, false, false, false, -2, 2, |
| 570 | 21 /* max_mem_size */, all_features)); |
| 571 | /* make a "long" trace */ |
| 572 | { |
| 573 | Opt_trace_object oto(&trace); |
| 574 | oto.add_alnum("some key1", "make it long"); |
| 575 | } |
| 576 | trace.end(); |
| 577 | /* A second similar trace */ |
| 578 | ASSERT_FALSE(trace.start(true, false, false, false, -2, 2, 21, all_features)); |
| 579 | { |
| 580 | Opt_trace_object oto(&trace); |
| 581 | oto.add_alnum("some key2", "make it long"); |
| 582 | } |
| 583 | trace.end(); |
| 584 | Opt_trace_iterator it(&trace); |
| 585 | ASSERT_FALSE(it.at_end()); |
| 586 | Opt_trace_info info; |
| 587 | it.get_value(&info); |
| 588 | EXPECT_EQ(17U, info.trace_length); |
| 589 | EXPECT_EQ(16U, info.missing_bytes); |
| 590 | EXPECT_FALSE(info.missing_priv); |
| 591 | EXPECT_FALSE(oom); |
| 592 | it.next(); |
| 593 | ASSERT_FALSE(it.at_end()); |
| 594 | it.get_value(&info); |
| 595 | /* 2nd trace completely empty as first trace left no room */ |
| 596 | EXPECT_EQ(0U, info.trace_length); |
| 597 | EXPECT_EQ(33U, info.missing_bytes); |
| 598 | it.next(); |
| 599 | ASSERT_TRUE(it.at_end()); |
| 600 | /* |
| 601 | 3rd trace; the first one should automatically be purged, thus the 3rd |
| 602 | should have a bit of room. |
| 603 | */ |
| 604 | ASSERT_FALSE(trace.start(true, false, false, false, -2, 2, 21, all_features)); |
| 605 | { |
| 606 | Opt_trace_object oto(&trace); |
| 607 | oto.add_alnum("some key3", "make it long"); |
| 608 | } |
| 609 | trace.end(); |
| 610 | Opt_trace_iterator it2(&trace); |
| 611 | ASSERT_FALSE(it2.at_end()); |
| 612 | it2.get_value(&info); |
| 613 | EXPECT_EQ(0U, info.trace_length); |
| 614 | EXPECT_EQ(33U, info.missing_bytes); |
| 615 | EXPECT_FALSE(info.missing_priv); |
| 616 | EXPECT_FALSE(oom); |
| 617 | it2.next(); |
| 618 | it2.get_value(&info); |
| 619 | /* |
| 620 | 3rd one had room. A bit less than first, because just reading the second |
| 621 | with the iterator has reallocated the second from 0 to 8 bytes... |
| 622 | */ |
| 623 | EXPECT_EQ(14U, info.trace_length); |
| 624 | EXPECT_EQ(19U, info.missing_bytes); |
| 625 | EXPECT_FALSE(info.missing_priv); |
| 626 | it2.next(); |
| 627 | ASSERT_TRUE(it2.at_end()); |
| 628 | } |
| 629 | |
| 630 | void open_object(uint count, Opt_trace_context *trace, bool simulate_oom) { |
| 631 | if (count == 0) return; |
| 632 | count--; |
| 633 | char key[4]; |
| 634 | /* |
| 635 | Add 100 to always have a key of length 3, this matters to |
| 636 | TraceContentTest.Indent. |
| 637 | We could use just a fixed string, but it would cause an assertion failure |
| 638 | (due to invalid JSON, which itself is conceivable in case of OOM). |
| 639 | */ |
| 640 | llstr(100 + count, key); |
| 641 | if (simulate_oom) { |
| 642 | if (count == 90) DBUG_SET("+d,opt_trace_oom_in_open_struct"); |
| 643 | /* |
| 644 | Now we let 80 objects be created, so that one of them surely hits |
| 645 | re-allocation and OOM failure. |
| 646 | */ |
| 647 | if (count == 10) DBUG_SET("-d,opt_trace_oom_in_open_struct"); |
| 648 | } |
| 649 | Opt_trace_object oto(trace, key); |
| 650 | open_object(count, trace, simulate_oom); |
| 651 | } |
| 652 | |
| 653 | #ifndef NDEBUG |
| 654 | |
| 655 | /// Test reaction to out-of-memory condition in trace buffer |
| 656 | TEST_F(TraceContentTest, OOMinBuffer) { |
| 657 | ASSERT_FALSE( |
| 658 | trace.start(true, false, false, false, -1, 1, ULONG_MAX, all_features)); |
| 659 | { |
| 660 | Opt_trace_object oto(&trace); |
| 661 | { |
| 662 | Opt_trace_array ota(&trace, "one array"); |
| 663 | DBUG_SET("+d,opt_trace_oom_in_buffers"); |
| 664 | for (int i = 0; i < 30; i++) |
| 665 | ota.add_alnum("_______________________________________________"); |
| 666 | DBUG_SET("-d,opt_trace_oom_in_buffers"); |
| 667 | } |
| 668 | } |
| 669 | trace.end(); |
| 670 | Opt_trace_iterator it(&trace); |
| 671 | ASSERT_FALSE(it.at_end()); |
| 672 | Opt_trace_info info; |
| 673 | it.get_value(&info); |
| 674 | EXPECT_EQ(0U, info.missing_bytes); |
| 675 | EXPECT_FALSE(info.missing_priv); |
| 676 | it.next(); |
| 677 | ASSERT_TRUE(it.at_end()); |
| 678 | EXPECT_TRUE(oom); |
| 679 | } |
| 680 | |
| 681 | /// Test reaction to out-of-memory condition in book-keeping data structures |
| 682 | TEST_F(TraceContentTest, OOMinBookKeeping) { |
| 683 | ASSERT_FALSE( |
| 684 | trace.start(true, false, false, false, -1, 1, ULONG_MAX, all_features)); |
| 685 | { |
| 686 | Opt_trace_object oto(&trace); |
| 687 | open_object(100, &trace, true); |
| 688 | } |
| 689 | trace.end(); |
| 690 | Opt_trace_iterator it(&trace); |
| 691 | ASSERT_FALSE(it.at_end()); |
| 692 | Opt_trace_info info; |
| 693 | it.get_value(&info); |
| 694 | EXPECT_EQ(0U, info.missing_bytes); |
| 695 | EXPECT_FALSE(info.missing_priv); |
| 696 | it.next(); |
| 697 | ASSERT_TRUE(it.at_end()); |
| 698 | EXPECT_TRUE(oom); |
| 699 | } |
| 700 | |
| 701 | /// Test reaction to OOM when purging traces |
| 702 | TEST_F(TraceContentTest, OOMinPurge) { |
| 703 | make_one_trace(&trace, "103", -3, 2); |
| 704 | make_one_trace(&trace, "104", -3, 2); |
| 705 | DBUG_SET("+d,opt_trace_oom_in_purge"); |
| 706 | make_one_trace(&trace, "105", -3, 2); |
| 707 | make_one_trace(&trace, "106", -3, 2); |
| 708 | make_one_trace(&trace, "107", -3, 2); |
| 709 | make_one_trace(&trace, "108", -3, 2); |
| 710 | make_one_trace(&trace, "109", -3, 2); |
| 711 | make_one_trace(&trace, "110", -3, 2); |
| 712 | make_one_trace(&trace, "111", -3, 2); |
| 713 | make_one_trace(&trace, "112", -3, 2); |
| 714 | make_one_trace(&trace, "113", -3, 2); |
| 715 | make_one_trace(&trace, "114", -3, 2); |
| 716 | make_one_trace(&trace, "115", -3, 2); |
| 717 | make_one_trace(&trace, "116", -3, 2); |
| 718 | make_one_trace(&trace, "117", -3, 2); |
| 719 | make_one_trace(&trace, "118", -3, 2); |
| 720 | make_one_trace(&trace, "119", -3, 2); |
| 721 | make_one_trace(&trace, "120", -3, 2); |
| 722 | make_one_trace(&trace, "121", -3, 2); |
| 723 | make_one_trace(&trace, "122", -3, 2); // purge first fails here |
| 724 | |
| 725 | DBUG_SET("-d,opt_trace_oom_in_purge"); |
| 726 | // 122 could not purge 119, so we should see 119 and 120 |
| 727 | const char *expected_traces3[] = {"119", "120", nullptr}; |
| 728 | check(trace, expected_traces3); |
| 729 | EXPECT_TRUE(oom); |
| 730 | |
| 731 | // Back to normal: |
| 732 | oom = false; |
| 733 | make_one_trace(&trace, "123", -3, 2); // purge succeeds |
| 734 | const char *expected_traces4[] = {"121", "122", nullptr}; |
| 735 | check(trace, expected_traces4); |
| 736 | EXPECT_FALSE(oom); |
| 737 | } |
| 738 | |
| 739 | #endif // !NDEBUG |
| 740 | |
| 741 | /** Test filtering by feature */ |
| 742 | TEST_F(TraceContentTest, FilteringByFeature) { |
| 743 | ASSERT_FALSE(trace.start(true, false, false, false, -1, 1, ULONG_MAX, |
| 744 | Opt_trace_context::MISC)); |
| 745 | { |
| 746 | Opt_trace_object oto(&trace); |
| 747 | { |
| 748 | Opt_trace_array ota(&trace, "one array"); |
| 749 | ota.add(200.4); |
| 750 | { |
| 751 | Opt_trace_object oto1(&trace, Opt_trace_context::GREEDY_SEARCH); |
| 752 | oto1.add_alnum("one key", "one value").add("another key", 100LL); |
| 753 | Opt_trace_object oto2(&trace, "a fourth key", Opt_trace_context::MISC); |
| 754 | oto2.add("another key inside", 5LL); |
| 755 | } |
| 756 | ota.add(true); |
| 757 | } |
| 758 | { |
| 759 | Opt_trace_object oto3(&trace, "key for oto3", |
| 760 | Opt_trace_context::GREEDY_SEARCH); |
| 761 | oto3.add("etc", 25); |
| 762 | } |
| 763 | oto.add("yet another key", -1000LL); |
| 764 | { |
| 765 | Opt_trace_array ota(&trace, "another array"); |
| 766 | ota.add(1LL).add(2LL).add(3LL).add(4LL); |
| 767 | } |
| 768 | } |
| 769 | trace.end(); |
| 770 | Opt_trace_iterator it(&trace); |
| 771 | ASSERT_FALSE(it.at_end()); |
| 772 | Opt_trace_info info; |
| 773 | it.get_value(&info); |
| 774 | const char expected[] = |
| 775 | "{\n" |
| 776 | " \"one array\": [\n" |
| 777 | " 200.4,\n" |
| 778 | " \"...\",\n" |
| 779 | " true\n" |
| 780 | " ],\n" |
| 781 | " \"key for oto3\": \"...\",\n" |
| 782 | " \"yet another key\": -1000,\n" |
| 783 | " \"another array\": [\n" |
| 784 | " 1,\n" |
| 785 | " 2,\n" |
| 786 | " 3,\n" |
| 787 | " 4\n" |
| 788 | " ]\n" |
| 789 | "}"; |
| 790 | EXPECT_STREQ(expected, info.trace_ptr); |
| 791 | EXPECT_EQ(sizeof(expected) - 1, info.trace_length); |
| 792 | check_json_compliance(info.trace_ptr, info.trace_length); |
| 793 | EXPECT_EQ(0U, info.missing_bytes); |
| 794 | EXPECT_FALSE(info.missing_priv); |
| 795 | EXPECT_FALSE(oom); |
| 796 | it.next(); |
| 797 | ASSERT_TRUE(it.at_end()); |
| 798 | } |
| 799 | |
| 800 | /** Test escaping of characters */ |
| 801 | TEST_F(TraceContentTest, Escaping) { |
| 802 | ASSERT_FALSE( |
| 803 | trace.start(true, false, true, false, -1, 1, ULONG_MAX, all_features)); |
| 804 | // All ASCII 0-127 chars are valid UTF8 encodings |
| 805 | char all_chars[130]; |
| 806 | for (uint c = 0; c < sizeof(all_chars) - 2; c++) all_chars[c] = c; |
| 807 | // Now a character with a two-byte code in utf8: ä |
| 808 | all_chars[128] = static_cast<char>(0xc3); |
| 809 | all_chars[129] = static_cast<char>(0xa4); |
| 810 | // all_chars is used both as query... |
| 811 | trace.set_query(all_chars, sizeof(all_chars), system_charset_info); |
| 812 | { |
| 813 | Opt_trace_object oto(&trace); |
| 814 | // ... and inside the trace: |
| 815 | oto.add_utf8("somekey", all_chars, sizeof(all_chars)); |
| 816 | } |
| 817 | trace.end(); |
| 818 | Opt_trace_iterator it(&trace); |
| 819 | ASSERT_FALSE(it.at_end()); |
| 820 | Opt_trace_info info; |
| 821 | it.get_value(&info); |
| 822 | // we get the trace escaped, JSON-compliant: |
| 823 | const char expected[] = |
| 824 | "{\n" |
| 825 | " \"somekey\": " |
| 826 | "\"\\u0000\\u0001\\u0002\\u0003\\u0004\\u0005\\u0006\\u0007\\u0008\\t\\n" |
| 827 | "\\u000b\\u000c\\r\\u000e\\u000f\\u0010\\u0011\\u0012\\u0013\\u0014\\u001" |
| 828 | "5\\u0016\\u0017\\u0018\\u0019\\u001a\\u001b\\u001c\\u001d\\u001e\\u001f " |
| 829 | "!\\\"#$%&'()*+,-./" |
| 830 | "0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\\\]^_`" |
| 831 | "abcdefghijklmnopqrstuvwxyz{|}~ä\"\n" |
| 832 | "}"; |
| 833 | EXPECT_STREQ(expected, info.trace_ptr); |
| 834 | EXPECT_EQ(sizeof(expected) - 1, info.trace_length); |
| 835 | check_json_compliance(info.trace_ptr, info.trace_length); |
| 836 | EXPECT_EQ(0U, info.missing_bytes); |
| 837 | EXPECT_FALSE(info.missing_priv); |
| 838 | EXPECT_FALSE(oom); |
| 839 | EXPECT_EQ(sizeof(all_chars), info.query_length); |
| 840 | // we get the query unescaped, verbatim, not 0-terminated: |
| 841 | EXPECT_EQ(0, memcmp(all_chars, info.query_ptr, sizeof(all_chars))); |
| 842 | EXPECT_EQ(system_charset_info, info.query_charset); |
| 843 | it.next(); |
| 844 | ASSERT_TRUE(it.at_end()); |
| 845 | } |
| 846 | |
| 847 | /** Test how the system handles non-UTF8 characters, a violation of its API */ |
| 848 | TEST_F(TraceContentTest, NonUtf8) { |
| 849 | ASSERT_FALSE( |
| 850 | trace.start(true, false, true, false, -1, 1, ULONG_MAX, all_features)); |
| 851 | /* |
| 852 | A string which starts with invalid utf8 (the four first bytes are éèÄà in |
| 853 | latin1). |
| 854 | In utf8, the following holds |
| 855 | - E0->EF can only be the start of a 3-byte sequence |
| 856 | - C2->DF 2-byte |
| 857 | - ASCII a single-byte sequence |
| 858 | */ |
| 859 | const char all_chars[] = |
| 860 | "\xe9\xe8\xc4\xe0" |
| 861 | "ABC"; |
| 862 | // We declare a query in latin1 |
| 863 | trace.set_query(all_chars, sizeof(all_chars), &my_charset_latin1); |
| 864 | { |
| 865 | Opt_trace_object oto(&trace); |
| 866 | /* |
| 867 | We pass the non-utf8-compliant string to add_utf8() (violating the |
| 868 | API). We get it back unchanged. The trace system could try to be robust, |
| 869 | detecting and sanitizing wrong characters (replacing them with '?'); but |
| 870 | it does not bother, as the MySQL Server normally does not violate the |
| 871 | API. |
| 872 | */ |
| 873 | oto.add_utf8("somekey", all_chars, sizeof(all_chars) - 1); |
| 874 | } |
| 875 | trace.end(); |
| 876 | Opt_trace_iterator it(&trace); |
| 877 | ASSERT_FALSE(it.at_end()); |
| 878 | Opt_trace_info info; |
| 879 | it.get_value(&info); |
| 880 | // This is not UTF8-compliant and thus not JSON-compliant. |
| 881 | const char expected[] = |
| 882 | "{\n" |
| 883 | " \"somekey\": \"" |
| 884 | "\xe9\xe8\xc4\xe0" |
| 885 | "ABC\"\n" |
| 886 | "}"; |
| 887 | EXPECT_STREQ(expected, info.trace_ptr); |
| 888 | EXPECT_EQ(sizeof(expected) - 1, info.trace_length); |
| 889 | EXPECT_EQ(0U, info.missing_bytes); |
| 890 | EXPECT_FALSE(info.missing_priv); |
| 891 | EXPECT_FALSE(oom); |
| 892 | EXPECT_EQ(sizeof(all_chars), info.query_length); |
| 893 | // we get the query unescaped, verbatim, not 0-terminated: |
| 894 | EXPECT_EQ(0, memcmp(all_chars, info.query_ptr, sizeof(all_chars))); |
| 895 | it.next(); |
| 896 | ASSERT_TRUE(it.at_end()); |
| 897 | } |
| 898 | |
| 899 | /** |
| 900 | Test indentation by many blanks. |
| 901 | By creating a 100-level deep structure, we force an indentation which |
| 902 | enters the while() block in Opt_trace_stmt::next_line(). |
| 903 | */ |
| 904 | TEST_F(TraceContentTest, Indent) { |
| 905 | ASSERT_FALSE( |
| 906 | trace.start(true, false, false, false, -1, 1, ULONG_MAX, all_features)); |
| 907 | { |
| 908 | Opt_trace_object oto(&trace); |
| 909 | open_object(99, &trace, false); |
| 910 | } |
| 911 | trace.end(); |
| 912 | Opt_trace_iterator it(&trace); |
| 913 | ASSERT_FALSE(it.at_end()); |
| 914 | Opt_trace_info info; |
| 915 | it.get_value(&info); |
| 916 | /* |
| 917 | Formula for the expected size. |
| 918 | Before the Nth call to open_object(), indentation inside the innermost |
| 919 | empty object is noted I(N); so the relationship between the size before |
| 920 | Nth call and the size after Nth call is: |
| 921 | S(N+1) = S(N) |
| 922 | + I(N) (indentation before added '"xxx": {\n' ) |
| 923 | + 9 (length of added '"xxx": {\n' ) |
| 924 | + I(N) (indentation before added '}\n' ) |
| 925 | + 2 (length of added '}\n' ) |
| 926 | and the indentation is increased by two as we are one level deeper: |
| 927 | I(N+1) = I(N) + 2 |
| 928 | With S(1) = 3 (length of '{\n}') and I(1) = 2. |
| 929 | So I(N) = 2 * N and |
| 930 | S(N+1) - S(N) = 11 + 4 * N |
| 931 | So S(N) = 3 + 11 * (N - 1) + 2 * N * (N - 1). |
| 932 | For 99 calls, the final size is S(100) = 20892. |
| 933 | Each call adds 10 non-space characters, so there should be |
| 934 | 20892 |
| 935 | - 10 * 99 (added non-spaces characters) |
| 936 | - 3 (non-spaces of initial object before first function call) |
| 937 | = 19899 spaces. |
| 938 | */ |
| 939 | EXPECT_EQ(20892U, info.trace_length); |
| 940 | uint spaces = 0; |
| 941 | for (uint i = 0; i < info.trace_length; i++) |
| 942 | if (info.trace_ptr[i] == ' ') spaces++; |
| 943 | EXPECT_EQ(19899U, spaces); |
| 944 | check_json_compliance(info.trace_ptr, info.trace_length); |
| 945 | EXPECT_EQ(0U, info.missing_bytes); |
| 946 | EXPECT_FALSE(info.missing_priv); |
| 947 | EXPECT_FALSE(oom); |
| 948 | it.next(); |
| 949 | ASSERT_TRUE(it.at_end()); |
| 950 | } |
| 951 | |
| 952 | /** Test Opt_trace_context::missing_privilege() */ |
| 953 | TEST_F(TraceContentTest, MissingPrivilege) { |
| 954 | ASSERT_FALSE( |
| 955 | trace.start(true, false, true, false, 0, 100, ULONG_MAX, all_features)); |
| 956 | { |
| 957 | Opt_trace_object oto(&trace); |
| 958 | { |
| 959 | Opt_trace_array ota(&trace, "one array"); |
| 960 | ota.add(200.4); |
| 961 | { |
| 962 | Opt_trace_object oto1(&trace); |
| 963 | oto1.add_alnum("one key", "one value").add("another key", 100LL); |
| 964 | oto1.add("a third key", false); |
| 965 | Opt_trace_object oto2(&trace, "a fourth key"); |
| 966 | oto2.add("key inside", 1LL); |
| 967 | ASSERT_FALSE(trace.start(true, false, true, false, 0, 100, ULONG_MAX, |
| 968 | all_features)); |
| 969 | { |
| 970 | Opt_trace_object oto3(&trace); |
| 971 | trace.missing_privilege(); |
| 972 | ASSERT_FALSE(trace.start(true, false, true, false, 0, 100, ULONG_MAX, |
| 973 | all_features)); |
| 974 | { |
| 975 | Opt_trace_object oto4(&trace); |
| 976 | oto4.add_alnum("in4", "key4"); |
| 977 | } |
| 978 | trace.end(); |
| 979 | } |
| 980 | trace.end(); // this should restore I_S support |
| 981 | // so this should be visible |
| 982 | oto2.add("another key inside", 5LL); |
| 983 | // and this new sub statement too: |
| 984 | ASSERT_FALSE(trace.start(true, false, true, false, 0, 100, ULONG_MAX, |
| 985 | all_features)); |
| 986 | { |
| 987 | Opt_trace_object oto5(&trace); |
| 988 | oto5.add("in5", true); |
| 989 | } |
| 990 | trace.end(); |
| 991 | } |
| 992 | ota.add_alnum("one string element"); |
| 993 | ota.add(true); |
| 994 | } |
| 995 | oto.add("yet another key", -1000LL); |
| 996 | { |
| 997 | Opt_trace_array ota(&trace, "another array"); |
| 998 | ota.add(1LL).add(2LL).add(3LL).add(4LL); |
| 999 | } |
| 1000 | } |
| 1001 | trace.end(); |
| 1002 | Opt_trace_iterator it(&trace); |
| 1003 | ASSERT_FALSE(it.at_end()); |
| 1004 | Opt_trace_info info; |
| 1005 | it.get_value(&info); |
| 1006 | const char expected[] = |
| 1007 | "{\n" |
| 1008 | " \"one array\": [\n" |
| 1009 | " 200.4,\n" |
| 1010 | " {\n" |
| 1011 | " \"one key\": \"one value\",\n" |
| 1012 | " \"another key\": 100,\n" |
| 1013 | " \"a third key\": false,\n" |
| 1014 | " \"a fourth key\": {\n" |
| 1015 | " \"key inside\": 1,\n" |
| 1016 | " \"another key inside\": 5\n" |
| 1017 | " } /* a fourth key */\n" |
| 1018 | " },\n" |
| 1019 | " \"one string element\",\n" |
| 1020 | " true\n" |
| 1021 | " ] /* one array */,\n" |
| 1022 | " \"yet another key\": -1000,\n" |
| 1023 | " \"another array\": [\n" |
| 1024 | " 1,\n" |
| 1025 | " 2,\n" |
| 1026 | " 3,\n" |
| 1027 | " 4\n" |
| 1028 | " ] /* another array */\n" |
| 1029 | "}"; |
| 1030 | EXPECT_STREQ(expected, info.trace_ptr); |
| 1031 | EXPECT_EQ(sizeof(expected) - 1, info.trace_length); |
| 1032 | check_json_compliance(info.trace_ptr, info.trace_length); |
| 1033 | EXPECT_EQ(0U, info.missing_bytes); |
| 1034 | EXPECT_FALSE(info.missing_priv); |
| 1035 | EXPECT_FALSE(oom); |
| 1036 | it.next(); |
| 1037 | ASSERT_FALSE(it.at_end()); |
| 1038 | // Now the substatement with a missing privilege |
| 1039 | it.get_value(&info); |
| 1040 | const char expected2[] = ""; // because of missing privilege... |
| 1041 | EXPECT_STREQ(expected2, info.trace_ptr); |
| 1042 | EXPECT_EQ(sizeof(expected2) - 1, info.trace_length); |
| 1043 | EXPECT_EQ(0U, info.missing_bytes); |
| 1044 | EXPECT_TRUE(info.missing_priv); // ... tested here. |
| 1045 | it.next(); |
| 1046 | ASSERT_FALSE(it.at_end()); |
| 1047 | // And now the last substatement, visible |
| 1048 | it.get_value(&info); |
| 1049 | const char expected3[] = |
| 1050 | "{\n" |
| 1051 | " \"in5\": true\n" |
| 1052 | "}"; |
| 1053 | EXPECT_STREQ(expected3, info.trace_ptr); |
| 1054 | EXPECT_EQ(sizeof(expected3) - 1, info.trace_length); |
| 1055 | check_json_compliance(info.trace_ptr, info.trace_length); |
| 1056 | EXPECT_EQ(0U, info.missing_bytes); |
| 1057 | EXPECT_FALSE(info.missing_priv); |
| 1058 | it.next(); |
| 1059 | ASSERT_TRUE(it.at_end()); |
| 1060 | } |
| 1061 | |
| 1062 | /** Test Opt_trace_context::missing_privilege() on absent trace */ |
| 1063 | TEST_F(TraceContentTest, MissingPrivilege2) { |
| 1064 | /* |
| 1065 | Ask for neither I_S not debug output, and no |
| 1066 | missing_privilege() support |
| 1067 | */ |
| 1068 | ASSERT_FALSE( |
| 1069 | trace.start(false, false, true, false, 0, 100, ULONG_MAX, all_features)); |
| 1070 | EXPECT_FALSE(trace.is_started()); |
| 1071 | trace.end(); |
| 1072 | /* |
| 1073 | Ask for neither I_S not debug output, but ask that |
| 1074 | missing_privilege() is supported. |
| 1075 | */ |
| 1076 | ASSERT_FALSE( |
| 1077 | trace.start(false, true, true, false, 0, 100, ULONG_MAX, all_features)); |
| 1078 | EXPECT_TRUE(trace.is_started()); |
| 1079 | trace.missing_privilege(); |
| 1080 | // This above should make the substatement below not be traced: |
| 1081 | ASSERT_FALSE( |
| 1082 | trace.start(true, false, true, false, 0, 100, ULONG_MAX, all_features)); |
| 1083 | { |
| 1084 | Opt_trace_object oto5(&trace); |
| 1085 | oto5.add("in5", true); |
| 1086 | } |
| 1087 | trace.end(); |
| 1088 | trace.end(); |
| 1089 | Opt_trace_iterator it(&trace); |
| 1090 | ASSERT_TRUE(it.at_end()); |
| 1091 | } |
| 1092 | |
| 1093 | /** |
| 1094 | Test an optimization: that no Opt_trace_stmt is created in common case |
| 1095 | where all statements and substatements ask neither for I_S nor for DBUG, |
| 1096 | nor for support of missing_privilege() function. |
| 1097 | */ |
| 1098 | TEST_F(TraceContentTest, NoOptTraceStmt) { |
| 1099 | ASSERT_FALSE( |
| 1100 | trace.start(false, false, false, false, -1, 1, ULONG_MAX, all_features)); |
| 1101 | EXPECT_FALSE(trace.is_started()); |
| 1102 | // one substatement: |
| 1103 | ASSERT_FALSE( |
| 1104 | trace.start(false, false, false, false, -1, 1, ULONG_MAX, all_features)); |
| 1105 | EXPECT_FALSE(trace.is_started()); |
| 1106 | // another one deeper nested: |
| 1107 | ASSERT_FALSE( |
| 1108 | trace.start(false, false, false, false, -1, 1, ULONG_MAX, all_features)); |
| 1109 | EXPECT_FALSE(trace.is_started()); |
| 1110 | trace.end(); |
| 1111 | trace.end(); |
| 1112 | trace.end(); |
| 1113 | } |
| 1114 | |
| 1115 | TEST_F(TraceContentTest, DoubleValues) { |
| 1116 | ASSERT_FALSE( |
| 1117 | trace.start(true, false, false, false, -1, 1, ULONG_MAX, all_features)); |
| 1118 | { |
| 1119 | Opt_trace_object oto(&trace); |
| 1120 | oto.add("max", std::numeric_limits<double>::max()); |
| 1121 | oto.add("lowest", std::numeric_limits<double>::lowest()); |
| 1122 | oto.add("min", std::numeric_limits<double>::min()); |
| 1123 | oto.add("pi", M_PI); |
| 1124 | oto.add("e", M_E); |
| 1125 | oto.add("zero", 0); |
| 1126 | oto.add("one", 1); |
| 1127 | oto.add("0.123456", 0.123456); |
| 1128 | oto.add("-0.123456", -0.123456); |
| 1129 | oto.add("1.123456", 1.123456); |
| 1130 | oto.add("-1.123456", -1.123456); |
| 1131 | oto.add("12.123456", 12.123456); |
| 1132 | oto.add("-12.123456", -12.123456); |
| 1133 | oto.add("123.123456", 123.123456); |
| 1134 | oto.add("-123.123456", -123.123456); |
| 1135 | oto.add("1234.123456", 1234.123456); |
| 1136 | oto.add("-1234.123456", -1234.123456); |
| 1137 | oto.add("12345.123456", 12345.123456); |
| 1138 | oto.add("-12345.123456", -12345.123456); |
| 1139 | oto.add("123456.123456", 123456.123456); |
| 1140 | oto.add("-123456.123456", -123456.123456); |
| 1141 | oto.add("1234567.123456", 1234567.123456); |
| 1142 | oto.add("-1234567.123456", -1234567.123456); |
| 1143 | oto.add("12345678.123456", 12345678.123456); |
| 1144 | oto.add("-12345678.123456", -12345678.123456); |
| 1145 | oto.add("123456789.123456", 123456789.123456); |
| 1146 | oto.add("-123456789.123456", -123456789.123456); |
| 1147 | } |
| 1148 | trace.end(); |
| 1149 | |
| 1150 | Opt_trace_iterator it(&trace); |
| 1151 | ASSERT_FALSE(it.at_end()); |
| 1152 | Opt_trace_info info; |
| 1153 | it.get_value(&info); |
| 1154 | check_json_compliance(info.trace_ptr, info.trace_length); |
| 1155 | const char expected[] = R"({ |
| 1156 | "max": 1.79769e+308, |
| 1157 | "lowest": -1.79769e+308, |
| 1158 | "min": 2.22507e-308, |
| 1159 | "pi": 3.14159, |
| 1160 | "e": 2.71828, |
| 1161 | "zero": 0, |
| 1162 | "one": 1, |
| 1163 | "0.123456": 0.123456, |
| 1164 | "-0.123456": -0.123456, |
| 1165 | "1.123456": 1.12346, |
| 1166 | "-1.123456": -1.12346, |
| 1167 | "12.123456": 12.1235, |
| 1168 | "-12.123456": -12.1235, |
| 1169 | "123.123456": 123.123, |
| 1170 | "-123.123456": -123.123, |
| 1171 | "1234.123456": 1234.12, |
| 1172 | "-1234.123456": -1234.12, |
| 1173 | "12345.123456": 12345.1, |
| 1174 | "-12345.123456": -12345.1, |
| 1175 | "123456.123456": 123456, |
| 1176 | "-123456.123456": -123456, |
| 1177 | "1234567.123456": 1.23457e+06, |
| 1178 | "-1234567.123456": -1.23457e+06, |
| 1179 | "12345678.123456": 1.23457e+07, |
| 1180 | "-12345678.123456": -1.23457e+07, |
| 1181 | "123456789.123456": 1.23457e+08, |
| 1182 | "-123456789.123456": -1.23457e+08 |
| 1183 | })"; |
| 1184 | EXPECT_STREQ(expected, info.trace_ptr); |
| 1185 | EXPECT_EQ(sizeof(expected) - 1, info.trace_length); |
| 1186 | EXPECT_EQ(0, info.missing_bytes); |
| 1187 | EXPECT_FALSE(info.missing_priv); |
| 1188 | EXPECT_FALSE(oom); |
| 1189 | it.next(); |
| 1190 | ASSERT_TRUE(it.at_end()); |
| 1191 | } |
| 1192 | |
| 1193 | } // namespace opt_trace_unittest |