| 1 | /* Copyright (c) 2022, 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 | |
| 26 | #include <cstring> |
| 27 | #include <map> |
| 28 | #include <memory> |
| 29 | #include <vector> |
| 30 | |
| 31 | #include "base64.h" |
| 32 | #include "my_aes.h" |
| 33 | #include "my_byteorder.h" |
| 34 | #include "my_inttypes.h" |
| 35 | #include "my_rnd.h" |
| 36 | #include "my_systime.h" |
| 37 | #include "sql-common/json_binary.h" |
| 38 | #include "sql-common/json_dom.h" |
| 39 | #include "sql-common/json_path.h" |
| 40 | #include "sql/json_diff.h" |
| 41 | #include "sql/my_decimal.h" |
| 42 | #include "sql/persisted_variable.h" |
| 43 | #include "sql/sql_class.h" |
| 44 | #include "sql/sql_time.h" |
| 45 | #include "sql_string.h" |
| 46 | #include "template_utils.h" // down_cast |
| 47 | #include "unittest/gunit/test_utils.h" |
| 48 | |
| 49 | /** |
| 50 | Test Json_dom class hierarchy API, cf. json_dom.h |
| 51 | */ |
| 52 | namespace persisted_variables_unittest { |
| 53 | |
| 54 | using my_testing::Server_initializer; |
| 55 | using std::string; |
| 56 | |
| 57 | class Persisted_variablesTest : public ::testing::Test { |
| 58 | protected: |
| 59 | void SetUp() override { initializer.SetUp(); } |
| 60 | void TearDown() override { initializer.TearDown(); } |
| 61 | THD *thd() const { return initializer.thd(); } |
| 62 | |
| 63 | Server_initializer initializer; |
| 64 | }; |
| 65 | |
| 66 | TEST_F(Persisted_variablesTest, HexEncryption) { |
| 67 | string secret("thisisa32bitlongsecretpassword1"); |
| 68 | const size_t file_key_length = 32; |
| 69 | auto file_key = std::make_unique<unsigned char[]>(file_key_length); |
| 70 | const size_t iv_length = 16; |
| 71 | auto iv = std::make_unique<unsigned char[]>(iv_length); |
| 72 | |
| 73 | Persisted_variables_cache cache; |
| 74 | |
| 75 | ASSERT_TRUE(file_key.get() != nullptr); |
| 76 | ASSERT_TRUE(iv.get() != nullptr); |
| 77 | |
| 78 | ASSERT_FALSE(my_rand_buffer(file_key.get(), file_key_length) || |
| 79 | my_rand_buffer(iv.get(), iv_length)); |
| 80 | |
| 81 | size_t encrypted_key_length = |
| 82 | (file_key_length / MY_AES_BLOCK_SIZE) * MY_AES_BLOCK_SIZE; |
| 83 | |
| 84 | auto encrypted_key = std::make_unique<unsigned char[]>(encrypted_key_length); |
| 85 | |
| 86 | auto error = |
| 87 | my_aes_encrypt(file_key.get(), file_key_length, encrypted_key.get(), |
| 88 | reinterpret_cast<const unsigned char *>(secret.c_str()), |
| 89 | secret.length(), my_aes_256_cbc, iv.get(), false); |
| 90 | |
| 91 | ASSERT_TRUE(error != -1); |
| 92 | ASSERT_TRUE(static_cast<size_t>(error) == encrypted_key_length); |
| 93 | ASSERT_TRUE(file_key_length == encrypted_key_length); |
| 94 | |
| 95 | string pre_ivstring(reinterpret_cast<char *>(iv.get()), iv_length); |
| 96 | string pre_filekeystring(reinterpret_cast<char *>(file_key.get()), |
| 97 | file_key_length); |
| 98 | |
| 99 | string filekeyhex = cache.to_hex(std::string{ |
| 100 | reinterpret_cast<char *>(encrypted_key.get()), encrypted_key_length}); |
| 101 | string ivhex = |
| 102 | cache.to_hex(std::string{reinterpret_cast<char *>(iv.get()), iv_length}); |
| 103 | |
| 104 | string pre_encrypted_key{reinterpret_cast<char *>(encrypted_key.get()), |
| 105 | encrypted_key_length}; |
| 106 | |
| 107 | string filekeystring = cache.from_hex(filekeyhex); |
| 108 | string post_ivstring = cache.from_hex(ivhex); |
| 109 | |
| 110 | ASSERT_TRUE(filekeystring == pre_encrypted_key); |
| 111 | |
| 112 | auto decrypted_key = |
| 113 | std::make_unique<unsigned char[]>(filekeystring.length()); |
| 114 | |
| 115 | error = my_aes_decrypt( |
| 116 | reinterpret_cast<const unsigned char *>(filekeystring.c_str()), |
| 117 | filekeystring.length(), decrypted_key.get(), |
| 118 | reinterpret_cast<const unsigned char *>(secret.c_str()), secret.length(), |
| 119 | my_aes_256_cbc, |
| 120 | reinterpret_cast<const unsigned char *>(post_ivstring.c_str()), false); |
| 121 | |
| 122 | ASSERT_TRUE(error != -1); |
| 123 | ASSERT_TRUE(error == file_key_length); |
| 124 | |
| 125 | string post_filekeystring{reinterpret_cast<char *>(decrypted_key.get()), |
| 126 | file_key_length}; |
| 127 | |
| 128 | ASSERT_TRUE(pre_filekeystring == post_filekeystring); |
| 129 | } |
| 130 | |
| 131 | TEST_F(Persisted_variablesTest, ReadWrite) { |
| 132 | ulonglong timestamp = my_micro_time(); |
| 133 | |
| 134 | /* RW variables */ |
| 135 | st_persist_var rw_variable_1("rw_variable_1", "rw_variable_1_value", |
| 136 | timestamp, "user", "host", false); |
| 137 | st_persist_var rw_variable_2("rw_variable_2", "rw_variable_2_value", |
| 138 | timestamp, "user", "host", false); |
| 139 | st_persist_var rw_variable_3("rw_variable_3", "rw_variable_3_value", |
| 140 | timestamp, "user", "host", false); |
| 141 | st_persist_var rw_variable_4("rw_variable_4", "", timestamp, "user", "host", |
| 142 | true); |
| 143 | |
| 144 | std::vector<st_persist_var> rw_variables_vector; |
| 145 | rw_variables_vector.push_back(rw_variable_1); |
| 146 | rw_variables_vector.push_back(rw_variable_2); |
| 147 | rw_variables_vector.push_back(rw_variable_3); |
| 148 | rw_variables_vector.push_back(rw_variable_4); |
| 149 | |
| 150 | string rw_variables_vector_key("mysql_rw_variables"); |
| 151 | |
| 152 | /* Static variables */ |
| 153 | st_persist_var static_variable_1("static_variable_1", |
| 154 | "static_variable_1_value", timestamp, "user", |
| 155 | "host", false); |
| 156 | st_persist_var static_variable_2("static_variable_2", |
| 157 | "static_variable_2_value", timestamp, "user", |
| 158 | "host", false); |
| 159 | st_persist_var static_variable_3("static_variable_3", |
| 160 | "static_variable_3_value", timestamp, "user", |
| 161 | "host", false); |
| 162 | st_persist_var static_variable_4("static_variable_4", "", timestamp, "user", |
| 163 | "host", true); |
| 164 | |
| 165 | std::map<string, st_persist_var> static_variables_map; |
| 166 | static_variables_map[static_variable_1.key] = static_variable_1; |
| 167 | static_variables_map[static_variable_2.key] = static_variable_2; |
| 168 | static_variables_map[static_variable_3.key] = static_variable_3; |
| 169 | static_variables_map[static_variable_4.key] = static_variable_4; |
| 170 | |
| 171 | string static_variables_map_key("mysql_static_variables"); |
| 172 | |
| 173 | /* Format JSON */ |
| 174 | |
| 175 | auto format_json = [](auto &entry, Json_object §ion_object) -> bool { |
| 176 | const string key_value("Value"); |
| 177 | const string key_metadata("Metadata"); |
| 178 | const string key_timestamp("Timestamp"); |
| 179 | const string key_user("User"); |
| 180 | const string key_host("Host"); |
| 181 | /* |
| 182 | Create metadata array |
| 183 | "Metadata" : { |
| 184 | "Timestamp" : timestamp_value, |
| 185 | "User" : "user_name", |
| 186 | "Host" : "host_name" |
| 187 | } |
| 188 | */ |
| 189 | Json_object object_metadata; |
| 190 | Json_uint value_timestamp(entry.timestamp); |
| 191 | if (object_metadata.add_clone(key_timestamp, &value_timestamp)) return true; |
| 192 | |
| 193 | Json_string value_user(entry.user.c_str()); |
| 194 | if (object_metadata.add_clone(key_user, &value_user)) return true; |
| 195 | |
| 196 | Json_string value_host(entry.host.c_str()); |
| 197 | if (object_metadata.add_clone(key_host, &value_host)) return true; |
| 198 | |
| 199 | /* |
| 200 | Create variable object |
| 201 | |
| 202 | "variable_name" : { |
| 203 | "Value" : "variable_value", |
| 204 | "Metadata" : { |
| 205 | "Timestamp" : timestamp_value, |
| 206 | "User" : "user_name", |
| 207 | "Host" : "host_name" |
| 208 | } |
| 209 | }, |
| 210 | */ |
| 211 | Json_object variable; |
| 212 | |
| 213 | Json_string value_value(entry.value.c_str()); |
| 214 | if (variable.add_clone(key_value, &value_value)) return true; |
| 215 | |
| 216 | if (variable.add_clone(key_metadata, &object_metadata)) return true; |
| 217 | |
| 218 | /* Push object to array */ |
| 219 | if (section_object.add_clone(entry.key, &variable)) return true; |
| 220 | return false; |
| 221 | }; |
| 222 | |
| 223 | auto add_json_object = [](Json_object §ion_object, |
| 224 | Json_object &json_object, |
| 225 | string key_section) -> bool { |
| 226 | if (section_object.cardinality() > 0) { |
| 227 | if (json_object.add_clone(key_section.c_str(), §ion_object)) |
| 228 | return true; |
| 229 | } |
| 230 | return false; |
| 231 | }; |
| 232 | |
| 233 | auto format_vector = [&format_json, &add_json_object]( |
| 234 | std::vector<st_persist_var> §ion, |
| 235 | string key_section, |
| 236 | Json_object &json_object) -> bool { |
| 237 | Json_object section_object; |
| 238 | for (auto &it : section) { |
| 239 | if (format_json(it, section_object)) return true; |
| 240 | } |
| 241 | if (add_json_object(section_object, json_object, key_section)) return true; |
| 242 | return false; |
| 243 | }; |
| 244 | |
| 245 | auto format_map = [&format_json, &add_json_object]( |
| 246 | std::map<string, st_persist_var> §ion, |
| 247 | string key_section, Json_object &json_object) -> bool { |
| 248 | Json_object section_object; |
| 249 | for (auto &it : section) { |
| 250 | if (format_json(it.second, section_object)) return true; |
| 251 | } |
| 252 | if (add_json_object(section_object, json_object, key_section)) return true; |
| 253 | return false; |
| 254 | }; |
| 255 | |
| 256 | Json_object main_json_object; |
| 257 | |
| 258 | ulonglong version = 2; |
| 259 | string key_version("Version"); |
| 260 | Json_uint value_version(2); |
| 261 | main_json_object.add_clone(key_version.c_str(), &value_version); |
| 262 | |
| 263 | ASSERT_TRUE(format_vector(rw_variables_vector, rw_variables_vector_key, |
| 264 | main_json_object) == false); |
| 265 | ASSERT_TRUE(format_map(static_variables_map, static_variables_map_key, |
| 266 | main_json_object) == false); |
| 267 | |
| 268 | /* Extract and Validate */ |
| 269 | |
| 270 | auto validate_version = [](const Json_object &json_object, |
| 271 | const std::string &version_key, |
| 272 | const ulonglong &expected_version) -> bool { |
| 273 | if (json_object.json_type() != enum_json_type::J_OBJECT) return true; |
| 274 | Json_dom *version_dom = json_object.get(version_key); |
| 275 | if (version_dom == nullptr || |
| 276 | version_dom->json_type() != enum_json_type::J_UINT) |
| 277 | return true; |
| 278 | Json_uint *fetched_version = down_cast<Json_uint *>(version_dom); |
| 279 | if (fetched_version->value() != expected_version) return true; |
| 280 | |
| 281 | return false; |
| 282 | }; |
| 283 | |
| 284 | ASSERT_TRUE(validate_version(main_json_object, key_version, version) == |
| 285 | false); |
| 286 | |
| 287 | auto extract_entry = [](const Json_object *json_object, |
| 288 | st_persist_var &output) -> bool { |
| 289 | const string key_value("Value"); |
| 290 | const string key_metadata("Metadata"); |
| 291 | const string key_timestamp("Timestamp"); |
| 292 | const string key_user("User"); |
| 293 | const string key_host("Host"); |
| 294 | if (json_object->json_type() != enum_json_type::J_OBJECT) return true; |
| 295 | |
| 296 | Json_dom *dom = nullptr; |
| 297 | Json_string *value_string = nullptr; |
| 298 | Json_uint *value_uint = nullptr; |
| 299 | |
| 300 | dom = json_object->get(key_value); |
| 301 | if (dom == nullptr || dom->json_type() != enum_json_type::J_STRING) |
| 302 | return true; |
| 303 | value_string = down_cast<Json_string *>(dom); |
| 304 | output.value.assign(value_string->value()); |
| 305 | output.is_null = (output.value.length() == 0); |
| 306 | |
| 307 | dom = json_object->get(key_metadata); |
| 308 | if (dom == nullptr || dom->json_type() != enum_json_type::J_OBJECT) |
| 309 | return true; |
| 310 | Json_object *object_metadata = down_cast<Json_object *>(dom); |
| 311 | |
| 312 | dom = object_metadata->get(key_timestamp); |
| 313 | if (dom == nullptr || dom->json_type() != enum_json_type::J_UINT) |
| 314 | return true; |
| 315 | value_uint = down_cast<Json_uint *>(dom); |
| 316 | output.timestamp = value_uint->value(); |
| 317 | |
| 318 | dom = object_metadata->get(key_user); |
| 319 | if (dom == nullptr || dom->json_type() != enum_json_type::J_STRING) |
| 320 | return true; |
| 321 | value_string = down_cast<Json_string *>(dom); |
| 322 | output.user.assign(value_string->value()); |
| 323 | |
| 324 | dom = object_metadata->get(key_host); |
| 325 | if (dom == nullptr || dom->json_type() != enum_json_type::J_STRING) |
| 326 | return true; |
| 327 | value_string = down_cast<Json_string *>(dom); |
| 328 | output.host.assign(value_string->value()); |
| 329 | |
| 330 | return false; |
| 331 | }; |
| 332 | |
| 333 | auto extract_vector = [&extract_entry]( |
| 334 | const Json_object &json_object, |
| 335 | const std::string vector_key, |
| 336 | std::vector<st_persist_var> &output) -> bool { |
| 337 | if (json_object.json_type() != enum_json_type::J_OBJECT) return true; |
| 338 | Json_dom *vector_dom = json_object.get(vector_key); |
| 339 | /* Not having any entry is fine */ |
| 340 | if (vector_dom == nullptr) return false; |
| 341 | if (vector_dom->json_type() != enum_json_type::J_OBJECT) return true; |
| 342 | Json_object *section_object = down_cast<Json_object *>(vector_dom); |
| 343 | for (auto &it : *section_object) { |
| 344 | /* Ignore problematic entry */ |
| 345 | if (it.second == nullptr || |
| 346 | it.second->json_type() != enum_json_type::J_OBJECT) |
| 347 | continue; |
| 348 | const Json_object *element = |
| 349 | down_cast<const Json_object *>(it.second.get()); |
| 350 | st_persist_var entry; |
| 351 | entry.key = it.first; |
| 352 | /* Ignore problematic entry */ |
| 353 | if (extract_entry(element, entry)) continue; |
| 354 | output.push_back(entry); |
| 355 | } |
| 356 | return false; |
| 357 | }; |
| 358 | |
| 359 | auto extract_map = |
| 360 | [&extract_entry](const Json_object &json_object, |
| 361 | const std::string vector_key, |
| 362 | std::map<std::string, st_persist_var> &output) -> bool { |
| 363 | if (json_object.json_type() != enum_json_type::J_OBJECT) return true; |
| 364 | Json_dom *map_dom = json_object.get(vector_key); |
| 365 | /* Not having any entry is fine */ |
| 366 | if (map_dom == nullptr) return false; |
| 367 | if (map_dom->json_type() != enum_json_type::J_OBJECT) return true; |
| 368 | Json_object *section_object = down_cast<Json_object *>(map_dom); |
| 369 | for (auto &it : *section_object) { |
| 370 | /* Ignore problematic entry */ |
| 371 | if (it.second == nullptr || |
| 372 | it.second->json_type() != enum_json_type::J_OBJECT) |
| 373 | continue; |
| 374 | const Json_object *element = |
| 375 | down_cast<const Json_object *>(it.second.get()); |
| 376 | st_persist_var entry; |
| 377 | entry.key = it.first; |
| 378 | /* Ignore problematic entry */ |
| 379 | if (extract_entry(element, entry)) continue; |
| 380 | output[entry.key] = entry; |
| 381 | } |
| 382 | return false; |
| 383 | }; |
| 384 | |
| 385 | auto compare = [](st_persist_var &lhs, st_persist_var &rhs) -> bool { |
| 386 | return (lhs.key == rhs.key && lhs.value == rhs.value && |
| 387 | lhs.timestamp == rhs.timestamp && lhs.user == rhs.user && |
| 388 | lhs.host == rhs.host); |
| 389 | }; |
| 390 | |
| 391 | std::vector<st_persist_var> output_rw_variables_vector; |
| 392 | ASSERT_TRUE(extract_vector(main_json_object, rw_variables_vector_key, |
| 393 | output_rw_variables_vector) == false); |
| 394 | |
| 395 | ASSERT_TRUE(rw_variables_vector.size() == output_rw_variables_vector.size()); |
| 396 | for (size_t index = 0; index < output_rw_variables_vector.size(); ++index) { |
| 397 | auto &input = rw_variables_vector[index]; |
| 398 | auto &output = output_rw_variables_vector[index]; |
| 399 | ASSERT_TRUE(compare(input, output)); |
| 400 | } |
| 401 | |
| 402 | std::map<std::string, st_persist_var> output_static_variables_map; |
| 403 | ASSERT_TRUE(extract_map(main_json_object, static_variables_map_key, |
| 404 | output_static_variables_map) == false); |
| 405 | |
| 406 | ASSERT_TRUE(static_variables_map.size() == |
| 407 | output_static_variables_map.size()); |
| 408 | |
| 409 | for (auto it_input = static_variables_map.cbegin(), |
| 410 | it_output = output_static_variables_map.cbegin(); |
| 411 | it_input != static_variables_map.cend() && |
| 412 | it_output != output_static_variables_map.cend(); |
| 413 | ++it_input, ++it_output) { |
| 414 | auto input = it_input->second; |
| 415 | auto output = it_output->second; |
| 416 | ASSERT_TRUE(compare(input, output)); |
| 417 | } |
| 418 | } |
| 419 | |
| 420 | } // namespace persisted_variables_unittest |