Improve the VFS Makefile so that it is easier for use out of tree but still works...
[Samba/gebeck_regimport.git] / lib / tdb / test / run-incompatible.c
blob6097026b49a452521ab4b7a49d3ae4f3b01ad69d
1 #include "../common/tdb_private.h"
2 #include "../common/io.c"
3 #include "../common/tdb.c"
4 #include "../common/lock.c"
5 #include "../common/freelist.c"
6 #include "../common/traverse.c"
7 #include "../common/transaction.c"
8 #include "../common/error.c"
9 #include "../common/open.c"
10 #include "../common/check.c"
11 #include "../common/hash.c"
12 #include "tap-interface.h"
13 #include <stdlib.h>
14 #include <err.h>
16 static unsigned int tdb_dumb_hash(TDB_DATA *key)
18 return key->dsize;
21 static void log_fn(struct tdb_context *tdb, enum tdb_debug_level level, const char *fmt, ...)
23 unsigned int *count = tdb_get_logging_private(tdb);
24 if (strstr(fmt, "hash"))
25 (*count)++;
28 static unsigned int hdr_rwlocks(const char *fname)
30 struct tdb_header hdr;
32 int fd = open(fname, O_RDONLY);
33 if (fd == -1)
34 return -1;
36 if (read(fd, &hdr, sizeof(hdr)) != sizeof(hdr))
37 return -1;
39 close(fd);
40 return hdr.rwlocks;
43 int main(int argc, char *argv[])
45 struct tdb_context *tdb;
46 unsigned int log_count, flags;
47 TDB_DATA d, r;
48 struct tdb_logging_context log_ctx = { log_fn, &log_count };
50 plan_tests(38 * 2);
52 for (flags = 0; flags <= TDB_CONVERT; flags += TDB_CONVERT) {
53 unsigned int rwmagic = TDB_HASH_RWLOCK_MAGIC;
55 if (flags & TDB_CONVERT)
56 tdb_convert(&rwmagic, sizeof(rwmagic));
58 /* Create an old-style hash. */
59 log_count = 0;
60 tdb = tdb_open_ex("run-incompatible.tdb", 0, flags,
61 O_CREAT|O_RDWR|O_TRUNC, 0600, &log_ctx,
62 NULL);
63 ok1(tdb);
64 ok1(log_count == 0);
65 d.dptr = (void *)"Hello";
66 d.dsize = 5;
67 ok1(tdb_store(tdb, d, d, TDB_INSERT) == 0);
68 tdb_close(tdb);
70 /* Should not have marked rwlocks field. */
71 ok1(hdr_rwlocks("run-incompatible.tdb") == 0);
73 /* We can still open any old-style with incompat flag. */
74 log_count = 0;
75 tdb = tdb_open_ex("run-incompatible.tdb", 0,
76 TDB_INCOMPATIBLE_HASH,
77 O_RDWR, 0600, &log_ctx, NULL);
78 ok1(tdb);
79 ok1(log_count == 0);
80 r = tdb_fetch(tdb, d);
81 ok1(r.dsize == 5);
82 free(r.dptr);
83 ok1(tdb_check(tdb, NULL, NULL) == 0);
84 tdb_close(tdb);
86 log_count = 0;
87 tdb = tdb_open_ex("test/jenkins-le-hash.tdb", 0, 0, O_RDONLY,
88 0, &log_ctx, tdb_jenkins_hash);
89 ok1(tdb);
90 ok1(log_count == 0);
91 ok1(tdb_check(tdb, NULL, NULL) == 0);
92 tdb_close(tdb);
94 log_count = 0;
95 tdb = tdb_open_ex("test/jenkins-be-hash.tdb", 0, 0, O_RDONLY,
96 0, &log_ctx, tdb_jenkins_hash);
97 ok1(tdb);
98 ok1(log_count == 0);
99 ok1(tdb_check(tdb, NULL, NULL) == 0);
100 tdb_close(tdb);
102 /* OK, now create with incompatible flag, default hash. */
103 log_count = 0;
104 tdb = tdb_open_ex("run-incompatible.tdb", 0,
105 flags|TDB_INCOMPATIBLE_HASH,
106 O_CREAT|O_RDWR|O_TRUNC, 0600, &log_ctx,
107 NULL);
108 ok1(tdb);
109 ok1(log_count == 0);
110 d.dptr = (void *)"Hello";
111 d.dsize = 5;
112 ok1(tdb_store(tdb, d, d, TDB_INSERT) == 0);
113 tdb_close(tdb);
115 /* Should have marked rwlocks field. */
116 ok1(hdr_rwlocks("run-incompatible.tdb") == rwmagic);
118 /* Cannot open with old hash. */
119 log_count = 0;
120 tdb = tdb_open_ex("run-incompatible.tdb", 0, 0,
121 O_RDWR, 0600, &log_ctx, tdb_old_hash);
122 ok1(!tdb);
123 ok1(log_count == 1);
125 /* Can open with jenkins hash. */
126 log_count = 0;
127 tdb = tdb_open_ex("run-incompatible.tdb", 0, 0,
128 O_RDWR, 0600, &log_ctx, tdb_jenkins_hash);
129 ok1(tdb);
130 ok1(log_count == 0);
131 r = tdb_fetch(tdb, d);
132 ok1(r.dsize == 5);
133 free(r.dptr);
134 ok1(tdb_check(tdb, NULL, NULL) == 0);
135 tdb_close(tdb);
137 /* Can open by letting it figure it out itself. */
138 log_count = 0;
139 tdb = tdb_open_ex("run-incompatible.tdb", 0, 0,
140 O_RDWR, 0600, &log_ctx, NULL);
141 ok1(tdb);
142 ok1(log_count == 0);
143 r = tdb_fetch(tdb, d);
144 ok1(r.dsize == 5);
145 free(r.dptr);
146 ok1(tdb_check(tdb, NULL, NULL) == 0);
147 tdb_close(tdb);
149 /* We can also use incompatible hash with other hashes. */
150 log_count = 0;
151 tdb = tdb_open_ex("run-incompatible.tdb", 0,
152 flags|TDB_INCOMPATIBLE_HASH,
153 O_CREAT|O_RDWR|O_TRUNC, 0600, &log_ctx,
154 tdb_dumb_hash);
155 ok1(tdb);
156 ok1(log_count == 0);
157 d.dptr = (void *)"Hello";
158 d.dsize = 5;
159 ok1(tdb_store(tdb, d, d, TDB_INSERT) == 0);
160 tdb_close(tdb);
162 /* Should have marked rwlocks field. */
163 ok1(hdr_rwlocks("run-incompatible.tdb") == rwmagic);
165 /* It should not open if we don't specify. */
166 log_count = 0;
167 tdb = tdb_open_ex("run-incompatible.tdb", 0, 0, O_RDWR, 0,
168 &log_ctx, NULL);
169 ok1(!tdb);
170 ok1(log_count == 1);
172 /* Should reopen with correct hash. */
173 log_count = 0;
174 tdb = tdb_open_ex("run-incompatible.tdb", 0, 0, O_RDWR, 0,
175 &log_ctx, tdb_dumb_hash);
176 ok1(tdb);
177 ok1(log_count == 0);
178 r = tdb_fetch(tdb, d);
179 ok1(r.dsize == 5);
180 free(r.dptr);
181 ok1(tdb_check(tdb, NULL, NULL) == 0);
182 tdb_close(tdb);
185 return exit_status();