]> git.ozlabs.org Git - ccan/blob - ccan/tdb2/test/run-tdb1-wronghash-fail.c
tdb2: add TDB_ATTRIBUTE_TDB1_HASHSIZE
[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 = tdb1_open("run-wronghash-fail.tdb", 0,
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(tdb1_store(tdb, d, d, TDB_INSERT) == 0);
64         tdb1_close(tdb);
65
66         /* Fail to open with different hash. */
67         tdb = tdb1_open("run-wronghash-fail.tdb", 0, 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 = tdb1_open("run-wronghash-fail.tdb", 0,
75                         O_CREAT|O_RDWR|O_TRUNC,
76                         0600, &jhash_attr);
77         ok1(tdb);
78         ok1(log_count == 0);
79         tdb1_close(tdb);
80
81         /* Endian should be no problem. */
82         log_count = 0;
83         tdb = tdb1_open("test/jenkins-le-hash.tdb1", 0, O_RDWR, 0,
84                         &ohash_attr);
85         ok1(!tdb);
86         ok1(log_count == 1);
87
88         log_count = 0;
89         tdb = tdb1_open("test/jenkins-be-hash.tdb1", 0, O_RDWR, 0,
90                         &ohash_attr);
91         ok1(!tdb);
92         ok1(log_count == 1);
93
94         log_count = 0;
95         /* Fail to open with old default hash. */
96         tdb = tdb1_open("run-wronghash-fail.tdb", 0, O_RDWR, 0,
97                         &ohash_attr);
98         ok1(!tdb);
99         ok1(log_count == 1);
100
101         log_count = 0;
102         tdb = tdb1_open("test/jenkins-le-hash.tdb1", 0, O_RDONLY,
103                         0, &incompat_hash_attr);
104         ok1(tdb);
105         ok1(log_count == 0);
106         ok1(tdb1_check(tdb, NULL, NULL) == 0);
107         tdb1_close(tdb);
108
109         log_count = 0;
110         tdb = tdb1_open("test/jenkins-be-hash.tdb1", 0, O_RDONLY,
111                         0, &incompat_hash_attr);
112         ok1(tdb);
113         ok1(log_count == 0);
114         ok1(tdb1_check(tdb, NULL, NULL) == 0);
115         tdb1_close(tdb);
116
117         /* It should open with jenkins hash if we don't specify. */
118         log_count = 0;
119         tdb = tdb1_open("test/jenkins-le-hash.tdb1", 0, O_RDWR, 0,
120                         &log_attr);
121         ok1(tdb);
122         ok1(log_count == 0);
123         ok1(tdb1_check(tdb, NULL, NULL) == 0);
124         tdb1_close(tdb);
125
126         log_count = 0;
127         tdb = tdb1_open("test/jenkins-be-hash.tdb1", 0, O_RDWR, 0,
128                         &log_attr);
129         ok1(tdb);
130         ok1(log_count == 0);
131         ok1(tdb1_check(tdb, NULL, NULL) == 0);
132         tdb1_close(tdb);
133
134         log_count = 0;
135         tdb = tdb1_open("run-wronghash-fail.tdb", 0, O_RDONLY,
136                         0, &log_attr);
137         ok1(tdb);
138         ok1(log_count == 0);
139         ok1(tdb1_check(tdb, NULL, NULL) == 0);
140         tdb1_close(tdb);
141
142
143         return exit_status();
144 }