Fix missing va_end()s
[ccan] / ccan / ntdb / test / run-05-readonly-open.c
1 #include <ccan/failtest/failtest_override.h>
2 #include "ntdb-source.h"
3 #include "tap-interface.h"
4 #include <ccan/failtest/failtest.h>
5 #include "logging.h"
6 #include "failtest_helper.h"
7 #include "helprun-external-agent.h"
8
9 int main(int argc, char *argv[])
10 {
11         unsigned int i;
12         struct ntdb_context *ntdb;
13         int flags[] = { NTDB_DEFAULT, NTDB_NOMMAP,
14                         NTDB_CONVERT, NTDB_NOMMAP|NTDB_CONVERT };
15         NTDB_DATA key = ntdb_mkdata("key", 3);
16         NTDB_DATA data = ntdb_mkdata("data", 4), d;
17         union ntdb_attribute seed_attr;
18         unsigned int msgs = 0;
19
20         failtest_init(argc, argv);
21         failtest_hook = block_repeat_failures;
22         failtest_exit_check = exit_check_log;
23
24         seed_attr.base.attr = NTDB_ATTRIBUTE_SEED;
25         seed_attr.base.next = &tap_log_attr;
26         seed_attr.seed.seed = 0;
27
28         failtest_suppress = true;
29         plan_tests(sizeof(flags) / sizeof(flags[0]) * 11);
30         for (i = 0; i < sizeof(flags) / sizeof(flags[0]); i++) {
31                 ntdb = ntdb_open("run-05-readonly-open.ntdb",
32                                  flags[i]|MAYBE_NOSYNC,
33                                  O_RDWR|O_CREAT|O_TRUNC, 0600,
34                                  &seed_attr);
35                 ok1(ntdb_store(ntdb, key, data, NTDB_INSERT) == 0);
36                 ntdb_close(ntdb);
37
38                 failtest_suppress = false;
39                 ntdb = ntdb_open("run-05-readonly-open.ntdb",
40                                  flags[i]|MAYBE_NOSYNC,
41                                  O_RDONLY, 0600, &tap_log_attr);
42                 if (!ok1(ntdb))
43                         break;
44                 ok1(tap_log_messages == msgs);
45                 /* Fetch should succeed, stores should fail. */
46                 if (!ok1(ntdb_fetch(ntdb, key, &d) == 0))
47                         goto fail;
48                 ok1(ntdb_deq(d, data));
49                 free(d.dptr);
50                 if (!ok1(ntdb_store(ntdb, key, data, NTDB_MODIFY)
51                          == NTDB_ERR_RDONLY))
52                         goto fail;
53                 ok1(tap_log_messages == ++msgs);
54                 if (!ok1(ntdb_store(ntdb, key, data, NTDB_INSERT)
55                          == NTDB_ERR_RDONLY))
56                         goto fail;
57                 ok1(tap_log_messages == ++msgs);
58                 failtest_suppress = true;
59                 ok1(ntdb_check(ntdb, NULL, NULL) == 0);
60                 ntdb_close(ntdb);
61                 ok1(tap_log_messages == msgs);
62                 /* SIGH: failtest bug, it doesn't save the ntdb file because
63                  * we have it read-only.  If we go around again, it gets
64                  * changed underneath us and things get screwy. */
65                 if (failtest_has_failed())
66                         break;
67         }
68         failtest_exit(exit_status());
69
70 fail:
71         failtest_suppress = true;
72         ntdb_close(ntdb);
73         failtest_exit(exit_status());
74
75         /*
76          * We will never reach this but the compiler complains if we do not
77          * return in this function.
78          */
79         return EFAULT;
80 }