tdb2: make tests include a single mega-header to simplify future patches
[ccan] / ccan / tdb2 / test / run-83-openhook.c
1 #include "tdb2-source.h"
2 #include <ccan/tap/tap.h>
3 #include <stdlib.h>
4 #include <stdbool.h>
5 #include <stdarg.h>
6 #include <err.h>
7 #include "external-agent.h"
8 #include "logging.h"
9
10 static enum TDB_ERROR clear_if_first(int fd, void *arg)
11 {
12 /* We hold a lock offset 63 always, so we can tell if anyone is holding it. */
13         struct flock fl;
14
15         if (arg != clear_if_first)
16                 return TDB_ERR_CORRUPT;
17
18         fl.l_type = F_WRLCK;
19         fl.l_whence = SEEK_SET;
20         fl.l_start = 63;
21         fl.l_len = 1;
22
23         if (fcntl(fd, F_SETLK, &fl) == 0) {
24                 /* We must be first ones to open it! */
25                 diag("truncating file!");
26                 if (ftruncate(fd, 0) != 0) {
27                         return TDB_ERR_IO;
28                 }
29         }
30         fl.l_type = F_RDLCK;
31         if (fcntl(fd, F_SETLKW, &fl) != 0) {
32                 return TDB_ERR_IO;
33         }
34         return TDB_SUCCESS;
35 }
36
37 int main(int argc, char *argv[])
38 {
39         unsigned int i;
40         struct tdb_context *tdb;
41         struct agent *agent;
42         union tdb_attribute cif;
43         struct tdb_data key = tdb_mkdata("key", 3);
44         int flags[] = { TDB_DEFAULT, TDB_NOMMAP,
45                         TDB_CONVERT, TDB_NOMMAP|TDB_CONVERT };
46
47         cif.openhook.base.attr = TDB_ATTRIBUTE_OPENHOOK;
48         cif.openhook.base.next = &tap_log_attr;
49         cif.openhook.fn = clear_if_first;
50         cif.openhook.data = clear_if_first;
51
52         agent = prepare_external_agent();
53         plan_tests(sizeof(flags) / sizeof(flags[0]) * 13);
54         for (i = 0; i < sizeof(flags) / sizeof(flags[0]); i++) {
55                 /* Create it */
56                 tdb = tdb_open("run-83-openhook.tdb", flags[i],
57                                O_RDWR|O_CREAT|O_TRUNC, 0600, NULL);
58                 ok1(tdb);
59                 ok1(tdb_store(tdb, key, key, TDB_REPLACE) == 0);
60                 tdb_close(tdb);
61
62                 /* Now, open with CIF, should clear it. */
63                 tdb = tdb_open("run-83-openhook.tdb", flags[i],
64                                O_RDWR, 0, &cif);
65                 ok1(tdb);
66                 ok1(!tdb_exists(tdb, key));
67                 ok1(tdb_store(tdb, key, key, TDB_REPLACE) == 0);
68
69                 /* Agent should not clear it, since it's still open. */
70                 ok1(external_agent_operation(agent, OPEN_WITH_HOOK,
71                                              "run-83-openhook.tdb") == SUCCESS);
72                 ok1(external_agent_operation(agent, FETCH, "key") == SUCCESS);
73                 ok1(external_agent_operation(agent, CLOSE, "") == SUCCESS);
74
75                 /* Still exists for us too. */
76                 ok1(tdb_exists(tdb, key));
77
78                 /* Close it, now agent should clear it. */
79                 tdb_close(tdb);
80
81                 ok1(external_agent_operation(agent, OPEN_WITH_HOOK,
82                                              "run-83-openhook.tdb") == SUCCESS);
83                 ok1(external_agent_operation(agent, FETCH, "key") == FAILED);
84                 ok1(external_agent_operation(agent, CLOSE, "") == SUCCESS);
85
86                 ok1(tap_log_messages == 0);
87         }
88
89         free_external_agent(agent);
90         return exit_status();
91 }
92