tdb: fix tdb_check() on read-only TDBs to actually work.
[ccan] / ccan / tdb / test / run-readonly-check.c
1 /* We should be able to tdb_check a O_RDONLY tdb, and we were previously allowed
2  * to tdb_check() inside a transaction (though that's paranoia!). */
3 #define _XOPEN_SOURCE 500
4 #include <ccan/tdb/tdb.h>
5 #include <ccan/tdb/io.c>
6 #include <ccan/tdb/tdb.c>
7 #include <ccan/tdb/lock.c>
8 #include <ccan/tdb/freelist.c>
9 #include <ccan/tdb/traverse.c>
10 #include <ccan/tdb/transaction.c>
11 #include <ccan/tdb/error.c>
12 #include <ccan/tdb/open.c>
13 #include <ccan/tdb/check.c>
14 #include <ccan/tap/tap.h>
15 #include <stdlib.h>
16 #include <err.h>
17 #include "logging.h"
18
19 int main(int argc, char *argv[])
20 {
21         struct tdb_context *tdb;
22         TDB_DATA key, data;
23
24         plan_tests(11);
25         tdb = tdb_open_ex("run-readonly-check.tdb", 1024,
26                           TDB_DEFAULT,
27                           O_CREAT|O_TRUNC|O_RDWR, 0600, &taplogctx, NULL);
28
29         ok1(tdb);
30         key.dsize = strlen("hi");
31         key.dptr = (void *)"hi";
32         data.dsize = strlen("world");
33         data.dptr = (void *)"world";
34
35         ok1(tdb_store(tdb, key, data, TDB_INSERT) == 0);
36         ok1(tdb_check(tdb, NULL, NULL) == 0);
37
38         /* We are also allowed to do a check inside a transaction. */
39         ok1(tdb_transaction_start(tdb) == 0);
40         ok1(tdb_check(tdb, NULL, NULL) == 0);
41         ok1(tdb_close(tdb) == 0);
42
43         tdb = tdb_open_ex("run-readonly-check.tdb", 1024,
44                           TDB_DEFAULT, O_RDONLY, 0, &taplogctx, NULL);
45
46         ok1(tdb);
47         ok1(tdb_store(tdb, key, data, TDB_MODIFY) == -1);
48         ok1(tdb_error(tdb) == TDB_ERR_RDONLY);
49         ok1(tdb_check(tdb, NULL, NULL) == 0);
50         ok1(tdb_close(tdb) == 0);
51
52         return exit_status();
53 }