ccan/ntdb: demote to junkcode.
[ccan] / junkcode / rusty@rustcorp.com.au-ntdb / test / run-features.c
1 #include "ntdb-source.h"
2 #include "tap-interface.h"
3 #include "logging.h"
4 #include "helprun-external-agent.h"
5
6 int main(int argc, char *argv[])
7 {
8         unsigned int i, j;
9         struct ntdb_context *ntdb;
10         int flags[] = { NTDB_DEFAULT, NTDB_NOMMAP,
11                         NTDB_CONVERT, NTDB_NOMMAP|NTDB_CONVERT };
12         NTDB_DATA key = { (unsigned char *)&j, sizeof(j) };
13         NTDB_DATA data = { (unsigned char *)&j, sizeof(j) };
14
15         plan_tests(sizeof(flags) / sizeof(flags[0]) * 8 + 1);
16         for (i = 0; i < sizeof(flags) / sizeof(flags[0]); i++) {
17                 uint64_t features;
18                 ntdb = ntdb_open("run-features.ntdb", flags[i]|MAYBE_NOSYNC,
19                                  O_RDWR|O_CREAT|O_TRUNC, 0600, &tap_log_attr);
20                 ok1(ntdb);
21                 if (!ntdb)
22                         continue;
23
24                 /* Put some stuff in there. */
25                 for (j = 0; j < 100; j++) {
26                         if (ntdb_store(ntdb, key, data, NTDB_REPLACE) != 0)
27                                 fail("Storing in ntdb");
28                 }
29
30                 /* Mess with features fields in hdr. */
31                 features = (~NTDB_FEATURE_MASK ^ 1);
32                 ok1(ntdb_write_convert(ntdb, offsetof(struct ntdb_header,
33                                                     features_used),
34                                       &features, sizeof(features)) == 0);
35                 ok1(ntdb_write_convert(ntdb, offsetof(struct ntdb_header,
36                                                     features_offered),
37                                       &features, sizeof(features)) == 0);
38                 ntdb_close(ntdb);
39
40                 ntdb = ntdb_open("run-features.ntdb", flags[i]|MAYBE_NOSYNC,
41                                  O_RDWR, 0, &tap_log_attr);
42                 ok1(ntdb);
43                 if (!ntdb)
44                         continue;
45
46                 /* Should not have changed features offered. */
47                 ok1(ntdb_read_convert(ntdb, offsetof(struct ntdb_header,
48                                                    features_offered),
49                                      &features, sizeof(features)) == 0);
50                 ok1(features == (~NTDB_FEATURE_MASK ^ 1));
51
52                 /* Should have cleared unknown bits in features_used. */
53                 ok1(ntdb_read_convert(ntdb, offsetof(struct ntdb_header,
54                                                    features_used),
55                                      &features, sizeof(features)) == 0);
56                 ok1(features == (1 & NTDB_FEATURE_MASK));
57
58                 ntdb_close(ntdb);
59         }
60
61         ok1(tap_log_messages == 0);
62         return exit_status();
63 }