Raise the level of a debug.
[Samba.git] / lib / ntdb / test / api-13-delete.c
blobcded8fde0bdafc3b4a0b1ad522459541a342fc24
1 #include "private.h" // For NTDB_TOPLEVEL_HASH_BITS
2 #include <ccan/hash/hash.h>
3 #include <sys/types.h>
4 #include <sys/stat.h>
5 #include <fcntl.h>
6 #include "ntdb.h"
7 #include "tap-interface.h"
8 #include "logging.h"
10 /* We rig the hash so adjacent-numbered records always clash. */
11 static uint32_t clash(const void *key, size_t len, uint32_t seed, void *priv)
13 return *((const unsigned int *)key) / 2;
16 /* We use the same seed which we saw a failure on. */
17 static uint32_t fixedhash(const void *key, size_t len, uint32_t seed, void *p)
19 return hash64_stable((const unsigned char *)key, len,
20 *(uint64_t *)p);
23 static bool store_records(struct ntdb_context *ntdb)
25 int i;
26 NTDB_DATA key = { (unsigned char *)&i, sizeof(i) };
27 NTDB_DATA d, data = { (unsigned char *)&i, sizeof(i) };
29 for (i = 0; i < 1000; i++) {
30 if (ntdb_store(ntdb, key, data, NTDB_REPLACE) != 0)
31 return false;
32 ntdb_fetch(ntdb, key, &d);
33 if (!ntdb_deq(d, data))
34 return false;
35 free(d.dptr);
37 return true;
40 static void test_val(struct ntdb_context *ntdb, uint64_t val)
42 uint64_t v;
43 NTDB_DATA key = { (unsigned char *)&v, sizeof(v) };
44 NTDB_DATA d, data = { (unsigned char *)&v, sizeof(v) };
46 /* Insert an entry, then delete it. */
47 v = val;
48 /* Delete should fail. */
49 ok1(ntdb_delete(ntdb, key) == NTDB_ERR_NOEXIST);
50 ok1(ntdb_check(ntdb, NULL, NULL) == 0);
52 /* Insert should succeed. */
53 ok1(ntdb_store(ntdb, key, data, NTDB_INSERT) == 0);
54 ok1(ntdb_check(ntdb, NULL, NULL) == 0);
56 /* Delete should succeed. */
57 ok1(ntdb_delete(ntdb, key) == 0);
58 ok1(ntdb_check(ntdb, NULL, NULL) == 0);
60 /* Re-add it, then add collision. */
61 ok1(ntdb_store(ntdb, key, data, NTDB_INSERT) == 0);
62 v = val + 1;
63 ok1(ntdb_store(ntdb, key, data, NTDB_INSERT) == 0);
64 ok1(ntdb_check(ntdb, NULL, NULL) == 0);
66 /* Can find both? */
67 ok1(ntdb_fetch(ntdb, key, &d) == NTDB_SUCCESS);
68 ok1(d.dsize == data.dsize);
69 free(d.dptr);
70 v = val;
71 ok1(ntdb_fetch(ntdb, key, &d) == NTDB_SUCCESS);
72 ok1(d.dsize == data.dsize);
73 free(d.dptr);
75 /* Delete second one. */
76 v = val + 1;
77 ok1(ntdb_delete(ntdb, key) == 0);
78 ok1(ntdb_check(ntdb, NULL, NULL) == 0);
80 /* Re-add */
81 ok1(ntdb_store(ntdb, key, data, NTDB_INSERT) == 0);
82 ok1(ntdb_check(ntdb, NULL, NULL) == 0);
84 /* Now, try deleting first one. */
85 v = val;
86 ok1(ntdb_delete(ntdb, key) == 0);
87 ok1(ntdb_check(ntdb, NULL, NULL) == 0);
89 /* Can still find second? */
90 v = val + 1;
91 ok1(ntdb_fetch(ntdb, key, &d) == NTDB_SUCCESS);
92 ok1(d.dsize == data.dsize);
93 free(d.dptr);
95 /* Now, this will be ideally placed. */
96 v = val + 2;
97 ok1(ntdb_store(ntdb, key, data, NTDB_INSERT) == 0);
98 ok1(ntdb_check(ntdb, NULL, NULL) == 0);
100 /* This will collide with both. */
101 v = val;
102 ok1(ntdb_store(ntdb, key, data, NTDB_INSERT) == 0);
104 /* We can still find them all, right? */
105 ok1(ntdb_fetch(ntdb, key, &d) == NTDB_SUCCESS);
106 ok1(d.dsize == data.dsize);
107 free(d.dptr);
108 v = val + 1;
109 ok1(ntdb_fetch(ntdb, key, &d) == NTDB_SUCCESS);
110 ok1(d.dsize == data.dsize);
111 free(d.dptr);
112 v = val + 2;
113 ok1(ntdb_fetch(ntdb, key, &d) == NTDB_SUCCESS);
114 ok1(d.dsize == data.dsize);
115 free(d.dptr);
117 /* And if we delete val + 1, that val + 2 should not move! */
118 v = val + 1;
119 ok1(ntdb_delete(ntdb, key) == 0);
120 ok1(ntdb_check(ntdb, NULL, NULL) == 0);
122 v = val;
123 ok1(ntdb_fetch(ntdb, key, &d) == NTDB_SUCCESS);
124 ok1(d.dsize == data.dsize);
125 free(d.dptr);
126 v = val + 2;
127 ok1(ntdb_fetch(ntdb, key, &d) == NTDB_SUCCESS);
128 ok1(d.dsize == data.dsize);
129 free(d.dptr);
131 /* Delete those two, so we are empty. */
132 ok1(ntdb_delete(ntdb, key) == 0);
133 v = val;
134 ok1(ntdb_delete(ntdb, key) == 0);
136 ok1(ntdb_check(ntdb, NULL, NULL) == 0);
139 int main(int argc, char *argv[])
141 unsigned int i, j;
142 struct ntdb_context *ntdb;
143 uint64_t seed = 16014841315512641303ULL;
144 union ntdb_attribute clash_hattr
145 = { .hash = { .base = { NTDB_ATTRIBUTE_HASH },
146 .fn = clash } };
147 union ntdb_attribute fixed_hattr
148 = { .hash = { .base = { NTDB_ATTRIBUTE_HASH },
149 .fn = fixedhash,
150 .data = &seed } };
151 int flags[] = { NTDB_INTERNAL, NTDB_DEFAULT, NTDB_NOMMAP,
152 NTDB_INTERNAL|NTDB_CONVERT, NTDB_CONVERT,
153 NTDB_NOMMAP|NTDB_CONVERT };
154 /* These two values gave trouble before. */
155 int vals[] = { 755, 837 };
157 clash_hattr.base.next = &tap_log_attr;
158 fixed_hattr.base.next = &tap_log_attr;
160 plan_tests(sizeof(flags) / sizeof(flags[0])
161 * (39 * 3 + 5 + sizeof(vals)/sizeof(vals[0])*2) + 1);
162 for (i = 0; i < sizeof(flags) / sizeof(flags[0]); i++) {
163 ntdb = ntdb_open("run-13-delete.ntdb", flags[i]|MAYBE_NOSYNC,
164 O_RDWR|O_CREAT|O_TRUNC, 0600, &clash_hattr);
165 ok1(ntdb);
166 if (!ntdb)
167 continue;
169 /* Check start of hash table. */
170 test_val(ntdb, 0);
172 /* Check end of hash table. */
173 test_val(ntdb, -1ULL);
175 /* Check mixed bitpattern. */
176 test_val(ntdb, 0x123456789ABCDEF0ULL);
178 ok1(!ntdb->file || (ntdb->file->allrecord_lock.count == 0
179 && ntdb->file->num_lockrecs == 0));
180 ntdb_close(ntdb);
182 /* Deleting these entries in the db gave problems. */
183 ntdb = ntdb_open("run-13-delete.ntdb", flags[i]|MAYBE_NOSYNC,
184 O_RDWR|O_CREAT|O_TRUNC, 0600, &fixed_hattr);
185 ok1(ntdb);
186 if (!ntdb)
187 continue;
189 ok1(store_records(ntdb));
190 ok1(ntdb_check(ntdb, NULL, NULL) == 0);
191 for (j = 0; j < sizeof(vals)/sizeof(vals[0]); j++) {
192 NTDB_DATA key;
194 key.dptr = (unsigned char *)&vals[j];
195 key.dsize = sizeof(vals[j]);
196 ok1(ntdb_delete(ntdb, key) == 0);
197 ok1(ntdb_check(ntdb, NULL, NULL) == 0);
199 ntdb_close(ntdb);
202 ok1(tap_log_messages == 0);
203 return exit_status();