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
= tdb_open("run-wronghash-fail.tdb1", TDB_VERSION1
,
58 O_CREAT
|O_RDWR
|O_TRUNC
, 0600, &log_attr
);
61 d
.dptr
= (void *)"Hello";
63 ok1(tdb_store(tdb
, d
, d
, TDB_INSERT
) == TDB_SUCCESS
);
66 /* Fail to open with different hash. */
67 tdb
= tdb_open("run-wronghash-fail.tdb1", TDB_VERSION1
, O_RDWR
, 0,
72 /* Create with different hash. */
74 tdb
= tdb_open("run-wronghash-fail.tdb1", TDB_VERSION1
,
75 O_CREAT
|O_RDWR
|O_TRUNC
, 0600, &jhash_attr
);
80 /* Endian should be no problem. */
82 tdb
= tdb_open("test/jenkins-le-hash.tdb1", TDB_VERSION1
, O_RDWR
, 0,
88 tdb
= tdb_open("test/jenkins-be-hash.tdb1", TDB_VERSION1
, O_RDWR
, 0,
94 /* Fail to open with old default hash. */
95 tdb
= tdb_open("run-wronghash-fail.tdb1", TDB_VERSION1
, O_RDWR
, 0,
101 tdb
= tdb_open("test/jenkins-le-hash.tdb1", TDB_VERSION1
, O_RDONLY
,
102 0, &incompat_hash_attr
);
105 ok1(tdb_check(tdb
, NULL
, NULL
) == TDB_SUCCESS
);
109 tdb
= tdb_open("test/jenkins-be-hash.tdb1", TDB_VERSION1
, O_RDONLY
,
110 0, &incompat_hash_attr
);
113 ok1(tdb_check(tdb
, NULL
, NULL
) == TDB_SUCCESS
);
116 /* It should open with jenkins hash if we don't specify. */
118 tdb
= tdb_open("test/jenkins-le-hash.tdb1", TDB_VERSION1
, O_RDWR
, 0,
122 ok1(tdb_check(tdb
, NULL
, NULL
) == TDB_SUCCESS
);
126 tdb
= tdb_open("test/jenkins-be-hash.tdb1", TDB_VERSION1
, O_RDWR
, 0,
130 ok1(tdb_check(tdb
, NULL
, NULL
) == TDB_SUCCESS
);
134 tdb
= tdb_open("run-wronghash-fail.tdb1", TDB_VERSION1
, O_RDONLY
,
138 ok1(tdb_check(tdb
, NULL
, NULL
) == TDB_SUCCESS
);
142 return exit_status();