waf: Do not recurse into ntdb
[Samba.git] / lib / ntdb / test / api-83-openhook.c
blobd2930acf552916fab9d1edd6e8a6bd474e229640
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"
8 #define KEY_STR "key"
10 static enum NTDB_ERROR clear_if_first(int fd, void *arg)
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;
16 if (arg != clear_if_first)
17 return NTDB_ERR_CORRUPT;
19 fl.l_type = F_WRLCK;
20 fl.l_whence = SEEK_SET;
21 fl.l_start = 4;
22 fl.l_len = 1;
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;
31 fl.l_type = F_RDLCK;
32 if (fcntl(fd, F_SETLKW, &fl) != 0) {
33 return NTDB_ERR_IO;
35 return NTDB_SUCCESS;
38 int main(int argc, char *argv[])
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 };
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;
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);
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);
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);
77 /* Still exists for us too. */
78 ok1(ntdb_exists(ntdb, key));
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);
87 ok1(ntdb_exists(ntdb, key));
89 /* Close it, now agent should clear it. */
90 ntdb_close(ntdb);
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);
98 ok1(tap_log_messages == 0);
101 free_external_agent(agent);
102 return exit_status();