tdb2: feature support.
[ccan] / ccan / tdb2 / test / run-seed.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/transaction.c>
8 #include <ccan/tap/tap.h>
9 #include "logging.h"
10
11 static int log_count = 0;
12
13 /* Normally we get a log when setting random seed. */
14 static void my_log_fn(struct tdb_context *tdb,
15                       enum tdb_log_level level, void *priv,
16                       const char *message)
17 {
18         log_count++;
19 }
20
21 static union tdb_attribute log_attr = {
22         .log = { .base = { .attr = TDB_ATTRIBUTE_LOG },
23                  .log_fn = my_log_fn }
24 };
25
26 int main(int argc, char *argv[])
27 {
28         unsigned int i;
29         struct tdb_context *tdb;
30         union tdb_attribute attr;
31         int flags[] = { TDB_INTERNAL, TDB_DEFAULT, TDB_NOMMAP,
32                         TDB_INTERNAL|TDB_CONVERT, TDB_CONVERT, 
33                         TDB_NOMMAP|TDB_CONVERT };
34
35         attr.seed.base.attr = TDB_ATTRIBUTE_SEED;
36         attr.seed.base.next = &log_attr;
37         attr.seed.seed = 42;
38
39         plan_tests(sizeof(flags) / sizeof(flags[0]) * 4 + 4 * 3);
40         for (i = 0; i < sizeof(flags) / sizeof(flags[0]); i++) {
41                 struct tdb_header hdr;
42                 int fd;
43                 tdb = tdb_open("run-seed.tdb", flags[i],
44                                O_RDWR|O_CREAT|O_TRUNC, 0600, &attr);
45                 ok1(tdb);
46                 if (!tdb)
47                         continue;
48                 ok1(tdb_check(tdb, NULL, NULL) == 0);
49                 ok1(tdb->hash_seed == 42);
50                 ok1(log_count == 0);
51                 tdb_close(tdb);
52
53                 if (flags[i] & TDB_INTERNAL)
54                         continue;
55
56                 fd = open("run-seed.tdb", O_RDONLY);
57                 ok1(fd >= 0);
58                 ok1(read(fd, &hdr, sizeof(hdr)) == sizeof(hdr));
59                 if (flags[i] & TDB_CONVERT)
60                         ok1(bswap_64(hdr.hash_seed) == 42);
61                 else
62                         ok1(hdr.hash_seed == 42);
63                 close(fd);
64         }
65         return exit_status();
66 }