]> git.ozlabs.org Git - ccan/blob - ccan/tdb2/test/run-tdb1-incompatible.c
tdb2: merge tdb1_context into tdb_context.
[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         struct tdb1_logging_context log_ctx = { log_fn, &log_count };
53
54         plan_tests(38 * 2);
55
56         for (flags = 0; flags <= TDB_CONVERT; flags += TDB_CONVERT) {
57                 unsigned int rwmagic = TDB1_HASH_RWLOCK_MAGIC;
58
59                 if (flags & TDB_CONVERT)
60                         tdb1_convert(&rwmagic, sizeof(rwmagic));
61
62                 /* Create an old-style hash. */
63                 log_count = 0;
64                 tdb = tdb1_open_ex("run-incompatible.tdb", 0, flags,
65                                   O_CREAT|O_RDWR|O_TRUNC, 0600, &log_ctx,
66                                   NULL);
67                 ok1(tdb);
68                 ok1(log_count == 0);
69                 d.dptr = (void *)"Hello";
70                 d.dsize = 5;
71                 ok1(tdb1_store(tdb, d, d, TDB_INSERT) == 0);
72                 tdb1_close(tdb);
73
74                 /* Should not have marked rwlocks field. */
75                 ok1(hdr_rwlocks("run-incompatible.tdb") == 0);
76
77                 /* We can still open any old-style with incompat hash. */
78                 log_count = 0;
79                 tdb = tdb1_open_ex("run-incompatible.tdb", 0,
80                                   TDB_DEFAULT,
81                                   O_RDWR, 0600, &log_ctx,
82                                    tdb1_incompatible_hash);
83                 ok1(tdb);
84                 ok1(log_count == 0);
85                 d = tdb1_fetch(tdb, d);
86                 ok1(d.dsize == 5);
87                 free(d.dptr);
88                 ok1(tdb1_check(tdb, NULL, NULL) == 0);
89                 tdb1_close(tdb);
90
91                 log_count = 0;
92                 tdb = tdb1_open_ex("test/jenkins-le-hash.tdb1", 0, 0, O_RDONLY,
93                                   0, &log_ctx, jenkins_hashfn);
94                 ok1(tdb);
95                 ok1(log_count == 0);
96                 ok1(tdb1_check(tdb, NULL, NULL) == 0);
97                 tdb1_close(tdb);
98
99                 log_count = 0;
100                 tdb = tdb1_open_ex("test/jenkins-be-hash.tdb1", 0, 0, O_RDONLY,
101                                   0, &log_ctx, jenkins_hashfn);
102                 ok1(tdb);
103                 ok1(log_count == 0);
104                 ok1(tdb1_check(tdb, NULL, NULL) == 0);
105                 tdb1_close(tdb);
106
107                 /* OK, now create with incompatible hash. */
108                 log_count = 0;
109                 tdb = tdb1_open_ex("run-incompatible.tdb", 0,
110                                   flags,
111                                   O_CREAT|O_RDWR|O_TRUNC, 0600, &log_ctx,
112                                   tdb1_incompatible_hash);
113                 ok1(tdb);
114                 ok1(log_count == 0);
115                 d.dptr = (void *)"Hello";
116                 d.dsize = 5;
117                 ok1(tdb1_store(tdb, d, d, TDB_INSERT) == 0);
118                 tdb1_close(tdb);
119
120                 /* Should have marked rwlocks field. */
121                 ok1(hdr_rwlocks("run-incompatible.tdb") == rwmagic);
122
123                 /* Cannot open with old hash. */
124                 log_count = 0;
125                 tdb = tdb1_open_ex("run-incompatible.tdb", 0, 0,
126                                   O_RDWR, 0600, &log_ctx, old_hash);
127                 ok1(!tdb);
128                 ok1(log_count == 1);
129
130                 /* Can open with jenkins hash. */
131                 log_count = 0;
132                 tdb = tdb1_open_ex("run-incompatible.tdb", 0, 0,
133                                   O_RDWR, 0600, &log_ctx, jenkins_hashfn);
134                 ok1(tdb);
135                 ok1(log_count == 0);
136                 d = tdb1_fetch(tdb, d);
137                 ok1(d.dsize == 5);
138                 free(d.dptr);
139                 ok1(tdb1_check(tdb, NULL, NULL) == 0);
140                 tdb1_close(tdb);
141
142                 /* Can open by letting it figure it out itself. */
143                 log_count = 0;
144                 tdb = tdb1_open_ex("run-incompatible.tdb", 0, 0,
145                                   O_RDWR, 0600, &log_ctx, NULL);
146                 ok1(tdb);
147                 ok1(log_count == 0);
148                 d.dptr = (void *)"Hello";
149                 d.dsize = 5;
150                 d = tdb1_fetch(tdb, d);
151                 ok1(d.dsize == 5);
152                 free(d.dptr);
153                 ok1(tdb1_check(tdb, NULL, NULL) == 0);
154                 tdb1_close(tdb);
155
156                 /* FIXME: Not possible with TDB2 :( */
157                 /* We can also use incompatible hash with other hashes. */
158                 log_count = 0;
159                 tdb = tdb1_open_ex("run-incompatible.tdb", 0,
160                                   flags,
161                                   O_CREAT|O_RDWR|O_TRUNC, 0600, &log_ctx,
162                                   tdb1_dumb_hash);
163                 ok1(tdb);
164                 ok1(log_count == 0);
165                 d.dptr = (void *)"Hello";
166                 d.dsize = 5;
167                 ok1(tdb1_store(tdb, d, d, TDB_INSERT) == 0);
168                 tdb1_close(tdb);
169
170                 /* FIXME: Should have marked rwlocks field. */
171                 ok1(hdr_rwlocks("run-incompatible.tdb") != rwmagic);
172
173                 /* It should not open if we don't specify. */
174                 log_count = 0;
175                 tdb = tdb1_open_ex("run-incompatible.tdb", 0, 0, O_RDWR, 0,
176                                   &log_ctx, NULL);
177                 ok1(!tdb);
178                 ok1(log_count == 1);
179
180                 /* Should reopen with correct hash. */
181                 log_count = 0;
182                 tdb = tdb1_open_ex("run-incompatible.tdb", 0, 0, O_RDWR, 0,
183                                   &log_ctx, tdb1_dumb_hash);
184                 ok1(tdb);
185                 ok1(log_count == 0);
186                 d = tdb1_fetch(tdb, d);
187                 ok1(d.dsize == 5);
188                 free(d.dptr);
189                 ok1(tdb1_check(tdb, NULL, NULL) == 0);
190                 tdb1_close(tdb);
191         }
192
193         return exit_status();
194 }