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