summaryrefslogtreecommitdiff
path: root/lib/tdb2/test
diff options
context:
space:
mode:
authorRusty Russell <rusty@rustcorp.com.au>2011-09-14 07:16:13 +0930
committerRusty Russell <rusty@rustcorp.com.au>2011-09-14 07:16:13 +0930
commitbdc5499205367eccef5700cba8af95ba941ac9b2 (patch)
tree1e6da444d90d469c8cd600c0006c7477150b5aa8 /lib/tdb2/test
parent37c704be0af3b5915e6630264dc4379309d83160 (diff)
downloadsamba-bdc5499205367eccef5700cba8af95ba941ac9b2.tar.gz
samba-bdc5499205367eccef5700cba8af95ba941ac9b2.tar.bz2
samba-bdc5499205367eccef5700cba8af95ba941ac9b2.zip
tdb2: add TDB_RDONLY flag, allow setting/unsetting it.
You can only unset it if the TDB was originally opened O_RDWR. Also, cleaned up error handling in tdb_allrecord_lock() so we only get one log message on a r/o database. Signed-off-by: Rusty Russell <rusty@rustcorp.com.au> (Imported from CCAN commit b87e14495d5b07e1b247218a72329f10ecb3da7f)
Diffstat (limited to 'lib/tdb2/test')
-rw-r--r--lib/tdb2/test/run-92-get-set-readonly.c109
1 files changed, 109 insertions, 0 deletions
diff --git a/lib/tdb2/test/run-92-get-set-readonly.c b/lib/tdb2/test/run-92-get-set-readonly.c
new file mode 100644
index 0000000000..09a6010c18
--- /dev/null
+++ b/lib/tdb2/test/run-92-get-set-readonly.c
@@ -0,0 +1,109 @@
+#include <ccan/tdb2/tdb.c>
+#include <ccan/tdb2/open.c>
+#include <ccan/tdb2/free.c>
+#include <ccan/tdb2/lock.c>
+#include <ccan/tdb2/io.c>
+#include <ccan/tdb2/hash.c>
+#include <ccan/tdb2/check.c>
+#include <ccan/tdb2/transaction.c>
+#include <ccan/tdb2/traverse.c>
+#include <ccan/tap/tap.h>
+#include "logging.h"
+
+int main(int argc, char *argv[])
+{
+ unsigned int i;
+ struct tdb_context *tdb;
+ struct tdb_data key = tdb_mkdata("key", 3);
+ struct tdb_data data = tdb_mkdata("data", 4);
+ int flags[] = { TDB_DEFAULT, TDB_NOMMAP,
+ TDB_CONVERT, TDB_NOMMAP|TDB_CONVERT };
+
+ plan_tests(sizeof(flags) / sizeof(flags[0]) * 48);
+
+ for (i = 0; i < sizeof(flags) / sizeof(flags[0]); i++) {
+ /* RW -> R0 */
+ tdb = tdb_open("run-92-get-set-readonly.tdb", flags[i],
+ O_RDWR|O_CREAT|O_TRUNC, 0600, &tap_log_attr);
+ ok1(tdb);
+ ok1(!(tdb_get_flags(tdb) & TDB_RDONLY));
+
+ ok1(tdb_store(tdb, key, data, TDB_INSERT) == TDB_SUCCESS);
+
+ tdb_add_flag(tdb, TDB_RDONLY);
+ ok1(tdb_get_flags(tdb) & TDB_RDONLY);
+
+ /* Can't store, append, delete. */
+ ok1(tdb_store(tdb, key, data, TDB_MODIFY) == TDB_ERR_RDONLY);
+ ok1(tap_log_messages == 1);
+ ok1(tdb_append(tdb, key, data) == TDB_ERR_RDONLY);
+ ok1(tap_log_messages == 2);
+ ok1(tdb_delete(tdb, key) == TDB_ERR_RDONLY);
+ ok1(tap_log_messages == 3);
+
+ /* Can't start a transaction, or any write lock. */
+ ok1(tdb_transaction_start(tdb) == TDB_ERR_RDONLY);
+ ok1(tap_log_messages == 4);
+ ok1(tdb_chainlock(tdb, key) == TDB_ERR_RDONLY);
+ ok1(tap_log_messages == 5);
+ ok1(tdb_lockall(tdb) == TDB_ERR_RDONLY);
+ ok1(tap_log_messages == 6);
+ ok1(tdb_wipe_all(tdb) == TDB_ERR_RDONLY);
+ ok1(tap_log_messages == 7);
+
+ /* Back to RW. */
+ tdb_remove_flag(tdb, TDB_RDONLY);
+ ok1(!(tdb_get_flags(tdb) & TDB_RDONLY));
+
+ ok1(tdb_store(tdb, key, data, TDB_MODIFY) == TDB_SUCCESS);
+ ok1(tdb_append(tdb, key, data) == TDB_SUCCESS);
+ ok1(tdb_delete(tdb, key) == TDB_SUCCESS);
+
+ ok1(tdb_transaction_start(tdb) == TDB_SUCCESS);
+ ok1(tdb_store(tdb, key, data, TDB_INSERT) == TDB_SUCCESS);
+ ok1(tdb_transaction_commit(tdb) == TDB_SUCCESS);
+
+ ok1(tdb_chainlock(tdb, key) == TDB_SUCCESS);
+ tdb_chainunlock(tdb, key);
+ ok1(tdb_lockall(tdb) == TDB_SUCCESS);
+ tdb_unlockall(tdb);
+ ok1(tdb_wipe_all(tdb) == TDB_SUCCESS);
+ ok1(tap_log_messages == 7);
+
+ tdb_close(tdb);
+
+ /* R0 -> RW */
+ tdb = tdb_open("run-92-get-set-readonly.tdb", flags[i],
+ O_RDONLY, 0600, &tap_log_attr);
+ ok1(tdb);
+ ok1(tdb_get_flags(tdb) & TDB_RDONLY);
+
+ /* Can't store, append, delete. */
+ ok1(tdb_store(tdb, key, data, TDB_INSERT) == TDB_ERR_RDONLY);
+ ok1(tap_log_messages == 8);
+ ok1(tdb_append(tdb, key, data) == TDB_ERR_RDONLY);
+ ok1(tap_log_messages == 9);
+ ok1(tdb_delete(tdb, key) == TDB_ERR_RDONLY);
+ ok1(tap_log_messages == 10);
+
+ /* Can't start a transaction, or any write lock. */
+ ok1(tdb_transaction_start(tdb) == TDB_ERR_RDONLY);
+ ok1(tap_log_messages == 11);
+ ok1(tdb_chainlock(tdb, key) == TDB_ERR_RDONLY);
+ ok1(tap_log_messages == 12);
+ ok1(tdb_lockall(tdb) == TDB_ERR_RDONLY);
+ ok1(tap_log_messages == 13);
+ ok1(tdb_wipe_all(tdb) == TDB_ERR_RDONLY);
+ ok1(tap_log_messages == 14);
+
+ /* Can't remove TDB_RDONLY since we opened with O_RDONLY */
+ tdb_remove_flag(tdb, TDB_RDONLY);
+ ok1(tap_log_messages == 15);
+ ok1(tdb_get_flags(tdb) & TDB_RDONLY);
+ tdb_close(tdb);
+
+ ok1(tap_log_messages == 15);
+ tap_log_messages = 0;
+ }
+ return exit_status();
+}