]> git.ozlabs.org Git - ccan/blob - ccan/tdb/test/run-incompatible.c
tdb: don't leak memory in tests.
[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, r;
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                 r = tdb_fetch(tdb, d);
82                 ok1(r.dsize == 5);
83                 free(r.dptr);
84                 ok1(tdb_check(tdb, NULL, NULL) == 0);
85                 tdb_close(tdb);
86
87                 log_count = 0;
88                 tdb = tdb_open_ex("test/jenkins-le-hash.tdb", 0, 0, O_RDONLY,
89                                   0, &log_ctx, tdb_jenkins_hash);
90                 ok1(tdb);
91                 ok1(log_count == 0);
92                 ok1(tdb_check(tdb, NULL, NULL) == 0);
93                 tdb_close(tdb);
94
95                 log_count = 0;
96                 tdb = tdb_open_ex("test/jenkins-be-hash.tdb", 0, 0, O_RDONLY,
97                                   0, &log_ctx, tdb_jenkins_hash);
98                 ok1(tdb);
99                 ok1(log_count == 0);
100                 ok1(tdb_check(tdb, NULL, NULL) == 0);
101                 tdb_close(tdb);
102
103                 /* OK, now create with incompatible flag, default hash. */
104                 log_count = 0;
105                 tdb = tdb_open_ex("run-incompatible.tdb", 0,
106                                   flags|TDB_INCOMPATIBLE_HASH,
107                                   O_CREAT|O_RDWR|O_TRUNC, 0600, &log_ctx,
108                                   NULL);
109                 ok1(tdb);
110                 ok1(log_count == 0);
111                 d.dptr = (void *)"Hello";
112                 d.dsize = 5;
113                 ok1(tdb_store(tdb, d, d, TDB_INSERT) == 0);
114                 tdb_close(tdb);
115
116                 /* Should have marked rwlocks field. */
117                 ok1(hdr_rwlocks("run-incompatible.tdb") == rwmagic);
118
119                 /* Cannot open with old hash. */
120                 log_count = 0;
121                 tdb = tdb_open_ex("run-incompatible.tdb", 0, 0,
122                                   O_RDWR, 0600, &log_ctx, tdb_old_hash);
123                 ok1(!tdb);
124                 ok1(log_count == 1);
125
126                 /* Can open with jenkins hash. */
127                 log_count = 0;
128                 tdb = tdb_open_ex("run-incompatible.tdb", 0, 0,
129                                   O_RDWR, 0600, &log_ctx, tdb_jenkins_hash);
130                 ok1(tdb);
131                 ok1(log_count == 0);
132                 r = tdb_fetch(tdb, d);
133                 ok1(r.dsize == 5);
134                 free(r.dptr);
135                 ok1(tdb_check(tdb, NULL, NULL) == 0);
136                 tdb_close(tdb);
137
138                 /* Can open by letting it figure it out itself. */
139                 log_count = 0;
140                 tdb = tdb_open_ex("run-incompatible.tdb", 0, 0,
141                                   O_RDWR, 0600, &log_ctx, NULL);
142                 ok1(tdb);
143                 ok1(log_count == 0);
144                 r = tdb_fetch(tdb, d);
145                 ok1(r.dsize == 5);
146                 free(r.dptr);
147                 ok1(tdb_check(tdb, NULL, NULL) == 0);
148                 tdb_close(tdb);
149
150                 /* We can also use incompatible hash with other hashes. */
151                 log_count = 0;
152                 tdb = tdb_open_ex("run-incompatible.tdb", 0,
153                                   flags|TDB_INCOMPATIBLE_HASH,
154                                   O_CREAT|O_RDWR|O_TRUNC, 0600, &log_ctx,
155                                   tdb_dumb_hash);
156                 ok1(tdb);
157                 ok1(log_count == 0);
158                 d.dptr = (void *)"Hello";
159                 d.dsize = 5;
160                 ok1(tdb_store(tdb, d, d, TDB_INSERT) == 0);
161                 tdb_close(tdb);
162
163                 /* Should have marked rwlocks field. */
164                 ok1(hdr_rwlocks("run-incompatible.tdb") == rwmagic);
165
166                 /* It should not open if we don't specify. */
167                 log_count = 0;
168                 tdb = tdb_open_ex("run-incompatible.tdb", 0, 0, O_RDWR, 0,
169                                   &log_ctx, NULL);
170                 ok1(!tdb);
171                 ok1(log_count == 1);
172
173                 /* Should reopen with correct hash. */
174                 log_count = 0;
175                 tdb = tdb_open_ex("run-incompatible.tdb", 0, 0, O_RDWR, 0,
176                                   &log_ctx, tdb_dumb_hash);
177                 ok1(tdb);
178                 ok1(log_count == 0);
179                 r = tdb_fetch(tdb, d);
180                 ok1(r.dsize == 5);
181                 free(r.dptr);
182                 ok1(tdb_check(tdb, NULL, NULL) == 0);
183                 tdb_close(tdb);
184         }
185
186         return exit_status();
187 }