wintest: add option to select the dns backend
[Samba/gebeck_regimport.git] / lib / ntdb / test / api-83-openhook.c
blob666b4b8d8fa9e7fb8f824761b42fd7ed6c946ae7
1 #include "config.h"
2 #include "ntdb.h"
3 #include "tap-interface.h"
4 #include <sys/types.h>
5 #include <sys/stat.h>
6 #include <fcntl.h>
7 #include <stdlib.h>
8 #include <stdbool.h>
9 #include <stdarg.h>
10 #include <unistd.h>
11 #include "external-agent.h"
12 #include "logging.h"
14 #define KEY_STR "key"
16 static enum NTDB_ERROR clear_if_first(int fd, void *arg)
18 /* We hold a lock offset 4 always, so we can tell if anyone is holding it.
19 * (This is compatible with tdb's TDB_CLEAR_IF_FIRST flag). */
20 struct flock fl;
22 if (arg != clear_if_first)
23 return NTDB_ERR_CORRUPT;
25 fl.l_type = F_WRLCK;
26 fl.l_whence = SEEK_SET;
27 fl.l_start = 4;
28 fl.l_len = 1;
30 if (fcntl(fd, F_SETLK, &fl) == 0) {
31 /* We must be first ones to open it! */
32 diag("truncating file!");
33 if (ftruncate(fd, 0) != 0) {
34 return NTDB_ERR_IO;
37 fl.l_type = F_RDLCK;
38 if (fcntl(fd, F_SETLKW, &fl) != 0) {
39 return NTDB_ERR_IO;
41 return NTDB_SUCCESS;
44 int main(int argc, char *argv[])
46 unsigned int i;
47 struct ntdb_context *ntdb;
48 struct agent *agent;
49 union ntdb_attribute cif;
50 NTDB_DATA key = ntdb_mkdata(KEY_STR, strlen(KEY_STR));
51 int flags[] = { NTDB_DEFAULT, NTDB_NOMMAP,
52 NTDB_CONVERT, NTDB_NOMMAP|NTDB_CONVERT };
54 cif.openhook.base.attr = NTDB_ATTRIBUTE_OPENHOOK;
55 cif.openhook.base.next = &tap_log_attr;
56 cif.openhook.fn = clear_if_first;
57 cif.openhook.data = clear_if_first;
59 agent = prepare_external_agent();
60 plan_tests(sizeof(flags) / sizeof(flags[0]) * 13);
61 for (i = 0; i < sizeof(flags) / sizeof(flags[0]); i++) {
62 /* Create it */
63 ntdb = ntdb_open("run-83-openhook.ntdb", flags[i],
64 O_RDWR|O_CREAT|O_TRUNC, 0600, NULL);
65 ok1(ntdb);
66 ok1(ntdb_store(ntdb, key, key, NTDB_REPLACE) == 0);
67 ntdb_close(ntdb);
69 /* Now, open with CIF, should clear it. */
70 ntdb = ntdb_open("run-83-openhook.ntdb", flags[i],
71 O_RDWR, 0, &cif);
72 ok1(ntdb);
73 ok1(!ntdb_exists(ntdb, key));
74 ok1(ntdb_store(ntdb, key, key, NTDB_REPLACE) == 0);
76 /* Agent should not clear it, since it's still open. */
77 ok1(external_agent_operation(agent, OPEN_WITH_HOOK,
78 "run-83-openhook.ntdb") == SUCCESS);
79 ok1(external_agent_operation(agent, FETCH, KEY_STR "=" KEY_STR)
80 == SUCCESS);
81 ok1(external_agent_operation(agent, CLOSE, "") == SUCCESS);
83 /* Still exists for us too. */
84 ok1(ntdb_exists(ntdb, key));
86 /* Close it, now agent should clear it. */
87 ntdb_close(ntdb);
89 ok1(external_agent_operation(agent, OPEN_WITH_HOOK,
90 "run-83-openhook.ntdb") == SUCCESS);
91 ok1(external_agent_operation(agent, FETCH, KEY_STR "=" KEY_STR)
92 == FAILED);
93 ok1(external_agent_operation(agent, CLOSE, "") == SUCCESS);
95 ok1(tap_log_messages == 0);
98 free_external_agent(agent);
99 return exit_status();