| 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/component_implementation.h> |
| 26 | #include <mysql/components/minimal_chassis.h> |
| 27 | #include <mysql/components/services/reference_caching.h> |
| 28 | #include <mysql/udf_registration_types.h> |
| 29 | #include <string> |
| 30 | #include "my_io.h" |
| 31 | #include "my_sys.h" |
| 32 | #include "test_reference_cache.h" |
| 33 | #include "unit_test_common.h" |
| 34 | |
| 35 | using registry_type_t = SERVICE_TYPE_NO_CONST(registry); |
| 36 | using loader_type_t = SERVICE_TYPE_NO_CONST(dynamic_loader); |
| 37 | using ref_cache_producer_type_t = |
| 38 | SERVICE_TYPE_NO_CONST(test_ref_cache_producer); |
| 39 | using ref_cache_consumer_type_t = |
| 40 | SERVICE_TYPE_NO_CONST(test_ref_cache_consumer); |
| 41 | SERVICE_TYPE(test_ref_cache_producer) * ref_cache_producer; |
| 42 | SERVICE_TYPE(test_ref_cache_consumer) * ref_cache_consumer; |
| 43 | |
| 44 | extern mysql_component_t mysql_component_mysql_server; |
| 45 | class reference_cache : public ::testing::Test { |
| 46 | protected: |
| 47 | void SetUp() override { |
| 48 | reg = nullptr; |
| 49 | loader = nullptr; |
| 50 | ref_cache_producer = nullptr; |
| 51 | ref_cache_consumer = nullptr; |
| 52 | ASSERT_FALSE(minimal_chassis_init((®), &COMPONENT_REF(mysql_server))); |
| 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 (loader) { |
| 60 | ASSERT_FALSE(reg->release( |
| 61 | reinterpret_cast<my_h_service>(const_cast<loader_type_t *>(loader)))); |
| 62 | } |
| 63 | ASSERT_FALSE(minimal_chassis_deinit(reg, &COMPONENT_REF(mysql_server))); |
| 64 | } |
| 65 | SERVICE_TYPE_NO_CONST(registry) * reg; |
| 66 | SERVICE_TYPE(dynamic_loader) * loader; |
| 67 | }; |
| 68 | |
| 69 | TEST_F(reference_cache, try_ref_cache_load_unload) { |
| 70 | static const char *urns[] = {"file://component_reference_cache"}; |
| 71 | std::string path; |
| 72 | const char *urn; |
| 73 | make_absolute_urn(*urns, &path); |
| 74 | urn = path.c_str(); |
| 75 | ASSERT_FALSE(loader->load(&urn, 1)); |
| 76 | ASSERT_FALSE(loader->unload(&urn, 1)); |
| 77 | } |
| 78 | |
| 79 | TEST_F(reference_cache, ref_cache_components_load_unload) { |
| 80 | static const char *urns[] = {"file://component_reference_cache", |
| 81 | "file://component_test_reference_cache"}; |
| 82 | |
| 83 | std::string path; |
| 84 | const char *absolute_urns[2]; |
| 85 | for (int i = 0; i < 2; i++) |
| 86 | absolute_urns[i] = (char *)malloc(2046 * sizeof(char)); |
| 87 | make_absolute_urn(urns[0], &path); |
| 88 | strcpy(const_cast<char *>(absolute_urns[0]), path.c_str()); |
| 89 | make_absolute_urn(urns[1], &path); |
| 90 | strcpy(const_cast<char *>(absolute_urns[1]), path.c_str()); |
| 91 | |
| 92 | ASSERT_FALSE(loader->load(absolute_urns, 2)); |
| 93 | ASSERT_FALSE(reg->acquire( |
| 94 | "test_ref_cache_producer", |
| 95 | reinterpret_cast<my_h_service *>( |
| 96 | const_cast<ref_cache_producer_type_t **>(&ref_cache_producer)))); |
| 97 | |
| 98 | ASSERT_FALSE(reg->acquire( |
| 99 | "test_ref_cache_consumer", |
| 100 | reinterpret_cast<my_h_service *>( |
| 101 | const_cast<ref_cache_consumer_type_t **>(&ref_cache_consumer)))); |
| 102 | |
| 103 | ASSERT_FALSE( |
| 104 | ref_cache_consumer->mysql_test_ref_cache_consumer_counter_reset()); |
| 105 | ASSERT_FALSE(ref_cache_consumer->mysql_test_ref_cache_consumer_counter_get()); |
| 106 | /* It will give one cache event, passing valid service_name_index */ |
| 107 | ASSERT_TRUE(ref_cache_producer->mysql_test_ref_cache_produce_event(0)); |
| 108 | /* It will give no cache event, passing invalid service_name_index */ |
| 109 | ASSERT_FALSE(ref_cache_producer->mysql_test_ref_cache_produce_event(1)); |
| 110 | ASSERT_FALSE(ref_cache_producer->mysql_test_ref_cache_flush()); |
| 111 | ASSERT_FALSE(ref_cache_producer->mysql_test_ref_cache_release_cache()); |
| 112 | ASSERT_FALSE( |
| 113 | ref_cache_producer->mysql_test_ref_cache_benchmark_run(0, 0, 0, 0)); |
| 114 | |
| 115 | if (ref_cache_producer) { |
| 116 | ASSERT_FALSE(reg->release(reinterpret_cast<my_h_service>( |
| 117 | const_cast<ref_cache_producer_type_t *>(ref_cache_producer)))); |
| 118 | } |
| 119 | |
| 120 | if (ref_cache_consumer) { |
| 121 | ASSERT_FALSE(reg->release(reinterpret_cast<my_h_service>( |
| 122 | const_cast<ref_cache_consumer_type_t *>(ref_cache_consumer)))); |
| 123 | } |
| 124 | ASSERT_FALSE(loader->unload(absolute_urns, 2)); |
| 125 | for (int i = 0; i < 2; i++) free(const_cast<char *>(absolute_urns[i])); |
| 126 | } |
| 127 | |
| 128 | /* mandatory main function */ |
| 129 | int main(int argc, char **argv) { |
| 130 | ::testing::InitGoogleTest(&argc, argv); |
| 131 | |
| 132 | MY_INIT(argv[0]); |
| 133 | |
| 134 | char realpath_buf[FN_REFLEN]; |
| 135 | char basedir_buf[FN_REFLEN]; |
| 136 | my_realpath(realpath_buf, my_progname, 0); |
| 137 | size_t res_length; |
| 138 | dirname_part(basedir_buf, realpath_buf, &res_length); |
| 139 | if (res_length > 0) basedir_buf[res_length - 1] = '\0'; |
| 140 | my_setwd(basedir_buf, 0); |
| 141 | |
| 142 | int retval = RUN_ALL_TESTS(); |
| 143 | return retval; |
| 144 | } |