| 6 | // |
| 7 | // https://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 |
| 15 | #include <openssl/lhash.h> |
| 16 |
| 17 | #include <stdio.h> |
| 18 | #include <stdlib.h> |
| 19 | #include <string.h> |
| 20 |
| 21 | #include <algorithm> |
| 22 | #include <map> |
| 23 | #include <memory> |
| 24 | #include <string> |
| 25 | #include <utility> |
| 26 | #include <vector> |
| 27 |
| 28 | #include <openssl/mem.h> |
| 29 |
| 30 | #include <gtest/gtest.h> |
| 31 |
| 32 | #include "internal.h" |
| 33 |
| 34 |
| 35 | namespace { |
| 36 |
| 37 | DEFINE_LHASH_OF(char) |
| 38 |
| 39 | static std::unique_ptr<char[]> RandString(void) { |
| 40 | unsigned len = 1 + (rand() % 3); |
| 41 | auto ret = std::make_unique<char[]>(len + 1); |
| 42 |
| 43 | for (unsigned i = 0; i < len; i++) { |
| 44 | ret[i] = '0' + (rand() & 7); |
| 45 | } |
| 46 | ret[len] = 0; |
| 47 |
| 48 | return ret; |
| 49 | } |
| 50 |
| 51 | struct FreeLHASH_OF_char { |
| 52 | void operator()(LHASH_OF(char) *lh) { lh_char_free(lh); } |
| 53 | }; |
| 54 |
| 55 | static const char *Lookup( |
| 56 | std::map<std::string, std::unique_ptr<char[]>> *dummy_lh, const char *key) { |
| 57 | // Using operator[] implicitly inserts into the map. |
| 58 | auto iter = dummy_lh->find(key); |
| 59 | if (iter == dummy_lh->end()) { |
| 60 | return nullptr; |
| 61 | } |
| 62 | return iter->second.get(); |
| 63 | } |
| 64 |
| 65 | TEST(LHashTest, Basic) { |
| 66 | std::unique_ptr<LHASH_OF(char), FreeLHASH_OF_char> lh( |
| 67 | lh_char_new(OPENSSL_strhash, strcmp)); |
| 68 | ASSERT_TRUE(lh); |
| 69 |
| 70 | // lh is expected to store a canonical instance of each string. dummy_lh |
| 71 | // mirrors what it stores for comparison. It also manages ownership of the |
| 72 | // pointers. |
| 73 | std::map<std::string, std::unique_ptr<char[]>> dummy_lh; |
| 74 |
| 75 | for (unsigned i = 0; i < 100000; i++) { |
| 76 | EXPECT_EQ(dummy_lh.size(), lh_char_num_items(lh.get())); |
| 77 |
| 78 | // Check the entire contents and test |lh_*_doall_arg|. This takes O(N) |
| 79 | // time, so only do it every few iterations. |
| 80 | // |
| 81 | // TODO(davidben): |lh_*_doall_arg| also supports modifying the hash in the |
| 82 | // callback. Test this. |
| 83 | if (i % 1000 == 0) { |
| 84 | using ValueList = std::vector<const char *>; |
| 85 | ValueList expected, actual; |
| 86 | for (const auto &pair : dummy_lh) { |
| 87 | expected.push_back(pair.second.get()); |
| 88 | } |
| 89 | std::sort(expected.begin(), expected.end()); |
| 90 |
| 91 | lh_char_doall_arg( |
| 92 | lh.get(), |
| 93 | [](char *ptr, void *arg) { |
| 94 | ValueList *out = reinterpret_cast<ValueList *>(arg); |
| 95 | out->push_back(ptr); |
| 96 | }, |
| 97 | &actual); |
| 98 | std::sort(actual.begin(), actual.end()); |
| 99 | EXPECT_EQ(expected, actual); |
| 100 | } |
| 101 |
| 102 | enum Action { |
| 103 | kRetrieve = 0, |
| 104 | kInsert, |
| 105 | kDelete, |
| 106 | }; |
| 107 |
| 108 | Action action = static_cast<Action>(rand() % 3); |
| 109 | switch (action) { |
| 110 | case kRetrieve: { |
| 111 | std::unique_ptr<char[]> key = RandString(); |
| 112 | char *value = lh_char_retrieve(lh.get(), key.get()); |
| 113 | EXPECT_EQ(Lookup(&dummy_lh, key.get()), value); |
| 114 |
| 115 | // Do the same lookup with |lh_char_retrieve_key|. |
| 116 | value = lh_char_retrieve_key( |
| 117 | lh.get(), &key, OPENSSL_strhash(key.get()), |
| 118 | [](const void *key_ptr, const char *data) -> int { |
| 119 | const char *key_data = |
| 120 | reinterpret_cast<const std::unique_ptr<char[]> *>(key_ptr) |
| 121 | ->get(); |
| 122 | return strcmp(key_data, data); |
| 123 | }); |
| 124 | EXPECT_EQ(Lookup(&dummy_lh, key.get()), value); |
| 125 | break; |
| 126 | } |
| 127 |
| 128 | case kInsert: { |
| 129 | std::unique_ptr<char[]> key = RandString(); |
| 130 | char *previous; |
| 131 | ASSERT_TRUE(lh_char_insert(lh.get(), &previous, key.get())); |
| 132 | EXPECT_EQ(Lookup(&dummy_lh, key.get()), previous); |
| 133 | dummy_lh[key.get()] = std::move(key); |
| 134 | break; |
| 135 | } |
| 136 |
| 137 | case kDelete: { |
| 138 | std::unique_ptr<char[]> key = RandString(); |
| 139 | char *value = lh_char_delete(lh.get(), key.get()); |
| 140 | EXPECT_EQ(Lookup(&dummy_lh, key.get()), value); |
| 141 | dummy_lh.erase(key.get()); |
| 142 | break; |
| 143 | } |
| 144 | } |
| 145 | } |
| 146 | } |
| 147 |
| 148 | } // namespace |
| 149 |