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