1 #include "../private.h" // For NTDB_TOPLEVEL_HASH_BITS
2 #include <ccan/hash/hash.h>
4 #include "tap-interface.h"
6 #include "helpapi-external-agent.h"
8 /* We rig the hash so adjacent-numbered records always clash. */
9 static uint32_t clash(const void *key, size_t len, uint32_t seed, void *priv)
11 return *((const unsigned int *)key) / 2;
14 /* We use the same seed which we saw a failure on. */
15 static uint32_t fixedhash(const void *key, size_t len, uint32_t seed, void *p)
17 return hash64_stable((const unsigned char *)key, len,
21 static bool store_records(struct ntdb_context *ntdb)
24 NTDB_DATA key = { (unsigned char *)&i, sizeof(i) };
25 NTDB_DATA d, data = { (unsigned char *)&i, sizeof(i) };
27 for (i = 0; i < 1000; i++) {
28 if (ntdb_store(ntdb, key, data, NTDB_REPLACE) != 0)
30 ntdb_fetch(ntdb, key, &d);
31 if (!ntdb_deq(d, data))
38 static void test_val(struct ntdb_context *ntdb, uint64_t val)
41 NTDB_DATA key = { (unsigned char *)&v, sizeof(v) };
42 NTDB_DATA d, data = { (unsigned char *)&v, sizeof(v) };
44 /* Insert an entry, then delete it. */
46 /* Delete should fail. */
47 ok1(ntdb_delete(ntdb, key) == NTDB_ERR_NOEXIST);
48 ok1(ntdb_check(ntdb, NULL, NULL) == 0);
50 /* Insert should succeed. */
51 ok1(ntdb_store(ntdb, key, data, NTDB_INSERT) == 0);
52 ok1(ntdb_check(ntdb, NULL, NULL) == 0);
54 /* Delete should succeed. */
55 ok1(ntdb_delete(ntdb, key) == 0);
56 ok1(ntdb_check(ntdb, NULL, NULL) == 0);
58 /* Re-add it, then add collision. */
59 ok1(ntdb_store(ntdb, key, data, NTDB_INSERT) == 0);
61 ok1(ntdb_store(ntdb, key, data, NTDB_INSERT) == 0);
62 ok1(ntdb_check(ntdb, NULL, NULL) == 0);
65 ok1(ntdb_fetch(ntdb, key, &d) == NTDB_SUCCESS);
66 ok1(d.dsize == data.dsize);
69 ok1(ntdb_fetch(ntdb, key, &d) == NTDB_SUCCESS);
70 ok1(d.dsize == data.dsize);
73 /* Delete second one. */
75 ok1(ntdb_delete(ntdb, key) == 0);
76 ok1(ntdb_check(ntdb, NULL, NULL) == 0);
79 ok1(ntdb_store(ntdb, key, data, NTDB_INSERT) == 0);
80 ok1(ntdb_check(ntdb, NULL, NULL) == 0);
82 /* Now, try deleting first one. */
84 ok1(ntdb_delete(ntdb, key) == 0);
85 ok1(ntdb_check(ntdb, NULL, NULL) == 0);
87 /* Can still find second? */
89 ok1(ntdb_fetch(ntdb, key, &d) == NTDB_SUCCESS);
90 ok1(d.dsize == data.dsize);
93 /* Now, this will be ideally placed. */
95 ok1(ntdb_store(ntdb, key, data, NTDB_INSERT) == 0);
96 ok1(ntdb_check(ntdb, NULL, NULL) == 0);
98 /* This will collide with both. */
100 ok1(ntdb_store(ntdb, key, data, NTDB_INSERT) == 0);
102 /* We can still find them all, right? */
103 ok1(ntdb_fetch(ntdb, key, &d) == NTDB_SUCCESS);
104 ok1(d.dsize == data.dsize);
107 ok1(ntdb_fetch(ntdb, key, &d) == NTDB_SUCCESS);
108 ok1(d.dsize == data.dsize);
111 ok1(ntdb_fetch(ntdb, key, &d) == NTDB_SUCCESS);
112 ok1(d.dsize == data.dsize);
115 /* And if we delete val + 1, that val + 2 should not move! */
117 ok1(ntdb_delete(ntdb, key) == 0);
118 ok1(ntdb_check(ntdb, NULL, NULL) == 0);
121 ok1(ntdb_fetch(ntdb, key, &d) == NTDB_SUCCESS);
122 ok1(d.dsize == data.dsize);
125 ok1(ntdb_fetch(ntdb, key, &d) == NTDB_SUCCESS);
126 ok1(d.dsize == data.dsize);
129 /* Delete those two, so we are empty. */
130 ok1(ntdb_delete(ntdb, key) == 0);
132 ok1(ntdb_delete(ntdb, key) == 0);
134 ok1(ntdb_check(ntdb, NULL, NULL) == 0);
137 int main(int argc, char *argv[])
140 struct ntdb_context *ntdb;
141 uint64_t seed = 16014841315512641303ULL;
142 union ntdb_attribute clash_hattr
143 = { .hash = { .base = { NTDB_ATTRIBUTE_HASH },
145 union ntdb_attribute fixed_hattr
146 = { .hash = { .base = { NTDB_ATTRIBUTE_HASH },
149 int flags[] = { NTDB_INTERNAL, NTDB_DEFAULT, NTDB_NOMMAP,
150 NTDB_INTERNAL|NTDB_CONVERT, NTDB_CONVERT,
151 NTDB_NOMMAP|NTDB_CONVERT };
152 /* These two values gave trouble before. */
153 int vals[] = { 755, 837 };
155 clash_hattr.base.next = &tap_log_attr;
156 fixed_hattr.base.next = &tap_log_attr;
158 plan_tests(sizeof(flags) / sizeof(flags[0])
159 * (39 * 3 + 5 + sizeof(vals)/sizeof(vals[0])*2) + 1);
160 for (i = 0; i < sizeof(flags) / sizeof(flags[0]); i++) {
161 ntdb = ntdb_open("run-13-delete.ntdb", flags[i]|MAYBE_NOSYNC,
162 O_RDWR|O_CREAT|O_TRUNC, 0600, &clash_hattr);
167 /* Check start of hash table. */
170 /* Check end of hash table. */
171 test_val(ntdb, -1ULL);
173 /* Check mixed bitpattern. */
174 test_val(ntdb, 0x123456789ABCDEF0ULL);
176 ok1(!ntdb->file || (ntdb->file->allrecord_lock.count == 0
177 && ntdb->file->num_lockrecs == 0));
180 /* Deleting these entries in the db gave problems. */
181 ntdb = ntdb_open("run-13-delete.ntdb", flags[i]|MAYBE_NOSYNC,
182 O_RDWR|O_CREAT|O_TRUNC, 0600, &fixed_hattr);
187 ok1(store_records(ntdb));
188 ok1(ntdb_check(ntdb, NULL, NULL) == 0);
189 for (j = 0; j < sizeof(vals)/sizeof(vals[0]); j++) {
192 key.dptr = (unsigned char *)&vals[j];
193 key.dsize = sizeof(vals[j]);
194 ok1(ntdb_delete(ntdb, key) == 0);
195 ok1(ntdb_check(ntdb, NULL, NULL) == 0);
200 ok1(tap_log_messages == 0);
201 return exit_status();