tdb: add Bob Jenkins lookup3 hash as helper hash.
[ccan] / ccan / tdb / test / run-wronghash-fail.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/tdb/hash.c>
13 #include <ccan/tap/tap.h>
14 #include <stdlib.h>
15 #include <err.h>
16
17 static void log_fn(struct tdb_context *tdb, enum tdb_debug_level level, const char *fmt, ...)
18 {
19         unsigned int *count = tdb_get_logging_private(tdb);
20         if (strstr(fmt, "hash"))
21                 (*count)++;
22 }
23
24 int main(int argc, char *argv[])
25 {
26         struct tdb_context *tdb;
27         unsigned int log_count;
28         struct tdb_logging_context log_ctx = { log_fn, &log_count };
29
30         plan_tests(18);
31
32         /* Create with default hash. */
33         log_count = 0;
34         tdb = tdb_open_ex("run-wronghash-fail.tdb", 0, 0,
35                           O_CREAT|O_RDWR|O_TRUNC, 0600, &log_ctx, NULL);
36         ok1(tdb);
37         ok1(log_count == 0);
38         tdb_close(tdb);
39
40         /* Fail to open with different hash. */
41         tdb = tdb_open_ex("run-wronghash-fail.tdb", 0, 0, O_RDWR, 0,
42                           &log_ctx, tdb_jenkins_hash);
43         ok1(!tdb);
44         ok1(log_count == 1);
45
46         /* Create with different hash. */
47         log_count = 0;
48         tdb = tdb_open_ex("run-wronghash-fail.tdb", 0, 0,
49                           O_CREAT|O_RDWR|O_TRUNC,
50                           0600, &log_ctx, tdb_jenkins_hash);
51         ok1(tdb);
52         ok1(log_count == 0);
53         tdb_close(tdb);
54
55         /* Endian should be no problem. */
56         log_count = 0;
57         tdb = tdb_open_ex("test/jenkins-le-hash.tdb", 0, 0, O_RDWR, 0,
58                           &log_ctx, NULL);
59         ok1(!tdb);
60         ok1(log_count == 1);
61
62         log_count = 0;
63         tdb = tdb_open_ex("test/jenkins-be-hash.tdb", 0, 0, O_RDWR, 0,
64                           &log_ctx, NULL);
65         ok1(!tdb);
66         ok1(log_count == 1);
67
68         log_count = 0;
69         /* Fail to open with default hash. */
70         tdb = tdb_open_ex("run-wronghash-fail.tdb", 0, 0, O_RDWR, 0,
71                           &log_ctx, NULL);
72         ok1(!tdb);
73         ok1(log_count == 1);
74
75         log_count = 0;
76         tdb = tdb_open_ex("test/jenkins-le-hash.tdb", 0, 0, O_RDONLY,
77                           0, &log_ctx, tdb_jenkins_hash);
78         ok1(tdb);
79         ok1(log_count == 0);
80         ok1(tdb_check(tdb, NULL, NULL) == 0);
81         tdb_close(tdb);
82
83         log_count = 0;
84         tdb = tdb_open_ex("test/jenkins-be-hash.tdb", 0, 0, O_RDONLY,
85                           0, &log_ctx, tdb_jenkins_hash);
86         ok1(tdb);
87         ok1(log_count == 0);
88         ok1(tdb_check(tdb, NULL, NULL) == 0);
89         tdb_close(tdb);
90
91         return exit_status();
92 }