torture-backupkey: Assert dcerpc_bkrp_BackupKey_r call was successful
[Samba.git] / source3 / torture / test_notify_online.c
blob7f4f52111cdfa2da932a068c132e42a4280c3878
1 /*
2 Unix SMB/CIFS implementation.
3 Make sure that for offline files pread and pwrite trigger a notify
4 Copyright (C) Volker Lendecke 2011
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program. If not, see <http://www.gnu.org/licenses/>.
20 #include "includes.h"
21 #include "torture/proto.h"
22 #include "libcli/security/security.h"
23 #include "lib/util/tevent_ntstatus.h"
24 #include "libsmb/libsmb.h"
26 extern char *test_filename;
28 struct notify_online_state {
29 struct tevent_context *ev;
30 struct cli_state *cli;
31 uint16_t dnum;
32 const char *fname;
33 uint16_t fnum;
34 bool got_notify;
37 static void notify_online_opened_dir(struct tevent_req *subreq);
38 static void notify_online_notify_callback(struct tevent_req *subreq);
39 static void notify_online_opened_file(struct tevent_req *subreq);
40 static void notify_online_sent_read(struct tevent_req *subreq);
41 static void notify_online_sent_closefile(struct tevent_req *subreq);
42 static void notify_online_waited(struct tevent_req *subreq);
43 static void notify_online_sent_closedir(struct tevent_req *subreq);
45 static struct tevent_req *notify_online_send(
46 TALLOC_CTX *mem_ctx, struct tevent_context *ev,
47 struct cli_state *cli, const char *dname, const char *fname)
49 struct tevent_req *req, *subreq;
50 struct notify_online_state *state;
52 req = tevent_req_create(mem_ctx, &state, struct notify_online_state);
53 if (req == NULL) {
54 return NULL;
56 state->ev = ev;
57 state->cli = cli;
58 state->fname = fname;
60 subreq = cli_ntcreate_send(
61 state, ev, cli, dname, EXTENDED_RESPONSE_REQUIRED,
62 SEC_FILE_READ_DATA, 0,
63 FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
64 FILE_OPEN, 0, 0);
65 if (tevent_req_nomem(subreq, req)) {
66 return tevent_req_post(req, ev);
68 tevent_req_set_callback(subreq, notify_online_opened_dir, req);
69 return req;
72 static void notify_online_opened_dir(struct tevent_req *subreq)
74 struct tevent_req *req = tevent_req_callback_data(
75 subreq, struct tevent_req);
76 struct notify_online_state *state = tevent_req_data(
77 req, struct notify_online_state);
78 NTSTATUS status;
80 status = cli_ntcreate_recv(subreq, &state->dnum, NULL);
81 TALLOC_FREE(subreq);
82 if (tevent_req_nterror(req, status)) {
83 return;
85 subreq = cli_notify_send(state, state->ev, state->cli, state->dnum,
86 128, FILE_NOTIFY_CHANGE_ATTRIBUTES, false);
87 if (tevent_req_nomem(subreq, req)) {
88 return;
90 tevent_req_set_callback(subreq, notify_online_notify_callback, req);
92 subreq = cli_ntcreate_send(
93 state, state->ev, state->cli, state->fname, 0,
94 GENERIC_READ_ACCESS, FILE_ATTRIBUTE_NORMAL,
95 FILE_SHARE_READ|FILE_SHARE_WRITE|FILE_SHARE_DELETE,
96 FILE_OPEN, FILE_NON_DIRECTORY_FILE, 0);
97 if (tevent_req_nomem(subreq, req)) {
98 return;
100 tevent_req_set_callback(subreq, notify_online_opened_file, req);
103 static void notify_online_notify_callback(struct tevent_req *subreq)
105 struct tevent_req *req = tevent_req_callback_data(
106 subreq, struct tevent_req);
107 struct notify_online_state *state = tevent_req_data(
108 req, struct notify_online_state);
109 NTSTATUS status;
110 uint32_t num_changes;
111 struct notify_change *changes;
113 status = cli_notify_recv(subreq, state, &num_changes, &changes);
114 TALLOC_FREE(subreq);
115 if (tevent_req_nterror(req, status)) {
116 return;
118 if ((num_changes == 1)
119 && (changes[0].action == NOTIFY_ACTION_MODIFIED)
120 && (strcmp(changes[0].name, state->fname) == 0)) {
121 state->got_notify = true;
123 tevent_req_done(req);
126 static void notify_online_opened_file(struct tevent_req *subreq)
128 struct tevent_req *req = tevent_req_callback_data(
129 subreq, struct tevent_req);
130 struct notify_online_state *state = tevent_req_data(
131 req, struct notify_online_state);
132 NTSTATUS status;
134 status = cli_ntcreate_recv(subreq, &state->fnum, NULL);
135 TALLOC_FREE(subreq);
136 if (tevent_req_nterror(req, status)) {
137 return;
139 subreq = cli_read_andx_send(
140 state, state->ev, state->cli, state->fnum, 0, 1);
141 if (tevent_req_nomem(subreq, req)) {
142 return;
144 tevent_req_set_callback(subreq, notify_online_sent_read, req);
147 static void notify_online_sent_read(struct tevent_req *subreq)
149 struct tevent_req *req = tevent_req_callback_data(
150 subreq, struct tevent_req);
151 struct notify_online_state *state = tevent_req_data(
152 req, struct notify_online_state);
153 NTSTATUS status;
154 ssize_t received;
155 uint8_t *buf;
157 status = cli_read_andx_recv(subreq, &received, &buf);
158 TALLOC_FREE(subreq);
159 if (tevent_req_nterror(req, status)) {
160 return;
162 subreq = cli_close_send(
163 state, state->ev, state->cli, state->fnum);
164 if (tevent_req_nomem(subreq, req)) {
165 return;
167 tevent_req_set_callback(subreq, notify_online_sent_closefile, req);
170 static void notify_online_sent_closefile(struct tevent_req *subreq)
172 struct tevent_req *req = tevent_req_callback_data(
173 subreq, struct tevent_req);
174 struct notify_online_state *state = tevent_req_data(
175 req, struct notify_online_state);
176 NTSTATUS status;
178 status = cli_close_recv(subreq);
179 TALLOC_FREE(subreq);
180 if (tevent_req_nterror(req, status)) {
181 return;
183 subreq = tevent_wakeup_send(
184 state, state->ev, timeval_current_ofs(10, 0));
185 if (tevent_req_nomem(subreq, req)) {
186 return;
188 tevent_req_set_callback(subreq, notify_online_waited, req);
191 static void notify_online_waited(struct tevent_req *subreq)
193 struct tevent_req *req = tevent_req_callback_data(
194 subreq, struct tevent_req);
195 struct notify_online_state *state = tevent_req_data(
196 req, struct notify_online_state);
198 tevent_wakeup_recv(subreq);
199 TALLOC_FREE(subreq);
200 subreq = cli_close_send(
201 state, state->ev, state->cli, state->dnum);
202 if (tevent_req_nomem(subreq, req)) {
203 return;
205 tevent_req_set_callback(subreq, notify_online_sent_closedir, req);
208 static void notify_online_sent_closedir(struct tevent_req *subreq)
210 struct tevent_req *req = tevent_req_callback_data(
211 subreq, struct tevent_req);
212 NTSTATUS status;
214 status = cli_close_recv(subreq);
215 TALLOC_FREE(subreq);
216 if (tevent_req_nterror(req, status)) {
217 return;
221 static NTSTATUS notify_online_recv(struct tevent_req *req, bool *got_notify)
223 struct notify_online_state *state = tevent_req_data(
224 req, struct notify_online_state);
225 NTSTATUS status;
227 if (tevent_req_is_nterror(req, &status)) {
228 return status;
230 *got_notify = state->got_notify;
231 return NT_STATUS_OK;
234 static NTSTATUS notify_online(struct cli_state *cli,
235 const char *dirname, const char *filename,
236 bool *got_notify)
238 TALLOC_CTX *frame = talloc_stackframe();
239 struct tevent_context *ev;
240 struct tevent_req *req;
241 NTSTATUS status = NT_STATUS_NO_MEMORY;
243 ev = samba_tevent_context_init(frame);
244 if (ev == NULL) {
245 goto fail;
247 req = notify_online_send(frame, ev, cli, dirname, filename);
248 if (req == NULL) {
249 goto fail;
251 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
252 goto fail;
254 status = notify_online_recv(req, got_notify);
255 fail:
256 TALLOC_FREE(frame);
257 return status;
260 bool run_notify_online(int dummy)
262 struct cli_state *cli;
263 NTSTATUS status;
264 char *p;
265 const char *dir;
266 const char *file;
267 bool got_notify = false;
269 printf("Starting NOTIFY_ONLINE\n");
271 if (test_filename == NULL) {
272 fprintf(stderr, "<-f filename> missing\n");
273 return false;
276 if (!torture_open_connection(&cli, 0)) {
277 return false;
280 p = strrchr(test_filename, '/');
281 if (p != NULL) {
282 dir = SMB_STRNDUP(test_filename, p-test_filename);
283 file = SMB_STRDUP(p+1);
284 } else {
285 dir = "";
286 file = test_filename;
289 status = notify_online(cli, dir, file, &got_notify);
290 d_printf("notify_online returned %s (%d)\n", nt_errstr(status),
291 (int)got_notify);
292 torture_close_connection(cli);
293 return NT_STATUS_IS_OK(status) && got_notify;