1 #include <ccan/failtest/failtest_override.h>
2 #include "ntdb-source.h"
3 #include "tap-interface.h"
6 #include "failtest_helper.h"
8 #include "helprun-external-agent.h"
10 static size_t len_of(bool breaks_check, bool breaks_write, bool breaks_open)
22 /* Creates a NTDB with various capabilities. */
23 static void create_ntdb(const char *name,
27 bool breaks_open, ...)
31 struct ntdb_layout *layout;
32 struct ntdb_context *ntdb;
34 union ntdb_attribute seed_attr;
36 /* Force a seed which doesn't allow records to clash! */
37 seed_attr.base.attr = NTDB_ATTRIBUTE_SEED;
38 seed_attr.base.next = &tap_log_attr;
39 seed_attr.seed.seed = 0;
41 key = ntdb_mkdata("Hello", 5);
42 data = ntdb_mkdata("world", 5);
44 /* Create a NTDB with some data, and some capabilities */
45 layout = new_ntdb_layout();
46 ntdb_layout_add_freetable(layout);
47 ntdb_layout_add_used(layout, key, data, 6);
48 clen = len_of(breaks_check, breaks_write, breaks_open);
49 ntdb_layout_add_free(layout, 15496 - clen, 0);
50 ntdb_layout_add_capability(layout, cap,
51 breaks_write, breaks_check, breaks_open,
54 va_start(ap, breaks_open);
55 while ((cap = va_arg(ap, int)) != 0) {
56 breaks_check = va_arg(ap, int);
57 breaks_write = va_arg(ap, int);
58 breaks_open = va_arg(ap, int);
61 ntdb_layout_add_used(layout, key, data, 11 - key.dsize);
62 clen = len_of(breaks_check, breaks_write, breaks_open);
63 ntdb_layout_add_free(layout, 16304 - clen, 0);
64 ntdb_layout_add_capability(layout, cap,
65 breaks_write, breaks_check,
70 /* We open-code this, because we need to use the failtest write. */
71 ntdb = ntdb_layout_get(layout, failtest_free, &seed_attr);
73 fd = open(name, O_RDWR|O_TRUNC|O_CREAT, 0600);
75 err(1, "opening %s for writing", name);
76 if (write(fd, ntdb->file->map_ptr, ntdb->file->map_size)
77 != ntdb->file->map_size)
78 err(1, "writing %s", name);
81 ntdb_layout_free(layout);
84 /* Note all the "goto out" early exits: they're to shorten failtest time. */
85 int main(int argc, char *argv[])
87 struct ntdb_context *ntdb;
90 failtest_init(argc, argv);
91 failtest_hook = block_repeat_failures;
92 failtest_exit_check = exit_check_log;
95 failtest_suppress = true;
96 /* Capability says you can ignore it? */
97 create_ntdb("run-capabilities.ntdb", 1, false, false, false, 0);
99 failtest_suppress = false;
100 ntdb = ntdb_open("run-capabilities.ntdb", MAYBE_NOSYNC, O_RDWR, 0,
102 failtest_suppress = true;
105 ok1(tap_log_messages == 0);
106 ok1(ntdb_check(ntdb, NULL, NULL) == NTDB_SUCCESS);
107 ok1(tap_log_messages == 0);
110 /* Two capabilitues say you can ignore them? */
111 create_ntdb("run-capabilities.ntdb",
112 1, false, false, false,
113 2, false, false, false, 0);
115 failtest_suppress = false;
116 ntdb = ntdb_open("run-capabilities.ntdb", MAYBE_NOSYNC, O_RDWR, 0,
118 failtest_suppress = true;
121 ok1(tap_log_messages == 0);
122 ok1(ntdb_check(ntdb, NULL, NULL) == NTDB_SUCCESS);
123 ok1(tap_log_messages == 0);
124 ok1(ntdb_summary(ntdb, 0, &summary) == NTDB_SUCCESS);
125 ok1(strstr(summary, "Capability 1\n"));
129 /* Capability says you can't check. */
130 create_ntdb("run-capabilities.ntdb",
131 1, false, false, false,
132 2, true, false, false, 0);
134 failtest_suppress = false;
135 ntdb = ntdb_open("run-capabilities.ntdb", MAYBE_NOSYNC, O_RDWR, 0,
137 failtest_suppress = true;
140 ok1(tap_log_messages == 0);
141 ok1(ntdb_get_flags(ntdb) & NTDB_CANT_CHECK);
142 ok1(ntdb_check(ntdb, NULL, NULL) == NTDB_SUCCESS);
143 /* We expect a warning! */
144 ok1(tap_log_messages == 1);
145 ok1(strstr(log_last, "capabilit"));
146 ok1(ntdb_summary(ntdb, 0, &summary) == NTDB_SUCCESS);
147 ok1(strstr(summary, "Capability 1\n"));
148 ok1(strstr(summary, "Capability 2 (uncheckable)\n"));
152 /* Capability says you can't write. */
153 create_ntdb("run-capabilities.ntdb",
154 1, false, false, false,
155 2, false, true, false, 0);
157 failtest_suppress = false;
158 ntdb = ntdb_open("run-capabilities.ntdb", MAYBE_NOSYNC, O_RDWR, 0,
160 failtest_suppress = true;
161 /* We expect a message. */
163 if (!ok1(tap_log_messages == 2))
165 if (!ok1(strstr(log_last, "unknown")))
167 ok1(strstr(log_last, "write"));
169 /* We can open it read-only though! */
170 failtest_suppress = false;
171 ntdb = ntdb_open("run-capabilities.ntdb", MAYBE_NOSYNC, O_RDONLY, 0,
173 failtest_suppress = true;
176 ok1(tap_log_messages == 2);
177 ok1(ntdb_check(ntdb, NULL, NULL) == NTDB_SUCCESS);
178 ok1(tap_log_messages == 2);
179 ok1(ntdb_summary(ntdb, 0, &summary) == NTDB_SUCCESS);
180 ok1(strstr(summary, "Capability 1\n"));
181 ok1(strstr(summary, "Capability 2 (read-only)\n"));
185 /* Capability says you can't open. */
186 create_ntdb("run-capabilities.ntdb",
187 1, false, false, false,
188 2, false, false, true, 0);
190 failtest_suppress = false;
191 ntdb = ntdb_open("run-capabilities.ntdb", MAYBE_NOSYNC, O_RDWR, 0,
193 failtest_suppress = true;
194 /* We expect a message. */
196 if (!ok1(tap_log_messages == 3))
198 if (!ok1(strstr(log_last, "unknown")))
201 /* Combine capabilities correctly. */
202 create_ntdb("run-capabilities.ntdb",
203 1, false, false, false,
204 2, true, false, false,
205 3, false, true, false, 0);
207 failtest_suppress = false;
208 ntdb = ntdb_open("run-capabilities.ntdb", MAYBE_NOSYNC, O_RDWR, 0,
210 failtest_suppress = true;
211 /* We expect a message. */
213 if (!ok1(tap_log_messages == 4))
215 if (!ok1(strstr(log_last, "unknown")))
217 ok1(strstr(log_last, "write"));
219 /* We can open it read-only though! */
220 failtest_suppress = false;
221 ntdb = ntdb_open("run-capabilities.ntdb", MAYBE_NOSYNC, O_RDONLY, 0,
223 failtest_suppress = true;
226 ok1(tap_log_messages == 4);
227 ok1(ntdb_get_flags(ntdb) & NTDB_CANT_CHECK);
228 ok1(ntdb_check(ntdb, NULL, NULL) == NTDB_SUCCESS);
229 /* We expect a warning! */
230 ok1(tap_log_messages == 5);
231 ok1(strstr(log_last, "unknown"));
232 ok1(ntdb_summary(ntdb, 0, &summary) == NTDB_SUCCESS);
233 ok1(strstr(summary, "Capability 1\n"));
234 ok1(strstr(summary, "Capability 2 (uncheckable)\n"));
235 ok1(strstr(summary, "Capability 3 (read-only)\n"));
239 /* Two capability flags in one. */
240 create_ntdb("run-capabilities.ntdb",
241 1, false, false, false,
242 2, true, true, false,
245 failtest_suppress = false;
246 ntdb = ntdb_open("run-capabilities.ntdb", MAYBE_NOSYNC, O_RDWR, 0,
248 failtest_suppress = true;
249 /* We expect a message. */
251 if (!ok1(tap_log_messages == 6))
253 if (!ok1(strstr(log_last, "unknown")))
255 ok1(strstr(log_last, "write"));
257 /* We can open it read-only though! */
258 failtest_suppress = false;
259 ntdb = ntdb_open("run-capabilities.ntdb", MAYBE_NOSYNC, O_RDONLY, 0,
261 failtest_suppress = true;
264 ok1(tap_log_messages == 6);
265 ok1(ntdb_get_flags(ntdb) & NTDB_CANT_CHECK);
266 ok1(ntdb_check(ntdb, NULL, NULL) == NTDB_SUCCESS);
267 /* We expect a warning! */
268 ok1(tap_log_messages == 7);
269 ok1(strstr(log_last, "unknown"));
270 ok1(ntdb_summary(ntdb, 0, &summary) == NTDB_SUCCESS);
271 ok1(strstr(summary, "Capability 1\n"));
272 ok1(strstr(summary, "Capability 2 (uncheckable,read-only)\n"));
277 failtest_exit(exit_status());
280 * We will never reach this but the compiler complains if we do not
281 * return in this function.