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 tdb_log_level level,
12 enum TDB_ERROR ecode, const char *message, void *priv)
14 unsigned int *count = priv;
15 if (strstr(message, "hash"))
19 static unsigned int hdr_rwlocks(const char *fname)
21 struct tdb1_header hdr;
23 int fd = open(fname, O_RDONLY);
27 if (read(fd, &hdr, sizeof(hdr)) != sizeof(hdr))
34 int main(int argc, char *argv[])
36 struct tdb1_context *tdb;
37 unsigned int log_count, flags;
39 struct tdb1_logging_context log_ctx = { log_fn, &log_count };
43 for (flags = 0; flags <= TDB1_CONVERT; flags += TDB1_CONVERT) {
44 unsigned int rwmagic = TDB1_HASH_RWLOCK_MAGIC;
46 if (flags & TDB1_CONVERT)
47 tdb1_convert(&rwmagic, sizeof(rwmagic));
49 /* Create an old-style hash. */
51 tdb = tdb1_open_ex("run-incompatible.tdb", 0, flags,
52 O_CREAT|O_RDWR|O_TRUNC, 0600, &log_ctx,
56 d.dptr = (void *)"Hello";
58 ok1(tdb1_store(tdb, d, d, TDB1_INSERT) == 0);
61 /* Should not have marked rwlocks field. */
62 ok1(hdr_rwlocks("run-incompatible.tdb") == 0);
64 /* We can still open any old-style with incompat flag. */
66 tdb = tdb1_open_ex("run-incompatible.tdb", 0,
67 TDB1_INCOMPATIBLE_HASH,
68 O_RDWR, 0600, &log_ctx, NULL);
71 d = tdb1_fetch(tdb, d);
74 ok1(tdb1_check(tdb, NULL, NULL) == 0);
78 tdb = tdb1_open_ex("test/jenkins-le-hash.tdb1", 0, 0, O_RDONLY,
79 0, &log_ctx, tdb1_jenkins_hash);
82 ok1(tdb1_check(tdb, NULL, NULL) == 0);
86 tdb = tdb1_open_ex("test/jenkins-be-hash.tdb1", 0, 0, O_RDONLY,
87 0, &log_ctx, tdb1_jenkins_hash);
90 ok1(tdb1_check(tdb, NULL, NULL) == 0);
93 /* OK, now create with incompatible flag, default hash. */
95 tdb = tdb1_open_ex("run-incompatible.tdb", 0,
96 flags|TDB1_INCOMPATIBLE_HASH,
97 O_CREAT|O_RDWR|O_TRUNC, 0600, &log_ctx,
101 d.dptr = (void *)"Hello";
103 ok1(tdb1_store(tdb, d, d, TDB1_INSERT) == 0);
106 /* Should have marked rwlocks field. */
107 ok1(hdr_rwlocks("run-incompatible.tdb") == rwmagic);
109 /* Cannot open with old hash. */
111 tdb = tdb1_open_ex("run-incompatible.tdb", 0, 0,
112 O_RDWR, 0600, &log_ctx, tdb1_old_hash);
116 /* Can open with jenkins hash. */
118 tdb = tdb1_open_ex("run-incompatible.tdb", 0, 0,
119 O_RDWR, 0600, &log_ctx, tdb1_jenkins_hash);
122 d = tdb1_fetch(tdb, d);
125 ok1(tdb1_check(tdb, NULL, NULL) == 0);
128 /* Can open by letting it figure it out itself. */
130 tdb = tdb1_open_ex("run-incompatible.tdb", 0, 0,
131 O_RDWR, 0600, &log_ctx, NULL);
134 d.dptr = (void *)"Hello";
136 d = tdb1_fetch(tdb, d);
139 ok1(tdb1_check(tdb, NULL, NULL) == 0);
142 /* We can also use incompatible hash with other hashes. */
144 tdb = tdb1_open_ex("run-incompatible.tdb", 0,
145 flags|TDB1_INCOMPATIBLE_HASH,
146 O_CREAT|O_RDWR|O_TRUNC, 0600, &log_ctx,
150 d.dptr = (void *)"Hello";
152 ok1(tdb1_store(tdb, d, d, TDB1_INSERT) == 0);
155 /* Should have marked rwlocks field. */
156 ok1(hdr_rwlocks("run-incompatible.tdb") == rwmagic);
158 /* It should not open if we don't specify. */
160 tdb = tdb1_open_ex("run-incompatible.tdb", 0, 0, O_RDWR, 0,
165 /* Should reopen with correct hash. */
167 tdb = tdb1_open_ex("run-incompatible.tdb", 0, 0, O_RDWR, 0,
168 &log_ctx, tdb1_dumb_hash);
171 d = tdb1_fetch(tdb, d);
174 ok1(tdb1_check(tdb, NULL, NULL) == 0);
178 return exit_status();