6c4ca8266880a91ea72aeca339cb3e8a7143daa9
[ddiss/samba.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 "libcli/raw/libcliraw.h"
22 #include "libcli/raw/raw_proto.h"
23 #include "libcli/libcli.h"
24 #include "system/filesys.h"
25 #include "torture/util.h"
26 #include "torture/raw/proto.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 #define CHECK_WSTR2(tctx, field, value, flags) \
55 do { \
56         if (!field.s || strcmp(field.s, value) || \
57             wire_bad_flags(&field, flags, cli->transport)) { \
58                 torture_result(tctx, TORTURE_FAIL, \
59                     "(%d) %s [%s] != %s\n",  __LINE__, #field, field.s, value); \
60         } \
61 } while (0)
62
63 /* 
64    basic testing of change notify on directories
65 */
66 static bool test_notify_dir(struct smbcli_state *cli, struct smbcli_state *cli2, 
67                             TALLOC_CTX *mem_ctx)
68 {
69         bool ret = true;
70         NTSTATUS status;
71         union smb_notify notify;
72         union smb_open io;
73         union smb_close cl;
74         int i, count, fnum, fnum2;
75         struct smbcli_request *req, *req2;
76         extern int torture_numops;
77
78         printf("TESTING CHANGE NOTIFY ON DIRECTORIES\n");
79                 
80         /*
81           get a handle on the directory
82         */
83         io.generic.level = RAW_OPEN_NTCREATEX;
84         io.ntcreatex.in.root_fid.fnum = 0;
85         io.ntcreatex.in.flags = 0;
86         io.ntcreatex.in.access_mask = SEC_FILE_ALL;
87         io.ntcreatex.in.create_options = NTCREATEX_OPTIONS_DIRECTORY;
88         io.ntcreatex.in.file_attr = FILE_ATTRIBUTE_NORMAL;
89         io.ntcreatex.in.share_access = NTCREATEX_SHARE_ACCESS_READ | NTCREATEX_SHARE_ACCESS_WRITE;
90         io.ntcreatex.in.alloc_size = 0;
91         io.ntcreatex.in.open_disposition = NTCREATEX_DISP_OPEN;
92         io.ntcreatex.in.impersonation = NTCREATEX_IMPERSONATION_ANONYMOUS;
93         io.ntcreatex.in.security_flags = 0;
94         io.ntcreatex.in.fname = BASEDIR;
95
96         status = smb_raw_open(cli->tree, mem_ctx, &io);
97         CHECK_STATUS(status, NT_STATUS_OK);
98         fnum = io.ntcreatex.out.file.fnum;
99
100         status = smb_raw_open(cli->tree, mem_ctx, &io);
101         CHECK_STATUS(status, NT_STATUS_OK);
102         fnum2 = io.ntcreatex.out.file.fnum;
103
104         /* ask for a change notify,
105            on file or directory name changes */
106         notify.nttrans.level = RAW_NOTIFY_NTTRANS;
107         notify.nttrans.in.buffer_size = 1000;
108         notify.nttrans.in.completion_filter = FILE_NOTIFY_CHANGE_NAME;
109         notify.nttrans.in.file.fnum = fnum;
110         notify.nttrans.in.recursive = true;
111
112         printf("Testing notify cancel\n");
113
114         req = smb_raw_changenotify_send(cli->tree, &notify);
115         smb_raw_ntcancel(req);
116         status = smb_raw_changenotify_recv(req, mem_ctx, &notify);
117         CHECK_STATUS(status, NT_STATUS_CANCELLED);
118
119         printf("Testing notify mkdir\n");
120
121         req = smb_raw_changenotify_send(cli->tree, &notify);
122         smbcli_mkdir(cli2->tree, BASEDIR "\\subdir-name");
123
124         status = smb_raw_changenotify_recv(req, mem_ctx, &notify);
125         CHECK_STATUS(status, NT_STATUS_OK);
126
127         CHECK_VAL(notify.nttrans.out.num_changes, 1);
128         CHECK_VAL(notify.nttrans.out.changes[0].action, NOTIFY_ACTION_ADDED);
129         CHECK_WSTR(notify.nttrans.out.changes[0].name, "subdir-name", STR_UNICODE);
130
131         printf("Testing notify rmdir\n");
132
133         req = smb_raw_changenotify_send(cli->tree, &notify);
134         smbcli_rmdir(cli2->tree, BASEDIR "\\subdir-name");
135
136         status = smb_raw_changenotify_recv(req, mem_ctx, &notify);
137         CHECK_STATUS(status, NT_STATUS_OK);
138         CHECK_VAL(notify.nttrans.out.num_changes, 1);
139         CHECK_VAL(notify.nttrans.out.changes[0].action, NOTIFY_ACTION_REMOVED);
140         CHECK_WSTR(notify.nttrans.out.changes[0].name, "subdir-name", STR_UNICODE);
141
142         printf("Testing notify mkdir - rmdir - mkdir - rmdir\n");
143
144         smbcli_mkdir(cli2->tree, BASEDIR "\\subdir-name");
145         smbcli_rmdir(cli2->tree, BASEDIR "\\subdir-name");
146         smbcli_mkdir(cli2->tree, BASEDIR "\\subdir-name");
147         smbcli_rmdir(cli2->tree, BASEDIR "\\subdir-name");
148         smb_msleep(200);
149         req = smb_raw_changenotify_send(cli->tree, &notify);
150         status = smb_raw_changenotify_recv(req, mem_ctx, &notify);
151         CHECK_STATUS(status, NT_STATUS_OK);
152         CHECK_VAL(notify.nttrans.out.num_changes, 4);
153         CHECK_VAL(notify.nttrans.out.changes[0].action, NOTIFY_ACTION_ADDED);
154         CHECK_WSTR(notify.nttrans.out.changes[0].name, "subdir-name", STR_UNICODE);
155         CHECK_VAL(notify.nttrans.out.changes[1].action, NOTIFY_ACTION_REMOVED);
156         CHECK_WSTR(notify.nttrans.out.changes[1].name, "subdir-name", STR_UNICODE);
157         CHECK_VAL(notify.nttrans.out.changes[2].action, NOTIFY_ACTION_ADDED);
158         CHECK_WSTR(notify.nttrans.out.changes[2].name, "subdir-name", STR_UNICODE);
159         CHECK_VAL(notify.nttrans.out.changes[3].action, NOTIFY_ACTION_REMOVED);
160         CHECK_WSTR(notify.nttrans.out.changes[3].name, "subdir-name", STR_UNICODE);
161
162         count = torture_numops;
163         printf("Testing buffered notify on create of %d files\n", count);
164         for (i=0;i<count;i++) {
165                 char *fname = talloc_asprintf(cli, BASEDIR "\\test%d.txt", i);
166                 int fnum3 = smbcli_open(cli->tree, fname, O_CREAT|O_RDWR, DENY_NONE);
167                 if (fnum3 == -1) {
168                         printf("Failed to create %s - %s\n", 
169                                fname, smbcli_errstr(cli->tree));
170                         ret = false;
171                         goto done;
172                 }
173                 talloc_free(fname);
174                 smbcli_close(cli->tree, fnum3);
175         }
176
177         /* (1st notify) setup a new notify on a different directory handle.
178            This new notify won't see the events above. */
179         notify.nttrans.in.file.fnum = fnum2;
180         req2 = smb_raw_changenotify_send(cli->tree, &notify);
181
182         /* (2nd notify) whereas this notify will see the above buffered events,
183            and it directly returns the buffered events */
184         notify.nttrans.in.file.fnum = fnum;
185         req = smb_raw_changenotify_send(cli->tree, &notify);
186
187         status = smbcli_unlink(cli->tree, BASEDIR "\\nonexistant.txt");
188         CHECK_STATUS(status, NT_STATUS_OBJECT_NAME_NOT_FOUND);
189
190         /* (1st unlink) as the 2nd notify directly returns,
191            this unlink is only seen by the 1st notify and 
192            the 3rd notify (later) */
193         printf("Testing notify on unlink for the first file\n");
194         status = smbcli_unlink(cli2->tree, BASEDIR "\\test0.txt");
195         CHECK_STATUS(status, NT_STATUS_OK);
196
197         /* receive the reply from the 2nd notify */
198         status = smb_raw_changenotify_recv(req, mem_ctx, &notify);
199         CHECK_STATUS(status, NT_STATUS_OK);
200
201         CHECK_VAL(notify.nttrans.out.num_changes, count);
202         for (i=1;i<count;i++) {
203                 CHECK_VAL(notify.nttrans.out.changes[i].action, NOTIFY_ACTION_ADDED);
204         }
205         CHECK_WSTR(notify.nttrans.out.changes[0].name, "test0.txt", STR_UNICODE);
206
207         printf("and now from the 1st notify\n");
208         status = smb_raw_changenotify_recv(req2, mem_ctx, &notify);
209         CHECK_STATUS(status, NT_STATUS_OK);
210         CHECK_VAL(notify.nttrans.out.num_changes, 1);
211         CHECK_VAL(notify.nttrans.out.changes[0].action, NOTIFY_ACTION_REMOVED);
212         CHECK_WSTR(notify.nttrans.out.changes[0].name, "test0.txt", STR_UNICODE);
213
214         printf("(3rd notify) this notify will only see the 1st unlink\n");
215         req = smb_raw_changenotify_send(cli->tree, &notify);
216
217         status = smbcli_unlink(cli->tree, BASEDIR "\\nonexistant.txt");
218         CHECK_STATUS(status, NT_STATUS_OBJECT_NAME_NOT_FOUND);
219
220         printf("Testing notify on wildcard unlink for %d files\n", count-1);
221         /* (2nd unlink) do a wildcard unlink */
222         status = smbcli_unlink(cli2->tree, BASEDIR "\\test*.txt");
223         CHECK_STATUS(status, NT_STATUS_OK);
224
225         /* receive the 3rd notify */
226         status = smb_raw_changenotify_recv(req, mem_ctx, &notify);
227         CHECK_STATUS(status, NT_STATUS_OK);
228         CHECK_VAL(notify.nttrans.out.num_changes, 1);
229         CHECK_VAL(notify.nttrans.out.changes[0].action, NOTIFY_ACTION_REMOVED);
230         CHECK_WSTR(notify.nttrans.out.changes[0].name, "test0.txt", STR_UNICODE);
231
232         /* and we now see the rest of the unlink calls on both directory handles */
233         notify.nttrans.in.file.fnum = fnum;
234         sleep(3);
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         notify.nttrans.in.file.fnum = fnum2;
243         req = smb_raw_changenotify_send(cli->tree, &notify);
244         status = smb_raw_changenotify_recv(req, mem_ctx, &notify);
245         CHECK_STATUS(status, NT_STATUS_OK);
246         CHECK_VAL(notify.nttrans.out.num_changes, count-1);
247         for (i=0;i<notify.nttrans.out.num_changes;i++) {
248                 CHECK_VAL(notify.nttrans.out.changes[i].action, NOTIFY_ACTION_REMOVED);
249         }
250
251         printf("Testing if a close() on the dir handle triggers the notify reply\n");
252
253         notify.nttrans.in.file.fnum = fnum;
254         req = smb_raw_changenotify_send(cli->tree, &notify);
255
256         cl.close.level = RAW_CLOSE_CLOSE;
257         cl.close.in.file.fnum = fnum;
258         cl.close.in.write_time = 0;
259         status = smb_raw_close(cli->tree, &cl);
260         CHECK_STATUS(status, NT_STATUS_OK);
261
262         status = smb_raw_changenotify_recv(req, mem_ctx, &notify);
263         CHECK_STATUS(status, NT_STATUS_OK);
264         CHECK_VAL(notify.nttrans.out.num_changes, 0);
265
266 done:
267         smb_raw_exit(cli->session);
268         return ret;
269 }
270
271 /*
272  * Check notify reply for a rename action. Not sure if this is a valid thing
273  * to do, but depending on timing between inotify and messaging we get the
274  * add/remove/modify in any order. This routines tries to find the action/name
275  * pair in any of the three following notify_changes.
276  */
277
278 static bool check_rename_reply(struct smbcli_state *cli,
279                                int line,
280                                struct notify_changes *actions,
281                                uint32_t action, const char *name)
282 {
283         int i;
284
285         for (i=0; i<3; i++) {
286                 if (actions[i].action == action) {
287                         if ((actions[i].name.s == NULL)
288                             || (strcmp(actions[i].name.s, name) != 0)
289                             || (wire_bad_flags(&actions[i].name, STR_UNICODE,
290                                                cli->transport))) {
291                                 printf("(%d) name [%s] != %s\n", line,
292                                        actions[i].name.s, name);
293                                 return false;
294                         }
295                         return true;
296                 }
297         }
298
299         printf("(%d) expected action %d, not found\n", line, action);
300         return false;
301 }
302
303 /* 
304    testing of recursive change notify
305 */
306 static bool test_notify_recursive(struct smbcli_state *cli, TALLOC_CTX *mem_ctx)
307 {
308         bool ret = true;
309         NTSTATUS status;
310         union smb_notify notify;
311         union smb_open io;
312         int fnum;
313         struct smbcli_request *req1, *req2;
314
315         printf("TESTING CHANGE NOTIFY WITH RECURSION\n");
316                 
317         /*
318           get a handle on the directory
319         */
320         io.generic.level = RAW_OPEN_NTCREATEX;
321         io.ntcreatex.in.root_fid.fnum = 0;
322         io.ntcreatex.in.flags = 0;
323         io.ntcreatex.in.access_mask = SEC_FILE_ALL;
324         io.ntcreatex.in.create_options = NTCREATEX_OPTIONS_DIRECTORY;
325         io.ntcreatex.in.file_attr = FILE_ATTRIBUTE_NORMAL;
326         io.ntcreatex.in.share_access = NTCREATEX_SHARE_ACCESS_READ | NTCREATEX_SHARE_ACCESS_WRITE;
327         io.ntcreatex.in.alloc_size = 0;
328         io.ntcreatex.in.open_disposition = NTCREATEX_DISP_OPEN;
329         io.ntcreatex.in.impersonation = NTCREATEX_IMPERSONATION_ANONYMOUS;
330         io.ntcreatex.in.security_flags = 0;
331         io.ntcreatex.in.fname = BASEDIR;
332
333         status = smb_raw_open(cli->tree, mem_ctx, &io);
334         CHECK_STATUS(status, NT_STATUS_OK);
335         fnum = io.ntcreatex.out.file.fnum;
336
337         /* ask for a change notify, on file or directory name
338            changes. Setup both with and without recursion */
339         notify.nttrans.level = RAW_NOTIFY_NTTRANS;
340         notify.nttrans.in.buffer_size = 1000;
341         notify.nttrans.in.completion_filter = FILE_NOTIFY_CHANGE_NAME | FILE_NOTIFY_CHANGE_ATTRIBUTES | FILE_NOTIFY_CHANGE_CREATION;
342         notify.nttrans.in.file.fnum = fnum;
343
344         notify.nttrans.in.recursive = true;
345         req1 = smb_raw_changenotify_send(cli->tree, &notify);
346
347         notify.nttrans.in.recursive = false;
348         req2 = smb_raw_changenotify_send(cli->tree, &notify);
349
350         /* cancel initial requests so the buffer is setup */
351         smb_raw_ntcancel(req1);
352         status = smb_raw_changenotify_recv(req1, mem_ctx, &notify);
353         CHECK_STATUS(status, NT_STATUS_CANCELLED);
354
355         smb_raw_ntcancel(req2);
356         status = smb_raw_changenotify_recv(req2, mem_ctx, &notify);
357         CHECK_STATUS(status, NT_STATUS_CANCELLED);
358
359         smbcli_mkdir(cli->tree, BASEDIR "\\subdir-name");
360         smbcli_mkdir(cli->tree, BASEDIR "\\subdir-name\\subname1");
361         smbcli_close(cli->tree, 
362                      smbcli_open(cli->tree, BASEDIR "\\subdir-name\\subname2", O_CREAT, 0));
363         smbcli_rename(cli->tree, BASEDIR "\\subdir-name\\subname1", BASEDIR "\\subdir-name\\subname1-r");
364         smbcli_rename(cli->tree, BASEDIR "\\subdir-name\\subname2", BASEDIR "\\subname2-r");
365         smbcli_rename(cli->tree, BASEDIR "\\subname2-r", BASEDIR "\\subname3-r");
366
367         notify.nttrans.in.completion_filter = 0;
368         notify.nttrans.in.recursive = true;
369         smb_msleep(200);
370         req1 = smb_raw_changenotify_send(cli->tree, &notify);
371
372         smbcli_rmdir(cli->tree, BASEDIR "\\subdir-name\\subname1-r");
373         smbcli_rmdir(cli->tree, BASEDIR "\\subdir-name");
374         smbcli_unlink(cli->tree, BASEDIR "\\subname3-r");
375
376         notify.nttrans.in.recursive = false;
377         req2 = smb_raw_changenotify_send(cli->tree, &notify);
378
379         status = smb_raw_changenotify_recv(req1, mem_ctx, &notify);
380         CHECK_STATUS(status, NT_STATUS_OK);
381
382         CHECK_VAL(notify.nttrans.out.num_changes, 11);
383         CHECK_VAL(notify.nttrans.out.changes[0].action, NOTIFY_ACTION_ADDED);
384         CHECK_WSTR(notify.nttrans.out.changes[0].name, "subdir-name", STR_UNICODE);
385         CHECK_VAL(notify.nttrans.out.changes[1].action, NOTIFY_ACTION_ADDED);
386         CHECK_WSTR(notify.nttrans.out.changes[1].name, "subdir-name\\subname1", STR_UNICODE);
387         CHECK_VAL(notify.nttrans.out.changes[2].action, NOTIFY_ACTION_ADDED);
388         CHECK_WSTR(notify.nttrans.out.changes[2].name, "subdir-name\\subname2", STR_UNICODE);
389         CHECK_VAL(notify.nttrans.out.changes[3].action, NOTIFY_ACTION_OLD_NAME);
390         CHECK_WSTR(notify.nttrans.out.changes[3].name, "subdir-name\\subname1", STR_UNICODE);
391         CHECK_VAL(notify.nttrans.out.changes[4].action, NOTIFY_ACTION_NEW_NAME);
392         CHECK_WSTR(notify.nttrans.out.changes[4].name, "subdir-name\\subname1-r", STR_UNICODE);
393
394         ret &= check_rename_reply(
395                 cli, __LINE__, &notify.nttrans.out.changes[5],
396                 NOTIFY_ACTION_ADDED, "subname2-r");
397         ret &= check_rename_reply(
398                 cli, __LINE__, &notify.nttrans.out.changes[5],
399                 NOTIFY_ACTION_REMOVED, "subdir-name\\subname2");
400         ret &= check_rename_reply(
401                 cli, __LINE__, &notify.nttrans.out.changes[5],
402                 NOTIFY_ACTION_MODIFIED, "subname2-r");
403                 
404         ret &= check_rename_reply(
405                 cli, __LINE__, &notify.nttrans.out.changes[8],
406                 NOTIFY_ACTION_OLD_NAME, "subname2-r");
407         ret &= check_rename_reply(
408                 cli, __LINE__, &notify.nttrans.out.changes[8],
409                 NOTIFY_ACTION_NEW_NAME, "subname3-r");
410         ret &= check_rename_reply(
411                 cli, __LINE__, &notify.nttrans.out.changes[8],
412                 NOTIFY_ACTION_MODIFIED, "subname3-r");
413
414         if (!ret) {
415                 goto done;
416         }
417
418         status = smb_raw_changenotify_recv(req2, mem_ctx, &notify);
419         CHECK_STATUS(status, NT_STATUS_OK);
420
421         CHECK_VAL(notify.nttrans.out.num_changes, 3);
422         CHECK_VAL(notify.nttrans.out.changes[0].action, NOTIFY_ACTION_REMOVED);
423         CHECK_WSTR(notify.nttrans.out.changes[0].name, "subdir-name\\subname1-r", STR_UNICODE);
424         CHECK_VAL(notify.nttrans.out.changes[1].action, NOTIFY_ACTION_REMOVED);
425         CHECK_WSTR(notify.nttrans.out.changes[1].name, "subdir-name", STR_UNICODE);
426         CHECK_VAL(notify.nttrans.out.changes[2].action, NOTIFY_ACTION_REMOVED);
427         CHECK_WSTR(notify.nttrans.out.changes[2].name, "subname3-r", STR_UNICODE);
428
429 done:
430         smb_raw_exit(cli->session);
431         return ret;
432 }
433
434 /* 
435    testing of change notify mask change
436 */
437 static bool test_notify_mask_change(struct smbcli_state *cli, TALLOC_CTX *mem_ctx)
438 {
439         bool ret = true;
440         NTSTATUS status;
441         union smb_notify notify;
442         union smb_open io;
443         int fnum;
444         struct smbcli_request *req1, *req2;
445
446         printf("TESTING CHANGE NOTIFY WITH MASK CHANGE\n");
447
448         /*
449           get a handle on the directory
450         */
451         io.generic.level = RAW_OPEN_NTCREATEX;
452         io.ntcreatex.in.root_fid.fnum = 0;
453         io.ntcreatex.in.flags = 0;
454         io.ntcreatex.in.access_mask = SEC_FILE_ALL;
455         io.ntcreatex.in.create_options = NTCREATEX_OPTIONS_DIRECTORY;
456         io.ntcreatex.in.file_attr = FILE_ATTRIBUTE_NORMAL;
457         io.ntcreatex.in.share_access = NTCREATEX_SHARE_ACCESS_READ | NTCREATEX_SHARE_ACCESS_WRITE;
458         io.ntcreatex.in.alloc_size = 0;
459         io.ntcreatex.in.open_disposition = NTCREATEX_DISP_OPEN;
460         io.ntcreatex.in.impersonation = NTCREATEX_IMPERSONATION_ANONYMOUS;
461         io.ntcreatex.in.security_flags = 0;
462         io.ntcreatex.in.fname = BASEDIR;
463
464         status = smb_raw_open(cli->tree, mem_ctx, &io);
465         CHECK_STATUS(status, NT_STATUS_OK);
466         fnum = io.ntcreatex.out.file.fnum;
467
468         /* ask for a change notify, on file or directory name
469            changes. Setup both with and without recursion */
470         notify.nttrans.level = RAW_NOTIFY_NTTRANS;
471         notify.nttrans.in.buffer_size = 1000;
472         notify.nttrans.in.completion_filter = FILE_NOTIFY_CHANGE_ATTRIBUTES;
473         notify.nttrans.in.file.fnum = fnum;
474
475         notify.nttrans.in.recursive = true;
476         req1 = smb_raw_changenotify_send(cli->tree, &notify);
477
478         notify.nttrans.in.recursive = false;
479         req2 = smb_raw_changenotify_send(cli->tree, &notify);
480
481         /* cancel initial requests so the buffer is setup */
482         smb_raw_ntcancel(req1);
483         status = smb_raw_changenotify_recv(req1, mem_ctx, &notify);
484         CHECK_STATUS(status, NT_STATUS_CANCELLED);
485
486         smb_raw_ntcancel(req2);
487         status = smb_raw_changenotify_recv(req2, mem_ctx, &notify);
488         CHECK_STATUS(status, NT_STATUS_CANCELLED);
489
490         notify.nttrans.in.recursive = true;
491         req1 = smb_raw_changenotify_send(cli->tree, &notify);
492
493         /* Set to hidden then back again. */
494         smbcli_close(cli->tree, smbcli_open(cli->tree, BASEDIR "\\tname1", O_CREAT, 0));
495         smbcli_setatr(cli->tree, BASEDIR "\\tname1", FILE_ATTRIBUTE_HIDDEN, 0);
496         smbcli_unlink(cli->tree, BASEDIR "\\tname1");
497
498         status = smb_raw_changenotify_recv(req1, mem_ctx, &notify);
499         CHECK_STATUS(status, NT_STATUS_OK);
500
501         CHECK_VAL(notify.nttrans.out.num_changes, 1);
502         CHECK_VAL(notify.nttrans.out.changes[0].action, NOTIFY_ACTION_MODIFIED);
503         CHECK_WSTR(notify.nttrans.out.changes[0].name, "tname1", STR_UNICODE);
504
505         /* Now try and change the mask to include other events.
506          * This should not work - once the mask is set on a directory
507          * fnum it seems to be fixed until the fnum is closed. */
508
509         notify.nttrans.in.completion_filter = FILE_NOTIFY_CHANGE_NAME | FILE_NOTIFY_CHANGE_ATTRIBUTES | FILE_NOTIFY_CHANGE_CREATION;
510         notify.nttrans.in.recursive = true;
511         req1 = smb_raw_changenotify_send(cli->tree, &notify);
512
513         notify.nttrans.in.recursive = false;
514         req2 = smb_raw_changenotify_send(cli->tree, &notify);
515
516         smbcli_mkdir(cli->tree, BASEDIR "\\subdir-name");
517         smbcli_mkdir(cli->tree, BASEDIR "\\subdir-name\\subname1");
518         smbcli_close(cli->tree, 
519                      smbcli_open(cli->tree, BASEDIR "\\subdir-name\\subname2", O_CREAT, 0));
520         smbcli_rename(cli->tree, BASEDIR "\\subdir-name\\subname1", BASEDIR "\\subdir-name\\subname1-r");
521         smbcli_rename(cli->tree, BASEDIR "\\subdir-name\\subname2", BASEDIR "\\subname2-r");
522         smbcli_rename(cli->tree, BASEDIR "\\subname2-r", BASEDIR "\\subname3-r");
523
524         smbcli_rmdir(cli->tree, BASEDIR "\\subdir-name\\subname1-r");
525         smbcli_rmdir(cli->tree, BASEDIR "\\subdir-name");
526         smbcli_unlink(cli->tree, BASEDIR "\\subname3-r");
527
528         status = smb_raw_changenotify_recv(req1, mem_ctx, &notify);
529         CHECK_STATUS(status, NT_STATUS_OK);
530
531         CHECK_VAL(notify.nttrans.out.num_changes, 1);
532         CHECK_VAL(notify.nttrans.out.changes[0].action, NOTIFY_ACTION_MODIFIED);
533         CHECK_WSTR(notify.nttrans.out.changes[0].name, "subname2-r", STR_UNICODE);
534
535         status = smb_raw_changenotify_recv(req2, mem_ctx, &notify);
536         CHECK_STATUS(status, NT_STATUS_OK);
537
538         CHECK_VAL(notify.nttrans.out.num_changes, 1);
539         CHECK_VAL(notify.nttrans.out.changes[0].action, NOTIFY_ACTION_MODIFIED);
540         CHECK_WSTR(notify.nttrans.out.changes[0].name, "subname3-r", STR_UNICODE);
541
542         if (!ret) {
543                 goto done;
544         }
545
546 done:
547         smb_raw_exit(cli->session);
548         return ret;
549 }
550
551
552 /* 
553    testing of mask bits for change notify
554 */
555 static bool test_notify_mask(struct smbcli_state *cli, struct torture_context *tctx)
556 {
557         bool ret = true;
558         NTSTATUS status;
559         union smb_notify notify;
560         union smb_open io;
561         int fnum, fnum2;
562         uint32_t mask;
563         int i;
564         char c = 1;
565         struct timeval tv;
566         NTTIME t;
567
568         printf("TESTING CHANGE NOTIFY COMPLETION FILTERS\n");
569
570         tv = timeval_current_ofs(1000, 0);
571         t = timeval_to_nttime(&tv);
572
573         /*
574           get a handle on the directory
575         */
576         io.generic.level = RAW_OPEN_NTCREATEX;
577         io.ntcreatex.in.root_fid.fnum = 0;
578         io.ntcreatex.in.flags = 0;
579         io.ntcreatex.in.access_mask = SEC_FILE_ALL;
580         io.ntcreatex.in.create_options = NTCREATEX_OPTIONS_DIRECTORY;
581         io.ntcreatex.in.file_attr = FILE_ATTRIBUTE_NORMAL;
582         io.ntcreatex.in.share_access = NTCREATEX_SHARE_ACCESS_READ | NTCREATEX_SHARE_ACCESS_WRITE;
583         io.ntcreatex.in.alloc_size = 0;
584         io.ntcreatex.in.open_disposition = NTCREATEX_DISP_OPEN;
585         io.ntcreatex.in.impersonation = NTCREATEX_IMPERSONATION_ANONYMOUS;
586         io.ntcreatex.in.security_flags = 0;
587         io.ntcreatex.in.fname = BASEDIR;
588
589         notify.nttrans.level = RAW_NOTIFY_NTTRANS;
590         notify.nttrans.in.buffer_size = 1000;
591         notify.nttrans.in.recursive = true;
592
593 #define NOTIFY_MASK_TEST(test_name, setup, op, cleanup, Action, expected, nchanges) \
594         do { \
595         smbcli_getatr(cli->tree, test_name, NULL, NULL, NULL); \
596         do { for (mask=i=0;i<32;i++) { \
597                 struct smbcli_request *req; \
598                 status = smb_raw_open(cli->tree, tctx, &io); \
599                 CHECK_STATUS(status, NT_STATUS_OK); \
600                 fnum = io.ntcreatex.out.file.fnum; \
601                 setup \
602                 notify.nttrans.in.file.fnum = fnum;     \
603                 notify.nttrans.in.completion_filter = (1<<i); \
604                 req = smb_raw_changenotify_send(cli->tree, &notify); \
605                 op \
606                 smb_msleep(200); smb_raw_ntcancel(req); \
607                 status = smb_raw_changenotify_recv(req, tctx, &notify); \
608                 cleanup \
609                 smbcli_close(cli->tree, fnum); \
610                 if (NT_STATUS_EQUAL(status, NT_STATUS_CANCELLED)) continue; \
611                 CHECK_STATUS(status, NT_STATUS_OK); \
612                 /* special case to cope with file rename behaviour */ \
613                 if (nchanges == 2 && notify.nttrans.out.num_changes == 1 && \
614                     notify.nttrans.out.changes[0].action == NOTIFY_ACTION_MODIFIED && \
615                     ((expected) & FILE_NOTIFY_CHANGE_ATTRIBUTES) && \
616                     Action == NOTIFY_ACTION_OLD_NAME) { \
617                         printf("(rename file special handling OK)\n"); \
618                 } else if (nchanges != notify.nttrans.out.num_changes) { \
619                         printf("ERROR: nchanges=%d expected=%d action=%d filter=0x%08x\n", \
620                                notify.nttrans.out.num_changes, \
621                                nchanges, \
622                                notify.nttrans.out.changes[0].action, \
623                                notify.nttrans.in.completion_filter); \
624                         ret = false; \
625                 } else if (notify.nttrans.out.changes[0].action != Action) { \
626                         printf("ERROR: nchanges=%d action=%d expectedAction=%d filter=0x%08x\n", \
627                                notify.nttrans.out.num_changes, \
628                                notify.nttrans.out.changes[0].action, \
629                                Action, \
630                                notify.nttrans.in.completion_filter); \
631                         ret = false; \
632                 } else if (strcmp(notify.nttrans.out.changes[0].name.s, "tname1") != 0) { \
633                         printf("ERROR: nchanges=%d action=%d filter=0x%08x name=%s\n", \
634                                notify.nttrans.out.num_changes, \
635                                notify.nttrans.out.changes[0].action, \
636                                notify.nttrans.in.completion_filter, \
637                                notify.nttrans.out.changes[0].name.s);   \
638                         ret = false; \
639                 } \
640                 mask |= (1<<i); \
641         } \
642         if ((expected) != mask) { \
643                 if (((expected) & ~mask) != 0) { \
644                         printf("ERROR: trigger on too few bits. mask=0x%08x expected=0x%08x\n", \
645                                mask, expected); \
646                         ret = false; \
647                 } else { \
648                         printf("WARNING: trigger on too many bits. mask=0x%08x expected=0x%08x\n", \
649                                mask, expected); \
650                 } \
651         } \
652         } while (0); \
653         } while (0);
654
655         printf("Testing mkdir\n");
656         NOTIFY_MASK_TEST("Testing mkdir",;,
657                          smbcli_mkdir(cli->tree, BASEDIR "\\tname1");,
658                          smbcli_rmdir(cli->tree, BASEDIR "\\tname1");,
659                          NOTIFY_ACTION_ADDED,
660                          FILE_NOTIFY_CHANGE_DIR_NAME, 1);
661
662         printf("Testing create file\n");
663         NOTIFY_MASK_TEST("Testing create file",;,
664                          smbcli_close(cli->tree, smbcli_open(cli->tree, BASEDIR "\\tname1", O_CREAT, 0));,
665                          smbcli_unlink(cli->tree, BASEDIR "\\tname1");,
666                          NOTIFY_ACTION_ADDED,
667                          FILE_NOTIFY_CHANGE_FILE_NAME, 1);
668
669         printf("Testing unlink\n");
670         NOTIFY_MASK_TEST("Testing unlink",
671                          smbcli_close(cli->tree, smbcli_open(cli->tree, BASEDIR "\\tname1", O_CREAT, 0));,
672                          smbcli_unlink(cli->tree, BASEDIR "\\tname1");,
673                          ;,
674                          NOTIFY_ACTION_REMOVED,
675                          FILE_NOTIFY_CHANGE_FILE_NAME, 1);
676
677         printf("Testing rmdir\n");
678         NOTIFY_MASK_TEST("Testing rmdir",
679                          smbcli_mkdir(cli->tree, BASEDIR "\\tname1");,
680                          smbcli_rmdir(cli->tree, BASEDIR "\\tname1");,
681                          ;,
682                          NOTIFY_ACTION_REMOVED,
683                          FILE_NOTIFY_CHANGE_DIR_NAME, 1);
684
685         printf("Testing rename file\n");
686         NOTIFY_MASK_TEST("Testing rename file",
687                          smbcli_close(cli->tree, smbcli_open(cli->tree, BASEDIR "\\tname1", O_CREAT, 0));,
688                          smbcli_rename(cli->tree, BASEDIR "\\tname1", BASEDIR "\\tname2");,
689                          smbcli_unlink(cli->tree, BASEDIR "\\tname2");,
690                          NOTIFY_ACTION_OLD_NAME,
691                          FILE_NOTIFY_CHANGE_FILE_NAME|FILE_NOTIFY_CHANGE_ATTRIBUTES|FILE_NOTIFY_CHANGE_CREATION, 2);
692
693         printf("Testing rename dir\n");
694         NOTIFY_MASK_TEST("Testing rename dir",
695                 smbcli_mkdir(cli->tree, BASEDIR "\\tname1");,
696                 smbcli_rename(cli->tree, BASEDIR "\\tname1", BASEDIR "\\tname2");,
697                 smbcli_rmdir(cli->tree, BASEDIR "\\tname2");,
698                 NOTIFY_ACTION_OLD_NAME,
699                 FILE_NOTIFY_CHANGE_DIR_NAME, 2);
700
701         printf("Testing set path attribute\n");
702         NOTIFY_MASK_TEST("Testing set path attribute",
703                 smbcli_close(cli->tree, smbcli_open(cli->tree, BASEDIR "\\tname1", O_CREAT, 0));,
704                 smbcli_setatr(cli->tree, BASEDIR "\\tname1", FILE_ATTRIBUTE_HIDDEN, 0);,
705                 smbcli_unlink(cli->tree, BASEDIR "\\tname1");,
706                 NOTIFY_ACTION_MODIFIED,
707                 FILE_NOTIFY_CHANGE_ATTRIBUTES, 1);
708
709         printf("Testing set path write time\n");
710         NOTIFY_MASK_TEST("Testing set path write time",
711                 smbcli_close(cli->tree, smbcli_open(cli->tree, BASEDIR "\\tname1", O_CREAT, 0));,
712                 smbcli_setatr(cli->tree, BASEDIR "\\tname1", FILE_ATTRIBUTE_NORMAL, 1000);,
713                 smbcli_unlink(cli->tree, BASEDIR "\\tname1");,
714                 NOTIFY_ACTION_MODIFIED,
715                 FILE_NOTIFY_CHANGE_LAST_WRITE, 1);
716
717         printf("Testing set file attribute\n");
718         NOTIFY_MASK_TEST("Testing set file attribute",
719                 fnum2 = create_complex_file(cli, tctx, BASEDIR "\\tname1");,
720                 smbcli_fsetatr(cli->tree, fnum2, FILE_ATTRIBUTE_HIDDEN, 0, 0, 0, 0);,
721                 (smbcli_close(cli->tree, fnum2), smbcli_unlink(cli->tree, BASEDIR "\\tname1"));,
722                 NOTIFY_ACTION_MODIFIED,
723                 FILE_NOTIFY_CHANGE_ATTRIBUTES, 1);
724
725         if (torture_setting_bool(tctx, "samba3", false)) {
726                 printf("Samba3 does not yet support create times "
727                        "everywhere\n");
728         }
729         else {
730                 printf("Testing set file create time\n");
731                 NOTIFY_MASK_TEST("Testing set file create time",
732                         fnum2 = create_complex_file(cli, tctx,
733                                                     BASEDIR "\\tname1");,
734                         smbcli_fsetatr(cli->tree, fnum2, 0, t, 0, 0, 0);,
735                         (smbcli_close(cli->tree, fnum2),
736                          smbcli_unlink(cli->tree, BASEDIR "\\tname1"));,
737                         NOTIFY_ACTION_MODIFIED,
738                         FILE_NOTIFY_CHANGE_CREATION, 1);
739         }
740
741         printf("Testing set file access time\n");
742         NOTIFY_MASK_TEST("Testing set file access time",
743                 fnum2 = create_complex_file(cli, tctx, BASEDIR "\\tname1");,
744                 smbcli_fsetatr(cli->tree, fnum2, 0, 0, t, 0, 0);,
745                 (smbcli_close(cli->tree, fnum2), smbcli_unlink(cli->tree, BASEDIR "\\tname1"));,
746                 NOTIFY_ACTION_MODIFIED,
747                 FILE_NOTIFY_CHANGE_LAST_ACCESS, 1);
748
749         printf("Testing set file write time\n");
750         NOTIFY_MASK_TEST("Testing set file write time",
751                 fnum2 = create_complex_file(cli, tctx, BASEDIR "\\tname1");,
752                 smbcli_fsetatr(cli->tree, fnum2, 0, 0, 0, t, 0);,
753                 (smbcli_close(cli->tree, fnum2), smbcli_unlink(cli->tree, BASEDIR "\\tname1"));,
754                 NOTIFY_ACTION_MODIFIED,
755                 FILE_NOTIFY_CHANGE_LAST_WRITE, 1);
756
757         printf("Testing set file change time\n");
758         NOTIFY_MASK_TEST("Testing set file change time",
759                 fnum2 = create_complex_file(cli, tctx, BASEDIR "\\tname1");,
760                 smbcli_fsetatr(cli->tree, fnum2, 0, 0, 0, 0, t);,
761                 (smbcli_close(cli->tree, fnum2), smbcli_unlink(cli->tree, BASEDIR "\\tname1"));,
762                 NOTIFY_ACTION_MODIFIED,
763                 0, 1);
764
765
766         printf("Testing write\n");
767         NOTIFY_MASK_TEST("Testing write",
768                 fnum2 = create_complex_file(cli, tctx, BASEDIR "\\tname1");,
769                 smbcli_write(cli->tree, fnum2, 1, &c, 10000, 1);,
770                 (smbcli_close(cli->tree, fnum2), smbcli_unlink(cli->tree, BASEDIR "\\tname1"));,
771                 NOTIFY_ACTION_MODIFIED,
772                 0, 1);
773
774         printf("Testing truncate\n");
775         NOTIFY_MASK_TEST("Testing truncate",
776                 fnum2 = create_complex_file(cli, tctx, BASEDIR "\\tname1");,
777                 smbcli_ftruncate(cli->tree, fnum2, 10000);,
778                 (smbcli_close(cli->tree, fnum2), smbcli_unlink(cli->tree, BASEDIR "\\tname1"));,
779                 NOTIFY_ACTION_MODIFIED,
780                 FILE_NOTIFY_CHANGE_SIZE | FILE_NOTIFY_CHANGE_ATTRIBUTES, 1);
781
782 done:
783         smb_raw_exit(cli->session);
784         return ret;
785 }
786
787 /*
788   basic testing of change notify on files
789 */
790 static bool test_notify_file(struct smbcli_state *cli, TALLOC_CTX *mem_ctx)
791 {
792         NTSTATUS status;
793         bool ret = true;
794         union smb_open io;
795         union smb_close cl;
796         union smb_notify notify;
797         struct smbcli_request *req;
798         int fnum;
799         const char *fname = BASEDIR "\\file.txt";
800
801         printf("TESTING CHANGE NOTIFY ON FILES\n");
802
803         io.generic.level = RAW_OPEN_NTCREATEX;
804         io.ntcreatex.in.root_fid.fnum = 0;
805         io.ntcreatex.in.flags = 0;
806         io.ntcreatex.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
807         io.ntcreatex.in.create_options = 0;
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_CREATE;
812         io.ntcreatex.in.impersonation = NTCREATEX_IMPERSONATION_ANONYMOUS;
813         io.ntcreatex.in.security_flags = 0;
814         io.ntcreatex.in.fname = fname;
815         status = smb_raw_open(cli->tree, mem_ctx, &io);
816         CHECK_STATUS(status, NT_STATUS_OK);
817         fnum = io.ntcreatex.out.file.fnum;
818
819         /* ask for a change notify,
820            on file or directory name changes */
821         notify.nttrans.level = RAW_NOTIFY_NTTRANS;
822         notify.nttrans.in.file.fnum = fnum;
823         notify.nttrans.in.buffer_size = 1000;
824         notify.nttrans.in.completion_filter = FILE_NOTIFY_CHANGE_STREAM_NAME;
825         notify.nttrans.in.recursive = false;
826
827         printf("Testing if notifies on file handles are invalid (should be)\n");
828
829         req = smb_raw_changenotify_send(cli->tree, &notify);
830         status = smb_raw_changenotify_recv(req, mem_ctx, &notify);
831         CHECK_STATUS(status, NT_STATUS_INVALID_PARAMETER);
832
833         cl.close.level = RAW_CLOSE_CLOSE;
834         cl.close.in.file.fnum = fnum;
835         cl.close.in.write_time = 0;
836         status = smb_raw_close(cli->tree, &cl);
837         CHECK_STATUS(status, NT_STATUS_OK);
838
839         status = smbcli_unlink(cli->tree, fname);
840         CHECK_STATUS(status, NT_STATUS_OK);
841
842 done:
843         smb_raw_exit(cli->session);
844         return ret;
845 }
846
847 /*
848   basic testing of change notifies followed by a tdis
849 */
850 static bool test_notify_tdis(struct torture_context *tctx)
851 {
852         bool ret = true;
853         NTSTATUS status;
854         union smb_notify notify;
855         union smb_open io;
856         int fnum;
857         struct smbcli_request *req;
858         struct smbcli_state *cli = NULL;
859
860         printf("TESTING CHANGE NOTIFY FOLLOWED BY TDIS\n");
861
862         if (!torture_open_connection(&cli, tctx, 0)) {
863                 return false;
864         }
865
866         /*
867           get a handle on the directory
868         */
869         io.generic.level = RAW_OPEN_NTCREATEX;
870         io.ntcreatex.in.root_fid.fnum = 0;
871         io.ntcreatex.in.flags = 0;
872         io.ntcreatex.in.access_mask = SEC_FILE_ALL;
873         io.ntcreatex.in.create_options = NTCREATEX_OPTIONS_DIRECTORY;
874         io.ntcreatex.in.file_attr = FILE_ATTRIBUTE_NORMAL;
875         io.ntcreatex.in.share_access = NTCREATEX_SHARE_ACCESS_READ | NTCREATEX_SHARE_ACCESS_WRITE;
876         io.ntcreatex.in.alloc_size = 0;
877         io.ntcreatex.in.open_disposition = NTCREATEX_DISP_OPEN;
878         io.ntcreatex.in.impersonation = NTCREATEX_IMPERSONATION_ANONYMOUS;
879         io.ntcreatex.in.security_flags = 0;
880         io.ntcreatex.in.fname = BASEDIR;
881
882         status = smb_raw_open(cli->tree, tctx, &io);
883         CHECK_STATUS(status, NT_STATUS_OK);
884         fnum = io.ntcreatex.out.file.fnum;
885
886         /* ask for a change notify,
887            on file or directory name changes */
888         notify.nttrans.level = RAW_NOTIFY_NTTRANS;
889         notify.nttrans.in.buffer_size = 1000;
890         notify.nttrans.in.completion_filter = FILE_NOTIFY_CHANGE_NAME;
891         notify.nttrans.in.file.fnum = fnum;
892         notify.nttrans.in.recursive = true;
893
894         req = smb_raw_changenotify_send(cli->tree, &notify);
895
896         status = smbcli_tdis(cli);
897         CHECK_STATUS(status, NT_STATUS_OK);
898         cli->tree = NULL;
899
900         status = smb_raw_changenotify_recv(req, tctx, &notify);
901         CHECK_STATUS(status, NT_STATUS_OK);
902         CHECK_VAL(notify.nttrans.out.num_changes, 0);
903
904 done:
905         torture_close_connection(cli);
906         return ret;
907 }
908
909 /*
910   basic testing of change notifies followed by a exit
911 */
912 static bool test_notify_exit(struct torture_context *tctx)
913 {
914         bool ret = true;
915         NTSTATUS status;
916         union smb_notify notify;
917         union smb_open io;
918         int fnum;
919         struct smbcli_request *req;
920         struct smbcli_state *cli = NULL;
921
922         printf("TESTING CHANGE NOTIFY FOLLOWED BY EXIT\n");
923
924         if (!torture_open_connection(&cli, tctx, 0)) {
925                 return false;
926         }
927
928         /*
929           get a handle on the directory
930         */
931         io.generic.level = RAW_OPEN_NTCREATEX;
932         io.ntcreatex.in.root_fid.fnum = 0;
933         io.ntcreatex.in.flags = 0;
934         io.ntcreatex.in.access_mask = SEC_FILE_ALL;
935         io.ntcreatex.in.create_options = NTCREATEX_OPTIONS_DIRECTORY;
936         io.ntcreatex.in.file_attr = FILE_ATTRIBUTE_NORMAL;
937         io.ntcreatex.in.share_access = NTCREATEX_SHARE_ACCESS_READ | NTCREATEX_SHARE_ACCESS_WRITE;
938         io.ntcreatex.in.alloc_size = 0;
939         io.ntcreatex.in.open_disposition = NTCREATEX_DISP_OPEN;
940         io.ntcreatex.in.impersonation = NTCREATEX_IMPERSONATION_ANONYMOUS;
941         io.ntcreatex.in.security_flags = 0;
942         io.ntcreatex.in.fname = BASEDIR;
943
944         status = smb_raw_open(cli->tree, tctx, &io);
945         CHECK_STATUS(status, NT_STATUS_OK);
946         fnum = io.ntcreatex.out.file.fnum;
947
948         /* ask for a change notify,
949            on file or directory name changes */
950         notify.nttrans.level = RAW_NOTIFY_NTTRANS;
951         notify.nttrans.in.buffer_size = 1000;
952         notify.nttrans.in.completion_filter = FILE_NOTIFY_CHANGE_NAME;
953         notify.nttrans.in.file.fnum = fnum;
954         notify.nttrans.in.recursive = true;
955
956         req = smb_raw_changenotify_send(cli->tree, &notify);
957
958         status = smb_raw_exit(cli->session);
959         CHECK_STATUS(status, NT_STATUS_OK);
960
961         status = smb_raw_changenotify_recv(req, tctx, &notify);
962         CHECK_STATUS(status, NT_STATUS_OK);
963         CHECK_VAL(notify.nttrans.out.num_changes, 0);
964
965 done:
966         torture_close_connection(cli);
967         return ret;
968 }
969
970 /*
971   basic testing of change notifies followed by a ulogoff
972 */
973 static bool test_notify_ulogoff(struct torture_context *tctx)
974 {
975         bool ret = true;
976         NTSTATUS status;
977         union smb_notify notify;
978         union smb_open io;
979         int fnum;
980         struct smbcli_request *req;
981         struct smbcli_state *cli = NULL;
982
983         printf("TESTING CHANGE NOTIFY FOLLOWED BY ULOGOFF\n");
984
985         if (!torture_open_connection(&cli, tctx, 0)) {
986                 return false;
987         }
988
989         /*
990           get a handle on the directory
991         */
992         io.generic.level = RAW_OPEN_NTCREATEX;
993         io.ntcreatex.in.root_fid.fnum = 0;
994         io.ntcreatex.in.flags = 0;
995         io.ntcreatex.in.access_mask = SEC_FILE_ALL;
996         io.ntcreatex.in.create_options = NTCREATEX_OPTIONS_DIRECTORY;
997         io.ntcreatex.in.file_attr = FILE_ATTRIBUTE_NORMAL;
998         io.ntcreatex.in.share_access = NTCREATEX_SHARE_ACCESS_READ | NTCREATEX_SHARE_ACCESS_WRITE;
999         io.ntcreatex.in.alloc_size = 0;
1000         io.ntcreatex.in.open_disposition = NTCREATEX_DISP_OPEN;
1001         io.ntcreatex.in.impersonation = NTCREATEX_IMPERSONATION_ANONYMOUS;
1002         io.ntcreatex.in.security_flags = 0;
1003         io.ntcreatex.in.fname = BASEDIR;
1004
1005         status = smb_raw_open(cli->tree, tctx, &io);
1006         CHECK_STATUS(status, NT_STATUS_OK);
1007         fnum = io.ntcreatex.out.file.fnum;
1008
1009         /* ask for a change notify,
1010            on file or directory name changes */
1011         notify.nttrans.level = RAW_NOTIFY_NTTRANS;
1012         notify.nttrans.in.buffer_size = 1000;
1013         notify.nttrans.in.completion_filter = FILE_NOTIFY_CHANGE_NAME;
1014         notify.nttrans.in.file.fnum = fnum;
1015         notify.nttrans.in.recursive = true;
1016
1017         req = smb_raw_changenotify_send(cli->tree, &notify);
1018
1019         status = smb_raw_ulogoff(cli->session);
1020         CHECK_STATUS(status, NT_STATUS_OK);
1021
1022         status = smb_raw_changenotify_recv(req, tctx, &notify);
1023         CHECK_STATUS(status, NT_STATUS_OK);
1024         CHECK_VAL(notify.nttrans.out.num_changes, 0);
1025
1026 done:
1027         torture_close_connection(cli);
1028         return ret;
1029 }
1030
1031 static void tcp_dis_handler(struct smbcli_transport *t, void *p)
1032 {
1033         struct smbcli_state *cli = (struct smbcli_state *)p;
1034         smbcli_transport_dead(cli->transport, NT_STATUS_LOCAL_DISCONNECT);
1035         cli->transport = NULL;
1036         cli->tree = NULL;
1037 }
1038 /*
1039   basic testing of change notifies followed by tcp disconnect
1040 */
1041 static bool test_notify_tcp_dis(struct torture_context *tctx)
1042 {
1043         bool ret = true;
1044         NTSTATUS status;
1045         union smb_notify notify;
1046         union smb_open io;
1047         int fnum;
1048         struct smbcli_request *req;
1049         struct smbcli_state *cli = NULL;
1050
1051         printf("TESTING CHANGE NOTIFY FOLLOWED BY TCP DISCONNECT\n");
1052
1053         if (!torture_open_connection(&cli, tctx, 0)) {
1054                 return false;
1055         }
1056
1057         /*
1058           get a handle on the directory
1059         */
1060         io.generic.level = RAW_OPEN_NTCREATEX;
1061         io.ntcreatex.in.root_fid.fnum = 0;
1062         io.ntcreatex.in.flags = 0;
1063         io.ntcreatex.in.access_mask = SEC_FILE_ALL;
1064         io.ntcreatex.in.create_options = NTCREATEX_OPTIONS_DIRECTORY;
1065         io.ntcreatex.in.file_attr = FILE_ATTRIBUTE_NORMAL;
1066         io.ntcreatex.in.share_access = NTCREATEX_SHARE_ACCESS_READ | NTCREATEX_SHARE_ACCESS_WRITE;
1067         io.ntcreatex.in.alloc_size = 0;
1068         io.ntcreatex.in.open_disposition = NTCREATEX_DISP_OPEN;
1069         io.ntcreatex.in.impersonation = NTCREATEX_IMPERSONATION_ANONYMOUS;
1070         io.ntcreatex.in.security_flags = 0;
1071         io.ntcreatex.in.fname = BASEDIR;
1072
1073         status = smb_raw_open(cli->tree, tctx, &io);
1074         CHECK_STATUS(status, NT_STATUS_OK);
1075         fnum = io.ntcreatex.out.file.fnum;
1076
1077         /* ask for a change notify,
1078            on file or directory name changes */
1079         notify.nttrans.level = RAW_NOTIFY_NTTRANS;
1080         notify.nttrans.in.buffer_size = 1000;
1081         notify.nttrans.in.completion_filter = FILE_NOTIFY_CHANGE_NAME;
1082         notify.nttrans.in.file.fnum = fnum;
1083         notify.nttrans.in.recursive = true;
1084
1085         req = smb_raw_changenotify_send(cli->tree, &notify);
1086
1087         smbcli_transport_idle_handler(cli->transport, tcp_dis_handler, 250, cli);
1088
1089         status = smb_raw_changenotify_recv(req, tctx, &notify);
1090         CHECK_STATUS(status, NT_STATUS_LOCAL_DISCONNECT);
1091
1092 done:
1093         torture_close_connection(cli);
1094         return ret;
1095 }
1096
1097 /* 
1098    test setting up two change notify requests on one handle
1099 */
1100 static bool test_notify_double(struct smbcli_state *cli, TALLOC_CTX *mem_ctx)
1101 {
1102         bool ret = true;
1103         NTSTATUS status;
1104         union smb_notify notify;
1105         union smb_open io;
1106         int fnum;
1107         struct smbcli_request *req1, *req2;
1108
1109         printf("TESTING CHANGE NOTIFY TWICE ON ONE DIRECTORY\n");
1110                 
1111         /*
1112           get a handle on the directory
1113         */
1114         io.generic.level = RAW_OPEN_NTCREATEX;
1115         io.ntcreatex.in.root_fid.fnum = 0;
1116         io.ntcreatex.in.flags = 0;
1117         io.ntcreatex.in.access_mask = SEC_FILE_ALL;
1118         io.ntcreatex.in.create_options = NTCREATEX_OPTIONS_DIRECTORY;
1119         io.ntcreatex.in.file_attr = FILE_ATTRIBUTE_NORMAL;
1120         io.ntcreatex.in.share_access = NTCREATEX_SHARE_ACCESS_READ | NTCREATEX_SHARE_ACCESS_WRITE;
1121         io.ntcreatex.in.alloc_size = 0;
1122         io.ntcreatex.in.open_disposition = NTCREATEX_DISP_OPEN;
1123         io.ntcreatex.in.impersonation = NTCREATEX_IMPERSONATION_ANONYMOUS;
1124         io.ntcreatex.in.security_flags = 0;
1125         io.ntcreatex.in.fname = BASEDIR;
1126
1127         status = smb_raw_open(cli->tree, mem_ctx, &io);
1128         CHECK_STATUS(status, NT_STATUS_OK);
1129         fnum = io.ntcreatex.out.file.fnum;
1130
1131         /* ask for a change notify,
1132            on file or directory name changes */
1133         notify.nttrans.level = RAW_NOTIFY_NTTRANS;
1134         notify.nttrans.in.buffer_size = 1000;
1135         notify.nttrans.in.completion_filter = FILE_NOTIFY_CHANGE_NAME;
1136         notify.nttrans.in.file.fnum = fnum;
1137         notify.nttrans.in.recursive = true;
1138
1139         req1 = smb_raw_changenotify_send(cli->tree, &notify);
1140         req2 = smb_raw_changenotify_send(cli->tree, &notify);
1141
1142         smbcli_mkdir(cli->tree, BASEDIR "\\subdir-name");
1143
1144         status = smb_raw_changenotify_recv(req1, mem_ctx, &notify);
1145         CHECK_STATUS(status, NT_STATUS_OK);
1146         CHECK_VAL(notify.nttrans.out.num_changes, 1);
1147         CHECK_WSTR(notify.nttrans.out.changes[0].name, "subdir-name", STR_UNICODE);
1148
1149         smbcli_mkdir(cli->tree, BASEDIR "\\subdir-name2");
1150
1151         status = smb_raw_changenotify_recv(req2, mem_ctx, &notify);
1152         CHECK_STATUS(status, NT_STATUS_OK);
1153         CHECK_VAL(notify.nttrans.out.num_changes, 1);
1154         CHECK_WSTR(notify.nttrans.out.changes[0].name, "subdir-name2", STR_UNICODE);
1155
1156 done:
1157         smb_raw_exit(cli->session);
1158         return ret;
1159 }
1160
1161
1162 /* 
1163    test multiple change notifies at different depths and with/without recursion
1164 */
1165 static bool test_notify_tree(struct smbcli_state *cli, TALLOC_CTX *mem_ctx)
1166 {
1167         bool ret = true;
1168         union smb_notify notify;
1169         union smb_open io;
1170         struct smbcli_request *req;
1171         struct timeval tv;
1172         struct {
1173                 const char *path;
1174                 bool recursive;
1175                 uint32_t filter;
1176                 int expected;
1177                 int fnum;
1178                 int counted;
1179         } dirs[] = {
1180                 {BASEDIR "\\abc",               true, FILE_NOTIFY_CHANGE_NAME, 30 },
1181                 {BASEDIR "\\zqy",               true, FILE_NOTIFY_CHANGE_NAME, 8 },
1182                 {BASEDIR "\\atsy",              true, FILE_NOTIFY_CHANGE_NAME, 4 },
1183                 {BASEDIR "\\abc\\foo",          true,  FILE_NOTIFY_CHANGE_NAME, 2 },
1184                 {BASEDIR "\\abc\\blah",         true,  FILE_NOTIFY_CHANGE_NAME, 13 },
1185                 {BASEDIR "\\abc\\blah",         false, FILE_NOTIFY_CHANGE_NAME, 7 },
1186                 {BASEDIR "\\abc\\blah\\a",      true, FILE_NOTIFY_CHANGE_NAME, 2 },
1187                 {BASEDIR "\\abc\\blah\\b",      true, FILE_NOTIFY_CHANGE_NAME, 2 },
1188                 {BASEDIR "\\abc\\blah\\c",      true, FILE_NOTIFY_CHANGE_NAME, 2 },
1189                 {BASEDIR "\\abc\\fooblah",      true, FILE_NOTIFY_CHANGE_NAME, 2 },
1190                 {BASEDIR "\\zqy\\xx",           true, FILE_NOTIFY_CHANGE_NAME, 2 },
1191                 {BASEDIR "\\zqy\\yyy",          true, FILE_NOTIFY_CHANGE_NAME, 2 },
1192                 {BASEDIR "\\zqy\\..",           true, FILE_NOTIFY_CHANGE_NAME, 40 },
1193                 {BASEDIR,                       true, FILE_NOTIFY_CHANGE_NAME, 40 },
1194                 {BASEDIR,                       false,FILE_NOTIFY_CHANGE_NAME, 6 },
1195                 {BASEDIR "\\atsy",              false,FILE_NOTIFY_CHANGE_NAME, 4 },
1196                 {BASEDIR "\\abc",               true, FILE_NOTIFY_CHANGE_NAME, 24 },
1197                 {BASEDIR "\\abc",               false,FILE_NOTIFY_CHANGE_FILE_NAME, 0 },
1198                 {BASEDIR "\\abc",               true, FILE_NOTIFY_CHANGE_FILE_NAME, 0 },
1199                 {BASEDIR "\\abc",               true, FILE_NOTIFY_CHANGE_NAME, 24 },
1200         };
1201         int i;
1202         NTSTATUS status;
1203         bool all_done = false;
1204
1205         printf("TESTING CHANGE NOTIFY FOR DIFFERENT DEPTHS\n");
1206
1207         io.generic.level = RAW_OPEN_NTCREATEX;
1208         io.ntcreatex.in.root_fid.fnum = 0;
1209         io.ntcreatex.in.flags = 0;
1210         io.ntcreatex.in.access_mask = SEC_FILE_ALL;
1211         io.ntcreatex.in.create_options = NTCREATEX_OPTIONS_DIRECTORY;
1212         io.ntcreatex.in.file_attr = FILE_ATTRIBUTE_NORMAL;
1213         io.ntcreatex.in.share_access = NTCREATEX_SHARE_ACCESS_READ | NTCREATEX_SHARE_ACCESS_WRITE;
1214         io.ntcreatex.in.alloc_size = 0;
1215         io.ntcreatex.in.open_disposition = NTCREATEX_DISP_OPEN_IF;
1216         io.ntcreatex.in.impersonation = NTCREATEX_IMPERSONATION_ANONYMOUS;
1217         io.ntcreatex.in.security_flags = 0;
1218
1219         notify.nttrans.level = RAW_NOTIFY_NTTRANS;
1220         notify.nttrans.in.buffer_size = 20000;
1221
1222         /*
1223           setup the directory tree, and the notify buffer on each directory
1224         */
1225         for (i=0;i<ARRAY_SIZE(dirs);i++) {
1226                 io.ntcreatex.in.fname = dirs[i].path;
1227                 status = smb_raw_open(cli->tree, mem_ctx, &io);
1228                 CHECK_STATUS(status, NT_STATUS_OK);
1229                 dirs[i].fnum = io.ntcreatex.out.file.fnum;
1230
1231                 notify.nttrans.in.completion_filter = dirs[i].filter;
1232                 notify.nttrans.in.file.fnum = dirs[i].fnum;
1233                 notify.nttrans.in.recursive = dirs[i].recursive;
1234                 req = smb_raw_changenotify_send(cli->tree, &notify);
1235                 smb_raw_ntcancel(req);
1236                 status = smb_raw_changenotify_recv(req, mem_ctx, &notify);
1237                 CHECK_STATUS(status, NT_STATUS_CANCELLED);
1238         }
1239
1240         /* trigger 2 events in each dir */
1241         for (i=0;i<ARRAY_SIZE(dirs);i++) {
1242                 char *path = talloc_asprintf(mem_ctx, "%s\\test.dir", dirs[i].path);
1243                 smbcli_mkdir(cli->tree, path);
1244                 smbcli_rmdir(cli->tree, path);
1245                 talloc_free(path);
1246         }
1247
1248         /* give a bit of time for the events to propogate */
1249         tv = timeval_current();
1250
1251         do {
1252                 /* count events that have happened in each dir */
1253                 for (i=0;i<ARRAY_SIZE(dirs);i++) {
1254                         notify.nttrans.in.file.fnum = dirs[i].fnum;
1255                         req = smb_raw_changenotify_send(cli->tree, &notify);
1256                         smb_raw_ntcancel(req);
1257                         notify.nttrans.out.num_changes = 0;
1258                         status = smb_raw_changenotify_recv(req, mem_ctx, &notify);
1259                         dirs[i].counted += notify.nttrans.out.num_changes;
1260                 }
1261                 
1262                 all_done = true;
1263
1264                 for (i=0;i<ARRAY_SIZE(dirs);i++) {
1265                         if (dirs[i].counted != dirs[i].expected) {
1266                                 all_done = false;
1267                         }
1268                 }
1269         } while (!all_done && timeval_elapsed(&tv) < 20);
1270
1271         printf("took %.4f seconds to propogate all events\n", timeval_elapsed(&tv));
1272
1273         for (i=0;i<ARRAY_SIZE(dirs);i++) {
1274                 if (dirs[i].counted != dirs[i].expected) {
1275                         printf("ERROR: i=%d expected %d got %d for '%s'\n",
1276                                i, dirs[i].expected, dirs[i].counted, dirs[i].path);
1277                         ret = false;
1278                 }
1279         }
1280
1281         /*
1282           run from the back, closing and deleting
1283         */
1284         for (i=ARRAY_SIZE(dirs)-1;i>=0;i--) {
1285                 smbcli_close(cli->tree, dirs[i].fnum);
1286                 smbcli_rmdir(cli->tree, dirs[i].path);
1287         }
1288
1289 done:
1290         smb_raw_exit(cli->session);
1291         return ret;
1292 }
1293
1294 /*
1295    Test response when cached server events exceed single NT NOTFIY response
1296    packet size.
1297 */
1298 static bool test_notify_overflow(struct smbcli_state *cli, TALLOC_CTX *mem_ctx)
1299 {
1300         bool ret = true;
1301         NTSTATUS status;
1302         union smb_notify notify;
1303         union smb_open io;
1304         int fnum;
1305         int count = 100;
1306         struct smbcli_request *req1;
1307         int i;
1308
1309         printf("TESTING CHANGE NOTIFY EVENT OVERFLOW\n");
1310
1311         /* get a handle on the directory */
1312         io.generic.level = RAW_OPEN_NTCREATEX;
1313         io.ntcreatex.in.root_fid.fnum = 0;
1314         io.ntcreatex.in.flags = 0;
1315         io.ntcreatex.in.access_mask = SEC_FILE_ALL;
1316         io.ntcreatex.in.create_options = NTCREATEX_OPTIONS_DIRECTORY;
1317         io.ntcreatex.in.file_attr = FILE_ATTRIBUTE_NORMAL;
1318         io.ntcreatex.in.share_access = NTCREATEX_SHARE_ACCESS_READ |
1319             NTCREATEX_SHARE_ACCESS_WRITE;
1320         io.ntcreatex.in.alloc_size = 0;
1321         io.ntcreatex.in.open_disposition = NTCREATEX_DISP_OPEN;
1322         io.ntcreatex.in.impersonation = NTCREATEX_IMPERSONATION_ANONYMOUS;
1323         io.ntcreatex.in.security_flags = 0;
1324         io.ntcreatex.in.fname = BASEDIR;
1325
1326         status = smb_raw_open(cli->tree, mem_ctx, &io);
1327         CHECK_STATUS(status, NT_STATUS_OK);
1328         fnum = io.ntcreatex.out.file.fnum;
1329
1330         /* ask for a change notify, on name changes. */
1331         notify.nttrans.level = RAW_NOTIFY_NTTRANS;
1332         notify.nttrans.in.buffer_size = 1000;
1333         notify.nttrans.in.completion_filter = FILE_NOTIFY_CHANGE_NAME;
1334         notify.nttrans.in.file.fnum = fnum;
1335
1336         notify.nttrans.in.recursive = true;
1337         req1 = smb_raw_changenotify_send(cli->tree, &notify);
1338
1339         /* cancel initial requests so the buffer is setup */
1340         smb_raw_ntcancel(req1);
1341         status = smb_raw_changenotify_recv(req1, mem_ctx, &notify);
1342         CHECK_STATUS(status, NT_STATUS_CANCELLED);
1343
1344         /* open a lot of files, filling up the server side notify buffer */
1345         printf("Testing overflowed buffer notify on create of %d files\n",
1346                count);
1347         for (i=0;i<count;i++) {
1348                 char *fname = talloc_asprintf(cli, BASEDIR "\\test%d.txt", i);
1349                 int fnum2 = smbcli_open(cli->tree, fname, O_CREAT|O_RDWR,
1350                                         DENY_NONE);
1351                 if (fnum2 == -1) {
1352                         printf("Failed to create %s - %s\n",
1353                                fname, smbcli_errstr(cli->tree));
1354                         ret = false;
1355                         goto done;
1356                 }
1357                 talloc_free(fname);
1358                 smbcli_close(cli->tree, fnum2);
1359         }
1360
1361         /* expect that 0 events will be returned with NT_STATUS_OK */
1362         req1 = smb_raw_changenotify_send(cli->tree, &notify);
1363         status = smb_raw_changenotify_recv(req1, mem_ctx, &notify);
1364         CHECK_STATUS(status, NT_STATUS_OK);
1365         CHECK_VAL(notify.nttrans.out.num_changes, 0);
1366
1367 done:
1368         smb_raw_exit(cli->session);
1369         return ret;
1370 }
1371
1372 /*
1373    Test if notifications are returned for changes to the base directory.
1374    They shouldn't be.
1375 */
1376 static bool test_notify_basedir(struct smbcli_state *cli, TALLOC_CTX *mem_ctx)
1377 {
1378         bool ret = true;
1379         NTSTATUS status;
1380         union smb_notify notify;
1381         union smb_open io;
1382         int fnum;
1383         struct smbcli_request *req1;
1384
1385         printf("TESTING CHANGE NOTIFY BASEDIR EVENTS\n");
1386
1387         /* get a handle on the directory */
1388         io.generic.level = RAW_OPEN_NTCREATEX;
1389         io.ntcreatex.in.root_fid.fnum = 0;
1390         io.ntcreatex.in.flags = 0;
1391         io.ntcreatex.in.access_mask = SEC_FILE_ALL;
1392         io.ntcreatex.in.create_options = NTCREATEX_OPTIONS_DIRECTORY;
1393         io.ntcreatex.in.file_attr = FILE_ATTRIBUTE_NORMAL;
1394         io.ntcreatex.in.share_access = NTCREATEX_SHARE_ACCESS_READ |
1395             NTCREATEX_SHARE_ACCESS_WRITE;
1396         io.ntcreatex.in.alloc_size = 0;
1397         io.ntcreatex.in.open_disposition = NTCREATEX_DISP_OPEN;
1398         io.ntcreatex.in.impersonation = NTCREATEX_IMPERSONATION_ANONYMOUS;
1399         io.ntcreatex.in.security_flags = 0;
1400         io.ntcreatex.in.fname = BASEDIR;
1401
1402         status = smb_raw_open(cli->tree, mem_ctx, &io);
1403         CHECK_STATUS(status, NT_STATUS_OK);
1404         fnum = io.ntcreatex.out.file.fnum;
1405
1406         /* create a test file that will also be modified */
1407         smbcli_close(cli->tree, smbcli_open(cli->tree, BASEDIR "\\tname1",
1408                                             O_CREAT, 0));
1409
1410         /* ask for a change notify, on attribute changes. */
1411         notify.nttrans.level = RAW_NOTIFY_NTTRANS;
1412         notify.nttrans.in.buffer_size = 1000;
1413         notify.nttrans.in.completion_filter = FILE_NOTIFY_CHANGE_ATTRIBUTES;
1414         notify.nttrans.in.file.fnum = fnum;
1415         notify.nttrans.in.recursive = true;
1416
1417         req1 = smb_raw_changenotify_send(cli->tree, &notify);
1418
1419         /* set attribute on the base dir */
1420         smbcli_setatr(cli->tree, BASEDIR, FILE_ATTRIBUTE_HIDDEN, 0);
1421
1422         /* set attribute on a file to assure we receive a notification */
1423         smbcli_setatr(cli->tree, BASEDIR "\\tname1", FILE_ATTRIBUTE_HIDDEN, 0);
1424         smb_msleep(200);
1425
1426         /* check how many responses were given, expect only 1 for the file */
1427         status = smb_raw_changenotify_recv(req1, mem_ctx, &notify);
1428         CHECK_STATUS(status, NT_STATUS_OK);
1429         CHECK_VAL(notify.nttrans.out.num_changes, 1);
1430         CHECK_VAL(notify.nttrans.out.changes[0].action, NOTIFY_ACTION_MODIFIED);
1431         CHECK_WSTR(notify.nttrans.out.changes[0].name, "tname1", STR_UNICODE);
1432
1433 done:
1434         smb_raw_exit(cli->session);
1435         return ret;
1436 }
1437
1438
1439 /*
1440   create a secondary tree connect - used to test for a bug in Samba3 messaging
1441   with change notify
1442 */
1443 static struct smbcli_tree *secondary_tcon(struct smbcli_state *cli, 
1444                                           struct torture_context *tctx)
1445 {
1446         NTSTATUS status;
1447         const char *share, *host;
1448         struct smbcli_tree *tree;
1449         union smb_tcon tcon;
1450
1451         share = torture_setting_string(tctx, "share", NULL);
1452         host  = torture_setting_string(tctx, "host", NULL);
1453         
1454         printf("create a second tree context on the same session\n");
1455         tree = smbcli_tree_init(cli->session, tctx, false);
1456
1457         tcon.generic.level = RAW_TCON_TCONX;
1458         tcon.tconx.in.flags = 0;
1459         tcon.tconx.in.password = data_blob(NULL, 0);
1460         tcon.tconx.in.path = talloc_asprintf(tctx, "\\\\%s\\%s", host, share);
1461         tcon.tconx.in.device = "A:";    
1462         status = smb_raw_tcon(tree, tctx, &tcon);
1463         if (!NT_STATUS_IS_OK(status)) {
1464                 talloc_free(tree);
1465                 printf("Failed to create secondary tree\n");
1466                 return NULL;
1467         }
1468
1469         tree->tid = tcon.tconx.out.tid;
1470         printf("tid1=%d tid2=%d\n", cli->tree->tid, tree->tid);
1471
1472         return tree;
1473 }
1474
1475
1476 /* 
1477    very simple change notify test
1478 */
1479 static bool test_notify_tcon(struct smbcli_state *cli, struct torture_context *torture)
1480 {
1481         bool ret = true;
1482         NTSTATUS status;
1483         union smb_notify notify;
1484         union smb_open io;
1485         int fnum, fnum2;
1486         struct smbcli_request *req;
1487         extern int torture_numops;
1488         struct smbcli_tree *tree = NULL;
1489                 
1490         printf("TESTING SIMPLE CHANGE NOTIFY\n");
1491                 
1492         /*
1493           get a handle on the directory
1494         */
1495         io.generic.level = RAW_OPEN_NTCREATEX;
1496         io.ntcreatex.in.root_fid.fnum = 0;
1497         io.ntcreatex.in.flags = 0;
1498         io.ntcreatex.in.access_mask = SEC_FILE_ALL;
1499         io.ntcreatex.in.create_options = NTCREATEX_OPTIONS_DIRECTORY;
1500         io.ntcreatex.in.file_attr = FILE_ATTRIBUTE_NORMAL;
1501         io.ntcreatex.in.share_access = NTCREATEX_SHARE_ACCESS_READ | NTCREATEX_SHARE_ACCESS_WRITE;
1502         io.ntcreatex.in.alloc_size = 0;
1503         io.ntcreatex.in.open_disposition = NTCREATEX_DISP_OPEN;
1504         io.ntcreatex.in.impersonation = NTCREATEX_IMPERSONATION_ANONYMOUS;
1505         io.ntcreatex.in.security_flags = 0;
1506         io.ntcreatex.in.fname = BASEDIR;
1507
1508         status = smb_raw_open(cli->tree, torture, &io);
1509         CHECK_STATUS(status, NT_STATUS_OK);
1510         fnum = io.ntcreatex.out.file.fnum;
1511
1512         status = smb_raw_open(cli->tree, torture, &io);
1513         CHECK_STATUS(status, NT_STATUS_OK);
1514         fnum2 = io.ntcreatex.out.file.fnum;
1515
1516         /* ask for a change notify,
1517            on file or directory name changes */
1518         notify.nttrans.level = RAW_NOTIFY_NTTRANS;
1519         notify.nttrans.in.buffer_size = 1000;
1520         notify.nttrans.in.completion_filter = FILE_NOTIFY_CHANGE_NAME;
1521         notify.nttrans.in.file.fnum = fnum;
1522         notify.nttrans.in.recursive = true;
1523
1524         printf("Testing notify mkdir\n");
1525         req = smb_raw_changenotify_send(cli->tree, &notify);
1526         smbcli_mkdir(cli->tree, BASEDIR "\\subdir-name");
1527
1528         status = smb_raw_changenotify_recv(req, torture, &notify);
1529         CHECK_STATUS(status, NT_STATUS_OK);
1530
1531         CHECK_VAL(notify.nttrans.out.num_changes, 1);
1532         CHECK_VAL(notify.nttrans.out.changes[0].action, NOTIFY_ACTION_ADDED);
1533         CHECK_WSTR(notify.nttrans.out.changes[0].name, "subdir-name", STR_UNICODE);
1534
1535         printf("Testing notify rmdir\n");
1536         req = smb_raw_changenotify_send(cli->tree, &notify);
1537         smbcli_rmdir(cli->tree, BASEDIR "\\subdir-name");
1538
1539         status = smb_raw_changenotify_recv(req, torture, &notify);
1540         CHECK_STATUS(status, NT_STATUS_OK);
1541         CHECK_VAL(notify.nttrans.out.num_changes, 1);
1542         CHECK_VAL(notify.nttrans.out.changes[0].action, NOTIFY_ACTION_REMOVED);
1543         CHECK_WSTR(notify.nttrans.out.changes[0].name, "subdir-name", STR_UNICODE);
1544
1545         printf("SIMPLE CHANGE NOTIFY OK\n");
1546
1547         printf("TESTING WITH SECONDARY TCON\n");
1548         tree = secondary_tcon(cli, torture);
1549
1550         printf("Testing notify mkdir\n");
1551         req = smb_raw_changenotify_send(cli->tree, &notify);
1552         smbcli_mkdir(cli->tree, BASEDIR "\\subdir-name");
1553
1554         status = smb_raw_changenotify_recv(req, torture, &notify);
1555         CHECK_STATUS(status, NT_STATUS_OK);
1556
1557         CHECK_VAL(notify.nttrans.out.num_changes, 1);
1558         CHECK_VAL(notify.nttrans.out.changes[0].action, NOTIFY_ACTION_ADDED);
1559         CHECK_WSTR(notify.nttrans.out.changes[0].name, "subdir-name", STR_UNICODE);
1560
1561         printf("Testing notify rmdir\n");
1562         req = smb_raw_changenotify_send(cli->tree, &notify);
1563         smbcli_rmdir(cli->tree, BASEDIR "\\subdir-name");
1564
1565         status = smb_raw_changenotify_recv(req, torture, &notify);
1566         CHECK_STATUS(status, NT_STATUS_OK);
1567         CHECK_VAL(notify.nttrans.out.num_changes, 1);
1568         CHECK_VAL(notify.nttrans.out.changes[0].action, NOTIFY_ACTION_REMOVED);
1569         CHECK_WSTR(notify.nttrans.out.changes[0].name, "subdir-name", STR_UNICODE);
1570
1571         printf("CHANGE NOTIFY WITH TCON OK\n");
1572
1573         printf("Disconnecting secondary tree\n");
1574         status = smb_tree_disconnect(tree);
1575         CHECK_STATUS(status, NT_STATUS_OK);
1576         talloc_free(tree);
1577
1578         printf("Testing notify mkdir\n");
1579         req = smb_raw_changenotify_send(cli->tree, &notify);
1580         smbcli_mkdir(cli->tree, BASEDIR "\\subdir-name");
1581
1582         status = smb_raw_changenotify_recv(req, torture, &notify);
1583         CHECK_STATUS(status, NT_STATUS_OK);
1584
1585         CHECK_VAL(notify.nttrans.out.num_changes, 1);
1586         CHECK_VAL(notify.nttrans.out.changes[0].action, NOTIFY_ACTION_ADDED);
1587         CHECK_WSTR(notify.nttrans.out.changes[0].name, "subdir-name", STR_UNICODE);
1588
1589         printf("Testing notify rmdir\n");
1590         req = smb_raw_changenotify_send(cli->tree, &notify);
1591         smbcli_rmdir(cli->tree, BASEDIR "\\subdir-name");
1592
1593         status = smb_raw_changenotify_recv(req, torture, &notify);
1594         CHECK_STATUS(status, NT_STATUS_OK);
1595         CHECK_VAL(notify.nttrans.out.num_changes, 1);
1596         CHECK_VAL(notify.nttrans.out.changes[0].action, NOTIFY_ACTION_REMOVED);
1597         CHECK_WSTR(notify.nttrans.out.changes[0].name, "subdir-name", STR_UNICODE);
1598
1599         printf("CHANGE NOTIFY WITH TDIS OK\n");
1600 done:
1601         smb_raw_exit(cli->session);
1602         return ret;
1603 }
1604
1605
1606 /*
1607    testing alignment of multiple change notify infos
1608 */
1609 static bool test_notify_alignment(struct smbcli_state *cli,
1610     struct torture_context *tctx)
1611 {
1612         NTSTATUS status;
1613         union smb_notify notify;
1614         union smb_open io;
1615         int i, fnum, fnum2;
1616         struct smbcli_request *req;
1617         const char *fname = BASEDIR "\\starter";
1618         const char *fnames[] = { "a",
1619                                  "ab",
1620                                  "abc",
1621                                  "abcd" };
1622         int num_names = ARRAY_SIZE(fnames);
1623         char *fpath = NULL;
1624
1625         torture_comment(tctx, "TESTING CHANGE NOTIFY REPLY ALIGNMENT\n");
1626
1627         /* get a handle on the directory */
1628         io.generic.level = RAW_OPEN_NTCREATEX;
1629         io.ntcreatex.in.root_fid.fnum = 0;
1630         io.ntcreatex.in.flags = 0;
1631         io.ntcreatex.in.access_mask = SEC_FILE_ALL;
1632         io.ntcreatex.in.create_options = NTCREATEX_OPTIONS_DIRECTORY;
1633         io.ntcreatex.in.file_attr = FILE_ATTRIBUTE_NORMAL;
1634         io.ntcreatex.in.share_access = NTCREATEX_SHARE_ACCESS_READ |
1635                                        NTCREATEX_SHARE_ACCESS_WRITE;
1636         io.ntcreatex.in.alloc_size = 0;
1637         io.ntcreatex.in.open_disposition = NTCREATEX_DISP_OPEN;
1638         io.ntcreatex.in.impersonation = NTCREATEX_IMPERSONATION_ANONYMOUS;
1639         io.ntcreatex.in.security_flags = 0;
1640         io.ntcreatex.in.fname = BASEDIR;
1641
1642         status = smb_raw_open(cli->tree, tctx, &io);
1643         torture_assert_ntstatus_ok(tctx, status, "");
1644         fnum = io.ntcreatex.out.file.fnum;
1645
1646         /* ask for a change notify, on file creation */
1647         notify.nttrans.level = RAW_NOTIFY_NTTRANS;
1648         notify.nttrans.in.buffer_size = 1000;
1649         notify.nttrans.in.completion_filter = FILE_NOTIFY_CHANGE_FILE_NAME;
1650         notify.nttrans.in.file.fnum = fnum;
1651         notify.nttrans.in.recursive = false;
1652
1653         /* start change tracking */
1654         req = smb_raw_changenotify_send(cli->tree, &notify);
1655
1656         fnum2 = smbcli_open(cli->tree, fname, O_CREAT|O_RDWR, DENY_NONE);
1657         torture_assert(tctx, fnum2 != -1, smbcli_errstr(cli->tree));
1658         smbcli_close(cli->tree, fnum2);
1659
1660         status = smb_raw_changenotify_recv(req, tctx, &notify);
1661         torture_assert_ntstatus_ok(tctx, status, "");
1662
1663         /* create 4 files that will cause CHANGE_NOTIFY_INFO structures
1664          * to be returned in the same packet with all possible 4-byte padding
1665          * permutations.  As per MS-CIFS 2.2.7.4.2 these structures should be
1666          * 4-byte aligned. */
1667
1668         for (i = 0; i < num_names; i++) {
1669                 fpath = talloc_asprintf(tctx, "%s\\%s", BASEDIR, fnames[i]);
1670                 fnum2 = smbcli_open(cli->tree, fpath,
1671                     O_CREAT|O_RDWR, DENY_NONE);
1672                 torture_assert(tctx, fnum2 != -1, smbcli_errstr(cli->tree));
1673                 smbcli_close(cli->tree, fnum2);
1674                 talloc_free(fpath);
1675         }
1676
1677         /* We send a notify packet, and let smb_raw_changenotify_recv() do
1678          * the alignment checking for us. */
1679         req = smb_raw_changenotify_send(cli->tree, &notify);
1680         status = smb_raw_changenotify_recv(req, tctx, &notify);
1681         torture_assert_ntstatus_ok(tctx, status, "");
1682
1683         /* Do basic checking for correctness. */
1684         torture_assert(tctx, notify.nttrans.out.num_changes == num_names, "");
1685         for (i = 0; i < num_names; i++) {
1686                 torture_assert(tctx, notify.nttrans.out.changes[i].action ==
1687                     NOTIFY_ACTION_ADDED, "");
1688                 CHECK_WSTR2(tctx, notify.nttrans.out.changes[i].name, fnames[i],
1689                     STR_UNICODE);
1690         }
1691
1692         return true;
1693 }
1694
1695 /*
1696    basic testing of change notify
1697 */
1698 bool torture_raw_notify(struct torture_context *torture, 
1699                         struct smbcli_state *cli, 
1700                         struct smbcli_state *cli2)
1701 {
1702         bool ret = true;
1703
1704         if (!torture_setup_dir(cli, BASEDIR)) {
1705                 return false;
1706         }
1707
1708         ret &= test_notify_tcon(cli, torture);
1709         ret &= test_notify_dir(cli, cli2, torture);
1710         ret &= test_notify_mask(cli, torture);
1711         ret &= test_notify_recursive(cli, torture);
1712         ret &= test_notify_mask_change(cli, torture);
1713         ret &= test_notify_file(cli, torture);
1714         ret &= test_notify_tdis(torture);
1715         ret &= test_notify_exit(torture);
1716         ret &= test_notify_ulogoff(torture);
1717         ret &= test_notify_tcp_dis(torture);
1718         ret &= test_notify_double(cli, torture);
1719         ret &= test_notify_tree(cli, torture);
1720         ret &= test_notify_overflow(cli, torture);
1721         ret &= test_notify_basedir(cli, torture);
1722         ret &= test_notify_alignment(cli, torture);
1723
1724         smb_raw_exit(cli->session);
1725         smbcli_deltree(cli->tree, BASEDIR);
1726         return ret;
1727 }