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