tdb2: unify tdb1_store into tdb_store
[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 = tdb_open("run-check.tdb1", TDB_VERSION1,
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(tdb_store(tdb, key, data, TDB_INSERT) == TDB_SUCCESS);
30         ok1(tdb1_check(tdb, NULL, NULL) == 0);
31         tdb_close(tdb);
32
33         tdb = tdb_open("run-check.tdb1", TDB_VERSION1, O_RDWR, 0, &tap_log_attr);
34         ok1(tdb);
35         ok1(tdb1_check(tdb, NULL, NULL) == 0);
36         tdb_close(tdb);
37
38         tdb = tdb_open("test/tdb1.corrupt", TDB_VERSION1, O_RDWR, 0,
39                         &tap_log_attr);
40         ok1(tdb);
41         ok1(tdb1_check(tdb, NULL, NULL) == -1);
42         ok1(tdb_error(tdb) == TDB_ERR_CORRUPT);
43         tdb_close(tdb);
44
45         /* Big and little endian should work! */
46         tdb = tdb_open("test/old-nohash-le.tdb1", TDB_VERSION1, O_RDWR, 0,
47                        &tap_log_attr);
48         ok1(tdb);
49         ok1(tdb1_check(tdb, NULL, NULL) == 0);
50         tdb_close(tdb);
51
52         tdb = tdb_open("test/old-nohash-be.tdb1", TDB_VERSION1, O_RDWR, 0,
53                         &tap_log_attr);
54         ok1(tdb);
55         ok1(tdb1_check(tdb, NULL, NULL) == 0);
56         tdb_close(tdb);
57
58         return exit_status();
59 }