tdb2: make TDB1 code use tdb2's TDB_ERROR and tdb_logerr()
[ccan] / ccan / tdb2 / test / run-tdb1-check.c
1 #include "tdb2-source.h"
2 #include <ccan/tap/tap.h>
3 #include <stdlib.h>
4 #include <err.h>
5 #include "tdb1-logging.h"
6
7 int main(int argc, char *argv[])
8 {
9         struct tdb1_context *tdb;
10         TDB1_DATA key, data;
11
12         plan_tests(13);
13         tdb = tdb1_open_ex("run-check.tdb", 1, TDB1_CLEAR_IF_FIRST,
14                           O_CREAT|O_TRUNC|O_RDWR, 0600, &taplogctx, NULL);
15
16         ok1(tdb);
17         ok1(tdb1_check(tdb, NULL, NULL) == 0);
18
19         key.dsize = strlen("hi");
20         key.dptr = (void *)"hi";
21         data.dsize = strlen("world");
22         data.dptr = (void *)"world";
23
24         ok1(tdb1_store(tdb, key, data, TDB1_INSERT) == 0);
25         ok1(tdb1_check(tdb, NULL, NULL) == 0);
26         tdb1_close(tdb);
27
28         tdb = tdb1_open_ex("run-check.tdb", 1024, 0, O_RDWR, 0,
29                           &taplogctx, NULL);
30         ok1(tdb);
31         ok1(tdb1_check(tdb, NULL, NULL) == 0);
32         tdb1_close(tdb);
33
34         tdb = tdb1_open_ex("test/tdb1.corrupt", 1024, 0, O_RDWR, 0,
35                           &taplogctx, NULL);
36         ok1(tdb);
37         ok1(tdb1_check(tdb, NULL, NULL) == -1);
38         ok1(tdb_error(tdb) == TDB_ERR_CORRUPT);
39         tdb1_close(tdb);
40
41         /* Big and little endian should work! */
42         tdb = tdb1_open_ex("test/old-nohash-le.tdb1", 1024, 0, O_RDWR, 0,
43                           &taplogctx, NULL);
44         ok1(tdb);
45         ok1(tdb1_check(tdb, NULL, NULL) == 0);
46         tdb1_close(tdb);
47
48         tdb = tdb1_open_ex("test/old-nohash-be.tdb1", 1024, 0, O_RDWR, 0,
49                           &taplogctx, NULL);
50         ok1(tdb);
51         ok1(tdb1_check(tdb, NULL, NULL) == 0);
52         tdb1_close(tdb);
53
54         return exit_status();
55 }