| 1 | /* Copyright (c) 2019, 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 "my_config.h" |
| 25 | |
| 26 | #include <algorithm> |
| 27 | #include <cstdlib> |
| 28 | #include <map> |
| 29 | #include <set> |
| 30 | #include <string> |
| 31 | |
| 32 | #include <gtest/gtest.h> |
| 33 | |
| 34 | #include "lex_string.h" |
| 35 | #include "m_string.h" |
| 36 | #include "my_inttypes.h" |
| 37 | #include "sql/auth/auth_common.h" |
| 38 | #include "sql/auth/auth_utility.h" |
| 39 | #include "sql/auth/sql_auth_cache.h" |
| 40 | #include "sql/auth/sql_authentication.h" |
| 41 | #include "sql/sql_class.h" |
| 42 | #include "unittest/gunit/test_utils.h" |
| 43 | |
| 44 | namespace decoy_user_unittest { |
| 45 | using my_testing::Server_initializer; |
| 46 | using std::max; |
| 47 | using std::rand; |
| 48 | |
| 49 | class Decoy_user_Test : public ::testing::Test { |
| 50 | protected: |
| 51 | void SetUp() override { initializer.SetUp(); } |
| 52 | void TearDown() override { initializer.TearDown(); } |
| 53 | |
| 54 | THD *get_thd() { return initializer.thd(); } |
| 55 | |
| 56 | Server_initializer initializer; |
| 57 | }; |
| 58 | |
| 59 | std::set<Auth_id> unknown_authids; |
| 60 | const size_t unknown_authids_size = 1000; |
| 61 | |
| 62 | void generate_random() { |
| 63 | unknown_authids.clear(); |
| 64 | |
| 65 | /* Random user and host of length size */ |
| 66 | auto get_random_account = [](size_t length) -> Auth_id { |
| 67 | const char chars[] = |
| 68 | "QWERTYUIOPASDFGHJKLZXCVBNM" |
| 69 | "qwertyuiopasdfghjklzxcvbnm"; |
| 70 | const size_t limit = (sizeof(chars) - 1); |
| 71 | |
| 72 | std::string user, host; |
| 73 | for (size_t i = 0; i < length; ++i) user.push_back(chars[rand() % limit]); |
| 74 | for (size_t i = 0; i < length; ++i) host.push_back(chars[rand() % limit]); |
| 75 | |
| 76 | return (Auth_id(user, host)); |
| 77 | }; |
| 78 | |
| 79 | while (unknown_authids.size() < unknown_authids_size) { |
| 80 | unknown_authids.insert(get_random_account(max(5, rand() % 16))); |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | TEST_F(Decoy_user_Test, DecoyUserConsistency) { |
| 85 | THD *thd = get_thd(); |
| 86 | ACL_USER *decoy = nullptr; |
| 87 | |
| 88 | unknown_accounts = new Map_with_rw_lock<Auth_id, uint>(0); |
| 89 | |
| 90 | const LEX_CSTRING users[] = {{STRING_WITH_LEN("foo")}, |
| 91 | {STRING_WITH_LEN("baz")}, |
| 92 | {STRING_WITH_LEN("quux")}}; |
| 93 | const LEX_CSTRING hosts[] = {{STRING_WITH_LEN("bar")}, |
| 94 | {STRING_WITH_LEN("qux")}, |
| 95 | {STRING_WITH_LEN("quuz")}}; |
| 96 | |
| 97 | for (auto i = 0; i < 3; ++i) { |
| 98 | for (auto j = 0; j < 3; ++j) { |
| 99 | decoy = nullptr; |
| 100 | decoy = decoy_user(users[i], hosts[j], thd->mem_root, &thd->rand, true); |
| 101 | std::string first(decoy->plugin.str, decoy->plugin.length); |
| 102 | decoy = decoy_user(users[i], hosts[j], thd->mem_root, &thd->rand, true); |
| 103 | std::string second(decoy->plugin.str, decoy->plugin.length); |
| 104 | EXPECT_FALSE(first.compare(second)); |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | EXPECT_TRUE(unknown_accounts->size() == 9); |
| 109 | |
| 110 | delete unknown_accounts; |
| 111 | } |
| 112 | |
| 113 | TEST_F(Decoy_user_Test, DecoyUserRandomness) { |
| 114 | THD *thd = get_thd(); |
| 115 | std::map<std::string, uint> plugin_hits; |
| 116 | uint hit_roof = 4; |
| 117 | uint trials = 0; |
| 118 | uint threshold = 500; |
| 119 | ACL_USER *decoy = nullptr; |
| 120 | |
| 121 | unknown_accounts = new Map_with_rw_lock<Auth_id, uint>(0); |
| 122 | |
| 123 | /* initialize the map */ |
| 124 | for (uint a = 0; a < (uint)PLUGIN_LAST; ++a) { |
| 125 | std::string plugin( |
| 126 | Cached_authentication_plugins::cached_plugins_names[a].str, |
| 127 | Cached_authentication_plugins::cached_plugins_names[a].length); |
| 128 | plugin_hits[plugin]; /* operator [] initializes entry with 0 if missing */ |
| 129 | } |
| 130 | |
| 131 | generate_random(); |
| 132 | |
| 133 | while (trials < hit_roof) { |
| 134 | for (auto &element : unknown_authids) { |
| 135 | decoy = nullptr; |
| 136 | LEX_CSTRING user; |
| 137 | LEX_CSTRING host; |
| 138 | user.str = element.user().c_str(); |
| 139 | user.length = element.user().length(); |
| 140 | host.str = element.host().c_str(); |
| 141 | host.length = element.host().length(); |
| 142 | |
| 143 | decoy = decoy_user(user, host, thd->mem_root, &thd->rand, true); |
| 144 | std::string hit(decoy->plugin.str, decoy->plugin.length); |
| 145 | plugin_hits[hit]++; |
| 146 | } |
| 147 | |
| 148 | ++trials; |
| 149 | } |
| 150 | |
| 151 | for (auto &entry : plugin_hits) { |
| 152 | EXPECT_TRUE(entry.second >= threshold); |
| 153 | } |
| 154 | |
| 155 | delete unknown_accounts; |
| 156 | } |
| 157 | |
| 158 | } // namespace decoy_user_unittest |