tdb2: allow multiple chain locks.
[ccan] / ccan / tdb2 / test / run-features.c
1 #include <ccan/tdb2/tdb.c>
2 #include <ccan/tdb2/open.c>
3 #include <ccan/tdb2/free.c>
4 #include <ccan/tdb2/lock.c>
5 #include <ccan/tdb2/io.c>
6 #include <ccan/tdb2/hash.c>
7 #include <ccan/tdb2/check.c>
8 #include <ccan/tdb2/summary.c>
9 #include <ccan/tdb2/transaction.c>
10 #include <ccan/tap/tap.h>
11 #include "logging.h"
12
13 int main(int argc, char *argv[])
14 {
15         unsigned int i, j;
16         struct tdb_context *tdb;
17         int flags[] = { TDB_DEFAULT, TDB_NOMMAP,
18                         TDB_CONVERT, TDB_NOMMAP|TDB_CONVERT };
19         struct tdb_data key = { (unsigned char *)&j, sizeof(j) };
20         struct tdb_data data = { (unsigned char *)&j, sizeof(j) };
21
22         plan_tests(sizeof(flags) / sizeof(flags[0]) * 8 + 1);
23         for (i = 0; i < sizeof(flags) / sizeof(flags[0]); i++) {
24                 uint64_t features;
25                 tdb = tdb_open("run-features.tdb", flags[i],
26                                O_RDWR|O_CREAT|O_TRUNC, 0600, &tap_log_attr);
27                 ok1(tdb);
28                 if (!tdb)
29                         continue;
30
31                 /* Put some stuff in there. */
32                 for (j = 0; j < 100; j++) {
33                         if (tdb_store(tdb, key, data, TDB_REPLACE) != 0)
34                                 fail("Storing in tdb");
35                 }
36
37                 /* Mess with features fields in hdr. */
38                 features = (~TDB_FEATURE_MASK ^ 1);
39                 ok1(tdb_write_convert(tdb, offsetof(struct tdb_header,
40                                                     features_used), 
41                                       &features, sizeof(features)) == 0);
42                 ok1(tdb_write_convert(tdb, offsetof(struct tdb_header,
43                                                     features_offered), 
44                                       &features, sizeof(features)) == 0);
45                 tdb_close(tdb);
46
47                 tdb = tdb_open("run-features.tdb", flags[i], O_RDWR, 0,
48                                &tap_log_attr);
49                 ok1(tdb);
50                 if (!tdb)
51                         continue;
52
53                 /* Should not have changed features offered. */
54                 ok1(tdb_read_convert(tdb, offsetof(struct tdb_header,
55                                                    features_offered), 
56                                      &features, sizeof(features)) == 0);
57                 ok1(features == (~TDB_FEATURE_MASK ^ 1));
58
59                 /* Should have cleared unknown bits in features_used. */
60                 ok1(tdb_read_convert(tdb, offsetof(struct tdb_header,
61                                                    features_used), 
62                                      &features, sizeof(features)) == 0);
63                 ok1(features == (1 & TDB_FEATURE_MASK));
64
65                 tdb_close(tdb);
66         }
67
68         ok1(tap_log_messages == 0);
69         return exit_status();
70 }
71
72