]> git.ozlabs.org Git - ccan/blob - ccan/tdb2/test/run-05-readonly-open.c
0f1a4343d82f7fc3e28f7b6243450e3dfa2898f4
[ccan] / ccan / tdb2 / test / run-05-readonly-open.c
1 #include <ccan/failtest/failtest_override.h>
2 #include <ccan/tdb2/tdb.c>
3 #include <ccan/tdb2/open.c>
4 #include <ccan/tdb2/free.c>
5 #include <ccan/tdb2/lock.c>
6 #include <ccan/tdb2/io.c>
7 #include <ccan/tdb2/hash.c>
8 #include <ccan/tdb2/transaction.c>
9 #include <ccan/tdb2/check.c>
10 #include <ccan/tap/tap.h>
11 #include <ccan/failtest/failtest.h>
12 #include "logging.h"
13 #include "failtest_helper.h"
14
15 static bool failtest_suppress = false;
16
17 /* Don't need to test everything here, just want expand testing. */
18 static enum failtest_result
19 suppress_failure(struct failtest_call *history, unsigned num)
20 {
21         if (failtest_suppress)
22                 return FAIL_DONT_FAIL;
23         return block_repeat_failures(history, num);
24 }
25
26 int main(int argc, char *argv[])
27 {
28         unsigned int i;
29         struct tdb_context *tdb;
30         int flags[] = { TDB_DEFAULT, TDB_NOMMAP,
31                         TDB_CONVERT, TDB_NOMMAP|TDB_CONVERT };
32         struct tdb_data key = tdb_mkdata("key", 3);
33         struct tdb_data data = tdb_mkdata("data", 4), d;
34         union tdb_attribute seed_attr;
35         unsigned int msgs = 0;
36
37         failtest_init(argc, argv);
38         failtest_hook = suppress_failure;
39         failtest_exit_check = exit_check_log;
40
41         seed_attr.base.attr = TDB_ATTRIBUTE_SEED;
42         seed_attr.base.next = &tap_log_attr;
43         seed_attr.seed.seed = 0;
44
45         failtest_suppress = true;
46         plan_tests(sizeof(flags) / sizeof(flags[0]) * 11);
47         for (i = 0; i < sizeof(flags) / sizeof(flags[0]); i++) {
48                 tdb = tdb_open("run-05-readonly-open.tdb", flags[i],
49                                O_RDWR|O_CREAT|O_TRUNC, 0600, &seed_attr);
50                 ok1(tdb_store(tdb, key, data, TDB_INSERT) == 0);
51                 tdb_close(tdb);
52
53                 failtest_suppress = false;
54                 tdb = tdb_open("run-05-readonly-open.tdb", flags[i],
55                                O_RDONLY, 0600, &tap_log_attr);
56                 if (!ok1(tdb))
57                         break;
58                 ok1(tap_log_messages == msgs);
59                 /* Fetch should succeed, stores should fail. */
60                 if (!ok1(tdb_fetch(tdb, key, &d) == 0))
61                         goto fail;
62                 ok1(tdb_deq(d, data));
63                 free(d.dptr);
64                 if (!ok1(tdb_store(tdb, key, data, TDB_MODIFY)
65                          == TDB_ERR_RDONLY))
66                         goto fail;
67                 ok1(tap_log_messages == ++msgs);
68                 if (!ok1(tdb_store(tdb, key, data, TDB_INSERT)
69                          == TDB_ERR_RDONLY))
70                         goto fail;
71                 ok1(tap_log_messages == ++msgs);
72                 failtest_suppress = true;
73                 ok1(tdb_check(tdb, NULL, NULL) == 0);
74                 tdb_close(tdb);
75                 ok1(tap_log_messages == msgs);
76                 /* SIGH: failtest bug, it doesn't save the tdb file because
77                  * we have it read-only.  If we go around again, it gets
78                  * changed underneath us and things get screwy. */
79                 if (failtest_has_failed())
80                         break;
81         }
82         failtest_exit(exit_status());
83
84 fail:
85         failtest_suppress = true;
86         tdb_close(tdb);
87         failtest_exit(exit_status());
88 }