]> git.ozlabs.org Git - ccan/blob - ccan/tdb2/test/run-tdb1-incompatible.c
tdb2: Make tdb1 share tdb_store flags, struct tdb_data and TDB_MAGIC_FOOD.
[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 int main(int argc, char *argv[])
35 {
36         struct tdb1_context *tdb;
37         unsigned int log_count, flags;
38         TDB_DATA d;
39         struct tdb1_logging_context log_ctx = { log_fn, &log_count };
40
41         plan_tests(38 * 2);
42
43         for (flags = 0; flags <= TDB1_CONVERT; flags += TDB1_CONVERT) {
44                 unsigned int rwmagic = TDB1_HASH_RWLOCK_MAGIC;
45
46                 if (flags & TDB1_CONVERT)
47                         tdb1_convert(&rwmagic, sizeof(rwmagic));
48
49                 /* Create an old-style hash. */
50                 log_count = 0;
51                 tdb = tdb1_open_ex("run-incompatible.tdb", 0, flags,
52                                   O_CREAT|O_RDWR|O_TRUNC, 0600, &log_ctx,
53                                   NULL);
54                 ok1(tdb);
55                 ok1(log_count == 0);
56                 d.dptr = (void *)"Hello";
57                 d.dsize = 5;
58                 ok1(tdb1_store(tdb, d, d, TDB_INSERT) == 0);
59                 tdb1_close(tdb);
60
61                 /* Should not have marked rwlocks field. */
62                 ok1(hdr_rwlocks("run-incompatible.tdb") == 0);
63
64                 /* We can still open any old-style with incompat flag. */
65                 log_count = 0;
66                 tdb = tdb1_open_ex("run-incompatible.tdb", 0,
67                                   TDB1_INCOMPATIBLE_HASH,
68                                   O_RDWR, 0600, &log_ctx, NULL);
69                 ok1(tdb);
70                 ok1(log_count == 0);
71                 d = tdb1_fetch(tdb, d);
72                 ok1(d.dsize == 5);
73                 free(d.dptr);
74                 ok1(tdb1_check(tdb, NULL, NULL) == 0);
75                 tdb1_close(tdb);
76
77                 log_count = 0;
78                 tdb = tdb1_open_ex("test/jenkins-le-hash.tdb1", 0, 0, O_RDONLY,
79                                   0, &log_ctx, tdb1_jenkins_hash);
80                 ok1(tdb);
81                 ok1(log_count == 0);
82                 ok1(tdb1_check(tdb, NULL, NULL) == 0);
83                 tdb1_close(tdb);
84
85                 log_count = 0;
86                 tdb = tdb1_open_ex("test/jenkins-be-hash.tdb1", 0, 0, O_RDONLY,
87                                   0, &log_ctx, tdb1_jenkins_hash);
88                 ok1(tdb);
89                 ok1(log_count == 0);
90                 ok1(tdb1_check(tdb, NULL, NULL) == 0);
91                 tdb1_close(tdb);
92
93                 /* OK, now create with incompatible flag, default hash. */
94                 log_count = 0;
95                 tdb = tdb1_open_ex("run-incompatible.tdb", 0,
96                                   flags|TDB1_INCOMPATIBLE_HASH,
97                                   O_CREAT|O_RDWR|O_TRUNC, 0600, &log_ctx,
98                                   NULL);
99                 ok1(tdb);
100                 ok1(log_count == 0);
101                 d.dptr = (void *)"Hello";
102                 d.dsize = 5;
103                 ok1(tdb1_store(tdb, d, d, TDB_INSERT) == 0);
104                 tdb1_close(tdb);
105
106                 /* Should have marked rwlocks field. */
107                 ok1(hdr_rwlocks("run-incompatible.tdb") == rwmagic);
108
109                 /* Cannot open with old hash. */
110                 log_count = 0;
111                 tdb = tdb1_open_ex("run-incompatible.tdb", 0, 0,
112                                   O_RDWR, 0600, &log_ctx, tdb1_old_hash);
113                 ok1(!tdb);
114                 ok1(log_count == 1);
115
116                 /* Can open with jenkins hash. */
117                 log_count = 0;
118                 tdb = tdb1_open_ex("run-incompatible.tdb", 0, 0,
119                                   O_RDWR, 0600, &log_ctx, tdb1_jenkins_hash);
120                 ok1(tdb);
121                 ok1(log_count == 0);
122                 d = tdb1_fetch(tdb, d);
123                 ok1(d.dsize == 5);
124                 free(d.dptr);
125                 ok1(tdb1_check(tdb, NULL, NULL) == 0);
126                 tdb1_close(tdb);
127
128                 /* Can open by letting it figure it out itself. */
129                 log_count = 0;
130                 tdb = tdb1_open_ex("run-incompatible.tdb", 0, 0,
131                                   O_RDWR, 0600, &log_ctx, NULL);
132                 ok1(tdb);
133                 ok1(log_count == 0);
134                 d.dptr = (void *)"Hello";
135                 d.dsize = 5;
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                 /* We can also use incompatible hash with other hashes. */
143                 log_count = 0;
144                 tdb = tdb1_open_ex("run-incompatible.tdb", 0,
145                                   flags|TDB1_INCOMPATIBLE_HASH,
146                                   O_CREAT|O_RDWR|O_TRUNC, 0600, &log_ctx,
147                                   tdb1_dumb_hash);
148                 ok1(tdb);
149                 ok1(log_count == 0);
150                 d.dptr = (void *)"Hello";
151                 d.dsize = 5;
152                 ok1(tdb1_store(tdb, d, d, TDB_INSERT) == 0);
153                 tdb1_close(tdb);
154
155                 /* Should have marked rwlocks field. */
156                 ok1(hdr_rwlocks("run-incompatible.tdb") == rwmagic);
157
158                 /* It should not open if we don't specify. */
159                 log_count = 0;
160                 tdb = tdb1_open_ex("run-incompatible.tdb", 0, 0, O_RDWR, 0,
161                                   &log_ctx, NULL);
162                 ok1(!tdb);
163                 ok1(log_count == 1);
164
165                 /* Should reopen with correct hash. */
166                 log_count = 0;
167                 tdb = tdb1_open_ex("run-incompatible.tdb", 0, 0, O_RDWR, 0,
168                                   &log_ctx, tdb1_dumb_hash);
169                 ok1(tdb);
170                 ok1(log_count == 0);
171                 d = tdb1_fetch(tdb, d);
172                 ok1(d.dsize == 5);
173                 free(d.dptr);
174                 ok1(tdb1_check(tdb, NULL, NULL) == 0);
175                 tdb1_close(tdb);
176         }
177
178         return exit_status();
179 }