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