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