]> git.ozlabs.org Git - ccan/blob - ccan/ntdb/test/run-10-simple-store.c
0add1f10fb588534fa3787e8d4337e2b084e13f0
[ccan] / ccan / ntdb / test / run-10-simple-store.c
1 #include <ccan/failtest/failtest_override.h>
2 #include "ntdb-source.h"
3 #include "tap-interface.h"
4 #include <ccan/failtest/failtest.h>
5 #include "logging.h"
6 #include "failtest_helper.h"
7
8 int main(int argc, char *argv[])
9 {
10         unsigned int i;
11         struct ntdb_context *ntdb;
12         int flags[] = { NTDB_INTERNAL, NTDB_DEFAULT, NTDB_NOMMAP,
13                         NTDB_INTERNAL|NTDB_CONVERT, NTDB_CONVERT,
14                         NTDB_NOMMAP|NTDB_CONVERT };
15         NTDB_DATA key = ntdb_mkdata("key", 3);
16         NTDB_DATA data = ntdb_mkdata("data", 4);
17
18         failtest_init(argc, argv);
19         failtest_hook = block_repeat_failures;
20         failtest_exit_check = exit_check_log;
21
22         failtest_suppress = true;
23         plan_tests(sizeof(flags) / sizeof(flags[0]) * 7 + 1);
24         for (i = 0; i < sizeof(flags) / sizeof(flags[0]); i++) {
25                 ntdb = ntdb_open("run-10-simple-store.ntdb",
26                                  flags[i]|MAYBE_NOSYNC,
27                                  O_RDWR|O_CREAT|O_TRUNC, 0600, &tap_log_attr);
28                 if (!ok1(ntdb))
29                         break;
30                 /* Modify should fail. */
31                 failtest_suppress = false;
32                 if (!ok1(ntdb_store(ntdb, key, data, NTDB_MODIFY)
33                          == NTDB_ERR_NOEXIST))
34                         goto fail;
35                 failtest_suppress = true;
36                 ok1(ntdb_check(ntdb, NULL, NULL) == 0);
37                 /* Insert should succeed. */
38                 failtest_suppress = false;
39                 if (!ok1(ntdb_store(ntdb, key, data, NTDB_INSERT) == 0))
40                         goto fail;
41                 failtest_suppress = true;
42                 ok1(ntdb_check(ntdb, NULL, NULL) == 0);
43                 /* Second insert should fail. */
44                 failtest_suppress = false;
45                 if (!ok1(ntdb_store(ntdb, key, data, NTDB_INSERT)
46                          == NTDB_ERR_EXISTS))
47                         goto fail;
48                 failtest_suppress = true;
49                 ok1(ntdb_check(ntdb, NULL, NULL) == 0);
50                 ntdb_close(ntdb);
51         }
52         ok1(tap_log_messages == 0);
53         failtest_exit(exit_status());
54
55 fail:
56         failtest_suppress = true;
57         ntdb_close(ntdb);
58         failtest_exit(exit_status());
59
60         /*
61          * We will never reach this but the compiler complains if we do not
62          * return in this function.
63          */
64         return EFAULT;
65 }