1 #include "private.h" // For NTDB_TOPLEVEL_HASH_BITS
2 #include <ccan/hash/hash.h>
4 #include "tap-interface.h"
7 /* We rig the hash so adjacent-numbered records always clash. */
8 static uint32_t clash(const void *key, size_t len, uint32_t seed, void *priv)
10 return *((const unsigned int *)key) / 2;
13 /* We use the same seed which we saw a failure on. */
14 static uint32_t fixedhash(const void *key, size_t len, uint32_t seed, void *p)
16 return hash64_stable((const unsigned char *)key, len,
20 static bool store_records(struct ntdb_context *ntdb)
23 NTDB_DATA key = { (unsigned char *)&i, sizeof(i) };
24 NTDB_DATA d, data = { (unsigned char *)&i, sizeof(i) };
26 for (i = 0; i < 1000; i++) {
27 if (ntdb_store(ntdb, key, data, NTDB_REPLACE) != 0)
29 ntdb_fetch(ntdb, key, &d);
30 if (!ntdb_deq(d, data))
37 static void test_val(struct ntdb_context *ntdb, uint64_t val)
40 NTDB_DATA key = { (unsigned char *)&v, sizeof(v) };
41 NTDB_DATA d, data = { (unsigned char *)&v, sizeof(v) };
43 /* Insert an entry, then delete it. */
45 /* Delete should fail. */
46 ok1(ntdb_delete(ntdb, key) == NTDB_ERR_NOEXIST);
47 ok1(ntdb_check(ntdb, NULL, NULL) == 0);
49 /* Insert should succeed. */
50 ok1(ntdb_store(ntdb, key, data, NTDB_INSERT) == 0);
51 ok1(ntdb_check(ntdb, NULL, NULL) == 0);
53 /* Delete should succeed. */
54 ok1(ntdb_delete(ntdb, key) == 0);
55 ok1(ntdb_check(ntdb, NULL, NULL) == 0);
57 /* Re-add it, then add collision. */
58 ok1(ntdb_store(ntdb, key, data, NTDB_INSERT) == 0);
60 ok1(ntdb_store(ntdb, key, data, NTDB_INSERT) == 0);
61 ok1(ntdb_check(ntdb, NULL, NULL) == 0);
64 ok1(ntdb_fetch(ntdb, key, &d) == NTDB_SUCCESS);
65 ok1(d.dsize == data.dsize);
68 ok1(ntdb_fetch(ntdb, key, &d) == NTDB_SUCCESS);
69 ok1(d.dsize == data.dsize);
72 /* Delete second one. */
74 ok1(ntdb_delete(ntdb, key) == 0);
75 ok1(ntdb_check(ntdb, NULL, NULL) == 0);
78 ok1(ntdb_store(ntdb, key, data, NTDB_INSERT) == 0);
79 ok1(ntdb_check(ntdb, NULL, NULL) == 0);
81 /* Now, try deleting first one. */
83 ok1(ntdb_delete(ntdb, key) == 0);
84 ok1(ntdb_check(ntdb, NULL, NULL) == 0);
86 /* Can still find second? */
88 ok1(ntdb_fetch(ntdb, key, &d) == NTDB_SUCCESS);
89 ok1(d.dsize == data.dsize);
92 /* Now, this will be ideally placed. */
94 ok1(ntdb_store(ntdb, key, data, NTDB_INSERT) == 0);
95 ok1(ntdb_check(ntdb, NULL, NULL) == 0);
97 /* This will collide with both. */
99 ok1(ntdb_store(ntdb, key, data, NTDB_INSERT) == 0);
101 /* We can still find them all, right? */
102 ok1(ntdb_fetch(ntdb, key, &d) == NTDB_SUCCESS);
103 ok1(d.dsize == data.dsize);
106 ok1(ntdb_fetch(ntdb, key, &d) == NTDB_SUCCESS);
107 ok1(d.dsize == data.dsize);
110 ok1(ntdb_fetch(ntdb, key, &d) == NTDB_SUCCESS);
111 ok1(d.dsize == data.dsize);
114 /* And if we delete val + 1, that val + 2 should not move! */
116 ok1(ntdb_delete(ntdb, key) == 0);
117 ok1(ntdb_check(ntdb, NULL, NULL) == 0);
120 ok1(ntdb_fetch(ntdb, key, &d) == NTDB_SUCCESS);
121 ok1(d.dsize == data.dsize);
124 ok1(ntdb_fetch(ntdb, key, &d) == NTDB_SUCCESS);
125 ok1(d.dsize == data.dsize);
128 /* Delete those two, so we are empty. */
129 ok1(ntdb_delete(ntdb, key) == 0);
131 ok1(ntdb_delete(ntdb, key) == 0);
133 ok1(ntdb_check(ntdb, NULL, NULL) == 0);
136 int main(int argc, char *argv[])
139 struct ntdb_context *ntdb;
140 uint64_t seed = 16014841315512641303ULL;
141 union ntdb_attribute clash_hattr
142 = { .hash = { .base = { NTDB_ATTRIBUTE_HASH },
144 union ntdb_attribute fixed_hattr
145 = { .hash = { .base = { NTDB_ATTRIBUTE_HASH },
148 int flags[] = { NTDB_INTERNAL, NTDB_DEFAULT, NTDB_NOMMAP,
149 NTDB_INTERNAL|NTDB_CONVERT, NTDB_CONVERT,
150 NTDB_NOMMAP|NTDB_CONVERT };
151 /* These two values gave trouble before. */
152 int vals[] = { 755, 837 };
154 clash_hattr.base.next = &tap_log_attr;
155 fixed_hattr.base.next = &tap_log_attr;
157 plan_tests(sizeof(flags) / sizeof(flags[0])
158 * (39 * 3 + 5 + sizeof(vals)/sizeof(vals[0])*2) + 1);
159 for (i = 0; i < sizeof(flags) / sizeof(flags[0]); i++) {
160 ntdb = ntdb_open("run-13-delete.ntdb", flags[i]|MAYBE_NOSYNC,
161 O_RDWR|O_CREAT|O_TRUNC, 0600, &clash_hattr);
166 /* Check start of hash table. */
169 /* Check end of hash table. */
170 test_val(ntdb, -1ULL);
172 /* Check mixed bitpattern. */
173 test_val(ntdb, 0x123456789ABCDEF0ULL);
175 ok1(!ntdb->file || (ntdb->file->allrecord_lock.count == 0
176 && ntdb->file->num_lockrecs == 0));
179 /* Deleting these entries in the db gave problems. */
180 ntdb = ntdb_open("run-13-delete.ntdb", flags[i]|MAYBE_NOSYNC,
181 O_RDWR|O_CREAT|O_TRUNC, 0600, &fixed_hattr);
186 ok1(store_records(ntdb));
187 ok1(ntdb_check(ntdb, NULL, NULL) == 0);
188 for (j = 0; j < sizeof(vals)/sizeof(vals[0]); j++) {
191 key.dptr = (unsigned char *)&vals[j];
192 key.dsize = sizeof(vals[j]);
193 ok1(ntdb_delete(ntdb, key) == 0);
194 ok1(ntdb_check(ntdb, NULL, NULL) == 0);
199 ok1(tap_log_messages == 0);
200 return exit_status();