]> git.ozlabs.org Git - ccan/blob - ccan/ntdb/test/run-seed.c
ntdb: fix up tests.
[ccan] / ccan / ntdb / test / run-seed.c
1 #include "ntdb-source.h"
2 #include "tap-interface.h"
3 #include "logging.h"
4 #include "helprun-external-agent.h"
5
6 static int log_count = 0;
7
8 /* Normally we get a log when setting random seed. */
9 static void my_log_fn(struct ntdb_context *ntdb,
10                       enum ntdb_log_level level,
11                       enum NTDB_ERROR ecode,
12                       const char *message, void *priv)
13 {
14         log_count++;
15 }
16
17 static union ntdb_attribute log_attr = {
18         .log = { .base = { .attr = NTDB_ATTRIBUTE_LOG },
19                  .fn = my_log_fn }
20 };
21
22 int main(int argc, char *argv[])
23 {
24         unsigned int i;
25         struct ntdb_context *ntdb;
26         union ntdb_attribute attr;
27         int flags[] = { NTDB_INTERNAL, NTDB_DEFAULT, NTDB_NOMMAP,
28                         NTDB_INTERNAL|NTDB_CONVERT, NTDB_CONVERT,
29                         NTDB_NOMMAP|NTDB_CONVERT };
30
31         attr.seed.base.attr = NTDB_ATTRIBUTE_SEED;
32         attr.seed.base.next = &log_attr;
33         attr.seed.seed = 42;
34
35         plan_tests(sizeof(flags) / sizeof(flags[0]) * 4 + 4 * 3);
36         for (i = 0; i < sizeof(flags) / sizeof(flags[0]); i++) {
37                 struct ntdb_header hdr;
38                 int fd;
39                 ntdb = ntdb_open("run-seed.ntdb", flags[i]|MAYBE_NOSYNC,
40                                O_RDWR|O_CREAT|O_TRUNC, 0600, &attr);
41                 ok1(ntdb);
42                 if (!ntdb)
43                         continue;
44                 ok1(ntdb_check(ntdb, NULL, NULL) == 0);
45                 ok1(ntdb->hash_seed == 42);
46                 ok1(log_count == 0);
47                 ntdb_close(ntdb);
48
49                 if (flags[i] & NTDB_INTERNAL)
50                         continue;
51
52                 fd = open("run-seed.ntdb", O_RDONLY);
53                 ok1(fd >= 0);
54                 ok1(read(fd, &hdr, sizeof(hdr)) == sizeof(hdr));
55                 if (flags[i] & NTDB_CONVERT)
56                         ok1(bswap_64(hdr.hash_seed) == 42);
57                 else
58                         ok1(hdr.hash_seed == 42);
59                 close(fd);
60         }
61         return exit_status();
62 }