1 #include "tdb2-source.h"
2 #include <ccan/tap/tap.h>
5 /* We rig the hash so adjacent-numbered records always clash. */
6 static uint64_t clash(const void *key, size_t len, uint64_t seed, void *priv)
8 return ((uint64_t)*(const unsigned int *)key)
9 << (64 - TDB_TOPLEVEL_HASH_BITS - 1);
12 /* We use the same seed which we saw a failure on. */
13 static uint64_t fixedhash(const void *key, size_t len, uint64_t seed, void *p)
15 return hash64_stable((const unsigned char *)key, len,
19 static bool store_records(struct tdb_context *tdb)
22 struct tdb_data key = { (unsigned char *)&i, sizeof(i) };
23 struct tdb_data d, data = { (unsigned char *)&i, sizeof(i) };
25 for (i = 0; i < 1000; i++) {
26 if (tdb_store(tdb, key, data, TDB_REPLACE) != 0)
28 tdb_fetch(tdb, key, &d);
29 if (!tdb_deq(d, data))
36 static void test_val(struct tdb_context *tdb, uint64_t val)
39 struct tdb_data key = { (unsigned char *)&v, sizeof(v) };
40 struct tdb_data d, data = { (unsigned char *)&v, sizeof(v) };
42 /* Insert an entry, then delete it. */
44 /* Delete should fail. */
45 ok1(tdb_delete(tdb, key) == TDB_ERR_NOEXIST);
46 ok1(tdb_check(tdb, NULL, NULL) == 0);
48 /* Insert should succeed. */
49 ok1(tdb_store(tdb, key, data, TDB_INSERT) == 0);
50 ok1(tdb_check(tdb, NULL, NULL) == 0);
52 /* Delete should succeed. */
53 ok1(tdb_delete(tdb, key) == 0);
54 ok1(tdb_check(tdb, NULL, NULL) == 0);
56 /* Re-add it, then add collision. */
57 ok1(tdb_store(tdb, key, data, TDB_INSERT) == 0);
59 ok1(tdb_store(tdb, key, data, TDB_INSERT) == 0);
60 ok1(tdb_check(tdb, NULL, NULL) == 0);
63 ok1(tdb_fetch(tdb, key, &d) == TDB_SUCCESS);
64 ok1(d.dsize == data.dsize);
67 ok1(tdb_fetch(tdb, key, &d) == TDB_SUCCESS);
68 ok1(d.dsize == data.dsize);
71 /* Delete second one. */
73 ok1(tdb_delete(tdb, key) == 0);
74 ok1(tdb_check(tdb, NULL, NULL) == 0);
77 ok1(tdb_store(tdb, key, data, TDB_INSERT) == 0);
78 ok1(tdb_check(tdb, NULL, NULL) == 0);
80 /* Now, try deleting first one. */
82 ok1(tdb_delete(tdb, key) == 0);
83 ok1(tdb_check(tdb, NULL, NULL) == 0);
85 /* Can still find second? */
87 ok1(tdb_fetch(tdb, key, &d) == TDB_SUCCESS);
88 ok1(d.dsize == data.dsize);
91 /* Now, this will be ideally placed. */
93 ok1(tdb_store(tdb, key, data, TDB_INSERT) == 0);
94 ok1(tdb_check(tdb, NULL, NULL) == 0);
96 /* This will collide with both. */
98 ok1(tdb_store(tdb, key, data, TDB_INSERT) == 0);
100 /* We can still find them all, right? */
101 ok1(tdb_fetch(tdb, key, &d) == TDB_SUCCESS);
102 ok1(d.dsize == data.dsize);
105 ok1(tdb_fetch(tdb, key, &d) == TDB_SUCCESS);
106 ok1(d.dsize == data.dsize);
109 ok1(tdb_fetch(tdb, key, &d) == TDB_SUCCESS);
110 ok1(d.dsize == data.dsize);
113 /* And if we delete val + 1, that val + 2 should not move! */
115 ok1(tdb_delete(tdb, key) == 0);
116 ok1(tdb_check(tdb, NULL, NULL) == 0);
119 ok1(tdb_fetch(tdb, key, &d) == TDB_SUCCESS);
120 ok1(d.dsize == data.dsize);
123 ok1(tdb_fetch(tdb, key, &d) == TDB_SUCCESS);
124 ok1(d.dsize == data.dsize);
127 /* Delete those two, so we are empty. */
128 ok1(tdb_delete(tdb, key) == 0);
130 ok1(tdb_delete(tdb, key) == 0);
132 ok1(tdb_check(tdb, NULL, NULL) == 0);
135 int main(int argc, char *argv[])
138 struct tdb_context *tdb;
139 uint64_t seed = 16014841315512641303ULL;
140 union tdb_attribute clash_hattr
141 = { .hash = { .base = { TDB_ATTRIBUTE_HASH },
143 union tdb_attribute fixed_hattr
144 = { .hash = { .base = { TDB_ATTRIBUTE_HASH },
147 int flags[] = { TDB_INTERNAL, TDB_DEFAULT, TDB_NOMMAP,
148 TDB_INTERNAL|TDB_CONVERT, TDB_CONVERT,
149 TDB_NOMMAP|TDB_CONVERT };
150 /* These two values gave trouble before. */
151 int vals[] = { 755, 837 };
153 clash_hattr.base.next = &tap_log_attr;
154 fixed_hattr.base.next = &tap_log_attr;
156 plan_tests(sizeof(flags) / sizeof(flags[0])
157 * (39 * 3 + 5 + sizeof(vals)/sizeof(vals[0])*2) + 1);
158 for (i = 0; i < sizeof(flags) / sizeof(flags[0]); i++) {
159 tdb = tdb_open("run-13-delete.tdb", flags[i],
160 O_RDWR|O_CREAT|O_TRUNC, 0600, &clash_hattr);
165 /* Check start of hash table. */
168 /* Check end of hash table. */
169 test_val(tdb, -1ULL);
171 /* Check mixed bitpattern. */
172 test_val(tdb, 0x123456789ABCDEF0ULL);
174 ok1(!tdb->file || (tdb->file->allrecord_lock.count == 0
175 && tdb->file->num_lockrecs == 0));
178 /* Deleting these entries in the db gave problems. */
179 tdb = tdb_open("run-13-delete.tdb", flags[i],
180 O_RDWR|O_CREAT|O_TRUNC, 0600, &fixed_hattr);
185 ok1(store_records(tdb));
186 ok1(tdb_check(tdb, NULL, NULL) == 0);
187 for (j = 0; j < sizeof(vals)/sizeof(vals[0]); j++) {
190 key.dptr = (unsigned char *)&vals[j];
191 key.dsize = sizeof(vals[j]);
192 ok1(tdb_delete(tdb, key) == 0);
193 ok1(tdb_check(tdb, NULL, NULL) == 0);
198 ok1(tap_log_messages == 0);
199 return exit_status();