]> git.ozlabs.org Git - ccan/blob - ccan/tdb2/test/run-10-simple-store.c
tdb2: test: try (almost) all tests with TDB_VERSION1 flag.
[ccan] / ccan / tdb2 / test / run-10-simple-store.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 expand 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                         TDB_INTERNAL|TDB_VERSION1, TDB_VERSION1,
27                         TDB_NOMMAP|TDB_VERSION1,
28                         TDB_INTERNAL|TDB_CONVERT|TDB_VERSION1,
29                         TDB_CONVERT|TDB_VERSION1,
30                         TDB_NOMMAP|TDB_CONVERT|TDB_VERSION1 };
31         struct tdb_data key = tdb_mkdata("key", 3);
32         struct tdb_data data = tdb_mkdata("data", 4);
33
34         failtest_init(argc, argv);
35         failtest_hook = suppress_failure;
36         failtest_exit_check = exit_check_log;
37
38         failtest_suppress = true;
39         plan_tests(sizeof(flags) / sizeof(flags[0]) * 7 + 1);
40         for (i = 0; i < sizeof(flags) / sizeof(flags[0]); i++) {
41                 tdb = tdb_open("run-10-simple-store.tdb", flags[i],
42                                O_RDWR|O_CREAT|O_TRUNC, 0600, &tap_log_attr);
43                 if (!ok1(tdb))
44                         break;
45                 /* Modify should fail. */
46                 failtest_suppress = false;
47                 if (!ok1(tdb_store(tdb, key, data, TDB_MODIFY)
48                          == TDB_ERR_NOEXIST))
49                         goto fail;
50                 failtest_suppress = true;
51                 ok1(tdb_check(tdb, NULL, NULL) == 0);
52                 /* Insert should succeed. */
53                 failtest_suppress = false;
54                 if (!ok1(tdb_store(tdb, key, data, TDB_INSERT) == 0))
55                         goto fail;
56                 failtest_suppress = true;
57                 ok1(tdb_check(tdb, NULL, NULL) == 0);
58                 /* Second insert should fail. */
59                 failtest_suppress = false;
60                 if (!ok1(tdb_store(tdb, key, data, TDB_INSERT)
61                          == TDB_ERR_EXISTS))
62                         goto fail;
63                 failtest_suppress = true;
64                 ok1(tdb_check(tdb, NULL, NULL) == 0);
65                 tdb_close(tdb);
66         }
67         ok1(tap_log_messages == 0);
68         failtest_exit(exit_status());
69
70 fail:
71         failtest_suppress = true;
72         tdb_close(tdb);
73         failtest_exit(exit_status());
74 }