summaryrefslogtreecommitdiff
path: root/lib/ntdb/test/run-capabilities.c
blob: cb037468626918f9d6c589213846a0cc112d56d3 (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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
#include <ccan/failtest/failtest_override.h>
#include "ntdb-source.h"
#include "tap-interface.h"
#include "logging.h"
#include "layout.h"
#include "failtest_helper.h"
#include <stdarg.h>

static size_t len_of(bool breaks_check, bool breaks_write, bool breaks_open)
{
	size_t len = 0;
	if (breaks_check)
		len += 8;
	if (breaks_write)
		len += 16;
	if (breaks_open)
		len += 32;
	return len;
}

/* Creates a NTDB with various capabilities. */
static void create_ntdb(const char *name,
		       unsigned int cap,
		       bool breaks_check,
		       bool breaks_write,
		       bool breaks_open, ...)
{
	NTDB_DATA key, data;
	va_list ap;
	struct ntdb_layout *layout;
	struct ntdb_context *ntdb;
	int fd, clen;

	key = ntdb_mkdata("Hello", 5);
	data = ntdb_mkdata("world", 5);

	/* Create a NTDB with some data, and some capabilities */
	layout = new_ntdb_layout();
	ntdb_layout_add_freetable(layout);
	ntdb_layout_add_used(layout, key, data, 6);
	clen = len_of(breaks_check, breaks_write, breaks_open);
	ntdb_layout_add_free(layout, 15496 - clen, 0);
	ntdb_layout_add_capability(layout, cap,
				   breaks_write, breaks_check, breaks_open,
				   clen);

	va_start(ap, breaks_open);
	while ((cap = va_arg(ap, int)) != 0) {
		breaks_check = va_arg(ap, int);
		breaks_write = va_arg(ap, int);
		breaks_open = va_arg(ap, int);

		key.dsize--;
		ntdb_layout_add_used(layout, key, data, 11 - key.dsize);
		clen = len_of(breaks_check, breaks_write, breaks_open);
		ntdb_layout_add_free(layout, 16304 - clen, 0);
		ntdb_layout_add_capability(layout, cap,
					  breaks_write, breaks_check,
					  breaks_open, clen);
	}
	va_end(ap);

	/* We open-code this, because we need to use the failtest write. */
	ntdb = ntdb_layout_get(layout, failtest_free, &tap_log_attr);

	fd = open(name, O_RDWR|O_TRUNC|O_CREAT, 0600);
	if (fd < 0)
		err(1, "opening %s for writing", name);
	if (write(fd, ntdb->file->map_ptr, ntdb->file->map_size)
	    != ntdb->file->map_size)
		err(1, "writing %s", name);
	close(fd);
	ntdb_close(ntdb);
	ntdb_layout_free(layout);
}

/* Note all the "goto out" early exits: they're to shorten failtest time. */
int main(int argc, char *argv[])
{
	struct ntdb_context *ntdb;
	char *summary;

	failtest_init(argc, argv);
	failtest_hook = block_repeat_failures;
	failtest_exit_check = exit_check_log;
	plan_tests(60);

	failtest_suppress = true;
	/* Capability says you can ignore it? */
	create_ntdb("run-capabilities.ntdb", 1, false, false, false, 0);

	failtest_suppress = false;
	ntdb = ntdb_open("run-capabilities.ntdb", NTDB_DEFAULT, O_RDWR, 0,
		       &tap_log_attr);
	failtest_suppress = true;
	if (!ok1(ntdb))
		goto out;
	ok1(tap_log_messages == 0);
	ok1(ntdb_check(ntdb, NULL, NULL) == NTDB_SUCCESS);
	ok1(tap_log_messages == 0);
	ntdb_close(ntdb);

	/* Two capabilitues say you can ignore them? */
	create_ntdb("run-capabilities.ntdb",
		   1, false, false, false,
		   2, false, false, false, 0);

	failtest_suppress = false;
	ntdb = ntdb_open("run-capabilities.ntdb", NTDB_DEFAULT, O_RDWR, 0,
		       &tap_log_attr);
	failtest_suppress = true;
	if (!ok1(ntdb))
		goto out;
	ok1(tap_log_messages == 0);
	ok1(ntdb_check(ntdb, NULL, NULL) == NTDB_SUCCESS);
	ok1(tap_log_messages == 0);
	ok1(ntdb_summary(ntdb, 0, &summary) == NTDB_SUCCESS);
	ok1(strstr(summary, "Capability 1\n"));
	free(summary);
	ntdb_close(ntdb);

	/* Capability says you can't check. */
	create_ntdb("run-capabilities.ntdb",
		   1, false, false, false,
		   2, true, false, false, 0);

	failtest_suppress = false;
	ntdb = ntdb_open("run-capabilities.ntdb", NTDB_DEFAULT, O_RDWR, 0,
		       &tap_log_attr);
	failtest_suppress = true;
	if (!ok1(ntdb))
		goto out;
	ok1(tap_log_messages == 0);
	ok1(ntdb_get_flags(ntdb) & NTDB_CANT_CHECK);
	ok1(ntdb_check(ntdb, NULL, NULL) == NTDB_SUCCESS);
	/* We expect a warning! */
	ok1(tap_log_messages == 1);
	ok1(strstr(log_last, "capabilit"));
	ok1(ntdb_summary(ntdb, 0, &summary) == NTDB_SUCCESS);
	ok1(strstr(summary, "Capability 1\n"));
	ok1(strstr(summary, "Capability 2 (uncheckable)\n"));
	free(summary);
	ntdb_close(ntdb);

	/* Capability says you can't write. */
	create_ntdb("run-capabilities.ntdb",
		   1, false, false, false,
		   2, false, true, false, 0);

	failtest_suppress = false;
	ntdb = ntdb_open("run-capabilities.ntdb", NTDB_DEFAULT, O_RDWR, 0,
		       &tap_log_attr);
	failtest_suppress = true;
	/* We expect a message. */
	ok1(!ntdb);
	if (!ok1(tap_log_messages == 2))
		goto out;
	if (!ok1(strstr(log_last, "unknown")))
		goto out;
	ok1(strstr(log_last, "write"));

	/* We can open it read-only though! */
	failtest_suppress = false;
	ntdb = ntdb_open("run-capabilities.ntdb", NTDB_DEFAULT, O_RDONLY, 0,
		       &tap_log_attr);
	failtest_suppress = true;
	if (!ok1(ntdb))
		goto out;
	ok1(tap_log_messages == 2);
	ok1(ntdb_check(ntdb, NULL, NULL) == NTDB_SUCCESS);
	ok1(tap_log_messages == 2);
	ok1(ntdb_summary(ntdb, 0, &summary) == NTDB_SUCCESS);
	ok1(strstr(summary, "Capability 1\n"));
	ok1(strstr(summary, "Capability 2 (read-only)\n"));
	free(summary);
	ntdb_close(ntdb);

	/* Capability says you can't open. */
	create_ntdb("run-capabilities.ntdb",
		   1, false, false, false,
		   2, false, false, true, 0);

	failtest_suppress = false;
	ntdb = ntdb_open("run-capabilities.ntdb", NTDB_DEFAULT, O_RDWR, 0,
		       &tap_log_attr);
	failtest_suppress = true;
	/* We expect a message. */
	ok1(!ntdb);
	if (!ok1(tap_log_messages == 3))
		goto out;
	if (!ok1(strstr(log_last, "unknown")))
		goto out;

	/* Combine capabilities correctly. */
	create_ntdb("run-capabilities.ntdb",
		   1, false, false, false,
		   2, true, false, false,
		   3, false, true, false, 0);

	failtest_suppress = false;
	ntdb = ntdb_open("run-capabilities.ntdb", NTDB_DEFAULT, O_RDWR, 0,
		       &tap_log_attr);
	failtest_suppress = true;
	/* We expect a message. */
	ok1(!ntdb);
	if (!ok1(tap_log_messages == 4))
		goto out;
	if (!ok1(strstr(log_last, "unknown")))
		goto out;
	ok1(strstr(log_last, "write"));

	/* We can open it read-only though! */
	failtest_suppress = false;
	ntdb = ntdb_open("run-capabilities.ntdb", NTDB_DEFAULT, O_RDONLY, 0,
		       &tap_log_attr);
	failtest_suppress = true;
	if (!ok1(ntdb))
		goto out;
	ok1(tap_log_messages == 4);
	ok1(ntdb_get_flags(ntdb) & NTDB_CANT_CHECK);
	ok1(ntdb_check(ntdb, NULL, NULL) == NTDB_SUCCESS);
	/* We expect a warning! */
	ok1(tap_log_messages == 5);
	ok1(strstr(log_last, "unknown"));
	ok1(ntdb_summary(ntdb, 0, &summary) == NTDB_SUCCESS);
	ok1(strstr(summary, "Capability 1\n"));
	ok1(strstr(summary, "Capability 2 (uncheckable)\n"));
	ok1(strstr(summary, "Capability 3 (read-only)\n"));
	free(summary);
	ntdb_close(ntdb);

	/* Two capability flags in one. */
	create_ntdb("run-capabilities.ntdb",
		   1, false, false, false,
		   2, true, true, false,
		   0);

	failtest_suppress = false;
	ntdb = ntdb_open("run-capabilities.ntdb", NTDB_DEFAULT, O_RDWR, 0,
		       &tap_log_attr);
	failtest_suppress = true;
	/* We expect a message. */
	ok1(!ntdb);
	if (!ok1(tap_log_messages == 6))
		goto out;
	if (!ok1(strstr(log_last, "unknown")))
		goto out;
	ok1(strstr(log_last, "write"));

	/* We can open it read-only though! */
	failtest_suppress = false;
	ntdb = ntdb_open("run-capabilities.ntdb", NTDB_DEFAULT, O_RDONLY, 0,
		       &tap_log_attr);
	failtest_suppress = true;
	if (!ok1(ntdb))
		goto out;
	ok1(tap_log_messages == 6);
	ok1(ntdb_get_flags(ntdb) & NTDB_CANT_CHECK);
	ok1(ntdb_check(ntdb, NULL, NULL) == NTDB_SUCCESS);
	/* We expect a warning! */
	ok1(tap_log_messages == 7);
	ok1(strstr(log_last, "unknown"));
	ok1(ntdb_summary(ntdb, 0, &summary) == NTDB_SUCCESS);
	ok1(strstr(summary, "Capability 1\n"));
	ok1(strstr(summary, "Capability 2 (uncheckable,read-only)\n"));
	free(summary);
	ntdb_close(ntdb);

out:
	failtest_exit(exit_status());
}