| 1 | /* Copyright (c) 2022, 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 | #include <array> |
| 26 | |
| 27 | #include <gtest/gtest.h> |
| 28 | #include "libbinlogevents/include/binary_log.h" |
| 29 | |
| 30 | #include <memory> |
| 31 | #include "sql/binlog_ostream.h" |
| 32 | #include "sql/current_thd.h" |
| 33 | #include "sql/sql_class.h" |
| 34 | #include "unittest/gunit/test_utils.h" |
| 35 | |
| 36 | #define private public |
| 37 | #include "sql/log_event.h" |
| 38 | #undef private |
| 39 | |
| 40 | namespace binary_log::unittests { |
| 41 | |
| 42 | using namespace std; |
| 43 | |
| 44 | class LogEventStatusSizeTest : public ::testing::Test { |
| 45 | protected: |
| 46 | LogEventStatusSizeTest() = default; |
| 47 | |
| 48 | void SetUp() override {} |
| 49 | |
| 50 | void TearDown() override {} |
| 51 | |
| 52 | static std::string fill_str(size_t str_len, char pattern) { |
| 53 | std::string tmp; |
| 54 | for (size_t id = 0; id < str_len; ++id) { |
| 55 | tmp.push_back(pattern); |
| 56 | } |
| 57 | return tmp; |
| 58 | } |
| 59 | |
| 60 | static std::string fill_str(size_t str_len) { return fill_str(str_len, 'a'); } |
| 61 | |
| 62 | static void test_query_log_event_max_status_size() { |
| 63 | my_testing::Server_initializer srv; |
| 64 | srv.SetUp(); |
| 65 | auto test_string_namelen = fill_str(NAME_LEN); |
| 66 | auto test_string_zone = fill_str(MAX_TIME_ZONE_NAME_LENGTH); |
| 67 | |
| 68 | ASSERT_EQ(test_string_namelen.length(), NAME_LEN); |
| 69 | |
| 70 | std::string query = "INSERT INTO t VALUES(1)"; |
| 71 | |
| 72 | bool using_trans = false; |
| 73 | bool immediate = false; |
| 74 | bool suppress_use = false; |
| 75 | int error = 0; |
| 76 | bool ignore_command = false; |
| 77 | |
| 78 | Query_log_event qe(srv.thd(), query.c_str(), query.length(), using_trans, |
| 79 | immediate, suppress_use, error, ignore_command); |
| 80 | |
| 81 | Binlog_cache_storage os; |
| 82 | os.open(50000, 90000); // random values, bigger than maximal packet size |
| 83 | |
| 84 | // set qe values to simulate maximal size of the status variables |
| 85 | // artificial data |
| 86 | qe.flags2_inited = 1; |
| 87 | qe.sql_mode_inited = 1; |
| 88 | auto test_catalog_string = fill_str(255); |
| 89 | qe.catalog_len = test_catalog_string.length(); |
| 90 | qe.catalog = test_catalog_string.c_str(); |
| 91 | qe.auto_increment_increment = 0; |
| 92 | qe.charset_inited = 1; |
| 93 | qe.time_zone_len = test_string_zone.length(); |
| 94 | qe.time_zone_str = test_string_zone.c_str(); |
| 95 | qe.lc_time_names_number = 1; |
| 96 | qe.charset_database_number = 1; |
| 97 | qe.table_map_for_update = 1; |
| 98 | qe.thd->binlog_need_explicit_defaults_ts = 1; |
| 99 | qe.thd->slave_thread = true; |
| 100 | string user = fill_str(32 * 3); |
| 101 | string host = fill_str(255); |
| 102 | LEX_STRING lexStrUser; |
| 103 | lexStrUser.str = &(user[0]); |
| 104 | lexStrUser.length = user.length(); |
| 105 | LEX_STRING lexStrHost; |
| 106 | lexStrHost.str = &(host[0]); |
| 107 | lexStrHost.length = host.length(); |
| 108 | |
| 109 | qe.thd->set_invoker(&lexStrUser, &lexStrHost); |
| 110 | qe.thd->binlog_invoker(); // sets true |
| 111 | qe.thd->query_start_usec_used = true; |
| 112 | qe.thd->binlog_need_explicit_defaults_ts = true; |
| 113 | qe.ddl_xid = 1; |
| 114 | qe.need_sql_require_primary_key = 1; |
| 115 | qe.needs_default_table_encryption = 1; |
| 116 | qe.default_collation_for_utf8mb4_number = 1; |
| 117 | |
| 118 | for (size_t did = 0; did < MAX_DBS_IN_EVENT_MTS; ++did) { |
| 119 | std::string db = fill_str(NAME_LEN, 'a' + did); |
| 120 | qe.thd->add_to_binlog_accessed_dbs(db.c_str()); |
| 121 | } |
| 122 | // sanity check after filling accessed dbs, db names must be unique |
| 123 | ASSERT_EQ(qe.thd->get_binlog_accessed_db_names()->elements, |
| 124 | MAX_DBS_IN_EVENT_MTS); |
| 125 | |
| 126 | qe.write(&os); |
| 127 | |
| 128 | // make sure that the length of status variables is equal to |
| 129 | // MAX_SIZE_LOG_EVENT_STATUS |
| 130 | ASSERT_EQ(MAX_SIZE_LOG_EVENT_STATUS, qe.status_vars_len); |
| 131 | |
| 132 | srv.TearDown(); |
| 133 | } |
| 134 | }; |
| 135 | |
| 136 | TEST_F(LogEventStatusSizeTest, LogEventBoundaryConditions) { |
| 137 | LogEventStatusSizeTest::test_query_log_event_max_status_size(); |
| 138 | } |
| 139 | |
| 140 | } // namespace binary_log::unittests |