]> git.ozlabs.org Git - ccan/blob - ccan/tdb2/test/run-92-get-set-readonly.c
tdb2: Make TDB1 code use TDB2's open flags.
[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;
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
14         plan_tests(sizeof(flags) / sizeof(flags[0]) * 48);
15
16         for (i = 0; i < sizeof(flags) / sizeof(flags[0]); i++) {
17                 /* RW -> R0 */
18                 tdb = tdb_open("run-92-get-set-readonly.tdb", flags[i],
19                                O_RDWR|O_CREAT|O_TRUNC, 0600, &tap_log_attr);
20                 ok1(tdb);
21                 ok1(!(tdb_get_flags(tdb) & TDB_RDONLY));
22
23                 ok1(tdb_store(tdb, key, data, TDB_INSERT) == TDB_SUCCESS);
24
25                 tdb_add_flag(tdb, TDB_RDONLY);
26                 ok1(tdb_get_flags(tdb) & TDB_RDONLY);
27
28                 /* Can't store, append, delete. */
29                 ok1(tdb_store(tdb, key, data, TDB_MODIFY) == TDB_ERR_RDONLY);
30                 ok1(tap_log_messages == 1);
31                 ok1(tdb_append(tdb, key, data) == TDB_ERR_RDONLY);
32                 ok1(tap_log_messages == 2);
33                 ok1(tdb_delete(tdb, key) == TDB_ERR_RDONLY);
34                 ok1(tap_log_messages == 3);
35
36                 /* Can't start a transaction, or any write lock. */
37                 ok1(tdb_transaction_start(tdb) == TDB_ERR_RDONLY);
38                 ok1(tap_log_messages == 4);
39                 ok1(tdb_chainlock(tdb, key) == TDB_ERR_RDONLY);
40                 ok1(tap_log_messages == 5);
41                 ok1(tdb_lockall(tdb) == TDB_ERR_RDONLY);
42                 ok1(tap_log_messages == 6);
43                 ok1(tdb_wipe_all(tdb) == TDB_ERR_RDONLY);
44                 ok1(tap_log_messages == 7);
45
46                 /* Back to RW. */
47                 tdb_remove_flag(tdb, TDB_RDONLY);
48                 ok1(!(tdb_get_flags(tdb) & TDB_RDONLY));
49
50                 ok1(tdb_store(tdb, key, data, TDB_MODIFY) == TDB_SUCCESS);
51                 ok1(tdb_append(tdb, key, data) == TDB_SUCCESS);
52                 ok1(tdb_delete(tdb, key) == TDB_SUCCESS);
53
54                 ok1(tdb_transaction_start(tdb) == TDB_SUCCESS);
55                 ok1(tdb_store(tdb, key, data, TDB_INSERT) == TDB_SUCCESS);
56                 ok1(tdb_transaction_commit(tdb) == TDB_SUCCESS);
57
58                 ok1(tdb_chainlock(tdb, key) == TDB_SUCCESS);
59                 tdb_chainunlock(tdb, key);
60                 ok1(tdb_lockall(tdb) == TDB_SUCCESS);
61                 tdb_unlockall(tdb);
62                 ok1(tdb_wipe_all(tdb) == TDB_SUCCESS);
63                 ok1(tap_log_messages == 7);
64
65                 tdb_close(tdb);
66
67                 /* R0 -> RW */
68                 tdb = tdb_open("run-92-get-set-readonly.tdb", flags[i],
69                                O_RDONLY, 0600, &tap_log_attr);
70                 ok1(tdb);
71                 ok1(tdb_get_flags(tdb) & TDB_RDONLY);
72
73                 /* Can't store, append, delete. */
74                 ok1(tdb_store(tdb, key, data, TDB_INSERT) == TDB_ERR_RDONLY);
75                 ok1(tap_log_messages == 8);
76                 ok1(tdb_append(tdb, key, data) == TDB_ERR_RDONLY);
77                 ok1(tap_log_messages == 9);
78                 ok1(tdb_delete(tdb, key) == TDB_ERR_RDONLY);
79                 ok1(tap_log_messages == 10);
80
81                 /* Can't start a transaction, or any write lock. */
82                 ok1(tdb_transaction_start(tdb) == TDB_ERR_RDONLY);
83                 ok1(tap_log_messages == 11);
84                 ok1(tdb_chainlock(tdb, key) == TDB_ERR_RDONLY);
85                 ok1(tap_log_messages == 12);
86                 ok1(tdb_lockall(tdb) == TDB_ERR_RDONLY);
87                 ok1(tap_log_messages == 13);
88                 ok1(tdb_wipe_all(tdb) == TDB_ERR_RDONLY);
89                 ok1(tap_log_messages == 14);
90
91                 /* Can't remove TDB_RDONLY since we opened with O_RDONLY */
92                 tdb_remove_flag(tdb, TDB_RDONLY);
93                 ok1(tap_log_messages == 15);
94                 ok1(tdb_get_flags(tdb) & TDB_RDONLY);
95                 tdb_close(tdb);
96
97                 ok1(tap_log_messages == 15);
98                 tap_log_messages = 0;
99         }
100         return exit_status();
101 }