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