1 #include "tdb2-source.h"
2 #include <ccan/tap/tap.h>
6 static uint64_t tdb1_dumb_hash(const void *key, size_t len, uint64_t seed,
12 static void log_fn(struct tdb1_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"))
20 static unsigned int hdr_rwlocks(const char *fname)
22 struct tdb1_header hdr;
24 int fd = open(fname, O_RDONLY);
28 if (read(fd, &hdr, sizeof(hdr)) != sizeof(hdr))
35 static uint64_t jenkins_hashfn(const void *key, size_t len, uint64_t seed,
38 return hashlittle(key, len);
41 static uint64_t old_hash(const void *key, size_t len, uint64_t seed,
44 return tdb1_old_hash(key, len, seed, unused);
47 int main(int argc, char *argv[])
49 struct tdb1_context *tdb;
50 unsigned int log_count, flags;
52 struct tdb1_logging_context log_ctx = { log_fn, &log_count };
56 for (flags = 0; flags <= TDB_CONVERT; flags += TDB_CONVERT) {
57 unsigned int rwmagic = TDB1_HASH_RWLOCK_MAGIC;
59 if (flags & TDB_CONVERT)
60 tdb1_convert(&rwmagic, sizeof(rwmagic));
62 /* Create an old-style hash. */
64 tdb = tdb1_open_ex("run-incompatible.tdb", 0, flags,
65 O_CREAT|O_RDWR|O_TRUNC, 0600, &log_ctx,
69 d.dptr = (void *)"Hello";
71 ok1(tdb1_store(tdb, d, d, TDB_INSERT) == 0);
74 /* Should not have marked rwlocks field. */
75 ok1(hdr_rwlocks("run-incompatible.tdb") == 0);
77 /* We can still open any old-style with incompat hash. */
79 tdb = tdb1_open_ex("run-incompatible.tdb", 0,
81 O_RDWR, 0600, &log_ctx,
82 tdb1_incompatible_hash);
85 d = tdb1_fetch(tdb, d);
88 ok1(tdb1_check(tdb, NULL, NULL) == 0);
92 tdb = tdb1_open_ex("test/jenkins-le-hash.tdb1", 0, 0, O_RDONLY,
93 0, &log_ctx, jenkins_hashfn);
96 ok1(tdb1_check(tdb, NULL, NULL) == 0);
100 tdb = tdb1_open_ex("test/jenkins-be-hash.tdb1", 0, 0, O_RDONLY,
101 0, &log_ctx, jenkins_hashfn);
104 ok1(tdb1_check(tdb, NULL, NULL) == 0);
107 /* OK, now create with incompatible hash. */
109 tdb = tdb1_open_ex("run-incompatible.tdb", 0,
111 O_CREAT|O_RDWR|O_TRUNC, 0600, &log_ctx,
112 tdb1_incompatible_hash);
115 d.dptr = (void *)"Hello";
117 ok1(tdb1_store(tdb, d, d, TDB_INSERT) == 0);
120 /* Should have marked rwlocks field. */
121 ok1(hdr_rwlocks("run-incompatible.tdb") == rwmagic);
123 /* Cannot open with old hash. */
125 tdb = tdb1_open_ex("run-incompatible.tdb", 0, 0,
126 O_RDWR, 0600, &log_ctx, old_hash);
130 /* Can open with jenkins hash. */
132 tdb = tdb1_open_ex("run-incompatible.tdb", 0, 0,
133 O_RDWR, 0600, &log_ctx, jenkins_hashfn);
136 d = tdb1_fetch(tdb, d);
139 ok1(tdb1_check(tdb, NULL, NULL) == 0);
142 /* Can open by letting it figure it out itself. */
144 tdb = tdb1_open_ex("run-incompatible.tdb", 0, 0,
145 O_RDWR, 0600, &log_ctx, NULL);
148 d.dptr = (void *)"Hello";
150 d = tdb1_fetch(tdb, d);
153 ok1(tdb1_check(tdb, NULL, NULL) == 0);
156 /* FIXME: Not possible with TDB2 :( */
157 /* We can also use incompatible hash with other hashes. */
159 tdb = tdb1_open_ex("run-incompatible.tdb", 0,
161 O_CREAT|O_RDWR|O_TRUNC, 0600, &log_ctx,
165 d.dptr = (void *)"Hello";
167 ok1(tdb1_store(tdb, d, d, TDB_INSERT) == 0);
170 /* FIXME: Should have marked rwlocks field. */
171 ok1(hdr_rwlocks("run-incompatible.tdb") != rwmagic);
173 /* It should not open if we don't specify. */
175 tdb = tdb1_open_ex("run-incompatible.tdb", 0, 0, O_RDWR, 0,
180 /* Should reopen with correct hash. */
182 tdb = tdb1_open_ex("run-incompatible.tdb", 0, 0, O_RDWR, 0,
183 &log_ctx, tdb1_dumb_hash);
186 d = tdb1_fetch(tdb, d);
189 ok1(tdb1_check(tdb, NULL, NULL) == 0);
193 return exit_status();