r17015: add a test that closes the tcp connection with an outstanding
[kai/samba-autobuild/.git] / source4 / torture / raw / notify.c
1 /* 
2    Unix SMB/CIFS implementation.
3    basic raw test suite for change notify
4    Copyright (C) Andrew Tridgell 2003
5    
6    This program is free software; you can redistribute it and/or modify
7    it under the terms of the GNU General Public License as published by
8    the Free Software Foundation; either version 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(1);
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(10);
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(10); 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         printf("testing set file create time\n");
545         NOTIFY_MASK_TEST(
546                 fnum2 = create_complex_file(cli, mem_ctx, BASEDIR "\\tname1");,
547                 smbcli_fsetatr(cli->tree, fnum2, 0, t, 0, 0, 0);,
548                 (smbcli_close(cli->tree, fnum2), smbcli_unlink(cli->tree, BASEDIR "\\tname1"));,
549                 NOTIFY_ACTION_MODIFIED,
550                 FILE_NOTIFY_CHANGE_CREATION, 1);
551
552         printf("testing set file access time\n");
553         NOTIFY_MASK_TEST(
554                 fnum2 = create_complex_file(cli, mem_ctx, BASEDIR "\\tname1");,
555                 smbcli_fsetatr(cli->tree, fnum2, 0, 0, t, 0, 0);,
556                 (smbcli_close(cli->tree, fnum2), smbcli_unlink(cli->tree, BASEDIR "\\tname1"));,
557                 NOTIFY_ACTION_MODIFIED,
558                 FILE_NOTIFY_CHANGE_LAST_ACCESS, 1);
559
560         printf("testing set file write time\n");
561         NOTIFY_MASK_TEST(
562                 fnum2 = create_complex_file(cli, mem_ctx, BASEDIR "\\tname1");,
563                 smbcli_fsetatr(cli->tree, fnum2, 0, 0, 0, t, 0);,
564                 (smbcli_close(cli->tree, fnum2), smbcli_unlink(cli->tree, BASEDIR "\\tname1"));,
565                 NOTIFY_ACTION_MODIFIED,
566                 FILE_NOTIFY_CHANGE_LAST_WRITE, 1);
567
568         printf("testing set file change 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, 0, t);,
572                 (smbcli_close(cli->tree, fnum2), smbcli_unlink(cli->tree, BASEDIR "\\tname1"));,
573                 NOTIFY_ACTION_MODIFIED,
574                 0, 1);
575
576
577         printf("testing write\n");
578         NOTIFY_MASK_TEST(
579                 fnum2 = create_complex_file(cli, mem_ctx, BASEDIR "\\tname1");,
580                 smbcli_write(cli->tree, fnum2, 1, &c, 10000, 1);,
581                 (smbcli_close(cli->tree, fnum2), smbcli_unlink(cli->tree, BASEDIR "\\tname1"));,
582                 NOTIFY_ACTION_MODIFIED,
583                 0, 1);
584
585         printf("testing truncate\n");
586         NOTIFY_MASK_TEST(
587                 fnum2 = create_complex_file(cli, mem_ctx, BASEDIR "\\tname1");,
588                 smbcli_ftruncate(cli->tree, fnum2, 10000);,
589                 (smbcli_close(cli->tree, fnum2), smbcli_unlink(cli->tree, BASEDIR "\\tname1"));,
590                 NOTIFY_ACTION_MODIFIED,
591                 FILE_NOTIFY_CHANGE_SIZE | FILE_NOTIFY_CHANGE_ATTRIBUTES, 1);
592
593 done:
594         smb_raw_exit(cli->session);
595         return ret;
596 }
597
598 /*
599   basic testing of change notify on files
600 */
601 static BOOL test_notify_file(struct smbcli_state *cli, TALLOC_CTX *mem_ctx)
602 {
603         NTSTATUS status;
604         BOOL ret = True;
605         union smb_open io;
606         union smb_close cl;
607         union smb_notify notify;
608         struct smbcli_request *req;
609         int fnum;
610         const char *fname = BASEDIR "\\file.txt";
611
612         printf("TESTING CHANGE NOTIFY ON FILES\n");
613
614         io.generic.level = RAW_OPEN_NTCREATEX;
615         io.ntcreatex.in.root_fid = 0;
616         io.ntcreatex.in.flags = 0;
617         io.ntcreatex.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
618         io.ntcreatex.in.create_options = 0;
619         io.ntcreatex.in.file_attr = FILE_ATTRIBUTE_NORMAL;
620         io.ntcreatex.in.share_access = NTCREATEX_SHARE_ACCESS_READ | NTCREATEX_SHARE_ACCESS_WRITE;
621         io.ntcreatex.in.alloc_size = 0;
622         io.ntcreatex.in.open_disposition = NTCREATEX_DISP_CREATE;
623         io.ntcreatex.in.impersonation = NTCREATEX_IMPERSONATION_ANONYMOUS;
624         io.ntcreatex.in.security_flags = 0;
625         io.ntcreatex.in.fname = fname;
626         status = smb_raw_open(cli->tree, mem_ctx, &io);
627         CHECK_STATUS(status, NT_STATUS_OK);
628         fnum = io.ntcreatex.out.file.fnum;
629
630         /* ask for a change notify,
631            on file or directory name changes */
632         notify.nttrans.level = RAW_NOTIFY_NTTRANS;
633         notify.nttrans.in.file.fnum = fnum;
634         notify.nttrans.in.buffer_size = 1000;
635         notify.nttrans.in.completion_filter = FILE_NOTIFY_CHANGE_STREAM_NAME;
636         notify.nttrans.in.recursive = False;
637
638         printf("testing if notifies on file handles are invalid (should be)\n");
639
640         req = smb_raw_changenotify_send(cli->tree, &notify);
641         status = smb_raw_changenotify_recv(req, mem_ctx, &notify);
642         CHECK_STATUS(status, NT_STATUS_INVALID_PARAMETER);
643
644         cl.close.level = RAW_CLOSE_CLOSE;
645         cl.close.in.file.fnum = fnum;
646         cl.close.in.write_time = 0;
647         status = smb_raw_close(cli->tree, &cl);
648         CHECK_STATUS(status, NT_STATUS_OK);
649
650         status = smbcli_unlink(cli->tree, fname);
651         CHECK_STATUS(status, NT_STATUS_OK);
652
653 done:
654         smb_raw_exit(cli->session);
655         return ret;
656 }
657
658 /*
659   basic testing of change notifies followed by a tdis
660 */
661 static BOOL test_notify_tdis(TALLOC_CTX *mem_ctx)
662 {
663         BOOL ret = True;
664         NTSTATUS status;
665         union smb_notify notify;
666         union smb_open io;
667         int fnum;
668         struct smbcli_request *req;
669         struct smbcli_state *cli = NULL;
670
671         printf("TESTING CHANGE NOTIFY FOLLOWED BY TDIS\n");
672
673         if (!torture_open_connection(&cli, 0)) {
674                 return False;
675         }
676
677         /*
678           get a handle on the directory
679         */
680         io.generic.level = RAW_OPEN_NTCREATEX;
681         io.ntcreatex.in.root_fid = 0;
682         io.ntcreatex.in.flags = 0;
683         io.ntcreatex.in.access_mask = SEC_FILE_ALL;
684         io.ntcreatex.in.create_options = NTCREATEX_OPTIONS_DIRECTORY;
685         io.ntcreatex.in.file_attr = FILE_ATTRIBUTE_NORMAL;
686         io.ntcreatex.in.share_access = NTCREATEX_SHARE_ACCESS_READ | NTCREATEX_SHARE_ACCESS_WRITE;
687         io.ntcreatex.in.alloc_size = 0;
688         io.ntcreatex.in.open_disposition = NTCREATEX_DISP_OPEN;
689         io.ntcreatex.in.impersonation = NTCREATEX_IMPERSONATION_ANONYMOUS;
690         io.ntcreatex.in.security_flags = 0;
691         io.ntcreatex.in.fname = BASEDIR;
692
693         status = smb_raw_open(cli->tree, mem_ctx, &io);
694         CHECK_STATUS(status, NT_STATUS_OK);
695         fnum = io.ntcreatex.out.file.fnum;
696
697         /* ask for a change notify,
698            on file or directory name changes */
699         notify.nttrans.level = RAW_NOTIFY_NTTRANS;
700         notify.nttrans.in.buffer_size = 1000;
701         notify.nttrans.in.completion_filter = FILE_NOTIFY_CHANGE_NAME;
702         notify.nttrans.in.file.fnum = fnum;
703         notify.nttrans.in.recursive = True;
704
705         req = smb_raw_changenotify_send(cli->tree, &notify);
706
707         status = smbcli_tdis(cli);
708         CHECK_STATUS(status, NT_STATUS_OK);
709         cli->tree = NULL;
710
711         status = smb_raw_changenotify_recv(req, mem_ctx, &notify);
712         CHECK_STATUS(status, NT_STATUS_OK);
713         CHECK_VAL(notify.nttrans.out.num_changes, 0);
714
715 done:
716         torture_close_connection(cli);
717         return ret;
718 }
719
720 /*
721   basic testing of change notifies followed by a exit
722 */
723 static BOOL test_notify_exit(TALLOC_CTX *mem_ctx)
724 {
725         BOOL ret = True;
726         NTSTATUS status;
727         union smb_notify notify;
728         union smb_open io;
729         int fnum;
730         struct smbcli_request *req;
731         struct smbcli_state *cli = NULL;
732
733         printf("TESTING CHANGE NOTIFY FOLLOWED BY EXIT\n");
734
735         if (!torture_open_connection(&cli, 0)) {
736                 return False;
737         }
738
739         /*
740           get a handle on the directory
741         */
742         io.generic.level = RAW_OPEN_NTCREATEX;
743         io.ntcreatex.in.root_fid = 0;
744         io.ntcreatex.in.flags = 0;
745         io.ntcreatex.in.access_mask = SEC_FILE_ALL;
746         io.ntcreatex.in.create_options = NTCREATEX_OPTIONS_DIRECTORY;
747         io.ntcreatex.in.file_attr = FILE_ATTRIBUTE_NORMAL;
748         io.ntcreatex.in.share_access = NTCREATEX_SHARE_ACCESS_READ | NTCREATEX_SHARE_ACCESS_WRITE;
749         io.ntcreatex.in.alloc_size = 0;
750         io.ntcreatex.in.open_disposition = NTCREATEX_DISP_OPEN;
751         io.ntcreatex.in.impersonation = NTCREATEX_IMPERSONATION_ANONYMOUS;
752         io.ntcreatex.in.security_flags = 0;
753         io.ntcreatex.in.fname = BASEDIR;
754
755         status = smb_raw_open(cli->tree, mem_ctx, &io);
756         CHECK_STATUS(status, NT_STATUS_OK);
757         fnum = io.ntcreatex.out.file.fnum;
758
759         /* ask for a change notify,
760            on file or directory name changes */
761         notify.nttrans.level = RAW_NOTIFY_NTTRANS;
762         notify.nttrans.in.buffer_size = 1000;
763         notify.nttrans.in.completion_filter = FILE_NOTIFY_CHANGE_NAME;
764         notify.nttrans.in.file.fnum = fnum;
765         notify.nttrans.in.recursive = True;
766
767         req = smb_raw_changenotify_send(cli->tree, &notify);
768
769         status = smb_raw_exit(cli->session);
770         CHECK_STATUS(status, NT_STATUS_OK);
771
772         status = smb_raw_changenotify_recv(req, mem_ctx, &notify);
773         CHECK_STATUS(status, NT_STATUS_OK);
774         CHECK_VAL(notify.nttrans.out.num_changes, 0);
775
776 done:
777         torture_close_connection(cli);
778         return ret;
779 }
780
781 /*
782   basic testing of change notifies followed by a ulogoff
783 */
784 static BOOL test_notify_ulogoff(TALLOC_CTX *mem_ctx)
785 {
786         BOOL ret = True;
787         NTSTATUS status;
788         union smb_notify notify;
789         union smb_open io;
790         int fnum;
791         struct smbcli_request *req;
792         struct smbcli_state *cli = NULL;
793
794         printf("TESTING CHANGE NOTIFY FOLLOWED BY ULOGOFF\n");
795
796         if (!torture_open_connection(&cli, 0)) {
797                 return False;
798         }
799
800         /*
801           get a handle on the directory
802         */
803         io.generic.level = RAW_OPEN_NTCREATEX;
804         io.ntcreatex.in.root_fid = 0;
805         io.ntcreatex.in.flags = 0;
806         io.ntcreatex.in.access_mask = SEC_FILE_ALL;
807         io.ntcreatex.in.create_options = NTCREATEX_OPTIONS_DIRECTORY;
808         io.ntcreatex.in.file_attr = FILE_ATTRIBUTE_NORMAL;
809         io.ntcreatex.in.share_access = NTCREATEX_SHARE_ACCESS_READ | NTCREATEX_SHARE_ACCESS_WRITE;
810         io.ntcreatex.in.alloc_size = 0;
811         io.ntcreatex.in.open_disposition = NTCREATEX_DISP_OPEN;
812         io.ntcreatex.in.impersonation = NTCREATEX_IMPERSONATION_ANONYMOUS;
813         io.ntcreatex.in.security_flags = 0;
814         io.ntcreatex.in.fname = BASEDIR;
815
816         status = smb_raw_open(cli->tree, mem_ctx, &io);
817         CHECK_STATUS(status, NT_STATUS_OK);
818         fnum = io.ntcreatex.out.file.fnum;
819
820         /* ask for a change notify,
821            on file or directory name changes */
822         notify.nttrans.level = RAW_NOTIFY_NTTRANS;
823         notify.nttrans.in.buffer_size = 1000;
824         notify.nttrans.in.completion_filter = FILE_NOTIFY_CHANGE_NAME;
825         notify.nttrans.in.file.fnum = fnum;
826         notify.nttrans.in.recursive = True;
827
828         req = smb_raw_changenotify_send(cli->tree, &notify);
829
830         status = smb_raw_ulogoff(cli->session);
831         CHECK_STATUS(status, NT_STATUS_OK);
832
833         status = smb_raw_changenotify_recv(req, mem_ctx, &notify);
834         CHECK_STATUS(status, NT_STATUS_OK);
835         CHECK_VAL(notify.nttrans.out.num_changes, 0);
836
837 done:
838         torture_close_connection(cli);
839         return ret;
840 }
841
842 static void tcp_dis_handler(struct smbcli_transport *t, void *p)
843 {
844         struct smbcli_state *cli = p;
845         smbcli_transport_dead(cli->transport);
846         cli->transport = NULL;
847         cli->tree = NULL;
848 }
849 /*
850   basic testing of change notifies followed by tcp disconnect
851 */
852 static BOOL test_notify_tcp_dis(TALLOC_CTX *mem_ctx)
853 {
854         BOOL ret = True;
855         NTSTATUS status;
856         union smb_notify notify;
857         union smb_open io;
858         int fnum;
859         struct smbcli_request *req;
860         struct smbcli_state *cli = NULL;
861
862         printf("TESTING CHANGE NOTIFY FOLLOWED BY TCP DISCONNECT\n");
863
864         if (!torture_open_connection(&cli, 0)) {
865                 return False;
866         }
867
868         /*
869           get a handle on the directory
870         */
871         io.generic.level = RAW_OPEN_NTCREATEX;
872         io.ntcreatex.in.root_fid = 0;
873         io.ntcreatex.in.flags = 0;
874         io.ntcreatex.in.access_mask = SEC_FILE_ALL;
875         io.ntcreatex.in.create_options = NTCREATEX_OPTIONS_DIRECTORY;
876         io.ntcreatex.in.file_attr = FILE_ATTRIBUTE_NORMAL;
877         io.ntcreatex.in.share_access = NTCREATEX_SHARE_ACCESS_READ | NTCREATEX_SHARE_ACCESS_WRITE;
878         io.ntcreatex.in.alloc_size = 0;
879         io.ntcreatex.in.open_disposition = NTCREATEX_DISP_OPEN;
880         io.ntcreatex.in.impersonation = NTCREATEX_IMPERSONATION_ANONYMOUS;
881         io.ntcreatex.in.security_flags = 0;
882         io.ntcreatex.in.fname = BASEDIR;
883
884         status = smb_raw_open(cli->tree, mem_ctx, &io);
885         CHECK_STATUS(status, NT_STATUS_OK);
886         fnum = io.ntcreatex.out.file.fnum;
887
888         /* ask for a change notify,
889            on file or directory name changes */
890         notify.nttrans.level = RAW_NOTIFY_NTTRANS;
891         notify.nttrans.in.buffer_size = 1000;
892         notify.nttrans.in.completion_filter = FILE_NOTIFY_CHANGE_NAME;
893         notify.nttrans.in.file.fnum = fnum;
894         notify.nttrans.in.recursive = True;
895
896         req = smb_raw_changenotify_send(cli->tree, &notify);
897
898         smbcli_transport_idle_handler(cli->transport, tcp_dis_handler, 250, cli);
899
900         status = smb_raw_changenotify_recv(req, mem_ctx, &notify);
901         CHECK_STATUS(status, NT_STATUS_NET_WRITE_FAULT);
902
903 done:
904         torture_close_connection(cli);
905         return ret;
906 }
907
908 /* 
909    test setting up two change notify requests on one handle
910 */
911 static BOOL test_notify_double(struct smbcli_state *cli, TALLOC_CTX *mem_ctx)
912 {
913         BOOL ret = True;
914         NTSTATUS status;
915         union smb_notify notify;
916         union smb_open io;
917         int fnum;
918         struct smbcli_request *req1, *req2;
919
920         printf("TESTING CHANGE NOTIFY TWICE ON ONE DIRECTORY\n");
921                 
922         /*
923           get a handle on the directory
924         */
925         io.generic.level = RAW_OPEN_NTCREATEX;
926         io.ntcreatex.in.root_fid = 0;
927         io.ntcreatex.in.flags = 0;
928         io.ntcreatex.in.access_mask = SEC_FILE_ALL;
929         io.ntcreatex.in.create_options = NTCREATEX_OPTIONS_DIRECTORY;
930         io.ntcreatex.in.file_attr = FILE_ATTRIBUTE_NORMAL;
931         io.ntcreatex.in.share_access = NTCREATEX_SHARE_ACCESS_READ | NTCREATEX_SHARE_ACCESS_WRITE;
932         io.ntcreatex.in.alloc_size = 0;
933         io.ntcreatex.in.open_disposition = NTCREATEX_DISP_OPEN;
934         io.ntcreatex.in.impersonation = NTCREATEX_IMPERSONATION_ANONYMOUS;
935         io.ntcreatex.in.security_flags = 0;
936         io.ntcreatex.in.fname = BASEDIR;
937
938         status = smb_raw_open(cli->tree, mem_ctx, &io);
939         CHECK_STATUS(status, NT_STATUS_OK);
940         fnum = io.ntcreatex.out.file.fnum;
941
942         /* ask for a change notify,
943            on file or directory name changes */
944         notify.nttrans.level = RAW_NOTIFY_NTTRANS;
945         notify.nttrans.in.buffer_size = 1000;
946         notify.nttrans.in.completion_filter = FILE_NOTIFY_CHANGE_NAME;
947         notify.nttrans.in.file.fnum = fnum;
948         notify.nttrans.in.recursive = True;
949
950         req1 = smb_raw_changenotify_send(cli->tree, &notify);
951         req2 = smb_raw_changenotify_send(cli->tree, &notify);
952
953         smbcli_mkdir(cli->tree, BASEDIR "\\subdir-name");
954
955         status = smb_raw_changenotify_recv(req1, mem_ctx, &notify);
956         CHECK_STATUS(status, NT_STATUS_OK);
957         CHECK_VAL(notify.nttrans.out.num_changes, 1);
958         CHECK_WSTR(notify.nttrans.out.changes[0].name, "subdir-name", STR_UNICODE);
959
960         smbcli_mkdir(cli->tree, BASEDIR "\\subdir-name2");
961
962         status = smb_raw_changenotify_recv(req2, mem_ctx, &notify);
963         CHECK_STATUS(status, NT_STATUS_OK);
964         CHECK_VAL(notify.nttrans.out.num_changes, 1);
965         CHECK_WSTR(notify.nttrans.out.changes[0].name, "subdir-name2", STR_UNICODE);
966
967 done:
968         smb_raw_exit(cli->session);
969         return ret;
970 }
971
972
973 /* 
974    test multiple change notifies at different depths and with/without recursion
975 */
976 static BOOL test_notify_tree(struct smbcli_state *cli, TALLOC_CTX *mem_ctx)
977 {
978         BOOL ret = True;
979         union smb_notify notify;
980         union smb_open io;
981         struct smbcli_request *req;
982         struct {
983                 const char *path;
984                 BOOL recursive;
985                 uint32_t filter;
986                 int expected;
987                 int fnum;
988         } dirs[] = {
989                 {BASEDIR "\\abc",               True, FILE_NOTIFY_CHANGE_NAME, 30 },
990                 {BASEDIR "\\zqy",               True, FILE_NOTIFY_CHANGE_NAME, 8 },
991                 {BASEDIR "\\atsy",              True, FILE_NOTIFY_CHANGE_NAME, 4 },
992                 {BASEDIR "\\abc\\foo",          True,  FILE_NOTIFY_CHANGE_NAME, 2 },
993                 {BASEDIR "\\abc\\blah",         True,  FILE_NOTIFY_CHANGE_NAME, 13 },
994                 {BASEDIR "\\abc\\blah",         False, FILE_NOTIFY_CHANGE_NAME, 7 },
995                 {BASEDIR "\\abc\\blah\\a",      True, FILE_NOTIFY_CHANGE_NAME, 2 },
996                 {BASEDIR "\\abc\\blah\\b",      True, FILE_NOTIFY_CHANGE_NAME, 2 },
997                 {BASEDIR "\\abc\\blah\\c",      True, FILE_NOTIFY_CHANGE_NAME, 2 },
998                 {BASEDIR "\\abc\\fooblah",      True, FILE_NOTIFY_CHANGE_NAME, 2 },
999                 {BASEDIR "\\zqy\\xx",           True, FILE_NOTIFY_CHANGE_NAME, 2 },
1000                 {BASEDIR "\\zqy\\yyy",          True, FILE_NOTIFY_CHANGE_NAME, 2 },
1001                 {BASEDIR "\\zqy\\..",           True, FILE_NOTIFY_CHANGE_NAME, 40 },
1002                 {BASEDIR,                       True, FILE_NOTIFY_CHANGE_NAME, 40 },
1003                 {BASEDIR,                       False,FILE_NOTIFY_CHANGE_NAME, 6 },
1004                 {BASEDIR "\\atsy",              False,FILE_NOTIFY_CHANGE_NAME, 4 },
1005                 {BASEDIR "\\abc",               True, FILE_NOTIFY_CHANGE_NAME, 24 },
1006                 {BASEDIR "\\abc",               False,FILE_NOTIFY_CHANGE_FILE_NAME, 0 },
1007                 {BASEDIR "\\abc",               True, FILE_NOTIFY_CHANGE_FILE_NAME, 0 },
1008                 {BASEDIR "\\abc",               True, FILE_NOTIFY_CHANGE_NAME, 24 },
1009         };
1010         int i;
1011         NTSTATUS status;
1012
1013         printf("TESTING CHANGE NOTIFY FOR DIFFERENT DEPTHS\n");
1014
1015         io.generic.level = RAW_OPEN_NTCREATEX;
1016         io.ntcreatex.in.root_fid = 0;
1017         io.ntcreatex.in.flags = 0;
1018         io.ntcreatex.in.access_mask = SEC_FILE_ALL;
1019         io.ntcreatex.in.create_options = NTCREATEX_OPTIONS_DIRECTORY;
1020         io.ntcreatex.in.file_attr = FILE_ATTRIBUTE_NORMAL;
1021         io.ntcreatex.in.share_access = NTCREATEX_SHARE_ACCESS_READ | NTCREATEX_SHARE_ACCESS_WRITE;
1022         io.ntcreatex.in.alloc_size = 0;
1023         io.ntcreatex.in.open_disposition = NTCREATEX_DISP_OPEN_IF;
1024         io.ntcreatex.in.impersonation = NTCREATEX_IMPERSONATION_ANONYMOUS;
1025         io.ntcreatex.in.security_flags = 0;
1026
1027         notify.nttrans.level = RAW_NOTIFY_NTTRANS;
1028         notify.nttrans.in.buffer_size = 20000;
1029
1030         /*
1031           setup the directory tree, and the notify buffer on each directory
1032         */
1033         for (i=0;i<ARRAY_SIZE(dirs);i++) {
1034                 io.ntcreatex.in.fname = dirs[i].path;
1035                 status = smb_raw_open(cli->tree, mem_ctx, &io);
1036                 CHECK_STATUS(status, NT_STATUS_OK);
1037                 dirs[i].fnum = io.ntcreatex.out.file.fnum;
1038
1039                 notify.nttrans.in.completion_filter = dirs[i].filter;
1040                 notify.nttrans.in.file.fnum = dirs[i].fnum;
1041                 notify.nttrans.in.recursive = dirs[i].recursive;
1042                 req = smb_raw_changenotify_send(cli->tree, &notify);
1043                 smb_raw_ntcancel(req);
1044                 status = smb_raw_changenotify_recv(req, mem_ctx, &notify);
1045                 CHECK_STATUS(status, NT_STATUS_CANCELLED);
1046         }
1047
1048         /* trigger 2 events in each dir */
1049         for (i=0;i<ARRAY_SIZE(dirs);i++) {
1050                 char *path = talloc_asprintf(mem_ctx, "%s\\test.dir", dirs[i].path);
1051                 smbcli_mkdir(cli->tree, path);
1052                 smbcli_rmdir(cli->tree, path);
1053                 talloc_free(path);
1054         }
1055
1056         /* give a bit of time for all the events to propogate */
1057         sleep(2);
1058
1059         /* count events that have happened in each dir */
1060         for (i=0;i<ARRAY_SIZE(dirs);i++) {
1061                 notify.nttrans.in.file.fnum = dirs[i].fnum;
1062                 req = smb_raw_changenotify_send(cli->tree, &notify);
1063                 smb_raw_ntcancel(req);
1064                 notify.nttrans.out.num_changes = 0;
1065                 status = smb_raw_changenotify_recv(req, mem_ctx, &notify);
1066                 if (notify.nttrans.out.num_changes != dirs[i].expected) {
1067                         printf("ERROR: i=%d expected %d got %d for '%s'\n",
1068                                i, dirs[i].expected, notify.nttrans.out.num_changes,
1069                                dirs[i].path);
1070                         ret = False;
1071                 }
1072         }
1073
1074         /*
1075           run from the back, closing and deleting
1076         */
1077         for (i=ARRAY_SIZE(dirs)-1;i>=0;i--) {
1078                 smbcli_close(cli->tree, dirs[i].fnum);
1079                 smbcli_rmdir(cli->tree, dirs[i].path);
1080         }
1081
1082 done:
1083         smb_raw_exit(cli->session);
1084         return ret;
1085 }
1086
1087 /* 
1088    basic testing of change notify
1089 */
1090 BOOL torture_raw_notify(struct torture_context *torture)
1091 {
1092         struct smbcli_state *cli;
1093         BOOL ret = True;
1094         TALLOC_CTX *mem_ctx;
1095                 
1096         if (!torture_open_connection(&cli, 0)) {
1097                 return False;
1098         }
1099
1100         mem_ctx = talloc_init("torture_raw_notify");
1101
1102         if (!torture_setup_dir(cli, BASEDIR)) {
1103                 return False;
1104         }
1105
1106         ret &= test_notify_dir(cli, mem_ctx);
1107         ret &= test_notify_mask(cli, mem_ctx);
1108         ret &= test_notify_recursive(cli, mem_ctx);
1109         ret &= test_notify_file(cli, mem_ctx);
1110         ret &= test_notify_tdis(mem_ctx);
1111         ret &= test_notify_exit(mem_ctx);
1112         ret &= test_notify_ulogoff(mem_ctx);
1113         ret &= test_notify_tcp_dis(mem_ctx);
1114         ret &= test_notify_double(cli, mem_ctx);
1115         ret &= test_notify_tree(cli, mem_ctx);
1116
1117         smb_raw_exit(cli->session);
1118         smbcli_deltree(cli->tree, BASEDIR);
1119         torture_close_connection(cli);
1120         talloc_free(mem_ctx);
1121         return ret;
1122 }