fe0842d3d0aa62f260e93ee307b21412b4d72eb6
[ccan] / ccan / tdb / test / run-check.c
1 #define _XOPEN_SOURCE 500
2 #include <ccan/tdb/tdb.h>
3 #include <ccan/tdb/io.c>
4 #include <ccan/tdb/tdb.c>
5 #include <ccan/tdb/lock.c>
6 #include <ccan/tdb/freelist.c>
7 #include <ccan/tdb/traverse.c>
8 #include <ccan/tdb/transaction.c>
9 #include <ccan/tdb/error.c>
10 #include <ccan/tdb/open.c>
11 #include <ccan/tdb/check.c>
12 #include <ccan/tap/tap.h>
13 #include <stdlib.h>
14 #include <err.h>
15 #include "logging.h"
16
17 int main(int argc, char *argv[])
18 {
19         struct tdb_context *tdb;
20         TDB_DATA key, data;
21
22         plan_tests(9);
23         tdb = tdb_open_ex("run-check.tdb", 1, TDB_CLEAR_IF_FIRST,
24                           O_CREAT|O_TRUNC|O_RDWR, 0600, &taplogctx, NULL);
25
26         ok1(tdb);
27         ok1(tdb_check(tdb, NULL, NULL) == 0);
28         
29         key.dsize = strlen("hi");
30         key.dptr = (void *)"hi";
31         data.dsize = strlen("world");
32         data.dptr = (void *)"world";
33
34         ok1(tdb_store(tdb, key, data, TDB_INSERT) == 0);
35         ok1(tdb_check(tdb, NULL, NULL) == 0);
36         tdb_close(tdb);
37
38         tdb = tdb_open_ex("run-check.tdb", 1024, 0, O_RDWR, 0,
39                           &taplogctx, NULL);
40         ok1(tdb);
41         ok1(tdb_check(tdb, NULL, NULL) == 0);
42         tdb_close(tdb);
43
44         tdb = tdb_open_ex("test/tdb.corrupt", 1024, 0, O_RDWR, 0,
45                           &taplogctx, NULL);
46         ok1(tdb);
47         ok1(tdb_check(tdb, NULL, NULL) == -1);
48         ok1(tdb_error(tdb) == TDB_ERR_CORRUPT);
49         tdb_close(tdb);
50
51         return exit_status();
52 }