7d0d46d3d1e307b96389d615f4af526fb884e4a6
[kai/samba-autobuild/.git] / source4 / torture / raw / notify.c
1 /* 
2    Unix SMB/CIFS implementation.
3    basic raw test suite for change notify
4    Copyright (C) Andrew Tridgell 2003
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/torture.h"
22 #include "libcli/raw/libcliraw.h"
23 #include "libcli/libcli.h"
24 #include "system/filesys.h"
25 #include "torture/util.h"
26
27 #define BASEDIR "\\test_notify"
28
29 #define CHECK_STATUS(status, correct) do { \
30         if (!NT_STATUS_EQUAL(status, correct)) { \
31                 printf("(%d) Incorrect status %s - should be %s\n", \
32                        __LINE__, nt_errstr(status), nt_errstr(correct)); \
33                 ret = False; \
34                 goto done; \
35         }} while (0)
36
37
38 #define CHECK_VAL(v, correct) do { \
39         if ((v) != (correct)) { \
40                 printf("(%d) wrong value for %s  0x%x should be 0x%x\n", \
41                        __LINE__, #v, (int)v, (int)correct); \
42                 ret = False; \
43                 goto done; \
44         }} while (0)
45
46 #define CHECK_WSTR(field, value, flags) do { \
47         if (!field.s || strcmp(field.s, value) || wire_bad_flags(&field, flags, cli->transport)) { \
48                 printf("(%d) %s [%s] != %s\n",  __LINE__, #field, field.s, value); \
49                         ret = False; \
50                 goto done; \
51         }} while (0)
52
53
54 /* 
55    basic testing of change notify on directories
56 */
57 static BOOL test_notify_dir(struct smbcli_state *cli, struct smbcli_state *cli2, 
58                             TALLOC_CTX *mem_ctx)
59 {
60         BOOL ret = True;
61         NTSTATUS status;
62         union smb_notify notify;
63         union smb_open io;
64         union smb_close cl;
65         int i, count, fnum, fnum2;
66         struct smbcli_request *req, *req2;
67         extern int torture_numops;
68
69         printf("TESTING CHANGE NOTIFY ON DIRECTRIES\n");
70                 
71         /*
72           get a handle on the directory
73         */
74         io.generic.level = RAW_OPEN_NTCREATEX;
75         io.ntcreatex.in.root_fid = 0;
76         io.ntcreatex.in.flags = 0;
77         io.ntcreatex.in.access_mask = SEC_FILE_ALL;
78         io.ntcreatex.in.create_options = NTCREATEX_OPTIONS_DIRECTORY;
79         io.ntcreatex.in.file_attr = FILE_ATTRIBUTE_NORMAL;
80         io.ntcreatex.in.share_access = NTCREATEX_SHARE_ACCESS_READ | NTCREATEX_SHARE_ACCESS_WRITE;
81         io.ntcreatex.in.alloc_size = 0;
82         io.ntcreatex.in.open_disposition = NTCREATEX_DISP_OPEN;
83         io.ntcreatex.in.impersonation = NTCREATEX_IMPERSONATION_ANONYMOUS;
84         io.ntcreatex.in.security_flags = 0;
85         io.ntcreatex.in.fname = BASEDIR;
86
87         status = smb_raw_open(cli->tree, mem_ctx, &io);
88         CHECK_STATUS(status, NT_STATUS_OK);
89         fnum = io.ntcreatex.out.file.fnum;
90
91         status = smb_raw_open(cli->tree, mem_ctx, &io);
92         CHECK_STATUS(status, NT_STATUS_OK);
93         fnum2 = io.ntcreatex.out.file.fnum;
94
95         /* ask for a change notify,
96            on file or directory name changes */
97         notify.nttrans.level = RAW_NOTIFY_NTTRANS;
98         notify.nttrans.in.buffer_size = 1000;
99         notify.nttrans.in.completion_filter = FILE_NOTIFY_CHANGE_NAME;
100         notify.nttrans.in.file.fnum = fnum;
101         notify.nttrans.in.recursive = True;
102
103         printf("testing notify cancel\n");
104
105         req = smb_raw_changenotify_send(cli->tree, &notify);
106         smb_raw_ntcancel(req);
107         status = smb_raw_changenotify_recv(req, mem_ctx, &notify);
108         CHECK_STATUS(status, NT_STATUS_CANCELLED);
109
110         printf("testing notify mkdir\n");
111
112         req = smb_raw_changenotify_send(cli->tree, &notify);
113         smbcli_mkdir(cli2->tree, BASEDIR "\\subdir-name");
114
115         status = smb_raw_changenotify_recv(req, mem_ctx, &notify);
116         CHECK_STATUS(status, NT_STATUS_OK);
117
118         CHECK_VAL(notify.nttrans.out.num_changes, 1);
119         CHECK_VAL(notify.nttrans.out.changes[0].action, NOTIFY_ACTION_ADDED);
120         CHECK_WSTR(notify.nttrans.out.changes[0].name, "subdir-name", STR_UNICODE);
121
122         printf("testing notify rmdir\n");
123
124         req = smb_raw_changenotify_send(cli->tree, &notify);
125         smbcli_rmdir(cli2->tree, BASEDIR "\\subdir-name");
126
127         status = smb_raw_changenotify_recv(req, mem_ctx, &notify);
128         CHECK_STATUS(status, NT_STATUS_OK);
129         CHECK_VAL(notify.nttrans.out.num_changes, 1);
130         CHECK_VAL(notify.nttrans.out.changes[0].action, NOTIFY_ACTION_REMOVED);
131         CHECK_WSTR(notify.nttrans.out.changes[0].name, "subdir-name", STR_UNICODE);
132
133         printf("testing notify mkdir - rmdir - mkdir - rmdir\n");
134
135         smbcli_mkdir(cli2->tree, BASEDIR "\\subdir-name");
136         smbcli_rmdir(cli2->tree, BASEDIR "\\subdir-name");
137         smbcli_mkdir(cli2->tree, BASEDIR "\\subdir-name");
138         smbcli_rmdir(cli2->tree, BASEDIR "\\subdir-name");
139         msleep(200);
140         req = smb_raw_changenotify_send(cli->tree, &notify);
141         status = smb_raw_changenotify_recv(req, mem_ctx, &notify);
142         CHECK_STATUS(status, NT_STATUS_OK);
143         CHECK_VAL(notify.nttrans.out.num_changes, 4);
144         CHECK_VAL(notify.nttrans.out.changes[0].action, NOTIFY_ACTION_ADDED);
145         CHECK_WSTR(notify.nttrans.out.changes[0].name, "subdir-name", STR_UNICODE);
146         CHECK_VAL(notify.nttrans.out.changes[1].action, NOTIFY_ACTION_REMOVED);
147         CHECK_WSTR(notify.nttrans.out.changes[1].name, "subdir-name", STR_UNICODE);
148         CHECK_VAL(notify.nttrans.out.changes[2].action, NOTIFY_ACTION_ADDED);
149         CHECK_WSTR(notify.nttrans.out.changes[2].name, "subdir-name", STR_UNICODE);
150         CHECK_VAL(notify.nttrans.out.changes[3].action, NOTIFY_ACTION_REMOVED);
151         CHECK_WSTR(notify.nttrans.out.changes[3].name, "subdir-name", STR_UNICODE);
152
153         count = torture_numops;
154         printf("testing buffered notify on create of %d files\n", count);
155         for (i=0;i<count;i++) {
156                 char *fname = talloc_asprintf(cli, BASEDIR "\\test%d.txt", i);
157                 int fnum3 = smbcli_open(cli->tree, fname, O_CREAT|O_RDWR, DENY_NONE);
158                 if (fnum3 == -1) {
159                         printf("Failed to create %s - %s\n", 
160                                fname, smbcli_errstr(cli->tree));
161                         ret = False;
162                         goto done;
163                 }
164                 talloc_free(fname);
165                 smbcli_close(cli->tree, fnum3);
166         }
167
168         /* (1st notify) setup a new notify on a different directory handle.
169            This new notify won't see the events above. */
170         notify.nttrans.in.file.fnum = fnum2;
171         req2 = smb_raw_changenotify_send(cli->tree, &notify);
172
173         /* (2nd notify) whereas this notify will see the above buffered events,
174            and it directly returns the buffered events */
175         notify.nttrans.in.file.fnum = fnum;
176         req = smb_raw_changenotify_send(cli->tree, &notify);
177
178         status = smbcli_unlink(cli->tree, BASEDIR "\\nonexistant.txt");
179         CHECK_STATUS(status, NT_STATUS_OBJECT_NAME_NOT_FOUND);
180
181         /* (1st unlink) as the 2nd notify directly returns,
182            this unlink is only seen by the 1st notify and 
183            the 3rd notify (later) */
184         printf("testing notify on unlink for the first file\n");
185         status = smbcli_unlink(cli2->tree, BASEDIR "\\test0.txt");
186         CHECK_STATUS(status, NT_STATUS_OK);
187
188         /* receive the reply from the 2nd notify */
189         status = smb_raw_changenotify_recv(req, mem_ctx, &notify);
190         CHECK_STATUS(status, NT_STATUS_OK);
191
192         CHECK_VAL(notify.nttrans.out.num_changes, count);
193         for (i=1;i<count;i++) {
194                 CHECK_VAL(notify.nttrans.out.changes[i].action, NOTIFY_ACTION_ADDED);
195         }
196         CHECK_WSTR(notify.nttrans.out.changes[0].name, "test0.txt", STR_UNICODE);
197
198         printf("and now from the 1st notify\n");
199         status = smb_raw_changenotify_recv(req2, mem_ctx, &notify);
200         CHECK_STATUS(status, NT_STATUS_OK);
201         CHECK_VAL(notify.nttrans.out.num_changes, 1);
202         CHECK_VAL(notify.nttrans.out.changes[0].action, NOTIFY_ACTION_REMOVED);
203         CHECK_WSTR(notify.nttrans.out.changes[0].name, "test0.txt", STR_UNICODE);
204
205         printf("(3rd notify) this notify will only see the 1st unlink\n");
206         req = smb_raw_changenotify_send(cli->tree, &notify);
207
208         status = smbcli_unlink(cli->tree, BASEDIR "\\nonexistant.txt");
209         CHECK_STATUS(status, NT_STATUS_OBJECT_NAME_NOT_FOUND);
210
211         printf("testing notify on wildcard unlink for %d files\n", count-1);
212         /* (2nd unlink) do a wildcard unlink */
213         status = smbcli_unlink(cli2->tree, BASEDIR "\\test*.txt");
214         CHECK_STATUS(status, NT_STATUS_OK);
215
216         /* receive the 3rd notify */
217         status = smb_raw_changenotify_recv(req, mem_ctx, &notify);
218         CHECK_STATUS(status, NT_STATUS_OK);
219         CHECK_VAL(notify.nttrans.out.num_changes, 1);
220         CHECK_VAL(notify.nttrans.out.changes[0].action, NOTIFY_ACTION_REMOVED);
221         CHECK_WSTR(notify.nttrans.out.changes[0].name, "test0.txt", STR_UNICODE);
222
223         /* and we now see the rest of the unlink calls on both directory handles */
224         notify.nttrans.in.file.fnum = fnum;
225         sleep(3);
226         req = smb_raw_changenotify_send(cli->tree, &notify);
227         status = smb_raw_changenotify_recv(req, mem_ctx, &notify);
228         CHECK_STATUS(status, NT_STATUS_OK);
229         CHECK_VAL(notify.nttrans.out.num_changes, count-1);
230         for (i=0;i<notify.nttrans.out.num_changes;i++) {
231                 CHECK_VAL(notify.nttrans.out.changes[i].action, NOTIFY_ACTION_REMOVED);
232         }
233         notify.nttrans.in.file.fnum = fnum2;
234         req = smb_raw_changenotify_send(cli->tree, &notify);
235         status = smb_raw_changenotify_recv(req, mem_ctx, &notify);
236         CHECK_STATUS(status, NT_STATUS_OK);
237         CHECK_VAL(notify.nttrans.out.num_changes, count-1);
238         for (i=0;i<notify.nttrans.out.num_changes;i++) {
239                 CHECK_VAL(notify.nttrans.out.changes[i].action, NOTIFY_ACTION_REMOVED);
240         }
241
242         printf("testing if a close() on the dir handle triggers the notify reply\n");
243
244         notify.nttrans.in.file.fnum = fnum;
245         req = smb_raw_changenotify_send(cli->tree, &notify);
246
247         cl.close.level = RAW_CLOSE_CLOSE;
248         cl.close.in.file.fnum = fnum;
249         cl.close.in.write_time = 0;
250         status = smb_raw_close(cli->tree, &cl);
251         CHECK_STATUS(status, NT_STATUS_OK);
252
253         status = smb_raw_changenotify_recv(req, mem_ctx, &notify);
254         CHECK_STATUS(status, NT_STATUS_OK);
255         CHECK_VAL(notify.nttrans.out.num_changes, 0);
256
257 done:
258         smb_raw_exit(cli->session);
259         return ret;
260 }
261
262 /*
263  * Check notify reply for a rename action. Not sure if this is a valid thing
264  * to do, but depending on timing between inotify and messaging we get the
265  * add/remove/modify in any order. This routines tries to find the action/name
266  * pair in any of the three following notify_changes.
267  */
268
269 static BOOL check_rename_reply(struct smbcli_state *cli,
270                                int line,
271                                struct notify_changes *actions,
272                                uint32_t action, const char *name)
273 {
274         int i;
275
276         for (i=0; i<3; i++) {
277                 if (actions[i].action == action) {
278                         if ((actions[i].name.s == NULL)
279                             || (strcmp(actions[i].name.s, name) != 0)
280                             || (wire_bad_flags(&actions[i].name, STR_UNICODE,
281                                                cli->transport))) {
282                                 printf("(%d) name [%s] != %s\n", line,
283                                        actions[i].name.s, name);
284                                 return False;
285                         }
286                         return True;
287                 }
288         }
289
290         printf("(%d) expected action %d, not found\n", line, action);
291         return False;
292 }
293
294 /* 
295    testing of recursive change notify
296 */
297 static BOOL test_notify_recursive(struct smbcli_state *cli, TALLOC_CTX *mem_ctx)
298 {
299         BOOL ret = True;
300         NTSTATUS status;
301         union smb_notify notify;
302         union smb_open io;
303         int fnum;
304         struct smbcli_request *req1, *req2;
305
306         printf("TESTING CHANGE NOTIFY WITH RECURSION\n");
307                 
308         /*
309           get a handle on the directory
310         */
311         io.generic.level = RAW_OPEN_NTCREATEX;
312         io.ntcreatex.in.root_fid = 0;
313         io.ntcreatex.in.flags = 0;
314         io.ntcreatex.in.access_mask = SEC_FILE_ALL;
315         io.ntcreatex.in.create_options = NTCREATEX_OPTIONS_DIRECTORY;
316         io.ntcreatex.in.file_attr = FILE_ATTRIBUTE_NORMAL;
317         io.ntcreatex.in.share_access = NTCREATEX_SHARE_ACCESS_READ | NTCREATEX_SHARE_ACCESS_WRITE;
318         io.ntcreatex.in.alloc_size = 0;
319         io.ntcreatex.in.open_disposition = NTCREATEX_DISP_OPEN;
320         io.ntcreatex.in.impersonation = NTCREATEX_IMPERSONATION_ANONYMOUS;
321         io.ntcreatex.in.security_flags = 0;
322         io.ntcreatex.in.fname = BASEDIR;
323
324         status = smb_raw_open(cli->tree, mem_ctx, &io);
325         CHECK_STATUS(status, NT_STATUS_OK);
326         fnum = io.ntcreatex.out.file.fnum;
327
328         /* ask for a change notify, on file or directory name
329            changes. Setup both with and without recursion */
330         notify.nttrans.level = RAW_NOTIFY_NTTRANS;
331         notify.nttrans.in.buffer_size = 1000;
332         notify.nttrans.in.completion_filter = FILE_NOTIFY_CHANGE_NAME | FILE_NOTIFY_CHANGE_ATTRIBUTES | FILE_NOTIFY_CHANGE_CREATION;
333         notify.nttrans.in.file.fnum = fnum;
334
335         notify.nttrans.in.recursive = True;
336         req1 = smb_raw_changenotify_send(cli->tree, &notify);
337
338         notify.nttrans.in.recursive = False;
339         req2 = smb_raw_changenotify_send(cli->tree, &notify);
340
341         /* cancel initial requests so the buffer is setup */
342         smb_raw_ntcancel(req1);
343         status = smb_raw_changenotify_recv(req1, mem_ctx, &notify);
344         CHECK_STATUS(status, NT_STATUS_CANCELLED);
345
346         smb_raw_ntcancel(req2);
347         status = smb_raw_changenotify_recv(req2, mem_ctx, &notify);
348         CHECK_STATUS(status, NT_STATUS_CANCELLED);
349
350         smbcli_mkdir(cli->tree, BASEDIR "\\subdir-name");
351         smbcli_mkdir(cli->tree, BASEDIR "\\subdir-name\\subname1");
352         smbcli_close(cli->tree, 
353                      smbcli_open(cli->tree, BASEDIR "\\subdir-name\\subname2", O_CREAT, 0));
354         smbcli_rename(cli->tree, BASEDIR "\\subdir-name\\subname1", BASEDIR "\\subdir-name\\subname1-r");
355         smbcli_rename(cli->tree, BASEDIR "\\subdir-name\\subname2", BASEDIR "\\subname2-r");
356         smbcli_rename(cli->tree, BASEDIR "\\subname2-r", BASEDIR "\\subname3-r");
357
358         notify.nttrans.in.completion_filter = 0;
359         notify.nttrans.in.recursive = True;
360         msleep(200);
361         req1 = smb_raw_changenotify_send(cli->tree, &notify);
362
363         smbcli_rmdir(cli->tree, BASEDIR "\\subdir-name\\subname1-r");
364         smbcli_rmdir(cli->tree, BASEDIR "\\subdir-name");
365         smbcli_unlink(cli->tree, BASEDIR "\\subname3-r");
366
367         notify.nttrans.in.recursive = False;
368         req2 = smb_raw_changenotify_send(cli->tree, &notify);
369
370         status = smb_raw_changenotify_recv(req1, mem_ctx, &notify);
371         CHECK_STATUS(status, NT_STATUS_OK);
372
373         CHECK_VAL(notify.nttrans.out.num_changes, 11);
374         CHECK_VAL(notify.nttrans.out.changes[0].action, NOTIFY_ACTION_ADDED);
375         CHECK_WSTR(notify.nttrans.out.changes[0].name, "subdir-name", STR_UNICODE);
376         CHECK_VAL(notify.nttrans.out.changes[1].action, NOTIFY_ACTION_ADDED);
377         CHECK_WSTR(notify.nttrans.out.changes[1].name, "subdir-name\\subname1", STR_UNICODE);
378         CHECK_VAL(notify.nttrans.out.changes[2].action, NOTIFY_ACTION_ADDED);
379         CHECK_WSTR(notify.nttrans.out.changes[2].name, "subdir-name\\subname2", STR_UNICODE);
380         CHECK_VAL(notify.nttrans.out.changes[3].action, NOTIFY_ACTION_OLD_NAME);
381         CHECK_WSTR(notify.nttrans.out.changes[3].name, "subdir-name\\subname1", STR_UNICODE);
382         CHECK_VAL(notify.nttrans.out.changes[4].action, NOTIFY_ACTION_NEW_NAME);
383         CHECK_WSTR(notify.nttrans.out.changes[4].name, "subdir-name\\subname1-r", STR_UNICODE);
384
385         ret &= check_rename_reply(
386                 cli, __LINE__, &notify.nttrans.out.changes[5],
387                 NOTIFY_ACTION_ADDED, "subname2-r");
388         ret &= check_rename_reply(
389                 cli, __LINE__, &notify.nttrans.out.changes[5],
390                 NOTIFY_ACTION_REMOVED, "subdir-name\\subname2");
391         ret &= check_rename_reply(
392                 cli, __LINE__, &notify.nttrans.out.changes[5],
393                 NOTIFY_ACTION_MODIFIED, "subname2-r");
394                 
395         ret &= check_rename_reply(
396                 cli, __LINE__, &notify.nttrans.out.changes[8],
397                 NOTIFY_ACTION_OLD_NAME, "subname2-r");
398         ret &= check_rename_reply(
399                 cli, __LINE__, &notify.nttrans.out.changes[8],
400                 NOTIFY_ACTION_NEW_NAME, "subname3-r");
401         ret &= check_rename_reply(
402                 cli, __LINE__, &notify.nttrans.out.changes[8],
403                 NOTIFY_ACTION_MODIFIED, "subname3-r");
404
405         if (!ret) {
406                 goto done;
407         }
408
409         status = smb_raw_changenotify_recv(req2, mem_ctx, &notify);
410         CHECK_STATUS(status, NT_STATUS_OK);
411
412         CHECK_VAL(notify.nttrans.out.num_changes, 3);
413         CHECK_VAL(notify.nttrans.out.changes[0].action, NOTIFY_ACTION_REMOVED);
414         CHECK_WSTR(notify.nttrans.out.changes[0].name, "subdir-name\\subname1-r", STR_UNICODE);
415         CHECK_VAL(notify.nttrans.out.changes[1].action, NOTIFY_ACTION_REMOVED);
416         CHECK_WSTR(notify.nttrans.out.changes[1].name, "subdir-name", STR_UNICODE);
417         CHECK_VAL(notify.nttrans.out.changes[2].action, NOTIFY_ACTION_REMOVED);
418         CHECK_WSTR(notify.nttrans.out.changes[2].name, "subname3-r", STR_UNICODE);
419
420 done:
421         smb_raw_exit(cli->session);
422         return ret;
423 }
424
425 /* 
426    testing of change notify mask change
427 */
428 static BOOL test_notify_mask_change(struct smbcli_state *cli, TALLOC_CTX *mem_ctx)
429 {
430         BOOL ret = True;
431         NTSTATUS status;
432         union smb_notify notify;
433         union smb_open io;
434         int fnum;
435         struct smbcli_request *req1, *req2;
436         union smb_setfileinfo sfinfo;
437
438         printf("TESTING CHANGE NOTIFY WITH MASK CHANGE\n");
439
440         /*
441           get a handle on the directory
442         */
443         io.generic.level = RAW_OPEN_NTCREATEX;
444         io.ntcreatex.in.root_fid = 0;
445         io.ntcreatex.in.flags = 0;
446         io.ntcreatex.in.access_mask = SEC_FILE_ALL;
447         io.ntcreatex.in.create_options = NTCREATEX_OPTIONS_DIRECTORY;
448         io.ntcreatex.in.file_attr = FILE_ATTRIBUTE_NORMAL;
449         io.ntcreatex.in.share_access = NTCREATEX_SHARE_ACCESS_READ | NTCREATEX_SHARE_ACCESS_WRITE;
450         io.ntcreatex.in.alloc_size = 0;
451         io.ntcreatex.in.open_disposition = NTCREATEX_DISP_OPEN;
452         io.ntcreatex.in.impersonation = NTCREATEX_IMPERSONATION_ANONYMOUS;
453         io.ntcreatex.in.security_flags = 0;
454         io.ntcreatex.in.fname = BASEDIR;
455
456         status = smb_raw_open(cli->tree, mem_ctx, &io);
457         CHECK_STATUS(status, NT_STATUS_OK);
458         fnum = io.ntcreatex.out.file.fnum;
459
460         /* ask for a change notify, on file or directory name
461            changes. Setup both with and without recursion */
462         notify.nttrans.level = RAW_NOTIFY_NTTRANS;
463         notify.nttrans.in.buffer_size = 1000;
464         notify.nttrans.in.completion_filter = FILE_NOTIFY_CHANGE_ATTRIBUTES;
465         notify.nttrans.in.file.fnum = fnum;
466
467         notify.nttrans.in.recursive = True;
468         req1 = smb_raw_changenotify_send(cli->tree, &notify);
469
470         notify.nttrans.in.recursive = False;
471         req2 = smb_raw_changenotify_send(cli->tree, &notify);
472
473         /* cancel initial requests so the buffer is setup */
474         smb_raw_ntcancel(req1);
475         status = smb_raw_changenotify_recv(req1, mem_ctx, &notify);
476         CHECK_STATUS(status, NT_STATUS_CANCELLED);
477
478         smb_raw_ntcancel(req2);
479         status = smb_raw_changenotify_recv(req2, mem_ctx, &notify);
480         CHECK_STATUS(status, NT_STATUS_CANCELLED);
481
482         notify.nttrans.in.recursive = True;
483         req1 = smb_raw_changenotify_send(cli->tree, &notify);
484
485         /* Set to hidden then back again. */
486         smbcli_close(cli->tree, smbcli_open(cli->tree, BASEDIR "\\tname1", O_CREAT, 0));
487         smbcli_setatr(cli->tree, BASEDIR "\\tname1", FILE_ATTRIBUTE_HIDDEN, 0);
488         smbcli_unlink(cli->tree, BASEDIR "\\tname1");
489
490         status = smb_raw_changenotify_recv(req1, mem_ctx, &notify);
491         CHECK_STATUS(status, NT_STATUS_OK);
492
493         CHECK_VAL(notify.nttrans.out.num_changes, 1);
494         CHECK_VAL(notify.nttrans.out.changes[0].action, NOTIFY_ACTION_MODIFIED);
495         CHECK_WSTR(notify.nttrans.out.changes[0].name, "tname1", STR_UNICODE);
496
497         /* Now try and change the mask to include other events.
498          * This should not work - once the mask is set on a directory
499          * fnum it seems to be fixed until the fnum is closed. */
500
501         notify.nttrans.in.completion_filter = FILE_NOTIFY_CHANGE_NAME | FILE_NOTIFY_CHANGE_ATTRIBUTES | FILE_NOTIFY_CHANGE_CREATION;
502         notify.nttrans.in.recursive = True;
503         req1 = smb_raw_changenotify_send(cli->tree, &notify);
504
505         notify.nttrans.in.recursive = False;
506         req2 = smb_raw_changenotify_send(cli->tree, &notify);
507
508         smbcli_mkdir(cli->tree, BASEDIR "\\subdir-name");
509         smbcli_mkdir(cli->tree, BASEDIR "\\subdir-name\\subname1");
510         smbcli_close(cli->tree, 
511                      smbcli_open(cli->tree, BASEDIR "\\subdir-name\\subname2", O_CREAT, 0));
512         smbcli_rename(cli->tree, BASEDIR "\\subdir-name\\subname1", BASEDIR "\\subdir-name\\subname1-r");
513         smbcli_rename(cli->tree, BASEDIR "\\subdir-name\\subname2", BASEDIR "\\subname2-r");
514         smbcli_rename(cli->tree, BASEDIR "\\subname2-r", BASEDIR "\\subname3-r");
515
516         smbcli_rmdir(cli->tree, BASEDIR "\\subdir-name\\subname1-r");
517         smbcli_rmdir(cli->tree, BASEDIR "\\subdir-name");
518         smbcli_unlink(cli->tree, BASEDIR "\\subname3-r");
519
520         status = smb_raw_changenotify_recv(req1, mem_ctx, &notify);
521         CHECK_STATUS(status, NT_STATUS_OK);
522
523         CHECK_VAL(notify.nttrans.out.num_changes, 1);
524         CHECK_VAL(notify.nttrans.out.changes[0].action, NOTIFY_ACTION_MODIFIED);
525         CHECK_WSTR(notify.nttrans.out.changes[0].name, "subname2-r", STR_UNICODE);
526
527         status = smb_raw_changenotify_recv(req2, mem_ctx, &notify);
528         CHECK_STATUS(status, NT_STATUS_OK);
529
530         CHECK_VAL(notify.nttrans.out.num_changes, 1);
531         CHECK_VAL(notify.nttrans.out.changes[0].action, NOTIFY_ACTION_MODIFIED);
532         CHECK_WSTR(notify.nttrans.out.changes[0].name, "subname3-r", STR_UNICODE);
533
534         if (!ret) {
535                 goto done;
536         }
537
538 done:
539         smb_raw_exit(cli->session);
540         return ret;
541 }
542
543
544 /* 
545    testing of mask bits for change notify
546 */
547 static BOOL test_notify_mask(struct smbcli_state *cli, TALLOC_CTX *mem_ctx)
548 {
549         BOOL ret = True;
550         NTSTATUS status;
551         union smb_notify notify;
552         union smb_open io;
553         int fnum, fnum2;
554         uint32_t mask;
555         int i;
556         char c = 1;
557         struct timeval tv;
558         NTTIME t;
559
560         printf("TESTING CHANGE NOTIFY COMPLETION FILTERS\n");
561
562         tv = timeval_current_ofs(1000, 0);
563         t = timeval_to_nttime(&tv);
564                 
565         /*
566           get a handle on the directory
567         */
568         io.generic.level = RAW_OPEN_NTCREATEX;
569         io.ntcreatex.in.root_fid = 0;
570         io.ntcreatex.in.flags = 0;
571         io.ntcreatex.in.access_mask = SEC_FILE_ALL;
572         io.ntcreatex.in.create_options = NTCREATEX_OPTIONS_DIRECTORY;
573         io.ntcreatex.in.file_attr = FILE_ATTRIBUTE_NORMAL;
574         io.ntcreatex.in.share_access = NTCREATEX_SHARE_ACCESS_READ | NTCREATEX_SHARE_ACCESS_WRITE;
575         io.ntcreatex.in.alloc_size = 0;
576         io.ntcreatex.in.open_disposition = NTCREATEX_DISP_OPEN;
577         io.ntcreatex.in.impersonation = NTCREATEX_IMPERSONATION_ANONYMOUS;
578         io.ntcreatex.in.security_flags = 0;
579         io.ntcreatex.in.fname = BASEDIR;
580
581         notify.nttrans.level = RAW_NOTIFY_NTTRANS;
582         notify.nttrans.in.buffer_size = 1000;
583         notify.nttrans.in.recursive = True;
584
585 #define NOTIFY_MASK_TEST(setup, op, cleanup, Action, expected, nchanges) \
586         do { for (mask=i=0;i<32;i++) { \
587                 struct smbcli_request *req; \
588                 status = smb_raw_open(cli->tree, mem_ctx, &io); \
589                 CHECK_STATUS(status, NT_STATUS_OK); \
590                 fnum = io.ntcreatex.out.file.fnum; \
591                 setup \
592                 notify.nttrans.in.file.fnum = fnum;     \
593                 notify.nttrans.in.completion_filter = (1<<i); \
594                 req = smb_raw_changenotify_send(cli->tree, &notify); \
595                 op \
596                 msleep(200); smb_raw_ntcancel(req); \
597                 status = smb_raw_changenotify_recv(req, mem_ctx, &notify); \
598                 cleanup \
599                 smbcli_close(cli->tree, fnum); \
600                 if (NT_STATUS_EQUAL(status, NT_STATUS_CANCELLED)) continue; \
601                 CHECK_STATUS(status, NT_STATUS_OK); \
602                 /* special case to cope with file rename behaviour */ \
603                 if (nchanges == 2 && notify.nttrans.out.num_changes == 1 && \
604                     notify.nttrans.out.changes[0].action == NOTIFY_ACTION_MODIFIED && \
605                     ((expected) & FILE_NOTIFY_CHANGE_ATTRIBUTES) && \
606                     Action == NOTIFY_ACTION_OLD_NAME) { \
607                         printf("(rename file special handling OK)\n"); \
608                 } else if (nchanges != notify.nttrans.out.num_changes) { \
609                         printf("ERROR: nchanges=%d expected=%d action=%d filter=0x%08x\n", \
610                                notify.nttrans.out.num_changes, \
611                                nchanges, \
612                                notify.nttrans.out.changes[0].action, \
613                                notify.nttrans.in.completion_filter); \
614                         ret = False; \
615                 } else if (notify.nttrans.out.changes[0].action != Action) { \
616                         printf("ERROR: nchanges=%d action=%d expectedAction=%d filter=0x%08x\n", \
617                                notify.nttrans.out.num_changes, \
618                                notify.nttrans.out.changes[0].action, \
619                                Action, \
620                                notify.nttrans.in.completion_filter); \
621                         ret = False; \
622                 } else if (strcmp(notify.nttrans.out.changes[0].name.s, "tname1") != 0) { \
623                         printf("ERROR: nchanges=%d action=%d filter=0x%08x name=%s\n", \
624                                notify.nttrans.out.num_changes, \
625                                notify.nttrans.out.changes[0].action, \
626                                notify.nttrans.in.completion_filter, \
627                                notify.nttrans.out.changes[0].name.s);   \
628                         ret = False; \
629                 } \
630                 mask |= (1<<i); \
631         } \
632         if ((expected) != mask) { \
633                 if (((expected) & ~mask) != 0) { \
634                         printf("ERROR: trigger on too few bits. mask=0x%08x expected=0x%08x\n", \
635                                mask, expected); \
636                         ret = False; \
637                 } else { \
638                         printf("WARNING: trigger on too many bits. mask=0x%08x expected=0x%08x\n", \
639                                mask, expected); \
640                 } \
641         } \
642         } while (0)
643
644         printf("testing mkdir\n");
645         NOTIFY_MASK_TEST(;,
646                          smbcli_mkdir(cli->tree, BASEDIR "\\tname1");,
647                          smbcli_rmdir(cli->tree, BASEDIR "\\tname1");,
648                          NOTIFY_ACTION_ADDED,
649                          FILE_NOTIFY_CHANGE_DIR_NAME, 1);
650
651         printf("testing create file\n");
652         NOTIFY_MASK_TEST(;,
653                          smbcli_close(cli->tree, smbcli_open(cli->tree, BASEDIR "\\tname1", O_CREAT, 0));,
654                          smbcli_unlink(cli->tree, BASEDIR "\\tname1");,
655                          NOTIFY_ACTION_ADDED,
656                          FILE_NOTIFY_CHANGE_FILE_NAME, 1);
657
658         printf("testing unlink\n");
659         NOTIFY_MASK_TEST(
660                          smbcli_close(cli->tree, smbcli_open(cli->tree, BASEDIR "\\tname1", O_CREAT, 0));,
661                          smbcli_unlink(cli->tree, BASEDIR "\\tname1");,
662                          ;,
663                          NOTIFY_ACTION_REMOVED,
664                          FILE_NOTIFY_CHANGE_FILE_NAME, 1);
665
666         printf("testing rmdir\n");
667         NOTIFY_MASK_TEST(
668                          smbcli_mkdir(cli->tree, BASEDIR "\\tname1");,
669                          smbcli_rmdir(cli->tree, BASEDIR "\\tname1");,
670                          ;,
671                          NOTIFY_ACTION_REMOVED,
672                          FILE_NOTIFY_CHANGE_DIR_NAME, 1);
673
674         printf("testing rename file\n");
675         NOTIFY_MASK_TEST(
676                          smbcli_close(cli->tree, smbcli_open(cli->tree, BASEDIR "\\tname1", O_CREAT, 0));,
677                          smbcli_rename(cli->tree, BASEDIR "\\tname1", BASEDIR "\\tname2");,
678                          smbcli_unlink(cli->tree, BASEDIR "\\tname2");,
679                          NOTIFY_ACTION_OLD_NAME,
680                          FILE_NOTIFY_CHANGE_FILE_NAME|FILE_NOTIFY_CHANGE_ATTRIBUTES|FILE_NOTIFY_CHANGE_CREATION, 2);
681
682         printf("testing rename dir\n");
683         NOTIFY_MASK_TEST(
684                 smbcli_mkdir(cli->tree, BASEDIR "\\tname1");,
685                 smbcli_rename(cli->tree, BASEDIR "\\tname1", BASEDIR "\\tname2");,
686                 smbcli_rmdir(cli->tree, BASEDIR "\\tname2");,
687                 NOTIFY_ACTION_OLD_NAME,
688                 FILE_NOTIFY_CHANGE_DIR_NAME, 2);
689
690         printf("testing set path attribute\n");
691         NOTIFY_MASK_TEST(
692                 smbcli_close(cli->tree, smbcli_open(cli->tree, BASEDIR "\\tname1", O_CREAT, 0));,
693                 smbcli_setatr(cli->tree, BASEDIR "\\tname1", FILE_ATTRIBUTE_HIDDEN, 0);,
694                 smbcli_unlink(cli->tree, BASEDIR "\\tname1");,
695                 NOTIFY_ACTION_MODIFIED,
696                 FILE_NOTIFY_CHANGE_ATTRIBUTES, 1);
697
698         printf("testing set path write time\n");
699         NOTIFY_MASK_TEST(
700                 smbcli_close(cli->tree, smbcli_open(cli->tree, BASEDIR "\\tname1", O_CREAT, 0));,
701                 smbcli_setatr(cli->tree, BASEDIR "\\tname1", FILE_ATTRIBUTE_NORMAL, 1000);,
702                 smbcli_unlink(cli->tree, BASEDIR "\\tname1");,
703                 NOTIFY_ACTION_MODIFIED,
704                 FILE_NOTIFY_CHANGE_LAST_WRITE, 1);
705
706         printf("testing set file attribute\n");
707         NOTIFY_MASK_TEST(
708                 fnum2 = create_complex_file(cli, mem_ctx, BASEDIR "\\tname1");,
709                 smbcli_fsetatr(cli->tree, fnum2, FILE_ATTRIBUTE_HIDDEN, 0, 0, 0, 0);,
710                 (smbcli_close(cli->tree, fnum2), smbcli_unlink(cli->tree, BASEDIR "\\tname1"));,
711                 NOTIFY_ACTION_MODIFIED,
712                 FILE_NOTIFY_CHANGE_ATTRIBUTES, 1);
713
714         if (lp_parm_bool(-1, "torture", "samba3", False)) {
715                 printf("Samba3 does not yet support create times "
716                        "everywhere\n");
717         }
718         else {
719                 printf("testing set file create time\n");
720                 NOTIFY_MASK_TEST(
721                         fnum2 = create_complex_file(cli, mem_ctx,
722                                                     BASEDIR "\\tname1");,
723                         smbcli_fsetatr(cli->tree, fnum2, 0, t, 0, 0, 0);,
724                         (smbcli_close(cli->tree, fnum2),
725                          smbcli_unlink(cli->tree, BASEDIR "\\tname1"));,
726                         NOTIFY_ACTION_MODIFIED,
727                         FILE_NOTIFY_CHANGE_CREATION, 1);
728         }
729
730         printf("testing set file access time\n");
731         NOTIFY_MASK_TEST(
732                 fnum2 = create_complex_file(cli, mem_ctx, BASEDIR "\\tname1");,
733                 smbcli_fsetatr(cli->tree, fnum2, 0, 0, t, 0, 0);,
734                 (smbcli_close(cli->tree, fnum2), smbcli_unlink(cli->tree, BASEDIR "\\tname1"));,
735                 NOTIFY_ACTION_MODIFIED,
736                 FILE_NOTIFY_CHANGE_LAST_ACCESS, 1);
737
738         printf("testing set file write time\n");
739         NOTIFY_MASK_TEST(
740                 fnum2 = create_complex_file(cli, mem_ctx, BASEDIR "\\tname1");,
741                 smbcli_fsetatr(cli->tree, fnum2, 0, 0, 0, t, 0);,
742                 (smbcli_close(cli->tree, fnum2), smbcli_unlink(cli->tree, BASEDIR "\\tname1"));,
743                 NOTIFY_ACTION_MODIFIED,
744                 FILE_NOTIFY_CHANGE_LAST_WRITE, 1);
745
746         printf("testing set file change time\n");
747         NOTIFY_MASK_TEST(
748                 fnum2 = create_complex_file(cli, mem_ctx, BASEDIR "\\tname1");,
749                 smbcli_fsetatr(cli->tree, fnum2, 0, 0, 0, 0, t);,
750                 (smbcli_close(cli->tree, fnum2), smbcli_unlink(cli->tree, BASEDIR "\\tname1"));,
751                 NOTIFY_ACTION_MODIFIED,
752                 0, 1);
753
754
755         printf("testing write\n");
756         NOTIFY_MASK_TEST(
757                 fnum2 = create_complex_file(cli, mem_ctx, BASEDIR "\\tname1");,
758                 smbcli_write(cli->tree, fnum2, 1, &c, 10000, 1);,
759                 (smbcli_close(cli->tree, fnum2), smbcli_unlink(cli->tree, BASEDIR "\\tname1"));,
760                 NOTIFY_ACTION_MODIFIED,
761                 0, 1);
762
763         printf("testing truncate\n");
764         NOTIFY_MASK_TEST(
765                 fnum2 = create_complex_file(cli, mem_ctx, BASEDIR "\\tname1");,
766                 smbcli_ftruncate(cli->tree, fnum2, 10000);,
767                 (smbcli_close(cli->tree, fnum2), smbcli_unlink(cli->tree, BASEDIR "\\tname1"));,
768                 NOTIFY_ACTION_MODIFIED,
769                 FILE_NOTIFY_CHANGE_SIZE | FILE_NOTIFY_CHANGE_ATTRIBUTES, 1);
770
771 done:
772         smb_raw_exit(cli->session);
773         return ret;
774 }
775
776 /*
777   basic testing of change notify on files
778 */
779 static BOOL test_notify_file(struct smbcli_state *cli, TALLOC_CTX *mem_ctx)
780 {
781         NTSTATUS status;
782         BOOL ret = True;
783         union smb_open io;
784         union smb_close cl;
785         union smb_notify notify;
786         struct smbcli_request *req;
787         int fnum;
788         const char *fname = BASEDIR "\\file.txt";
789
790         printf("TESTING CHANGE NOTIFY ON FILES\n");
791
792         io.generic.level = RAW_OPEN_NTCREATEX;
793         io.ntcreatex.in.root_fid = 0;
794         io.ntcreatex.in.flags = 0;
795         io.ntcreatex.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
796         io.ntcreatex.in.create_options = 0;
797         io.ntcreatex.in.file_attr = FILE_ATTRIBUTE_NORMAL;
798         io.ntcreatex.in.share_access = NTCREATEX_SHARE_ACCESS_READ | NTCREATEX_SHARE_ACCESS_WRITE;
799         io.ntcreatex.in.alloc_size = 0;
800         io.ntcreatex.in.open_disposition = NTCREATEX_DISP_CREATE;
801         io.ntcreatex.in.impersonation = NTCREATEX_IMPERSONATION_ANONYMOUS;
802         io.ntcreatex.in.security_flags = 0;
803         io.ntcreatex.in.fname = fname;
804         status = smb_raw_open(cli->tree, mem_ctx, &io);
805         CHECK_STATUS(status, NT_STATUS_OK);
806         fnum = io.ntcreatex.out.file.fnum;
807
808         /* ask for a change notify,
809            on file or directory name changes */
810         notify.nttrans.level = RAW_NOTIFY_NTTRANS;
811         notify.nttrans.in.file.fnum = fnum;
812         notify.nttrans.in.buffer_size = 1000;
813         notify.nttrans.in.completion_filter = FILE_NOTIFY_CHANGE_STREAM_NAME;
814         notify.nttrans.in.recursive = False;
815
816         printf("testing if notifies on file handles are invalid (should be)\n");
817
818         req = smb_raw_changenotify_send(cli->tree, &notify);
819         status = smb_raw_changenotify_recv(req, mem_ctx, &notify);
820         CHECK_STATUS(status, NT_STATUS_INVALID_PARAMETER);
821
822         cl.close.level = RAW_CLOSE_CLOSE;
823         cl.close.in.file.fnum = fnum;
824         cl.close.in.write_time = 0;
825         status = smb_raw_close(cli->tree, &cl);
826         CHECK_STATUS(status, NT_STATUS_OK);
827
828         status = smbcli_unlink(cli->tree, fname);
829         CHECK_STATUS(status, NT_STATUS_OK);
830
831 done:
832         smb_raw_exit(cli->session);
833         return ret;
834 }
835
836 /*
837   basic testing of change notifies followed by a tdis
838 */
839 static BOOL test_notify_tdis(TALLOC_CTX *mem_ctx)
840 {
841         BOOL ret = True;
842         NTSTATUS status;
843         union smb_notify notify;
844         union smb_open io;
845         int fnum;
846         struct smbcli_request *req;
847         struct smbcli_state *cli = NULL;
848
849         printf("TESTING CHANGE NOTIFY FOLLOWED BY TDIS\n");
850
851         if (!torture_open_connection(&cli, 0)) {
852                 return False;
853         }
854
855         /*
856           get a handle on the directory
857         */
858         io.generic.level = RAW_OPEN_NTCREATEX;
859         io.ntcreatex.in.root_fid = 0;
860         io.ntcreatex.in.flags = 0;
861         io.ntcreatex.in.access_mask = SEC_FILE_ALL;
862         io.ntcreatex.in.create_options = NTCREATEX_OPTIONS_DIRECTORY;
863         io.ntcreatex.in.file_attr = FILE_ATTRIBUTE_NORMAL;
864         io.ntcreatex.in.share_access = NTCREATEX_SHARE_ACCESS_READ | NTCREATEX_SHARE_ACCESS_WRITE;
865         io.ntcreatex.in.alloc_size = 0;
866         io.ntcreatex.in.open_disposition = NTCREATEX_DISP_OPEN;
867         io.ntcreatex.in.impersonation = NTCREATEX_IMPERSONATION_ANONYMOUS;
868         io.ntcreatex.in.security_flags = 0;
869         io.ntcreatex.in.fname = BASEDIR;
870
871         status = smb_raw_open(cli->tree, mem_ctx, &io);
872         CHECK_STATUS(status, NT_STATUS_OK);
873         fnum = io.ntcreatex.out.file.fnum;
874
875         /* ask for a change notify,
876            on file or directory name changes */
877         notify.nttrans.level = RAW_NOTIFY_NTTRANS;
878         notify.nttrans.in.buffer_size = 1000;
879         notify.nttrans.in.completion_filter = FILE_NOTIFY_CHANGE_NAME;
880         notify.nttrans.in.file.fnum = fnum;
881         notify.nttrans.in.recursive = True;
882
883         req = smb_raw_changenotify_send(cli->tree, &notify);
884
885         status = smbcli_tdis(cli);
886         CHECK_STATUS(status, NT_STATUS_OK);
887         cli->tree = NULL;
888
889         status = smb_raw_changenotify_recv(req, mem_ctx, &notify);
890         CHECK_STATUS(status, NT_STATUS_OK);
891         CHECK_VAL(notify.nttrans.out.num_changes, 0);
892
893 done:
894         torture_close_connection(cli);
895         return ret;
896 }
897
898 /*
899   basic testing of change notifies followed by a exit
900 */
901 static BOOL test_notify_exit(TALLOC_CTX *mem_ctx)
902 {
903         BOOL ret = True;
904         NTSTATUS status;
905         union smb_notify notify;
906         union smb_open io;
907         int fnum;
908         struct smbcli_request *req;
909         struct smbcli_state *cli = NULL;
910
911         printf("TESTING CHANGE NOTIFY FOLLOWED BY EXIT\n");
912
913         if (!torture_open_connection(&cli, 0)) {
914                 return False;
915         }
916
917         /*
918           get a handle on the directory
919         */
920         io.generic.level = RAW_OPEN_NTCREATEX;
921         io.ntcreatex.in.root_fid = 0;
922         io.ntcreatex.in.flags = 0;
923         io.ntcreatex.in.access_mask = SEC_FILE_ALL;
924         io.ntcreatex.in.create_options = NTCREATEX_OPTIONS_DIRECTORY;
925         io.ntcreatex.in.file_attr = FILE_ATTRIBUTE_NORMAL;
926         io.ntcreatex.in.share_access = NTCREATEX_SHARE_ACCESS_READ | NTCREATEX_SHARE_ACCESS_WRITE;
927         io.ntcreatex.in.alloc_size = 0;
928         io.ntcreatex.in.open_disposition = NTCREATEX_DISP_OPEN;
929         io.ntcreatex.in.impersonation = NTCREATEX_IMPERSONATION_ANONYMOUS;
930         io.ntcreatex.in.security_flags = 0;
931         io.ntcreatex.in.fname = BASEDIR;
932
933         status = smb_raw_open(cli->tree, mem_ctx, &io);
934         CHECK_STATUS(status, NT_STATUS_OK);
935         fnum = io.ntcreatex.out.file.fnum;
936
937         /* ask for a change notify,
938            on file or directory name changes */
939         notify.nttrans.level = RAW_NOTIFY_NTTRANS;
940         notify.nttrans.in.buffer_size = 1000;
941         notify.nttrans.in.completion_filter = FILE_NOTIFY_CHANGE_NAME;
942         notify.nttrans.in.file.fnum = fnum;
943         notify.nttrans.in.recursive = True;
944
945         req = smb_raw_changenotify_send(cli->tree, &notify);
946
947         status = smb_raw_exit(cli->session);
948         CHECK_STATUS(status, NT_STATUS_OK);
949
950         status = smb_raw_changenotify_recv(req, mem_ctx, &notify);
951         CHECK_STATUS(status, NT_STATUS_OK);
952         CHECK_VAL(notify.nttrans.out.num_changes, 0);
953
954 done:
955         torture_close_connection(cli);
956         return ret;
957 }
958
959 /*
960   basic testing of change notifies followed by a ulogoff
961 */
962 static BOOL test_notify_ulogoff(TALLOC_CTX *mem_ctx)
963 {
964         BOOL ret = True;
965         NTSTATUS status;
966         union smb_notify notify;
967         union smb_open io;
968         int fnum;
969         struct smbcli_request *req;
970         struct smbcli_state *cli = NULL;
971
972         printf("TESTING CHANGE NOTIFY FOLLOWED BY ULOGOFF\n");
973
974         if (!torture_open_connection(&cli, 0)) {
975                 return False;
976         }
977
978         /*
979           get a handle on the directory
980         */
981         io.generic.level = RAW_OPEN_NTCREATEX;
982         io.ntcreatex.in.root_fid = 0;
983         io.ntcreatex.in.flags = 0;
984         io.ntcreatex.in.access_mask = SEC_FILE_ALL;
985         io.ntcreatex.in.create_options = NTCREATEX_OPTIONS_DIRECTORY;
986         io.ntcreatex.in.file_attr = FILE_ATTRIBUTE_NORMAL;
987         io.ntcreatex.in.share_access = NTCREATEX_SHARE_ACCESS_READ | NTCREATEX_SHARE_ACCESS_WRITE;
988         io.ntcreatex.in.alloc_size = 0;
989         io.ntcreatex.in.open_disposition = NTCREATEX_DISP_OPEN;
990         io.ntcreatex.in.impersonation = NTCREATEX_IMPERSONATION_ANONYMOUS;
991         io.ntcreatex.in.security_flags = 0;
992         io.ntcreatex.in.fname = BASEDIR;
993
994         status = smb_raw_open(cli->tree, mem_ctx, &io);
995         CHECK_STATUS(status, NT_STATUS_OK);
996         fnum = io.ntcreatex.out.file.fnum;
997
998         /* ask for a change notify,
999            on file or directory name changes */
1000         notify.nttrans.level = RAW_NOTIFY_NTTRANS;
1001         notify.nttrans.in.buffer_size = 1000;
1002         notify.nttrans.in.completion_filter = FILE_NOTIFY_CHANGE_NAME;
1003         notify.nttrans.in.file.fnum = fnum;
1004         notify.nttrans.in.recursive = True;
1005
1006         req = smb_raw_changenotify_send(cli->tree, &notify);
1007
1008         status = smb_raw_ulogoff(cli->session);
1009         CHECK_STATUS(status, NT_STATUS_OK);
1010
1011         status = smb_raw_changenotify_recv(req, mem_ctx, &notify);
1012         CHECK_STATUS(status, NT_STATUS_OK);
1013         CHECK_VAL(notify.nttrans.out.num_changes, 0);
1014
1015 done:
1016         torture_close_connection(cli);
1017         return ret;
1018 }
1019
1020 static void tcp_dis_handler(struct smbcli_transport *t, void *p)
1021 {
1022         struct smbcli_state *cli = (struct smbcli_state *)p;
1023         smbcli_transport_dead(cli->transport, NT_STATUS_LOCAL_DISCONNECT);
1024         cli->transport = NULL;
1025         cli->tree = NULL;
1026 }
1027 /*
1028   basic testing of change notifies followed by tcp disconnect
1029 */
1030 static BOOL test_notify_tcp_dis(TALLOC_CTX *mem_ctx)
1031 {
1032         BOOL ret = True;
1033         NTSTATUS status;
1034         union smb_notify notify;
1035         union smb_open io;
1036         int fnum;
1037         struct smbcli_request *req;
1038         struct smbcli_state *cli = NULL;
1039
1040         printf("TESTING CHANGE NOTIFY FOLLOWED BY TCP DISCONNECT\n");
1041
1042         if (!torture_open_connection(&cli, 0)) {
1043                 return False;
1044         }
1045
1046         /*
1047           get a handle on the directory
1048         */
1049         io.generic.level = RAW_OPEN_NTCREATEX;
1050         io.ntcreatex.in.root_fid = 0;
1051         io.ntcreatex.in.flags = 0;
1052         io.ntcreatex.in.access_mask = SEC_FILE_ALL;
1053         io.ntcreatex.in.create_options = NTCREATEX_OPTIONS_DIRECTORY;
1054         io.ntcreatex.in.file_attr = FILE_ATTRIBUTE_NORMAL;
1055         io.ntcreatex.in.share_access = NTCREATEX_SHARE_ACCESS_READ | NTCREATEX_SHARE_ACCESS_WRITE;
1056         io.ntcreatex.in.alloc_size = 0;
1057         io.ntcreatex.in.open_disposition = NTCREATEX_DISP_OPEN;
1058         io.ntcreatex.in.impersonation = NTCREATEX_IMPERSONATION_ANONYMOUS;
1059         io.ntcreatex.in.security_flags = 0;
1060         io.ntcreatex.in.fname = BASEDIR;
1061
1062         status = smb_raw_open(cli->tree, mem_ctx, &io);
1063         CHECK_STATUS(status, NT_STATUS_OK);
1064         fnum = io.ntcreatex.out.file.fnum;
1065
1066         /* ask for a change notify,
1067            on file or directory name changes */
1068         notify.nttrans.level = RAW_NOTIFY_NTTRANS;
1069         notify.nttrans.in.buffer_size = 1000;
1070         notify.nttrans.in.completion_filter = FILE_NOTIFY_CHANGE_NAME;
1071         notify.nttrans.in.file.fnum = fnum;
1072         notify.nttrans.in.recursive = True;
1073
1074         req = smb_raw_changenotify_send(cli->tree, &notify);
1075
1076         smbcli_transport_idle_handler(cli->transport, tcp_dis_handler, 250, cli);
1077
1078         status = smb_raw_changenotify_recv(req, mem_ctx, &notify);
1079         CHECK_STATUS(status, NT_STATUS_LOCAL_DISCONNECT);
1080
1081 done:
1082         torture_close_connection(cli);
1083         return ret;
1084 }
1085
1086 /* 
1087    test setting up two change notify requests on one handle
1088 */
1089 static BOOL test_notify_double(struct smbcli_state *cli, TALLOC_CTX *mem_ctx)
1090 {
1091         BOOL ret = True;
1092         NTSTATUS status;
1093         union smb_notify notify;
1094         union smb_open io;
1095         int fnum;
1096         struct smbcli_request *req1, *req2;
1097
1098         printf("TESTING CHANGE NOTIFY TWICE ON ONE DIRECTORY\n");
1099                 
1100         /*
1101           get a handle on the directory
1102         */
1103         io.generic.level = RAW_OPEN_NTCREATEX;
1104         io.ntcreatex.in.root_fid = 0;
1105         io.ntcreatex.in.flags = 0;
1106         io.ntcreatex.in.access_mask = SEC_FILE_ALL;
1107         io.ntcreatex.in.create_options = NTCREATEX_OPTIONS_DIRECTORY;
1108         io.ntcreatex.in.file_attr = FILE_ATTRIBUTE_NORMAL;
1109         io.ntcreatex.in.share_access = NTCREATEX_SHARE_ACCESS_READ | NTCREATEX_SHARE_ACCESS_WRITE;
1110         io.ntcreatex.in.alloc_size = 0;
1111         io.ntcreatex.in.open_disposition = NTCREATEX_DISP_OPEN;
1112         io.ntcreatex.in.impersonation = NTCREATEX_IMPERSONATION_ANONYMOUS;
1113         io.ntcreatex.in.security_flags = 0;
1114         io.ntcreatex.in.fname = BASEDIR;
1115
1116         status = smb_raw_open(cli->tree, mem_ctx, &io);
1117         CHECK_STATUS(status, NT_STATUS_OK);
1118         fnum = io.ntcreatex.out.file.fnum;
1119
1120         /* ask for a change notify,
1121            on file or directory name changes */
1122         notify.nttrans.level = RAW_NOTIFY_NTTRANS;
1123         notify.nttrans.in.buffer_size = 1000;
1124         notify.nttrans.in.completion_filter = FILE_NOTIFY_CHANGE_NAME;
1125         notify.nttrans.in.file.fnum = fnum;
1126         notify.nttrans.in.recursive = True;
1127
1128         req1 = smb_raw_changenotify_send(cli->tree, &notify);
1129         req2 = smb_raw_changenotify_send(cli->tree, &notify);
1130
1131         smbcli_mkdir(cli->tree, BASEDIR "\\subdir-name");
1132
1133         status = smb_raw_changenotify_recv(req1, mem_ctx, &notify);
1134         CHECK_STATUS(status, NT_STATUS_OK);
1135         CHECK_VAL(notify.nttrans.out.num_changes, 1);
1136         CHECK_WSTR(notify.nttrans.out.changes[0].name, "subdir-name", STR_UNICODE);
1137
1138         smbcli_mkdir(cli->tree, BASEDIR "\\subdir-name2");
1139
1140         status = smb_raw_changenotify_recv(req2, mem_ctx, &notify);
1141         CHECK_STATUS(status, NT_STATUS_OK);
1142         CHECK_VAL(notify.nttrans.out.num_changes, 1);
1143         CHECK_WSTR(notify.nttrans.out.changes[0].name, "subdir-name2", STR_UNICODE);
1144
1145 done:
1146         smb_raw_exit(cli->session);
1147         return ret;
1148 }
1149
1150
1151 /* 
1152    test multiple change notifies at different depths and with/without recursion
1153 */
1154 static BOOL test_notify_tree(struct smbcli_state *cli, TALLOC_CTX *mem_ctx)
1155 {
1156         BOOL ret = True;
1157         union smb_notify notify;
1158         union smb_open io;
1159         struct smbcli_request *req;
1160         struct timeval tv;
1161         struct {
1162                 const char *path;
1163                 BOOL recursive;
1164                 uint32_t filter;
1165                 int expected;
1166                 int fnum;
1167                 int counted;
1168         } dirs[] = {
1169                 {BASEDIR "\\abc",               True, FILE_NOTIFY_CHANGE_NAME, 30 },
1170                 {BASEDIR "\\zqy",               True, FILE_NOTIFY_CHANGE_NAME, 8 },
1171                 {BASEDIR "\\atsy",              True, FILE_NOTIFY_CHANGE_NAME, 4 },
1172                 {BASEDIR "\\abc\\foo",          True,  FILE_NOTIFY_CHANGE_NAME, 2 },
1173                 {BASEDIR "\\abc\\blah",         True,  FILE_NOTIFY_CHANGE_NAME, 13 },
1174                 {BASEDIR "\\abc\\blah",         False, FILE_NOTIFY_CHANGE_NAME, 7 },
1175                 {BASEDIR "\\abc\\blah\\a",      True, FILE_NOTIFY_CHANGE_NAME, 2 },
1176                 {BASEDIR "\\abc\\blah\\b",      True, FILE_NOTIFY_CHANGE_NAME, 2 },
1177                 {BASEDIR "\\abc\\blah\\c",      True, FILE_NOTIFY_CHANGE_NAME, 2 },
1178                 {BASEDIR "\\abc\\fooblah",      True, FILE_NOTIFY_CHANGE_NAME, 2 },
1179                 {BASEDIR "\\zqy\\xx",           True, FILE_NOTIFY_CHANGE_NAME, 2 },
1180                 {BASEDIR "\\zqy\\yyy",          True, FILE_NOTIFY_CHANGE_NAME, 2 },
1181                 {BASEDIR "\\zqy\\..",           True, FILE_NOTIFY_CHANGE_NAME, 40 },
1182                 {BASEDIR,                       True, FILE_NOTIFY_CHANGE_NAME, 40 },
1183                 {BASEDIR,                       False,FILE_NOTIFY_CHANGE_NAME, 6 },
1184                 {BASEDIR "\\atsy",              False,FILE_NOTIFY_CHANGE_NAME, 4 },
1185                 {BASEDIR "\\abc",               True, FILE_NOTIFY_CHANGE_NAME, 24 },
1186                 {BASEDIR "\\abc",               False,FILE_NOTIFY_CHANGE_FILE_NAME, 0 },
1187                 {BASEDIR "\\abc",               True, FILE_NOTIFY_CHANGE_FILE_NAME, 0 },
1188                 {BASEDIR "\\abc",               True, FILE_NOTIFY_CHANGE_NAME, 24 },
1189         };
1190         int i;
1191         NTSTATUS status;
1192         BOOL all_done = False;
1193
1194         printf("TESTING CHANGE NOTIFY FOR DIFFERENT DEPTHS\n");
1195
1196         io.generic.level = RAW_OPEN_NTCREATEX;
1197         io.ntcreatex.in.root_fid = 0;
1198         io.ntcreatex.in.flags = 0;
1199         io.ntcreatex.in.access_mask = SEC_FILE_ALL;
1200         io.ntcreatex.in.create_options = NTCREATEX_OPTIONS_DIRECTORY;
1201         io.ntcreatex.in.file_attr = FILE_ATTRIBUTE_NORMAL;
1202         io.ntcreatex.in.share_access = NTCREATEX_SHARE_ACCESS_READ | NTCREATEX_SHARE_ACCESS_WRITE;
1203         io.ntcreatex.in.alloc_size = 0;
1204         io.ntcreatex.in.open_disposition = NTCREATEX_DISP_OPEN_IF;
1205         io.ntcreatex.in.impersonation = NTCREATEX_IMPERSONATION_ANONYMOUS;
1206         io.ntcreatex.in.security_flags = 0;
1207
1208         notify.nttrans.level = RAW_NOTIFY_NTTRANS;
1209         notify.nttrans.in.buffer_size = 20000;
1210
1211         /*
1212           setup the directory tree, and the notify buffer on each directory
1213         */
1214         for (i=0;i<ARRAY_SIZE(dirs);i++) {
1215                 io.ntcreatex.in.fname = dirs[i].path;
1216                 status = smb_raw_open(cli->tree, mem_ctx, &io);
1217                 CHECK_STATUS(status, NT_STATUS_OK);
1218                 dirs[i].fnum = io.ntcreatex.out.file.fnum;
1219
1220                 notify.nttrans.in.completion_filter = dirs[i].filter;
1221                 notify.nttrans.in.file.fnum = dirs[i].fnum;
1222                 notify.nttrans.in.recursive = dirs[i].recursive;
1223                 req = smb_raw_changenotify_send(cli->tree, &notify);
1224                 smb_raw_ntcancel(req);
1225                 status = smb_raw_changenotify_recv(req, mem_ctx, &notify);
1226                 CHECK_STATUS(status, NT_STATUS_CANCELLED);
1227         }
1228
1229         /* trigger 2 events in each dir */
1230         for (i=0;i<ARRAY_SIZE(dirs);i++) {
1231                 char *path = talloc_asprintf(mem_ctx, "%s\\test.dir", dirs[i].path);
1232                 smbcli_mkdir(cli->tree, path);
1233                 smbcli_rmdir(cli->tree, path);
1234                 talloc_free(path);
1235         }
1236
1237         /* give a bit of time for the events to propogate */
1238         tv = timeval_current();
1239
1240         do {
1241                 /* count events that have happened in each dir */
1242                 for (i=0;i<ARRAY_SIZE(dirs);i++) {
1243                         notify.nttrans.in.file.fnum = dirs[i].fnum;
1244                         req = smb_raw_changenotify_send(cli->tree, &notify);
1245                         smb_raw_ntcancel(req);
1246                         notify.nttrans.out.num_changes = 0;
1247                         status = smb_raw_changenotify_recv(req, mem_ctx, &notify);
1248                         dirs[i].counted += notify.nttrans.out.num_changes;
1249                 }
1250                 
1251                 all_done = True;
1252
1253                 for (i=0;i<ARRAY_SIZE(dirs);i++) {
1254                         if (dirs[i].counted != dirs[i].expected) {
1255                                 all_done = False;
1256                         }
1257                 }
1258         } while (!all_done && timeval_elapsed(&tv) < 20);
1259
1260         printf("took %.4f seconds to propogate all events\n", timeval_elapsed(&tv));
1261
1262         for (i=0;i<ARRAY_SIZE(dirs);i++) {
1263                 if (dirs[i].counted != dirs[i].expected) {
1264                         printf("ERROR: i=%d expected %d got %d for '%s'\n",
1265                                i, dirs[i].expected, dirs[i].counted, dirs[i].path);
1266                         ret = False;
1267                 }
1268         }
1269
1270         /*
1271           run from the back, closing and deleting
1272         */
1273         for (i=ARRAY_SIZE(dirs)-1;i>=0;i--) {
1274                 smbcli_close(cli->tree, dirs[i].fnum);
1275                 smbcli_rmdir(cli->tree, dirs[i].path);
1276         }
1277
1278 done:
1279         smb_raw_exit(cli->session);
1280         return ret;
1281 }
1282
1283 /* 
1284    basic testing of change notify
1285 */
1286 bool torture_raw_notify(struct torture_context *torture, 
1287                         struct smbcli_state *cli, 
1288                         struct smbcli_state *cli2)
1289 {
1290         bool ret = true;
1291                 
1292         if (!torture_setup_dir(cli, BASEDIR)) {
1293                 return false;
1294         }
1295
1296         ret &= test_notify_dir(cli, cli2, torture);
1297         ret &= test_notify_mask(cli, torture);
1298         ret &= test_notify_recursive(cli, torture);
1299         ret &= test_notify_mask_change(cli, torture);
1300         ret &= test_notify_file(cli, torture);
1301         ret &= test_notify_tdis(torture);
1302         ret &= test_notify_exit(torture);
1303         ret &= test_notify_ulogoff(torture);
1304         ret &= test_notify_tcp_dis(torture);
1305         ret &= test_notify_double(cli, torture);
1306         ret &= test_notify_tree(cli, torture);
1307
1308         smb_raw_exit(cli->session);
1309         smbcli_deltree(cli->tree, BASEDIR);
1310         return ret;
1311 }