tdb2: add TDB_ATTRIBUTE_TDB1_HASHSIZE
[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         union tdb_attribute hsize;
12
13         hsize.base.attr = TDB_ATTRIBUTE_TDB1_HASHSIZE;
14         hsize.base.next = &tap_log_attr;
15         hsize.tdb1_hashsize.hsize = 1;
16
17         plan_tests(13);
18         tdb = tdb1_open("run-check.tdb", TDB_DEFAULT,
19                         O_CREAT|O_TRUNC|O_RDWR, 0600, &hsize);
20
21         ok1(tdb);
22         ok1(tdb1_check(tdb, NULL, NULL) == 0);
23
24         key.dsize = strlen("hi");
25         key.dptr = (void *)"hi";
26         data.dsize = strlen("world");
27         data.dptr = (void *)"world";
28
29         ok1(tdb1_store(tdb, key, data, TDB_INSERT) == 0);
30         ok1(tdb1_check(tdb, NULL, NULL) == 0);
31         tdb1_close(tdb);
32
33         tdb = tdb1_open("run-check.tdb", 0, O_RDWR, 0,
34                         &tap_log_attr);
35         ok1(tdb);
36         ok1(tdb1_check(tdb, NULL, NULL) == 0);
37         tdb1_close(tdb);
38
39         tdb = tdb1_open("test/tdb1.corrupt", 0, O_RDWR, 0,
40                         &tap_log_attr);
41         ok1(tdb);
42         ok1(tdb1_check(tdb, NULL, NULL) == -1);
43         ok1(tdb_error(tdb) == TDB_ERR_CORRUPT);
44         tdb1_close(tdb);
45
46         /* Big and little endian should work! */
47         tdb = tdb1_open("test/old-nohash-le.tdb1", 0, O_RDWR, 0,
48                         &tap_log_attr);
49         ok1(tdb);
50         ok1(tdb1_check(tdb, NULL, NULL) == 0);
51         tdb1_close(tdb);
52
53         tdb = tdb1_open("test/old-nohash-be.tdb1", 0, O_RDWR, 0,
54                         &tap_log_attr);
55         ok1(tdb);
56         ok1(tdb1_check(tdb, NULL, NULL) == 0);
57         tdb1_close(tdb);
58
59         return exit_status();
60 }