| 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 | |
| 25 | #include <gtest/gtest.h> |
| 26 | |
| 27 | #include "sql/rpl_channel_credentials.h" |
| 28 | |
| 29 | namespace credential_struct_unittest { |
| 30 | |
| 31 | class CredentialStructTesting : public ::testing::Test { |
| 32 | protected: |
| 33 | CredentialStructTesting() = default; |
| 34 | |
| 35 | void SetUp() override { |
| 36 | user[0] = 0; |
| 37 | pass[0] = 0; |
| 38 | auth[0] = 0; |
| 39 | strcpy(user, "username"); |
| 40 | strcpy(pass, "password"); |
| 41 | strcpy(auth, "authentication"); |
| 42 | } |
| 43 | |
| 44 | void TearDown() override { |
| 45 | user[0] = 0; |
| 46 | pass[0] = 0; |
| 47 | auth[0] = 0; |
| 48 | } |
| 49 | |
| 50 | char user[1024]; |
| 51 | char pass[1024]; |
| 52 | char auth[1024]; |
| 53 | }; |
| 54 | |
| 55 | TEST_F(CredentialStructTesting, AssertAddition) { |
| 56 | /* Assert credential set is empty. */ |
| 57 | ASSERT_EQ(Rpl_channel_credentials::get_instance().number_of_channels(), 0); |
| 58 | |
| 59 | /* Create channel channel_1 */ |
| 60 | ASSERT_EQ(Rpl_channel_credentials::get_instance().store_credentials( |
| 61 | "channel_1", this->user, this->pass, this->auth), |
| 62 | 0); |
| 63 | /* Assert credentials of channel are added. */ |
| 64 | ASSERT_EQ(1, |
| 65 | (int)Rpl_channel_credentials::get_instance().number_of_channels()); |
| 66 | |
| 67 | /* Create channel channel_2 */ |
| 68 | ASSERT_EQ(Rpl_channel_credentials::get_instance().store_credentials( |
| 69 | "channel_2", this->user, this->pass, this->auth), |
| 70 | 0); |
| 71 | /* Assert credentials of channel are added. */ |
| 72 | ASSERT_EQ(2, |
| 73 | (int)Rpl_channel_credentials::get_instance().number_of_channels()); |
| 74 | |
| 75 | /* channel_2 already exist so it should fail, re-use store pointer. */ |
| 76 | ASSERT_EQ(Rpl_channel_credentials::get_instance().store_credentials( |
| 77 | "channel_2", this->user, this->pass, this->auth), |
| 78 | 1); |
| 79 | /* Assert credentials of channel are not added. */ |
| 80 | ASSERT_EQ(2, |
| 81 | (int)Rpl_channel_credentials::get_instance().number_of_channels()); |
| 82 | |
| 83 | /* Create channel channel_3, reuse store pointer. */ |
| 84 | this->user[0] = 0; |
| 85 | this->pass[0] = 0; |
| 86 | this->auth[0] = 0; |
| 87 | ASSERT_EQ(Rpl_channel_credentials::get_instance().store_credentials( |
| 88 | "channel_3", this->user, this->pass, this->auth), |
| 89 | 0); |
| 90 | /* Assert credentials of channel are not added. */ |
| 91 | ASSERT_EQ(3, |
| 92 | (int)Rpl_channel_credentials::get_instance().number_of_channels()); |
| 93 | |
| 94 | /* Create channel channel_4. */ |
| 95 | ASSERT_EQ(Rpl_channel_credentials::get_instance().store_credentials( |
| 96 | "channel_4", this->user, this->pass, this->auth), |
| 97 | 0); |
| 98 | |
| 99 | ASSERT_EQ(4, |
| 100 | (int)Rpl_channel_credentials::get_instance().number_of_channels()); |
| 101 | |
| 102 | /* Cleanup */ |
| 103 | Rpl_channel_credentials::get_instance().reset(); |
| 104 | ASSERT_EQ(true, |
| 105 | Rpl_channel_credentials::get_instance().number_of_channels() == 0); |
| 106 | } |
| 107 | |
| 108 | TEST_F(CredentialStructTesting, AssertDeletion) { |
| 109 | /* Assert credential set is empty. */ |
| 110 | ASSERT_EQ(Rpl_channel_credentials::get_instance().number_of_channels(), 0); |
| 111 | |
| 112 | /* Store 3 channels. */ |
| 113 | ASSERT_EQ(Rpl_channel_credentials::get_instance().store_credentials( |
| 114 | "channel_1", this->user, this->pass, this->auth), |
| 115 | 0); |
| 116 | ASSERT_EQ(Rpl_channel_credentials::get_instance().store_credentials( |
| 117 | "channel_2", this->user, this->pass, this->auth), |
| 118 | 0); |
| 119 | ASSERT_EQ(Rpl_channel_credentials::get_instance().store_credentials( |
| 120 | "channel_3", this->user, this->pass, this->auth), |
| 121 | 0); |
| 122 | |
| 123 | /* Assert 3 channels are present. */ |
| 124 | ASSERT_EQ(3, |
| 125 | (int)Rpl_channel_credentials::get_instance().number_of_channels()); |
| 126 | |
| 127 | /* Delete channel_3. */ |
| 128 | ASSERT_EQ( |
| 129 | Rpl_channel_credentials::get_instance().delete_credentials("channel_3"), |
| 130 | 0); |
| 131 | /* Assert channel is deleted. */ |
| 132 | ASSERT_EQ(2, |
| 133 | (int)Rpl_channel_credentials::get_instance().number_of_channels()); |
| 134 | |
| 135 | /* Delete channel_3 throws error now. */ |
| 136 | ASSERT_EQ( |
| 137 | Rpl_channel_credentials::get_instance().delete_credentials("channel_3"), |
| 138 | 1); |
| 139 | ASSERT_EQ(2, |
| 140 | (int)Rpl_channel_credentials::get_instance().number_of_channels()); |
| 141 | |
| 142 | /* Check error is thrown for non-existing channel. */ |
| 143 | ASSERT_EQ(Rpl_channel_credentials::get_instance().delete_credentials( |
| 144 | "channel_does_not_exist"), |
| 145 | 1); |
| 146 | ASSERT_EQ(2, |
| 147 | (int)Rpl_channel_credentials::get_instance().number_of_channels()); |
| 148 | |
| 149 | /* Delete channel_2. */ |
| 150 | ASSERT_EQ( |
| 151 | Rpl_channel_credentials::get_instance().delete_credentials("channel_2"), |
| 152 | 0); |
| 153 | /* Assert channel is deleted. */ |
| 154 | ASSERT_EQ(1, |
| 155 | (int)Rpl_channel_credentials::get_instance().number_of_channels()); |
| 156 | |
| 157 | /* Delete channel_1. */ |
| 158 | ASSERT_EQ( |
| 159 | Rpl_channel_credentials::get_instance().delete_credentials("channel_1"), |
| 160 | 0); |
| 161 | |
| 162 | /* Assert instance is automatically deleted. */ |
| 163 | ASSERT_EQ(true, |
| 164 | Rpl_channel_credentials::get_instance().number_of_channels() == 0); |
| 165 | |
| 166 | /* Assert delete gives error when channel list is empty. */ |
| 167 | ASSERT_EQ(Rpl_channel_credentials::get_instance().delete_credentials( |
| 168 | "channel_list_empty"), |
| 169 | 1); |
| 170 | ASSERT_EQ(true, |
| 171 | Rpl_channel_credentials::get_instance().number_of_channels() == 0); |
| 172 | } |
| 173 | |
| 174 | TEST_F(CredentialStructTesting, AssertCleanup) { |
| 175 | /* Assert credential set is empty. */ |
| 176 | ASSERT_EQ(Rpl_channel_credentials::get_instance().number_of_channels(), 0); |
| 177 | Rpl_channel_credentials::get_instance().reset(); |
| 178 | ASSERT_EQ(Rpl_channel_credentials::get_instance().number_of_channels(), 0); |
| 179 | |
| 180 | /* Store 3 channels. */ |
| 181 | ASSERT_EQ(Rpl_channel_credentials::get_instance().store_credentials( |
| 182 | "channel_1", this->user, this->pass, this->auth), |
| 183 | 0); |
| 184 | ASSERT_EQ(Rpl_channel_credentials::get_instance().store_credentials( |
| 185 | "channel_2", this->user, this->pass, this->auth), |
| 186 | 0); |
| 187 | ASSERT_EQ(Rpl_channel_credentials::get_instance().store_credentials( |
| 188 | "channel_3", this->user, this->pass, this->auth), |
| 189 | 0); |
| 190 | ASSERT_EQ(3, |
| 191 | (int)Rpl_channel_credentials::get_instance().number_of_channels()); |
| 192 | |
| 193 | /* Delete all channels. */ |
| 194 | Rpl_channel_credentials::get_instance().reset(); |
| 195 | ASSERT_EQ(true, |
| 196 | Rpl_channel_credentials::get_instance().number_of_channels() == 0); |
| 197 | } |
| 198 | |
| 199 | TEST_F(CredentialStructTesting, AssertGet) { |
| 200 | String_set user, pass, auth; |
| 201 | |
| 202 | /* Assert credential set is empty. */ |
| 203 | ASSERT_EQ(Rpl_channel_credentials::get_instance().number_of_channels(), 0); |
| 204 | ASSERT_EQ(Rpl_channel_credentials::get_instance().get_credentials( |
| 205 | "channel_does_not_exist", user, pass, auth), |
| 206 | 1); |
| 207 | |
| 208 | /* Store 3 channels. */ |
| 209 | /* Channel_1 credentials are username, password, authentication. */ |
| 210 | ASSERT_EQ(Rpl_channel_credentials::get_instance().store_credentials( |
| 211 | "channel_1", this->user, this->pass, this->auth), |
| 212 | 0); |
| 213 | /* Channel_2 credentials are username, password, NULL, NULL. */ |
| 214 | ASSERT_EQ(Rpl_channel_credentials::get_instance().store_credentials( |
| 215 | "channel_2", this->user, this->pass, nullptr), |
| 216 | 0); |
| 217 | /* Channel_3 credentials are username, NULL, NULL, NULL. */ |
| 218 | ASSERT_EQ(Rpl_channel_credentials::get_instance().store_credentials( |
| 219 | "channel_3", this->user, nullptr, nullptr), |
| 220 | 0); |
| 221 | /* Channel_4 credentials are "", "", "", "". */ |
| 222 | this->user[0] = 0; |
| 223 | this->pass[0] = 0; |
| 224 | this->auth[0] = 0; |
| 225 | ASSERT_EQ(Rpl_channel_credentials::get_instance().store_credentials( |
| 226 | "channel_4", this->user, this->pass, this->auth), |
| 227 | 0); |
| 228 | /* Channel_5 credentials are NULL, NULL, NULL, NULL. */ |
| 229 | ASSERT_EQ(Rpl_channel_credentials::get_instance().store_credentials( |
| 230 | "channel_5", nullptr, nullptr, nullptr), |
| 231 | 0); |
| 232 | |
| 233 | ASSERT_EQ(5, |
| 234 | (int)Rpl_channel_credentials::get_instance().number_of_channels()); |
| 235 | |
| 236 | /* Assert credentials are correct for channel_1. */ |
| 237 | /* Channel_1 credentials are username, password, authentication. */ |
| 238 | Rpl_channel_credentials::get_instance().get_credentials("channel_1", user, |
| 239 | pass, auth); |
| 240 | ASSERT_EQ(std::string("username"), user.second); |
| 241 | ASSERT_EQ(std::string("password"), pass.second); |
| 242 | ASSERT_EQ(std::string("authentication"), auth.second); |
| 243 | ASSERT_EQ(user.first, true); |
| 244 | ASSERT_EQ(pass.first, true); |
| 245 | ASSERT_EQ(auth.first, true); |
| 246 | |
| 247 | /* Assert credentials are correct for channel_2. */ |
| 248 | /* Channel_2 credentials are username, password, NULL, NULL. */ |
| 249 | Rpl_channel_credentials::get_instance().get_credentials("channel_2", user, |
| 250 | pass, auth); |
| 251 | ASSERT_EQ(std::string("username"), user.second); |
| 252 | ASSERT_EQ(std::string("password"), pass.second); |
| 253 | ASSERT_EQ(std::string(""), auth.second); |
| 254 | ASSERT_EQ(user.first, true); |
| 255 | ASSERT_EQ(pass.first, true); |
| 256 | ASSERT_EQ(auth.first, false); |
| 257 | |
| 258 | /* Assert credentials are correct for channel_3. */ |
| 259 | /* Channel_3 credentials are username, NULL, NULL, NULL. */ |
| 260 | Rpl_channel_credentials::get_instance().get_credentials("channel_3", user, |
| 261 | pass, auth); |
| 262 | ASSERT_EQ(std::string("username"), user.second); |
| 263 | ASSERT_EQ(std::string(""), pass.second); |
| 264 | ASSERT_EQ(std::string(""), auth.second); |
| 265 | ASSERT_EQ(user.first, true); |
| 266 | ASSERT_EQ(pass.first, false); |
| 267 | ASSERT_EQ(auth.first, false); |
| 268 | |
| 269 | /* Assert credentials are correct for channel_4. */ |
| 270 | /* Channel_4 credentials are "", "", "", "". */ |
| 271 | Rpl_channel_credentials::get_instance().get_credentials("channel_4", user, |
| 272 | pass, auth); |
| 273 | ASSERT_EQ(std::string(""), user.second); |
| 274 | ASSERT_EQ(std::string(""), pass.second); |
| 275 | ASSERT_EQ(std::string(""), auth.second); |
| 276 | ASSERT_EQ(user.first, true); |
| 277 | ASSERT_EQ(pass.first, true); |
| 278 | ASSERT_EQ(auth.first, true); |
| 279 | |
| 280 | /* Assert credentials are correct for channel_5. */ |
| 281 | /* Channel_5 credentials are NULL, NULL, NULL, NULL. */ |
| 282 | Rpl_channel_credentials::get_instance().get_credentials("channel_5", user, |
| 283 | pass, auth); |
| 284 | ASSERT_EQ(user.second.empty(), true); |
| 285 | ASSERT_EQ(pass.second.empty(), true); |
| 286 | ASSERT_EQ(auth.second.empty(), true); |
| 287 | ASSERT_EQ(user.first, false); |
| 288 | ASSERT_EQ(pass.first, false); |
| 289 | ASSERT_EQ(auth.first, false); |
| 290 | |
| 291 | /* get returns error when channel does not exist. */ |
| 292 | ASSERT_EQ(Rpl_channel_credentials::get_instance().get_credentials( |
| 293 | "channel_does_not_exist", user, pass, auth), |
| 294 | 1); |
| 295 | ASSERT_EQ(user.second.empty(), true); |
| 296 | ASSERT_EQ(pass.second.empty(), true); |
| 297 | ASSERT_EQ(auth.second.empty(), true); |
| 298 | ASSERT_EQ(user.first, false); |
| 299 | ASSERT_EQ(pass.first, false); |
| 300 | ASSERT_EQ(auth.first, false); |
| 301 | |
| 302 | /* Delete all channels. */ |
| 303 | Rpl_channel_credentials::get_instance().reset(); |
| 304 | ASSERT_EQ(true, |
| 305 | Rpl_channel_credentials::get_instance().number_of_channels() == 0); |
| 306 | } |
| 307 | |
| 308 | } // namespace credential_struct_unittest |