1 #include <ccan/failtest/failtest_override.h>
2 #include "tdb2-source.h"
3 #include <ccan/tap/tap.h>
6 #include "failtest_helper.h"
10 static size_t len_of(bool breaks_check, bool breaks_write, bool breaks_open)
22 /* Creates a TDB with various capabilities. */
23 static void create_tdb(const char *name,
27 bool breaks_open, ...)
31 struct tdb_layout *layout;
32 struct tdb_context *tdb;
35 key = tdb_mkdata("Hello", 5);
36 data = tdb_mkdata("world", 5);
38 /* Create a TDB with some data, and some capabilities */
39 layout = new_tdb_layout();
40 tdb_layout_add_freetable(layout);
41 tdb_layout_add_used(layout, key, data, 6);
42 tdb_layout_add_free(layout, 80, 0);
43 tdb_layout_add_capability(layout, cap,
44 breaks_write, breaks_check, breaks_open,
45 len_of(breaks_check, breaks_write, breaks_open));
47 va_start(ap, breaks_open);
48 while ((cap = va_arg(ap, int)) != 0) {
49 breaks_check = va_arg(ap, int);
50 breaks_write = va_arg(ap, int);
51 breaks_open = va_arg(ap, int);
54 tdb_layout_add_used(layout, key, data, 11 - key.dsize);
55 tdb_layout_add_free(layout, 80, 0);
56 tdb_layout_add_capability(layout, cap,
57 breaks_write, breaks_check,
59 len_of(breaks_check, breaks_write,
64 /* We open-code this, because we need to use the failtest write. */
65 tdb = tdb_layout_get(layout, failtest_free, &tap_log_attr);
67 fd = open(name, O_RDWR|O_TRUNC|O_CREAT, 0600);
69 err(1, "opening %s for writing", name);
70 if (write(fd, tdb->file->map_ptr, tdb->file->map_size)
71 != tdb->file->map_size)
72 err(1, "writing %s", name);
75 tdb_layout_free(layout);
78 /* Note all the "goto out" early exits: they're to shorten failtest time. */
79 int main(int argc, char *argv[])
81 struct tdb_context *tdb;
84 failtest_init(argc, argv);
85 failtest_hook = block_repeat_failures;
86 failtest_exit_check = exit_check_log;
89 failtest_suppress = true;
90 /* Capability says you can ignore it? */
91 create_tdb("run-capabilities.tdb", 1, false, false, false, 0);
93 failtest_suppress = false;
94 tdb = tdb_open("run-capabilities.tdb", TDB_DEFAULT, O_RDWR, 0,
96 failtest_suppress = true;
99 ok1(tap_log_messages == 0);
100 ok1(tdb_check(tdb, NULL, NULL) == TDB_SUCCESS);
101 ok1(tap_log_messages == 0);
104 /* Two capabilitues say you can ignore them? */
105 create_tdb("run-capabilities.tdb",
106 1, false, false, false,
107 2, false, false, false, 0);
109 failtest_suppress = false;
110 tdb = tdb_open("run-capabilities.tdb", TDB_DEFAULT, O_RDWR, 0,
112 failtest_suppress = true;
115 ok1(tap_log_messages == 0);
116 ok1(tdb_check(tdb, NULL, NULL) == TDB_SUCCESS);
117 ok1(tap_log_messages == 0);
118 ok1(tdb_summary(tdb, 0, &summary) == TDB_SUCCESS);
119 ok1(strstr(summary, "Capability 1\n"));
123 /* Capability says you can't check. */
124 create_tdb("run-capabilities.tdb",
125 1, false, false, false,
126 2, true, false, false, 0);
128 failtest_suppress = false;
129 tdb = tdb_open("run-capabilities.tdb", TDB_DEFAULT, O_RDWR, 0,
131 failtest_suppress = true;
134 ok1(tap_log_messages == 0);
135 ok1(tdb_get_flags(tdb) & TDB_CANT_CHECK);
136 ok1(tdb_check(tdb, NULL, NULL) == TDB_SUCCESS);
137 /* We expect a warning! */
138 ok1(tap_log_messages == 1);
139 ok1(strstr(log_last, "capabilit"));
140 ok1(tdb_summary(tdb, 0, &summary) == TDB_SUCCESS);
141 ok1(strstr(summary, "Capability 1\n"));
142 ok1(strstr(summary, "Capability 2 (uncheckable)\n"));
146 /* Capability says you can't write. */
147 create_tdb("run-capabilities.tdb",
148 1, false, false, false,
149 2, false, true, false, 0);
151 failtest_suppress = false;
152 tdb = tdb_open("run-capabilities.tdb", TDB_DEFAULT, O_RDWR, 0,
154 failtest_suppress = true;
155 /* We expect a message. */
157 if (!ok1(tap_log_messages == 2))
159 if (!ok1(strstr(log_last, "unknown")))
161 ok1(strstr(log_last, "write"));
163 /* We can open it read-only though! */
164 failtest_suppress = false;
165 tdb = tdb_open("run-capabilities.tdb", TDB_DEFAULT, O_RDONLY, 0,
167 failtest_suppress = true;
170 ok1(tap_log_messages == 2);
171 ok1(tdb_check(tdb, NULL, NULL) == TDB_SUCCESS);
172 ok1(tap_log_messages == 2);
173 ok1(tdb_summary(tdb, 0, &summary) == TDB_SUCCESS);
174 ok1(strstr(summary, "Capability 1\n"));
175 ok1(strstr(summary, "Capability 2 (read-only)\n"));
179 /* Capability says you can't open. */
180 create_tdb("run-capabilities.tdb",
181 1, false, false, false,
182 2, false, false, true, 0);
184 failtest_suppress = false;
185 tdb = tdb_open("run-capabilities.tdb", TDB_DEFAULT, O_RDWR, 0,
187 failtest_suppress = true;
188 /* We expect a message. */
190 if (!ok1(tap_log_messages == 3))
192 if (!ok1(strstr(log_last, "unknown")))
195 /* Combine capabilities correctly. */
196 create_tdb("run-capabilities.tdb",
197 1, false, false, false,
198 2, true, false, false,
199 3, false, true, false, 0);
201 failtest_suppress = false;
202 tdb = tdb_open("run-capabilities.tdb", TDB_DEFAULT, O_RDWR, 0,
204 failtest_suppress = true;
205 /* We expect a message. */
207 if (!ok1(tap_log_messages == 4))
209 if (!ok1(strstr(log_last, "unknown")))
211 ok1(strstr(log_last, "write"));
213 /* We can open it read-only though! */
214 failtest_suppress = false;
215 tdb = tdb_open("run-capabilities.tdb", TDB_DEFAULT, O_RDONLY, 0,
217 failtest_suppress = true;
220 ok1(tap_log_messages == 4);
221 ok1(tdb_get_flags(tdb) & TDB_CANT_CHECK);
222 ok1(tdb_check(tdb, NULL, NULL) == TDB_SUCCESS);
223 /* We expect a warning! */
224 ok1(tap_log_messages == 5);
225 ok1(strstr(log_last, "unknown"));
226 ok1(tdb_summary(tdb, 0, &summary) == TDB_SUCCESS);
227 ok1(strstr(summary, "Capability 1\n"));
228 ok1(strstr(summary, "Capability 2 (uncheckable)\n"));
229 ok1(strstr(summary, "Capability 3 (read-only)\n"));
233 /* Two capability flags in one. */
234 create_tdb("run-capabilities.tdb",
235 1, false, false, false,
236 2, true, true, false,
239 failtest_suppress = false;
240 tdb = tdb_open("run-capabilities.tdb", TDB_DEFAULT, O_RDWR, 0,
242 failtest_suppress = true;
243 /* We expect a message. */
245 if (!ok1(tap_log_messages == 6))
247 if (!ok1(strstr(log_last, "unknown")))
249 ok1(strstr(log_last, "write"));
251 /* We can open it read-only though! */
252 failtest_suppress = false;
253 tdb = tdb_open("run-capabilities.tdb", TDB_DEFAULT, O_RDONLY, 0,
255 failtest_suppress = true;
258 ok1(tap_log_messages == 6);
259 ok1(tdb_get_flags(tdb) & TDB_CANT_CHECK);
260 ok1(tdb_check(tdb, NULL, NULL) == TDB_SUCCESS);
261 /* We expect a warning! */
262 ok1(tap_log_messages == 7);
263 ok1(strstr(log_last, "unknown"));
264 ok1(tdb_summary(tdb, 0, &summary) == TDB_SUCCESS);
265 ok1(strstr(summary, "Capability 1\n"));
266 ok1(strstr(summary, "Capability 2 (uncheckable,read-only)\n"));
271 failtest_exit(exit_status());