]> git.ozlabs.org Git - ccan/blob - ccan/ntdb/test/api-83-openhook.c
ntdb: fix up tests.
[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 #include "helpapi-external-agent.h"
8
9 #define KEY_STR "key"
10
11 static enum NTDB_ERROR clear_if_first(int fd, void *arg)
12 {
13 /* We hold a lock offset 4 always, so we can tell if anyone is holding it.
14  * (This is compatible with tdb's TDB_CLEAR_IF_FIRST flag).  */
15         struct flock fl;
16
17         if (arg != clear_if_first)
18                 return NTDB_ERR_CORRUPT;
19
20         fl.l_type = F_WRLCK;
21         fl.l_whence = SEEK_SET;
22         fl.l_start = 4;
23         fl.l_len = 1;
24
25         if (fcntl(fd, F_SETLK, &fl) == 0) {
26                 /* We must be first ones to open it! */
27                 diag("truncating file!");
28                 if (ftruncate(fd, 0) != 0) {
29                         return NTDB_ERR_IO;
30                 }
31         }
32         fl.l_type = F_RDLCK;
33         if (fcntl(fd, F_SETLKW, &fl) != 0) {
34                 return NTDB_ERR_IO;
35         }
36         return NTDB_SUCCESS;
37 }
38
39 int main(int argc, char *argv[])
40 {
41         unsigned int i;
42         struct ntdb_context *ntdb, *ntdb2;
43         struct agent *agent;
44         union ntdb_attribute cif;
45         NTDB_DATA key = ntdb_mkdata(KEY_STR, strlen(KEY_STR));
46         int flags[] = { NTDB_DEFAULT, NTDB_NOMMAP,
47                         NTDB_CONVERT, NTDB_NOMMAP|NTDB_CONVERT };
48
49         cif.openhook.base.attr = NTDB_ATTRIBUTE_OPENHOOK;
50         cif.openhook.base.next = &tap_log_attr;
51         cif.openhook.fn = clear_if_first;
52         cif.openhook.data = clear_if_first;
53
54         agent = prepare_external_agent();
55         plan_tests(sizeof(flags) / sizeof(flags[0]) * 16);
56         for (i = 0; i < sizeof(flags) / sizeof(flags[0]); i++) {
57                 /* Create it */
58                 ntdb = ntdb_open("run-83-openhook.ntdb", flags[i]|MAYBE_NOSYNC,
59                                  O_RDWR|O_CREAT|O_TRUNC, 0600, NULL);
60                 ok1(ntdb);
61                 ok1(ntdb_store(ntdb, key, key, NTDB_REPLACE) == 0);
62                 ntdb_close(ntdb);
63
64                 /* Now, open with CIF, should clear it. */
65                 ntdb = ntdb_open("run-83-openhook.ntdb", flags[i]|MAYBE_NOSYNC,
66                                  O_RDWR, 0, &cif);
67                 ok1(ntdb);
68                 ok1(!ntdb_exists(ntdb, key));
69                 ok1(ntdb_store(ntdb, key, key, NTDB_REPLACE) == 0);
70
71                 /* Agent should not clear it, since it's still open. */
72                 ok1(external_agent_operation(agent, OPEN_WITH_HOOK,
73                                              "run-83-openhook.ntdb") == SUCCESS);
74                 ok1(external_agent_operation(agent, FETCH, KEY_STR "=" KEY_STR)
75                     == SUCCESS);
76                 ok1(external_agent_operation(agent, CLOSE, "") == SUCCESS);
77
78                 /* Still exists for us too. */
79                 ok1(ntdb_exists(ntdb, key));
80
81                 /* Nested open should not erase db. */
82                 ntdb2 = ntdb_open("run-83-openhook.ntdb", flags[i]|MAYBE_NOSYNC,
83                                   O_RDWR, 0, &cif);
84                 ok1(ntdb_exists(ntdb2, key));
85                 ok1(ntdb_exists(ntdb, key));
86                 ntdb_close(ntdb2);
87
88                 ok1(ntdb_exists(ntdb, key));
89
90                 /* Close it, now agent should clear it. */
91                 ntdb_close(ntdb);
92
93                 ok1(external_agent_operation(agent, OPEN_WITH_HOOK,
94                                              "run-83-openhook.ntdb") == SUCCESS);
95                 ok1(external_agent_operation(agent, FETCH, KEY_STR "=" KEY_STR)
96                     == FAILED);
97                 ok1(external_agent_operation(agent, CLOSE, "") == SUCCESS);
98
99                 ok1(tap_log_messages == 0);
100         }
101
102         free_external_agent(agent);
103         return exit_status();
104 }