1 #include "tdb2-source.h"
2 #include <ccan/tap/tap.h>
6 static void log_fn(struct tdb_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 tdb_context *tdb;
31 unsigned int log_count;
33 union tdb_attribute log_attr, jhash_attr, ohash_attr,
36 log_attr.base.attr = TDB_ATTRIBUTE_LOG;
37 log_attr.base.next = NULL;
38 log_attr.log.fn = log_fn;
39 log_attr.log.data = &log_count;
41 jhash_attr.base.attr = TDB_ATTRIBUTE_HASH;
42 jhash_attr.base.next = &log_attr;
43 jhash_attr.hash.fn = jenkins_hashfn;
45 ohash_attr.base.attr = TDB_ATTRIBUTE_HASH;
46 ohash_attr.base.next = &log_attr;
47 ohash_attr.hash.fn = old_hash;
49 incompat_hash_attr.base.attr = TDB_ATTRIBUTE_HASH;
50 incompat_hash_attr.base.next = &log_attr;
51 incompat_hash_attr.hash.fn = tdb1_incompatible_hash;
55 /* Create with default hash. */
57 tdb = tdb1_open("run-wronghash-fail.tdb", 0, 0,
58 O_CREAT|O_RDWR|O_TRUNC, 0600, &log_attr);
61 d.dptr = (void *)"Hello";
63 ok1(tdb1_store(tdb, d, d, TDB_INSERT) == 0);
66 /* Fail to open with different hash. */
67 tdb = tdb1_open("run-wronghash-fail.tdb", 0, 0, O_RDWR, 0,
72 /* Create with different hash. */
74 tdb = tdb1_open("run-wronghash-fail.tdb", 0, 0,
75 O_CREAT|O_RDWR|O_TRUNC,
81 /* Endian should be no problem. */
83 tdb = tdb1_open("test/jenkins-le-hash.tdb1", 0, 0, O_RDWR, 0,
89 tdb = tdb1_open("test/jenkins-be-hash.tdb1", 0, 0, O_RDWR, 0,
95 /* Fail to open with old default hash. */
96 tdb = tdb1_open("run-wronghash-fail.tdb", 0, 0, O_RDWR, 0,
102 tdb = tdb1_open("test/jenkins-le-hash.tdb1", 0, 0, O_RDONLY,
103 0, &incompat_hash_attr);
106 ok1(tdb1_check(tdb, NULL, NULL) == 0);
110 tdb = tdb1_open("test/jenkins-be-hash.tdb1", 0, 0, O_RDONLY,
111 0, &incompat_hash_attr);
114 ok1(tdb1_check(tdb, NULL, NULL) == 0);
117 /* It should open with jenkins hash if we don't specify. */
119 tdb = tdb1_open("test/jenkins-le-hash.tdb1", 0, 0, O_RDWR, 0,
123 ok1(tdb1_check(tdb, NULL, NULL) == 0);
127 tdb = tdb1_open("test/jenkins-be-hash.tdb1", 0, 0, O_RDWR, 0,
131 ok1(tdb1_check(tdb, NULL, NULL) == 0);
135 tdb = tdb1_open("run-wronghash-fail.tdb", 0, 0, O_RDONLY,
139 ok1(tdb1_check(tdb, NULL, NULL) == 0);
143 return exit_status();