1 #include <ccan/tdb2/tdb2.h>
2 #include <ccan/tap/tap.h>
8 int main(int argc
, char *argv
[])
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
};
20 plan_tests(sizeof(flags
) / sizeof(flags
[0]) * 48);
22 for (i
= 0; i
< sizeof(flags
) / sizeof(flags
[0]); i
++) {
24 tdb
= tdb_open("run-92-get-set-readonly.tdb", flags
[i
],
25 O_RDWR
|O_CREAT
|O_TRUNC
, 0600, &tap_log_attr
);
27 ok1(!(tdb_get_flags(tdb
) & TDB_RDONLY
));
29 /* TDB1 complains multiple times. */
30 if (flags
[i
] & TDB_VERSION1
) {
36 ok1(tdb_store(tdb
, key
, data
, TDB_INSERT
) == TDB_SUCCESS
);
38 tdb_add_flag(tdb
, TDB_RDONLY
);
39 ok1(tdb_get_flags(tdb
) & TDB_RDONLY
);
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);
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);
63 tdb_remove_flag(tdb
, TDB_RDONLY
);
64 ok1(!(tdb_get_flags(tdb
) & TDB_RDONLY
));
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
);
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
);
74 ok1(tdb_chainlock(tdb
, key
) == TDB_SUCCESS
);
75 tdb_chainunlock(tdb
, key
);
76 ok1(tdb_lockall(tdb
) == TDB_SUCCESS
);
78 ok1(tdb_wipe_all(tdb
) == TDB_SUCCESS
);
79 ok1(tap_log_messages
== 7);
84 tdb
= tdb_open("run-92-get-set-readonly.tdb", flags
[i
],
85 O_RDONLY
, 0600, &tap_log_attr
);
87 ok1(tdb_get_flags(tdb
) & TDB_RDONLY
);
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);
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);
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
);
116 ok1(tap_log_messages
== 15);
117 tap_log_messages
= 0;
119 return exit_status();