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;
83 failtest_init(argc, argv);
84 failtest_hook = block_repeat_failures;
85 failtest_exit_check = exit_check_log;
88 failtest_suppress = true;
89 /* Capability says you can ignore it? */
90 create_tdb("run-capabilities.tdb", 1, false, false, false, 0);
92 failtest_suppress = false;
93 tdb = tdb_open("run-capabilities.tdb", TDB_DEFAULT, O_RDWR, 0,
95 failtest_suppress = true;
98 ok1(tap_log_messages == 0);
99 ok1(tdb_check(tdb, NULL, NULL) == TDB_SUCCESS);
100 ok1(tap_log_messages == 0);
103 /* Two capabilitues say you can ignore them? */
104 create_tdb("run-capabilities.tdb",
105 1, false, false, false,
106 2, false, false, false, 0);
108 failtest_suppress = false;
109 tdb = tdb_open("run-capabilities.tdb", TDB_DEFAULT, O_RDWR, 0,
111 failtest_suppress = true;
114 ok1(tap_log_messages == 0);
115 ok1(tdb_check(tdb, NULL, NULL) == TDB_SUCCESS);
116 ok1(tap_log_messages == 0);
119 /* Capability says you can't check. */
120 create_tdb("run-capabilities.tdb",
121 1, false, false, false,
122 2, true, false, false, 0);
124 failtest_suppress = false;
125 tdb = tdb_open("run-capabilities.tdb", TDB_DEFAULT, O_RDWR, 0,
127 failtest_suppress = true;
130 ok1(tap_log_messages == 0);
131 ok1(tdb_get_flags(tdb) & TDB_CANT_CHECK);
132 ok1(tdb_check(tdb, NULL, NULL) == TDB_SUCCESS);
133 /* We expect a warning! */
134 ok1(tap_log_messages == 1);
135 ok1(strstr(log_last, "capabilit"));
138 /* Capability says you can't write. */
139 create_tdb("run-capabilities.tdb",
140 1, false, false, false,
141 2, false, true, false, 0);
143 failtest_suppress = false;
144 tdb = tdb_open("run-capabilities.tdb", TDB_DEFAULT, O_RDWR, 0,
146 failtest_suppress = true;
147 /* We expect a message. */
149 if (!ok1(tap_log_messages == 2))
151 if (!ok1(strstr(log_last, "unknown")))
153 ok1(strstr(log_last, "write"));
155 /* We can open it read-only though! */
156 failtest_suppress = false;
157 tdb = tdb_open("run-capabilities.tdb", TDB_DEFAULT, O_RDONLY, 0,
159 failtest_suppress = true;
162 ok1(tap_log_messages == 2);
163 ok1(tdb_check(tdb, NULL, NULL) == TDB_SUCCESS);
164 ok1(tap_log_messages == 2);
167 /* Capability says you can't open. */
168 create_tdb("run-capabilities.tdb",
169 1, false, false, false,
170 2, false, false, true, 0);
172 failtest_suppress = false;
173 tdb = tdb_open("run-capabilities.tdb", TDB_DEFAULT, O_RDWR, 0,
175 failtest_suppress = true;
176 /* We expect a message. */
178 if (!ok1(tap_log_messages == 3))
180 if (!ok1(strstr(log_last, "unknown")))
183 /* Combine capabilities correctly. */
184 create_tdb("run-capabilities.tdb",
185 1, false, false, false,
186 2, true, false, false,
187 3, false, true, false, 0);
189 failtest_suppress = false;
190 tdb = tdb_open("run-capabilities.tdb", TDB_DEFAULT, O_RDWR, 0,
192 failtest_suppress = true;
193 /* We expect a message. */
195 if (!ok1(tap_log_messages == 4))
197 if (!ok1(strstr(log_last, "unknown")))
199 ok1(strstr(log_last, "write"));
201 /* We can open it read-only though! */
202 failtest_suppress = false;
203 tdb = tdb_open("run-capabilities.tdb", TDB_DEFAULT, O_RDONLY, 0,
205 failtest_suppress = true;
208 ok1(tap_log_messages == 4);
209 ok1(tdb_get_flags(tdb) & TDB_CANT_CHECK);
210 ok1(tdb_check(tdb, NULL, NULL) == TDB_SUCCESS);
211 /* We expect a warning! */
212 ok1(tap_log_messages == 5);
213 ok1(strstr(log_last, "unknown"));
217 failtest_exit(exit_status());