]> git.ozlabs.org Git - ccan/blob - ccan/tdb2/test/run-tdb1-wronghash-fail.c
tdb2: merge tdb1_context into tdb_context.
[ccan] / ccan / tdb2 / test / run-tdb1-wronghash-fail.c
1 #include "tdb2-source.h"
2 #include <ccan/tap/tap.h>
3 #include <stdlib.h>
4 #include <err.h>
5
6 static void log_fn(struct tdb_context *tdb, enum tdb_log_level level,
7                    enum TDB_ERROR ecode, const char *message, void *priv)
8 {
9         unsigned int *count = priv;
10         if (strstr(message, "hash"))
11                 (*count)++;
12 }
13
14 static uint64_t jenkins_hashfn(const void *key, size_t len, uint64_t seed,
15                                void *unused)
16 {
17         return hashlittle(key, len);
18 }
19
20 /* the tdb1_old_hash function is "magic" as it automatically makes us test the
21  * tdb1_incompatible_hash as well, so use this wrapper. */
22 static uint64_t old_hash(const void *key, size_t len, uint64_t seed,
23                          void *unused)
24 {
25         return tdb1_old_hash(key, len, seed, unused);
26 }
27
28 int main(int argc, char *argv[])
29 {
30         struct tdb_context *tdb;
31         unsigned int log_count;
32         TDB_DATA d;
33         struct tdb1_logging_context log_ctx = { log_fn, &log_count };
34
35         plan_tests(28);
36
37         /* Create with default hash. */
38         log_count = 0;
39         tdb = tdb1_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         d.dptr = (void *)"Hello";
44         d.dsize = 5;
45         ok1(tdb1_store(tdb, d, d, TDB_INSERT) == 0);
46         tdb1_close(tdb);
47
48         /* Fail to open with different hash. */
49         tdb = tdb1_open_ex("run-wronghash-fail.tdb", 0, 0, O_RDWR, 0,
50                           &log_ctx, jenkins_hashfn);
51         ok1(!tdb);
52         ok1(log_count == 1);
53
54         /* Create with different hash. */
55         log_count = 0;
56         tdb = tdb1_open_ex("run-wronghash-fail.tdb", 0, 0,
57                           O_CREAT|O_RDWR|O_TRUNC,
58                           0600, &log_ctx, jenkins_hashfn);
59         ok1(tdb);
60         ok1(log_count == 0);
61         tdb1_close(tdb);
62
63         /* Endian should be no problem. */
64         log_count = 0;
65         tdb = tdb1_open_ex("test/jenkins-le-hash.tdb1", 0, 0, O_RDWR, 0,
66                           &log_ctx, old_hash);
67         ok1(!tdb);
68         ok1(log_count == 1);
69
70         log_count = 0;
71         tdb = tdb1_open_ex("test/jenkins-be-hash.tdb1", 0, 0, O_RDWR, 0,
72                           &log_ctx, old_hash);
73         ok1(!tdb);
74         ok1(log_count == 1);
75
76         log_count = 0;
77         /* Fail to open with old default hash. */
78         tdb = tdb1_open_ex("run-wronghash-fail.tdb", 0, 0, O_RDWR, 0,
79                           &log_ctx, old_hash);
80         ok1(!tdb);
81         ok1(log_count == 1);
82
83         log_count = 0;
84         tdb = tdb1_open_ex("test/jenkins-le-hash.tdb1", 0, 0, O_RDONLY,
85                           0, &log_ctx, tdb1_incompatible_hash);
86         ok1(tdb);
87         ok1(log_count == 0);
88         ok1(tdb1_check(tdb, NULL, NULL) == 0);
89         tdb1_close(tdb);
90
91         log_count = 0;
92         tdb = tdb1_open_ex("test/jenkins-be-hash.tdb1", 0, 0, O_RDONLY,
93                           0, &log_ctx, tdb1_incompatible_hash);
94         ok1(tdb);
95         ok1(log_count == 0);
96         ok1(tdb1_check(tdb, NULL, NULL) == 0);
97         tdb1_close(tdb);
98
99         /* It should open with jenkins hash if we don't specify. */
100         log_count = 0;
101         tdb = tdb1_open_ex("test/jenkins-le-hash.tdb1", 0, 0, O_RDWR, 0,
102                           &log_ctx, NULL);
103         ok1(tdb);
104         ok1(log_count == 0);
105         ok1(tdb1_check(tdb, NULL, NULL) == 0);
106         tdb1_close(tdb);
107
108         log_count = 0;
109         tdb = tdb1_open_ex("test/jenkins-be-hash.tdb1", 0, 0, O_RDWR, 0,
110                           &log_ctx, NULL);
111         ok1(tdb);
112         ok1(log_count == 0);
113         ok1(tdb1_check(tdb, NULL, NULL) == 0);
114         tdb1_close(tdb);
115
116         log_count = 0;
117         tdb = tdb1_open_ex("run-wronghash-fail.tdb", 0, 0, O_RDONLY,
118                           0, &log_ctx, NULL);
119         ok1(tdb);
120         ok1(log_count == 0);
121         ok1(tdb1_check(tdb, NULL, NULL) == 0);
122         tdb1_close(tdb);
123
124
125         return exit_status();
126 }