1 #include "tdb2-source.h"
2 #include "tap-interface.h"
5 static void log_fn(struct tdb_context
*tdb
, enum tdb_log_level level
,
6 enum TDB_ERROR ecode
, const char *message
, void *priv
)
8 unsigned int *count
= priv
;
9 if (strstr(message
, "hash"))
13 static uint64_t jenkins_hashfn(const void *key
, size_t len
, uint64_t seed
,
16 return hashlittle(key
, len
);
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 uint64_t old_hash(const void *key
, size_t len
, uint64_t seed
,
24 return tdb1_old_hash(key
, len
, seed
, unused
);
27 int main(int argc
, char *argv
[])
29 struct tdb_context
*tdb
;
30 unsigned int log_count
;
32 union tdb_attribute log_attr
, jhash_attr
, ohash_attr
,
35 log_attr
.base
.attr
= TDB_ATTRIBUTE_LOG
;
36 log_attr
.base
.next
= NULL
;
37 log_attr
.log
.fn
= log_fn
;
38 log_attr
.log
.data
= &log_count
;
40 jhash_attr
.base
.attr
= TDB_ATTRIBUTE_HASH
;
41 jhash_attr
.base
.next
= &log_attr
;
42 jhash_attr
.hash
.fn
= jenkins_hashfn
;
44 ohash_attr
.base
.attr
= TDB_ATTRIBUTE_HASH
;
45 ohash_attr
.base
.next
= &log_attr
;
46 ohash_attr
.hash
.fn
= old_hash
;
48 incompat_hash_attr
.base
.attr
= TDB_ATTRIBUTE_HASH
;
49 incompat_hash_attr
.base
.next
= &log_attr
;
50 incompat_hash_attr
.hash
.fn
= tdb1_incompatible_hash
;
54 /* Create with default hash. */
56 tdb
= tdb_open("run-wronghash-fail.tdb1", TDB_VERSION1
,
57 O_CREAT
|O_RDWR
|O_TRUNC
, 0600, &log_attr
);
60 d
= tdb_mkdata("Hello", strlen("Hello"));
61 ok1(tdb_store(tdb
, d
, d
, TDB_INSERT
) == TDB_SUCCESS
);
64 /* Fail to open with different hash. */
65 tdb
= tdb_open("run-wronghash-fail.tdb1", TDB_VERSION1
, O_RDWR
, 0,
70 /* Create with different hash. */
72 tdb
= tdb_open("run-wronghash-fail.tdb1", TDB_VERSION1
,
73 O_CREAT
|O_RDWR
|O_TRUNC
, 0600, &jhash_attr
);
78 /* Endian should be no problem. */
80 tdb
= tdb_open("test/jenkins-le-hash.tdb1", TDB_VERSION1
, O_RDWR
, 0,
86 tdb
= tdb_open("test/jenkins-be-hash.tdb1", TDB_VERSION1
, O_RDWR
, 0,
92 /* Fail to open with old default hash. */
93 tdb
= tdb_open("run-wronghash-fail.tdb1", TDB_VERSION1
, O_RDWR
, 0,
99 tdb
= tdb_open("test/jenkins-le-hash.tdb1", TDB_VERSION1
, O_RDONLY
,
100 0, &incompat_hash_attr
);
103 ok1(tdb_check(tdb
, NULL
, NULL
) == TDB_SUCCESS
);
107 tdb
= tdb_open("test/jenkins-be-hash.tdb1", TDB_VERSION1
, O_RDONLY
,
108 0, &incompat_hash_attr
);
111 ok1(tdb_check(tdb
, NULL
, NULL
) == TDB_SUCCESS
);
114 /* It should open with jenkins hash if we don't specify. */
116 tdb
= tdb_open("test/jenkins-le-hash.tdb1", TDB_VERSION1
, O_RDWR
, 0,
120 ok1(tdb_check(tdb
, NULL
, NULL
) == TDB_SUCCESS
);
124 tdb
= tdb_open("test/jenkins-be-hash.tdb1", TDB_VERSION1
, O_RDWR
, 0,
128 ok1(tdb_check(tdb
, NULL
, NULL
) == TDB_SUCCESS
);
132 tdb
= tdb_open("run-wronghash-fail.tdb1", TDB_VERSION1
, O_RDONLY
,
136 ok1(tdb_check(tdb
, NULL
, NULL
) == TDB_SUCCESS
);
140 return exit_status();