1 #include "tdb2-source.h"
2 #include <ccan/tap/tap.h>
6 static unsigned int tdb1_dumb_hash(TDB1_DATA *key)
11 static void log_fn(struct tdb1_context *tdb, enum tdb1_debug_level level, const char *fmt, ...)
13 unsigned int *count = tdb1_get_logging_private(tdb);
14 if (strstr(fmt, "hash"))
18 static unsigned int hdr_rwlocks(const char *fname)
20 struct tdb1_header hdr;
22 int fd = open(fname, O_RDONLY);
26 if (read(fd, &hdr, sizeof(hdr)) != sizeof(hdr))
33 int main(int argc, char *argv[])
35 struct tdb1_context *tdb;
36 unsigned int log_count, flags;
38 struct tdb1_logging_context log_ctx = { log_fn, &log_count };
42 for (flags = 0; flags <= TDB1_CONVERT; flags += TDB1_CONVERT) {
43 unsigned int rwmagic = TDB1_HASH_RWLOCK_MAGIC;
45 if (flags & TDB1_CONVERT)
46 tdb1_convert(&rwmagic, sizeof(rwmagic));
48 /* Create an old-style hash. */
50 tdb = tdb1_open_ex("run-incompatible.tdb", 0, flags,
51 O_CREAT|O_RDWR|O_TRUNC, 0600, &log_ctx,
55 d.dptr = (void *)"Hello";
57 ok1(tdb1_store(tdb, d, d, TDB1_INSERT) == 0);
60 /* Should not have marked rwlocks field. */
61 ok1(hdr_rwlocks("run-incompatible.tdb") == 0);
63 /* We can still open any old-style with incompat flag. */
65 tdb = tdb1_open_ex("run-incompatible.tdb", 0,
66 TDB1_INCOMPATIBLE_HASH,
67 O_RDWR, 0600, &log_ctx, NULL);
70 d = tdb1_fetch(tdb, d);
73 ok1(tdb1_check(tdb, NULL, NULL) == 0);
77 tdb = tdb1_open_ex("test/jenkins-le-hash.tdb1", 0, 0, O_RDONLY,
78 0, &log_ctx, tdb1_jenkins_hash);
81 ok1(tdb1_check(tdb, NULL, NULL) == 0);
85 tdb = tdb1_open_ex("test/jenkins-be-hash.tdb1", 0, 0, O_RDONLY,
86 0, &log_ctx, tdb1_jenkins_hash);
89 ok1(tdb1_check(tdb, NULL, NULL) == 0);
92 /* OK, now create with incompatible flag, default hash. */
94 tdb = tdb1_open_ex("run-incompatible.tdb", 0,
95 flags|TDB1_INCOMPATIBLE_HASH,
96 O_CREAT|O_RDWR|O_TRUNC, 0600, &log_ctx,
100 d.dptr = (void *)"Hello";
102 ok1(tdb1_store(tdb, d, d, TDB1_INSERT) == 0);
105 /* Should have marked rwlocks field. */
106 ok1(hdr_rwlocks("run-incompatible.tdb") == rwmagic);
108 /* Cannot open with old hash. */
110 tdb = tdb1_open_ex("run-incompatible.tdb", 0, 0,
111 O_RDWR, 0600, &log_ctx, tdb1_old_hash);
115 /* Can open with jenkins hash. */
117 tdb = tdb1_open_ex("run-incompatible.tdb", 0, 0,
118 O_RDWR, 0600, &log_ctx, tdb1_jenkins_hash);
121 d = tdb1_fetch(tdb, d);
124 ok1(tdb1_check(tdb, NULL, NULL) == 0);
127 /* Can open by letting it figure it out itself. */
129 tdb = tdb1_open_ex("run-incompatible.tdb", 0, 0,
130 O_RDWR, 0600, &log_ctx, NULL);
133 d.dptr = (void *)"Hello";
135 d = tdb1_fetch(tdb, d);
138 ok1(tdb1_check(tdb, NULL, NULL) == 0);
141 /* We can also use incompatible hash with other hashes. */
143 tdb = tdb1_open_ex("run-incompatible.tdb", 0,
144 flags|TDB1_INCOMPATIBLE_HASH,
145 O_CREAT|O_RDWR|O_TRUNC, 0600, &log_ctx,
149 d.dptr = (void *)"Hello";
151 ok1(tdb1_store(tdb, d, d, TDB1_INSERT) == 0);
154 /* Should have marked rwlocks field. */
155 ok1(hdr_rwlocks("run-incompatible.tdb") == rwmagic);
157 /* It should not open if we don't specify. */
159 tdb = tdb1_open_ex("run-incompatible.tdb", 0, 0, O_RDWR, 0,
164 /* Should reopen with correct hash. */
166 tdb = tdb1_open_ex("run-incompatible.tdb", 0, 0, O_RDWR, 0,
167 &log_ctx, tdb1_dumb_hash);
170 d = tdb1_fetch(tdb, d);
173 ok1(tdb1_check(tdb, NULL, NULL) == 0);
177 return exit_status();