1 #include "tdb2-source.h"
2 #include <ccan/tap/tap.h>
5 int main(int argc, char *argv[])
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 };
14 plan_tests(sizeof(flags) / sizeof(flags[0]) * 48);
16 for (i = 0; i < sizeof(flags) / sizeof(flags[0]); i++) {
18 tdb = tdb_open("run-92-get-set-readonly.tdb", flags[i],
19 O_RDWR|O_CREAT|O_TRUNC, 0600, &tap_log_attr);
21 ok1(!(tdb_get_flags(tdb) & TDB_RDONLY));
23 ok1(tdb_store(tdb, key, data, TDB_INSERT) == TDB_SUCCESS);
25 tdb_add_flag(tdb, TDB_RDONLY);
26 ok1(tdb_get_flags(tdb) & TDB_RDONLY);
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);
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);
47 tdb_remove_flag(tdb, TDB_RDONLY);
48 ok1(!(tdb_get_flags(tdb) & TDB_RDONLY));
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);
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);
58 ok1(tdb_chainlock(tdb, key) == TDB_SUCCESS);
59 tdb_chainunlock(tdb, key);
60 ok1(tdb_lockall(tdb) == TDB_SUCCESS);
62 ok1(tdb_wipe_all(tdb) == TDB_SUCCESS);
63 ok1(tap_log_messages == 7);
68 tdb = tdb_open("run-92-get-set-readonly.tdb", flags[i],
69 O_RDONLY, 0600, &tap_log_attr);
71 ok1(tdb_get_flags(tdb) & TDB_RDONLY);
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);
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);
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);
97 ok1(tap_log_messages == 15);
100 return exit_status();