1 /* Copyright (c) 2014, Google Inc.
2 *
3 * Permission to use, copy, modify, and/or distribute this software for any
4 * purpose with or without fee is hereby granted, provided that the above
5 * copyright notice and this permission notice appear in all copies.
6 *
7 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
8 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
9 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
10 * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
11 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
12 * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
13 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */
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 <memory>
23 #include <map>
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
DEFINE_LHASH_OF(char)35 DEFINE_LHASH_OF(char)
36
37 static std::unique_ptr<char[]> RandString(void) {
38 unsigned len = 1 + (rand() % 3);
39 std::unique_ptr<char[]> ret(new char[len + 1]);
40
41 for (unsigned i = 0; i < len; i++) {
42 ret[i] = '0' + (rand() & 7);
43 }
44 ret[len] = 0;
45
46 return ret;
47 }
48
49 struct FreeLHASH_OF_char {
operator ()FreeLHASH_OF_char50 void operator()(LHASH_OF(char) *lh) { lh_char_free(lh); }
51 };
52
Lookup(std::map<std::string,std::unique_ptr<char[]>> * dummy_lh,const char * key)53 static const char *Lookup(
54 std::map<std::string, std::unique_ptr<char[]>> *dummy_lh, const char *key) {
55 // Using operator[] implicitly inserts into the map.
56 auto iter = dummy_lh->find(key);
57 if (iter == dummy_lh->end()) {
58 return nullptr;
59 }
60 return iter->second.get();
61 }
62
TEST(LHashTest,Basic)63 TEST(LHashTest, Basic) {
64 std::unique_ptr<LHASH_OF(char), FreeLHASH_OF_char> lh(
65 lh_char_new(OPENSSL_strhash, strcmp));
66 ASSERT_TRUE(lh);
67
68 // lh is expected to store a canonical instance of each string. dummy_lh
69 // mirrors what it stores for comparison. It also manages ownership of the
70 // pointers.
71 std::map<std::string, std::unique_ptr<char[]>> dummy_lh;
72
73 for (unsigned i = 0; i < 100000; i++) {
74 EXPECT_EQ(dummy_lh.size(), lh_char_num_items(lh.get()));
75
76 // Check the entire contents and test |lh_*_doall_arg|. This takes O(N)
77 // time, so only do it every few iterations.
78 //
79 // TODO(davidben): |lh_*_doall_arg| also supports modifying the hash in the
80 // callback. Test this.
81 if (i % 1000 == 0) {
82 using ValueList = std::vector<const char *>;
83 ValueList expected, actual;
84 for (const auto &pair : dummy_lh) {
85 expected.push_back(pair.second.get());
86 }
87 std::sort(expected.begin(), expected.end());
88
89 lh_char_doall_arg(lh.get(),
90 [](char *ptr, void *arg) {
91 ValueList *out = reinterpret_cast<ValueList *>(arg);
92 out->push_back(ptr);
93 },
94 &actual);
95 std::sort(actual.begin(), actual.end());
96 EXPECT_EQ(expected, actual);
97 }
98
99 enum Action {
100 kRetrieve = 0,
101 kInsert,
102 kDelete,
103 };
104
105 Action action = static_cast<Action>(rand() % 3);
106 switch (action) {
107 case kRetrieve: {
108 std::unique_ptr<char[]> key = RandString();
109 char *value = lh_char_retrieve(lh.get(), key.get());
110 EXPECT_EQ(Lookup(&dummy_lh, key.get()), value);
111
112 // Do the same lookup with |lh_char_retrieve_key|.
113 value = lh_char_retrieve_key(
114 lh.get(), &key, OPENSSL_strhash(key.get()),
115 [](const void *key_ptr, const char *data) -> int {
116 const char *key_data =
117 reinterpret_cast<const std::unique_ptr<char[]> *>(key_ptr)
118 ->get();
119 return strcmp(key_data, data);
120 });
121 EXPECT_EQ(Lookup(&dummy_lh, key.get()), value);
122 break;
123 }
124
125 case kInsert: {
126 std::unique_ptr<char[]> key = RandString();
127 char *previous;
128 ASSERT_TRUE(lh_char_insert(lh.get(), &previous, key.get()));
129 EXPECT_EQ(Lookup(&dummy_lh, key.get()), previous);
130 dummy_lh[key.get()] = std::move(key);
131 break;
132 }
133
134 case kDelete: {
135 std::unique_ptr<char[]> key = RandString();
136 char *value = lh_char_delete(lh.get(), key.get());
137 EXPECT_EQ(Lookup(&dummy_lh, key.get()), value);
138 dummy_lh.erase(key.get());
139 break;
140 }
141 }
142 }
143 }
144