]> git.ozlabs.org Git - ccan/blob - ccan/tdb/test/run-incompatible.c
tdb: TDB_INCOMPATIBLE_HASH, to allow safe changing of default hash.
[ccan] / ccan / tdb / test / run-incompatible.c
1 #define _XOPEN_SOURCE 500
2 #include <ccan/tdb/tdb.h>
3 #include <ccan/tdb/io.c>
4 #include <ccan/tdb/tdb.c>
5 #include <ccan/tdb/lock.c>
6 #include <ccan/tdb/freelist.c>
7 #include <ccan/tdb/traverse.c>
8 #include <ccan/tdb/transaction.c>
9 #include <ccan/tdb/error.c>
10 #include <ccan/tdb/open.c>
11 #include <ccan/tdb/check.c>
12 #include <ccan/tdb/hash.c>
13 #include <ccan/tap/tap.h>
14 #include <stdlib.h>
15 #include <err.h>
16
17 static unsigned int tdb_dumb_hash(TDB_DATA *key)
18 {
19         return key->dsize;
20 }
21
22 static void log_fn(struct tdb_context *tdb, enum tdb_debug_level level, const char *fmt, ...)
23 {
24         unsigned int *count = tdb_get_logging_private(tdb);
25         if (strstr(fmt, "hash"))
26                 (*count)++;
27 }
28
29 static unsigned int hdr_rwlocks(const char *fname)
30 {
31         struct tdb_header hdr;
32
33         int fd = open(fname, O_RDONLY);
34         if (fd == -1)
35                 return -1;
36
37         if (read(fd, &hdr, sizeof(hdr)) != sizeof(hdr))
38                 return -1;
39
40         close(fd);
41         return hdr.rwlocks;
42 }
43
44 int main(int argc, char *argv[])
45 {
46         struct tdb_context *tdb;
47         unsigned int log_count, flags;
48         TDB_DATA d;
49         struct tdb_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 = TDB_HASH_RWLOCK_MAGIC;
55
56                 if (flags & TDB_CONVERT)
57                         tdb_convert(&rwmagic, sizeof(rwmagic));
58
59                 /* Create an old-style hash. */
60                 log_count = 0;
61                 tdb = tdb_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(tdb_store(tdb, d, d, TDB_INSERT) == 0);
69                 tdb_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 flag. */
75                 log_count = 0;
76                 tdb = tdb_open_ex("run-incompatible.tdb", 0,
77                                   TDB_INCOMPATIBLE_HASH,
78                                   O_RDWR, 0600, &log_ctx, NULL);
79                 ok1(tdb);
80                 ok1(log_count == 0);
81                 ok1(tdb_fetch(tdb, d).dsize == 5);
82                 ok1(tdb_check(tdb, NULL, NULL) == 0);
83                 tdb_close(tdb);
84
85                 log_count = 0;
86                 tdb = tdb_open_ex("test/jenkins-le-hash.tdb", 0, 0, O_RDONLY,
87                                   0, &log_ctx, tdb_jenkins_hash);
88                 ok1(tdb);
89                 ok1(log_count == 0);
90                 ok1(tdb_check(tdb, NULL, NULL) == 0);
91                 tdb_close(tdb);
92
93                 log_count = 0;
94                 tdb = tdb_open_ex("test/jenkins-be-hash.tdb", 0, 0, O_RDONLY,
95                                   0, &log_ctx, tdb_jenkins_hash);
96                 ok1(tdb);
97                 ok1(log_count == 0);
98                 ok1(tdb_check(tdb, NULL, NULL) == 0);
99                 tdb_close(tdb);
100
101                 /* OK, now create with incompatible flag, default hash. */
102                 log_count = 0;
103                 tdb = tdb_open_ex("run-incompatible.tdb", 0,
104                                   flags|TDB_INCOMPATIBLE_HASH,
105                                   O_CREAT|O_RDWR|O_TRUNC, 0600, &log_ctx,
106                                   NULL);
107                 ok1(tdb);
108                 ok1(log_count == 0);
109                 d.dptr = (void *)"Hello";
110                 d.dsize = 5;
111                 ok1(tdb_store(tdb, d, d, TDB_INSERT) == 0);
112                 tdb_close(tdb);
113
114                 /* Should have marked rwlocks field. */
115                 ok1(hdr_rwlocks("run-incompatible.tdb") == rwmagic);
116
117                 /* Cannot open with old hash. */
118                 log_count = 0;
119                 tdb = tdb_open_ex("run-incompatible.tdb", 0, 0,
120                                   O_RDWR, 0600, &log_ctx, tdb_old_hash);
121                 ok1(!tdb);
122                 ok1(log_count == 1);
123
124                 /* Can open with jenkins hash. */
125                 log_count = 0;
126                 tdb = tdb_open_ex("run-incompatible.tdb", 0, 0,
127                                   O_RDWR, 0600, &log_ctx, tdb_jenkins_hash);
128                 ok1(tdb);
129                 ok1(log_count == 0);
130                 ok1(tdb_fetch(tdb, d).dsize == 5);
131                 ok1(tdb_check(tdb, NULL, NULL) == 0);
132                 tdb_close(tdb);
133
134                 /* Can open by letting it figure it out itself. */
135                 log_count = 0;
136                 tdb = tdb_open_ex("run-incompatible.tdb", 0, 0,
137                                   O_RDWR, 0600, &log_ctx, NULL);
138                 ok1(tdb);
139                 ok1(log_count == 0);
140                 ok1(tdb_fetch(tdb, d).dsize == 5);
141                 ok1(tdb_check(tdb, NULL, NULL) == 0);
142                 tdb_close(tdb);
143
144                 /* We can also use incompatible hash with other hashes. */
145                 log_count = 0;
146                 tdb = tdb_open_ex("run-incompatible.tdb", 0,
147                                   flags|TDB_INCOMPATIBLE_HASH,
148                                   O_CREAT|O_RDWR|O_TRUNC, 0600, &log_ctx,
149                                   tdb_dumb_hash);
150                 ok1(tdb);
151                 ok1(log_count == 0);
152                 d.dptr = (void *)"Hello";
153                 d.dsize = 5;
154                 ok1(tdb_store(tdb, d, d, TDB_INSERT) == 0);
155                 tdb_close(tdb);
156
157                 /* Should have marked rwlocks field. */
158                 ok1(hdr_rwlocks("run-incompatible.tdb") == rwmagic);
159
160                 /* It should not open if we don't specify. */
161                 log_count = 0;
162                 tdb = tdb_open_ex("run-incompatible.tdb", 0, 0, O_RDWR, 0,
163                                   &log_ctx, NULL);
164                 ok1(!tdb);
165                 ok1(log_count == 1);
166
167                 /* Should reopen with correct hash. */
168                 log_count = 0;
169                 tdb = tdb_open_ex("run-incompatible.tdb", 0, 0, O_RDWR, 0,
170                                   &log_ctx, tdb_dumb_hash);
171                 ok1(tdb);
172                 ok1(log_count == 0);
173                 ok1(tdb_fetch(tdb, d).dsize == 5);
174                 ok1(tdb_check(tdb, NULL, NULL) == 0);
175                 tdb_close(tdb);
176         }
177
178         return exit_status();
179 }