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