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