tdb2: allow multiple chain locks.
[ccan] / ccan / tdb2 / test / run-11-simple-fetch.c
1 #include <ccan/failtest/failtest_override.h>
2 #include <ccan/tdb2/tdb.c>
3 #include <ccan/tdb2/open.c>
4 #include <ccan/tdb2/free.c>
5 #include <ccan/tdb2/lock.c>
6 #include <ccan/tdb2/io.c>
7 #include <ccan/tdb2/hash.c>
8 #include <ccan/tdb2/transaction.c>
9 #include <ccan/tdb2/check.c>
10 #include <ccan/tap/tap.h>
11 #include <ccan/failtest/failtest.h>
12 #include "logging.h"
13 #include "failtest_helper.h"
14
15 static bool failtest_suppress = false;
16
17 /* Don't need to test everything here, just want fetch testing. */
18 static enum failtest_result
19 suppress_failure(struct failtest_call *history, unsigned num)
20 {
21         if (failtest_suppress)
22                 return FAIL_DONT_FAIL;
23         return block_repeat_failures(history, num);
24 }
25
26 int main(int argc, char *argv[])
27 {
28         unsigned int i;
29         struct tdb_context *tdb;
30         int flags[] = { TDB_INTERNAL, TDB_DEFAULT, TDB_NOMMAP,
31                         TDB_INTERNAL|TDB_CONVERT, TDB_CONVERT, 
32                         TDB_NOMMAP|TDB_CONVERT };
33         struct tdb_data key = tdb_mkdata("key", 3);
34         struct tdb_data data = tdb_mkdata("data", 4);
35
36         failtest_init(argc, argv);
37         failtest_hook = suppress_failure;
38         failtest_exit_check = exit_check_log;
39
40         failtest_suppress = true;
41         plan_tests(sizeof(flags) / sizeof(flags[0]) * 8 + 1);
42         for (i = 0; i < sizeof(flags) / sizeof(flags[0]); i++) {
43                 tdb = tdb_open("run-11-simple-fetch.tdb", flags[i],
44                                O_RDWR|O_CREAT|O_TRUNC, 0600, &tap_log_attr);
45                 ok1(tdb);
46                 if (tdb) {
47                         struct tdb_data d = { NULL, 0 }; /* Bogus GCC warning */
48
49                         /* fetch should fail. */
50                         failtest_suppress = false;
51                         if (!ok1(tdb_fetch(tdb, key, &d) == TDB_ERR_NOEXIST))
52                                 goto fail;
53                         failtest_suppress = true;
54                         ok1(tdb_check(tdb, NULL, NULL) == 0);
55                         /* Insert should succeed. */
56                         ok1(tdb_store(tdb, key, data, TDB_INSERT) == 0);
57                         ok1(tdb_check(tdb, NULL, NULL) == 0);
58                         /* Fetch should now work. */
59                         failtest_suppress = false;
60                         if (!ok1(tdb_fetch(tdb, key, &d) == TDB_SUCCESS))
61                                 goto fail;
62                         failtest_suppress = true;
63                         ok1(tdb_deq(d, data));
64                         free(d.dptr);
65                         ok1(tdb_check(tdb, NULL, NULL) == 0);
66                         tdb_close(tdb);
67                 }
68         }
69         ok1(tap_log_messages == 0);
70         return exit_status();
71
72 fail:
73         failtest_suppress = true;
74         tdb_close(tdb);
75         failtest_exit(exit_status());
76 }