s3-torture: Add a test for notify upon read&x
[sfrench/samba-autobuild/.git] / source3 / torture / test_notify_online.c
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
5
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.
10
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.
15
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/>.
18 */
19
20 #include "includes.h"
21 #include "torture/proto.h"
22 #include "libcli/security/security.h"
23 #include "lib/util/tevent_ntstatus.h"
24
25 extern char *test_filename;
26
27 struct notify_online_state {
28         struct tevent_context *ev;
29         struct cli_state *cli;
30         uint16_t dnum;
31         const char *fname;
32         uint16_t fnum;
33         bool got_notify;
34 };
35
36 static void notify_online_opened_dir(struct tevent_req *subreq);
37 static void notify_online_notify_callback(struct tevent_req *subreq);
38 static void notify_online_opened_file(struct tevent_req *subreq);
39 static void notify_online_sent_read(struct tevent_req *subreq);
40 static void notify_online_sent_closefile(struct tevent_req *subreq);
41 static void notify_online_waited(struct tevent_req *subreq);
42 static void notify_online_sent_closedir(struct tevent_req *subreq);
43
44 static struct tevent_req *notify_online_send(
45         TALLOC_CTX *mem_ctx, struct tevent_context *ev,
46         struct cli_state *cli, const char *dname, const char *fname)
47 {
48         struct tevent_req *req, *subreq;
49         struct notify_online_state *state;
50
51         req = tevent_req_create(mem_ctx, &state, struct notify_online_state);
52         if (req == NULL) {
53                 return NULL;
54         }
55         state->ev = ev;
56         state->cli = cli;
57         state->fname = fname;
58
59         subreq = cli_ntcreate_send(
60                 state, ev, cli, dname, EXTENDED_RESPONSE_REQUIRED,
61                 SEC_FILE_READ_DATA, 0,
62                 FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
63                 FILE_OPEN, 0, 0);
64         if (tevent_req_nomem(subreq, req)) {
65                 return tevent_req_post(req, ev);
66         }
67         tevent_req_set_callback(subreq, notify_online_opened_dir, req);
68         return req;
69 }
70
71 static void notify_online_opened_dir(struct tevent_req *subreq)
72 {
73         struct tevent_req *req = tevent_req_callback_data(
74                 subreq, struct tevent_req);
75         struct notify_online_state *state = tevent_req_data(
76                 req, struct notify_online_state);
77         NTSTATUS status;
78
79         status = cli_ntcreate_recv(subreq, &state->dnum);
80         TALLOC_FREE(subreq);
81         if (tevent_req_nterror(req, status)) {
82                 return;
83         }
84         subreq = cli_notify_send(state, state->ev, state->cli, state->dnum,
85                                  128, FILE_NOTIFY_CHANGE_ATTRIBUTES, false);
86         if (tevent_req_nomem(subreq, req)) {
87                 return;
88         }
89         tevent_req_set_callback(subreq, notify_online_notify_callback, req);
90
91         subreq = cli_ntcreate_send(
92                 state, state->ev, state->cli, state->fname, 0,
93                 GENERIC_READ_ACCESS, FILE_ATTRIBUTE_NORMAL,
94                 FILE_SHARE_READ|FILE_SHARE_WRITE|FILE_SHARE_DELETE,
95                 FILE_OPEN, FILE_NON_DIRECTORY_FILE, 0);
96         if (tevent_req_nomem(subreq, req)) {
97                 return;
98         }
99         tevent_req_set_callback(subreq, notify_online_opened_file, req);
100 }
101
102 static void notify_online_notify_callback(struct tevent_req *subreq)
103 {
104         struct tevent_req *req = tevent_req_callback_data(
105                 subreq, struct tevent_req);
106         struct notify_online_state *state = tevent_req_data(
107                 req, struct notify_online_state);
108         NTSTATUS status;
109         uint32_t num_changes;
110         struct notify_change *changes;
111
112         status = cli_notify_recv(subreq, state, &num_changes, &changes);
113         TALLOC_FREE(subreq);
114         if (tevent_req_nterror(req, status)) {
115                 return;
116         }
117         if ((num_changes == 1)
118             && (changes[0].action == NOTIFY_ACTION_MODIFIED)
119             && (strcmp(changes[0].name, state->fname) == 0)) {
120                 state->got_notify = true;
121         }
122         tevent_req_done(req);
123 }
124
125 static void notify_online_opened_file(struct tevent_req *subreq)
126 {
127         struct tevent_req *req = tevent_req_callback_data(
128                 subreq, struct tevent_req);
129         struct notify_online_state *state = tevent_req_data(
130                 req, struct notify_online_state);
131         NTSTATUS status;
132
133         status = cli_ntcreate_recv(subreq, &state->fnum);
134         TALLOC_FREE(subreq);
135         if (tevent_req_nterror(req, status)) {
136                 return;
137         }
138         subreq = cli_read_andx_send(
139                 state, state->ev, state->cli, state->fnum, 0, 1);
140         if (tevent_req_nomem(subreq, req)) {
141                 return;
142         }
143         tevent_req_set_callback(subreq, notify_online_sent_read, req);
144 }
145
146 static void notify_online_sent_read(struct tevent_req *subreq)
147 {
148         struct tevent_req *req = tevent_req_callback_data(
149                 subreq, struct tevent_req);
150         struct notify_online_state *state = tevent_req_data(
151                 req, struct notify_online_state);
152         NTSTATUS status;
153         ssize_t received;
154         uint8_t *buf;
155
156         status = cli_read_andx_recv(subreq, &received, &buf);
157         TALLOC_FREE(subreq);
158         if (tevent_req_nterror(req, status)) {
159                 return;
160         }
161         subreq = cli_close_send(
162                 state, state->ev, state->cli, state->fnum);
163         if (tevent_req_nomem(subreq, req)) {
164                 return;
165         }
166         tevent_req_set_callback(subreq, notify_online_sent_closefile, req);
167 }
168
169 static void notify_online_sent_closefile(struct tevent_req *subreq)
170 {
171         struct tevent_req *req = tevent_req_callback_data(
172                 subreq, struct tevent_req);
173         struct notify_online_state *state = tevent_req_data(
174                 req, struct notify_online_state);
175         NTSTATUS status;
176
177         status = cli_close_recv(subreq);
178         TALLOC_FREE(subreq);
179         if (tevent_req_nterror(req, status)) {
180                 return;
181         }
182         subreq = tevent_wakeup_send(
183                 state, state->ev, timeval_current_ofs(10, 0));
184         if (tevent_req_nomem(subreq, req)) {
185                 return;
186         }
187         tevent_req_set_callback(subreq, notify_online_waited, req);
188 }
189
190 static void notify_online_waited(struct tevent_req *subreq)
191 {
192         struct tevent_req *req = tevent_req_callback_data(
193                 subreq, struct tevent_req);
194         struct notify_online_state *state = tevent_req_data(
195                 req, struct notify_online_state);
196
197         tevent_wakeup_recv(subreq);
198         TALLOC_FREE(subreq);
199         subreq = cli_close_send(
200                 state, state->ev, state->cli, state->dnum);
201         if (tevent_req_nomem(subreq, req)) {
202                 return;
203         }
204         tevent_req_set_callback(subreq, notify_online_sent_closedir, req);
205 }
206
207 static void notify_online_sent_closedir(struct tevent_req *subreq)
208 {
209         struct tevent_req *req = tevent_req_callback_data(
210                 subreq, struct tevent_req);
211         NTSTATUS status;
212
213         status = cli_close_recv(subreq);
214         TALLOC_FREE(subreq);
215         if (tevent_req_nterror(req, status)) {
216                 return;
217         }
218 }
219
220 static NTSTATUS notify_online_recv(struct tevent_req *req, bool *got_notify)
221 {
222         struct notify_online_state *state = tevent_req_data(
223                 req, struct notify_online_state);
224         NTSTATUS status;
225
226         if (tevent_req_is_nterror(req, &status)) {
227                 return status;
228         }
229         *got_notify = state->got_notify;
230         return NT_STATUS_OK;
231 }
232
233 static NTSTATUS notify_online(struct cli_state *cli,
234                               const char *dirname, const char *filename,
235                               bool *got_notify)
236 {
237         TALLOC_CTX *frame = talloc_stackframe();
238         struct event_context *ev;
239         struct tevent_req *req;
240         NTSTATUS status = NT_STATUS_NO_MEMORY;
241
242         ev = event_context_init(frame);
243         if (ev == NULL) {
244                 goto fail;
245         }
246         req = notify_online_send(frame, ev, cli, dirname, filename);
247         if (req == NULL) {
248                 goto fail;
249         }
250         if (!tevent_req_poll_ntstatus(req, ev, &status)) {
251                 goto fail;
252         }
253         status = notify_online_recv(req, got_notify);
254  fail:
255         TALLOC_FREE(frame);
256         return status;
257 }
258
259 bool run_notify_online(int dummy)
260 {
261         struct cli_state *cli;
262         NTSTATUS status;
263         char *p;
264         const char *dir;
265         const char *file;
266         bool got_notify = false;
267
268         printf("Starting NOTIFY_ONLINE\n");
269
270         if (test_filename == NULL) {
271                 fprintf(stderr, "<-f filename> missing\n");
272                 return false;
273         }
274
275         if (!torture_open_connection(&cli, 0)) {
276                 return false;
277         }
278
279         p = strrchr(test_filename, '/');
280         if (p != NULL) {
281                 dir = SMB_STRNDUP(test_filename, p-test_filename);
282                 file = SMB_STRDUP(p+1);
283         } else {
284                 dir = "";
285                 file = test_filename;
286         }
287
288         status = notify_online(cli, dir, file, &got_notify);
289         d_printf("notify_online returned %s (%d)\n", nt_errstr(status),
290                  (int)got_notify);
291         torture_close_connection(cli);
292         return NT_STATUS_IS_OK(status) && got_notify;
293 }