s3:registry: do not use regdb functions during db upgrade
[Samba/gebeck_regimport.git] / lib / tdb2 / test / run-tdb1-incompatible.c
blob46ab5669207b4e02c75fd88fd4afce25e11f04b7
1 #include "tdb2-source.h"
2 #include <ccan/tap/tap.h>
3 #include <stdlib.h>
4 #include <err.h>
6 static uint64_t tdb1_dumb_hash(const void *key, size_t len, uint64_t seed,
7 void *unused)
9 return len;
12 static void log_fn(struct tdb_context *tdb, enum tdb_log_level level,
13 enum TDB_ERROR ecode, const char *message, void *priv)
15 unsigned int *count = priv;
16 if (strstr(message, "hash"))
17 (*count)++;
20 static unsigned int hdr_rwlocks(const char *fname)
22 struct tdb1_header hdr;
24 int fd = open(fname, O_RDONLY);
25 if (fd == -1)
26 return -1;
28 if (read(fd, &hdr, sizeof(hdr)) != sizeof(hdr))
29 return -1;
31 close(fd);
32 return hdr.rwlocks;
35 static uint64_t jenkins_hashfn(const void *key, size_t len, uint64_t seed,
36 void *unused)
38 return hashlittle(key, len);
41 static uint64_t old_hash(const void *key, size_t len, uint64_t seed,
42 void *unused)
44 return tdb1_old_hash(key, len, seed, unused);
47 int main(int argc, char *argv[])
49 struct tdb_context *tdb;
50 unsigned int log_count, flags;
51 TDB_DATA d;
52 union tdb_attribute log_attr, jhash_attr, ohash_attr,
53 incompat_hash_attr, dumbhash_attr;
55 log_attr.base.attr = TDB_ATTRIBUTE_LOG;
56 log_attr.base.next = NULL;
57 log_attr.log.fn = log_fn;
58 log_attr.log.data = &log_count;
60 jhash_attr.base.attr = TDB_ATTRIBUTE_HASH;
61 jhash_attr.base.next = &log_attr;
62 jhash_attr.hash.fn = jenkins_hashfn;
64 ohash_attr.base.attr = TDB_ATTRIBUTE_HASH;
65 ohash_attr.base.next = &log_attr;
66 ohash_attr.hash.fn = old_hash;
68 incompat_hash_attr.base.attr = TDB_ATTRIBUTE_HASH;
69 incompat_hash_attr.base.next = &log_attr;
70 incompat_hash_attr.hash.fn = tdb1_incompatible_hash;
72 dumbhash_attr.base.attr = TDB_ATTRIBUTE_HASH;
73 dumbhash_attr.base.next = &log_attr;
74 dumbhash_attr.hash.fn = tdb1_dumb_hash;
76 plan_tests(42 * 2);
78 for (flags = 0; flags <= TDB_CONVERT; flags += TDB_CONVERT) {
79 unsigned int rwmagic = TDB1_HASH_RWLOCK_MAGIC;
81 if (flags & TDB_CONVERT)
82 tdb1_convert(&rwmagic, sizeof(rwmagic));
84 /* Create an old-style hash. */
85 log_count = 0;
86 tdb = tdb_open("run-incompatible.tdb1", flags|TDB_VERSION1,
87 O_CREAT|O_RDWR|O_TRUNC, 0600, &log_attr);
88 ok1(tdb);
89 ok1(log_count == 0);
90 d.dptr = (void *)"Hello";
91 d.dsize = 5;
92 ok1(tdb_store(tdb, d, d, TDB_INSERT) == TDB_SUCCESS);
93 tdb_close(tdb);
95 /* Should not have marked rwlocks field. */
96 ok1(hdr_rwlocks("run-incompatible.tdb1") == 0);
98 /* We can still open any old-style with incompat hash. */
99 log_count = 0;
100 tdb = tdb_open("run-incompatible.tdb1",
101 TDB_VERSION1,
102 O_RDWR, 0600, &incompat_hash_attr);
103 ok1(tdb);
104 ok1(log_count == 0);
105 ok1(tdb_fetch(tdb, d, &d) == TDB_SUCCESS);
106 ok1(d.dsize == 5);
107 free(d.dptr);
108 ok1(tdb_check(tdb, NULL, NULL) == TDB_SUCCESS);
109 tdb_close(tdb);
111 log_count = 0;
112 tdb = tdb_open("test/jenkins-le-hash.tdb1",
113 TDB_VERSION1, O_RDONLY, 0, &jhash_attr);
114 ok1(tdb);
115 ok1(log_count == 0);
116 ok1(tdb_check(tdb, NULL, NULL) == TDB_SUCCESS);
117 tdb_close(tdb);
119 log_count = 0;
120 tdb = tdb_open("test/jenkins-be-hash.tdb1",
121 TDB_VERSION1, O_RDONLY, 0, &jhash_attr);
122 ok1(tdb);
123 ok1(log_count == 0);
124 ok1(tdb_check(tdb, NULL, NULL) == TDB_SUCCESS);
125 tdb_close(tdb);
127 /* OK, now create with incompatible hash. */
128 log_count = 0;
129 tdb = tdb_open("run-incompatible.tdb1",
130 flags|TDB_VERSION1,
131 O_CREAT|O_RDWR|O_TRUNC, 0600,
132 &incompat_hash_attr);
133 ok1(tdb);
134 ok1(log_count == 0);
135 d.dptr = (void *)"Hello";
136 d.dsize = 5;
137 ok1(tdb_store(tdb, d, d, TDB_INSERT) == TDB_SUCCESS);
138 tdb_close(tdb);
140 /* Should have marked rwlocks field. */
141 ok1(hdr_rwlocks("run-incompatible.tdb1") == rwmagic);
143 /* Cannot open with old hash. */
144 log_count = 0;
145 tdb = tdb_open("run-incompatible.tdb1", TDB_VERSION1,
146 O_RDWR, 0600, &ohash_attr);
147 ok1(!tdb);
148 ok1(log_count == 1);
150 /* Can open with jenkins hash. */
151 log_count = 0;
152 tdb = tdb_open("run-incompatible.tdb1", TDB_VERSION1,
153 O_RDWR, 0600, &jhash_attr);
154 ok1(tdb);
155 ok1(log_count == 0);
156 ok1(tdb_fetch(tdb, d, &d) == TDB_SUCCESS);
157 ok1(d.dsize == 5);
158 free(d.dptr);
159 ok1(tdb_check(tdb, NULL, NULL) == TDB_SUCCESS);
160 tdb_close(tdb);
162 /* Can open by letting it figure it out itself. */
163 log_count = 0;
164 tdb = tdb_open("run-incompatible.tdb1", TDB_VERSION1,
165 O_RDWR, 0600, &log_attr);
166 ok1(tdb);
167 ok1(log_count == 0);
168 d.dptr = (void *)"Hello";
169 d.dsize = 5;
170 ok1(tdb_fetch(tdb, d, &d) == TDB_SUCCESS);
171 ok1(d.dsize == 5);
172 free(d.dptr);
173 ok1(tdb_check(tdb, NULL, NULL) == TDB_SUCCESS);
174 tdb_close(tdb);
176 /* FIXME: Not possible with TDB2 :( */
177 /* We can also use incompatible hash with other hashes. */
178 log_count = 0;
179 tdb = tdb_open("run-incompatible.tdb1",
180 flags|TDB_VERSION1,
181 O_CREAT|O_RDWR|O_TRUNC, 0600, &dumbhash_attr);
182 ok1(tdb);
183 ok1(log_count == 0);
184 d.dptr = (void *)"Hello";
185 d.dsize = 5;
186 ok1(tdb_store(tdb, d, d, TDB_INSERT) == TDB_SUCCESS);
187 tdb_close(tdb);
189 /* FIXME: Should have marked rwlocks field. */
190 ok1(hdr_rwlocks("run-incompatible.tdb1") != rwmagic);
192 /* It should not open if we don't specify. */
193 log_count = 0;
194 tdb = tdb_open("run-incompatible.tdb1", TDB_VERSION1, O_RDWR, 0,
195 &log_attr);
196 ok1(!tdb);
197 ok1(log_count == 1);
199 /* Should reopen with correct hash. */
200 log_count = 0;
201 tdb = tdb_open("run-incompatible.tdb1", TDB_VERSION1, O_RDWR, 0,
202 &dumbhash_attr);
203 ok1(tdb);
204 ok1(log_count == 0);
205 ok1(tdb_fetch(tdb, d, &d) == TDB_SUCCESS);
206 ok1(d.dsize == 5);
207 free(d.dptr);
208 ok1(tdb_check(tdb, NULL, NULL) == TDB_SUCCESS);
209 tdb_close(tdb);
212 return exit_status();