tdb2: make tdb1_open use attributes for logging, hash function.
[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 "logging.h"
6
7 int main(int argc, char *argv[])
8 {
9         struct tdb_context *tdb;
10         TDB_DATA key, data;
11
12         plan_tests(13);
13         tdb = tdb1_open("run-check.tdb", 1, TDB_DEFAULT,
14                         O_CREAT|O_TRUNC|O_RDWR, 0600, &tap_log_attr);
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, TDB_INSERT) == 0);
25         ok1(tdb1_check(tdb, NULL, NULL) == 0);
26         tdb1_close(tdb);
27
28         tdb = tdb1_open("run-check.tdb", 1024, 0, O_RDWR, 0,
29                         &tap_log_attr);
30         ok1(tdb);
31         ok1(tdb1_check(tdb, NULL, NULL) == 0);
32         tdb1_close(tdb);
33
34         tdb = tdb1_open("test/tdb1.corrupt", 1024, 0, O_RDWR, 0,
35                         &tap_log_attr);
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("test/old-nohash-le.tdb1", 1024, 0, O_RDWR, 0,
43                         &tap_log_attr);
44         ok1(tdb);
45         ok1(tdb1_check(tdb, NULL, NULL) == 0);
46         tdb1_close(tdb);
47
48         tdb = tdb1_open("test/old-nohash-be.tdb1", 1024, 0, O_RDWR, 0,
49                         &tap_log_attr);
50         ok1(tdb);
51         ok1(tdb1_check(tdb, NULL, NULL) == 0);
52         tdb1_close(tdb);
53
54         return exit_status();
55 }