Debian Codegraph @

debian/rust-vendor/aws-lc-sys/aws-lc/crypto/lhash/lhash_test.cc

raw
1 // Copyright (c) 2014, Google Inc.
2 // SPDX-License-Identifier: ISC
3
4 #include <openssl/lhash.h>
5
6 #include <stdio.h>
7 #include <stdlib.h>
8 #include <string.h>
9
10 #include <algorithm>
11 #include <memory>
12 #include <map>
13 #include <string>
14 #include <utility>
15 #include <vector>
16
17 #include <openssl/mem.h>
18
19 #include <gtest/gtest.h>
20
21 #include "internal.h"
22
23
24 DEFINE_LHASH_OF(char)
25
26 static std::unique_ptr<char[]> RandString(void) {
27 unsigned len = 1 + (rand() % 3); // NOLINT(clang-analyzer-security.insecureAPI.rand)
28 std::unique_ptr<char[]> ret(new char[len + 1]);
29
30 for (unsigned i = 0; i < len; i++) {
31 ret[i] = '0' + (rand() & 7); // NOLINT(clang-analyzer-security.insecureAPI.rand)
32 }
33 ret[len] = 0;
34
35 return ret;
36 }
37
38 struct FreeLHASH_OF_char {
39 void operator()(LHASH_OF(char) *lh) { lh_char_free(lh); }
40 };
41
42 static const char *Lookup(
43 std::map<std::string, std::unique_ptr<char[]>> *dummy_lh, const char *key) {
44 // Using operator[] implicitly inserts into the map.
45 auto iter = dummy_lh->find(key);
46 if (iter == dummy_lh->end()) {
47 return nullptr;
48 }
49 return iter->second.get();
50 }
51
52 TEST(LHashTest, Basic) {
53 std::unique_ptr<LHASH_OF(char), FreeLHASH_OF_char> lh(
54 lh_char_new(OPENSSL_strhash, strcmp));
55 ASSERT_TRUE(lh);
56
57 // lh is expected to store a canonical instance of each string. dummy_lh
58 // mirrors what it stores for comparison. It also manages ownership of the
59 // pointers.
60 std::map<std::string, std::unique_ptr<char[]>> dummy_lh;
61
62 for (unsigned i = 0; i < 100000; i++) {
63 EXPECT_EQ(dummy_lh.size(), lh_char_num_items(lh.get()));
64
65 // Check the entire contents and test |lh_*_doall_arg|. This takes O(N)
66 // time, so only do it every few iterations.
67 //
68 // TODO(davidben): |lh_*_doall_arg| also supports modifying the hash in the
69 // callback. Test this.
70 if (i % 1000 == 0) {
71 using ValueList = std::vector<const char *>;
72 ValueList expected, actual;
73 for (const auto &pair : dummy_lh) {
74 expected.push_back(pair.second.get());
75 }
76 std::sort(expected.begin(), expected.end());
77
78 lh_char_doall_arg(lh.get(),
79 [](char *ptr, void *arg) {
80 ValueList *out = reinterpret_cast<ValueList *>(arg);
81 out->push_back(ptr);
82 },
83 &actual);
84 std::sort(actual.begin(), actual.end());
85 EXPECT_EQ(expected, actual);
86 }
87
88 enum Action {
89 kRetrieve = 0,
90 kInsert,
91 kDelete,
92 };
93
94 Action action = static_cast<Action>(rand() % 3); // NOLINT(clang-analyzer-security.insecureAPI.rand)
95 switch (action) {
96 case kRetrieve: {
97 std::unique_ptr<char[]> key = RandString();
98 char *value = lh_char_retrieve(lh.get(), key.get());
99 EXPECT_EQ(Lookup(&dummy_lh, key.get()), value);
100
101 // Do the same lookup with |lh_char_retrieve_key|.
102 value = lh_char_retrieve_key(
103 lh.get(), &key, OPENSSL_strhash(key.get()),
104 [](const void *key_ptr, const char *data) -> int {
105 const char *key_data =
106 reinterpret_cast<const std::unique_ptr<char[]> *>(key_ptr)
107 ->get();
108 return strcmp(key_data, data);
109 });
110 EXPECT_EQ(Lookup(&dummy_lh, key.get()), value);
111 break;
112 }
113
114 case kInsert: {
115 std::unique_ptr<char[]> key = RandString();
116 char *previous;
117 ASSERT_TRUE(lh_char_insert(lh.get(), &previous, key.get()));
118 EXPECT_EQ(Lookup(&dummy_lh, key.get()), previous);
119 dummy_lh[key.get()] = std::move(key);
120 break;
121 }
122
123 case kDelete: {
124 std::unique_ptr<char[]> key = RandString();
125 char *value = lh_char_delete(lh.get(), key.get());
126 EXPECT_EQ(Lookup(&dummy_lh, key.get()), value);
127 dummy_lh.erase(key.get());
128 break;
129 }
130 }
131 }
132 }