| 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 "components/keyrings/common/data/data_extension.h" |
| 27 | #include "components/keyrings/common/memstore/iterator.h" |
| 28 | |
| 29 | namespace iterator_unittest { |
| 30 | |
| 31 | using keyring_common::cache::Datacache; |
| 32 | using keyring_common::data::Data; |
| 33 | using keyring_common::data::Data_extension; |
| 34 | using keyring_common::iterator::Iterator; |
| 35 | using keyring_common::meta::Metadata; |
| 36 | |
| 37 | class KeyringCommonIterator_test : public ::testing::Test {}; |
| 38 | |
| 39 | class Dummy_extension { |
| 40 | public: |
| 41 | Dummy_extension(std::string ext_data) : ext_data_(ext_data) {} |
| 42 | Dummy_extension() : Dummy_extension(std::string{""}) {} |
| 43 | std::string ext_data() const { return ext_data_; } |
| 44 | |
| 45 | private: |
| 46 | std::string ext_data_; |
| 47 | }; |
| 48 | |
| 49 | TEST_F(KeyringCommonIterator_test, IteratorTest) { |
| 50 | Metadata metadata1("key1", "[email protected]"), metadata2("key2", "[email protected]"), |
| 51 | metadata3("key2", "[email protected]"), metadata4("key1", "[email protected]"); |
| 52 | Data data1("data1", "type1"), data2("data2", "type2"); |
| 53 | Dummy_extension ext1("ex1"), ext2("ex2"); |
| 54 | Data_extension<Dummy_extension> dw1(data1, ext1), dw2(data2, ext2); |
| 55 | |
| 56 | Datacache<Data_extension<Dummy_extension>> cache; |
| 57 | |
| 58 | ASSERT_TRUE(cache.store(metadata1, dw1)); |
| 59 | ASSERT_TRUE(cache.store(metadata2, dw2)); |
| 60 | ASSERT_TRUE(cache.store(metadata3, dw1)); |
| 61 | ASSERT_EQ(cache.size(), 3); |
| 62 | |
| 63 | Metadata returned_metadata; |
| 64 | Data_extension<Dummy_extension> returned_dw; |
| 65 | |
| 66 | Iterator<Data_extension<Dummy_extension>> it1(cache, false); |
| 67 | ASSERT_TRUE(it1.valid(cache.version())); |
| 68 | |
| 69 | ASSERT_TRUE(it1.metadata(cache.version(), returned_metadata)); |
| 70 | ASSERT_TRUE(it1.data(cache.version(), returned_dw)); |
| 71 | |
| 72 | ASSERT_TRUE(it1.next(cache.version())); |
| 73 | ASSERT_TRUE(it1.valid(cache.version())); |
| 74 | |
| 75 | ASSERT_TRUE(it1.metadata(cache.version(), returned_metadata)); |
| 76 | ASSERT_TRUE(it1.data(cache.version(), returned_dw)); |
| 77 | |
| 78 | ASSERT_TRUE(returned_metadata.valid()); |
| 79 | ASSERT_TRUE(returned_dw.get_data().valid()); |
| 80 | |
| 81 | ASSERT_TRUE(it1.next(cache.version())); |
| 82 | ASSERT_TRUE(it1.valid(cache.version())); |
| 83 | |
| 84 | ASSERT_TRUE(cache.store(metadata4, dw2)); |
| 85 | ASSERT_FALSE(it1.next(cache.version())); |
| 86 | |
| 87 | Iterator<Data_extension<Dummy_extension>> it2(cache, false); |
| 88 | ASSERT_TRUE(it2.valid(cache.version())); |
| 89 | |
| 90 | ASSERT_TRUE(it2.metadata(cache.version(), returned_metadata)); |
| 91 | ASSERT_TRUE(it2.data(cache.version(), returned_dw)); |
| 92 | |
| 93 | ASSERT_TRUE(returned_metadata.valid()); |
| 94 | ASSERT_TRUE(returned_dw.get_data().valid()); |
| 95 | |
| 96 | ASSERT_TRUE(it2.next(cache.version())); |
| 97 | ASSERT_TRUE(it2.valid(cache.version())); |
| 98 | |
| 99 | ASSERT_TRUE(cache.erase(metadata3)); |
| 100 | ASSERT_FALSE(it2.valid(cache.version())); |
| 101 | |
| 102 | Iterator<Data_extension<Dummy_extension>> it3(cache, false); |
| 103 | ASSERT_TRUE(it3.valid(cache.version())); |
| 104 | |
| 105 | ASSERT_TRUE(it3.metadata(cache.version(), returned_metadata)); |
| 106 | ASSERT_TRUE(it3.data(cache.version(), returned_dw)); |
| 107 | |
| 108 | ASSERT_TRUE(returned_metadata.valid()); |
| 109 | ASSERT_TRUE(returned_dw.get_data().valid()); |
| 110 | |
| 111 | ASSERT_TRUE(cache.store(metadata3, data1)); |
| 112 | ASSERT_FALSE(it3.metadata(cache.version(), returned_metadata)); |
| 113 | |
| 114 | Iterator<Data_extension<Dummy_extension>> it4(cache, false); |
| 115 | ASSERT_TRUE(it4.valid(cache.version())); |
| 116 | |
| 117 | ASSERT_TRUE(it4.metadata(cache.version(), returned_metadata)); |
| 118 | ASSERT_TRUE(it4.data(cache.version(), returned_dw)); |
| 119 | |
| 120 | ASSERT_TRUE(returned_metadata.valid()); |
| 121 | ASSERT_TRUE(returned_dw.get_data().valid()); |
| 122 | |
| 123 | ASSERT_TRUE(cache.erase(metadata2)); |
| 124 | ASSERT_FALSE(it4.data(cache.version(), returned_dw)); |
| 125 | |
| 126 | Iterator<Data_extension<Dummy_extension>> it5(cache, true); |
| 127 | ASSERT_TRUE(it5.valid(cache.version())); |
| 128 | |
| 129 | ASSERT_TRUE(it5.metadata(cache.version(), returned_metadata)); |
| 130 | ASSERT_TRUE(it5.data(cache.version(), returned_dw)); |
| 131 | |
| 132 | ASSERT_TRUE(returned_metadata.valid()); |
| 133 | ASSERT_TRUE(returned_dw.get_data().valid()); |
| 134 | |
| 135 | ASSERT_TRUE(cache.store(metadata2, dw2)); |
| 136 | ASSERT_TRUE(it5.data(cache.version(), returned_dw)); |
| 137 | } |
| 138 | |
| 139 | TEST_F(KeyringCommonIterator_test, IteratorAtTest) { |
| 140 | Metadata metadata1("key1", "[email protected]"), metadata2("key2", "[email protected]"), |
| 141 | metadata3("key2", "[email protected]"), metadata4("key1", "[email protected]"); |
| 142 | Data data1("data1", "type1"), data2("data2", "type2"); |
| 143 | Dummy_extension ext1("ex1"), ext2("ex2"); |
| 144 | Data_extension<Dummy_extension> dw1(data1, ext1), dw2(data2, ext2); |
| 145 | |
| 146 | Datacache<Data_extension<Dummy_extension>> cache; |
| 147 | |
| 148 | ASSERT_TRUE(cache.store(metadata1, dw1)); |
| 149 | ASSERT_TRUE(cache.store(metadata2, dw2)); |
| 150 | ASSERT_TRUE(cache.store(metadata3, dw1)); |
| 151 | ASSERT_EQ(cache.size(), 3); |
| 152 | |
| 153 | Metadata returned_metadata; |
| 154 | Data_extension<Dummy_extension> returned_dw; |
| 155 | |
| 156 | Iterator<Data_extension<Dummy_extension>> it1(cache, metadata2); |
| 157 | ASSERT_TRUE(it1.valid(cache.version())); |
| 158 | |
| 159 | ASSERT_TRUE(it1.metadata(cache.version(), returned_metadata)); |
| 160 | ASSERT_TRUE(it1.data(cache.version(), returned_dw)); |
| 161 | |
| 162 | ASSERT_TRUE(returned_metadata == metadata2); |
| 163 | ASSERT_TRUE(returned_dw == dw2); |
| 164 | |
| 165 | ASSERT_TRUE(cache.store(metadata4, dw2)); |
| 166 | ASSERT_EQ(cache.size(), 4); |
| 167 | |
| 168 | ASSERT_FALSE(it1.metadata(cache.version(), returned_metadata)); |
| 169 | ASSERT_FALSE(it1.data(cache.version(), returned_dw)); |
| 170 | } |
| 171 | } // namespace iterator_unittest |