summaryrefslogtreecommitdiff
path: root/lib/ntdb/test/api-92-get-set-readonly.c
blob: 81eadff32321bd5a7ddc3353543a4b5387b72e6e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
#include "config.h"
#include "ntdb.h"
#include "tap-interface.h"
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include "logging.h"

int main(int argc, char *argv[])
{
	unsigned int i;
	struct ntdb_context *ntdb;
	NTDB_DATA key = ntdb_mkdata("key", 3);
	NTDB_DATA data = ntdb_mkdata("data", 4);
	int flags[] = { NTDB_DEFAULT, NTDB_NOMMAP,
			NTDB_CONVERT, NTDB_NOMMAP|NTDB_CONVERT };

	plan_tests(sizeof(flags) / sizeof(flags[0]) * 48);

	for (i = 0; i < sizeof(flags) / sizeof(flags[0]); i++) {
		/* RW -> R0 */
		ntdb = ntdb_open("run-92-get-set-readonly.ntdb",
				 flags[i]|MAYBE_NOSYNC,
				 O_RDWR|O_CREAT|O_TRUNC, 0600, &tap_log_attr);
		ok1(ntdb);
		ok1(!(ntdb_get_flags(ntdb) & NTDB_RDONLY));

		ok1(ntdb_store(ntdb, key, data, NTDB_INSERT) == NTDB_SUCCESS);

		ntdb_add_flag(ntdb, NTDB_RDONLY);
		ok1(ntdb_get_flags(ntdb) & NTDB_RDONLY);

		/* Can't store, append, delete. */
		ok1(ntdb_store(ntdb, key, data, NTDB_MODIFY) == NTDB_ERR_RDONLY);
		ok1(tap_log_messages == 1);
		ok1(ntdb_append(ntdb, key, data) == NTDB_ERR_RDONLY);
		ok1(tap_log_messages == 2);
		ok1(ntdb_delete(ntdb, key) == NTDB_ERR_RDONLY);
		ok1(tap_log_messages == 3);

		/* Can't start a transaction, or any write lock. */
		ok1(ntdb_transaction_start(ntdb) == NTDB_ERR_RDONLY);
		ok1(tap_log_messages == 4);
		ok1(ntdb_chainlock(ntdb, key) == NTDB_ERR_RDONLY);
		ok1(tap_log_messages == 5);
		ok1(ntdb_lockall(ntdb) == NTDB_ERR_RDONLY);
		ok1(tap_log_messages == 6);
		ok1(ntdb_wipe_all(ntdb) == NTDB_ERR_RDONLY);
		ok1(tap_log_messages == 7);

		/* Back to RW. */
		ntdb_remove_flag(ntdb, NTDB_RDONLY);
		ok1(!(ntdb_get_flags(ntdb) & NTDB_RDONLY));

		ok1(ntdb_store(ntdb, key, data, NTDB_MODIFY) == NTDB_SUCCESS);
		ok1(ntdb_append(ntdb, key, data) == NTDB_SUCCESS);
		ok1(ntdb_delete(ntdb, key) == NTDB_SUCCESS);

		ok1(ntdb_transaction_start(ntdb) == NTDB_SUCCESS);
		ok1(ntdb_store(ntdb, key, data, NTDB_INSERT) == NTDB_SUCCESS);
		ok1(ntdb_transaction_commit(ntdb) == NTDB_SUCCESS);

		ok1(ntdb_chainlock(ntdb, key) == NTDB_SUCCESS);
		ntdb_chainunlock(ntdb, key);
		ok1(ntdb_lockall(ntdb) == NTDB_SUCCESS);
		ntdb_unlockall(ntdb);
		ok1(ntdb_wipe_all(ntdb) == NTDB_SUCCESS);
		ok1(tap_log_messages == 7);

		ntdb_close(ntdb);

		/* R0 -> RW */
		ntdb = ntdb_open("run-92-get-set-readonly.ntdb",
				 flags[i]|MAYBE_NOSYNC,
				 O_RDONLY, 0600, &tap_log_attr);
		ok1(ntdb);
		ok1(ntdb_get_flags(ntdb) & NTDB_RDONLY);

		/* Can't store, append, delete. */
		ok1(ntdb_store(ntdb, key, data, NTDB_INSERT) == NTDB_ERR_RDONLY);
		ok1(tap_log_messages == 8);
		ok1(ntdb_append(ntdb, key, data) == NTDB_ERR_RDONLY);
		ok1(tap_log_messages == 9);
		ok1(ntdb_delete(ntdb, key) == NTDB_ERR_RDONLY);
		ok1(tap_log_messages == 10);

		/* Can't start a transaction, or any write lock. */
		ok1(ntdb_transaction_start(ntdb) == NTDB_ERR_RDONLY);
		ok1(tap_log_messages == 11);
		ok1(ntdb_chainlock(ntdb, key) == NTDB_ERR_RDONLY);
		ok1(tap_log_messages == 12);
		ok1(ntdb_lockall(ntdb) == NTDB_ERR_RDONLY);
		ok1(tap_log_messages == 13);
		ok1(ntdb_wipe_all(ntdb) == NTDB_ERR_RDONLY);
		ok1(tap_log_messages == 14);

		/* Can't remove NTDB_RDONLY since we opened with O_RDONLY */
		ntdb_remove_flag(ntdb, NTDB_RDONLY);
		ok1(tap_log_messages == 15);
		ok1(ntdb_get_flags(ntdb) & NTDB_RDONLY);
		ntdb_close(ntdb);

		ok1(tap_log_messages == 15);
		tap_log_messages = 0;
	}
	return exit_status();
}