]> git.ozlabs.org Git - ccan/blob - ccan/tdb/test/run-wronghash-old.c
tdb: rewrite external agent for testing.
[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         if (strstr(fmt, "wrong hash"))
26                 (*count)++;
27 }
28
29 /* The old code should barf on new-style TDBs created with a non-default hash.
30  */
31 int main(int argc, char *argv[])
32 {
33         struct tdb_context *tdb;
34         unsigned int log_count;
35         struct tdb_logging_context log_ctx = { log_fn, &log_count };
36
37         plan_tests(8);
38
39         /* We should fail to open new-style non-default-hash tdbs of
40          * either endian. */
41         log_count = 0;
42         tdb = tdb_open_ex("test/jenkins-le-hash.tdb", 0, 0, O_RDWR, 0,
43                           &log_ctx, NULL);
44         ok1(!tdb);
45         ok1(log_count == 1);
46
47         log_count = 0;
48         tdb = tdb_open_ex("test/jenkins-be-hash.tdb", 0, 0, O_RDWR, 0,
49                           &log_ctx, NULL);
50         ok1(!tdb);
51         ok1(log_count == 1);
52
53         /* And of course, if we use the wrong hash it will still fail. */
54         log_count = 0;
55         tdb = tdb_open_ex("test/jenkins-le-hash.tdb", 0, 0, O_RDWR, 0,
56                           &log_ctx, non_jenkins_hash);
57         ok1(!tdb);
58         ok1(log_count == 1);
59
60         log_count = 0;
61         tdb = tdb_open_ex("test/jenkins-be-hash.tdb", 0, 0, O_RDWR, 0,
62                           &log_ctx, non_jenkins_hash);
63         ok1(!tdb);
64         ok1(log_count == 1);
65
66         return exit_status();
67 }