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