Debian Codegraph @

unittest/gunit/xplugin/xpl/user_password_verification_t.cc

raw symbols SCIP
1 /* Copyright (c) 2017, 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 "mysql_com.h" // NOLINT(build/include_subdir)
27 #include "sha1.h" // for SHA1_HASH_SIZE NOLINT(build/include_subdir)
28
29 #include "plugin/x/src/cache_based_verification.h"
30 #include "plugin/x/src/native_plain_verification.h"
31 #include "plugin/x/src/native_verification.h"
32 #include "plugin/x/src/sha256_password_cache.h"
33 #include "plugin/x/src/sha256_plain_verification.h"
34 #include "plugin/x/src/sha2_plain_verification.h"
35 #include "unittest/gunit/xplugin/xpl/mock/sha256_password_cache.h"
36
37 namespace xpl {
38 namespace test {
39
40 using ::testing::_;
41 using ::testing::Return;
42 using ::testing::ReturnRef;
43
44 class User_password_verification : public ::testing::Test {
45 public:
46 void SetUp() override {
47 m_cache_mock.reset(new mock::Sha256_password_cache());
48 m_cached_value = {std::begin(CACHED_VALUE_TABLE),
49 std::end(CACHED_VALUE_TABLE)};
50 }
51
52 std::unique_ptr<mock::Sha256_password_cache> m_cache_mock;
53
54 const char *const EMPTY = "";
55 const char *const EXPECTED_NATIVE_HASH =
56 "*BF201911C951DCC0264E2C7577977E0A3EF06695";
57 const char *const EXPECTED_SHA256_HASH =
58 "$5$1S> j#F2}Vz3yqu`fC8X$HrURSrHutEhr6orwomWpNiRquOS/xy9DzQFj5TuVHn0";
59 const char *const WRONG_PASSWD = "ALA_MA_KACA";
60 const char *const GOOD_PASSWD = "ALA_MA_KOTA";
61
62 const std::string SHA256_MEMORY_CLIENT_STRING =
63 "9F85587C3992CA48B17B727FAAC84299093B40E83797497980A58931342DA5BE";
64 const std::string MADE_UP_SALT = std::string(20, 'x');
65 const unsigned char CACHED_VALUE_TABLE[64] = {
66 0x22, 0xf5, 0xa3, 0xa4, 0x64, 0x7f, 0x33, 0xe5, 0x3a, 0xe8, 0x2c,
67 0x18, 0x1a, 0x51, 0x33, 0xe6, 0x2c, 0x47, 0x56, 0x21, 0x65, 0xb0,
68 0x62, 0xd0, 0x37, 0x7b, 0x1e, 0x3c, 0x79, 0x57, 0xc2, 0xde};
69 std::string m_cached_value;
70 };
71
72 TEST_F(User_password_verification, native_plain_verification_get_salt) {
73 Native_plain_verification verificator{m_cache_mock.get()};
74 ASSERT_STREQ(EMPTY, verificator.get_salt().c_str());
75 }
76
77 TEST_F(User_password_verification, native_plain_verification_pass) {
78 EXPECT_CALL(*m_cache_mock.get(), contains("user", "host", GOOD_PASSWD));
79 EXPECT_CALL(*m_cache_mock.get(), upsert("user", "host", GOOD_PASSWD));
80 Native_plain_verification verificator{m_cache_mock.get()};
81 ASSERT_TRUE(verificator.verify_authentication_string(
82 "user", "host", GOOD_PASSWD, EXPECTED_NATIVE_HASH));
83 }
84
85 TEST_F(User_password_verification, native_plain_verification_fail) {
86 EXPECT_CALL(*m_cache_mock.get(), contains("user", "host", WRONG_PASSWD));
87 EXPECT_CALL(*m_cache_mock.get(), upsert(_, _, _)).Times(0);
88 Native_plain_verification verificator{m_cache_mock.get()};
89 ASSERT_FALSE(verificator.verify_authentication_string(
90 "user", "host", WRONG_PASSWD, EXPECTED_NATIVE_HASH));
91 }
92
93 TEST_F(User_password_verification, native_verification_get_salt) {
94 Native_verification verificator{m_cache_mock.get()};
95 ASSERT_STRNE(EMPTY, verificator.get_salt().c_str());
96 }
97
98 std::string get_hash(const std::string &salt, const std::string &user_string) {
99 char scrambled[SCRAMBLE_LENGTH + 1] = {0};
100 char hash[2 * SHA1_HASH_SIZE + 2] = {0};
101 ::scramble(scrambled, salt.c_str(), user_string.c_str());
102 ::make_password_from_salt(hash, reinterpret_cast<const uint8_t *>(scrambled));
103 return hash;
104 }
105
106 TEST_F(User_password_verification, native_verification_pass) {
107 Native_verification verificator{m_cache_mock.get()};
108 EXPECT_CALL(*m_cache_mock.get(), contains(_, _, _)).Times(0);
109 EXPECT_CALL(*m_cache_mock.get(), upsert(_, _, _)).Times(0);
110 ASSERT_TRUE(verificator.verify_authentication_string(
111 "", "", get_hash(verificator.get_salt(), GOOD_PASSWD),
112 EXPECTED_NATIVE_HASH));
113 }
114
115 TEST_F(User_password_verification, native_verification_fail) {
116 Native_verification verificator{m_cache_mock.get()};
117 EXPECT_CALL(*m_cache_mock.get(), contains(_, _, _)).Times(0);
118 EXPECT_CALL(*m_cache_mock.get(), upsert(_, _, _)).Times(0);
119 ASSERT_FALSE(verificator.verify_authentication_string(
120 "", "", get_hash(verificator.get_salt(), WRONG_PASSWD),
121 EXPECTED_NATIVE_HASH));
122 }
123
124 TEST_F(User_password_verification, sha256_plain_verification_get_salt) {
125 Sha256_plain_verification verificator{m_cache_mock.get()};
126 ASSERT_STREQ(EMPTY, verificator.get_salt().c_str());
127 }
128
129 TEST_F(User_password_verification, sha256_plain_verification_pass) {
130 EXPECT_CALL(*m_cache_mock.get(), contains("user", "host", GOOD_PASSWD));
131 EXPECT_CALL(*m_cache_mock.get(), upsert("user", "host", GOOD_PASSWD));
132 Sha256_plain_verification verificator{m_cache_mock.get()};
133 ASSERT_TRUE(verificator.verify_authentication_string(
134 "user", "host", GOOD_PASSWD, EXPECTED_SHA256_HASH));
135 }
136
137 TEST_F(User_password_verification, sha256_plain_verification_fail) {
138 EXPECT_CALL(*m_cache_mock.get(), contains("user", "host", WRONG_PASSWD));
139 EXPECT_CALL(*m_cache_mock.get(), upsert(_, _, _)).Times(0);
140 Sha256_plain_verification verificator{m_cache_mock.get()};
141 ASSERT_FALSE(verificator.verify_authentication_string(
142 "user", "host", WRONG_PASSWD, EXPECTED_SHA256_HASH));
143 }
144
145 TEST_F(User_password_verification, sha256_memory_verification_get_salt) {
146 Cache_based_verification verificator{m_cache_mock.get()};
147 ASSERT_STRNE(EMPTY, verificator.get_salt().c_str());
148 }
149
150 TEST_F(User_password_verification, sha256_memory_verification_pass) {
151 EXPECT_CALL(*m_cache_mock.get(), get_entry("user", "host"))
152 .WillOnce(Return(std::pair<bool, std::string>{true, m_cached_value}));
153 mock::Cache_based_verification verificator{m_cache_mock.get()};
154 EXPECT_CALL(verificator, get_salt()).WillRepeatedly(ReturnRef(MADE_UP_SALT));
155 ASSERT_TRUE(verificator.verify_authentication_string(
156 "user", "host", SHA256_MEMORY_CLIENT_STRING, ""));
157 }
158
159 TEST_F(User_password_verification, sha256_memory_verification_no_entry) {
160 EXPECT_CALL(*m_cache_mock.get(), get_entry("user", "host"))
161 .WillOnce(Return(std::pair<bool, std::string>{false, ""}));
162 mock::Cache_based_verification verificator{m_cache_mock.get()};
163 EXPECT_CALL(verificator, get_salt()).Times(0);
164 ASSERT_FALSE(verificator.verify_authentication_string(
165 "user", "host", SHA256_MEMORY_CLIENT_STRING, ""));
166 }
167
168 TEST_F(User_password_verification, sha256_memory_verification_fail) {
169 const std::string bogus_entry(32, 'z');
170 EXPECT_CALL(*m_cache_mock.get(), get_entry("user", "host"))
171 .WillOnce(Return(std::pair<bool, std::string>{true, bogus_entry}));
172 mock::Cache_based_verification verificator{m_cache_mock.get()};
173 EXPECT_CALL(verificator, get_salt()).WillRepeatedly(ReturnRef(MADE_UP_SALT));
174 ASSERT_FALSE(verificator.verify_authentication_string(
175 "user", "host", SHA256_MEMORY_CLIENT_STRING, ""));
176 }
177
178 } // namespace test
179 } // namespace xpl