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