1 #include "tdb2-source.h"
2 #include <ccan/tap/tap.h>
5 int main(int argc, char *argv[])
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 };
17 plan_tests(sizeof(flags) / sizeof(flags[0]) * 48);
19 for (i = 0; i < sizeof(flags) / sizeof(flags[0]); i++) {
21 tdb = tdb_open("run-92-get-set-readonly.tdb", flags[i],
22 O_RDWR|O_CREAT|O_TRUNC, 0600, &tap_log_attr);
24 ok1(!(tdb_get_flags(tdb) & TDB_RDONLY));
26 /* TDB1 complains multiple times. */
27 if (flags[i] & TDB_VERSION1) {
33 ok1(tdb_store(tdb, key, data, TDB_INSERT) == TDB_SUCCESS);
35 tdb_add_flag(tdb, TDB_RDONLY);
36 ok1(tdb_get_flags(tdb) & TDB_RDONLY);
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);
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);
60 tdb_remove_flag(tdb, TDB_RDONLY);
61 ok1(!(tdb_get_flags(tdb) & TDB_RDONLY));
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);
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);
71 ok1(tdb_chainlock(tdb, key) == TDB_SUCCESS);
72 tdb_chainunlock(tdb, key);
73 ok1(tdb_lockall(tdb) == TDB_SUCCESS);
75 ok1(tdb_wipe_all(tdb) == TDB_SUCCESS);
76 ok1(tap_log_messages == 7);
81 tdb = tdb_open("run-92-get-set-readonly.tdb", flags[i],
82 O_RDONLY, 0600, &tap_log_attr);
84 ok1(tdb_get_flags(tdb) & TDB_RDONLY);
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);
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);
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);
113 ok1(tap_log_messages == 15);
114 tap_log_messages = 0;
116 return exit_status();