1 #include "tdb2-source.h"
2 #include <ccan/tap/tap.h>
6 static void log_fn(struct tdb1_context *tdb, enum tdb_log_level level,
7 enum TDB_ERROR ecode, const char *message, void *priv)
9 unsigned int *count = priv;
10 if (strstr(message, "hash"))
14 static uint64_t jenkins_hashfn(const void *key, size_t len, uint64_t seed,
17 return hashlittle(key, len);
20 /* the tdb1_old_hash function is "magic" as it automatically makes us test the
21 * tdb1_incompatible_hash as well, so use this wrapper. */
22 static uint64_t old_hash(const void *key, size_t len, uint64_t seed,
25 return tdb1_old_hash(key, len, seed, unused);
28 int main(int argc, char *argv[])
30 struct tdb1_context *tdb;
31 unsigned int log_count;
33 struct tdb1_logging_context log_ctx = { log_fn, &log_count };
37 /* Create with default hash. */
39 tdb = tdb1_open_ex("run-wronghash-fail.tdb", 0, 0,
40 O_CREAT|O_RDWR|O_TRUNC, 0600, &log_ctx, NULL);
43 d.dptr = (void *)"Hello";
45 ok1(tdb1_store(tdb, d, d, TDB_INSERT) == 0);
48 /* Fail to open with different hash. */
49 tdb = tdb1_open_ex("run-wronghash-fail.tdb", 0, 0, O_RDWR, 0,
50 &log_ctx, jenkins_hashfn);
54 /* Create with different hash. */
56 tdb = tdb1_open_ex("run-wronghash-fail.tdb", 0, 0,
57 O_CREAT|O_RDWR|O_TRUNC,
58 0600, &log_ctx, jenkins_hashfn);
63 /* Endian should be no problem. */
65 tdb = tdb1_open_ex("test/jenkins-le-hash.tdb1", 0, 0, O_RDWR, 0,
71 tdb = tdb1_open_ex("test/jenkins-be-hash.tdb1", 0, 0, O_RDWR, 0,
77 /* Fail to open with old default hash. */
78 tdb = tdb1_open_ex("run-wronghash-fail.tdb", 0, 0, O_RDWR, 0,
84 tdb = tdb1_open_ex("test/jenkins-le-hash.tdb1", 0, 0, O_RDONLY,
85 0, &log_ctx, tdb1_incompatible_hash);
88 ok1(tdb1_check(tdb, NULL, NULL) == 0);
92 tdb = tdb1_open_ex("test/jenkins-be-hash.tdb1", 0, 0, O_RDONLY,
93 0, &log_ctx, tdb1_incompatible_hash);
96 ok1(tdb1_check(tdb, NULL, NULL) == 0);
99 /* It should open with jenkins hash if we don't specify. */
101 tdb = tdb1_open_ex("test/jenkins-le-hash.tdb1", 0, 0, O_RDWR, 0,
105 ok1(tdb1_check(tdb, NULL, NULL) == 0);
109 tdb = tdb1_open_ex("test/jenkins-be-hash.tdb1", 0, 0, O_RDWR, 0,
113 ok1(tdb1_check(tdb, NULL, NULL) == 0);
117 tdb = tdb1_open_ex("run-wronghash-fail.tdb", 0, 0, O_RDONLY,
121 ok1(tdb1_check(tdb, NULL, NULL) == 0);
125 return exit_status();