]> git.ozlabs.org Git - ccan/blob - ccan/ntdb/test/run-seed.c
ntdb: next-generation trivial key-value database
[ccan] / ccan / ntdb / test / run-seed.c
1 #include "ntdb-source.h"
2 #include "tap-interface.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 ntdb_context *ntdb,
9                       enum ntdb_log_level level,
10                       enum NTDB_ERROR ecode,
11                       const char *message, void *priv)
12 {
13         log_count++;
14 }
15
16 static union ntdb_attribute log_attr = {
17         .log = { .base = { .attr = NTDB_ATTRIBUTE_LOG },
18                  .fn = my_log_fn }
19 };
20
21 int main(int argc, char *argv[])
22 {
23         unsigned int i;
24         struct ntdb_context *ntdb;
25         union ntdb_attribute attr;
26         int flags[] = { NTDB_INTERNAL, NTDB_DEFAULT, NTDB_NOMMAP,
27                         NTDB_INTERNAL|NTDB_CONVERT, NTDB_CONVERT,
28                         NTDB_NOMMAP|NTDB_CONVERT };
29
30         attr.seed.base.attr = NTDB_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 ntdb_header hdr;
37                 int fd;
38                 ntdb = ntdb_open("run-seed.ntdb", flags[i]|MAYBE_NOSYNC,
39                                O_RDWR|O_CREAT|O_TRUNC, 0600, &attr);
40                 ok1(ntdb);
41                 if (!ntdb)
42                         continue;
43                 ok1(ntdb_check(ntdb, NULL, NULL) == 0);
44                 ok1(ntdb->hash_seed == 42);
45                 ok1(log_count == 0);
46                 ntdb_close(ntdb);
47
48                 if (flags[i] & NTDB_INTERNAL)
49                         continue;
50
51                 fd = open("run-seed.ntdb", O_RDONLY);
52                 ok1(fd >= 0);
53                 ok1(read(fd, &hdr, sizeof(hdr)) == sizeof(hdr));
54                 if (flags[i] & NTDB_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 }