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