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