tdb2: test: try (almost) all tests with TDB_VERSION1 flag.
[ccan] / ccan / tdb2 / test / run-13-delete.c
1 #include "tdb2-source.h"
2 #include <ccan/tap/tap.h>
3 #include "logging.h"
4
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)
7 {
8         return ((uint64_t)*(const unsigned int *)key)
9                 << (64 - TDB_TOPLEVEL_HASH_BITS - 1);
10 }
11
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)
14 {
15         return hash64_stable((const unsigned char *)key, len,
16                              *(uint64_t *)p);
17 }
18
19 static bool store_records(struct tdb_context *tdb)
20 {
21         int i;
22         struct tdb_data key = { (unsigned char *)&i, sizeof(i) };
23         struct tdb_data d, data = { (unsigned char *)&i, sizeof(i) };
24
25         for (i = 0; i < 1000; i++) {
26                 if (tdb_store(tdb, key, data, TDB_REPLACE) != 0)
27                         return false;
28                 tdb_fetch(tdb, key, &d);
29                 if (!tdb_deq(d, data))
30                         return false;
31                 free(d.dptr);
32         }
33         return true;
34 }
35
36 static void test_val(struct tdb_context *tdb, uint64_t val)
37 {
38         uint64_t v;
39         struct tdb_data key = { (unsigned char *)&v, sizeof(v) };
40         struct tdb_data d, data = { (unsigned char *)&v, sizeof(v) };
41
42         /* Insert an entry, then delete it. */
43         v = val;
44         /* Delete should fail. */
45         ok1(tdb_delete(tdb, key) == TDB_ERR_NOEXIST);
46         ok1(tdb_check(tdb, NULL, NULL) == 0);
47
48         /* Insert should succeed. */
49         ok1(tdb_store(tdb, key, data, TDB_INSERT) == 0);
50         ok1(tdb_check(tdb, NULL, NULL) == 0);
51
52         /* Delete should succeed. */
53         ok1(tdb_delete(tdb, key) == 0);
54         ok1(tdb_check(tdb, NULL, NULL) == 0);
55
56         /* Re-add it, then add collision. */
57         ok1(tdb_store(tdb, key, data, TDB_INSERT) == 0);
58         v = val + 1;
59         ok1(tdb_store(tdb, key, data, TDB_INSERT) == 0);
60         ok1(tdb_check(tdb, NULL, NULL) == 0);
61
62         /* Can find both? */
63         ok1(tdb_fetch(tdb, key, &d) == TDB_SUCCESS);
64         ok1(d.dsize == data.dsize);
65         free(d.dptr);
66         v = val;
67         ok1(tdb_fetch(tdb, key, &d) == TDB_SUCCESS);
68         ok1(d.dsize == data.dsize);
69         free(d.dptr);
70
71         /* Delete second one. */
72         v = val + 1;
73         ok1(tdb_delete(tdb, key) == 0);
74         ok1(tdb_check(tdb, NULL, NULL) == 0);
75
76         /* Re-add */
77         ok1(tdb_store(tdb, key, data, TDB_INSERT) == 0);
78         ok1(tdb_check(tdb, NULL, NULL) == 0);
79
80         /* Now, try deleting first one. */
81         v = val;
82         ok1(tdb_delete(tdb, key) == 0);
83         ok1(tdb_check(tdb, NULL, NULL) == 0);
84
85         /* Can still find second? */
86         v = val + 1;
87         ok1(tdb_fetch(tdb, key, &d) == TDB_SUCCESS);
88         ok1(d.dsize == data.dsize);
89         free(d.dptr);
90
91         /* Now, this will be ideally placed. */
92         v = val + 2;
93         ok1(tdb_store(tdb, key, data, TDB_INSERT) == 0);
94         ok1(tdb_check(tdb, NULL, NULL) == 0);
95
96         /* This will collide with both. */
97         v = val;
98         ok1(tdb_store(tdb, key, data, TDB_INSERT) == 0);
99
100         /* We can still find them all, right? */
101         ok1(tdb_fetch(tdb, key, &d) == TDB_SUCCESS);
102         ok1(d.dsize == data.dsize);
103         free(d.dptr);
104         v = val + 1;
105         ok1(tdb_fetch(tdb, key, &d) == TDB_SUCCESS);
106         ok1(d.dsize == data.dsize);
107         free(d.dptr);
108         v = val + 2;
109         ok1(tdb_fetch(tdb, key, &d) == TDB_SUCCESS);
110         ok1(d.dsize == data.dsize);
111         free(d.dptr);
112
113         /* And if we delete val + 1, that val + 2 should not move! */
114         v = val + 1;
115         ok1(tdb_delete(tdb, key) == 0);
116         ok1(tdb_check(tdb, NULL, NULL) == 0);
117
118         v = val;
119         ok1(tdb_fetch(tdb, key, &d) == TDB_SUCCESS);
120         ok1(d.dsize == data.dsize);
121         free(d.dptr);
122         v = val + 2;
123         ok1(tdb_fetch(tdb, key, &d) == TDB_SUCCESS);
124         ok1(d.dsize == data.dsize);
125         free(d.dptr);
126
127         /* Delete those two, so we are empty. */
128         ok1(tdb_delete(tdb, key) == 0);
129         v = val;
130         ok1(tdb_delete(tdb, key) == 0);
131
132         ok1(tdb_check(tdb, NULL, NULL) == 0);
133 }
134
135 int main(int argc, char *argv[])
136 {
137         unsigned int i, j;
138         struct tdb_context *tdb;
139         uint64_t seed = 16014841315512641303ULL;
140         union tdb_attribute clash_hattr
141                 = { .hash = { .base = { TDB_ATTRIBUTE_HASH },
142                               .fn = clash } };
143         union tdb_attribute fixed_hattr
144                 = { .hash = { .base = { TDB_ATTRIBUTE_HASH },
145                               .fn = fixedhash,
146                               .data = &seed } };
147         int flags[] = { TDB_INTERNAL, TDB_DEFAULT, TDB_NOMMAP,
148                         TDB_INTERNAL|TDB_CONVERT, TDB_CONVERT,
149                         TDB_NOMMAP|TDB_CONVERT,
150                         TDB_INTERNAL|TDB_VERSION1, TDB_VERSION1,
151                         TDB_NOMMAP|TDB_VERSION1,
152                         TDB_INTERNAL|TDB_CONVERT|TDB_VERSION1,
153                         TDB_CONVERT|TDB_VERSION1,
154                         TDB_NOMMAP|TDB_CONVERT|TDB_VERSION1 };
155         /* These two values gave trouble before. */
156         int vals[] = { 755, 837 };
157
158         clash_hattr.base.next = &tap_log_attr;
159         fixed_hattr.base.next = &tap_log_attr;
160
161         plan_tests(sizeof(flags) / sizeof(flags[0])
162                    * (39 * 3 + 5 + sizeof(vals)/sizeof(vals[0])*2) + 1);
163         for (i = 0; i < sizeof(flags) / sizeof(flags[0]); i++) {
164                 tdb = tdb_open("run-13-delete.tdb", flags[i],
165                                O_RDWR|O_CREAT|O_TRUNC, 0600, &clash_hattr);
166                 ok1(tdb);
167                 if (!tdb)
168                         continue;
169
170                 /* Check start of hash table. */
171                 test_val(tdb, 0);
172
173                 /* Check end of hash table. */
174                 test_val(tdb, -1ULL);
175
176                 /* Check mixed bitpattern. */
177                 test_val(tdb, 0x123456789ABCDEF0ULL);
178
179                 ok1(!tdb->file || (tdb->file->allrecord_lock.count == 0
180                                    && tdb->file->num_lockrecs == 0));
181                 tdb_close(tdb);
182
183                 /* Deleting these entries in the db gave problems. */
184                 tdb = tdb_open("run-13-delete.tdb", flags[i],
185                                O_RDWR|O_CREAT|O_TRUNC, 0600, &fixed_hattr);
186                 ok1(tdb);
187                 if (!tdb)
188                         continue;
189
190                 ok1(store_records(tdb));
191                 ok1(tdb_check(tdb, NULL, NULL) == 0);
192                 for (j = 0; j < sizeof(vals)/sizeof(vals[0]); j++) {
193                         struct tdb_data key;
194
195                         key.dptr = (unsigned char *)&vals[j];
196                         key.dsize = sizeof(vals[j]);
197                         ok1(tdb_delete(tdb, key) == 0);
198                         ok1(tdb_check(tdb, NULL, NULL) == 0);
199                 }
200                 tdb_close(tdb);
201         }
202
203         ok1(tap_log_messages == 0);
204         return exit_status();
205 }