]> git.ozlabs.org Git - ccan/blob - ccan/ntdb/test/api-13-delete.c
ntdb: fix up tests.
[ccan] / ccan / ntdb / test / api-13-delete.c
1 #include "../private.h" // For NTDB_TOPLEVEL_HASH_BITS
2 #include <ccan/hash/hash.h>
3 #include "../ntdb.h"
4 #include "tap-interface.h"
5 #include "logging.h"
6 #include "helpapi-external-agent.h"
7
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)
10 {
11         return *((const unsigned int *)key) / 2;
12 }
13
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)
16 {
17         return hash64_stable((const unsigned char *)key, len,
18                              *(uint64_t *)p);
19 }
20
21 static bool store_records(struct ntdb_context *ntdb)
22 {
23         int i;
24         NTDB_DATA key = { (unsigned char *)&i, sizeof(i) };
25         NTDB_DATA d, data = { (unsigned char *)&i, sizeof(i) };
26
27         for (i = 0; i < 1000; i++) {
28                 if (ntdb_store(ntdb, key, data, NTDB_REPLACE) != 0)
29                         return false;
30                 ntdb_fetch(ntdb, key, &d);
31                 if (!ntdb_deq(d, data))
32                         return false;
33                 free(d.dptr);
34         }
35         return true;
36 }
37
38 static void test_val(struct ntdb_context *ntdb, uint64_t val)
39 {
40         uint64_t v;
41         NTDB_DATA key = { (unsigned char *)&v, sizeof(v) };
42         NTDB_DATA d, data = { (unsigned char *)&v, sizeof(v) };
43
44         /* Insert an entry, then delete it. */
45         v = val;
46         /* Delete should fail. */
47         ok1(ntdb_delete(ntdb, key) == NTDB_ERR_NOEXIST);
48         ok1(ntdb_check(ntdb, NULL, NULL) == 0);
49
50         /* Insert should succeed. */
51         ok1(ntdb_store(ntdb, key, data, NTDB_INSERT) == 0);
52         ok1(ntdb_check(ntdb, NULL, NULL) == 0);
53
54         /* Delete should succeed. */
55         ok1(ntdb_delete(ntdb, key) == 0);
56         ok1(ntdb_check(ntdb, NULL, NULL) == 0);
57
58         /* Re-add it, then add collision. */
59         ok1(ntdb_store(ntdb, key, data, NTDB_INSERT) == 0);
60         v = val + 1;
61         ok1(ntdb_store(ntdb, key, data, NTDB_INSERT) == 0);
62         ok1(ntdb_check(ntdb, NULL, NULL) == 0);
63
64         /* Can find both? */
65         ok1(ntdb_fetch(ntdb, key, &d) == NTDB_SUCCESS);
66         ok1(d.dsize == data.dsize);
67         free(d.dptr);
68         v = val;
69         ok1(ntdb_fetch(ntdb, key, &d) == NTDB_SUCCESS);
70         ok1(d.dsize == data.dsize);
71         free(d.dptr);
72
73         /* Delete second one. */
74         v = val + 1;
75         ok1(ntdb_delete(ntdb, key) == 0);
76         ok1(ntdb_check(ntdb, NULL, NULL) == 0);
77
78         /* Re-add */
79         ok1(ntdb_store(ntdb, key, data, NTDB_INSERT) == 0);
80         ok1(ntdb_check(ntdb, NULL, NULL) == 0);
81
82         /* Now, try deleting first one. */
83         v = val;
84         ok1(ntdb_delete(ntdb, key) == 0);
85         ok1(ntdb_check(ntdb, NULL, NULL) == 0);
86
87         /* Can still find second? */
88         v = val + 1;
89         ok1(ntdb_fetch(ntdb, key, &d) == NTDB_SUCCESS);
90         ok1(d.dsize == data.dsize);
91         free(d.dptr);
92
93         /* Now, this will be ideally placed. */
94         v = val + 2;
95         ok1(ntdb_store(ntdb, key, data, NTDB_INSERT) == 0);
96         ok1(ntdb_check(ntdb, NULL, NULL) == 0);
97
98         /* This will collide with both. */
99         v = val;
100         ok1(ntdb_store(ntdb, key, data, NTDB_INSERT) == 0);
101
102         /* We can still find them all, right? */
103         ok1(ntdb_fetch(ntdb, key, &d) == NTDB_SUCCESS);
104         ok1(d.dsize == data.dsize);
105         free(d.dptr);
106         v = val + 1;
107         ok1(ntdb_fetch(ntdb, key, &d) == NTDB_SUCCESS);
108         ok1(d.dsize == data.dsize);
109         free(d.dptr);
110         v = val + 2;
111         ok1(ntdb_fetch(ntdb, key, &d) == NTDB_SUCCESS);
112         ok1(d.dsize == data.dsize);
113         free(d.dptr);
114
115         /* And if we delete val + 1, that val + 2 should not move! */
116         v = val + 1;
117         ok1(ntdb_delete(ntdb, key) == 0);
118         ok1(ntdb_check(ntdb, NULL, NULL) == 0);
119
120         v = val;
121         ok1(ntdb_fetch(ntdb, key, &d) == NTDB_SUCCESS);
122         ok1(d.dsize == data.dsize);
123         free(d.dptr);
124         v = val + 2;
125         ok1(ntdb_fetch(ntdb, key, &d) == NTDB_SUCCESS);
126         ok1(d.dsize == data.dsize);
127         free(d.dptr);
128
129         /* Delete those two, so we are empty. */
130         ok1(ntdb_delete(ntdb, key) == 0);
131         v = val;
132         ok1(ntdb_delete(ntdb, key) == 0);
133
134         ok1(ntdb_check(ntdb, NULL, NULL) == 0);
135 }
136
137 int main(int argc, char *argv[])
138 {
139         unsigned int i, j;
140         struct ntdb_context *ntdb;
141         uint64_t seed = 16014841315512641303ULL;
142         union ntdb_attribute clash_hattr
143                 = { .hash = { .base = { NTDB_ATTRIBUTE_HASH },
144                               .fn = clash } };
145         union ntdb_attribute fixed_hattr
146                 = { .hash = { .base = { NTDB_ATTRIBUTE_HASH },
147                               .fn = fixedhash,
148                               .data = &seed } };
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 };
154
155         clash_hattr.base.next = &tap_log_attr;
156         fixed_hattr.base.next = &tap_log_attr;
157
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);
163                 ok1(ntdb);
164                 if (!ntdb)
165                         continue;
166
167                 /* Check start of hash table. */
168                 test_val(ntdb, 0);
169
170                 /* Check end of hash table. */
171                 test_val(ntdb, -1ULL);
172
173                 /* Check mixed bitpattern. */
174                 test_val(ntdb, 0x123456789ABCDEF0ULL);
175
176                 ok1(!ntdb->file || (ntdb->file->allrecord_lock.count == 0
177                                    && ntdb->file->num_lockrecs == 0));
178                 ntdb_close(ntdb);
179
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);
183                 ok1(ntdb);
184                 if (!ntdb)
185                         continue;
186
187                 ok1(store_records(ntdb));
188                 ok1(ntdb_check(ntdb, NULL, NULL) == 0);
189                 for (j = 0; j < sizeof(vals)/sizeof(vals[0]); j++) {
190                         NTDB_DATA key;
191
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);
196                 }
197                 ntdb_close(ntdb);
198         }
199
200         ok1(tap_log_messages == 0);
201         return exit_status();
202 }