| 1 | /* Copyright (c) 2016, 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 "gcs_base_test.h" |
| 25 | #include "mysql/gcs/gcs_logging_system.h" |
| 26 | |
| 27 | namespace gcs_logging_unittest { |
| 28 | class Mock_Logger : public Logger_interface { |
| 29 | public: |
| 30 | Mock_Logger() { |
| 31 | ON_CALL(*this, initialize()).WillByDefault(Return(GCS_OK)); |
| 32 | ON_CALL(*this, finalize()).WillByDefault(Return(GCS_OK)); |
| 33 | } |
| 34 | |
| 35 | ~Mock_Logger() = default; |
| 36 | MOCK_METHOD0(initialize, enum_gcs_error()); |
| 37 | MOCK_METHOD0(finalize, enum_gcs_error()); |
| 38 | MOCK_METHOD2(log_event, void(const gcs_log_level_t, const std::string &)); |
| 39 | }; |
| 40 | |
| 41 | class LoggingInfrastructureTest : public GcsBaseTestNoLogging { |
| 42 | protected: |
| 43 | LoggingInfrastructureTest() : logger(nullptr) {} |
| 44 | |
| 45 | void SetUp() override { logger = new Mock_Logger(); } |
| 46 | |
| 47 | void TearDown() override { |
| 48 | Gcs_log_manager::finalize(); |
| 49 | delete logger; |
| 50 | logger = nullptr; |
| 51 | } |
| 52 | |
| 53 | Mock_Logger *logger; |
| 54 | }; |
| 55 | |
| 56 | TEST_F(LoggingInfrastructureTest, InjectedMockLoggerTest) { |
| 57 | EXPECT_CALL(*logger, initialize()).Times(1); |
| 58 | EXPECT_CALL(*logger, log_event(_, _)).Times(4); |
| 59 | |
| 60 | Gcs_log_manager::initialize(logger); |
| 61 | |
| 62 | // Logger 1 initialized |
| 63 | ASSERT_EQ(true, Gcs_log_manager::get_logger() != nullptr); |
| 64 | ASSERT_EQ(logger, Gcs_log_manager::get_logger()); |
| 65 | |
| 66 | // Log some messages on logger |
| 67 | int l; |
| 68 | for (l = GCS_FATAL; l <= GCS_INFO; l++) { |
| 69 | MYSQL_GCS_LOG( |
| 70 | (gcs_log_level_t)l, |
| 71 | gcs_log_levels[l] << "This is a logging message with level " << l); |
| 72 | } |
| 73 | |
| 74 | // Initialize new mock logger |
| 75 | Mock_Logger *anotherLogger = new Mock_Logger(); |
| 76 | Gcs_log_manager::initialize(anotherLogger); |
| 77 | |
| 78 | // anotherLogger initialized |
| 79 | ASSERT_EQ(true, Gcs_log_manager::get_logger() != nullptr); |
| 80 | ASSERT_EQ(anotherLogger, Gcs_log_manager::get_logger()); |
| 81 | |
| 82 | Gcs_log_manager::finalize(); |
| 83 | delete anotherLogger; |
| 84 | } |
| 85 | |
| 86 | class DebuggingInfrastructureTest : public GcsBaseTestNoLogging { |
| 87 | protected: |
| 88 | DebuggingInfrastructureTest() |
| 89 | : debugger(nullptr), sink(nullptr), saved_options(GCS_DEBUG_NONE) {} |
| 90 | |
| 91 | void SetUp() override { |
| 92 | sink = new Gcs_async_buffer(new Gcs_output_sink()); |
| 93 | debugger = new Gcs_default_debugger(sink); |
| 94 | saved_options = Gcs_debug_manager::get_current_debug_options(); |
| 95 | Gcs_debug_manager::unset_debug_options(GCS_DEBUG_ALL); |
| 96 | ASSERT_EQ(Gcs_debug_manager::get_current_debug_options(), GCS_DEBUG_NONE); |
| 97 | } |
| 98 | |
| 99 | void TearDown() override { |
| 100 | Gcs_debug_manager::unset_debug_options(GCS_DEBUG_ALL); |
| 101 | Gcs_debug_manager::set_debug_options(saved_options); |
| 102 | ASSERT_EQ(Gcs_debug_manager::get_current_debug_options(), saved_options); |
| 103 | |
| 104 | Gcs_debug_manager::finalize(); |
| 105 | delete debugger; |
| 106 | delete sink; |
| 107 | debugger = nullptr; |
| 108 | } |
| 109 | |
| 110 | Gcs_default_debugger *debugger; |
| 111 | Gcs_async_buffer *sink; |
| 112 | int64_t saved_options; |
| 113 | }; |
| 114 | |
| 115 | TEST_F(DebuggingInfrastructureTest, DebugManagerTestingSetOfOptions) { |
| 116 | /* |
| 117 | Prepare the environement. |
| 118 | */ |
| 119 | Gcs_debug_manager::initialize(debugger); |
| 120 | |
| 121 | /* |
| 122 | Checking if there are gaps in the set of valid options. |
| 123 | */ |
| 124 | int64_t option; |
| 125 | int64_t options = GCS_DEBUG_NONE; |
| 126 | for (unsigned int i = 0; i < Gcs_debug_manager::get_number_debug_options(); |
| 127 | i++) { |
| 128 | option = static_cast<int64_t>(1) << i; |
| 129 | ASSERT_TRUE(Gcs_debug_manager::is_valid_debug_options(option)); |
| 130 | options = options | option; |
| 131 | } |
| 132 | ASSERT_EQ(Gcs_debug_manager::get_valid_debug_options(), options); |
| 133 | |
| 134 | /* |
| 135 | Check if the set of valid options is different from GCS_DEBUG_NONE and |
| 136 | GCS_DEBUG_ALL. Although one can pass them as parameter to methods in |
| 137 | this interface, they are only useful definitions. |
| 138 | */ |
| 139 | ASSERT_TRUE(Gcs_debug_manager::is_valid_debug_options(GCS_DEBUG_ALL)); |
| 140 | ASSERT_TRUE(Gcs_debug_manager::is_valid_debug_options(GCS_DEBUG_NONE)); |
| 141 | ASSERT_FALSE(GCS_DEBUG_ALL == options); |
| 142 | ASSERT_FALSE(GCS_DEBUG_NONE == options); |
| 143 | ASSERT_FALSE(Gcs_debug_manager::is_valid_debug_options(GCS_INVALID_DEBUG)); |
| 144 | |
| 145 | /* |
| 146 | Restore the environment. |
| 147 | */ |
| 148 | Gcs_debug_manager::finalize(); |
| 149 | } |
| 150 | |
| 151 | TEST_F(DebuggingInfrastructureTest, DebugManagerTestingSettingIntegerOptions) { |
| 152 | /* |
| 153 | Prepare the environement to set the current debug options using integers. |
| 154 | */ |
| 155 | std::string res_debug_options; |
| 156 | Gcs_debug_manager::initialize(debugger); |
| 157 | |
| 158 | /* |
| 159 | Check if the initial value is GCS_DEBUG_NONE. |
| 160 | */ |
| 161 | ASSERT_EQ(Gcs_debug_manager::get_current_debug_options(), GCS_DEBUG_NONE); |
| 162 | Gcs_debug_manager::get_current_debug_options(res_debug_options); |
| 163 | ASSERT_EQ(res_debug_options.compare("GCS_DEBUG_NONE"), 0); |
| 164 | |
| 165 | /* |
| 166 | Check if it is possible to set GCS_DEBUG_BASIC. |
| 167 | */ |
| 168 | Gcs_debug_manager::set_debug_options(GCS_DEBUG_BASIC); |
| 169 | ASSERT_EQ(Gcs_debug_manager::get_current_debug_options(), GCS_DEBUG_BASIC); |
| 170 | Gcs_debug_manager::get_current_debug_options(res_debug_options); |
| 171 | ASSERT_EQ(res_debug_options.compare("GCS_DEBUG_BASIC"), 0); |
| 172 | |
| 173 | /* |
| 174 | Check if it is possible to set GCS_DEBUG_TRACE. |
| 175 | */ |
| 176 | Gcs_debug_manager::set_debug_options(GCS_DEBUG_TRACE); |
| 177 | ASSERT_EQ(Gcs_debug_manager::get_current_debug_options(), |
| 178 | GCS_DEBUG_BASIC | GCS_DEBUG_TRACE); |
| 179 | Gcs_debug_manager::get_current_debug_options(res_debug_options); |
| 180 | ASSERT_EQ(res_debug_options.compare("GCS_DEBUG_BASIC,GCS_DEBUG_TRACE"), 0); |
| 181 | |
| 182 | /* |
| 183 | Check if it is possible to set GCS_DEBUG_ALL. |
| 184 | */ |
| 185 | Gcs_debug_manager::set_debug_options(GCS_DEBUG_ALL); |
| 186 | ASSERT_EQ(Gcs_debug_manager::get_current_debug_options(), GCS_DEBUG_ALL); |
| 187 | Gcs_debug_manager::get_current_debug_options(res_debug_options); |
| 188 | ASSERT_EQ(res_debug_options.compare("GCS_DEBUG_ALL"), 0); |
| 189 | |
| 190 | /* |
| 191 | Check if it is possible to set GCS_DEBUG_NONE. |
| 192 | */ |
| 193 | Gcs_debug_manager::set_debug_options(GCS_DEBUG_NONE); |
| 194 | ASSERT_EQ(Gcs_debug_manager::get_current_debug_options(), GCS_DEBUG_ALL); |
| 195 | Gcs_debug_manager::get_current_debug_options(res_debug_options); |
| 196 | ASSERT_EQ(res_debug_options.compare("GCS_DEBUG_ALL"), 0); |
| 197 | |
| 198 | Gcs_debug_manager::unset_debug_options(GCS_DEBUG_ALL); |
| 199 | |
| 200 | Gcs_debug_manager::set_debug_options(GCS_DEBUG_BASIC | GCS_DEBUG_TRACE); |
| 201 | ASSERT_EQ(Gcs_debug_manager::get_current_debug_options(), |
| 202 | GCS_DEBUG_BASIC | GCS_DEBUG_TRACE); |
| 203 | Gcs_debug_manager::get_current_debug_options(res_debug_options); |
| 204 | ASSERT_EQ(res_debug_options.compare("GCS_DEBUG_BASIC,GCS_DEBUG_TRACE"), 0); |
| 205 | |
| 206 | Gcs_debug_manager::force_debug_options(GCS_DEBUG_BASIC); |
| 207 | ASSERT_EQ(Gcs_debug_manager::get_current_debug_options(), GCS_DEBUG_BASIC); |
| 208 | Gcs_debug_manager::get_current_debug_options(res_debug_options); |
| 209 | ASSERT_EQ(res_debug_options.compare("GCS_DEBUG_BASIC"), 0); |
| 210 | |
| 211 | Gcs_debug_manager::unset_debug_options(GCS_DEBUG_ALL); |
| 212 | ASSERT_EQ(Gcs_debug_manager::get_current_debug_options(), GCS_DEBUG_NONE); |
| 213 | Gcs_debug_manager::get_current_debug_options(res_debug_options); |
| 214 | ASSERT_EQ(res_debug_options.compare("GCS_DEBUG_NONE"), 0); |
| 215 | |
| 216 | ASSERT_TRUE(Gcs_debug_manager::set_debug_options(GCS_INVALID_DEBUG)); |
| 217 | ASSERT_EQ(Gcs_debug_manager::get_current_debug_options(), GCS_DEBUG_NONE); |
| 218 | Gcs_debug_manager::get_current_debug_options(res_debug_options); |
| 219 | ASSERT_EQ(res_debug_options.compare("GCS_DEBUG_NONE"), 0); |
| 220 | |
| 221 | ASSERT_FALSE(Gcs_debug_manager::set_debug_options(GCS_DEBUG_ALL)); |
| 222 | ASSERT_TRUE(Gcs_debug_manager::unset_debug_options(GCS_INVALID_DEBUG)); |
| 223 | ASSERT_EQ(Gcs_debug_manager::get_current_debug_options(), GCS_DEBUG_ALL); |
| 224 | Gcs_debug_manager::get_current_debug_options(res_debug_options); |
| 225 | ASSERT_EQ(res_debug_options.compare("GCS_DEBUG_ALL"), 0); |
| 226 | |
| 227 | Gcs_debug_manager::force_debug_options(GCS_INVALID_DEBUG); |
| 228 | ASSERT_EQ(Gcs_debug_manager::get_current_debug_options(), GCS_DEBUG_ALL); |
| 229 | Gcs_debug_manager::get_current_debug_options(res_debug_options); |
| 230 | ASSERT_EQ(res_debug_options.compare("GCS_DEBUG_ALL"), 0); |
| 231 | |
| 232 | Gcs_debug_manager::unset_debug_options(GCS_DEBUG_ALL); |
| 233 | |
| 234 | ASSERT_FALSE(Gcs_debug_manager::is_valid_debug_options(GCS_INVALID_DEBUG)); |
| 235 | Gcs_debug_manager::set_debug_options(GCS_DEBUG_BASIC | GCS_INVALID_DEBUG); |
| 236 | ASSERT_EQ(Gcs_debug_manager::get_current_debug_options(), GCS_DEBUG_NONE); |
| 237 | Gcs_debug_manager::get_current_debug_options(res_debug_options); |
| 238 | ASSERT_EQ(res_debug_options.compare("GCS_DEBUG_NONE"), 0); |
| 239 | |
| 240 | /* |
| 241 | Restore the environment. |
| 242 | */ |
| 243 | Gcs_debug_manager::finalize(); |
| 244 | } |
| 245 | |
| 246 | TEST_F(DebuggingInfrastructureTest, DebugManagerTestingSettingStringOptions) { |
| 247 | /* |
| 248 | Prepare the environement. |
| 249 | */ |
| 250 | std::string res_debug_options; |
| 251 | Gcs_debug_manager::initialize(debugger); |
| 252 | |
| 253 | /* |
| 254 | Setting the current debug options using strings. |
| 255 | */ |
| 256 | Gcs_debug_manager::set_debug_options("GCS_DEBUG_BASIC"); |
| 257 | ASSERT_EQ(Gcs_debug_manager::get_current_debug_options(), GCS_DEBUG_BASIC); |
| 258 | Gcs_debug_manager::get_current_debug_options(res_debug_options); |
| 259 | ASSERT_EQ(res_debug_options.compare("GCS_DEBUG_BASIC"), 0); |
| 260 | |
| 261 | Gcs_debug_manager::set_debug_options("GCS_DEBUG_TRACE"); |
| 262 | ASSERT_EQ(Gcs_debug_manager::get_current_debug_options(), |
| 263 | GCS_DEBUG_BASIC | GCS_DEBUG_TRACE); |
| 264 | Gcs_debug_manager::get_current_debug_options(res_debug_options); |
| 265 | ASSERT_EQ(res_debug_options.compare("GCS_DEBUG_BASIC,GCS_DEBUG_TRACE"), 0); |
| 266 | |
| 267 | Gcs_debug_manager::set_debug_options("GCS_DEBUG_ALL"); |
| 268 | ASSERT_EQ(Gcs_debug_manager::get_current_debug_options(), GCS_DEBUG_ALL); |
| 269 | Gcs_debug_manager::get_current_debug_options(res_debug_options); |
| 270 | ASSERT_EQ(res_debug_options.compare("GCS_DEBUG_ALL"), 0); |
| 271 | |
| 272 | Gcs_debug_manager::set_debug_options("GCS_DEBUG_NONE"); |
| 273 | ASSERT_EQ(Gcs_debug_manager::get_current_debug_options(), GCS_DEBUG_ALL); |
| 274 | Gcs_debug_manager::get_current_debug_options(res_debug_options); |
| 275 | ASSERT_EQ(res_debug_options.compare("GCS_DEBUG_ALL"), 0); |
| 276 | |
| 277 | Gcs_debug_manager::unset_debug_options("gcs_debug_all"); |
| 278 | Gcs_debug_manager::set_debug_options("gcs_debug_basic,gcs_invalid_debug,,"); |
| 279 | ASSERT_FALSE(Gcs_debug_manager::is_valid_debug_options("gcs_invalid_debug")); |
| 280 | ASSERT_EQ(Gcs_debug_manager::get_current_debug_options(), GCS_DEBUG_NONE); |
| 281 | Gcs_debug_manager::get_current_debug_options(res_debug_options); |
| 282 | ASSERT_EQ(res_debug_options.compare("GCS_DEBUG_NONE"), 0); |
| 283 | |
| 284 | Gcs_debug_manager::set_debug_options(",,,"); |
| 285 | ASSERT_FALSE(Gcs_debug_manager::is_valid_debug_options(",,,")); |
| 286 | ASSERT_EQ(Gcs_debug_manager::get_current_debug_options(), GCS_DEBUG_NONE); |
| 287 | Gcs_debug_manager::get_current_debug_options(res_debug_options); |
| 288 | ASSERT_EQ(res_debug_options.compare("GCS_DEBUG_NONE"), 0); |
| 289 | |
| 290 | Gcs_debug_manager::set_debug_options( |
| 291 | ",,gcs_debug_basic , gcs_debug_trace ,,"); |
| 292 | ASSERT_EQ(Gcs_debug_manager::get_current_debug_options(), |
| 293 | GCS_DEBUG_BASIC | GCS_DEBUG_TRACE); |
| 294 | Gcs_debug_manager::get_current_debug_options(res_debug_options); |
| 295 | ASSERT_EQ(res_debug_options.compare("GCS_DEBUG_BASIC,GCS_DEBUG_TRACE"), 0); |
| 296 | |
| 297 | Gcs_debug_manager::set_debug_options(",,,"); |
| 298 | ASSERT_EQ(Gcs_debug_manager::get_current_debug_options(), |
| 299 | GCS_DEBUG_BASIC | GCS_DEBUG_TRACE); |
| 300 | Gcs_debug_manager::get_current_debug_options(res_debug_options); |
| 301 | ASSERT_EQ(res_debug_options.compare("GCS_DEBUG_BASIC,GCS_DEBUG_TRACE"), 0); |
| 302 | |
| 303 | Gcs_debug_manager::force_debug_options("GCS_DEBUG_BASIC"); |
| 304 | ASSERT_EQ(Gcs_debug_manager::get_current_debug_options(), GCS_DEBUG_BASIC); |
| 305 | Gcs_debug_manager::get_current_debug_options(res_debug_options); |
| 306 | ASSERT_EQ(res_debug_options.compare("GCS_DEBUG_BASIC"), 0); |
| 307 | |
| 308 | Gcs_debug_manager::unset_debug_options("GCS_DEBUG_ALL"); |
| 309 | Gcs_debug_manager::set_debug_options( |
| 310 | "gcs_debug_basic,gcs_debug_trace,gcs_debug_all,gcs_debug_none"); |
| 311 | ASSERT_EQ(Gcs_debug_manager::get_current_debug_options(), GCS_DEBUG_ALL); |
| 312 | Gcs_debug_manager::get_current_debug_options(res_debug_options); |
| 313 | ASSERT_EQ(res_debug_options.compare("GCS_DEBUG_ALL"), 0); |
| 314 | |
| 315 | Gcs_debug_manager::unset_debug_options("GCS_DEBUG_ALL"); |
| 316 | ASSERT_EQ(Gcs_debug_manager::get_current_debug_options(), GCS_DEBUG_NONE); |
| 317 | Gcs_debug_manager::get_current_debug_options(res_debug_options); |
| 318 | ASSERT_EQ(res_debug_options.compare("GCS_DEBUG_NONE"), 0); |
| 319 | |
| 320 | ASSERT_FALSE(Gcs_debug_manager::is_valid_debug_options("GCS_INVALID_DEBUG")); |
| 321 | ASSERT_TRUE(Gcs_debug_manager::set_debug_options("GCS_INVALID_DEBUG")); |
| 322 | ASSERT_EQ(Gcs_debug_manager::get_current_debug_options(), GCS_DEBUG_NONE); |
| 323 | Gcs_debug_manager::get_current_debug_options(res_debug_options); |
| 324 | ASSERT_EQ(res_debug_options.compare("GCS_DEBUG_NONE"), 0); |
| 325 | |
| 326 | ASSERT_FALSE(Gcs_debug_manager::set_debug_options("GCS_DEBUG_ALL")); |
| 327 | ASSERT_TRUE(Gcs_debug_manager::unset_debug_options("GCS_INVALID_DEBUG")); |
| 328 | ASSERT_EQ(Gcs_debug_manager::get_current_debug_options(), GCS_DEBUG_ALL); |
| 329 | Gcs_debug_manager::get_current_debug_options(res_debug_options); |
| 330 | ASSERT_EQ(res_debug_options.compare("GCS_DEBUG_ALL"), 0); |
| 331 | |
| 332 | Gcs_debug_manager::force_debug_options("GCS_INVALID_DEBUG"); |
| 333 | ASSERT_EQ(Gcs_debug_manager::get_current_debug_options(), GCS_DEBUG_ALL); |
| 334 | Gcs_debug_manager::get_current_debug_options(res_debug_options); |
| 335 | ASSERT_EQ(res_debug_options.compare("GCS_DEBUG_ALL"), 0); |
| 336 | |
| 337 | Gcs_debug_manager::force_debug_options("GCS_DEBUG_BASIC"); |
| 338 | ASSERT_EQ(Gcs_debug_manager::get_current_debug_options(), GCS_DEBUG_BASIC); |
| 339 | Gcs_debug_manager::set_debug_options("GCS_DEBUG_ALL,GCS_INVALID_DEBUG"); |
| 340 | ASSERT_EQ(Gcs_debug_manager::get_current_debug_options(), GCS_DEBUG_BASIC); |
| 341 | Gcs_debug_manager::set_debug_options("GCS_INVALID_ALL,GCS_DEBUG_ALL"); |
| 342 | ASSERT_EQ(Gcs_debug_manager::get_current_debug_options(), GCS_DEBUG_BASIC); |
| 343 | Gcs_debug_manager::set_debug_options("GCS_DEBUG_ALL"); |
| 344 | ASSERT_EQ(Gcs_debug_manager::get_current_debug_options(), GCS_DEBUG_ALL); |
| 345 | Gcs_debug_manager::get_current_debug_options(res_debug_options); |
| 346 | ASSERT_EQ(res_debug_options.compare("GCS_DEBUG_ALL"), 0); |
| 347 | |
| 348 | /* |
| 349 | Restore the environment. |
| 350 | */ |
| 351 | Gcs_debug_manager::finalize(); |
| 352 | } |
| 353 | |
| 354 | } // namespace gcs_logging_unittest |