]> git.ozlabs.org Git - ccan/blob - ccan/ntdb/test/run-35-convert.c
873d4e7781810debd6cc010794172a0b02d40258
[ccan] / ccan / ntdb / test / run-35-convert.c
1 #include "private.h"
2 #include <ccan/failtest/failtest_override.h>
3 #include "ntdb-source.h"
4 #include "tap-interface.h"
5 #include <ccan/failtest/failtest.h>
6 #include "logging.h"
7 #include "failtest_helper.h"
8
9 int main(int argc, char *argv[])
10 {
11         unsigned int i, messages = 0;
12         struct ntdb_context *ntdb;
13         int flags[] = { NTDB_DEFAULT, NTDB_NOMMAP,
14                         NTDB_CONVERT, NTDB_NOMMAP|NTDB_CONVERT };
15
16         failtest_init(argc, argv);
17         failtest_hook = block_repeat_failures;
18         failtest_exit_check = exit_check_log;
19         plan_tests(sizeof(flags) / sizeof(flags[0]) * 4);
20         for (i = 0; i < sizeof(flags) / sizeof(flags[0]); i++) {
21                 ntdb = ntdb_open("run-35-convert.ntdb", flags[i]|MAYBE_NOSYNC,
22                                  O_RDWR|O_CREAT|O_TRUNC, 0600, &tap_log_attr);
23                 if (!ok1(ntdb))
24                         failtest_exit(exit_status());
25
26                 ntdb_close(ntdb);
27                 /* We can fail in log message formatting or open.  That's OK */
28                 if (failtest_has_failed()) {
29                         failtest_exit(exit_status());
30                 }
31                 /* If we say NTDB_CONVERT, it must be converted */
32                 ntdb = ntdb_open("run-35-convert.ntdb",
33                                  flags[i]|NTDB_CONVERT|MAYBE_NOSYNC,
34                                  O_RDWR, 0600, &tap_log_attr);
35                 if (flags[i] & NTDB_CONVERT) {
36                         if (!ntdb)
37                                 failtest_exit(exit_status());
38                         ok1(ntdb_get_flags(ntdb) & NTDB_CONVERT);
39                         ntdb_close(ntdb);
40                 } else {
41                         if (!ok1(!ntdb && errno == EIO))
42                                 failtest_exit(exit_status());
43                         ok1(tap_log_messages == ++messages);
44                         if (!ok1(log_last && strstr(log_last, "NTDB_CONVERT")))
45                                 failtest_exit(exit_status());
46                 }
47
48                 /* If don't say NTDB_CONVERT, it *may* be converted */
49                 ntdb = ntdb_open("run-35-convert.ntdb",
50                                  (flags[i] & ~NTDB_CONVERT)|MAYBE_NOSYNC,
51                                  O_RDWR, 0600, &tap_log_attr);
52                 if (!ntdb)
53                         failtest_exit(exit_status());
54                 ok1(ntdb_get_flags(ntdb) == (flags[i]|MAYBE_NOSYNC));
55                 ntdb_close(ntdb);
56         }
57         failtest_exit(exit_status());
58
59         /*
60          * We will never reach this but the compiler complains if we do not
61          * return in this function.
62          */
63         return EFAULT;
64 }