| 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 | #include "unittest/gunit/test_utils.h" |
| 25 | |
| 26 | #include <assert.h> |
| 27 | #include <gtest/gtest.h> |
| 28 | #include <atomic> |
| 29 | #include <new> |
| 30 | #include <ostream> |
| 31 | #include <string> |
| 32 | |
| 33 | #include "gtest/gtest-message.h" |
| 34 | #include "m_ctype.h" |
| 35 | #include "m_string.h" |
| 36 | // assert |
| 37 | #include "my_inttypes.h" |
| 38 | #include "mysql_com.h" |
| 39 | #include "sql/binlog.h" |
| 40 | #include "sql/client_settings.h" |
| 41 | #include "sql/conn_handler/connection_handler_manager.h" |
| 42 | #include "sql/dd/dd.h" |
| 43 | #include "sql/dd/impl/dictionary_impl.h" // dd::Dictionary_impl |
| 44 | #include "sql/dd/impl/tables/column_type_elements.h" |
| 45 | #include "sql/dd/impl/tables/schemata.h" |
| 46 | #include "sql/dd/impl/tables/tables.h" |
| 47 | #include "sql/derror.h" |
| 48 | #include "sql/item_func.h" |
| 49 | #include "sql/keycaches.h" |
| 50 | #include "sql/log.h" // query_logger |
| 51 | #include "sql/my_decimal.h" |
| 52 | #include "sql/mysqld.h" // set_remaining_args |
| 53 | #include "sql/mysqld_thd_manager.h" |
| 54 | #include "sql/opt_costconstantcache.h" // optimizer cost constant cache |
| 55 | #include "sql/range_optimizer/range_optimizer.h" |
| 56 | #include "sql/rpl_filter.h" |
| 57 | #include "sql/rpl_handler.h" // delegates_init() |
| 58 | #include "sql/set_var.h" |
| 59 | #include "sql/sql_class.h" |
| 60 | #include "sql/sql_lex.h" |
| 61 | #include "sql/sql_locale.h" |
| 62 | #include "sql/sql_plugin.h" |
| 63 | #include "sql/xa.h" |
| 64 | #include "sql/xa/transaction_cache.h" // xa::Transaction_cache |
| 65 | #include "unicode/uclean.h" |
| 66 | #include "unittest/gunit/fake_table.h" |
| 67 | |
| 68 | void my_server_abort(); |
| 69 | |
| 70 | namespace my_testing { |
| 71 | |
| 72 | int chars_2_decimal(const char *chars, my_decimal *to) { |
| 73 | const char *end = strend(chars); |
| 74 | return string2decimal(chars, to, &end); |
| 75 | } |
| 76 | |
| 77 | /* |
| 78 | A mock error handler for error_handler_hook. |
| 79 | */ |
| 80 | std::atomic<uint> expected_error{0}; |
| 81 | extern "C" void test_error_handler_hook(uint err, const char *str, myf) { |
| 82 | EXPECT_EQ(expected_error.load(), err) << str; |
| 83 | } |
| 84 | |
| 85 | void setup_server_for_unit_tests() { |
| 86 | std::string my_name(my_progname); |
| 87 | char *argv[] = {const_cast<char *>(my_name.c_str()), |
| 88 | const_cast<char *>("--secure-file-priv=NULL"), |
| 89 | const_cast<char *>("--log_syslog=0"), |
| 90 | const_cast<char *>("--explicit_defaults_for_timestamp"), |
| 91 | const_cast<char *>("--datadir=" DATA_DIR), |
| 92 | const_cast<char *>("--lc-messages-dir=" ERRMSG_DIR), |
| 93 | nullptr}; |
| 94 | set_remaining_args(6, argv); |
| 95 | system_charset_info = &my_charset_utf8mb3_general_ci; |
| 96 | |
| 97 | mysql_mutex_init(PSI_NOT_INSTRUMENTED, &LOCK_plugin, MY_MUTEX_INIT_FAST); |
| 98 | sys_var_init(); |
| 99 | init_common_variables(); |
| 100 | test_flags |= TEST_SIGINT; |
| 101 | test_flags |= TEST_NO_TEMP_TABLES; |
| 102 | test_flags &= ~TEST_CORE_ON_SIGNAL; |
| 103 | my_init_signals(); |
| 104 | // Install server's abort handler to better represent server environment. |
| 105 | set_my_abort(my_server_abort); |
| 106 | randominit(&sql_rand, 0, 0); |
| 107 | xa::Transaction_cache::initialize(); |
| 108 | delegates_init(); |
| 109 | gtid_server_init(); |
| 110 | error_handler_hook = test_error_handler_hook; |
| 111 | // Initialize Query_logger last, to avoid spurious warnings to stderr. |
| 112 | query_logger.init(); |
| 113 | init_optimizer_cost_module(false); |
| 114 | DD_initializer::SetUp(); |
| 115 | } |
| 116 | |
| 117 | void teardown_server_for_unit_tests() { |
| 118 | mysql_bin_log.cleanup(); |
| 119 | range_optimizer_free(); |
| 120 | sys_var_end(); |
| 121 | delegates_destroy(); |
| 122 | xa::Transaction_cache::dispose(); |
| 123 | gtid_server_cleanup(); |
| 124 | query_logger.cleanup(); |
| 125 | item_func_sleep_free(); |
| 126 | item_create_cleanup(); |
| 127 | delete_optimizer_cost_module(); |
| 128 | DD_initializer::TearDown(); |
| 129 | delete &dd::tables::Column_type_elements::instance(); |
| 130 | delete &dd::tables::Schemata::instance(); |
| 131 | delete &dd::tables::Tables::instance(); |
| 132 | key_caches.delete_elements(); |
| 133 | free_tmpdir(&mysql_tmpdir_list); |
| 134 | delete binlog_filter; |
| 135 | u_cleanup(); |
| 136 | cleanup_errmsgs(); |
| 137 | Connection_handler_manager::destroy_instance(); |
| 138 | mysql_client_plugin_deinit(); |
| 139 | deinit_errmessage(); // finish server errs |
| 140 | Global_THD_manager::destroy_instance(); |
| 141 | my_end(0); |
| 142 | clean_up_mysqld_mutexes(); |
| 143 | // Restore standard's abort. |
| 144 | set_my_abort(abort); |
| 145 | } |
| 146 | |
| 147 | void Server_initializer::set_expected_error(uint val) { expected_error = val; } |
| 148 | |
| 149 | void Server_initializer::SetUp() { |
| 150 | expected_error = 0; |
| 151 | m_thd = new THD(false); |
| 152 | THD *stack_thd = m_thd; |
| 153 | |
| 154 | m_thd->set_new_thread_id(); |
| 155 | |
| 156 | m_thd->thread_stack = (char *)&stack_thd; |
| 157 | m_thd->store_globals(); |
| 158 | lex_start(m_thd); |
| 159 | Fake_TABLE::reset_highest_table_id(); |
| 160 | } |
| 161 | |
| 162 | void Server_initializer::TearDown() { |
| 163 | if (m_thd != nullptr) { |
| 164 | m_thd->cleanup_after_query(); |
| 165 | delete m_thd; |
| 166 | m_thd = nullptr; |
| 167 | } |
| 168 | } |
| 169 | |
| 170 | Mock_error_handler::Mock_error_handler(THD *thd, uint expected_error) |
| 171 | : m_thd(thd), m_expected_error(expected_error), m_handle_called(0) { |
| 172 | thd->push_internal_handler(this); |
| 173 | } |
| 174 | |
| 175 | Mock_error_handler::~Mock_error_handler() { |
| 176 | // Strange Visual Studio bug: have to store 'this' in local variable. |
| 177 | Internal_error_handler *me = this; |
| 178 | EXPECT_EQ(me, m_thd->pop_internal_handler()); |
| 179 | if (m_expected_error == 0) { |
| 180 | EXPECT_EQ(0, m_handle_called); |
| 181 | } else { |
| 182 | EXPECT_GT(m_handle_called, 0) |
| 183 | << "Error " << m_expected_error << " expected."; |
| 184 | } |
| 185 | } |
| 186 | |
| 187 | bool Mock_error_handler::handle_condition(THD *, uint sql_errno, const char *, |
| 188 | Sql_condition::enum_severity_level *, |
| 189 | const char *) { |
| 190 | EXPECT_EQ(m_expected_error, sql_errno); |
| 191 | ++m_handle_called; |
| 192 | return true; |
| 193 | } |
| 194 | |
| 195 | void DD_initializer::SetUp() { |
| 196 | /* |
| 197 | With WL#6599, Query_block::add_table_to_list() will invoke |
| 198 | dd::Dictionary::is_system_view_name() method. E.g., the unit |
| 199 | test InsertDelayed would invoke above API. This requires us |
| 200 | to have a instance of dictionary_impl. We do not really need |
| 201 | to initialize dd::System_views for this test. Also, there can |
| 202 | be future test cases that need the same. |
| 203 | */ |
| 204 | dd::Dictionary_impl::s_instance = new (std::nothrow) dd::Dictionary_impl(); |
| 205 | assert(dd::Dictionary_impl::s_instance != nullptr); |
| 206 | } |
| 207 | |
| 208 | void DD_initializer::TearDown() { |
| 209 | assert(dd::Dictionary_impl::s_instance != nullptr); |
| 210 | delete dd::Dictionary_impl::s_instance; |
| 211 | } |
| 212 | |
| 213 | } // namespace my_testing |