| 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 <gmock/gmock.h> |
| 25 | #include <gtest/gtest.h> |
| 26 | |
| 27 | #include "plugin/x/src/sha256_password_cache.h" |
| 28 | |
| 29 | namespace xpl { |
| 30 | |
| 31 | namespace test { |
| 32 | |
| 33 | using ::testing::_; |
| 34 | using ::testing::DoAll; |
| 35 | using ::testing::Expectation; |
| 36 | using ::testing::Return; |
| 37 | using ::testing::ReturnPointee; |
| 38 | using ::testing::SetArrayArgument; |
| 39 | using ::testing::SetErrnoAndReturn; |
| 40 | using ::testing::StrictMock; |
| 41 | |
| 42 | class Sha256_cache_test_suite : public ::testing::Test { |
| 43 | public: |
| 44 | void SetUp() override { m_cache.enable(); } |
| 45 | |
| 46 | Sha256_cache_test_suite() = default; |
| 47 | |
| 48 | xpl::SHA256_password_cache m_cache; |
| 49 | }; |
| 50 | |
| 51 | TEST_F(Sha256_cache_test_suite, add_entry_cache_enabled) { |
| 52 | ASSERT_TRUE(m_cache.size() == 0); |
| 53 | m_cache.upsert("user1", "host1", "value1"); |
| 54 | m_cache.upsert("user2", "host2", "value2"); |
| 55 | ASSERT_TRUE(m_cache.size() == 2); |
| 56 | } |
| 57 | |
| 58 | TEST_F(Sha256_cache_test_suite, add_entry_cache_disabled) { |
| 59 | ASSERT_TRUE(m_cache.size() == 0); |
| 60 | m_cache.upsert("user1", "host1", "value1"); |
| 61 | ASSERT_TRUE(m_cache.size() == 1); |
| 62 | m_cache.disable(); |
| 63 | ASSERT_TRUE(m_cache.size() == 0); |
| 64 | m_cache.upsert("user2", "host2", "value2"); |
| 65 | m_cache.upsert("user3", "host3", "value3"); |
| 66 | ASSERT_TRUE(m_cache.size() == 0); |
| 67 | } |
| 68 | |
| 69 | TEST_F(Sha256_cache_test_suite, update_entry) { |
| 70 | m_cache.upsert("user", "host", "value1"); |
| 71 | m_cache.upsert("user", "host", "value2"); |
| 72 | ASSERT_TRUE(m_cache.size() == 1); |
| 73 | } |
| 74 | |
| 75 | TEST_F(Sha256_cache_test_suite, get_existing_entry) { |
| 76 | m_cache.upsert("user1", "host1", "value1"); |
| 77 | auto optional_entry = m_cache.get_entry("user1", "host1"); |
| 78 | ASSERT_TRUE(optional_entry.first); |
| 79 | ASSERT_FALSE(optional_entry.second.empty()); |
| 80 | } |
| 81 | |
| 82 | TEST_F(Sha256_cache_test_suite, get_nonexistent_entry) { |
| 83 | m_cache.upsert("user1", "host1", "value1"); |
| 84 | auto optional_entry = m_cache.get_entry("bogus_user", "host1"); |
| 85 | ASSERT_FALSE(optional_entry.first); |
| 86 | ASSERT_TRUE(optional_entry.second.empty()); |
| 87 | optional_entry = m_cache.get_entry("user1", "bogus host"); |
| 88 | ASSERT_FALSE(optional_entry.first); |
| 89 | ASSERT_TRUE(optional_entry.second.empty()); |
| 90 | } |
| 91 | |
| 92 | TEST_F(Sha256_cache_test_suite, get_entry_cache_disabled) { |
| 93 | m_cache.upsert("user1", "host1", "value1"); |
| 94 | m_cache.disable(); |
| 95 | auto optional_entry = m_cache.get_entry("user1", "host1"); |
| 96 | ASSERT_FALSE(optional_entry.first); |
| 97 | ASSERT_TRUE(optional_entry.second.empty()); |
| 98 | } |
| 99 | |
| 100 | TEST_F(Sha256_cache_test_suite, find_entry) { |
| 101 | m_cache.upsert("user1", "host1", "value1"); |
| 102 | EXPECT_TRUE(m_cache.contains("user1", "host1", "value1")); |
| 103 | EXPECT_FALSE(m_cache.contains("bogus user", "host1", "value1")); |
| 104 | EXPECT_FALSE(m_cache.contains("user1", "bogus host", "value1")); |
| 105 | EXPECT_FALSE(m_cache.contains("user1", "host1", "bogus value")); |
| 106 | } |
| 107 | |
| 108 | TEST_F(Sha256_cache_test_suite, find_entry_cache_disabled) { |
| 109 | m_cache.upsert("user1", "host1", "value1"); |
| 110 | m_cache.disable(); |
| 111 | EXPECT_FALSE(m_cache.contains("user1", "host1", "value1")); |
| 112 | EXPECT_FALSE(m_cache.contains("bogus user", "host1", "value1")); |
| 113 | EXPECT_FALSE(m_cache.contains("user1", "bogus host", "value1")); |
| 114 | EXPECT_FALSE(m_cache.contains("user1", "host1", "bogus value")); |
| 115 | } |
| 116 | |
| 117 | TEST_F(Sha256_cache_test_suite, check_entry_hashing) { |
| 118 | m_cache.upsert("user1", "host1", "value1"); |
| 119 | auto optional_entry = m_cache.get_entry("user1", "host1"); |
| 120 | ASSERT_TRUE(optional_entry.first); |
| 121 | ASSERT_NE("value1", optional_entry.second); |
| 122 | } |
| 123 | |
| 124 | TEST_F(Sha256_cache_test_suite, remove_entry) { |
| 125 | m_cache.upsert("user1", "host1", "value1"); |
| 126 | m_cache.upsert("user2", "host2", "value2"); |
| 127 | ASSERT_TRUE(m_cache.size() == 2); |
| 128 | m_cache.remove("user1", "host1"); |
| 129 | ASSERT_TRUE(m_cache.size() == 1); |
| 130 | } |
| 131 | |
| 132 | TEST_F(Sha256_cache_test_suite, clear_cache) { |
| 133 | m_cache.upsert("user1", "host1", "value1"); |
| 134 | m_cache.upsert("user2", "host2", "value2"); |
| 135 | ASSERT_TRUE(m_cache.size() == 2); |
| 136 | m_cache.clear(); |
| 137 | ASSERT_TRUE(m_cache.size() == 0); |
| 138 | } |
| 139 | |
| 140 | } // namespace test |
| 141 | } // namespace xpl |