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