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