blob: cbeb15b1d39da646d472e67b418d3cf9ef65cc9d [file] [log] [blame]
Robert Sloan69939df2017-01-09 10:53:07 -08001/* 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#if !defined(_POSIX_C_SOURCE)
16#define _POSIX_C_SOURCE 201410L
17#endif
18
19#include <openssl/crypto.h>
20#include <openssl/lhash.h>
21
22#include <stdio.h>
23#include <stdlib.h>
24#include <string.h>
25
26#include <algorithm>
27#include <memory>
28#include <map>
29#include <string>
30#include <utility>
31#include <vector>
32
33
34static std::unique_ptr<char[]> RandString(void) {
35 unsigned len = 1 + (rand() % 3);
36 std::unique_ptr<char[]> ret(new char[len + 1]);
37
38 for (unsigned i = 0; i < len; i++) {
39 ret[i] = '0' + (rand() & 7);
40 }
41 ret[len] = 0;
42
43 return ret;
44}
45
46struct FreeLHASH {
47 void operator()(_LHASH *lh) { lh_free(lh); }
48};
49
50static const char *Lookup(
51 std::map<std::string, std::unique_ptr<char[]>> *dummy_lh, const char *key) {
52 // Using operator[] implicitly inserts into the map.
53 auto iter = dummy_lh->find(key);
54 if (iter == dummy_lh->end()) {
55 return nullptr;
56 }
57 return iter->second.get();
58}
59
60int main(int argc, char **argv) {
61 CRYPTO_library_init();
62
63 std::unique_ptr<_LHASH, FreeLHASH> lh(
64 lh_new((lhash_hash_func)lh_strhash, (lhash_cmp_func)strcmp));
65 if (!lh) {
66 return 1;
67 }
68
69 // lh is expected to store a canonical instance of each string. dummy_lh
70 // mirrors what it stores for comparison. It also manages ownership of the
71 // pointers.
72 std::map<std::string, std::unique_ptr<char[]>> dummy_lh;
73
74 for (unsigned i = 0; i < 100000; i++) {
75 if (dummy_lh.size() != lh_num_items(lh.get())) {
76 fprintf(stderr, "Length mismatch\n");
77 return 1;
78 }
79
80 // Check the entire contents and test |lh_doall_arg|. This takes O(N) time,
81 // so only do it every few iterations.
82 //
83 // TODO(davidben): |lh_doall_arg| also supports modifying the hash in the
84 // callback. Test this.
85 if (i % 1000 == 0) {
86 using ValueList = std::vector<const char *>;
87 ValueList expected, actual;
88 for (const auto &pair : dummy_lh) {
89 expected.push_back(pair.second.get());
90 }
91 std::sort(expected.begin(), expected.end());
92
93 lh_doall_arg(lh.get(),
94 [](void *ptr, void *arg) {
95 ValueList *out = reinterpret_cast<ValueList *>(arg);
96 out->push_back(reinterpret_cast<char *>(ptr));
97 },
98 &actual);
99 std::sort(actual.begin(), actual.end());
100
101 if (expected != actual) {
102 fprintf(stderr, "Contents mismatch\n");
103 return 1;
104 }
105 }
106
107 enum Action {
108 kRetrieve = 0,
109 kInsert,
110 kDelete,
111 };
112
113 Action action = static_cast<Action>(rand() % 3);
114 switch (action) {
115 case kRetrieve: {
116 std::unique_ptr<char[]> key = RandString();
117 void *value = lh_retrieve(lh.get(), key.get());
118 if (value != Lookup(&dummy_lh, key.get())) {
119 fprintf(stderr, "lh_retrieve failure\n");
120 return 1;
121 }
122 break;
123 }
124
125 case kInsert: {
126 std::unique_ptr<char[]> key = RandString();
127 void *previous;
128 if (!lh_insert(lh.get(), &previous, key.get())) {
129 return 1;
130 }
131
132 if (previous != Lookup(&dummy_lh, key.get())) {
133 fprintf(stderr, "lh_insert failure\n");
134 return 1;
135 }
136
137 dummy_lh[key.get()] = std::move(key);
138 break;
139 }
140
141 case kDelete: {
142 std::unique_ptr<char[]> key = RandString();
143 void *value = lh_delete(lh.get(), key.get());
144
145 if (value != Lookup(&dummy_lh, key.get())) {
146 fprintf(stderr, "lh_delete failure\n");
147 return 1;
148 }
149
150 dummy_lh.erase(key.get());
151 break;
152 }
153
154 default:
155 abort();
156 }
157 }
158
159 printf("PASS\n");
160 return 0;
161}