tdb2: unify tdb1_store into tdb_store
[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 "logging.h"
8
9 int main(int argc, char *argv[])
10 {
11         struct tdb_context *tdb;
12         TDB_DATA key, data;
13         union tdb_attribute hsize;
14
15         hsize.base.attr = TDB_ATTRIBUTE_TDB1_HASHSIZE;
16         hsize.base.next = &tap_log_attr;
17         hsize.tdb1_hashsize.hsize = 1024;
18
19         plan_tests(10);
20         tdb = tdb_open("run-readonly-check.tdb1",
21                        TDB_VERSION1,
22                        O_CREAT|O_TRUNC|O_RDWR, 0600, &hsize);
23
24         ok1(tdb);
25         key.dsize = strlen("hi");
26         key.dptr = (void *)"hi";
27         data.dsize = strlen("world");
28         data.dptr = (void *)"world";
29
30         ok1(tdb_store(tdb, key, data, TDB_INSERT) == TDB_SUCCESS);
31         ok1(tdb1_check(tdb, NULL, NULL) == 0);
32
33         /* We are also allowed to do a check inside a transaction. */
34         ok1(tdb1_transaction_start(tdb) == 0);
35         ok1(tdb1_check(tdb, NULL, NULL) == 0);
36         ok1(tdb_close(tdb) == 0);
37
38         tdb = tdb_open("run-readonly-check.tdb1",
39                        TDB_DEFAULT, O_RDONLY, 0, &tap_log_attr);
40
41         ok1(tdb);
42         ok1(tdb_store(tdb, key, data, TDB_MODIFY) == TDB_ERR_RDONLY);
43         ok1(tdb1_check(tdb, NULL, NULL) == 0);
44         ok1(tdb_close(tdb) == 0);
45
46         return exit_status();
47 }