1 #include "private.h" // For TDB_TOPLEVEL_HASH_BITS
2 #include <ccan/hash/hash.h>
7 #include "tap-interface.h"
10 /* We rig the hash so adjacent-numbered records always clash. */
11 static uint64_t clash(const void *key, size_t len, uint64_t seed, void *priv)
13 return ((uint64_t)*(const unsigned int *)key)
14 << (64 - TDB_TOPLEVEL_HASH_BITS - 1);
17 /* We use the same seed which we saw a failure on. */
18 static uint64_t fixedhash(const void *key, size_t len, uint64_t seed, void *p)
20 return hash64_stable((const unsigned char *)key, len,
24 static bool store_records(struct tdb_context *tdb)
27 struct tdb_data key = { (unsigned char *)&i, sizeof(i) };
28 struct tdb_data d, data = { (unsigned char *)&i, sizeof(i) };
30 for (i = 0; i < 1000; i++) {
31 if (tdb_store(tdb, key, data, TDB_REPLACE) != 0)
33 tdb_fetch(tdb, key, &d);
34 if (!tdb_deq(d, data))
41 static void test_val(struct tdb_context *tdb, uint64_t val)
44 struct tdb_data key = { (unsigned char *)&v, sizeof(v) };
45 struct tdb_data d, data = { (unsigned char *)&v, sizeof(v) };
47 /* Insert an entry, then delete it. */
49 /* Delete should fail. */
50 ok1(tdb_delete(tdb, key) == TDB_ERR_NOEXIST);
51 ok1(tdb_check(tdb, NULL, NULL) == 0);
53 /* Insert should succeed. */
54 ok1(tdb_store(tdb, key, data, TDB_INSERT) == 0);
55 ok1(tdb_check(tdb, NULL, NULL) == 0);
57 /* Delete should succeed. */
58 ok1(tdb_delete(tdb, key) == 0);
59 ok1(tdb_check(tdb, NULL, NULL) == 0);
61 /* Re-add it, then add collision. */
62 ok1(tdb_store(tdb, key, data, TDB_INSERT) == 0);
64 ok1(tdb_store(tdb, key, data, TDB_INSERT) == 0);
65 ok1(tdb_check(tdb, NULL, NULL) == 0);
68 ok1(tdb_fetch(tdb, key, &d) == TDB_SUCCESS);
69 ok1(d.dsize == data.dsize);
72 ok1(tdb_fetch(tdb, key, &d) == TDB_SUCCESS);
73 ok1(d.dsize == data.dsize);
76 /* Delete second one. */
78 ok1(tdb_delete(tdb, key) == 0);
79 ok1(tdb_check(tdb, NULL, NULL) == 0);
82 ok1(tdb_store(tdb, key, data, TDB_INSERT) == 0);
83 ok1(tdb_check(tdb, NULL, NULL) == 0);
85 /* Now, try deleting first one. */
87 ok1(tdb_delete(tdb, key) == 0);
88 ok1(tdb_check(tdb, NULL, NULL) == 0);
90 /* Can still find second? */
92 ok1(tdb_fetch(tdb, key, &d) == TDB_SUCCESS);
93 ok1(d.dsize == data.dsize);
96 /* Now, this will be ideally placed. */
98 ok1(tdb_store(tdb, key, data, TDB_INSERT) == 0);
99 ok1(tdb_check(tdb, NULL, NULL) == 0);
101 /* This will collide with both. */
103 ok1(tdb_store(tdb, key, data, TDB_INSERT) == 0);
105 /* We can still find them all, right? */
106 ok1(tdb_fetch(tdb, key, &d) == TDB_SUCCESS);
107 ok1(d.dsize == data.dsize);
110 ok1(tdb_fetch(tdb, key, &d) == TDB_SUCCESS);
111 ok1(d.dsize == data.dsize);
114 ok1(tdb_fetch(tdb, key, &d) == TDB_SUCCESS);
115 ok1(d.dsize == data.dsize);
118 /* And if we delete val + 1, that val + 2 should not move! */
120 ok1(tdb_delete(tdb, key) == 0);
121 ok1(tdb_check(tdb, NULL, NULL) == 0);
124 ok1(tdb_fetch(tdb, key, &d) == TDB_SUCCESS);
125 ok1(d.dsize == data.dsize);
128 ok1(tdb_fetch(tdb, key, &d) == TDB_SUCCESS);
129 ok1(d.dsize == data.dsize);
132 /* Delete those two, so we are empty. */
133 ok1(tdb_delete(tdb, key) == 0);
135 ok1(tdb_delete(tdb, key) == 0);
137 ok1(tdb_check(tdb, NULL, NULL) == 0);
140 int main(int argc, char *argv[])
143 struct tdb_context *tdb;
144 uint64_t seed = 16014841315512641303ULL;
145 union tdb_attribute clash_hattr
146 = { .hash = { .base = { TDB_ATTRIBUTE_HASH },
148 union tdb_attribute fixed_hattr
149 = { .hash = { .base = { TDB_ATTRIBUTE_HASH },
152 int flags[] = { TDB_INTERNAL, TDB_DEFAULT, TDB_NOMMAP,
153 TDB_INTERNAL|TDB_CONVERT, TDB_CONVERT,
154 TDB_NOMMAP|TDB_CONVERT };
155 /* These two values gave trouble before. */
156 int vals[] = { 755, 837 };
158 clash_hattr.base.next = &tap_log_attr;
159 fixed_hattr.base.next = &tap_log_attr;
161 plan_tests(sizeof(flags) / sizeof(flags[0])
162 * (39 * 3 + 5 + sizeof(vals)/sizeof(vals[0])*2) + 1);
163 for (i = 0; i < sizeof(flags) / sizeof(flags[0]); i++) {
164 tdb = tdb_open("run-13-delete.tdb", flags[i],
165 O_RDWR|O_CREAT|O_TRUNC, 0600, &clash_hattr);
170 /* Check start of hash table. */
173 /* Check end of hash table. */
174 test_val(tdb, -1ULL);
176 /* Check mixed bitpattern. */
177 test_val(tdb, 0x123456789ABCDEF0ULL);
179 ok1(!tdb->file || (tdb->file->allrecord_lock.count == 0
180 && tdb->file->num_lockrecs == 0));
183 /* Deleting these entries in the db gave problems. */
184 tdb = tdb_open("run-13-delete.tdb", flags[i],
185 O_RDWR|O_CREAT|O_TRUNC, 0600, &fixed_hattr);
190 ok1(store_records(tdb));
191 ok1(tdb_check(tdb, NULL, NULL) == 0);
192 for (j = 0; j < sizeof(vals)/sizeof(vals[0]); j++) {
195 key.dptr = (unsigned char *)&vals[j];
196 key.dsize = sizeof(vals[j]);
197 ok1(tdb_delete(tdb, key) == 0);
198 ok1(tdb_check(tdb, NULL, NULL) == 0);
203 ok1(tap_log_messages == 0);
204 return exit_status();