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