]> git.ozlabs.org Git - ccan/blob - ccan/ntdb/test/api-83-openhook.c
ntdb: next-generation trivial key-value database
[ccan] / ccan / ntdb / test / api-83-openhook.c
1 #include "config.h"
2 #include "ntdb.h"
3 #include "private.h"
4 #include "tap-interface.h"
5 #include "external-agent.h"
6 #include "logging.h"
7
8 #define KEY_STR "key"
9
10 static enum NTDB_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 tdb's TDB_CLEAR_IF_FIRST flag).  */
14         struct flock fl;
15
16         if (arg != clear_if_first)
17                 return NTDB_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 NTDB_ERR_IO;
29                 }
30         }
31         fl.l_type = F_RDLCK;
32         if (fcntl(fd, F_SETLKW, &fl) != 0) {
33                 return NTDB_ERR_IO;
34         }
35         return NTDB_SUCCESS;
36 }
37
38 int main(int argc, char *argv[])
39 {
40         unsigned int i;
41         struct ntdb_context *ntdb, *ntdb2;
42         struct agent *agent;
43         union ntdb_attribute cif;
44         NTDB_DATA key = ntdb_mkdata(KEY_STR, strlen(KEY_STR));
45         int flags[] = { NTDB_DEFAULT, NTDB_NOMMAP,
46                         NTDB_CONVERT, NTDB_NOMMAP|NTDB_CONVERT };
47
48         cif.openhook.base.attr = NTDB_ATTRIBUTE_OPENHOOK;
49         cif.openhook.base.next = &tap_log_attr;
50         cif.openhook.fn = clear_if_first;
51         cif.openhook.data = clear_if_first;
52
53         agent = prepare_external_agent();
54         plan_tests(sizeof(flags) / sizeof(flags[0]) * 16);
55         for (i = 0; i < sizeof(flags) / sizeof(flags[0]); i++) {
56                 /* Create it */
57                 ntdb = ntdb_open("run-83-openhook.ntdb", flags[i]|MAYBE_NOSYNC,
58                                  O_RDWR|O_CREAT|O_TRUNC, 0600, NULL);
59                 ok1(ntdb);
60                 ok1(ntdb_store(ntdb, key, key, NTDB_REPLACE) == 0);
61                 ntdb_close(ntdb);
62
63                 /* Now, open with CIF, should clear it. */
64                 ntdb = ntdb_open("run-83-openhook.ntdb", flags[i]|MAYBE_NOSYNC,
65                                  O_RDWR, 0, &cif);
66                 ok1(ntdb);
67                 ok1(!ntdb_exists(ntdb, key));
68                 ok1(ntdb_store(ntdb, key, key, NTDB_REPLACE) == 0);
69
70                 /* Agent should not clear it, since it's still open. */
71                 ok1(external_agent_operation(agent, OPEN_WITH_HOOK,
72                                              "run-83-openhook.ntdb") == SUCCESS);
73                 ok1(external_agent_operation(agent, FETCH, KEY_STR "=" KEY_STR)
74                     == SUCCESS);
75                 ok1(external_agent_operation(agent, CLOSE, "") == SUCCESS);
76
77                 /* Still exists for us too. */
78                 ok1(ntdb_exists(ntdb, key));
79
80                 /* Nested open should not erase db. */
81                 ntdb2 = ntdb_open("run-83-openhook.ntdb", flags[i]|MAYBE_NOSYNC,
82                                   O_RDWR, 0, &cif);
83                 ok1(ntdb_exists(ntdb2, key));
84                 ok1(ntdb_exists(ntdb, key));
85                 ntdb_close(ntdb2);
86
87                 ok1(ntdb_exists(ntdb, key));
88
89                 /* Close it, now agent should clear it. */
90                 ntdb_close(ntdb);
91
92                 ok1(external_agent_operation(agent, OPEN_WITH_HOOK,
93                                              "run-83-openhook.ntdb") == SUCCESS);
94                 ok1(external_agent_operation(agent, FETCH, KEY_STR "=" KEY_STR)
95                     == FAILED);
96                 ok1(external_agent_operation(agent, CLOSE, "") == SUCCESS);
97
98                 ok1(tap_log_messages == 0);
99         }
100
101         free_external_agent(agent);
102         return exit_status();
103 }