]> git.ozlabs.org Git - ccan/blob - ccan/tdb/test/run-wronghash-old.c
tdb: test that new-style TDBs with non-default hashes can't be opened.
[ccan] / ccan / tdb / test / run-wronghash-old.c
1 #define _XOPEN_SOURCE 500
2 #include <ccan/tdb/tdb.h>
3 #include <ccan/tdb/io.c>
4 #include <ccan/tdb/tdb.c>
5 #include <ccan/tdb/lock.c>
6 #include <ccan/tdb/freelist.c>
7 #include <ccan/tdb/traverse.c>
8 #include <ccan/tdb/transaction.c>
9 #include <ccan/tdb/error.c>
10 #include <ccan/tdb/open.c>
11 #include <ccan/tdb/check.c>
12 #include <ccan/hash/hash.h>
13 #include <ccan/tap/tap.h>
14 #include <stdlib.h>
15 #include <err.h>
16
17 static unsigned int non_jenkins_hash(TDB_DATA *key)
18 {
19         return ~hash_stable(key->dptr, key->dsize, 0);
20 }
21
22 static void log_fn(struct tdb_context *tdb, enum tdb_debug_level level, const char *fmt, ...)
23 {
24         unsigned int *count = tdb_get_logging_private(tdb);
25         /* Old code used to complain about spinlocks on new databases. */
26         if (strstr(fmt, "spinlock"))
27                 (*count)++;
28 }
29
30 /* The old code should barf on new-style TDBs created with a non-default hash.
31  */
32 int main(int argc, char *argv[])
33 {
34         struct tdb_context *tdb;
35         unsigned int log_count;
36         struct tdb_logging_context log_ctx = { log_fn, &log_count };
37
38         plan_tests(8);
39
40         /* We should fail to open new-style non-default-hash tdbs of
41          * either endian. */
42         log_count = 0;
43         tdb = tdb_open_ex("test/jenkins-le-hash.tdb", 0, 0, O_RDWR, 0,
44                           &log_ctx, NULL);
45         ok1(!tdb);
46         ok1(log_count == 1);
47
48         log_count = 0;
49         tdb = tdb_open_ex("test/jenkins-be-hash.tdb", 0, 0, O_RDWR, 0,
50                           &log_ctx, NULL);
51         ok1(!tdb);
52         ok1(log_count == 1);
53
54         /* And of course, if we use the wrong hash it will still fail. */
55         log_count = 0;
56         tdb = tdb_open_ex("test/jenkins-le-hash.tdb", 0, 0, O_RDWR, 0,
57                           &log_ctx, non_jenkins_hash);
58         ok1(!tdb);
59         ok1(log_count == 1);
60
61         log_count = 0;
62         tdb = tdb_open_ex("test/jenkins-be-hash.tdb", 0, 0, O_RDWR, 0,
63                           &log_ctx, non_jenkins_hash);
64         ok1(!tdb);
65         ok1(log_count == 1);
66
67         return exit_status();
68 }