| 1 | /* Copyright (c) 2020, 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 <gtest/gtest.h> |
| 25 | #include <mysql/components/minimal_chassis.h> |
| 26 | #include <string> |
| 27 | |
| 28 | #include "my_io.h" |
| 29 | #include "my_sys.h" |
| 30 | #include "unit_test_common.h" |
| 31 | |
| 32 | /* This is the test which uses the minimal chassis library and checks the |
| 33 | dynamic loader load and unload functions using mysql_server component. |
| 34 | |
| 35 | This testdriver does the following: |
| 36 | * minimal_chassis_init() Which makes the registry and dynamic loader |
| 37 | services ready. |
| 38 | * acquire "dynamic_loader" service from the registry. |
| 39 | * loads mysql_server component |
| 40 | * unloads mysql_server component |
| 41 | * unregisters all the services at the exit time. |
| 42 | */ |
| 43 | |
| 44 | using registry_type_t = SERVICE_TYPE_NO_CONST(registry); |
| 45 | using loader_type_t = SERVICE_TYPE_NO_CONST(dynamic_loader); |
| 46 | |
| 47 | class minimal_chassis : public ::testing::Test { |
| 48 | protected: |
| 49 | void SetUp() override { |
| 50 | reg = nullptr; |
| 51 | loader = nullptr; |
| 52 | ASSERT_FALSE(minimal_chassis_init((®), nullptr)); |
| 53 | ASSERT_FALSE(reg->acquire("dynamic_loader", |
| 54 | reinterpret_cast<my_h_service *>( |
| 55 | const_cast<loader_type_t **>(&loader)))); |
| 56 | } |
| 57 | |
| 58 | void TearDown() override { |
| 59 | if (reg) { |
| 60 | ASSERT_FALSE(reg->release( |
| 61 | reinterpret_cast<my_h_service>(const_cast<registry_type_t *>(reg)))); |
| 62 | } |
| 63 | if (loader) { |
| 64 | ASSERT_FALSE(reg->release( |
| 65 | reinterpret_cast<my_h_service>(const_cast<loader_type_t *>(loader)))); |
| 66 | } |
| 67 | ASSERT_FALSE(minimal_chassis_deinit(reg, nullptr)); |
| 68 | } |
| 69 | SERVICE_TYPE_NO_CONST(registry) * reg; |
| 70 | SERVICE_TYPE(dynamic_loader) * loader; |
| 71 | }; |
| 72 | |
| 73 | TEST_F(minimal_chassis, try_load_component) { |
| 74 | static const char *urns[] = {"file://component_example_component1"}; |
| 75 | std::string path; |
| 76 | const char *urn; |
| 77 | make_absolute_urn(*urns, &path); |
| 78 | urn = path.c_str(); |
| 79 | ASSERT_FALSE(loader->load(&urn, 1)); |
| 80 | ASSERT_FALSE(loader->unload(&urn, 1)); |
| 81 | } |
| 82 | |
| 83 | /* mandatory main function */ |
| 84 | int main(int argc, char **argv) { |
| 85 | ::testing::InitGoogleTest(&argc, argv); |
| 86 | |
| 87 | MY_INIT(argv[0]); |
| 88 | |
| 89 | char realpath_buf[FN_REFLEN]; |
| 90 | char basedir_buf[FN_REFLEN]; |
| 91 | my_realpath(realpath_buf, my_progname, 0); |
| 92 | size_t res_length; |
| 93 | dirname_part(basedir_buf, realpath_buf, &res_length); |
| 94 | if (res_length > 0) basedir_buf[res_length - 1] = '\0'; |
| 95 | my_setwd(basedir_buf, 0); |
| 96 | |
| 97 | int retval = RUN_ALL_TESTS(); |
| 98 | return retval; |
| 99 | } |