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