tdb: put example hashes into header, so we notice incorrect hash_fn.
[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/hash/hash.h>
13 #include <ccan/tap/tap.h>
14 #include <stdlib.h>
15 #include <err.h>
16
17 static unsigned int 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, "hash"))
26                 (*count)++;
27 }
28
29 int main(int argc, char *argv[])
30 {
31         struct tdb_context *tdb;
32         unsigned int log_count;
33         struct tdb_logging_context log_ctx = { log_fn, &log_count };
34
35         plan_tests(18);
36
37         /* Create with default hash. */
38         log_count = 0;
39         tdb = tdb_open_ex("run-wronghash-fail.tdb", 0, 0,
40                           O_CREAT|O_RDWR|O_TRUNC, 0600, &log_ctx, NULL);
41         ok1(tdb);
42         ok1(log_count == 0);
43         tdb_close(tdb);
44
45         /* Fail to open with different hash. */
46         tdb = tdb_open_ex("run-wronghash-fail.tdb", 0, 0, O_RDWR, 0,
47                           &log_ctx, jenkins_hash);
48         ok1(!tdb);
49         ok1(log_count == 1);
50
51         /* Create with different hash. */
52         log_count = 0;
53         tdb = tdb_open_ex("run-wronghash-fail.tdb", 0, 0,
54                           O_CREAT|O_RDWR|O_TRUNC,
55                           0600, &log_ctx, jenkins_hash);
56         ok1(tdb);
57         ok1(log_count == 0);
58         tdb_close(tdb);
59
60         /* Endian should be no problem. */
61         log_count = 0;
62         tdb = tdb_open_ex("test/jenkins-le-hash.tdb", 0, 0, O_RDWR, 0,
63                           &log_ctx, NULL);
64         ok1(!tdb);
65         ok1(log_count == 1);
66
67         log_count = 0;
68         tdb = tdb_open_ex("test/jenkins-be-hash.tdb", 0, 0, O_RDWR, 0,
69                           &log_ctx, NULL);
70         ok1(!tdb);
71         ok1(log_count == 1);
72
73         log_count = 0;
74         /* Fail to open with defailt hash. */
75         tdb = tdb_open_ex("run-wronghash-fail.tdb", 0, 0, O_RDWR, 0,
76                           &log_ctx, NULL);
77         ok1(!tdb);
78         ok1(log_count == 1);
79
80         log_count = 0;
81         tdb = tdb_open_ex("test/jenkins-le-hash.tdb", 0, 0, O_RDONLY,
82                           0, &log_ctx, jenkins_hash);
83         ok1(tdb);
84         ok1(log_count == 0);
85         ok1(tdb_check(tdb, NULL, NULL) == 0);
86         tdb_close(tdb);
87
88         log_count = 0;
89         tdb = tdb_open_ex("test/jenkins-be-hash.tdb", 0, 0, O_RDONLY,
90                           0, &log_ctx, jenkins_hash);
91         ok1(tdb);
92         ok1(log_count == 0);
93         ok1(tdb_check(tdb, NULL, NULL) == 0);
94         tdb_close(tdb);
95
96         return exit_status();
97 }