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