tdb: add test for tdb_summary
[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         TDB_DATA d;
29         struct tdb_logging_context log_ctx = { log_fn, &log_count };
30
31         plan_tests(28);
32
33         /* Create with default hash. */
34         log_count = 0;
35         tdb = tdb_open_ex("run-wronghash-fail.tdb", 0, 0,
36                           O_CREAT|O_RDWR|O_TRUNC, 0600, &log_ctx, NULL);
37         ok1(tdb);
38         ok1(log_count == 0);
39         d.dptr = (void *)"Hello";
40         d.dsize = 5;
41         ok1(tdb_store(tdb, d, d, TDB_INSERT) == 0);
42         tdb_close(tdb);
43
44         /* Fail to open with different hash. */
45         tdb = tdb_open_ex("run-wronghash-fail.tdb", 0, 0, O_RDWR, 0,
46                           &log_ctx, tdb_jenkins_hash);
47         ok1(!tdb);
48         ok1(log_count == 1);
49
50         /* Create with different hash. */
51         log_count = 0;
52         tdb = tdb_open_ex("run-wronghash-fail.tdb", 0, 0,
53                           O_CREAT|O_RDWR|O_TRUNC,
54                           0600, &log_ctx, tdb_jenkins_hash);
55         ok1(tdb);
56         ok1(log_count == 0);
57         tdb_close(tdb);
58
59         /* Endian should be no problem. */
60         log_count = 0;
61         tdb = tdb_open_ex("test/jenkins-le-hash.tdb", 0, 0, O_RDWR, 0,
62                           &log_ctx, tdb_old_hash);
63         ok1(!tdb);
64         ok1(log_count == 1);
65
66         log_count = 0;
67         tdb = tdb_open_ex("test/jenkins-be-hash.tdb", 0, 0, O_RDWR, 0,
68                           &log_ctx, tdb_old_hash);
69         ok1(!tdb);
70         ok1(log_count == 1);
71
72         log_count = 0;
73         /* Fail to open with old default hash. */
74         tdb = tdb_open_ex("run-wronghash-fail.tdb", 0, 0, O_RDWR, 0,
75                           &log_ctx, tdb_old_hash);
76         ok1(!tdb);
77         ok1(log_count == 1);
78
79         log_count = 0;
80         tdb = tdb_open_ex("test/jenkins-le-hash.tdb", 0, 0, O_RDONLY,
81                           0, &log_ctx, tdb_jenkins_hash);
82         ok1(tdb);
83         ok1(log_count == 0);
84         ok1(tdb_check(tdb, NULL, NULL) == 0);
85         tdb_close(tdb);
86
87         log_count = 0;
88         tdb = tdb_open_ex("test/jenkins-be-hash.tdb", 0, 0, O_RDONLY,
89                           0, &log_ctx, tdb_jenkins_hash);
90         ok1(tdb);
91         ok1(log_count == 0);
92         ok1(tdb_check(tdb, NULL, NULL) == 0);
93         tdb_close(tdb);
94
95         /* It should open with jenkins hash if we don't specify. */
96         log_count = 0;
97         tdb = tdb_open_ex("test/jenkins-le-hash.tdb", 0, 0, O_RDWR, 0,
98                           &log_ctx, NULL);
99         ok1(tdb);
100         ok1(log_count == 0);
101         ok1(tdb_check(tdb, NULL, NULL) == 0);
102         tdb_close(tdb);
103
104         log_count = 0;
105         tdb = tdb_open_ex("test/jenkins-be-hash.tdb", 0, 0, O_RDWR, 0,
106                           &log_ctx, NULL);
107         ok1(tdb);
108         ok1(log_count == 0);
109         ok1(tdb_check(tdb, NULL, NULL) == 0);
110         tdb_close(tdb);
111
112         log_count = 0;
113         tdb = tdb_open_ex("run-wronghash-fail.tdb", 0, 0, O_RDONLY,
114                           0, &log_ctx, NULL);
115         ok1(tdb);
116         ok1(log_count == 0);
117         ok1(tdb_check(tdb, NULL, NULL) == 0);
118         tdb_close(tdb);
119
120
121         return exit_status();
122 }