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