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