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