| 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 <gtest/gtest.h> |
| 25 | #include <mysql/components/minimal_chassis.h> |
| 26 | #include <mysql/components/my_service.h> |
| 27 | |
| 28 | #include "scope_guard.h" |
| 29 | #include "unit_test_common.h" |
| 30 | |
| 31 | using service_type_t = SERVICE_TYPE_NO_CONST(registry); |
| 32 | |
| 33 | class registry : public ::testing::Test { |
| 34 | protected: |
| 35 | void SetUp() override { ASSERT_FALSE(minimal_chassis_init(®, nullptr)); } |
| 36 | |
| 37 | void TearDown() override { |
| 38 | ASSERT_FALSE(reg->release( |
| 39 | reinterpret_cast<my_h_service>(const_cast<service_type_t *>(reg)))); |
| 40 | ASSERT_FALSE(minimal_chassis_deinit(reg, nullptr)); |
| 41 | } |
| 42 | SERVICE_TYPE_NO_CONST(registry) * reg; |
| 43 | }; |
| 44 | |
| 45 | TEST_F(registry, bootstrap) { ASSERT_TRUE(reg != nullptr); } |
| 46 | |
| 47 | TEST_F(registry, basic_operations) { |
| 48 | my_h_service hreg, hreg2; |
| 49 | |
| 50 | ASSERT_FALSE(reg->acquire("registry", &hreg)); |
| 51 | ASSERT_TRUE(hreg != nullptr); |
| 52 | ASSERT_FALSE(reg->acquire("registry.mysql_minimal_chassis", &hreg2)); |
| 53 | ASSERT_TRUE(hreg == hreg2); |
| 54 | ASSERT_TRUE(hreg == reinterpret_cast<my_h_service>( |
| 55 | const_cast<service_type_t *>(reg))); |
| 56 | ASSERT_FALSE(reg->release(hreg)); |
| 57 | ASSERT_FALSE(reg->release(hreg2)); |
| 58 | ASSERT_TRUE(reg->release(my_h_service{})); |
| 59 | } |
| 60 | |
| 61 | TEST_F(registry, register_twice) { |
| 62 | my_service<SERVICE_TYPE(registry_registration)> registration_service( |
| 63 | "registry_registration", reg); |
| 64 | ASSERT_FALSE(registration_service); |
| 65 | { |
| 66 | my_service<SERVICE_TYPE(registry)> service("test", reg); |
| 67 | ASSERT_TRUE(service); |
| 68 | } |
| 69 | ASSERT_FALSE(registration_service->register_service( |
| 70 | "test.test1", reinterpret_cast<my_h_service_imp *>(1))); |
| 71 | ASSERT_TRUE(registration_service->register_service( |
| 72 | "test.test1", reinterpret_cast<my_h_service_imp *>(1))); |
| 73 | { |
| 74 | my_service<SERVICE_TYPE(registry)> service("test", reg); |
| 75 | ASSERT_FALSE(service); |
| 76 | } |
| 77 | |
| 78 | ASSERT_FALSE(registration_service->unregister("test.test1")); |
| 79 | { |
| 80 | my_service<SERVICE_TYPE(registry)> service("test", reg); |
| 81 | ASSERT_TRUE(service); |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | TEST_F(registry, unregister_activelly_used) { |
| 86 | my_service<SERVICE_TYPE(registry_registration)> registration_service( |
| 87 | "registry_registration", reg); |
| 88 | ASSERT_FALSE(registration_service); |
| 89 | { |
| 90 | my_service<SERVICE_TYPE(registry)> service("test", reg); |
| 91 | ASSERT_TRUE(service); |
| 92 | } |
| 93 | ASSERT_FALSE(registration_service->register_service( |
| 94 | "test.test1", reinterpret_cast<my_h_service_imp *>(1))); |
| 95 | { |
| 96 | my_service<SERVICE_TYPE(registry)> service("test", reg); |
| 97 | ASSERT_FALSE(service); |
| 98 | ASSERT_TRUE(registration_service->unregister("test.test1")); |
| 99 | } |
| 100 | |
| 101 | ASSERT_FALSE(registration_service->unregister("test.test1")); |
| 102 | { |
| 103 | my_service<SERVICE_TYPE(registry)> service("test", reg); |
| 104 | ASSERT_TRUE(service); |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | TEST_F(registry, unregister_non_registered) { |
| 109 | my_service<SERVICE_TYPE(registry_registration)> registration_service( |
| 110 | "registry_registration", reg); |
| 111 | ASSERT_FALSE(registration_service); |
| 112 | ASSERT_TRUE(registration_service->unregister("test.test1")); |
| 113 | { |
| 114 | my_service<SERVICE_TYPE(registry)> service("test", reg); |
| 115 | ASSERT_TRUE(service); |
| 116 | } |
| 117 | } |
| 118 | |
| 119 | TEST_F(registry, registration_and_default) { |
| 120 | my_service<SERVICE_TYPE(registry_registration)> registration_service( |
| 121 | "registry_registration", reg); |
| 122 | ASSERT_FALSE(registration_service); |
| 123 | { |
| 124 | my_service<SERVICE_TYPE(registry)> service("test", reg); |
| 125 | ASSERT_TRUE(service); |
| 126 | } |
| 127 | /* Null interface */ |
| 128 | ASSERT_TRUE(registration_service->register_service("test", my_h_service{})); |
| 129 | /* Bad name */ |
| 130 | ASSERT_TRUE(registration_service->register_service( |
| 131 | "test", reinterpret_cast<my_h_service_imp *>(1))); |
| 132 | ASSERT_TRUE(registration_service->register_service( |
| 133 | ".test", reinterpret_cast<my_h_service_imp *>(1))); |
| 134 | ASSERT_TRUE(registration_service->register_service( |
| 135 | "test.", reinterpret_cast<my_h_service_imp *>(1))); |
| 136 | ASSERT_TRUE(registration_service->register_service( |
| 137 | "test.test.test", reinterpret_cast<my_h_service_imp *>(1))); |
| 138 | |
| 139 | ASSERT_FALSE(registration_service->register_service( |
| 140 | "test.test1", reinterpret_cast<my_h_service_imp *>(1))); |
| 141 | { |
| 142 | my_service<SERVICE_TYPE(registry)> service("test", reg); |
| 143 | ASSERT_FALSE(service); |
| 144 | ASSERT_EQ( |
| 145 | static_cast<my_h_service_imp *>(static_cast<my_h_service>(service)), |
| 146 | reinterpret_cast<my_h_service_imp *>(1)); |
| 147 | } |
| 148 | ASSERT_FALSE(registration_service->register_service( |
| 149 | "test.test2", reinterpret_cast<my_h_service_imp *>(2))); |
| 150 | ASSERT_TRUE(registration_service->register_service( |
| 151 | "test.test2", reinterpret_cast<my_h_service_imp *>(3))); |
| 152 | { |
| 153 | my_service<SERVICE_TYPE(registry)> service("test", reg); |
| 154 | ASSERT_FALSE(service); |
| 155 | ASSERT_EQ( |
| 156 | static_cast<my_h_service_imp *>(static_cast<my_h_service>(service)), |
| 157 | reinterpret_cast<my_h_service_imp *>(1)); |
| 158 | } |
| 159 | ASSERT_FALSE(registration_service->set_default("test.test2")); |
| 160 | ASSERT_TRUE(registration_service->set_default("bad_name.test2")); |
| 161 | { |
| 162 | my_service<SERVICE_TYPE(registry)> service("test", reg); |
| 163 | ASSERT_FALSE(service); |
| 164 | ASSERT_EQ( |
| 165 | static_cast<my_h_service_imp *>(static_cast<my_h_service>(service)), |
| 166 | reinterpret_cast<my_h_service_imp *>(2)); |
| 167 | } |
| 168 | ASSERT_FALSE(registration_service->unregister("test.test2")); |
| 169 | { |
| 170 | my_service<SERVICE_TYPE(registry)> service("test", reg); |
| 171 | ASSERT_FALSE(service); |
| 172 | ASSERT_EQ( |
| 173 | static_cast<my_h_service_imp *>(static_cast<my_h_service>(service)), |
| 174 | reinterpret_cast<my_h_service_imp *>(1)); |
| 175 | } |
| 176 | ASSERT_FALSE(registration_service->unregister("test.test1")); |
| 177 | { |
| 178 | my_service<SERVICE_TYPE(registry)> service("test", reg); |
| 179 | ASSERT_TRUE(service); |
| 180 | } |
| 181 | } |
| 182 | TEST_F(registry, my_service) { |
| 183 | my_h_service hreg; |
| 184 | ASSERT_FALSE(reg->acquire("registry_query", &hreg)); |
| 185 | ASSERT_TRUE(hreg != nullptr); |
| 186 | |
| 187 | { |
| 188 | my_service<SERVICE_TYPE(registry_query)> service("registry_query", reg); |
| 189 | ASSERT_FALSE(service); |
| 190 | ASSERT_TRUE(hreg == service); |
| 191 | } |
| 192 | |
| 193 | ASSERT_FALSE(reg->release(hreg)); |
| 194 | ASSERT_TRUE(reg->release(hreg)); |
| 195 | } |
| 196 | TEST_F(registry, acquire_related) { |
| 197 | my_service<SERVICE_TYPE(registry_registration)> registration_service( |
| 198 | "registry_registration", reg); |
| 199 | ASSERT_FALSE(registration_service); |
| 200 | ASSERT_FALSE(registration_service->register_service( |
| 201 | "test.component1", reinterpret_cast<my_h_service_imp *>(1))); |
| 202 | ASSERT_FALSE(registration_service->register_service( |
| 203 | "test.component2", reinterpret_cast<my_h_service_imp *>(2))); |
| 204 | ASSERT_FALSE(registration_service->register_service( |
| 205 | "test.component3", reinterpret_cast<my_h_service_imp *>(3))); |
| 206 | ASSERT_FALSE(registration_service->register_service( |
| 207 | "another_service.component1", reinterpret_cast<my_h_service_imp *>(11))); |
| 208 | ASSERT_FALSE(registration_service->register_service( |
| 209 | "another_service.component2", reinterpret_cast<my_h_service_imp *>(12))); |
| 210 | |
| 211 | { |
| 212 | my_service<SERVICE_TYPE(registry)> service1("test", reg); |
| 213 | ASSERT_FALSE(service1); |
| 214 | ASSERT_EQ( |
| 215 | static_cast<my_h_service_imp *>(static_cast<my_h_service>(service1)), |
| 216 | reinterpret_cast<my_h_service_imp *>(1)); |
| 217 | |
| 218 | my_service<SERVICE_TYPE(registry)> service2("test.component2", reg); |
| 219 | ASSERT_FALSE(service2); |
| 220 | ASSERT_EQ( |
| 221 | static_cast<my_h_service_imp *>(static_cast<my_h_service>(service2)), |
| 222 | reinterpret_cast<my_h_service_imp *>(2)); |
| 223 | |
| 224 | my_service<SERVICE_TYPE(registry)> service3("test.component3", reg); |
| 225 | ASSERT_FALSE(service3); |
| 226 | ASSERT_EQ( |
| 227 | static_cast<my_h_service_imp *>(static_cast<my_h_service>(service3)), |
| 228 | reinterpret_cast<my_h_service_imp *>(3)); |
| 229 | |
| 230 | my_service<SERVICE_TYPE(registry)> another_service1("another_service", |
| 231 | service1, reg); |
| 232 | ASSERT_FALSE(another_service1); |
| 233 | ASSERT_EQ(static_cast<my_h_service_imp *>( |
| 234 | static_cast<my_h_service>(another_service1)), |
| 235 | reinterpret_cast<my_h_service_imp *>(11)); |
| 236 | |
| 237 | my_service<SERVICE_TYPE(registry)> another_service2("another_service", |
| 238 | service2, reg); |
| 239 | ASSERT_FALSE(another_service2); |
| 240 | ASSERT_EQ(static_cast<my_h_service_imp *>( |
| 241 | static_cast<my_h_service>(another_service2)), |
| 242 | reinterpret_cast<my_h_service_imp *>(12)); |
| 243 | |
| 244 | my_service<SERVICE_TYPE(registry)> another_service3("another_service", |
| 245 | service3, reg); |
| 246 | ASSERT_TRUE(another_service3); |
| 247 | } |
| 248 | |
| 249 | ASSERT_FALSE(registration_service->unregister("test.component1")); |
| 250 | ASSERT_FALSE(registration_service->unregister("test.component2")); |
| 251 | ASSERT_FALSE(registration_service->unregister("test.component3")); |
| 252 | ASSERT_FALSE(registration_service->unregister("another_service.component1")); |
| 253 | ASSERT_FALSE(registration_service->unregister("another_service.component2")); |
| 254 | |
| 255 | /* Bad service implementation pointer */ |
| 256 | ASSERT_TRUE(reg->acquire_related("bad_name", my_h_service{}, nullptr)); |
| 257 | ASSERT_TRUE(reg->acquire_related( |
| 258 | "bad_name", |
| 259 | reinterpret_cast<my_h_service>(const_cast<service_type_t *>(reg)), |
| 260 | nullptr)); |
| 261 | ASSERT_TRUE(reg->acquire_related( |
| 262 | "bad_name.with_component", |
| 263 | reinterpret_cast<my_h_service>(const_cast<service_type_t *>(reg)), |
| 264 | nullptr)); |
| 265 | |
| 266 | { |
| 267 | my_service<SERVICE_TYPE(registry)> scheme_file_service( |
| 268 | "dynamic_loader_scheme_file.mysql_minimal_chassis", reg); |
| 269 | ASSERT_FALSE(scheme_file_service); |
| 270 | |
| 271 | /* |
| 272 | No other services implemented with that implementation name, should |
| 273 | fallback to default. |
| 274 | */ |
| 275 | my_service<SERVICE_TYPE(registry)> another_service( |
| 276 | "registry", scheme_file_service, reg); |
| 277 | ASSERT_FALSE(another_service); |
| 278 | ASSERT_EQ(another_service, reg); |
| 279 | } |
| 280 | } |
| 281 | TEST_F(registry, iteration) { |
| 282 | my_service<SERVICE_TYPE(registry_query)> service("registry_query", reg); |
| 283 | ASSERT_FALSE(service); |
| 284 | |
| 285 | my_h_service_iterator iterator; |
| 286 | const char *name; |
| 287 | int count = 0; |
| 288 | bool registrator_found = false; |
| 289 | |
| 290 | ASSERT_TRUE(service->create("not_existing", &iterator)); |
| 291 | ASSERT_FALSE(service->create("", &iterator)); |
| 292 | |
| 293 | auto guard = create_scope_guard( |
| 294 | [&service, &iterator]() { service->release(iterator); }); |
| 295 | |
| 296 | service->release(my_h_service_iterator{}); |
| 297 | ASSERT_TRUE(service->get(my_h_service_iterator{}, &name)); |
| 298 | ASSERT_TRUE(service->next(my_h_service_iterator{})); |
| 299 | ASSERT_TRUE(service->is_valid(my_h_service_iterator{})); |
| 300 | |
| 301 | for (; !service->is_valid(iterator); service->next(iterator)) { |
| 302 | ASSERT_FALSE(service->get(iterator, &name)); |
| 303 | |
| 304 | count++; |
| 305 | registrator_found |= |
| 306 | !strcmp(name, "registry_registration.mysql_minimal_chassis"); |
| 307 | } |
| 308 | ASSERT_TRUE(service->get(iterator, &name)); |
| 309 | ASSERT_TRUE(service->next(iterator)); |
| 310 | ASSERT_TRUE(service->is_valid(iterator)); |
| 311 | |
| 312 | ASSERT_GE(count, 2); /* there should be at least 2 services in registry. */ |
| 313 | ASSERT_TRUE(registrator_found); |
| 314 | } |
| 315 | |
| 316 | /* mandatory main function */ |
| 317 | int main(int argc, char **argv) { |
| 318 | ::testing::InitGoogleTest(&argc, argv); |
| 319 | return RUN_ALL_TESTS(); |
| 320 | } |