r16907: Add an index parameter to torture_open_connection. Next step is to enable the
[gd/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 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, TALLOC_CTX *mem_ctx)
59 {
60         BOOL ret = True;
61         NTSTATUS status;
62         struct 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.in.buffer_size = 1000;
98         notify.in.completion_filter = FILE_NOTIFY_CHANGE_NAME;
99         notify.in.file.fnum = fnum;
100         notify.in.recursive = True;
101
102         printf("testing notify cancel\n");
103
104         req = smb_raw_changenotify_send(cli->tree, &notify);
105         smb_raw_ntcancel(req);
106         status = smb_raw_changenotify_recv(req, mem_ctx, &notify);
107         CHECK_STATUS(status, NT_STATUS_CANCELLED);
108
109         printf("testing notify mkdir\n");
110
111         req = smb_raw_changenotify_send(cli->tree, &notify);
112         smbcli_mkdir(cli->tree, BASEDIR "\\subdir-name");
113
114         status = smb_raw_changenotify_recv(req, mem_ctx, &notify);
115         CHECK_STATUS(status, NT_STATUS_OK);
116
117         CHECK_VAL(notify.out.num_changes, 1);
118         CHECK_VAL(notify.out.changes[0].action, NOTIFY_ACTION_ADDED);
119         CHECK_WSTR(notify.out.changes[0].name, "subdir-name", STR_UNICODE);
120
121         printf("testing notify rmdir\n");
122
123         req = smb_raw_changenotify_send(cli->tree, &notify);
124         smbcli_rmdir(cli->tree, BASEDIR "\\subdir-name");
125
126         status = smb_raw_changenotify_recv(req, mem_ctx, &notify);
127         CHECK_STATUS(status, NT_STATUS_OK);
128         CHECK_VAL(notify.out.num_changes, 1);
129         CHECK_VAL(notify.out.changes[0].action, NOTIFY_ACTION_REMOVED);
130         CHECK_WSTR(notify.out.changes[0].name, "subdir-name", STR_UNICODE);
131
132         printf("testing notify mkdir - rmdir - mkdir - rmdir\n");
133
134         smbcli_mkdir(cli->tree, BASEDIR "\\subdir-name");
135         smbcli_rmdir(cli->tree, BASEDIR "\\subdir-name");
136         smbcli_mkdir(cli->tree, BASEDIR "\\subdir-name");
137         smbcli_rmdir(cli->tree, BASEDIR "\\subdir-name");
138         req = smb_raw_changenotify_send(cli->tree, &notify);
139         status = smb_raw_changenotify_recv(req, mem_ctx, &notify);
140         CHECK_STATUS(status, NT_STATUS_OK);
141         CHECK_VAL(notify.out.num_changes, 4);
142         CHECK_VAL(notify.out.changes[0].action, NOTIFY_ACTION_ADDED);
143         CHECK_WSTR(notify.out.changes[0].name, "subdir-name", STR_UNICODE);
144         CHECK_VAL(notify.out.changes[1].action, NOTIFY_ACTION_REMOVED);
145         CHECK_WSTR(notify.out.changes[1].name, "subdir-name", STR_UNICODE);
146         CHECK_VAL(notify.out.changes[2].action, NOTIFY_ACTION_ADDED);
147         CHECK_WSTR(notify.out.changes[2].name, "subdir-name", STR_UNICODE);
148         CHECK_VAL(notify.out.changes[3].action, NOTIFY_ACTION_REMOVED);
149         CHECK_WSTR(notify.out.changes[3].name, "subdir-name", STR_UNICODE);
150
151         count = torture_numops;
152         printf("testing buffered notify on create of %d files\n", count);
153         for (i=0;i<count;i++) {
154                 char *fname = talloc_asprintf(cli, BASEDIR "\\test%d.txt", i);
155                 int fnum3 = smbcli_open(cli->tree, fname, O_CREAT|O_RDWR, DENY_NONE);
156                 if (fnum3 == -1) {
157                         printf("Failed to create %s - %s\n", 
158                                fname, smbcli_errstr(cli->tree));
159                         ret = False;
160                         goto done;
161                 }
162                 talloc_free(fname);
163                 smbcli_close(cli->tree, fnum3);
164         }
165
166         /* (1st notify) setup a new notify on a different directory handle.
167            This new notify won't see the events above. */
168         notify.in.file.fnum = fnum2;
169         req2 = smb_raw_changenotify_send(cli->tree, &notify);
170
171         /* (2nd notify) whereas this notify will see the above buffered events,
172            and it directly returns the buffered events */
173         notify.in.file.fnum = fnum;
174         req = smb_raw_changenotify_send(cli->tree, &notify);
175
176         /* (1st unlink) as the 2nd notify directly returns,
177            this unlink is only seen by the 1st notify and 
178            the 3rd notify (later) */
179         printf("testing notify on unlink for the first file\n");
180         status = smbcli_unlink(cli->tree, BASEDIR "\\test0.txt");
181         CHECK_STATUS(status, NT_STATUS_OK);
182
183         /* receive the reply from the 2nd notify */
184         status = smb_raw_changenotify_recv(req, mem_ctx, &notify);
185         CHECK_STATUS(status, NT_STATUS_OK);
186
187         CHECK_VAL(notify.out.num_changes, count);
188         for (i=1;i<notify.out.num_changes;i++) {
189                 CHECK_VAL(notify.out.changes[i].action, NOTIFY_ACTION_ADDED);
190         }
191         CHECK_WSTR(notify.out.changes[0].name, "test0.txt", STR_UNICODE);
192
193         /* and now from the 1st notify */
194         status = smb_raw_changenotify_recv(req2, mem_ctx, &notify);
195         CHECK_STATUS(status, NT_STATUS_OK);
196         CHECK_VAL(notify.out.num_changes, 1);
197         CHECK_VAL(notify.out.changes[0].action, NOTIFY_ACTION_REMOVED);
198         CHECK_WSTR(notify.out.changes[0].name, "test0.txt", STR_UNICODE);
199
200         /* (3rd notify) this notify will only see the 1st unlink */
201         req = smb_raw_changenotify_send(cli->tree, &notify);
202
203         printf("testing notify on wildcard unlink for %d files\n", count-1);
204         /* (2nd unlink) do a wildcard unlink */
205         status = smbcli_unlink(cli->tree, BASEDIR "\\test*.txt");
206         CHECK_STATUS(status, NT_STATUS_OK);
207
208         /* receive the 3rd notify */
209         status = smb_raw_changenotify_recv(req, mem_ctx, &notify);
210         CHECK_STATUS(status, NT_STATUS_OK);
211         CHECK_VAL(notify.out.num_changes, 1);
212         CHECK_VAL(notify.out.changes[0].action, NOTIFY_ACTION_REMOVED);
213         CHECK_WSTR(notify.out.changes[0].name, "test0.txt", STR_UNICODE);
214
215         /* and we now see the rest of the unlink calls on both directory handles */
216         notify.in.file.fnum = fnum;
217         sleep(1);
218         req = smb_raw_changenotify_send(cli->tree, &notify);
219         status = smb_raw_changenotify_recv(req, mem_ctx, &notify);
220         CHECK_STATUS(status, NT_STATUS_OK);
221         CHECK_VAL(notify.out.num_changes, count-1);
222         for (i=0;i<notify.out.num_changes;i++) {
223                 CHECK_VAL(notify.out.changes[i].action, NOTIFY_ACTION_REMOVED);
224         }
225         notify.in.file.fnum = fnum2;
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.out.num_changes, count-1);
230         for (i=0;i<notify.out.num_changes;i++) {
231                 CHECK_VAL(notify.out.changes[i].action, NOTIFY_ACTION_REMOVED);
232         }
233
234         printf("testing if a close() on the dir handle triggers the notify reply\n");
235
236         notify.in.file.fnum = fnum;
237         req = smb_raw_changenotify_send(cli->tree, &notify);
238
239         cl.close.level = RAW_CLOSE_CLOSE;
240         cl.close.in.file.fnum = fnum;
241         cl.close.in.write_time = 0;
242         status = smb_raw_close(cli->tree, &cl);
243         CHECK_STATUS(status, NT_STATUS_OK);
244
245         status = smb_raw_changenotify_recv(req, mem_ctx, &notify);
246         CHECK_STATUS(status, NT_STATUS_OK);
247         CHECK_VAL(notify.out.num_changes, 0);
248
249 done:
250         smb_raw_exit(cli->session);
251         return ret;
252 }
253
254
255 /* 
256    testing of recursive change notify
257 */
258 static BOOL test_notify_recursive(struct smbcli_state *cli, TALLOC_CTX *mem_ctx)
259 {
260         BOOL ret = True;
261         NTSTATUS status;
262         struct smb_notify notify;
263         union smb_open io;
264         int fnum;
265         struct smbcli_request *req1, *req2;
266
267         printf("TESTING CHANGE NOTIFY WITH RECURSION\n");
268                 
269         /*
270           get a handle on the directory
271         */
272         io.generic.level = RAW_OPEN_NTCREATEX;
273         io.ntcreatex.in.root_fid = 0;
274         io.ntcreatex.in.flags = 0;
275         io.ntcreatex.in.access_mask = SEC_FILE_ALL;
276         io.ntcreatex.in.create_options = NTCREATEX_OPTIONS_DIRECTORY;
277         io.ntcreatex.in.file_attr = FILE_ATTRIBUTE_NORMAL;
278         io.ntcreatex.in.share_access = NTCREATEX_SHARE_ACCESS_READ | NTCREATEX_SHARE_ACCESS_WRITE;
279         io.ntcreatex.in.alloc_size = 0;
280         io.ntcreatex.in.open_disposition = NTCREATEX_DISP_OPEN;
281         io.ntcreatex.in.impersonation = NTCREATEX_IMPERSONATION_ANONYMOUS;
282         io.ntcreatex.in.security_flags = 0;
283         io.ntcreatex.in.fname = BASEDIR;
284
285         status = smb_raw_open(cli->tree, mem_ctx, &io);
286         CHECK_STATUS(status, NT_STATUS_OK);
287         fnum = io.ntcreatex.out.file.fnum;
288
289         /* ask for a change notify, on file or directory name
290            changes. Setup both with and without recursion */
291         notify.in.buffer_size = 1000;
292         notify.in.completion_filter = FILE_NOTIFY_CHANGE_NAME | FILE_NOTIFY_CHANGE_ATTRIBUTES | FILE_NOTIFY_CHANGE_CREATION;
293         notify.in.file.fnum = fnum;
294
295         notify.in.recursive = True;
296         req1 = smb_raw_changenotify_send(cli->tree, &notify);
297
298         notify.in.recursive = False;
299         req2 = smb_raw_changenotify_send(cli->tree, &notify);
300
301         /* cancel initial requests so the buffer is setup */
302         smb_raw_ntcancel(req1);
303         status = smb_raw_changenotify_recv(req1, mem_ctx, &notify);
304         CHECK_STATUS(status, NT_STATUS_CANCELLED);
305
306         smb_raw_ntcancel(req2);
307         status = smb_raw_changenotify_recv(req2, mem_ctx, &notify);
308         CHECK_STATUS(status, NT_STATUS_CANCELLED);
309
310         smbcli_mkdir(cli->tree, BASEDIR "\\subdir-name");
311         smbcli_mkdir(cli->tree, BASEDIR "\\subdir-name\\subname1");
312         smbcli_close(cli->tree, 
313                      smbcli_open(cli->tree, BASEDIR "\\subdir-name\\subname2", O_CREAT, 0));
314         smbcli_rename(cli->tree, BASEDIR "\\subdir-name\\subname1", BASEDIR "\\subdir-name\\subname1-r");
315         smbcli_rename(cli->tree, BASEDIR "\\subdir-name\\subname2", BASEDIR "\\subname2-r");
316         smbcli_rename(cli->tree, BASEDIR "\\subname2-r", BASEDIR "\\subname3-r");
317
318         notify.in.completion_filter = 0;
319         notify.in.recursive = True;
320         msleep(10);
321         req1 = smb_raw_changenotify_send(cli->tree, &notify);
322
323         smbcli_rmdir(cli->tree, BASEDIR "\\subdir-name\\subname1-r");
324         smbcli_rmdir(cli->tree, BASEDIR "\\subdir-name");
325         smbcli_unlink(cli->tree, BASEDIR "\\subname3-r");
326
327         notify.in.recursive = False;
328         req2 = smb_raw_changenotify_send(cli->tree, &notify);
329
330         status = smb_raw_changenotify_recv(req1, mem_ctx, &notify);
331         CHECK_STATUS(status, NT_STATUS_OK);
332
333         CHECK_VAL(notify.out.num_changes, 11);
334         CHECK_VAL(notify.out.changes[0].action, NOTIFY_ACTION_ADDED);
335         CHECK_WSTR(notify.out.changes[0].name, "subdir-name", STR_UNICODE);
336         CHECK_VAL(notify.out.changes[1].action, NOTIFY_ACTION_ADDED);
337         CHECK_WSTR(notify.out.changes[1].name, "subdir-name\\subname1", STR_UNICODE);
338         CHECK_VAL(notify.out.changes[2].action, NOTIFY_ACTION_ADDED);
339         CHECK_WSTR(notify.out.changes[2].name, "subdir-name\\subname2", STR_UNICODE);
340         CHECK_VAL(notify.out.changes[3].action, NOTIFY_ACTION_OLD_NAME);
341         CHECK_WSTR(notify.out.changes[3].name, "subdir-name\\subname1", STR_UNICODE);
342         CHECK_VAL(notify.out.changes[4].action, NOTIFY_ACTION_NEW_NAME);
343         CHECK_WSTR(notify.out.changes[4].name, "subdir-name\\subname1-r", STR_UNICODE);
344
345         /* the remove/add between directories is acceptable in either order */
346         if (notify.out.changes[5].action == NOTIFY_ACTION_ADDED) {
347                 CHECK_VAL(notify.out.changes[6].action, NOTIFY_ACTION_REMOVED);
348                 CHECK_WSTR(notify.out.changes[6].name, "subdir-name\\subname2", STR_UNICODE);
349                 CHECK_VAL(notify.out.changes[5].action, NOTIFY_ACTION_ADDED);
350                 CHECK_WSTR(notify.out.changes[5].name, "subname2-r", STR_UNICODE);
351         } else {
352                 CHECK_VAL(notify.out.changes[5].action, NOTIFY_ACTION_REMOVED);
353                 CHECK_WSTR(notify.out.changes[5].name, "subdir-name\\subname2", STR_UNICODE);
354                 CHECK_VAL(notify.out.changes[6].action, NOTIFY_ACTION_ADDED);
355                 CHECK_WSTR(notify.out.changes[6].name, "subname2-r", STR_UNICODE);
356         }
357
358         CHECK_VAL(notify.out.changes[7].action, NOTIFY_ACTION_MODIFIED);
359         CHECK_WSTR(notify.out.changes[7].name, "subname2-r", STR_UNICODE);
360
361         CHECK_VAL(notify.out.changes[8].action, NOTIFY_ACTION_OLD_NAME);
362         CHECK_WSTR(notify.out.changes[8].name, "subname2-r", STR_UNICODE);
363         CHECK_VAL(notify.out.changes[9].action, NOTIFY_ACTION_NEW_NAME);
364         CHECK_WSTR(notify.out.changes[9].name, "subname3-r", STR_UNICODE);
365         CHECK_VAL(notify.out.changes[10].action, NOTIFY_ACTION_MODIFIED);
366         CHECK_WSTR(notify.out.changes[10].name, "subname3-r", STR_UNICODE);
367
368         status = smb_raw_changenotify_recv(req2, mem_ctx, &notify);
369         CHECK_STATUS(status, NT_STATUS_OK);
370
371         CHECK_VAL(notify.out.num_changes, 3);
372         CHECK_VAL(notify.out.changes[0].action, NOTIFY_ACTION_REMOVED);
373         CHECK_WSTR(notify.out.changes[0].name, "subdir-name\\subname1-r", STR_UNICODE);
374         CHECK_VAL(notify.out.changes[1].action, NOTIFY_ACTION_REMOVED);
375         CHECK_WSTR(notify.out.changes[1].name, "subdir-name", STR_UNICODE);
376         CHECK_VAL(notify.out.changes[2].action, NOTIFY_ACTION_REMOVED);
377         CHECK_WSTR(notify.out.changes[2].name, "subname3-r", STR_UNICODE);
378
379 done:
380         smb_raw_exit(cli->session);
381         return ret;
382 }
383
384
385 /* 
386    testing of mask bits for change notify
387 */
388 static BOOL test_notify_mask(struct smbcli_state *cli, TALLOC_CTX *mem_ctx)
389 {
390         BOOL ret = True;
391         NTSTATUS status;
392         struct smb_notify notify;
393         union smb_open io;
394         int fnum, fnum2;
395         uint32_t mask;
396         int i;
397         char c = 1;
398         struct timeval tv;
399         NTTIME t;
400
401         printf("TESTING CHANGE NOTIFY COMPLETION FILTERS\n");
402
403         tv = timeval_current_ofs(1000, 0);
404         t = timeval_to_nttime(&tv);
405                 
406         /*
407           get a handle on the directory
408         */
409         io.generic.level = RAW_OPEN_NTCREATEX;
410         io.ntcreatex.in.root_fid = 0;
411         io.ntcreatex.in.flags = 0;
412         io.ntcreatex.in.access_mask = SEC_FILE_ALL;
413         io.ntcreatex.in.create_options = NTCREATEX_OPTIONS_DIRECTORY;
414         io.ntcreatex.in.file_attr = FILE_ATTRIBUTE_NORMAL;
415         io.ntcreatex.in.share_access = NTCREATEX_SHARE_ACCESS_READ | NTCREATEX_SHARE_ACCESS_WRITE;
416         io.ntcreatex.in.alloc_size = 0;
417         io.ntcreatex.in.open_disposition = NTCREATEX_DISP_OPEN;
418         io.ntcreatex.in.impersonation = NTCREATEX_IMPERSONATION_ANONYMOUS;
419         io.ntcreatex.in.security_flags = 0;
420         io.ntcreatex.in.fname = BASEDIR;
421
422         notify.in.buffer_size = 1000;
423         notify.in.recursive = True;
424
425 #define NOTIFY_MASK_TEST(setup, op, cleanup, Action, expected, nchanges) \
426         do { for (mask=i=0;i<32;i++) { \
427                 struct smbcli_request *req; \
428                 status = smb_raw_open(cli->tree, mem_ctx, &io); \
429                 CHECK_STATUS(status, NT_STATUS_OK); \
430                 fnum = io.ntcreatex.out.file.fnum; \
431                 setup \
432                 notify.in.file.fnum = fnum;     \
433                 notify.in.completion_filter = (1<<i); \
434                 req = smb_raw_changenotify_send(cli->tree, &notify); \
435                 op \
436                 msleep(10); smb_raw_ntcancel(req); \
437                 status = smb_raw_changenotify_recv(req, mem_ctx, &notify); \
438                 cleanup \
439                 smbcli_close(cli->tree, fnum); \
440                 if (NT_STATUS_EQUAL(status, NT_STATUS_CANCELLED)) continue; \
441                 CHECK_STATUS(status, NT_STATUS_OK); \
442                 /* special case to cope with file rename behaviour */ \
443                 if (nchanges == 2 && notify.out.num_changes == 1 && \
444                     notify.out.changes[0].action == NOTIFY_ACTION_MODIFIED && \
445                     ((expected) & FILE_NOTIFY_CHANGE_ATTRIBUTES) && \
446                     Action == NOTIFY_ACTION_OLD_NAME) { \
447                         printf("(rename file special handling OK)\n"); \
448                 } else if (nchanges != notify.out.num_changes || \
449                     notify.out.changes[0].action != Action || \
450                     strcmp(notify.out.changes[0].name.s, "tname1") != 0) { \
451                         printf("ERROR: nchanges=%d action=%d filter=0x%08x\n", \
452                                notify.out.num_changes, \
453                                notify.out.changes[0].action, \
454                                notify.in.completion_filter); \
455                         ret = False; \
456                 } \
457                 mask |= (1<<i); \
458         } \
459         if ((expected) != mask) { \
460                 if (((expected) & ~mask) != 0) { \
461                         printf("ERROR: trigger on too few bits. mask=0x%08x expected=0x%08x\n", \
462                                mask, expected); \
463                         ret = False; \
464                 } else { \
465                         printf("WARNING: trigger on too many bits. mask=0x%08x expected=0x%08x\n", \
466                                mask, expected); \
467                 } \
468         } \
469         } while (0)
470
471         printf("testing mkdir\n");
472         NOTIFY_MASK_TEST(;,
473                          smbcli_mkdir(cli->tree, BASEDIR "\\tname1");,
474                          smbcli_rmdir(cli->tree, BASEDIR "\\tname1");,
475                          NOTIFY_ACTION_ADDED,
476                          FILE_NOTIFY_CHANGE_DIR_NAME, 1);
477
478         printf("testing create file\n");
479         NOTIFY_MASK_TEST(;,
480                          smbcli_close(cli->tree, smbcli_open(cli->tree, BASEDIR "\\tname1", O_CREAT, 0));,
481                          smbcli_unlink(cli->tree, BASEDIR "\\tname1");,
482                          NOTIFY_ACTION_ADDED,
483                          FILE_NOTIFY_CHANGE_FILE_NAME, 1);
484
485         printf("testing unlink\n");
486         NOTIFY_MASK_TEST(
487                          smbcli_close(cli->tree, smbcli_open(cli->tree, BASEDIR "\\tname1", O_CREAT, 0));,
488                          smbcli_unlink(cli->tree, BASEDIR "\\tname1");,
489                          ;,
490                          NOTIFY_ACTION_REMOVED,
491                          FILE_NOTIFY_CHANGE_FILE_NAME, 1);
492
493         printf("testing rmdir\n");
494         NOTIFY_MASK_TEST(
495                          smbcli_mkdir(cli->tree, BASEDIR "\\tname1");,
496                          smbcli_rmdir(cli->tree, BASEDIR "\\tname1");,
497                          ;,
498                          NOTIFY_ACTION_REMOVED,
499                          FILE_NOTIFY_CHANGE_DIR_NAME, 1);
500
501         printf("testing rename file\n");
502         NOTIFY_MASK_TEST(
503                          smbcli_close(cli->tree, smbcli_open(cli->tree, BASEDIR "\\tname1", O_CREAT, 0));,
504                          smbcli_rename(cli->tree, BASEDIR "\\tname1", BASEDIR "\\tname2");,
505                          smbcli_unlink(cli->tree, BASEDIR "\\tname2");,
506                          NOTIFY_ACTION_OLD_NAME,
507                          FILE_NOTIFY_CHANGE_FILE_NAME|FILE_NOTIFY_CHANGE_ATTRIBUTES|FILE_NOTIFY_CHANGE_CREATION, 2);
508
509         printf("testing rename dir\n");
510         NOTIFY_MASK_TEST(
511                 smbcli_mkdir(cli->tree, BASEDIR "\\tname1");,
512                 smbcli_rename(cli->tree, BASEDIR "\\tname1", BASEDIR "\\tname2");,
513                 smbcli_rmdir(cli->tree, BASEDIR "\\tname2");,
514                 NOTIFY_ACTION_OLD_NAME,
515                 FILE_NOTIFY_CHANGE_DIR_NAME, 2);
516
517         printf("testing set path attribute\n");
518         NOTIFY_MASK_TEST(
519                 smbcli_close(cli->tree, smbcli_open(cli->tree, BASEDIR "\\tname1", O_CREAT, 0));,
520                 smbcli_setatr(cli->tree, BASEDIR "\\tname1", FILE_ATTRIBUTE_HIDDEN, 0);,
521                 smbcli_unlink(cli->tree, BASEDIR "\\tname1");,
522                 NOTIFY_ACTION_MODIFIED,
523                 FILE_NOTIFY_CHANGE_ATTRIBUTES, 1);
524
525         printf("testing set path write time\n");
526         NOTIFY_MASK_TEST(
527                 smbcli_close(cli->tree, smbcli_open(cli->tree, BASEDIR "\\tname1", O_CREAT, 0));,
528                 smbcli_setatr(cli->tree, BASEDIR "\\tname1", FILE_ATTRIBUTE_NORMAL, 1000);,
529                 smbcli_unlink(cli->tree, BASEDIR "\\tname1");,
530                 NOTIFY_ACTION_MODIFIED,
531                 FILE_NOTIFY_CHANGE_LAST_WRITE, 1);
532
533         printf("testing set file attribute\n");
534         NOTIFY_MASK_TEST(
535                 fnum2 = create_complex_file(cli, mem_ctx, BASEDIR "\\tname1");,
536                 smbcli_fsetatr(cli->tree, fnum2, FILE_ATTRIBUTE_HIDDEN, 0, 0, 0, 0);,
537                 (smbcli_close(cli->tree, fnum2), smbcli_unlink(cli->tree, BASEDIR "\\tname1"));,
538                 NOTIFY_ACTION_MODIFIED,
539                 FILE_NOTIFY_CHANGE_ATTRIBUTES, 1);
540
541         printf("testing set file create time\n");
542         NOTIFY_MASK_TEST(
543                 fnum2 = create_complex_file(cli, mem_ctx, BASEDIR "\\tname1");,
544                 smbcli_fsetatr(cli->tree, fnum2, 0, t, 0, 0, 0);,
545                 (smbcli_close(cli->tree, fnum2), smbcli_unlink(cli->tree, BASEDIR "\\tname1"));,
546                 NOTIFY_ACTION_MODIFIED,
547                 FILE_NOTIFY_CHANGE_CREATION, 1);
548
549         printf("testing set file access time\n");
550         NOTIFY_MASK_TEST(
551                 fnum2 = create_complex_file(cli, mem_ctx, BASEDIR "\\tname1");,
552                 smbcli_fsetatr(cli->tree, fnum2, 0, 0, t, 0, 0);,
553                 (smbcli_close(cli->tree, fnum2), smbcli_unlink(cli->tree, BASEDIR "\\tname1"));,
554                 NOTIFY_ACTION_MODIFIED,
555                 FILE_NOTIFY_CHANGE_LAST_ACCESS, 1);
556
557         printf("testing set file write time\n");
558         NOTIFY_MASK_TEST(
559                 fnum2 = create_complex_file(cli, mem_ctx, BASEDIR "\\tname1");,
560                 smbcli_fsetatr(cli->tree, fnum2, 0, 0, 0, t, 0);,
561                 (smbcli_close(cli->tree, fnum2), smbcli_unlink(cli->tree, BASEDIR "\\tname1"));,
562                 NOTIFY_ACTION_MODIFIED,
563                 FILE_NOTIFY_CHANGE_LAST_WRITE, 1);
564
565         printf("testing set file change time\n");
566         NOTIFY_MASK_TEST(
567                 fnum2 = create_complex_file(cli, mem_ctx, BASEDIR "\\tname1");,
568                 smbcli_fsetatr(cli->tree, fnum2, 0, 0, 0, 0, t);,
569                 (smbcli_close(cli->tree, fnum2), smbcli_unlink(cli->tree, BASEDIR "\\tname1"));,
570                 NOTIFY_ACTION_MODIFIED,
571                 0, 1);
572
573
574         printf("testing write\n");
575         NOTIFY_MASK_TEST(
576                 fnum2 = create_complex_file(cli, mem_ctx, BASEDIR "\\tname1");,
577                 smbcli_write(cli->tree, fnum2, 1, &c, 10000, 1);,
578                 (smbcli_close(cli->tree, fnum2), smbcli_unlink(cli->tree, BASEDIR "\\tname1"));,
579                 NOTIFY_ACTION_MODIFIED,
580                 0, 1);
581
582         printf("testing truncate\n");
583         NOTIFY_MASK_TEST(
584                 fnum2 = create_complex_file(cli, mem_ctx, BASEDIR "\\tname1");,
585                 smbcli_ftruncate(cli->tree, fnum2, 10000);,
586                 (smbcli_close(cli->tree, fnum2), smbcli_unlink(cli->tree, BASEDIR "\\tname1"));,
587                 NOTIFY_ACTION_MODIFIED,
588                 FILE_NOTIFY_CHANGE_SIZE | FILE_NOTIFY_CHANGE_ATTRIBUTES, 1);
589
590 done:
591         smb_raw_exit(cli->session);
592         return ret;
593 }
594
595 /*
596   basic testing of change notify on files
597 */
598 static BOOL test_notify_file(struct smbcli_state *cli, TALLOC_CTX *mem_ctx)
599 {
600         NTSTATUS status;
601         BOOL ret = True;
602         union smb_open io;
603         union smb_close cl;
604         struct smb_notify notify;
605         struct smbcli_request *req;
606         int fnum;
607         const char *fname = BASEDIR "\\file.txt";
608
609         printf("TESTING CHANGE NOTIFY ON FILES\n");
610
611         io.generic.level = RAW_OPEN_NTCREATEX;
612         io.ntcreatex.in.root_fid = 0;
613         io.ntcreatex.in.flags = 0;
614         io.ntcreatex.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
615         io.ntcreatex.in.create_options = 0;
616         io.ntcreatex.in.file_attr = FILE_ATTRIBUTE_NORMAL;
617         io.ntcreatex.in.share_access = NTCREATEX_SHARE_ACCESS_READ | NTCREATEX_SHARE_ACCESS_WRITE;
618         io.ntcreatex.in.alloc_size = 0;
619         io.ntcreatex.in.open_disposition = NTCREATEX_DISP_CREATE;
620         io.ntcreatex.in.impersonation = NTCREATEX_IMPERSONATION_ANONYMOUS;
621         io.ntcreatex.in.security_flags = 0;
622         io.ntcreatex.in.fname = fname;
623         status = smb_raw_open(cli->tree, mem_ctx, &io);
624         CHECK_STATUS(status, NT_STATUS_OK);
625         fnum = io.ntcreatex.out.file.fnum;
626
627         /* ask for a change notify,
628            on file or directory name changes */
629         notify.in.file.fnum = fnum;
630         notify.in.buffer_size = 1000;
631         notify.in.completion_filter = FILE_NOTIFY_CHANGE_STREAM_NAME;
632         notify.in.recursive = False;
633
634         printf("testing if notifies on file handles are invalid (should be)\n");
635
636         req = smb_raw_changenotify_send(cli->tree, &notify);
637         status = smb_raw_changenotify_recv(req, mem_ctx, &notify);
638         CHECK_STATUS(status, NT_STATUS_INVALID_PARAMETER);
639
640         cl.close.level = RAW_CLOSE_CLOSE;
641         cl.close.in.file.fnum = fnum;
642         cl.close.in.write_time = 0;
643         status = smb_raw_close(cli->tree, &cl);
644         CHECK_STATUS(status, NT_STATUS_OK);
645
646         status = smbcli_unlink(cli->tree, fname);
647         CHECK_STATUS(status, NT_STATUS_OK);
648
649 done:
650         smb_raw_exit(cli->session);
651         return ret;
652 }
653
654 /*
655   basic testing of change notifies followed by a tdis
656 */
657 static BOOL test_notify_tdis(TALLOC_CTX *mem_ctx)
658 {
659         BOOL ret = True;
660         NTSTATUS status;
661         struct smb_notify notify;
662         union smb_open io;
663         int fnum;
664         struct smbcli_request *req;
665         struct smbcli_state *cli = NULL;
666
667         printf("TESTING CHANGE NOTIFY FOLLOWED BY TDIS\n");
668
669         if (!torture_open_connection(&cli, 0)) {
670                 return False;
671         }
672
673         /*
674           get a handle on the directory
675         */
676         io.generic.level = RAW_OPEN_NTCREATEX;
677         io.ntcreatex.in.root_fid = 0;
678         io.ntcreatex.in.flags = 0;
679         io.ntcreatex.in.access_mask = SEC_FILE_ALL;
680         io.ntcreatex.in.create_options = NTCREATEX_OPTIONS_DIRECTORY;
681         io.ntcreatex.in.file_attr = FILE_ATTRIBUTE_NORMAL;
682         io.ntcreatex.in.share_access = NTCREATEX_SHARE_ACCESS_READ | NTCREATEX_SHARE_ACCESS_WRITE;
683         io.ntcreatex.in.alloc_size = 0;
684         io.ntcreatex.in.open_disposition = NTCREATEX_DISP_OPEN;
685         io.ntcreatex.in.impersonation = NTCREATEX_IMPERSONATION_ANONYMOUS;
686         io.ntcreatex.in.security_flags = 0;
687         io.ntcreatex.in.fname = BASEDIR;
688
689         status = smb_raw_open(cli->tree, mem_ctx, &io);
690         CHECK_STATUS(status, NT_STATUS_OK);
691         fnum = io.ntcreatex.out.file.fnum;
692
693         /* ask for a change notify,
694            on file or directory name changes */
695         notify.in.buffer_size = 1000;
696         notify.in.completion_filter = FILE_NOTIFY_CHANGE_NAME;
697         notify.in.file.fnum = fnum;
698         notify.in.recursive = True;
699
700         req = smb_raw_changenotify_send(cli->tree, &notify);
701
702         status = smbcli_tdis(cli);
703         CHECK_STATUS(status, NT_STATUS_OK);
704
705         status = smb_raw_changenotify_recv(req, mem_ctx, &notify);
706         CHECK_STATUS(status, NT_STATUS_OK);
707         CHECK_VAL(notify.out.num_changes, 0);
708
709 done:
710         torture_close_connection(cli);
711         return ret;
712 }
713
714 /*
715   basic testing of change notifies followed by a exit
716 */
717 static BOOL test_notify_exit(TALLOC_CTX *mem_ctx)
718 {
719         BOOL ret = True;
720         NTSTATUS status;
721         struct smb_notify notify;
722         union smb_open io;
723         int fnum;
724         struct smbcli_request *req;
725         struct smbcli_state *cli = NULL;
726
727         printf("TESTING CHANGE NOTIFY FOLLOWED BY EXIT\n");
728
729         if (!torture_open_connection(&cli, 0)) {
730                 return False;
731         }
732
733         /*
734           get a handle on the directory
735         */
736         io.generic.level = RAW_OPEN_NTCREATEX;
737         io.ntcreatex.in.root_fid = 0;
738         io.ntcreatex.in.flags = 0;
739         io.ntcreatex.in.access_mask = SEC_FILE_ALL;
740         io.ntcreatex.in.create_options = NTCREATEX_OPTIONS_DIRECTORY;
741         io.ntcreatex.in.file_attr = FILE_ATTRIBUTE_NORMAL;
742         io.ntcreatex.in.share_access = NTCREATEX_SHARE_ACCESS_READ | NTCREATEX_SHARE_ACCESS_WRITE;
743         io.ntcreatex.in.alloc_size = 0;
744         io.ntcreatex.in.open_disposition = NTCREATEX_DISP_OPEN;
745         io.ntcreatex.in.impersonation = NTCREATEX_IMPERSONATION_ANONYMOUS;
746         io.ntcreatex.in.security_flags = 0;
747         io.ntcreatex.in.fname = BASEDIR;
748
749         status = smb_raw_open(cli->tree, mem_ctx, &io);
750         CHECK_STATUS(status, NT_STATUS_OK);
751         fnum = io.ntcreatex.out.file.fnum;
752
753         /* ask for a change notify,
754            on file or directory name changes */
755         notify.in.buffer_size = 1000;
756         notify.in.completion_filter = FILE_NOTIFY_CHANGE_NAME;
757         notify.in.file.fnum = fnum;
758         notify.in.recursive = True;
759
760         req = smb_raw_changenotify_send(cli->tree, &notify);
761
762         status = smb_raw_exit(cli->session);
763         CHECK_STATUS(status, NT_STATUS_OK);
764
765         status = smb_raw_changenotify_recv(req, mem_ctx, &notify);
766         CHECK_STATUS(status, NT_STATUS_OK);
767         CHECK_VAL(notify.out.num_changes, 0);
768
769 done:
770         torture_close_connection(cli);
771         return ret;
772 }
773
774 /*
775   basic testing of change notifies followed by a ulogoff
776 */
777 static BOOL test_notify_ulogoff(TALLOC_CTX *mem_ctx)
778 {
779         BOOL ret = True;
780         NTSTATUS status;
781         struct smb_notify notify;
782         union smb_open io;
783         int fnum;
784         struct smbcli_request *req;
785         struct smbcli_state *cli = NULL;
786
787         printf("TESTING CHANGE NOTIFY FOLLOWED BY ULOGOFF\n");
788
789         if (!torture_open_connection(&cli, 0)) {
790                 return False;
791         }
792
793         /*
794           get a handle on the directory
795         */
796         io.generic.level = RAW_OPEN_NTCREATEX;
797         io.ntcreatex.in.root_fid = 0;
798         io.ntcreatex.in.flags = 0;
799         io.ntcreatex.in.access_mask = SEC_FILE_ALL;
800         io.ntcreatex.in.create_options = NTCREATEX_OPTIONS_DIRECTORY;
801         io.ntcreatex.in.file_attr = FILE_ATTRIBUTE_NORMAL;
802         io.ntcreatex.in.share_access = NTCREATEX_SHARE_ACCESS_READ | NTCREATEX_SHARE_ACCESS_WRITE;
803         io.ntcreatex.in.alloc_size = 0;
804         io.ntcreatex.in.open_disposition = NTCREATEX_DISP_OPEN;
805         io.ntcreatex.in.impersonation = NTCREATEX_IMPERSONATION_ANONYMOUS;
806         io.ntcreatex.in.security_flags = 0;
807         io.ntcreatex.in.fname = BASEDIR;
808
809         status = smb_raw_open(cli->tree, mem_ctx, &io);
810         CHECK_STATUS(status, NT_STATUS_OK);
811         fnum = io.ntcreatex.out.file.fnum;
812
813         /* ask for a change notify,
814            on file or directory name changes */
815         notify.in.buffer_size = 1000;
816         notify.in.completion_filter = FILE_NOTIFY_CHANGE_NAME;
817         notify.in.file.fnum = fnum;
818         notify.in.recursive = True;
819
820         req = smb_raw_changenotify_send(cli->tree, &notify);
821
822         status = smb_raw_ulogoff(cli->session);
823         CHECK_STATUS(status, NT_STATUS_OK);
824
825         status = smb_raw_changenotify_recv(req, mem_ctx, &notify);
826         CHECK_STATUS(status, NT_STATUS_OK);
827         CHECK_VAL(notify.out.num_changes, 0);
828
829 done:
830         torture_close_connection(cli);
831         return ret;
832 }
833
834
835 /* 
836    test setting up two change notify requests on one handle
837 */
838 static BOOL test_notify_double(struct smbcli_state *cli, TALLOC_CTX *mem_ctx)
839 {
840         BOOL ret = True;
841         NTSTATUS status;
842         struct smb_notify notify;
843         union smb_open io;
844         int fnum;
845         struct smbcli_request *req1, *req2;
846
847         printf("TESTING CHANGE NOTIFY TWICE ON ONE DIRECTORY\n");
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.in.buffer_size = 1000;
872         notify.in.completion_filter = FILE_NOTIFY_CHANGE_NAME;
873         notify.in.file.fnum = fnum;
874         notify.in.recursive = True;
875
876         req1 = smb_raw_changenotify_send(cli->tree, &notify);
877         req2 = smb_raw_changenotify_send(cli->tree, &notify);
878
879         smbcli_mkdir(cli->tree, BASEDIR "\\subdir-name");
880
881         status = smb_raw_changenotify_recv(req1, mem_ctx, &notify);
882         CHECK_STATUS(status, NT_STATUS_OK);
883         CHECK_VAL(notify.out.num_changes, 1);
884         CHECK_WSTR(notify.out.changes[0].name, "subdir-name", STR_UNICODE);
885
886         smbcli_mkdir(cli->tree, BASEDIR "\\subdir-name2");
887
888         status = smb_raw_changenotify_recv(req2, mem_ctx, &notify);
889         CHECK_STATUS(status, NT_STATUS_OK);
890         CHECK_VAL(notify.out.num_changes, 1);
891         CHECK_WSTR(notify.out.changes[0].name, "subdir-name2", STR_UNICODE);
892
893 done:
894         smb_raw_exit(cli->session);
895         return ret;
896 }
897
898
899 /* 
900    test multiple change notifies at different depths and with/without recursion
901 */
902 static BOOL test_notify_tree(struct smbcli_state *cli, TALLOC_CTX *mem_ctx)
903 {
904         BOOL ret = True;
905         struct smb_notify notify;
906         union smb_open io;
907         struct smbcli_request *req;
908         struct {
909                 const char *path;
910                 BOOL recursive;
911                 uint32_t filter;
912                 int expected;
913                 int fnum;
914         } dirs[] = {
915                 {BASEDIR "\\abc",               True, FILE_NOTIFY_CHANGE_NAME, 30 },
916                 {BASEDIR "\\zqy",               True, FILE_NOTIFY_CHANGE_NAME, 8 },
917                 {BASEDIR "\\atsy",              True, FILE_NOTIFY_CHANGE_NAME, 4 },
918                 {BASEDIR "\\abc\\foo",          True,  FILE_NOTIFY_CHANGE_NAME, 2 },
919                 {BASEDIR "\\abc\\blah",         True,  FILE_NOTIFY_CHANGE_NAME, 13 },
920                 {BASEDIR "\\abc\\blah",         False, FILE_NOTIFY_CHANGE_NAME, 7 },
921                 {BASEDIR "\\abc\\blah\\a",      True, FILE_NOTIFY_CHANGE_NAME, 2 },
922                 {BASEDIR "\\abc\\blah\\b",      True, FILE_NOTIFY_CHANGE_NAME, 2 },
923                 {BASEDIR "\\abc\\blah\\c",      True, FILE_NOTIFY_CHANGE_NAME, 2 },
924                 {BASEDIR "\\abc\\fooblah",      True, FILE_NOTIFY_CHANGE_NAME, 2 },
925                 {BASEDIR "\\zqy\\xx",           True, FILE_NOTIFY_CHANGE_NAME, 2 },
926                 {BASEDIR "\\zqy\\yyy",          True, FILE_NOTIFY_CHANGE_NAME, 2 },
927                 {BASEDIR "\\zqy\\..",           True, FILE_NOTIFY_CHANGE_NAME, 40 },
928                 {BASEDIR,                       True, FILE_NOTIFY_CHANGE_NAME, 40 },
929                 {BASEDIR,                       False,FILE_NOTIFY_CHANGE_NAME, 6 },
930                 {BASEDIR "\\atsy",              False,FILE_NOTIFY_CHANGE_NAME, 4 },
931                 {BASEDIR "\\abc",               True, FILE_NOTIFY_CHANGE_NAME, 24 },
932                 {BASEDIR "\\abc",               False,FILE_NOTIFY_CHANGE_FILE_NAME, 0 },
933                 {BASEDIR "\\abc",               True, FILE_NOTIFY_CHANGE_FILE_NAME, 0 },
934                 {BASEDIR "\\abc",               True, FILE_NOTIFY_CHANGE_NAME, 24 },
935         };
936         int i;
937         NTSTATUS status;
938
939         printf("TESTING CHANGE NOTIFY FOR DIFFERENT DEPTHS\n");
940
941         io.generic.level = RAW_OPEN_NTCREATEX;
942         io.ntcreatex.in.root_fid = 0;
943         io.ntcreatex.in.flags = 0;
944         io.ntcreatex.in.access_mask = SEC_FILE_ALL;
945         io.ntcreatex.in.create_options = NTCREATEX_OPTIONS_DIRECTORY;
946         io.ntcreatex.in.file_attr = FILE_ATTRIBUTE_NORMAL;
947         io.ntcreatex.in.share_access = NTCREATEX_SHARE_ACCESS_READ | NTCREATEX_SHARE_ACCESS_WRITE;
948         io.ntcreatex.in.alloc_size = 0;
949         io.ntcreatex.in.open_disposition = NTCREATEX_DISP_OPEN_IF;
950         io.ntcreatex.in.impersonation = NTCREATEX_IMPERSONATION_ANONYMOUS;
951         io.ntcreatex.in.security_flags = 0;
952
953         notify.in.buffer_size = 20000;
954
955         /*
956           setup the directory tree, and the notify buffer on each directory
957         */
958         for (i=0;i<ARRAY_SIZE(dirs);i++) {
959                 io.ntcreatex.in.fname = dirs[i].path;
960                 status = smb_raw_open(cli->tree, mem_ctx, &io);
961                 CHECK_STATUS(status, NT_STATUS_OK);
962                 dirs[i].fnum = io.ntcreatex.out.file.fnum;
963
964                 notify.in.completion_filter = dirs[i].filter;
965                 notify.in.file.fnum = dirs[i].fnum;
966                 notify.in.recursive = dirs[i].recursive;
967                 req = smb_raw_changenotify_send(cli->tree, &notify);
968                 smb_raw_ntcancel(req);
969                 status = smb_raw_changenotify_recv(req, mem_ctx, &notify);
970                 CHECK_STATUS(status, NT_STATUS_CANCELLED);
971         }
972
973         /* trigger 2 events in each dir */
974         for (i=0;i<ARRAY_SIZE(dirs);i++) {
975                 char *path = talloc_asprintf(mem_ctx, "%s\\test.dir", dirs[i].path);
976                 smbcli_mkdir(cli->tree, path);
977                 smbcli_rmdir(cli->tree, path);
978                 talloc_free(path);
979         }
980
981         /* give a bit of time for all the events to propogate */
982         sleep(2);
983
984         /* count events that have happened in each dir */
985         for (i=0;i<ARRAY_SIZE(dirs);i++) {
986                 notify.in.file.fnum = dirs[i].fnum;
987                 req = smb_raw_changenotify_send(cli->tree, &notify);
988                 smb_raw_ntcancel(req);
989                 notify.out.num_changes = 0;
990                 status = smb_raw_changenotify_recv(req, mem_ctx, &notify);
991                 if (notify.out.num_changes != dirs[i].expected) {
992                         printf("ERROR: i=%d expected %d got %d for '%s'\n",
993                                i, dirs[i].expected, notify.out.num_changes,
994                                dirs[i].path);
995                         ret = False;
996                 }
997         }
998
999         /*
1000           run from the back, closing and deleting
1001         */
1002         for (i=ARRAY_SIZE(dirs)-1;i>=0;i--) {
1003                 smbcli_close(cli->tree, dirs[i].fnum);
1004                 smbcli_rmdir(cli->tree, dirs[i].path);
1005         }
1006
1007 done:
1008         smb_raw_exit(cli->session);
1009         return ret;
1010 }
1011
1012 /* 
1013    basic testing of change notify
1014 */
1015 BOOL torture_raw_notify(struct torture_context *torture)
1016 {
1017         struct smbcli_state *cli;
1018         BOOL ret = True;
1019         TALLOC_CTX *mem_ctx;
1020                 
1021         if (!torture_open_connection(&cli, 0)) {
1022                 return False;
1023         }
1024
1025         mem_ctx = talloc_init("torture_raw_notify");
1026
1027         if (!torture_setup_dir(cli, BASEDIR)) {
1028                 return False;
1029         }
1030
1031         ret &= test_notify_dir(cli, mem_ctx);
1032         ret &= test_notify_mask(cli, mem_ctx);
1033         ret &= test_notify_recursive(cli, mem_ctx);
1034         ret &= test_notify_file(cli, mem_ctx);
1035         ret &= test_notify_tdis(mem_ctx);
1036         ret &= test_notify_exit(mem_ctx);
1037         ret &= test_notify_ulogoff(mem_ctx);
1038         ret &= test_notify_double(cli, mem_ctx);
1039         ret &= test_notify_tree(cli, mem_ctx);
1040
1041         smb_raw_exit(cli->session);
1042         smbcli_deltree(cli->tree, BASEDIR);
1043         torture_close_connection(cli);
1044         talloc_free(mem_ctx);
1045         return ret;
1046 }