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