tdb: enforce hashing, via example hash in old rwlocks entry in header.
[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         /* Old code used to complain about spinlocks when we put something
26            here. */
27         if (strstr(fmt, "wrong hash") || strstr(fmt, "spinlock"))
28                 (*count)++;
29 }
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(16);
38
39         /* Create with default hash. */
40         log_count = 0;
41         tdb = tdb_open_ex("run-wronghash-fail.tdb", 0, 0,
42                           O_CREAT|O_RDWR|O_TRUNC, 0600, &log_ctx, NULL);
43         ok1(tdb);
44         ok1(log_count == 0);
45         tdb_close(tdb);
46
47         /* Fail to open with different hash. */
48         tdb = tdb_open_ex("run-wronghash-fail.tdb", 0, 0, O_RDWR, 0,
49                           &log_ctx, jenkins_hash);
50         ok1(!tdb);
51         ok1(log_count == 1);
52
53         /* Create with different hash. */
54         log_count = 0;
55         tdb = tdb_open_ex("run-wronghash-fail.tdb", 0, 0,
56                           O_CREAT|O_RDWR|O_TRUNC,
57                           0600, &log_ctx, jenkins_hash);
58         ok1(tdb);
59         ok1(log_count == 0);
60         tdb_close(tdb);
61
62         /* Endian should be no problem. */
63         log_count = 0;
64         tdb = tdb_open_ex("test/jenkins-le-hash.tdb", 0, 0, O_RDWR, 0,
65                           &log_ctx, NULL);
66         ok1(!tdb);
67         ok1(log_count == 1);
68
69         log_count = 0;
70         tdb = tdb_open_ex("test/jenkins-be-hash.tdb", 0, 0, O_RDWR, 0,
71                           &log_ctx, NULL);
72         ok1(!tdb);
73         ok1(log_count == 1);
74
75         log_count = 0;
76         /* Fail to open with defailt hash. */
77         tdb = tdb_open_ex("run-wronghash-fail.tdb", 0, 0, O_RDWR, 0,
78                           &log_ctx, NULL);
79         ok1(!tdb);
80         ok1(log_count == 1);
81
82         log_count = 0;
83         tdb = tdb_open_ex("test/jenkins-le-hash.tdb", 0, 0, O_RDONLY,
84                           0, &log_ctx, jenkins_hash);
85         ok1(tdb);
86         ok1(log_count == 0);
87         tdb_close(tdb);
88
89         log_count = 0;
90         tdb = tdb_open_ex("test/jenkins-be-hash.tdb", 0, 0, O_RDONLY,
91                           0, &log_ctx, jenkins_hash);
92         ok1(tdb);
93         ok1(log_count == 0);
94         tdb_close(tdb);
95
96         return exit_status();
97 }