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