r22670: changed the RAW-NOTIFY test to support clustered testing (two nodes)
[metze/samba/wip.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 2 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, write to the Free Software
18    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 */
20
21 #include "includes.h"
22 #include "torture/torture.h"
23 #include "libcli/raw/libcliraw.h"
24 #include "libcli/libcli.h"
25 #include "system/filesys.h"
26 #include "torture/util.h"
27
28 #define BASEDIR "\\test_notify"
29
30 #define CHECK_STATUS(status, correct) do { \
31         if (!NT_STATUS_EQUAL(status, correct)) { \
32                 printf("(%d) Incorrect status %s - should be %s\n", \
33                        __LINE__, nt_errstr(status), nt_errstr(correct)); \
34                 ret = False; \
35                 goto done; \
36         }} while (0)
37
38
39 #define CHECK_VAL(v, correct) do { \
40         if ((v) != (correct)) { \
41                 printf("(%d) wrong value for %s  0x%x should be 0x%x\n", \
42                        __LINE__, #v, (int)v, (int)correct); \
43                 ret = False; \
44                 goto done; \
45         }} while (0)
46
47 #define CHECK_WSTR(field, value, flags) do { \
48         if (!field.s || strcmp(field.s, value) || wire_bad_flags(&field, flags, cli)) { \
49                 printf("(%d) %s [%s] != %s\n",  __LINE__, #field, field.s, value); \
50                         ret = False; \
51                 goto done; \
52         }} while (0)
53
54
55 /* 
56    basic testing of change notify on directories
57 */
58 static BOOL test_notify_dir(struct smbcli_state *cli, struct smbcli_state *cli2, 
59                             TALLOC_CTX *mem_ctx)
60 {
61         BOOL ret = True;
62         NTSTATUS status;
63         union smb_notify notify;
64         union smb_open io;
65         union smb_close cl;
66         int i, count, fnum, fnum2;
67         struct smbcli_request *req, *req2;
68         extern int torture_numops;
69
70         printf("TESTING CHANGE NOTIFY ON DIRECTRIES\n");
71                 
72         /*
73           get a handle on the directory
74         */
75         io.generic.level = RAW_OPEN_NTCREATEX;
76         io.ntcreatex.in.root_fid = 0;
77         io.ntcreatex.in.flags = 0;
78         io.ntcreatex.in.access_mask = SEC_FILE_ALL;
79         io.ntcreatex.in.create_options = NTCREATEX_OPTIONS_DIRECTORY;
80         io.ntcreatex.in.file_attr = FILE_ATTRIBUTE_NORMAL;
81         io.ntcreatex.in.share_access = NTCREATEX_SHARE_ACCESS_READ | NTCREATEX_SHARE_ACCESS_WRITE;
82         io.ntcreatex.in.alloc_size = 0;
83         io.ntcreatex.in.open_disposition = NTCREATEX_DISP_OPEN;
84         io.ntcreatex.in.impersonation = NTCREATEX_IMPERSONATION_ANONYMOUS;
85         io.ntcreatex.in.security_flags = 0;
86         io.ntcreatex.in.fname = BASEDIR;
87
88         status = smb_raw_open(cli->tree, mem_ctx, &io);
89         CHECK_STATUS(status, NT_STATUS_OK);
90         fnum = io.ntcreatex.out.file.fnum;
91
92         status = smb_raw_open(cli->tree, mem_ctx, &io);
93         CHECK_STATUS(status, NT_STATUS_OK);
94         fnum2 = io.ntcreatex.out.file.fnum;
95
96         /* ask for a change notify,
97            on file or directory name changes */
98         notify.nttrans.level = RAW_NOTIFY_NTTRANS;
99         notify.nttrans.in.buffer_size = 1000;
100         notify.nttrans.in.completion_filter = FILE_NOTIFY_CHANGE_NAME;
101         notify.nttrans.in.file.fnum = fnum;
102         notify.nttrans.in.recursive = True;
103
104         printf("testing notify cancel\n");
105
106         req = smb_raw_changenotify_send(cli->tree, &notify);
107         smb_raw_ntcancel(req);
108         status = smb_raw_changenotify_recv(req, mem_ctx, &notify);
109         CHECK_STATUS(status, NT_STATUS_CANCELLED);
110
111         printf("testing notify mkdir\n");
112
113         req = smb_raw_changenotify_send(cli->tree, &notify);
114         smbcli_mkdir(cli2->tree, BASEDIR "\\subdir-name");
115
116         status = smb_raw_changenotify_recv(req, mem_ctx, &notify);
117         CHECK_STATUS(status, NT_STATUS_OK);
118
119         CHECK_VAL(notify.nttrans.out.num_changes, 1);
120         CHECK_VAL(notify.nttrans.out.changes[0].action, NOTIFY_ACTION_ADDED);
121         CHECK_WSTR(notify.nttrans.out.changes[0].name, "subdir-name", STR_UNICODE);
122
123         printf("testing notify rmdir\n");
124
125         req = smb_raw_changenotify_send(cli->tree, &notify);
126         smbcli_rmdir(cli2->tree, BASEDIR "\\subdir-name");
127
128         status = smb_raw_changenotify_recv(req, mem_ctx, &notify);
129         CHECK_STATUS(status, NT_STATUS_OK);
130         CHECK_VAL(notify.nttrans.out.num_changes, 1);
131         CHECK_VAL(notify.nttrans.out.changes[0].action, NOTIFY_ACTION_REMOVED);
132         CHECK_WSTR(notify.nttrans.out.changes[0].name, "subdir-name", STR_UNICODE);
133
134         printf("testing notify mkdir - rmdir - mkdir - rmdir\n");
135
136         smbcli_mkdir(cli2->tree, BASEDIR "\\subdir-name");
137         smbcli_rmdir(cli2->tree, BASEDIR "\\subdir-name");
138         smbcli_mkdir(cli2->tree, BASEDIR "\\subdir-name");
139         smbcli_rmdir(cli2->tree, BASEDIR "\\subdir-name");
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))) {
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
296 /* 
297    testing of recursive change notify
298 */
299 static BOOL test_notify_recursive(struct smbcli_state *cli, TALLOC_CTX *mem_ctx)
300 {
301         BOOL ret = True;
302         NTSTATUS status;
303         union smb_notify notify;
304         union smb_open io;
305         int fnum;
306         struct smbcli_request *req1, *req2;
307
308         printf("TESTING CHANGE NOTIFY WITH RECURSION\n");
309                 
310         /*
311           get a handle on the directory
312         */
313         io.generic.level = RAW_OPEN_NTCREATEX;
314         io.ntcreatex.in.root_fid = 0;
315         io.ntcreatex.in.flags = 0;
316         io.ntcreatex.in.access_mask = SEC_FILE_ALL;
317         io.ntcreatex.in.create_options = NTCREATEX_OPTIONS_DIRECTORY;
318         io.ntcreatex.in.file_attr = FILE_ATTRIBUTE_NORMAL;
319         io.ntcreatex.in.share_access = NTCREATEX_SHARE_ACCESS_READ | NTCREATEX_SHARE_ACCESS_WRITE;
320         io.ntcreatex.in.alloc_size = 0;
321         io.ntcreatex.in.open_disposition = NTCREATEX_DISP_OPEN;
322         io.ntcreatex.in.impersonation = NTCREATEX_IMPERSONATION_ANONYMOUS;
323         io.ntcreatex.in.security_flags = 0;
324         io.ntcreatex.in.fname = BASEDIR;
325
326         status = smb_raw_open(cli->tree, mem_ctx, &io);
327         CHECK_STATUS(status, NT_STATUS_OK);
328         fnum = io.ntcreatex.out.file.fnum;
329
330         /* ask for a change notify, on file or directory name
331            changes. Setup both with and without recursion */
332         notify.nttrans.level = RAW_NOTIFY_NTTRANS;
333         notify.nttrans.in.buffer_size = 1000;
334         notify.nttrans.in.completion_filter = FILE_NOTIFY_CHANGE_NAME | FILE_NOTIFY_CHANGE_ATTRIBUTES | FILE_NOTIFY_CHANGE_CREATION;
335         notify.nttrans.in.file.fnum = fnum;
336
337         notify.nttrans.in.recursive = True;
338         req1 = smb_raw_changenotify_send(cli->tree, &notify);
339
340         notify.nttrans.in.recursive = False;
341         req2 = smb_raw_changenotify_send(cli->tree, &notify);
342
343         /* cancel initial requests so the buffer is setup */
344         smb_raw_ntcancel(req1);
345         status = smb_raw_changenotify_recv(req1, mem_ctx, &notify);
346         CHECK_STATUS(status, NT_STATUS_CANCELLED);
347
348         smb_raw_ntcancel(req2);
349         status = smb_raw_changenotify_recv(req2, mem_ctx, &notify);
350         CHECK_STATUS(status, NT_STATUS_CANCELLED);
351
352         smbcli_mkdir(cli->tree, BASEDIR "\\subdir-name");
353         smbcli_mkdir(cli->tree, BASEDIR "\\subdir-name\\subname1");
354         smbcli_close(cli->tree, 
355                      smbcli_open(cli->tree, BASEDIR "\\subdir-name\\subname2", O_CREAT, 0));
356         smbcli_rename(cli->tree, BASEDIR "\\subdir-name\\subname1", BASEDIR "\\subdir-name\\subname1-r");
357         smbcli_rename(cli->tree, BASEDIR "\\subdir-name\\subname2", BASEDIR "\\subname2-r");
358         smbcli_rename(cli->tree, BASEDIR "\\subname2-r", BASEDIR "\\subname3-r");
359
360         notify.nttrans.in.completion_filter = 0;
361         notify.nttrans.in.recursive = True;
362         msleep(200);
363         req1 = smb_raw_changenotify_send(cli->tree, &notify);
364
365         smbcli_rmdir(cli->tree, BASEDIR "\\subdir-name\\subname1-r");
366         smbcli_rmdir(cli->tree, BASEDIR "\\subdir-name");
367         smbcli_unlink(cli->tree, BASEDIR "\\subname3-r");
368
369         notify.nttrans.in.recursive = False;
370         req2 = smb_raw_changenotify_send(cli->tree, &notify);
371
372         status = smb_raw_changenotify_recv(req1, mem_ctx, &notify);
373         CHECK_STATUS(status, NT_STATUS_OK);
374
375         CHECK_VAL(notify.nttrans.out.num_changes, 11);
376         CHECK_VAL(notify.nttrans.out.changes[0].action, NOTIFY_ACTION_ADDED);
377         CHECK_WSTR(notify.nttrans.out.changes[0].name, "subdir-name", STR_UNICODE);
378         CHECK_VAL(notify.nttrans.out.changes[1].action, NOTIFY_ACTION_ADDED);
379         CHECK_WSTR(notify.nttrans.out.changes[1].name, "subdir-name\\subname1", STR_UNICODE);
380         CHECK_VAL(notify.nttrans.out.changes[2].action, NOTIFY_ACTION_ADDED);
381         CHECK_WSTR(notify.nttrans.out.changes[2].name, "subdir-name\\subname2", STR_UNICODE);
382         CHECK_VAL(notify.nttrans.out.changes[3].action, NOTIFY_ACTION_OLD_NAME);
383         CHECK_WSTR(notify.nttrans.out.changes[3].name, "subdir-name\\subname1", STR_UNICODE);
384         CHECK_VAL(notify.nttrans.out.changes[4].action, NOTIFY_ACTION_NEW_NAME);
385         CHECK_WSTR(notify.nttrans.out.changes[4].name, "subdir-name\\subname1-r", STR_UNICODE);
386
387         ret &= check_rename_reply(
388                 cli, __LINE__, &notify.nttrans.out.changes[5],
389                 NOTIFY_ACTION_ADDED, "subname2-r");
390         ret &= check_rename_reply(
391                 cli, __LINE__, &notify.nttrans.out.changes[5],
392                 NOTIFY_ACTION_REMOVED, "subdir-name\\subname2");
393         ret &= check_rename_reply(
394                 cli, __LINE__, &notify.nttrans.out.changes[5],
395                 NOTIFY_ACTION_MODIFIED, "subname2-r");
396                 
397         ret &= check_rename_reply(
398                 cli, __LINE__, &notify.nttrans.out.changes[8],
399                 NOTIFY_ACTION_OLD_NAME, "subname2-r");
400         ret &= check_rename_reply(
401                 cli, __LINE__, &notify.nttrans.out.changes[8],
402                 NOTIFY_ACTION_NEW_NAME, "subname3-r");
403         ret &= check_rename_reply(
404                 cli, __LINE__, &notify.nttrans.out.changes[8],
405                 NOTIFY_ACTION_MODIFIED, "subname3-r");
406
407         if (!ret) {
408                 goto done;
409         }
410
411         status = smb_raw_changenotify_recv(req2, mem_ctx, &notify);
412         CHECK_STATUS(status, NT_STATUS_OK);
413
414         CHECK_VAL(notify.nttrans.out.num_changes, 3);
415         CHECK_VAL(notify.nttrans.out.changes[0].action, NOTIFY_ACTION_REMOVED);
416         CHECK_WSTR(notify.nttrans.out.changes[0].name, "subdir-name\\subname1-r", STR_UNICODE);
417         CHECK_VAL(notify.nttrans.out.changes[1].action, NOTIFY_ACTION_REMOVED);
418         CHECK_WSTR(notify.nttrans.out.changes[1].name, "subdir-name", STR_UNICODE);
419         CHECK_VAL(notify.nttrans.out.changes[2].action, NOTIFY_ACTION_REMOVED);
420         CHECK_WSTR(notify.nttrans.out.changes[2].name, "subname3-r", STR_UNICODE);
421
422 done:
423         smb_raw_exit(cli->session);
424         return ret;
425 }
426
427
428 /* 
429    testing of mask bits for change notify
430 */
431 static BOOL test_notify_mask(struct smbcli_state *cli, TALLOC_CTX *mem_ctx)
432 {
433         BOOL ret = True;
434         NTSTATUS status;
435         union smb_notify notify;
436         union smb_open io;
437         int fnum, fnum2;
438         uint32_t mask;
439         int i;
440         char c = 1;
441         struct timeval tv;
442         NTTIME t;
443
444         printf("TESTING CHANGE NOTIFY COMPLETION FILTERS\n");
445
446         tv = timeval_current_ofs(1000, 0);
447         t = timeval_to_nttime(&tv);
448                 
449         /*
450           get a handle on the directory
451         */
452         io.generic.level = RAW_OPEN_NTCREATEX;
453         io.ntcreatex.in.root_fid = 0;
454         io.ntcreatex.in.flags = 0;
455         io.ntcreatex.in.access_mask = SEC_FILE_ALL;
456         io.ntcreatex.in.create_options = NTCREATEX_OPTIONS_DIRECTORY;
457         io.ntcreatex.in.file_attr = FILE_ATTRIBUTE_NORMAL;
458         io.ntcreatex.in.share_access = NTCREATEX_SHARE_ACCESS_READ | NTCREATEX_SHARE_ACCESS_WRITE;
459         io.ntcreatex.in.alloc_size = 0;
460         io.ntcreatex.in.open_disposition = NTCREATEX_DISP_OPEN;
461         io.ntcreatex.in.impersonation = NTCREATEX_IMPERSONATION_ANONYMOUS;
462         io.ntcreatex.in.security_flags = 0;
463         io.ntcreatex.in.fname = BASEDIR;
464
465         notify.nttrans.level = RAW_NOTIFY_NTTRANS;
466         notify.nttrans.in.buffer_size = 1000;
467         notify.nttrans.in.recursive = True;
468
469 #define NOTIFY_MASK_TEST(setup, op, cleanup, Action, expected, nchanges) \
470         do { for (mask=i=0;i<32;i++) { \
471                 struct smbcli_request *req; \
472                 status = smb_raw_open(cli->tree, mem_ctx, &io); \
473                 CHECK_STATUS(status, NT_STATUS_OK); \
474                 fnum = io.ntcreatex.out.file.fnum; \
475                 setup \
476                 notify.nttrans.in.file.fnum = fnum;     \
477                 notify.nttrans.in.completion_filter = (1<<i); \
478                 req = smb_raw_changenotify_send(cli->tree, &notify); \
479                 op \
480                 msleep(200); smb_raw_ntcancel(req); \
481                 status = smb_raw_changenotify_recv(req, mem_ctx, &notify); \
482                 cleanup \
483                 smbcli_close(cli->tree, fnum); \
484                 if (NT_STATUS_EQUAL(status, NT_STATUS_CANCELLED)) continue; \
485                 CHECK_STATUS(status, NT_STATUS_OK); \
486                 /* special case to cope with file rename behaviour */ \
487                 if (nchanges == 2 && notify.nttrans.out.num_changes == 1 && \
488                     notify.nttrans.out.changes[0].action == NOTIFY_ACTION_MODIFIED && \
489                     ((expected) & FILE_NOTIFY_CHANGE_ATTRIBUTES) && \
490                     Action == NOTIFY_ACTION_OLD_NAME) { \
491                         printf("(rename file special handling OK)\n"); \
492                 } else if (nchanges != notify.nttrans.out.num_changes || \
493                     notify.nttrans.out.changes[0].action != Action || \
494                     strcmp(notify.nttrans.out.changes[0].name.s, "tname1") != 0) { \
495                         printf("ERROR: nchanges=%d action=%d filter=0x%08x\n", \
496                                notify.nttrans.out.num_changes, \
497                                notify.nttrans.out.changes[0].action, \
498                                notify.nttrans.in.completion_filter); \
499                         ret = False; \
500                 } \
501                 mask |= (1<<i); \
502         } \
503         if ((expected) != mask) { \
504                 if (((expected) & ~mask) != 0) { \
505                         printf("ERROR: trigger on too few bits. mask=0x%08x expected=0x%08x\n", \
506                                mask, expected); \
507                         ret = False; \
508                 } else { \
509                         printf("WARNING: trigger on too many bits. mask=0x%08x expected=0x%08x\n", \
510                                mask, expected); \
511                 } \
512         } \
513         } while (0)
514
515         printf("testing mkdir\n");
516         NOTIFY_MASK_TEST(;,
517                          smbcli_mkdir(cli->tree, BASEDIR "\\tname1");,
518                          smbcli_rmdir(cli->tree, BASEDIR "\\tname1");,
519                          NOTIFY_ACTION_ADDED,
520                          FILE_NOTIFY_CHANGE_DIR_NAME, 1);
521
522         printf("testing create file\n");
523         NOTIFY_MASK_TEST(;,
524                          smbcli_close(cli->tree, smbcli_open(cli->tree, BASEDIR "\\tname1", O_CREAT, 0));,
525                          smbcli_unlink(cli->tree, BASEDIR "\\tname1");,
526                          NOTIFY_ACTION_ADDED,
527                          FILE_NOTIFY_CHANGE_FILE_NAME, 1);
528
529         printf("testing unlink\n");
530         NOTIFY_MASK_TEST(
531                          smbcli_close(cli->tree, smbcli_open(cli->tree, BASEDIR "\\tname1", O_CREAT, 0));,
532                          smbcli_unlink(cli->tree, BASEDIR "\\tname1");,
533                          ;,
534                          NOTIFY_ACTION_REMOVED,
535                          FILE_NOTIFY_CHANGE_FILE_NAME, 1);
536
537         printf("testing rmdir\n");
538         NOTIFY_MASK_TEST(
539                          smbcli_mkdir(cli->tree, BASEDIR "\\tname1");,
540                          smbcli_rmdir(cli->tree, BASEDIR "\\tname1");,
541                          ;,
542                          NOTIFY_ACTION_REMOVED,
543                          FILE_NOTIFY_CHANGE_DIR_NAME, 1);
544
545         printf("testing rename file\n");
546         NOTIFY_MASK_TEST(
547                          smbcli_close(cli->tree, smbcli_open(cli->tree, BASEDIR "\\tname1", O_CREAT, 0));,
548                          smbcli_rename(cli->tree, BASEDIR "\\tname1", BASEDIR "\\tname2");,
549                          smbcli_unlink(cli->tree, BASEDIR "\\tname2");,
550                          NOTIFY_ACTION_OLD_NAME,
551                          FILE_NOTIFY_CHANGE_FILE_NAME|FILE_NOTIFY_CHANGE_ATTRIBUTES|FILE_NOTIFY_CHANGE_CREATION, 2);
552
553         printf("testing rename dir\n");
554         NOTIFY_MASK_TEST(
555                 smbcli_mkdir(cli->tree, BASEDIR "\\tname1");,
556                 smbcli_rename(cli->tree, BASEDIR "\\tname1", BASEDIR "\\tname2");,
557                 smbcli_rmdir(cli->tree, BASEDIR "\\tname2");,
558                 NOTIFY_ACTION_OLD_NAME,
559                 FILE_NOTIFY_CHANGE_DIR_NAME, 2);
560
561         printf("testing set path attribute\n");
562         NOTIFY_MASK_TEST(
563                 smbcli_close(cli->tree, smbcli_open(cli->tree, BASEDIR "\\tname1", O_CREAT, 0));,
564                 smbcli_setatr(cli->tree, BASEDIR "\\tname1", FILE_ATTRIBUTE_HIDDEN, 0);,
565                 smbcli_unlink(cli->tree, BASEDIR "\\tname1");,
566                 NOTIFY_ACTION_MODIFIED,
567                 FILE_NOTIFY_CHANGE_ATTRIBUTES, 1);
568
569         printf("testing set path write time\n");
570         NOTIFY_MASK_TEST(
571                 smbcli_close(cli->tree, smbcli_open(cli->tree, BASEDIR "\\tname1", O_CREAT, 0));,
572                 smbcli_setatr(cli->tree, BASEDIR "\\tname1", FILE_ATTRIBUTE_NORMAL, 1000);,
573                 smbcli_unlink(cli->tree, BASEDIR "\\tname1");,
574                 NOTIFY_ACTION_MODIFIED,
575                 FILE_NOTIFY_CHANGE_LAST_WRITE, 1);
576
577         printf("testing set file attribute\n");
578         NOTIFY_MASK_TEST(
579                 fnum2 = create_complex_file(cli, mem_ctx, BASEDIR "\\tname1");,
580                 smbcli_fsetatr(cli->tree, fnum2, FILE_ATTRIBUTE_HIDDEN, 0, 0, 0, 0);,
581                 (smbcli_close(cli->tree, fnum2), smbcli_unlink(cli->tree, BASEDIR "\\tname1"));,
582                 NOTIFY_ACTION_MODIFIED,
583                 FILE_NOTIFY_CHANGE_ATTRIBUTES, 1);
584
585         if (lp_parm_bool(-1, "torture", "samba3", False)) {
586                 printf("Samba3 does not yet support create times "
587                        "everywhere\n");
588         }
589         else {
590                 printf("testing set file create time\n");
591                 NOTIFY_MASK_TEST(
592                         fnum2 = create_complex_file(cli, mem_ctx,
593                                                     BASEDIR "\\tname1");,
594                         smbcli_fsetatr(cli->tree, fnum2, 0, t, 0, 0, 0);,
595                         (smbcli_close(cli->tree, fnum2),
596                          smbcli_unlink(cli->tree, BASEDIR "\\tname1"));,
597                         NOTIFY_ACTION_MODIFIED,
598                         FILE_NOTIFY_CHANGE_CREATION, 1);
599         }
600
601         printf("testing set file access time\n");
602         NOTIFY_MASK_TEST(
603                 fnum2 = create_complex_file(cli, mem_ctx, BASEDIR "\\tname1");,
604                 smbcli_fsetatr(cli->tree, fnum2, 0, 0, t, 0, 0);,
605                 (smbcli_close(cli->tree, fnum2), smbcli_unlink(cli->tree, BASEDIR "\\tname1"));,
606                 NOTIFY_ACTION_MODIFIED,
607                 FILE_NOTIFY_CHANGE_LAST_ACCESS, 1);
608
609         printf("testing set file write time\n");
610         NOTIFY_MASK_TEST(
611                 fnum2 = create_complex_file(cli, mem_ctx, BASEDIR "\\tname1");,
612                 smbcli_fsetatr(cli->tree, fnum2, 0, 0, 0, t, 0);,
613                 (smbcli_close(cli->tree, fnum2), smbcli_unlink(cli->tree, BASEDIR "\\tname1"));,
614                 NOTIFY_ACTION_MODIFIED,
615                 FILE_NOTIFY_CHANGE_LAST_WRITE, 1);
616
617         printf("testing set file change time\n");
618         NOTIFY_MASK_TEST(
619                 fnum2 = create_complex_file(cli, mem_ctx, BASEDIR "\\tname1");,
620                 smbcli_fsetatr(cli->tree, fnum2, 0, 0, 0, 0, t);,
621                 (smbcli_close(cli->tree, fnum2), smbcli_unlink(cli->tree, BASEDIR "\\tname1"));,
622                 NOTIFY_ACTION_MODIFIED,
623                 0, 1);
624
625
626         printf("testing write\n");
627         NOTIFY_MASK_TEST(
628                 fnum2 = create_complex_file(cli, mem_ctx, BASEDIR "\\tname1");,
629                 smbcli_write(cli->tree, fnum2, 1, &c, 10000, 1);,
630                 (smbcli_close(cli->tree, fnum2), smbcli_unlink(cli->tree, BASEDIR "\\tname1"));,
631                 NOTIFY_ACTION_MODIFIED,
632                 0, 1);
633
634         printf("testing truncate\n");
635         NOTIFY_MASK_TEST(
636                 fnum2 = create_complex_file(cli, mem_ctx, BASEDIR "\\tname1");,
637                 smbcli_ftruncate(cli->tree, fnum2, 10000);,
638                 (smbcli_close(cli->tree, fnum2), smbcli_unlink(cli->tree, BASEDIR "\\tname1"));,
639                 NOTIFY_ACTION_MODIFIED,
640                 FILE_NOTIFY_CHANGE_SIZE | FILE_NOTIFY_CHANGE_ATTRIBUTES, 1);
641
642 done:
643         smb_raw_exit(cli->session);
644         return ret;
645 }
646
647 /*
648   basic testing of change notify on files
649 */
650 static BOOL test_notify_file(struct smbcli_state *cli, TALLOC_CTX *mem_ctx)
651 {
652         NTSTATUS status;
653         BOOL ret = True;
654         union smb_open io;
655         union smb_close cl;
656         union smb_notify notify;
657         struct smbcli_request *req;
658         int fnum;
659         const char *fname = BASEDIR "\\file.txt";
660
661         printf("TESTING CHANGE NOTIFY ON FILES\n");
662
663         io.generic.level = RAW_OPEN_NTCREATEX;
664         io.ntcreatex.in.root_fid = 0;
665         io.ntcreatex.in.flags = 0;
666         io.ntcreatex.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
667         io.ntcreatex.in.create_options = 0;
668         io.ntcreatex.in.file_attr = FILE_ATTRIBUTE_NORMAL;
669         io.ntcreatex.in.share_access = NTCREATEX_SHARE_ACCESS_READ | NTCREATEX_SHARE_ACCESS_WRITE;
670         io.ntcreatex.in.alloc_size = 0;
671         io.ntcreatex.in.open_disposition = NTCREATEX_DISP_CREATE;
672         io.ntcreatex.in.impersonation = NTCREATEX_IMPERSONATION_ANONYMOUS;
673         io.ntcreatex.in.security_flags = 0;
674         io.ntcreatex.in.fname = fname;
675         status = smb_raw_open(cli->tree, mem_ctx, &io);
676         CHECK_STATUS(status, NT_STATUS_OK);
677         fnum = io.ntcreatex.out.file.fnum;
678
679         /* ask for a change notify,
680            on file or directory name changes */
681         notify.nttrans.level = RAW_NOTIFY_NTTRANS;
682         notify.nttrans.in.file.fnum = fnum;
683         notify.nttrans.in.buffer_size = 1000;
684         notify.nttrans.in.completion_filter = FILE_NOTIFY_CHANGE_STREAM_NAME;
685         notify.nttrans.in.recursive = False;
686
687         printf("testing if notifies on file handles are invalid (should be)\n");
688
689         req = smb_raw_changenotify_send(cli->tree, &notify);
690         status = smb_raw_changenotify_recv(req, mem_ctx, &notify);
691         CHECK_STATUS(status, NT_STATUS_INVALID_PARAMETER);
692
693         cl.close.level = RAW_CLOSE_CLOSE;
694         cl.close.in.file.fnum = fnum;
695         cl.close.in.write_time = 0;
696         status = smb_raw_close(cli->tree, &cl);
697         CHECK_STATUS(status, NT_STATUS_OK);
698
699         status = smbcli_unlink(cli->tree, fname);
700         CHECK_STATUS(status, NT_STATUS_OK);
701
702 done:
703         smb_raw_exit(cli->session);
704         return ret;
705 }
706
707 /*
708   basic testing of change notifies followed by a tdis
709 */
710 static BOOL test_notify_tdis(TALLOC_CTX *mem_ctx)
711 {
712         BOOL ret = True;
713         NTSTATUS status;
714         union smb_notify notify;
715         union smb_open io;
716         int fnum;
717         struct smbcli_request *req;
718         struct smbcli_state *cli = NULL;
719
720         printf("TESTING CHANGE NOTIFY FOLLOWED BY TDIS\n");
721
722         if (!torture_open_connection(&cli, 0)) {
723                 return False;
724         }
725
726         /*
727           get a handle on the directory
728         */
729         io.generic.level = RAW_OPEN_NTCREATEX;
730         io.ntcreatex.in.root_fid = 0;
731         io.ntcreatex.in.flags = 0;
732         io.ntcreatex.in.access_mask = SEC_FILE_ALL;
733         io.ntcreatex.in.create_options = NTCREATEX_OPTIONS_DIRECTORY;
734         io.ntcreatex.in.file_attr = FILE_ATTRIBUTE_NORMAL;
735         io.ntcreatex.in.share_access = NTCREATEX_SHARE_ACCESS_READ | NTCREATEX_SHARE_ACCESS_WRITE;
736         io.ntcreatex.in.alloc_size = 0;
737         io.ntcreatex.in.open_disposition = NTCREATEX_DISP_OPEN;
738         io.ntcreatex.in.impersonation = NTCREATEX_IMPERSONATION_ANONYMOUS;
739         io.ntcreatex.in.security_flags = 0;
740         io.ntcreatex.in.fname = BASEDIR;
741
742         status = smb_raw_open(cli->tree, mem_ctx, &io);
743         CHECK_STATUS(status, NT_STATUS_OK);
744         fnum = io.ntcreatex.out.file.fnum;
745
746         /* ask for a change notify,
747            on file or directory name changes */
748         notify.nttrans.level = RAW_NOTIFY_NTTRANS;
749         notify.nttrans.in.buffer_size = 1000;
750         notify.nttrans.in.completion_filter = FILE_NOTIFY_CHANGE_NAME;
751         notify.nttrans.in.file.fnum = fnum;
752         notify.nttrans.in.recursive = True;
753
754         req = smb_raw_changenotify_send(cli->tree, &notify);
755
756         status = smbcli_tdis(cli);
757         CHECK_STATUS(status, NT_STATUS_OK);
758         cli->tree = NULL;
759
760         status = smb_raw_changenotify_recv(req, mem_ctx, &notify);
761         CHECK_STATUS(status, NT_STATUS_OK);
762         CHECK_VAL(notify.nttrans.out.num_changes, 0);
763
764 done:
765         torture_close_connection(cli);
766         return ret;
767 }
768
769 /*
770   basic testing of change notifies followed by a exit
771 */
772 static BOOL test_notify_exit(TALLOC_CTX *mem_ctx)
773 {
774         BOOL ret = True;
775         NTSTATUS status;
776         union smb_notify notify;
777         union smb_open io;
778         int fnum;
779         struct smbcli_request *req;
780         struct smbcli_state *cli = NULL;
781
782         printf("TESTING CHANGE NOTIFY FOLLOWED BY EXIT\n");
783
784         if (!torture_open_connection(&cli, 0)) {
785                 return False;
786         }
787
788         /*
789           get a handle on the directory
790         */
791         io.generic.level = RAW_OPEN_NTCREATEX;
792         io.ntcreatex.in.root_fid = 0;
793         io.ntcreatex.in.flags = 0;
794         io.ntcreatex.in.access_mask = SEC_FILE_ALL;
795         io.ntcreatex.in.create_options = NTCREATEX_OPTIONS_DIRECTORY;
796         io.ntcreatex.in.file_attr = FILE_ATTRIBUTE_NORMAL;
797         io.ntcreatex.in.share_access = NTCREATEX_SHARE_ACCESS_READ | NTCREATEX_SHARE_ACCESS_WRITE;
798         io.ntcreatex.in.alloc_size = 0;
799         io.ntcreatex.in.open_disposition = NTCREATEX_DISP_OPEN;
800         io.ntcreatex.in.impersonation = NTCREATEX_IMPERSONATION_ANONYMOUS;
801         io.ntcreatex.in.security_flags = 0;
802         io.ntcreatex.in.fname = BASEDIR;
803
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.buffer_size = 1000;
812         notify.nttrans.in.completion_filter = FILE_NOTIFY_CHANGE_NAME;
813         notify.nttrans.in.file.fnum = fnum;
814         notify.nttrans.in.recursive = True;
815
816         req = smb_raw_changenotify_send(cli->tree, &notify);
817
818         status = smb_raw_exit(cli->session);
819         CHECK_STATUS(status, NT_STATUS_OK);
820
821         status = smb_raw_changenotify_recv(req, mem_ctx, &notify);
822         CHECK_STATUS(status, NT_STATUS_OK);
823         CHECK_VAL(notify.nttrans.out.num_changes, 0);
824
825 done:
826         torture_close_connection(cli);
827         return ret;
828 }
829
830 /*
831   basic testing of change notifies followed by a ulogoff
832 */
833 static BOOL test_notify_ulogoff(TALLOC_CTX *mem_ctx)
834 {
835         BOOL ret = True;
836         NTSTATUS status;
837         union smb_notify notify;
838         union smb_open io;
839         int fnum;
840         struct smbcli_request *req;
841         struct smbcli_state *cli = NULL;
842
843         printf("TESTING CHANGE NOTIFY FOLLOWED BY ULOGOFF\n");
844
845         if (!torture_open_connection(&cli, 0)) {
846                 return False;
847         }
848
849         /*
850           get a handle on the directory
851         */
852         io.generic.level = RAW_OPEN_NTCREATEX;
853         io.ntcreatex.in.root_fid = 0;
854         io.ntcreatex.in.flags = 0;
855         io.ntcreatex.in.access_mask = SEC_FILE_ALL;
856         io.ntcreatex.in.create_options = NTCREATEX_OPTIONS_DIRECTORY;
857         io.ntcreatex.in.file_attr = FILE_ATTRIBUTE_NORMAL;
858         io.ntcreatex.in.share_access = NTCREATEX_SHARE_ACCESS_READ | NTCREATEX_SHARE_ACCESS_WRITE;
859         io.ntcreatex.in.alloc_size = 0;
860         io.ntcreatex.in.open_disposition = NTCREATEX_DISP_OPEN;
861         io.ntcreatex.in.impersonation = NTCREATEX_IMPERSONATION_ANONYMOUS;
862         io.ntcreatex.in.security_flags = 0;
863         io.ntcreatex.in.fname = BASEDIR;
864
865         status = smb_raw_open(cli->tree, mem_ctx, &io);
866         CHECK_STATUS(status, NT_STATUS_OK);
867         fnum = io.ntcreatex.out.file.fnum;
868
869         /* ask for a change notify,
870            on file or directory name changes */
871         notify.nttrans.level = RAW_NOTIFY_NTTRANS;
872         notify.nttrans.in.buffer_size = 1000;
873         notify.nttrans.in.completion_filter = FILE_NOTIFY_CHANGE_NAME;
874         notify.nttrans.in.file.fnum = fnum;
875         notify.nttrans.in.recursive = True;
876
877         req = smb_raw_changenotify_send(cli->tree, &notify);
878
879         status = smb_raw_ulogoff(cli->session);
880         CHECK_STATUS(status, NT_STATUS_OK);
881
882         status = smb_raw_changenotify_recv(req, mem_ctx, &notify);
883         CHECK_STATUS(status, NT_STATUS_OK);
884         CHECK_VAL(notify.nttrans.out.num_changes, 0);
885
886 done:
887         torture_close_connection(cli);
888         return ret;
889 }
890
891 static void tcp_dis_handler(struct smbcli_transport *t, void *p)
892 {
893         struct smbcli_state *cli = p;
894         smbcli_transport_dead(cli->transport, NT_STATUS_LOCAL_DISCONNECT);
895         cli->transport = NULL;
896         cli->tree = NULL;
897 }
898 /*
899   basic testing of change notifies followed by tcp disconnect
900 */
901 static BOOL test_notify_tcp_dis(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 TCP DISCONNECT\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         smbcli_transport_idle_handler(cli->transport, tcp_dis_handler, 250, cli);
948
949         status = smb_raw_changenotify_recv(req, mem_ctx, &notify);
950         CHECK_STATUS(status, NT_STATUS_LOCAL_DISCONNECT);
951
952 done:
953         torture_close_connection(cli);
954         return ret;
955 }
956
957 /* 
958    test setting up two change notify requests on one handle
959 */
960 static BOOL test_notify_double(struct smbcli_state *cli, TALLOC_CTX *mem_ctx)
961 {
962         BOOL ret = True;
963         NTSTATUS status;
964         union smb_notify notify;
965         union smb_open io;
966         int fnum;
967         struct smbcli_request *req1, *req2;
968
969         printf("TESTING CHANGE NOTIFY TWICE ON ONE DIRECTORY\n");
970                 
971         /*
972           get a handle on the directory
973         */
974         io.generic.level = RAW_OPEN_NTCREATEX;
975         io.ntcreatex.in.root_fid = 0;
976         io.ntcreatex.in.flags = 0;
977         io.ntcreatex.in.access_mask = SEC_FILE_ALL;
978         io.ntcreatex.in.create_options = NTCREATEX_OPTIONS_DIRECTORY;
979         io.ntcreatex.in.file_attr = FILE_ATTRIBUTE_NORMAL;
980         io.ntcreatex.in.share_access = NTCREATEX_SHARE_ACCESS_READ | NTCREATEX_SHARE_ACCESS_WRITE;
981         io.ntcreatex.in.alloc_size = 0;
982         io.ntcreatex.in.open_disposition = NTCREATEX_DISP_OPEN;
983         io.ntcreatex.in.impersonation = NTCREATEX_IMPERSONATION_ANONYMOUS;
984         io.ntcreatex.in.security_flags = 0;
985         io.ntcreatex.in.fname = BASEDIR;
986
987         status = smb_raw_open(cli->tree, mem_ctx, &io);
988         CHECK_STATUS(status, NT_STATUS_OK);
989         fnum = io.ntcreatex.out.file.fnum;
990
991         /* ask for a change notify,
992            on file or directory name changes */
993         notify.nttrans.level = RAW_NOTIFY_NTTRANS;
994         notify.nttrans.in.buffer_size = 1000;
995         notify.nttrans.in.completion_filter = FILE_NOTIFY_CHANGE_NAME;
996         notify.nttrans.in.file.fnum = fnum;
997         notify.nttrans.in.recursive = True;
998
999         req1 = smb_raw_changenotify_send(cli->tree, &notify);
1000         req2 = smb_raw_changenotify_send(cli->tree, &notify);
1001
1002         smbcli_mkdir(cli->tree, BASEDIR "\\subdir-name");
1003
1004         status = smb_raw_changenotify_recv(req1, mem_ctx, &notify);
1005         CHECK_STATUS(status, NT_STATUS_OK);
1006         CHECK_VAL(notify.nttrans.out.num_changes, 1);
1007         CHECK_WSTR(notify.nttrans.out.changes[0].name, "subdir-name", STR_UNICODE);
1008
1009         smbcli_mkdir(cli->tree, BASEDIR "\\subdir-name2");
1010
1011         status = smb_raw_changenotify_recv(req2, mem_ctx, &notify);
1012         CHECK_STATUS(status, NT_STATUS_OK);
1013         CHECK_VAL(notify.nttrans.out.num_changes, 1);
1014         CHECK_WSTR(notify.nttrans.out.changes[0].name, "subdir-name2", STR_UNICODE);
1015
1016 done:
1017         smb_raw_exit(cli->session);
1018         return ret;
1019 }
1020
1021
1022 /* 
1023    test multiple change notifies at different depths and with/without recursion
1024 */
1025 static BOOL test_notify_tree(struct smbcli_state *cli, TALLOC_CTX *mem_ctx)
1026 {
1027         BOOL ret = True;
1028         union smb_notify notify;
1029         union smb_open io;
1030         struct smbcli_request *req;
1031         struct timeval tv;
1032         struct {
1033                 const char *path;
1034                 BOOL recursive;
1035                 uint32_t filter;
1036                 int expected;
1037                 int fnum;
1038                 int counted;
1039         } dirs[] = {
1040                 {BASEDIR "\\abc",               True, FILE_NOTIFY_CHANGE_NAME, 30 },
1041                 {BASEDIR "\\zqy",               True, FILE_NOTIFY_CHANGE_NAME, 8 },
1042                 {BASEDIR "\\atsy",              True, FILE_NOTIFY_CHANGE_NAME, 4 },
1043                 {BASEDIR "\\abc\\foo",          True,  FILE_NOTIFY_CHANGE_NAME, 2 },
1044                 {BASEDIR "\\abc\\blah",         True,  FILE_NOTIFY_CHANGE_NAME, 13 },
1045                 {BASEDIR "\\abc\\blah",         False, FILE_NOTIFY_CHANGE_NAME, 7 },
1046                 {BASEDIR "\\abc\\blah\\a",      True, FILE_NOTIFY_CHANGE_NAME, 2 },
1047                 {BASEDIR "\\abc\\blah\\b",      True, FILE_NOTIFY_CHANGE_NAME, 2 },
1048                 {BASEDIR "\\abc\\blah\\c",      True, FILE_NOTIFY_CHANGE_NAME, 2 },
1049                 {BASEDIR "\\abc\\fooblah",      True, FILE_NOTIFY_CHANGE_NAME, 2 },
1050                 {BASEDIR "\\zqy\\xx",           True, FILE_NOTIFY_CHANGE_NAME, 2 },
1051                 {BASEDIR "\\zqy\\yyy",          True, FILE_NOTIFY_CHANGE_NAME, 2 },
1052                 {BASEDIR "\\zqy\\..",           True, FILE_NOTIFY_CHANGE_NAME, 40 },
1053                 {BASEDIR,                       True, FILE_NOTIFY_CHANGE_NAME, 40 },
1054                 {BASEDIR,                       False,FILE_NOTIFY_CHANGE_NAME, 6 },
1055                 {BASEDIR "\\atsy",              False,FILE_NOTIFY_CHANGE_NAME, 4 },
1056                 {BASEDIR "\\abc",               True, FILE_NOTIFY_CHANGE_NAME, 24 },
1057                 {BASEDIR "\\abc",               False,FILE_NOTIFY_CHANGE_FILE_NAME, 0 },
1058                 {BASEDIR "\\abc",               True, FILE_NOTIFY_CHANGE_FILE_NAME, 0 },
1059                 {BASEDIR "\\abc",               True, FILE_NOTIFY_CHANGE_NAME, 24 },
1060         };
1061         int i;
1062         NTSTATUS status;
1063         BOOL all_done = False;
1064
1065         printf("TESTING CHANGE NOTIFY FOR DIFFERENT DEPTHS\n");
1066
1067         io.generic.level = RAW_OPEN_NTCREATEX;
1068         io.ntcreatex.in.root_fid = 0;
1069         io.ntcreatex.in.flags = 0;
1070         io.ntcreatex.in.access_mask = SEC_FILE_ALL;
1071         io.ntcreatex.in.create_options = NTCREATEX_OPTIONS_DIRECTORY;
1072         io.ntcreatex.in.file_attr = FILE_ATTRIBUTE_NORMAL;
1073         io.ntcreatex.in.share_access = NTCREATEX_SHARE_ACCESS_READ | NTCREATEX_SHARE_ACCESS_WRITE;
1074         io.ntcreatex.in.alloc_size = 0;
1075         io.ntcreatex.in.open_disposition = NTCREATEX_DISP_OPEN_IF;
1076         io.ntcreatex.in.impersonation = NTCREATEX_IMPERSONATION_ANONYMOUS;
1077         io.ntcreatex.in.security_flags = 0;
1078
1079         notify.nttrans.level = RAW_NOTIFY_NTTRANS;
1080         notify.nttrans.in.buffer_size = 20000;
1081
1082         /*
1083           setup the directory tree, and the notify buffer on each directory
1084         */
1085         for (i=0;i<ARRAY_SIZE(dirs);i++) {
1086                 io.ntcreatex.in.fname = dirs[i].path;
1087                 status = smb_raw_open(cli->tree, mem_ctx, &io);
1088                 CHECK_STATUS(status, NT_STATUS_OK);
1089                 dirs[i].fnum = io.ntcreatex.out.file.fnum;
1090
1091                 notify.nttrans.in.completion_filter = dirs[i].filter;
1092                 notify.nttrans.in.file.fnum = dirs[i].fnum;
1093                 notify.nttrans.in.recursive = dirs[i].recursive;
1094                 req = smb_raw_changenotify_send(cli->tree, &notify);
1095                 smb_raw_ntcancel(req);
1096                 status = smb_raw_changenotify_recv(req, mem_ctx, &notify);
1097                 CHECK_STATUS(status, NT_STATUS_CANCELLED);
1098         }
1099
1100         /* trigger 2 events in each dir */
1101         for (i=0;i<ARRAY_SIZE(dirs);i++) {
1102                 char *path = talloc_asprintf(mem_ctx, "%s\\test.dir", dirs[i].path);
1103                 smbcli_mkdir(cli->tree, path);
1104                 smbcli_rmdir(cli->tree, path);
1105                 talloc_free(path);
1106         }
1107
1108         /* give a bit of time for the events to propogate */
1109         tv = timeval_current();
1110
1111         do {
1112                 /* count events that have happened in each dir */
1113                 for (i=0;i<ARRAY_SIZE(dirs);i++) {
1114                         notify.nttrans.in.file.fnum = dirs[i].fnum;
1115                         req = smb_raw_changenotify_send(cli->tree, &notify);
1116                         smb_raw_ntcancel(req);
1117                         notify.nttrans.out.num_changes = 0;
1118                         status = smb_raw_changenotify_recv(req, mem_ctx, &notify);
1119                         dirs[i].counted += notify.nttrans.out.num_changes;
1120                 }
1121                 
1122                 all_done = True;
1123
1124                 for (i=0;i<ARRAY_SIZE(dirs);i++) {
1125                         if (dirs[i].counted != dirs[i].expected) {
1126                                 all_done = False;
1127                         }
1128                 }
1129         } while (!all_done && timeval_elapsed(&tv) < 20);
1130
1131         printf("took %.4f seconds to propogate all events\n", timeval_elapsed(&tv));
1132
1133         for (i=0;i<ARRAY_SIZE(dirs);i++) {
1134                 if (dirs[i].counted != dirs[i].expected) {
1135                         printf("ERROR: i=%d expected %d got %d for '%s'\n",
1136                                i, dirs[i].expected, dirs[i].counted, dirs[i].path);
1137                         ret = False;
1138                 }
1139         }
1140
1141         /*
1142           run from the back, closing and deleting
1143         */
1144         for (i=ARRAY_SIZE(dirs)-1;i>=0;i--) {
1145                 smbcli_close(cli->tree, dirs[i].fnum);
1146                 smbcli_rmdir(cli->tree, dirs[i].path);
1147         }
1148
1149 done:
1150         smb_raw_exit(cli->session);
1151         return ret;
1152 }
1153
1154 /* 
1155    basic testing of change notify
1156 */
1157 BOOL torture_raw_notify(struct torture_context *torture)
1158 {
1159         struct smbcli_state *cli, *cli2;
1160         BOOL ret = True;
1161         TALLOC_CTX *mem_ctx;
1162                 
1163         if (!torture_open_connection(&cli, 0)) {
1164                 return False;
1165         }
1166         if (!torture_open_connection(&cli2, 0)) {
1167                 return False;
1168         }
1169
1170         mem_ctx = talloc_init("torture_raw_notify");
1171
1172         if (!torture_setup_dir(cli, BASEDIR)) {
1173                 return False;
1174         }
1175
1176         ret &= test_notify_dir(cli, cli2, mem_ctx);
1177         ret &= test_notify_mask(cli, mem_ctx);
1178         ret &= test_notify_recursive(cli, mem_ctx);
1179         ret &= test_notify_file(cli, mem_ctx);
1180         ret &= test_notify_tdis(mem_ctx);
1181         ret &= test_notify_exit(mem_ctx);
1182         ret &= test_notify_ulogoff(mem_ctx);
1183         ret &= test_notify_tcp_dis(mem_ctx);
1184         ret &= test_notify_double(cli, mem_ctx);
1185         ret &= test_notify_tree(cli, mem_ctx);
1186
1187         smb_raw_exit(cli->session);
1188         smbcli_deltree(cli->tree, BASEDIR);
1189         torture_close_connection(cli);
1190         torture_close_connection(cli2);
1191         talloc_free(mem_ctx);
1192         return ret;
1193 }