]> git.ozlabs.org Git - ccan/blob - ccan/tdb2/test/run-tdb1-incompatible.c
350c78b2b4f12f881fbba41b203eea2d72408d94
[ccan] / ccan / tdb2 / test / run-tdb1-incompatible.c
1 #include "tdb2-source.h"
2 #include <ccan/tap/tap.h>
3 #include <stdlib.h>
4 #include <err.h>
5
6 static uint64_t tdb1_dumb_hash(const void *key, size_t len, uint64_t seed,
7                                void *unused)
8 {
9         return len;
10 }
11
12 static void log_fn(struct tdb_context *tdb, enum tdb_log_level level,
13                    enum TDB_ERROR ecode, const char *message, void *priv)
14 {
15         unsigned int *count = priv;
16         if (strstr(message, "hash"))
17                 (*count)++;
18 }
19
20 static unsigned int hdr_rwlocks(const char *fname)
21 {
22         struct tdb1_header hdr;
23
24         int fd = open(fname, O_RDONLY);
25         if (fd == -1)
26                 return -1;
27
28         if (read(fd, &hdr, sizeof(hdr)) != sizeof(hdr))
29                 return -1;
30
31         close(fd);
32         return hdr.rwlocks;
33 }
34
35 static uint64_t jenkins_hashfn(const void *key, size_t len, uint64_t seed,
36                                void *unused)
37 {
38         return hashlittle(key, len);
39 }
40
41 static uint64_t old_hash(const void *key, size_t len, uint64_t seed,
42                          void *unused)
43 {
44         return tdb1_old_hash(key, len, seed, unused);
45 }
46
47 int main(int argc, char *argv[])
48 {
49         struct tdb_context *tdb;
50         unsigned int log_count, flags;
51         TDB_DATA d;
52         union tdb_attribute log_attr, jhash_attr, ohash_attr,
53                 incompat_hash_attr, dumbhash_attr;
54
55         log_attr.base.attr = TDB_ATTRIBUTE_LOG;
56         log_attr.base.next = NULL;
57         log_attr.log.fn = log_fn;
58         log_attr.log.data = &log_count;
59
60         jhash_attr.base.attr = TDB_ATTRIBUTE_HASH;
61         jhash_attr.base.next = &log_attr;
62         jhash_attr.hash.fn = jenkins_hashfn;
63
64         ohash_attr.base.attr = TDB_ATTRIBUTE_HASH;
65         ohash_attr.base.next = &log_attr;
66         ohash_attr.hash.fn = old_hash;
67
68         incompat_hash_attr.base.attr = TDB_ATTRIBUTE_HASH;
69         incompat_hash_attr.base.next = &log_attr;
70         incompat_hash_attr.hash.fn = tdb1_incompatible_hash;
71
72         dumbhash_attr.base.attr = TDB_ATTRIBUTE_HASH;
73         dumbhash_attr.base.next = &log_attr;
74         dumbhash_attr.hash.fn = tdb1_dumb_hash;
75
76         plan_tests(38 * 2);
77
78         for (flags = 0; flags <= TDB_CONVERT; flags += TDB_CONVERT) {
79                 unsigned int rwmagic = TDB1_HASH_RWLOCK_MAGIC;
80
81                 if (flags & TDB_CONVERT)
82                         tdb1_convert(&rwmagic, sizeof(rwmagic));
83
84                 /* Create an old-style hash. */
85                 log_count = 0;
86                 tdb = tdb_open("run-incompatible.tdb1", flags|TDB_VERSION1,
87                                O_CREAT|O_RDWR|O_TRUNC, 0600, &log_attr);
88                 ok1(tdb);
89                 ok1(log_count == 0);
90                 d.dptr = (void *)"Hello";
91                 d.dsize = 5;
92                 ok1(tdb1_store(tdb, d, d, TDB_INSERT) == 0);
93                 tdb_close(tdb);
94
95                 /* Should not have marked rwlocks field. */
96                 ok1(hdr_rwlocks("run-incompatible.tdb1") == 0);
97
98                 /* We can still open any old-style with incompat hash. */
99                 log_count = 0;
100                 tdb = tdb_open("run-incompatible.tdb1",
101                                TDB_VERSION1,
102                                O_RDWR, 0600, &incompat_hash_attr);
103                 ok1(tdb);
104                 ok1(log_count == 0);
105                 d = tdb1_fetch(tdb, d);
106                 ok1(d.dsize == 5);
107                 free(d.dptr);
108                 ok1(tdb1_check(tdb, NULL, NULL) == 0);
109                 tdb_close(tdb);
110
111                 log_count = 0;
112                 tdb = tdb_open("test/jenkins-le-hash.tdb1",
113                                TDB_VERSION1, O_RDONLY, 0, &jhash_attr);
114                 ok1(tdb);
115                 ok1(log_count == 0);
116                 ok1(tdb1_check(tdb, NULL, NULL) == 0);
117                 tdb_close(tdb);
118
119                 log_count = 0;
120                 tdb = tdb_open("test/jenkins-be-hash.tdb1",
121                                TDB_VERSION1, O_RDONLY, 0, &jhash_attr);
122                 ok1(tdb);
123                 ok1(log_count == 0);
124                 ok1(tdb1_check(tdb, NULL, NULL) == 0);
125                 tdb_close(tdb);
126
127                 /* OK, now create with incompatible hash. */
128                 log_count = 0;
129                 tdb = tdb_open("run-incompatible.tdb1",
130                                flags|TDB_VERSION1,
131                                O_CREAT|O_RDWR|O_TRUNC, 0600,
132                                &incompat_hash_attr);
133                 ok1(tdb);
134                 ok1(log_count == 0);
135                 d.dptr = (void *)"Hello";
136                 d.dsize = 5;
137                 ok1(tdb1_store(tdb, d, d, TDB_INSERT) == 0);
138                 tdb_close(tdb);
139
140                 /* Should have marked rwlocks field. */
141                 ok1(hdr_rwlocks("run-incompatible.tdb1") == rwmagic);
142
143                 /* Cannot open with old hash. */
144                 log_count = 0;
145                 tdb = tdb_open("run-incompatible.tdb1", TDB_VERSION1,
146                                O_RDWR, 0600, &ohash_attr);
147                 ok1(!tdb);
148                 ok1(log_count == 1);
149
150                 /* Can open with jenkins hash. */
151                 log_count = 0;
152                 tdb = tdb_open("run-incompatible.tdb1", TDB_VERSION1,
153                                O_RDWR, 0600, &jhash_attr);
154                 ok1(tdb);
155                 ok1(log_count == 0);
156                 d = tdb1_fetch(tdb, d);
157                 ok1(d.dsize == 5);
158                 free(d.dptr);
159                 ok1(tdb1_check(tdb, NULL, NULL) == 0);
160                 tdb_close(tdb);
161
162                 /* Can open by letting it figure it out itself. */
163                 log_count = 0;
164                 tdb = tdb_open("run-incompatible.tdb1", TDB_VERSION1,
165                                O_RDWR, 0600, &log_attr);
166                 ok1(tdb);
167                 ok1(log_count == 0);
168                 d.dptr = (void *)"Hello";
169                 d.dsize = 5;
170                 d = tdb1_fetch(tdb, d);
171                 ok1(d.dsize == 5);
172                 free(d.dptr);
173                 ok1(tdb1_check(tdb, NULL, NULL) == 0);
174                 tdb_close(tdb);
175
176                 /* FIXME: Not possible with TDB2 :( */
177                 /* We can also use incompatible hash with other hashes. */
178                 log_count = 0;
179                 tdb = tdb_open("run-incompatible.tdb1",
180                                flags|TDB_VERSION1,
181                                O_CREAT|O_RDWR|O_TRUNC, 0600, &dumbhash_attr);
182                 ok1(tdb);
183                 ok1(log_count == 0);
184                 d.dptr = (void *)"Hello";
185                 d.dsize = 5;
186                 ok1(tdb1_store(tdb, d, d, TDB_INSERT) == 0);
187                 tdb_close(tdb);
188
189                 /* FIXME: Should have marked rwlocks field. */
190                 ok1(hdr_rwlocks("run-incompatible.tdb1") != rwmagic);
191
192                 /* It should not open if we don't specify. */
193                 log_count = 0;
194                 tdb = tdb_open("run-incompatible.tdb1", TDB_VERSION1, O_RDWR, 0,
195                                &log_attr);
196                 ok1(!tdb);
197                 ok1(log_count == 1);
198
199                 /* Should reopen with correct hash. */
200                 log_count = 0;
201                 tdb = tdb_open("run-incompatible.tdb1", TDB_VERSION1, O_RDWR, 0,
202                                &dumbhash_attr);
203                 ok1(tdb);
204                 ok1(log_count == 0);
205                 d = tdb1_fetch(tdb, d);
206                 ok1(d.dsize == 5);
207                 free(d.dptr);
208                 ok1(tdb1_check(tdb, NULL, NULL) == 0);
209                 tdb_close(tdb);
210         }
211
212         return exit_status();
213 }