]> git.ozlabs.org Git - ccan/blob - ccan/ntdb/test/api-92-get-set-readonly.c
ntdb: fix up tests.
[ccan] / ccan / ntdb / test / api-92-get-set-readonly.c
1 #include "config.h"
2 #include "../ntdb.h"
3 #include "../private.h"
4 #include "tap-interface.h"
5 #include "logging.h"
6 #include "helpapi-external-agent.h"
7
8 int main(int argc, char *argv[])
9 {
10         unsigned int i;
11         struct ntdb_context *ntdb;
12         NTDB_DATA key = ntdb_mkdata("key", 3);
13         NTDB_DATA data = ntdb_mkdata("data", 4);
14         int flags[] = { NTDB_DEFAULT, NTDB_NOMMAP,
15                         NTDB_CONVERT, NTDB_NOMMAP|NTDB_CONVERT };
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                 ntdb = ntdb_open("run-92-get-set-readonly.ntdb",
22                                  flags[i]|MAYBE_NOSYNC,
23                                  O_RDWR|O_CREAT|O_TRUNC, 0600, &tap_log_attr);
24                 ok1(ntdb);
25                 ok1(!(ntdb_get_flags(ntdb) & NTDB_RDONLY));
26
27                 ok1(ntdb_store(ntdb, key, data, NTDB_INSERT) == NTDB_SUCCESS);
28
29                 ntdb_add_flag(ntdb, NTDB_RDONLY);
30                 ok1(ntdb_get_flags(ntdb) & NTDB_RDONLY);
31
32                 /* Can't store, append, delete. */
33                 ok1(ntdb_store(ntdb, key, data, NTDB_MODIFY) == NTDB_ERR_RDONLY);
34                 ok1(tap_log_messages == 1);
35                 ok1(ntdb_append(ntdb, key, data) == NTDB_ERR_RDONLY);
36                 ok1(tap_log_messages == 2);
37                 ok1(ntdb_delete(ntdb, key) == NTDB_ERR_RDONLY);
38                 ok1(tap_log_messages == 3);
39
40                 /* Can't start a transaction, or any write lock. */
41                 ok1(ntdb_transaction_start(ntdb) == NTDB_ERR_RDONLY);
42                 ok1(tap_log_messages == 4);
43                 ok1(ntdb_chainlock(ntdb, key) == NTDB_ERR_RDONLY);
44                 ok1(tap_log_messages == 5);
45                 ok1(ntdb_lockall(ntdb) == NTDB_ERR_RDONLY);
46                 ok1(tap_log_messages == 6);
47                 ok1(ntdb_wipe_all(ntdb) == NTDB_ERR_RDONLY);
48                 ok1(tap_log_messages == 7);
49
50                 /* Back to RW. */
51                 ntdb_remove_flag(ntdb, NTDB_RDONLY);
52                 ok1(!(ntdb_get_flags(ntdb) & NTDB_RDONLY));
53
54                 ok1(ntdb_store(ntdb, key, data, NTDB_MODIFY) == NTDB_SUCCESS);
55                 ok1(ntdb_append(ntdb, key, data) == NTDB_SUCCESS);
56                 ok1(ntdb_delete(ntdb, key) == NTDB_SUCCESS);
57
58                 ok1(ntdb_transaction_start(ntdb) == NTDB_SUCCESS);
59                 ok1(ntdb_store(ntdb, key, data, NTDB_INSERT) == NTDB_SUCCESS);
60                 ok1(ntdb_transaction_commit(ntdb) == NTDB_SUCCESS);
61
62                 ok1(ntdb_chainlock(ntdb, key) == NTDB_SUCCESS);
63                 ntdb_chainunlock(ntdb, key);
64                 ok1(ntdb_lockall(ntdb) == NTDB_SUCCESS);
65                 ntdb_unlockall(ntdb);
66                 ok1(ntdb_wipe_all(ntdb) == NTDB_SUCCESS);
67                 ok1(tap_log_messages == 7);
68
69                 ntdb_close(ntdb);
70
71                 /* R0 -> RW */
72                 ntdb = ntdb_open("run-92-get-set-readonly.ntdb",
73                                  flags[i]|MAYBE_NOSYNC,
74                                  O_RDONLY, 0600, &tap_log_attr);
75                 ok1(ntdb);
76                 ok1(ntdb_get_flags(ntdb) & NTDB_RDONLY);
77
78                 /* Can't store, append, delete. */
79                 ok1(ntdb_store(ntdb, key, data, NTDB_INSERT) == NTDB_ERR_RDONLY);
80                 ok1(tap_log_messages == 8);
81                 ok1(ntdb_append(ntdb, key, data) == NTDB_ERR_RDONLY);
82                 ok1(tap_log_messages == 9);
83                 ok1(ntdb_delete(ntdb, key) == NTDB_ERR_RDONLY);
84                 ok1(tap_log_messages == 10);
85
86                 /* Can't start a transaction, or any write lock. */
87                 ok1(ntdb_transaction_start(ntdb) == NTDB_ERR_RDONLY);
88                 ok1(tap_log_messages == 11);
89                 ok1(ntdb_chainlock(ntdb, key) == NTDB_ERR_RDONLY);
90                 ok1(tap_log_messages == 12);
91                 ok1(ntdb_lockall(ntdb) == NTDB_ERR_RDONLY);
92                 ok1(tap_log_messages == 13);
93                 ok1(ntdb_wipe_all(ntdb) == NTDB_ERR_RDONLY);
94                 ok1(tap_log_messages == 14);
95
96                 /* Can't remove NTDB_RDONLY since we opened with O_RDONLY */
97                 ntdb_remove_flag(ntdb, NTDB_RDONLY);
98                 ok1(tap_log_messages == 15);
99                 ok1(ntdb_get_flags(ntdb) & NTDB_RDONLY);
100                 ntdb_close(ntdb);
101
102                 ok1(tap_log_messages == 15);
103                 tap_log_messages = 0;
104         }
105         return exit_status();
106 }