]> git.ozlabs.org Git - ccan/blob - ccan/tdb2/test/run-tdb1-readonly-check.c
2c06ca92eeb02cdc930f57b5677db93d0743001e
[ccan] / ccan / tdb2 / test / run-tdb1-readonly-check.c
1 /* We should be able to tdb1_check a O_RDONLY tdb, and we were previously allowed
2  * to tdb1_check() inside a transaction (though that's paranoia!). */
3 #include "tdb2-source.h"
4 #include <ccan/tap/tap.h>
5 #include <stdlib.h>
6 #include <err.h>
7 #include "tdb1-logging.h"
8
9 int main(int argc, char *argv[])
10 {
11         struct tdb1_context *tdb;
12         TDB1_DATA key, data;
13
14         plan_tests(11);
15         tdb = tdb1_open_ex("run-readonly-check.tdb", 1024,
16                           TDB1_DEFAULT,
17                           O_CREAT|O_TRUNC|O_RDWR, 0600, &taplogctx, NULL);
18
19         ok1(tdb);
20         key.dsize = strlen("hi");
21         key.dptr = (void *)"hi";
22         data.dsize = strlen("world");
23         data.dptr = (void *)"world";
24
25         ok1(tdb1_store(tdb, key, data, TDB1_INSERT) == 0);
26         ok1(tdb1_check(tdb, NULL, NULL) == 0);
27
28         /* We are also allowed to do a check inside a transaction. */
29         ok1(tdb1_transaction_start(tdb) == 0);
30         ok1(tdb1_check(tdb, NULL, NULL) == 0);
31         ok1(tdb1_close(tdb) == 0);
32
33         tdb = tdb1_open_ex("run-readonly-check.tdb", 1024,
34                           TDB1_DEFAULT, O_RDONLY, 0, &taplogctx, NULL);
35
36         ok1(tdb);
37         ok1(tdb1_store(tdb, key, data, TDB1_MODIFY) == -1);
38         ok1(tdb1_error(tdb) == TDB1_ERR_RDONLY);
39         ok1(tdb1_check(tdb, NULL, NULL) == 0);
40         ok1(tdb1_close(tdb) == 0);
41
42         return exit_status();
43 }