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 unsigned int jenkins_hashfn(TDB_DATA *key)
16 return hashlittle(key->dptr, key->dsize);
19 /* the tdb1_old_hash function is "magic" as it automatically makes us test the
20 * tdb1_incompatible_hash as well, so use this wrapper. */
21 static unsigned int old_hash(TDB_DATA *key)
23 return tdb1_old_hash(key);
26 int main(int argc, char *argv[])
28 struct tdb1_context *tdb;
29 unsigned int log_count;
31 struct tdb1_logging_context log_ctx = { log_fn, &log_count };
35 /* Create with default hash. */
37 tdb = tdb1_open_ex("run-wronghash-fail.tdb", 0, 0,
38 O_CREAT|O_RDWR|O_TRUNC, 0600, &log_ctx, NULL);
41 d.dptr = (void *)"Hello";
43 ok1(tdb1_store(tdb, d, d, TDB_INSERT) == 0);
46 /* Fail to open with different hash. */
47 tdb = tdb1_open_ex("run-wronghash-fail.tdb", 0, 0, O_RDWR, 0,
48 &log_ctx, jenkins_hashfn);
52 /* Create with different hash. */
54 tdb = tdb1_open_ex("run-wronghash-fail.tdb", 0, 0,
55 O_CREAT|O_RDWR|O_TRUNC,
56 0600, &log_ctx, jenkins_hashfn);
61 /* Endian should be no problem. */
63 tdb = tdb1_open_ex("test/jenkins-le-hash.tdb1", 0, 0, O_RDWR, 0,
69 tdb = tdb1_open_ex("test/jenkins-be-hash.tdb1", 0, 0, O_RDWR, 0,
75 /* Fail to open with old default hash. */
76 tdb = tdb1_open_ex("run-wronghash-fail.tdb", 0, 0, O_RDWR, 0,
82 tdb = tdb1_open_ex("test/jenkins-le-hash.tdb1", 0, 0, O_RDONLY,
83 0, &log_ctx, tdb1_incompatible_hash);
86 ok1(tdb1_check(tdb, NULL, NULL) == 0);
90 tdb = tdb1_open_ex("test/jenkins-be-hash.tdb1", 0, 0, O_RDONLY,
91 0, &log_ctx, tdb1_incompatible_hash);
94 ok1(tdb1_check(tdb, NULL, NULL) == 0);
97 /* It should open with jenkins hash if we don't specify. */
99 tdb = tdb1_open_ex("test/jenkins-le-hash.tdb1", 0, 0, O_RDWR, 0,
103 ok1(tdb1_check(tdb, NULL, NULL) == 0);
107 tdb = tdb1_open_ex("test/jenkins-be-hash.tdb1", 0, 0, O_RDWR, 0,
111 ok1(tdb1_check(tdb, NULL, NULL) == 0);
115 tdb = tdb1_open_ex("run-wronghash-fail.tdb", 0, 0, O_RDONLY,
119 ok1(tdb1_check(tdb, NULL, NULL) == 0);
123 return exit_status();