]> git.ozlabs.org Git - ccan/blob - ccan/tdb2/test/run-tdb1-wronghash-fail.c
c7e789f2ca7a02f6dec0a87fdc3632fc315ba84d
[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         union tdb_attribute log_attr, jhash_attr, ohash_attr,
34                 incompat_hash_attr;
35
36         log_attr.base.attr = TDB_ATTRIBUTE_LOG;
37         log_attr.base.next = NULL;
38         log_attr.log.fn = log_fn;
39         log_attr.log.data = &log_count;
40
41         jhash_attr.base.attr = TDB_ATTRIBUTE_HASH;
42         jhash_attr.base.next = &log_attr;
43         jhash_attr.hash.fn = jenkins_hashfn;
44
45         ohash_attr.base.attr = TDB_ATTRIBUTE_HASH;
46         ohash_attr.base.next = &log_attr;
47         ohash_attr.hash.fn = old_hash;
48
49         incompat_hash_attr.base.attr = TDB_ATTRIBUTE_HASH;
50         incompat_hash_attr.base.next = &log_attr;
51         incompat_hash_attr.hash.fn = tdb1_incompatible_hash;
52
53         plan_tests(28);
54
55         /* Create with default hash. */
56         log_count = 0;
57         tdb = tdb_open("run-wronghash-fail.tdb1", TDB_VERSION1,
58                        O_CREAT|O_RDWR|O_TRUNC, 0600, &log_attr);
59         ok1(tdb);
60         ok1(log_count == 0);
61         d.dptr = (void *)"Hello";
62         d.dsize = 5;
63         ok1(tdb_store(tdb, d, d, TDB_INSERT) == TDB_SUCCESS);
64         tdb_close(tdb);
65
66         /* Fail to open with different hash. */
67         tdb = tdb_open("run-wronghash-fail.tdb1", TDB_VERSION1, O_RDWR, 0,
68                        &jhash_attr);
69         ok1(!tdb);
70         ok1(log_count == 1);
71
72         /* Create with different hash. */
73         log_count = 0;
74         tdb = tdb_open("run-wronghash-fail.tdb1", TDB_VERSION1,
75                        O_CREAT|O_RDWR|O_TRUNC, 0600, &jhash_attr);
76         ok1(tdb);
77         ok1(log_count == 0);
78         tdb_close(tdb);
79
80         /* Endian should be no problem. */
81         log_count = 0;
82         tdb = tdb_open("test/jenkins-le-hash.tdb1", TDB_VERSION1, O_RDWR, 0,
83                        &ohash_attr);
84         ok1(!tdb);
85         ok1(log_count == 1);
86
87         log_count = 0;
88         tdb = tdb_open("test/jenkins-be-hash.tdb1", TDB_VERSION1, O_RDWR, 0,
89                        &ohash_attr);
90         ok1(!tdb);
91         ok1(log_count == 1);
92
93         log_count = 0;
94         /* Fail to open with old default hash. */
95         tdb = tdb_open("run-wronghash-fail.tdb1", TDB_VERSION1, O_RDWR, 0,
96                        &ohash_attr);
97         ok1(!tdb);
98         ok1(log_count == 1);
99
100         log_count = 0;
101         tdb = tdb_open("test/jenkins-le-hash.tdb1", TDB_VERSION1, O_RDONLY,
102                        0, &incompat_hash_attr);
103         ok1(tdb);
104         ok1(log_count == 0);
105         ok1(tdb1_check(tdb, NULL, NULL) == 0);
106         tdb_close(tdb);
107
108         log_count = 0;
109         tdb = tdb_open("test/jenkins-be-hash.tdb1", TDB_VERSION1, O_RDONLY,
110                        0, &incompat_hash_attr);
111         ok1(tdb);
112         ok1(log_count == 0);
113         ok1(tdb1_check(tdb, NULL, NULL) == 0);
114         tdb_close(tdb);
115
116         /* It should open with jenkins hash if we don't specify. */
117         log_count = 0;
118         tdb = tdb_open("test/jenkins-le-hash.tdb1", TDB_VERSION1, O_RDWR, 0,
119                        &log_attr);
120         ok1(tdb);
121         ok1(log_count == 0);
122         ok1(tdb1_check(tdb, NULL, NULL) == 0);
123         tdb_close(tdb);
124
125         log_count = 0;
126         tdb = tdb_open("test/jenkins-be-hash.tdb1", TDB_VERSION1, O_RDWR, 0,
127                        &log_attr);
128         ok1(tdb);
129         ok1(log_count == 0);
130         ok1(tdb1_check(tdb, NULL, NULL) == 0);
131         tdb_close(tdb);
132
133         log_count = 0;
134         tdb = tdb_open("run-wronghash-fail.tdb1", TDB_VERSION1, O_RDONLY,
135                        0, &log_attr);
136         ok1(tdb);
137         ok1(log_count == 0);
138         ok1(tdb1_check(tdb, NULL, NULL) == 0);
139         tdb_close(tdb);
140
141
142         return exit_status();
143 }