| 1 | /* Copyright (c) 2021, 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 "backend_mem.h" // Memory keyring backend |
| 27 | |
| 28 | #include "components/keyrings/common/operations/operations.h" |
| 29 | |
| 30 | namespace operations_unittest { |
| 31 | |
| 32 | class KeyringCommonOperations_test : public ::testing::Test {}; |
| 33 | |
| 34 | using keyring_common::data::Data; |
| 35 | using keyring_common::data::Type; |
| 36 | using keyring_common::iterator::Iterator; |
| 37 | using keyring_common::meta::Metadata; |
| 38 | using keyring_common::operations::Keyring_operations; |
| 39 | |
| 40 | using keyring_common_unit::Memory_backend; |
| 41 | |
| 42 | TEST_F(KeyringCommonOperations_test, OperationsTestWithoutCache) { |
| 43 | Metadata metadata1("key1", "[email protected]"), metadata2("key2", "[email protected]"), |
| 44 | metadata3("key3", "[email protected]"); |
| 45 | Metadata metadata4("key1", "[email protected]"), metadata5("key2", "[email protected]"), |
| 46 | metadata6("key3", "[email protected]"); |
| 47 | Metadata metadata7("key1", "[email protected]"), metadata8("key2", "[email protected]"), |
| 48 | metadata9("key3", "[email protected]"); |
| 49 | Data data1("Data1", "Type1"), data2("Data2", "Type2"), |
| 50 | data3("Data3", "Type3"); |
| 51 | |
| 52 | std::unique_ptr<Memory_backend> memory_backend = |
| 53 | std::make_unique<Memory_backend>(); |
| 54 | Keyring_operations<Memory_backend> keyring_operations( |
| 55 | false, memory_backend.release()); |
| 56 | |
| 57 | /* Store */ |
| 58 | ASSERT_FALSE(keyring_operations.store(metadata1, data1)); |
| 59 | ASSERT_FALSE(keyring_operations.store(metadata4, data1)); |
| 60 | ASSERT_FALSE(keyring_operations.store(metadata7, data1)); |
| 61 | |
| 62 | ASSERT_FALSE(keyring_operations.store(metadata2, data2)); |
| 63 | ASSERT_FALSE(keyring_operations.store(metadata5, data2)); |
| 64 | ASSERT_FALSE(keyring_operations.store(metadata8, data2)); |
| 65 | |
| 66 | ASSERT_FALSE(keyring_operations.store(metadata3, data3)); |
| 67 | ASSERT_FALSE(keyring_operations.store(metadata6, data3)); |
| 68 | ASSERT_FALSE(keyring_operations.store(metadata9, data3)); |
| 69 | |
| 70 | ASSERT_TRUE(keyring_operations.store(metadata2, data2)); |
| 71 | ASSERT_TRUE(keyring_operations.store(metadata4, data2)); |
| 72 | ASSERT_TRUE(keyring_operations.store(metadata8, data2)); |
| 73 | |
| 74 | ASSERT_EQ(keyring_operations.keyring_size(), 9); |
| 75 | |
| 76 | // data does not matter |
| 77 | ASSERT_TRUE(keyring_operations.store(metadata2, data1)); |
| 78 | ASSERT_TRUE(keyring_operations.store(metadata4, data1)); |
| 79 | ASSERT_TRUE(keyring_operations.store(metadata8, data1)); |
| 80 | |
| 81 | /* Erase */ |
| 82 | ASSERT_FALSE(keyring_operations.erase(metadata1)); |
| 83 | ASSERT_FALSE(keyring_operations.erase(metadata5)); |
| 84 | ASSERT_FALSE(keyring_operations.erase(metadata9)); |
| 85 | |
| 86 | ASSERT_EQ(keyring_operations.keyring_size(), 6); |
| 87 | |
| 88 | ASSERT_TRUE(keyring_operations.erase(metadata1)); |
| 89 | ASSERT_TRUE(keyring_operations.erase(metadata5)); |
| 90 | ASSERT_TRUE(keyring_operations.erase(metadata9)); |
| 91 | |
| 92 | ASSERT_EQ(keyring_operations.keyring_size(), 6); |
| 93 | |
| 94 | /* Search */ |
| 95 | Data returned_data1, returned_data2, returned_data3; |
| 96 | |
| 97 | ASSERT_FALSE(keyring_operations.get(metadata4, returned_data1)); |
| 98 | ASSERT_FALSE(keyring_operations.get(metadata8, returned_data2)); |
| 99 | ASSERT_FALSE(keyring_operations.get(metadata6, returned_data3)); |
| 100 | |
| 101 | ASSERT_TRUE(returned_data1 == data1); |
| 102 | ASSERT_TRUE(returned_data2 == data2); |
| 103 | ASSERT_TRUE(returned_data3 == data3); |
| 104 | |
| 105 | ASSERT_TRUE(keyring_operations.get(metadata1, returned_data1)); |
| 106 | ASSERT_TRUE(keyring_operations.get(metadata5, returned_data2)); |
| 107 | ASSERT_TRUE(keyring_operations.get(metadata9, returned_data3)); |
| 108 | |
| 109 | /* Read iterators */ |
| 110 | std::unique_ptr<Iterator<Data>> read_it1, read_it2, read_it3; |
| 111 | ASSERT_FALSE(keyring_operations.init_read_iterator(read_it1, metadata4)); |
| 112 | ASSERT_FALSE(keyring_operations.init_read_iterator(read_it2, metadata8)); |
| 113 | ASSERT_FALSE(keyring_operations.init_read_iterator(read_it3, metadata6)); |
| 114 | |
| 115 | Metadata read_metadata1, read_metadata2, read_metadata3; |
| 116 | ASSERT_FALSE(keyring_operations.get_iterator_data(read_it1, read_metadata1, |
| 117 | returned_data1)); |
| 118 | ASSERT_FALSE(keyring_operations.get_iterator_data(read_it2, read_metadata2, |
| 119 | returned_data2)); |
| 120 | ASSERT_FALSE(keyring_operations.get_iterator_data(read_it3, read_metadata3, |
| 121 | returned_data3)); |
| 122 | |
| 123 | ASSERT_TRUE(read_metadata1 == metadata4); |
| 124 | ASSERT_TRUE(read_metadata2 == metadata8); |
| 125 | ASSERT_TRUE(read_metadata3 == metadata6); |
| 126 | |
| 127 | ASSERT_TRUE(returned_data1 == data1); |
| 128 | ASSERT_TRUE(returned_data2 == data2); |
| 129 | ASSERT_TRUE(returned_data3 == data3); |
| 130 | |
| 131 | keyring_operations.deinit_forward_iterator(read_it1); |
| 132 | keyring_operations.deinit_forward_iterator(read_it2); |
| 133 | keyring_operations.deinit_forward_iterator(read_it3); |
| 134 | ASSERT_TRUE(read_it1.get() == nullptr); |
| 135 | ASSERT_TRUE(read_it2.get() == nullptr); |
| 136 | ASSERT_TRUE(read_it3.get() == nullptr); |
| 137 | |
| 138 | /* Generate */ |
| 139 | ASSERT_FALSE(keyring_operations.generate(metadata1, "Type1", 8)); |
| 140 | ASSERT_FALSE(keyring_operations.generate(metadata5, "Type2", 16)); |
| 141 | ASSERT_FALSE(keyring_operations.generate(metadata9, "Type3", 32)); |
| 142 | |
| 143 | ASSERT_TRUE(keyring_operations.keyring_size() == 9); |
| 144 | |
| 145 | ASSERT_FALSE(keyring_operations.get(metadata1, returned_data1)); |
| 146 | ASSERT_FALSE(keyring_operations.get(metadata5, returned_data2)); |
| 147 | ASSERT_FALSE(keyring_operations.get(metadata9, returned_data3)); |
| 148 | |
| 149 | ASSERT_TRUE(returned_data1.get_data().valid() && |
| 150 | returned_data1.get_data().type() == "Type1"); |
| 151 | ASSERT_TRUE(returned_data2.get_data().valid() && |
| 152 | returned_data2.get_data().type() == "Type2"); |
| 153 | ASSERT_TRUE(returned_data3.get_data().valid() && |
| 154 | returned_data3.get_data().type() == "Type3"); |
| 155 | |
| 156 | ASSERT_FALSE(keyring_operations.erase(metadata1)); |
| 157 | ASSERT_FALSE(keyring_operations.erase(metadata5)); |
| 158 | ASSERT_FALSE(keyring_operations.erase(metadata9)); |
| 159 | |
| 160 | ASSERT_TRUE(keyring_operations.keyring_size() == 6); |
| 161 | |
| 162 | /* Iterator */ |
| 163 | std::unique_ptr<Iterator<Data>> it; |
| 164 | ASSERT_FALSE(keyring_operations.init_forward_iterator(it, false)); |
| 165 | Metadata returned_metadata; |
| 166 | Data returned_data; |
| 167 | |
| 168 | for (size_t i = 0; i < keyring_operations.keyring_size(); ++i) { |
| 169 | ASSERT_FALSE(keyring_operations.get_iterator_data(it, returned_metadata, |
| 170 | returned_data)); |
| 171 | keyring_operations.next(it); |
| 172 | } |
| 173 | keyring_operations.deinit_forward_iterator(it); |
| 174 | ASSERT_TRUE(it.get() == nullptr); |
| 175 | |
| 176 | /* Mix operations */ |
| 177 | ASSERT_FALSE(keyring_operations.store(metadata1, data1)); |
| 178 | ASSERT_FALSE(keyring_operations.store(metadata5, data2)); |
| 179 | ASSERT_FALSE(keyring_operations.store(metadata9, data3)); |
| 180 | |
| 181 | ASSERT_EQ(keyring_operations.keyring_size(), 9); |
| 182 | |
| 183 | ASSERT_FALSE(keyring_operations.erase(metadata2)); |
| 184 | ASSERT_FALSE(keyring_operations.get(metadata1, returned_data1)); |
| 185 | ASSERT_TRUE(returned_data1 == data1); |
| 186 | ASSERT_FALSE(keyring_operations.get(metadata3, returned_data3)); |
| 187 | ASSERT_TRUE(returned_data3 == data3); |
| 188 | ASSERT_FALSE(keyring_operations.erase(metadata6)); |
| 189 | ASSERT_FALSE(keyring_operations.store(metadata2, data2)); |
| 190 | ASSERT_FALSE(keyring_operations.erase(metadata7)); |
| 191 | ASSERT_TRUE(keyring_operations.get(metadata6, returned_data)); |
| 192 | ASSERT_FALSE(keyring_operations.generate(metadata6, "Type3", 8)); |
| 193 | |
| 194 | ASSERT_EQ(keyring_operations.keyring_size(), 8); |
| 195 | |
| 196 | ASSERT_FALSE(keyring_operations.init_forward_iterator(it, false)); |
| 197 | ASSERT_FALSE(keyring_operations.get_iterator_data(it, returned_metadata, |
| 198 | returned_data)); |
| 199 | ASSERT_FALSE(keyring_operations.next(it)); |
| 200 | |
| 201 | ASSERT_FALSE(keyring_operations.store(metadata7, data3)); |
| 202 | ASSERT_EQ(keyring_operations.keyring_size(), 9); |
| 203 | |
| 204 | ASSERT_TRUE(keyring_operations.get_iterator_data(it, returned_metadata, |
| 205 | returned_data)); |
| 206 | ASSERT_TRUE(keyring_operations.next(it)); |
| 207 | |
| 208 | keyring_operations.deinit_forward_iterator(it); |
| 209 | ASSERT_TRUE(it.get() == nullptr); |
| 210 | } |
| 211 | |
| 212 | } // namespace operations_unittest |