tdb2: test: fix run-57-die-during-transaction.c to be more efficient.
[ccan] / ccan / tdb2 / test / run-92-get-set-readonly.c
1 #include "tdb2-source.h"
2 #include <ccan/tap/tap.h>
3 #include "logging.h"
4
5 int main(int argc, char *argv[])
6 {
7         unsigned int i, extra_msgs;
8         struct tdb_context *tdb;
9         struct tdb_data key = tdb_mkdata("key", 3);
10         struct tdb_data data = tdb_mkdata("data", 4);
11         int flags[] = { TDB_DEFAULT, TDB_NOMMAP,
12                         TDB_CONVERT, TDB_NOMMAP|TDB_CONVERT,
13                         TDB_VERSION1, TDB_NOMMAP|TDB_VERSION1,
14                         TDB_CONVERT|TDB_VERSION1,
15                         TDB_NOMMAP|TDB_CONVERT|TDB_VERSION1 };
16
17         plan_tests(sizeof(flags) / sizeof(flags[0]) * 48);
18
19         for (i = 0; i < sizeof(flags) / sizeof(flags[0]); i++) {
20                 /* RW -> R0 */
21                 tdb = tdb_open("run-92-get-set-readonly.tdb", flags[i],
22                                O_RDWR|O_CREAT|O_TRUNC, 0600, &tap_log_attr);
23                 ok1(tdb);
24                 ok1(!(tdb_get_flags(tdb) & TDB_RDONLY));
25
26                 /* TDB1 complains multiple times. */
27                 if (flags[i] & TDB_VERSION1) {
28                         extra_msgs = 1;
29                 } else {
30                         extra_msgs = 0;
31                 }
32
33                 ok1(tdb_store(tdb, key, data, TDB_INSERT) == TDB_SUCCESS);
34
35                 tdb_add_flag(tdb, TDB_RDONLY);
36                 ok1(tdb_get_flags(tdb) & TDB_RDONLY);
37
38                 /* Can't store, append, delete. */
39                 ok1(tdb_store(tdb, key, data, TDB_MODIFY) == TDB_ERR_RDONLY);
40                 ok1(tap_log_messages == 1);
41                 ok1(tdb_append(tdb, key, data) == TDB_ERR_RDONLY);
42                 tap_log_messages -= extra_msgs;
43                 ok1(tap_log_messages == 2);
44                 ok1(tdb_delete(tdb, key) == TDB_ERR_RDONLY);
45                 tap_log_messages -= extra_msgs;
46                 ok1(tap_log_messages == 3);
47
48                 /* Can't start a transaction, or any write lock. */
49                 ok1(tdb_transaction_start(tdb) == TDB_ERR_RDONLY);
50                 ok1(tap_log_messages == 4);
51                 ok1(tdb_chainlock(tdb, key) == TDB_ERR_RDONLY);
52                 tap_log_messages -= extra_msgs;
53                 ok1(tap_log_messages == 5);
54                 ok1(tdb_lockall(tdb) == TDB_ERR_RDONLY);
55                 ok1(tap_log_messages == 6);
56                 ok1(tdb_wipe_all(tdb) == TDB_ERR_RDONLY);
57                 ok1(tap_log_messages == 7);
58
59                 /* Back to RW. */
60                 tdb_remove_flag(tdb, TDB_RDONLY);
61                 ok1(!(tdb_get_flags(tdb) & TDB_RDONLY));
62
63                 ok1(tdb_store(tdb, key, data, TDB_MODIFY) == TDB_SUCCESS);
64                 ok1(tdb_append(tdb, key, data) == TDB_SUCCESS);
65                 ok1(tdb_delete(tdb, key) == TDB_SUCCESS);
66
67                 ok1(tdb_transaction_start(tdb) == TDB_SUCCESS);
68                 ok1(tdb_store(tdb, key, data, TDB_INSERT) == TDB_SUCCESS);
69                 ok1(tdb_transaction_commit(tdb) == TDB_SUCCESS);
70
71                 ok1(tdb_chainlock(tdb, key) == TDB_SUCCESS);
72                 tdb_chainunlock(tdb, key);
73                 ok1(tdb_lockall(tdb) == TDB_SUCCESS);
74                 tdb_unlockall(tdb);
75                 ok1(tdb_wipe_all(tdb) == TDB_SUCCESS);
76                 ok1(tap_log_messages == 7);
77
78                 tdb_close(tdb);
79
80                 /* R0 -> RW */
81                 tdb = tdb_open("run-92-get-set-readonly.tdb", flags[i],
82                                O_RDONLY, 0600, &tap_log_attr);
83                 ok1(tdb);
84                 ok1(tdb_get_flags(tdb) & TDB_RDONLY);
85
86                 /* Can't store, append, delete. */
87                 ok1(tdb_store(tdb, key, data, TDB_INSERT) == TDB_ERR_RDONLY);
88                 ok1(tap_log_messages == 8);
89                 ok1(tdb_append(tdb, key, data) == TDB_ERR_RDONLY);
90                 tap_log_messages -= extra_msgs;
91                 ok1(tap_log_messages == 9);
92                 ok1(tdb_delete(tdb, key) == TDB_ERR_RDONLY);
93                 tap_log_messages -= extra_msgs;
94                 ok1(tap_log_messages == 10);
95
96                 /* Can't start a transaction, or any write lock. */
97                 ok1(tdb_transaction_start(tdb) == TDB_ERR_RDONLY);
98                 ok1(tap_log_messages == 11);
99                 ok1(tdb_chainlock(tdb, key) == TDB_ERR_RDONLY);
100                 tap_log_messages -= extra_msgs;
101                 ok1(tap_log_messages == 12);
102                 ok1(tdb_lockall(tdb) == TDB_ERR_RDONLY);
103                 ok1(tap_log_messages == 13);
104                 ok1(tdb_wipe_all(tdb) == TDB_ERR_RDONLY);
105                 ok1(tap_log_messages == 14);
106
107                 /* Can't remove TDB_RDONLY since we opened with O_RDONLY */
108                 tdb_remove_flag(tdb, TDB_RDONLY);
109                 ok1(tap_log_messages == 15);
110                 ok1(tdb_get_flags(tdb) & TDB_RDONLY);
111                 tdb_close(tdb);
112
113                 ok1(tap_log_messages == 15);
114                 tap_log_messages = 0;
115         }
116         return exit_status();
117 }