s4:torture:raw:notify: treat torture_open_connection calls with torture_assert
[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         torture_assert(tctx, torture_open_connection(&cli, tctx, 0),
1027                        "Failed to open connection.");
1028
1029         /*
1030           get a handle on the directory
1031         */
1032         io.generic.level = RAW_OPEN_NTCREATEX;
1033         io.ntcreatex.in.root_fid.fnum = 0;
1034         io.ntcreatex.in.flags = 0;
1035         io.ntcreatex.in.access_mask = SEC_FILE_ALL;
1036         io.ntcreatex.in.create_options = NTCREATEX_OPTIONS_DIRECTORY;
1037         io.ntcreatex.in.file_attr = FILE_ATTRIBUTE_NORMAL;
1038         io.ntcreatex.in.share_access = NTCREATEX_SHARE_ACCESS_READ | NTCREATEX_SHARE_ACCESS_WRITE;
1039         io.ntcreatex.in.alloc_size = 0;
1040         io.ntcreatex.in.open_disposition = NTCREATEX_DISP_OPEN;
1041         io.ntcreatex.in.impersonation = NTCREATEX_IMPERSONATION_ANONYMOUS;
1042         io.ntcreatex.in.security_flags = 0;
1043         io.ntcreatex.in.fname = BASEDIR;
1044
1045         status = smb_raw_open(cli->tree, tctx, &io);
1046         torture_assert_ntstatus_ok_goto(tctx, status, ret, done,
1047                                         "smb_raw_open");
1048         fnum = io.ntcreatex.out.file.fnum;
1049
1050         /* ask for a change notify,
1051            on file or directory name changes */
1052         notify.nttrans.level = RAW_NOTIFY_NTTRANS;
1053         notify.nttrans.in.buffer_size = 1000;
1054         notify.nttrans.in.completion_filter = FILE_NOTIFY_CHANGE_NAME;
1055         notify.nttrans.in.file.fnum = fnum;
1056         notify.nttrans.in.recursive = true;
1057
1058         req = smb_raw_changenotify_send(cli->tree, &notify);
1059
1060         status = smbcli_tdis(cli);
1061         torture_assert_ntstatus_ok_goto(tctx, status, ret, done,
1062                                         "smbcli_tdis");
1063         cli->tree = NULL;
1064
1065         status = smb_raw_changenotify_recv(req, tctx, &notify);
1066         torture_assert_ntstatus_ok_goto(tctx, status, ret, done,
1067                                         "smb_raw_changenotify_recv");
1068         torture_assert_int_equal_goto(tctx, notify.nttrans.out.num_changes,
1069                                       0, ret, done, "no changes expected");
1070
1071 done:
1072         torture_close_connection(cli);
1073         smbcli_deltree(cli1->tree, BASEDIR);
1074         return ret;
1075 }
1076
1077 /*
1078   basic testing of change notifies followed by a exit
1079 */
1080 static bool test_notify_exit(struct torture_context *tctx,
1081                              struct smbcli_state *cli1)
1082 {
1083         bool ret = true;
1084         NTSTATUS status;
1085         union smb_notify notify;
1086         union smb_open io;
1087         int fnum;
1088         struct smbcli_request *req;
1089         struct smbcli_state *cli = NULL;
1090
1091         printf("TESTING CHANGE NOTIFY FOLLOWED BY EXIT\n");
1092
1093         torture_assert(tctx, torture_setup_dir(cli1, BASEDIR),
1094                        "Failed to setup up test directory: " BASEDIR);
1095
1096         torture_assert(tctx, torture_open_connection(&cli, tctx, 0),
1097                        "Failed to open connection.");
1098
1099         /*
1100           get a handle on the directory
1101         */
1102         io.generic.level = RAW_OPEN_NTCREATEX;
1103         io.ntcreatex.in.root_fid.fnum = 0;
1104         io.ntcreatex.in.flags = 0;
1105         io.ntcreatex.in.access_mask = SEC_FILE_ALL;
1106         io.ntcreatex.in.create_options = NTCREATEX_OPTIONS_DIRECTORY;
1107         io.ntcreatex.in.file_attr = FILE_ATTRIBUTE_NORMAL;
1108         io.ntcreatex.in.share_access = NTCREATEX_SHARE_ACCESS_READ | NTCREATEX_SHARE_ACCESS_WRITE;
1109         io.ntcreatex.in.alloc_size = 0;
1110         io.ntcreatex.in.open_disposition = NTCREATEX_DISP_OPEN;
1111         io.ntcreatex.in.impersonation = NTCREATEX_IMPERSONATION_ANONYMOUS;
1112         io.ntcreatex.in.security_flags = 0;
1113         io.ntcreatex.in.fname = BASEDIR;
1114
1115         status = smb_raw_open(cli->tree, tctx, &io);
1116         torture_assert_ntstatus_ok_goto(tctx, status, ret, done,
1117                                         "smb_raw_open");
1118         fnum = io.ntcreatex.out.file.fnum;
1119
1120         /* ask for a change notify,
1121            on file or directory name changes */
1122         notify.nttrans.level = RAW_NOTIFY_NTTRANS;
1123         notify.nttrans.in.buffer_size = 1000;
1124         notify.nttrans.in.completion_filter = FILE_NOTIFY_CHANGE_NAME;
1125         notify.nttrans.in.file.fnum = fnum;
1126         notify.nttrans.in.recursive = true;
1127
1128         req = smb_raw_changenotify_send(cli->tree, &notify);
1129
1130         status = smb_raw_exit(cli->session);
1131         torture_assert_ntstatus_ok_goto(tctx, status, ret, done,
1132                                         "smb_raw_exit");
1133
1134         status = smb_raw_changenotify_recv(req, tctx, &notify);
1135         torture_assert_ntstatus_ok_goto(tctx, status, ret, done,
1136                                         "smb_raw_changenotify_recv");
1137         torture_assert_int_equal_goto(tctx, notify.nttrans.out.num_changes,
1138                                       0, ret, done, "no changes expected");
1139
1140 done:
1141         torture_close_connection(cli);
1142         smbcli_deltree(cli1->tree, BASEDIR);
1143         return ret;
1144 }
1145
1146 /*
1147   basic testing of change notifies followed by a ulogoff
1148 */
1149 static bool test_notify_ulogoff(struct torture_context *tctx,
1150                                 struct smbcli_state *cli1)
1151 {
1152         bool ret = true;
1153         NTSTATUS status;
1154         union smb_notify notify;
1155         union smb_open io;
1156         int fnum;
1157         struct smbcli_request *req;
1158         struct smbcli_state *cli = NULL;
1159
1160         printf("TESTING CHANGE NOTIFY FOLLOWED BY ULOGOFF\n");
1161
1162         torture_assert(tctx, torture_setup_dir(cli1, BASEDIR),
1163                        "Failed to setup up test directory: " BASEDIR);
1164
1165         torture_assert(tctx, torture_open_connection(&cli, tctx, 0),
1166                        "Failed to open connection.");
1167
1168         /*
1169           get a handle on the directory
1170         */
1171         io.generic.level = RAW_OPEN_NTCREATEX;
1172         io.ntcreatex.in.root_fid.fnum = 0;
1173         io.ntcreatex.in.flags = 0;
1174         io.ntcreatex.in.access_mask = SEC_FILE_ALL;
1175         io.ntcreatex.in.create_options = NTCREATEX_OPTIONS_DIRECTORY;
1176         io.ntcreatex.in.file_attr = FILE_ATTRIBUTE_NORMAL;
1177         io.ntcreatex.in.share_access = NTCREATEX_SHARE_ACCESS_READ | NTCREATEX_SHARE_ACCESS_WRITE;
1178         io.ntcreatex.in.alloc_size = 0;
1179         io.ntcreatex.in.open_disposition = NTCREATEX_DISP_OPEN;
1180         io.ntcreatex.in.impersonation = NTCREATEX_IMPERSONATION_ANONYMOUS;
1181         io.ntcreatex.in.security_flags = 0;
1182         io.ntcreatex.in.fname = BASEDIR;
1183
1184         status = smb_raw_open(cli->tree, tctx, &io);
1185         torture_assert_ntstatus_ok_goto(tctx, status, ret, done,
1186                                         "smb_raw_open");
1187         fnum = io.ntcreatex.out.file.fnum;
1188
1189         /* ask for a change notify,
1190            on file or directory name changes */
1191         notify.nttrans.level = RAW_NOTIFY_NTTRANS;
1192         notify.nttrans.in.buffer_size = 1000;
1193         notify.nttrans.in.completion_filter = FILE_NOTIFY_CHANGE_NAME;
1194         notify.nttrans.in.file.fnum = fnum;
1195         notify.nttrans.in.recursive = true;
1196
1197         req = smb_raw_changenotify_send(cli->tree, &notify);
1198
1199         status = smb_raw_ulogoff(cli->session);
1200         torture_assert_ntstatus_ok_goto(tctx, status, ret, done,
1201                                         "smb_raw_ulogoff");
1202
1203         status = smb_raw_changenotify_recv(req, tctx, &notify);
1204         torture_assert_ntstatus_ok_goto(tctx, status, ret, done,
1205                                         "smb_raw_changenotify_recv");
1206         torture_assert_int_equal_goto(tctx, notify.nttrans.out.num_changes,
1207                                       0, ret, done, "no changes expected");
1208
1209 done:
1210         torture_close_connection(cli);
1211         smbcli_deltree(cli1->tree, BASEDIR);
1212         return ret;
1213 }
1214
1215 static void tcp_dis_handler(struct smbcli_transport *t, void *p)
1216 {
1217         struct smbcli_state *cli = (struct smbcli_state *)p;
1218         smbcli_transport_dead(cli->transport, NT_STATUS_LOCAL_DISCONNECT);
1219         cli->transport = NULL;
1220         cli->tree = NULL;
1221 }
1222 /*
1223   basic testing of change notifies followed by tcp disconnect
1224 */
1225 static bool test_notify_tcp_dis(struct torture_context *tctx,
1226                                 struct smbcli_state *cli1)
1227 {
1228         bool ret = true;
1229         NTSTATUS status;
1230         union smb_notify notify;
1231         union smb_open io;
1232         int fnum;
1233         struct smbcli_request *req;
1234         struct smbcli_state *cli = NULL;
1235
1236         printf("TESTING CHANGE NOTIFY FOLLOWED BY TCP DISCONNECT\n");
1237
1238         torture_assert(tctx, torture_setup_dir(cli1, BASEDIR),
1239                        "Failed to setup up test directory: " BASEDIR);
1240
1241         torture_assert(tctx, torture_open_connection(&cli, tctx, 0),
1242                        "Failed to open connection.");
1243
1244         /*
1245           get a handle on the directory
1246         */
1247         io.generic.level = RAW_OPEN_NTCREATEX;
1248         io.ntcreatex.in.root_fid.fnum = 0;
1249         io.ntcreatex.in.flags = 0;
1250         io.ntcreatex.in.access_mask = SEC_FILE_ALL;
1251         io.ntcreatex.in.create_options = NTCREATEX_OPTIONS_DIRECTORY;
1252         io.ntcreatex.in.file_attr = FILE_ATTRIBUTE_NORMAL;
1253         io.ntcreatex.in.share_access = NTCREATEX_SHARE_ACCESS_READ | NTCREATEX_SHARE_ACCESS_WRITE;
1254         io.ntcreatex.in.alloc_size = 0;
1255         io.ntcreatex.in.open_disposition = NTCREATEX_DISP_OPEN;
1256         io.ntcreatex.in.impersonation = NTCREATEX_IMPERSONATION_ANONYMOUS;
1257         io.ntcreatex.in.security_flags = 0;
1258         io.ntcreatex.in.fname = BASEDIR;
1259
1260         status = smb_raw_open(cli->tree, tctx, &io);
1261         torture_assert_ntstatus_ok_goto(tctx, status, ret, done,
1262                                         "smb_raw_open");
1263         fnum = io.ntcreatex.out.file.fnum;
1264
1265         /* ask for a change notify,
1266            on file or directory name changes */
1267         notify.nttrans.level = RAW_NOTIFY_NTTRANS;
1268         notify.nttrans.in.buffer_size = 1000;
1269         notify.nttrans.in.completion_filter = FILE_NOTIFY_CHANGE_NAME;
1270         notify.nttrans.in.file.fnum = fnum;
1271         notify.nttrans.in.recursive = true;
1272
1273         req = smb_raw_changenotify_send(cli->tree, &notify);
1274
1275         smbcli_transport_idle_handler(cli->transport, tcp_dis_handler, 250, cli);
1276
1277         status = smb_raw_changenotify_recv(req, tctx, &notify);
1278         torture_assert_ntstatus_equal_goto(tctx, status,
1279                                            NT_STATUS_LOCAL_DISCONNECT,
1280                                            ret, done,
1281                                            "smb_raw_changenotify_recv");
1282
1283 done:
1284         torture_close_connection(cli);
1285         smbcli_deltree(cli1->tree, BASEDIR);
1286         return ret;
1287 }
1288
1289 /* 
1290    test setting up two change notify requests on one handle
1291 */
1292 static bool test_notify_double(struct torture_context *mem_ctx,
1293                                struct smbcli_state *cli)
1294 {
1295         bool ret = true;
1296         NTSTATUS status;
1297         union smb_notify notify;
1298         union smb_open io;
1299         int fnum;
1300         struct smbcli_request *req1, *req2;
1301
1302         printf("TESTING CHANGE NOTIFY TWICE ON ONE DIRECTORY\n");
1303
1304         torture_assert(mem_ctx, torture_setup_dir(cli, BASEDIR),
1305                        "Failed to setup up test directory: " BASEDIR);
1306
1307         /*
1308           get a handle on the directory
1309         */
1310         io.generic.level = RAW_OPEN_NTCREATEX;
1311         io.ntcreatex.in.root_fid.fnum = 0;
1312         io.ntcreatex.in.flags = 0;
1313         io.ntcreatex.in.access_mask = SEC_FILE_ALL;
1314         io.ntcreatex.in.create_options = NTCREATEX_OPTIONS_DIRECTORY;
1315         io.ntcreatex.in.file_attr = FILE_ATTRIBUTE_NORMAL;
1316         io.ntcreatex.in.share_access = NTCREATEX_SHARE_ACCESS_READ | NTCREATEX_SHARE_ACCESS_WRITE;
1317         io.ntcreatex.in.alloc_size = 0;
1318         io.ntcreatex.in.open_disposition = NTCREATEX_DISP_OPEN;
1319         io.ntcreatex.in.impersonation = NTCREATEX_IMPERSONATION_ANONYMOUS;
1320         io.ntcreatex.in.security_flags = 0;
1321         io.ntcreatex.in.fname = BASEDIR;
1322
1323         status = smb_raw_open(cli->tree, mem_ctx, &io);
1324         torture_assert_ntstatus_ok_goto(mem_ctx, status, ret, done,
1325                                         "smb_raw_open");
1326         fnum = io.ntcreatex.out.file.fnum;
1327
1328         /* ask for a change notify,
1329            on file or directory name changes */
1330         notify.nttrans.level = RAW_NOTIFY_NTTRANS;
1331         notify.nttrans.in.buffer_size = 1000;
1332         notify.nttrans.in.completion_filter = FILE_NOTIFY_CHANGE_NAME;
1333         notify.nttrans.in.file.fnum = fnum;
1334         notify.nttrans.in.recursive = true;
1335
1336         req1 = smb_raw_changenotify_send(cli->tree, &notify);
1337         req2 = smb_raw_changenotify_send(cli->tree, &notify);
1338
1339         smbcli_mkdir(cli->tree, BASEDIR "\\subdir-name");
1340
1341         status = smb_raw_changenotify_recv(req1, mem_ctx, &notify);
1342         torture_assert_ntstatus_ok_goto(mem_ctx, status, ret, done,
1343                                         "smb_raw_changenotify_recv");
1344         torture_assert_int_equal_goto(mem_ctx, notify.nttrans.out.num_changes,
1345                                       1, ret, done, "wrong number of changes");
1346         CHECK_WSTR(mem_ctx, notify.nttrans.out.changes[0].name, "subdir-name",
1347                    STR_UNICODE);
1348
1349         smbcli_mkdir(cli->tree, BASEDIR "\\subdir-name2");
1350
1351         status = smb_raw_changenotify_recv(req2, mem_ctx, &notify);
1352         torture_assert_ntstatus_ok_goto(mem_ctx, status, ret, done,
1353                                         "smb_raw_changenotify_recv");
1354         torture_assert_int_equal_goto(mem_ctx, notify.nttrans.out.num_changes,
1355                                       1, ret, done, "wrong number of changes");
1356         CHECK_WSTR(mem_ctx, notify.nttrans.out.changes[0].name, "subdir-name2",
1357                    STR_UNICODE);
1358
1359 done:
1360         smb_raw_exit(cli->session);
1361         smbcli_deltree(cli->tree, BASEDIR);
1362         return ret;
1363 }
1364
1365
1366 /* 
1367    test multiple change notifies at different depths and with/without recursion
1368 */
1369 static bool test_notify_tree(struct torture_context *mem_ctx,
1370                              struct smbcli_state *cli,
1371                              struct smbcli_state *cli2)
1372 {
1373         bool ret = true;
1374         union smb_notify notify;
1375         union smb_open io;
1376         struct smbcli_request *req;
1377         struct timeval tv;
1378         struct {
1379                 const char *path;
1380                 bool recursive;
1381                 uint32_t filter;
1382                 int expected;
1383                 int fnum;
1384                 int counted;
1385         } dirs[] = {
1386                 {BASEDIR "\\abc",               true, FILE_NOTIFY_CHANGE_NAME, 30 },
1387                 {BASEDIR "\\zqy",               true, FILE_NOTIFY_CHANGE_NAME, 8 },
1388                 {BASEDIR "\\atsy",              true, FILE_NOTIFY_CHANGE_NAME, 4 },
1389                 {BASEDIR "\\abc\\foo",          true,  FILE_NOTIFY_CHANGE_NAME, 2 },
1390                 {BASEDIR "\\abc\\blah",         true,  FILE_NOTIFY_CHANGE_NAME, 13 },
1391                 {BASEDIR "\\abc\\blah",         false, FILE_NOTIFY_CHANGE_NAME, 7 },
1392                 {BASEDIR "\\abc\\blah\\a",      true, FILE_NOTIFY_CHANGE_NAME, 2 },
1393                 {BASEDIR "\\abc\\blah\\b",      true, FILE_NOTIFY_CHANGE_NAME, 2 },
1394                 {BASEDIR "\\abc\\blah\\c",      true, FILE_NOTIFY_CHANGE_NAME, 2 },
1395                 {BASEDIR "\\abc\\fooblah",      true, FILE_NOTIFY_CHANGE_NAME, 2 },
1396                 {BASEDIR "\\zqy\\xx",           true, FILE_NOTIFY_CHANGE_NAME, 2 },
1397                 {BASEDIR "\\zqy\\yyy",          true, FILE_NOTIFY_CHANGE_NAME, 2 },
1398                 {BASEDIR "\\zqy\\..",           true, FILE_NOTIFY_CHANGE_NAME, 40 },
1399                 {BASEDIR,                       true, FILE_NOTIFY_CHANGE_NAME, 40 },
1400                 {BASEDIR,                       false,FILE_NOTIFY_CHANGE_NAME, 6 },
1401                 {BASEDIR "\\atsy",              false,FILE_NOTIFY_CHANGE_NAME, 4 },
1402                 {BASEDIR "\\abc",               true, FILE_NOTIFY_CHANGE_NAME, 24 },
1403                 {BASEDIR "\\abc",               false,FILE_NOTIFY_CHANGE_FILE_NAME, 0 },
1404                 {BASEDIR "\\abc",               true, FILE_NOTIFY_CHANGE_FILE_NAME, 0 },
1405                 {BASEDIR "\\abc",               true, FILE_NOTIFY_CHANGE_NAME, 24 },
1406         };
1407         int i;
1408         NTSTATUS status;
1409         bool all_done = false;
1410
1411         printf("TESTING CHANGE NOTIFY FOR DIFFERENT DEPTHS\n");
1412
1413         torture_assert(mem_ctx, torture_setup_dir(cli, BASEDIR),
1414                        "Failed to setup up test directory: " BASEDIR);
1415
1416         io.generic.level = RAW_OPEN_NTCREATEX;
1417         io.ntcreatex.in.root_fid.fnum = 0;
1418         io.ntcreatex.in.flags = 0;
1419         io.ntcreatex.in.access_mask = SEC_FILE_ALL;
1420         io.ntcreatex.in.create_options = NTCREATEX_OPTIONS_DIRECTORY;
1421         io.ntcreatex.in.file_attr = FILE_ATTRIBUTE_NORMAL;
1422         io.ntcreatex.in.share_access = NTCREATEX_SHARE_ACCESS_READ | NTCREATEX_SHARE_ACCESS_WRITE;
1423         io.ntcreatex.in.alloc_size = 0;
1424         io.ntcreatex.in.open_disposition = NTCREATEX_DISP_OPEN_IF;
1425         io.ntcreatex.in.impersonation = NTCREATEX_IMPERSONATION_ANONYMOUS;
1426         io.ntcreatex.in.security_flags = 0;
1427
1428         notify.nttrans.level = RAW_NOTIFY_NTTRANS;
1429         notify.nttrans.in.buffer_size = 20000;
1430
1431         /*
1432           setup the directory tree, and the notify buffer on each directory
1433         */
1434         for (i=0;i<ARRAY_SIZE(dirs);i++) {
1435                 io.ntcreatex.in.fname = dirs[i].path;
1436                 status = smb_raw_open(cli->tree, mem_ctx, &io);
1437                 torture_assert_ntstatus_ok_goto(mem_ctx, status, ret, done,
1438                                                 "smb_raw_open");
1439                 dirs[i].fnum = io.ntcreatex.out.file.fnum;
1440
1441                 notify.nttrans.in.completion_filter = dirs[i].filter;
1442                 notify.nttrans.in.file.fnum = dirs[i].fnum;
1443                 notify.nttrans.in.recursive = dirs[i].recursive;
1444                 req = smb_raw_changenotify_send(cli->tree, &notify);
1445                 smb_raw_ntcancel(req);
1446                 status = smb_raw_changenotify_recv(req, mem_ctx, &notify);
1447                 torture_assert_ntstatus_equal_goto(mem_ctx, status,
1448                                                    NT_STATUS_CANCELLED,
1449                                                    ret, done,
1450                                                    "smb_raw_changenotify_recv");
1451         }
1452
1453         /* trigger 2 events in each dir */
1454         for (i=0;i<ARRAY_SIZE(dirs);i++) {
1455                 char *path = talloc_asprintf(mem_ctx, "%s\\test.dir", dirs[i].path);
1456                 /*
1457                  * Make notifies a bit more interesting in a cluster
1458                  * by doing the changes against different nodes with
1459                  * --unclist
1460                  */
1461                 smbcli_mkdir(cli->tree, path);
1462                 smbcli_rmdir(cli2->tree, path);
1463                 talloc_free(path);
1464         }
1465
1466         /* give a bit of time for the events to propogate */
1467         tv = timeval_current();
1468
1469         do {
1470                 /* count events that have happened in each dir */
1471                 for (i=0;i<ARRAY_SIZE(dirs);i++) {
1472                         notify.nttrans.in.file.fnum = dirs[i].fnum;
1473                         req = smb_raw_changenotify_send(cli->tree, &notify);
1474                         smb_raw_ntcancel(req);
1475                         notify.nttrans.out.num_changes = 0;
1476                         status = smb_raw_changenotify_recv(req, mem_ctx, &notify);
1477                         dirs[i].counted += notify.nttrans.out.num_changes;
1478                 }
1479                 
1480                 all_done = true;
1481
1482                 for (i=0;i<ARRAY_SIZE(dirs);i++) {
1483                         if (dirs[i].counted != dirs[i].expected) {
1484                                 all_done = false;
1485                         }
1486                 }
1487         } while (!all_done && timeval_elapsed(&tv) < 20);
1488
1489         printf("took %.4f seconds to propogate all events\n", timeval_elapsed(&tv));
1490
1491         for (i=0;i<ARRAY_SIZE(dirs);i++) {
1492                 if (dirs[i].counted != dirs[i].expected) {
1493                         printf("ERROR: i=%d expected %d got %d for '%s'\n",
1494                                i, dirs[i].expected, dirs[i].counted, dirs[i].path);
1495                         ret = false;
1496                 }
1497         }
1498
1499         /*
1500           run from the back, closing and deleting
1501         */
1502         for (i=ARRAY_SIZE(dirs)-1;i>=0;i--) {
1503                 smbcli_close(cli->tree, dirs[i].fnum);
1504                 smbcli_rmdir(cli->tree, dirs[i].path);
1505         }
1506
1507 done:
1508         smb_raw_exit(cli->session);
1509         smbcli_deltree(cli->tree, BASEDIR);
1510         return ret;
1511 }
1512
1513 /*
1514    Test response when cached server events exceed single NT NOTFIY response
1515    packet size.
1516 */
1517 static bool test_notify_overflow(struct torture_context *mem_ctx,
1518                                  struct smbcli_state *cli)
1519 {
1520         bool ret = true;
1521         NTSTATUS status;
1522         union smb_notify notify;
1523         union smb_open io;
1524         int fnum;
1525         int count = 100;
1526         struct smbcli_request *req1;
1527         int i;
1528
1529         printf("TESTING CHANGE NOTIFY EVENT OVERFLOW\n");
1530
1531         torture_assert(mem_ctx, torture_setup_dir(cli, BASEDIR),
1532                        "Failed to setup up test directory: " BASEDIR);
1533
1534         /* get a handle on the directory */
1535         io.generic.level = RAW_OPEN_NTCREATEX;
1536         io.ntcreatex.in.root_fid.fnum = 0;
1537         io.ntcreatex.in.flags = 0;
1538         io.ntcreatex.in.access_mask = SEC_FILE_ALL;
1539         io.ntcreatex.in.create_options = NTCREATEX_OPTIONS_DIRECTORY;
1540         io.ntcreatex.in.file_attr = FILE_ATTRIBUTE_NORMAL;
1541         io.ntcreatex.in.share_access = NTCREATEX_SHARE_ACCESS_READ |
1542             NTCREATEX_SHARE_ACCESS_WRITE;
1543         io.ntcreatex.in.alloc_size = 0;
1544         io.ntcreatex.in.open_disposition = NTCREATEX_DISP_OPEN;
1545         io.ntcreatex.in.impersonation = NTCREATEX_IMPERSONATION_ANONYMOUS;
1546         io.ntcreatex.in.security_flags = 0;
1547         io.ntcreatex.in.fname = BASEDIR;
1548
1549         status = smb_raw_open(cli->tree, mem_ctx, &io);
1550         torture_assert_ntstatus_ok_goto(mem_ctx, status, ret, done,
1551                                         "smb_raw_open");
1552         fnum = io.ntcreatex.out.file.fnum;
1553
1554         /* ask for a change notify, on name changes. */
1555         notify.nttrans.level = RAW_NOTIFY_NTTRANS;
1556         notify.nttrans.in.buffer_size = 1000;
1557         notify.nttrans.in.completion_filter = FILE_NOTIFY_CHANGE_NAME;
1558         notify.nttrans.in.file.fnum = fnum;
1559
1560         notify.nttrans.in.recursive = true;
1561         req1 = smb_raw_changenotify_send(cli->tree, &notify);
1562
1563         /* cancel initial requests so the buffer is setup */
1564         smb_raw_ntcancel(req1);
1565         status = smb_raw_changenotify_recv(req1, mem_ctx, &notify);
1566         torture_assert_ntstatus_equal_goto(mem_ctx, status,
1567                                            NT_STATUS_CANCELLED,
1568                                            ret, done,
1569                                            "smb_raw_changenotify_recv");
1570
1571         /* open a lot of files, filling up the server side notify buffer */
1572         printf("Testing overflowed buffer notify on create of %d files\n",
1573                count);
1574         for (i=0;i<count;i++) {
1575                 char *fname = talloc_asprintf(cli, BASEDIR "\\test%d.txt", i);
1576                 int fnum2 = smbcli_open(cli->tree, fname, O_CREAT|O_RDWR,
1577                                         DENY_NONE);
1578                 if (fnum2 == -1) {
1579                         printf("Failed to create %s - %s\n",
1580                                fname, smbcli_errstr(cli->tree));
1581                         ret = false;
1582                         goto done;
1583                 }
1584                 talloc_free(fname);
1585                 smbcli_close(cli->tree, fnum2);
1586         }
1587
1588         /* expect that 0 events will be returned with NT_STATUS_OK */
1589         req1 = smb_raw_changenotify_send(cli->tree, &notify);
1590         status = smb_raw_changenotify_recv(req1, mem_ctx, &notify);
1591         torture_assert_ntstatus_ok_goto(mem_ctx, status, ret, done,
1592                                         "smb_raw_changenotify_recv");
1593         torture_assert_int_equal_goto(mem_ctx, notify.nttrans.out.num_changes,
1594                                       0, ret, done, "no changes expected");
1595
1596 done:
1597         smb_raw_exit(cli->session);
1598         smbcli_deltree(cli->tree, BASEDIR);
1599         return ret;
1600 }
1601
1602 /*
1603    Test if notifications are returned for changes to the base directory.
1604    They shouldn't be.
1605 */
1606 static bool test_notify_basedir(struct torture_context *mem_ctx,
1607                                 struct smbcli_state *cli)
1608 {
1609         bool ret = true;
1610         NTSTATUS status;
1611         union smb_notify notify;
1612         union smb_open io;
1613         int fnum;
1614         struct smbcli_request *req1;
1615
1616         printf("TESTING CHANGE NOTIFY BASEDIR EVENTS\n");
1617
1618         torture_assert(mem_ctx, torture_setup_dir(cli, BASEDIR),
1619                        "Failed to setup up test directory: " BASEDIR);
1620
1621         /* get a handle on the directory */
1622         io.generic.level = RAW_OPEN_NTCREATEX;
1623         io.ntcreatex.in.root_fid.fnum = 0;
1624         io.ntcreatex.in.flags = 0;
1625         io.ntcreatex.in.access_mask = SEC_FILE_ALL;
1626         io.ntcreatex.in.create_options = NTCREATEX_OPTIONS_DIRECTORY;
1627         io.ntcreatex.in.file_attr = FILE_ATTRIBUTE_NORMAL;
1628         io.ntcreatex.in.share_access = NTCREATEX_SHARE_ACCESS_READ |
1629             NTCREATEX_SHARE_ACCESS_WRITE;
1630         io.ntcreatex.in.alloc_size = 0;
1631         io.ntcreatex.in.open_disposition = NTCREATEX_DISP_OPEN;
1632         io.ntcreatex.in.impersonation = NTCREATEX_IMPERSONATION_ANONYMOUS;
1633         io.ntcreatex.in.security_flags = 0;
1634         io.ntcreatex.in.fname = BASEDIR;
1635
1636         status = smb_raw_open(cli->tree, mem_ctx, &io);
1637         torture_assert_ntstatus_ok_goto(mem_ctx, status, ret, done,
1638                                         "smb_raw_open");
1639         fnum = io.ntcreatex.out.file.fnum;
1640
1641         /* create a test file that will also be modified */
1642         smbcli_close(cli->tree, smbcli_open(cli->tree, BASEDIR "\\tname1",
1643                                             O_CREAT, 0));
1644
1645         /* ask for a change notify, on attribute changes. */
1646         notify.nttrans.level = RAW_NOTIFY_NTTRANS;
1647         notify.nttrans.in.buffer_size = 1000;
1648         notify.nttrans.in.completion_filter = FILE_NOTIFY_CHANGE_ATTRIBUTES;
1649         notify.nttrans.in.file.fnum = fnum;
1650         notify.nttrans.in.recursive = true;
1651
1652         req1 = smb_raw_changenotify_send(cli->tree, &notify);
1653
1654         /* set attribute on the base dir */
1655         smbcli_setatr(cli->tree, BASEDIR, FILE_ATTRIBUTE_HIDDEN, 0);
1656
1657         /* set attribute on a file to assure we receive a notification */
1658         smbcli_setatr(cli->tree, BASEDIR "\\tname1", FILE_ATTRIBUTE_HIDDEN, 0);
1659         smb_msleep(200);
1660
1661         /* check how many responses were given, expect only 1 for the file */
1662         status = smb_raw_changenotify_recv(req1, mem_ctx, &notify);
1663         torture_assert_ntstatus_ok_goto(mem_ctx, status, ret, done,
1664                                         "smb_raw_changenotify_recv");
1665         torture_assert_int_equal_goto(mem_ctx, notify.nttrans.out.num_changes,
1666                                       1, ret, done, "wrong number of  changes");
1667         torture_assert_int_equal_goto(mem_ctx,
1668                                       notify.nttrans.out.changes[0].action,
1669                                       NOTIFY_ACTION_MODIFIED, ret, done,
1670                                       "wrong action (exp: MODIFIED)");
1671         CHECK_WSTR(mem_ctx, notify.nttrans.out.changes[0].name, "tname1",
1672                    STR_UNICODE);
1673
1674 done:
1675         smb_raw_exit(cli->session);
1676         smbcli_deltree(cli->tree, BASEDIR);
1677         return ret;
1678 }
1679
1680
1681 /*
1682   create a secondary tree connect - used to test for a bug in Samba3 messaging
1683   with change notify
1684 */
1685 static struct smbcli_tree *secondary_tcon(struct smbcli_state *cli, 
1686                                           struct torture_context *tctx)
1687 {
1688         NTSTATUS status;
1689         const char *share, *host;
1690         struct smbcli_tree *tree;
1691         union smb_tcon tcon;
1692
1693         share = torture_setting_string(tctx, "share", NULL);
1694         host  = torture_setting_string(tctx, "host", NULL);
1695         
1696         printf("create a second tree context on the same session\n");
1697         tree = smbcli_tree_init(cli->session, tctx, false);
1698
1699         tcon.generic.level = RAW_TCON_TCONX;
1700         tcon.tconx.in.flags = TCONX_FLAG_EXTENDED_RESPONSE;
1701         tcon.tconx.in.password = data_blob(NULL, 0);
1702         tcon.tconx.in.path = talloc_asprintf(tctx, "\\\\%s\\%s", host, share);
1703         tcon.tconx.in.device = "A:";    
1704         status = smb_raw_tcon(tree, tctx, &tcon);
1705         if (!NT_STATUS_IS_OK(status)) {
1706                 talloc_free(tree);
1707                 printf("Failed to create secondary tree\n");
1708                 return NULL;
1709         }
1710
1711         tree->tid = tcon.tconx.out.tid;
1712         printf("tid1=%d tid2=%d\n", cli->tree->tid, tree->tid);
1713
1714         return tree;
1715 }
1716
1717
1718 /* 
1719    very simple change notify test
1720 */
1721 static bool test_notify_tcon(struct torture_context *torture,
1722                              struct smbcli_state *cli)
1723 {
1724         bool ret = true;
1725         NTSTATUS status;
1726         union smb_notify notify;
1727         union smb_open io;
1728         int fnum;
1729         struct smbcli_request *req;
1730         extern int torture_numops;
1731         struct smbcli_tree *tree = NULL;
1732                 
1733         printf("TESTING SIMPLE CHANGE NOTIFY\n");
1734
1735         torture_assert(torture, torture_setup_dir(cli, BASEDIR),
1736                        "Failed to setup up test directory: " BASEDIR);
1737
1738         /*
1739           get a handle on the directory
1740         */
1741         io.generic.level = RAW_OPEN_NTCREATEX;
1742         io.ntcreatex.in.root_fid.fnum = 0;
1743         io.ntcreatex.in.flags = 0;
1744         io.ntcreatex.in.access_mask = SEC_FILE_ALL;
1745         io.ntcreatex.in.create_options = NTCREATEX_OPTIONS_DIRECTORY;
1746         io.ntcreatex.in.file_attr = FILE_ATTRIBUTE_NORMAL;
1747         io.ntcreatex.in.share_access = NTCREATEX_SHARE_ACCESS_READ | NTCREATEX_SHARE_ACCESS_WRITE;
1748         io.ntcreatex.in.alloc_size = 0;
1749         io.ntcreatex.in.open_disposition = NTCREATEX_DISP_OPEN;
1750         io.ntcreatex.in.impersonation = NTCREATEX_IMPERSONATION_ANONYMOUS;
1751         io.ntcreatex.in.security_flags = 0;
1752         io.ntcreatex.in.fname = BASEDIR;
1753
1754         status = smb_raw_open(cli->tree, torture, &io);
1755         torture_assert_ntstatus_ok_goto(torture, status, ret, done,
1756                                         "smb_raw_open");
1757         fnum = io.ntcreatex.out.file.fnum;
1758
1759         status = smb_raw_open(cli->tree, torture, &io);
1760         torture_assert_ntstatus_ok_goto(torture, status, ret, done,
1761                                         "smb_raw_open");
1762
1763         /* ask for a change notify,
1764            on file or directory name changes */
1765         notify.nttrans.level = RAW_NOTIFY_NTTRANS;
1766         notify.nttrans.in.buffer_size = 1000;
1767         notify.nttrans.in.completion_filter = FILE_NOTIFY_CHANGE_NAME;
1768         notify.nttrans.in.file.fnum = fnum;
1769         notify.nttrans.in.recursive = true;
1770
1771         printf("Testing notify mkdir\n");
1772         req = smb_raw_changenotify_send(cli->tree, &notify);
1773         smbcli_mkdir(cli->tree, BASEDIR "\\subdir-name");
1774
1775         status = smb_raw_changenotify_recv(req, torture, &notify);
1776         torture_assert_ntstatus_ok_goto(torture, status, ret, done,
1777                                         "smb_raw_changenotify_recv");
1778
1779         torture_assert_int_equal_goto(torture, notify.nttrans.out.num_changes,
1780                                       1, ret, done, "wrong number of changes");
1781         torture_assert_int_equal_goto(torture,
1782                                       notify.nttrans.out.changes[0].action,
1783                                       NOTIFY_ACTION_ADDED, ret, done,
1784                                       "wrong action (exp: ADDED)");
1785         CHECK_WSTR(torture, notify.nttrans.out.changes[0].name, "subdir-name",
1786                    STR_UNICODE);
1787
1788         printf("Testing notify rmdir\n");
1789         req = smb_raw_changenotify_send(cli->tree, &notify);
1790         smbcli_rmdir(cli->tree, BASEDIR "\\subdir-name");
1791
1792         status = smb_raw_changenotify_recv(req, torture, &notify);
1793         torture_assert_ntstatus_ok_goto(torture, status, ret, done,
1794                                         "smb_raw_changenotify_recv");
1795         torture_assert_int_equal_goto(torture, notify.nttrans.out.num_changes,
1796                                       1, ret, done, "wrong number of changes");
1797         torture_assert_int_equal_goto(torture,
1798                                       notify.nttrans.out.changes[0].action,
1799                                       NOTIFY_ACTION_REMOVED, ret, done,
1800                                       "wrong action (exp: REMOVED)");
1801         CHECK_WSTR(torture, notify.nttrans.out.changes[0].name, "subdir-name",
1802                    STR_UNICODE);
1803
1804         printf("SIMPLE CHANGE NOTIFY OK\n");
1805
1806         printf("TESTING WITH SECONDARY TCON\n");
1807         tree = secondary_tcon(cli, torture);
1808
1809         printf("Testing notify mkdir\n");
1810         req = smb_raw_changenotify_send(cli->tree, &notify);
1811         smbcli_mkdir(cli->tree, BASEDIR "\\subdir-name");
1812
1813         status = smb_raw_changenotify_recv(req, torture, &notify);
1814         torture_assert_ntstatus_ok_goto(torture, status, ret, done,
1815                                         "smb_raw_changenotify_recv");
1816
1817         torture_assert_int_equal_goto(torture, notify.nttrans.out.num_changes,
1818                                       1, ret, done, "wrong number of changes");
1819         torture_assert_int_equal_goto(torture,
1820                                       notify.nttrans.out.changes[0].action,
1821                                       NOTIFY_ACTION_ADDED, ret, done,
1822                                       "wrong action (exp: ADDED)");
1823         CHECK_WSTR(torture, notify.nttrans.out.changes[0].name, "subdir-name",
1824                    STR_UNICODE);
1825
1826         printf("Testing notify rmdir\n");
1827         req = smb_raw_changenotify_send(cli->tree, &notify);
1828         smbcli_rmdir(cli->tree, BASEDIR "\\subdir-name");
1829
1830         status = smb_raw_changenotify_recv(req, torture, &notify);
1831         torture_assert_ntstatus_ok_goto(torture, status, ret, done,
1832                                         "smb_raw_changenotify_recv");
1833         torture_assert_int_equal_goto(torture, notify.nttrans.out.num_changes,
1834                                       1, ret, done, "wrong number of changes");
1835         torture_assert_int_equal_goto(torture,
1836                                       notify.nttrans.out.changes[0].action,
1837                                       NOTIFY_ACTION_REMOVED, ret, done,
1838                                       "wrong action (exp: REMOVED)");
1839         CHECK_WSTR(torture, notify.nttrans.out.changes[0].name, "subdir-name",
1840                    STR_UNICODE);
1841
1842         printf("CHANGE NOTIFY WITH TCON OK\n");
1843
1844         printf("Disconnecting secondary tree\n");
1845         status = smb_tree_disconnect(tree);
1846         torture_assert_ntstatus_ok_goto(torture, status, ret, done,
1847                                         "smb_tree_disconnect");
1848         talloc_free(tree);
1849
1850         printf("Testing notify mkdir\n");
1851         req = smb_raw_changenotify_send(cli->tree, &notify);
1852         smbcli_mkdir(cli->tree, BASEDIR "\\subdir-name");
1853
1854         status = smb_raw_changenotify_recv(req, torture, &notify);
1855         torture_assert_ntstatus_ok_goto(torture, status, ret, done,
1856                                         "smb_raw_changenotify_recv");
1857
1858         torture_assert_int_equal_goto(torture, notify.nttrans.out.num_changes,
1859                                       1, ret, done, "wrong number of changes");
1860         torture_assert_int_equal_goto(torture,
1861                                       notify.nttrans.out.changes[0].action,
1862                                       NOTIFY_ACTION_ADDED, ret, done,
1863                                       "wrong action (exp: ADDED)");
1864         CHECK_WSTR(torture, notify.nttrans.out.changes[0].name, "subdir-name",
1865                    STR_UNICODE);
1866
1867         printf("Testing notify rmdir\n");
1868         req = smb_raw_changenotify_send(cli->tree, &notify);
1869         smbcli_rmdir(cli->tree, BASEDIR "\\subdir-name");
1870
1871         status = smb_raw_changenotify_recv(req, torture, &notify);
1872         torture_assert_ntstatus_ok_goto(torture, status, ret, done,
1873                                         "smb_raw_changenotify_recv");
1874         torture_assert_int_equal_goto(torture, notify.nttrans.out.num_changes,
1875                                       1, ret, done, "wrong number of changes");
1876         torture_assert_int_equal_goto(torture,
1877                                       notify.nttrans.out.changes[0].action,
1878                                       NOTIFY_ACTION_REMOVED, ret, done,
1879                                       "wrong action (exp: REMOVED)");
1880         CHECK_WSTR(torture, notify.nttrans.out.changes[0].name, "subdir-name",
1881                    STR_UNICODE);
1882
1883         printf("CHANGE NOTIFY WITH TDIS OK\n");
1884 done:
1885         smb_raw_exit(cli->session);
1886         smbcli_deltree(cli->tree, BASEDIR);
1887         return ret;
1888 }
1889
1890
1891 /*
1892    testing alignment of multiple change notify infos
1893 */
1894 static bool test_notify_alignment(struct torture_context *tctx,
1895                                   struct smbcli_state *cli)
1896 {
1897         NTSTATUS status;
1898         union smb_notify notify;
1899         union smb_open io;
1900         int i, fnum, fnum2;
1901         struct smbcli_request *req;
1902         const char *fname = BASEDIR "\\starter";
1903         const char *fnames[] = { "a",
1904                                  "ab",
1905                                  "abc",
1906                                  "abcd" };
1907         int num_names = ARRAY_SIZE(fnames);
1908         char *fpath = NULL;
1909
1910         torture_comment(tctx, "TESTING CHANGE NOTIFY REPLY ALIGNMENT\n");
1911
1912         torture_assert(tctx, torture_setup_dir(cli, BASEDIR),
1913                        "Failed to setup up test directory: " BASEDIR);
1914
1915         /* get a handle on the directory */
1916         io.generic.level = RAW_OPEN_NTCREATEX;
1917         io.ntcreatex.in.root_fid.fnum = 0;
1918         io.ntcreatex.in.flags = 0;
1919         io.ntcreatex.in.access_mask = SEC_FILE_ALL;
1920         io.ntcreatex.in.create_options = NTCREATEX_OPTIONS_DIRECTORY;
1921         io.ntcreatex.in.file_attr = FILE_ATTRIBUTE_NORMAL;
1922         io.ntcreatex.in.share_access = NTCREATEX_SHARE_ACCESS_READ |
1923                                        NTCREATEX_SHARE_ACCESS_WRITE;
1924         io.ntcreatex.in.alloc_size = 0;
1925         io.ntcreatex.in.open_disposition = NTCREATEX_DISP_OPEN;
1926         io.ntcreatex.in.impersonation = NTCREATEX_IMPERSONATION_ANONYMOUS;
1927         io.ntcreatex.in.security_flags = 0;
1928         io.ntcreatex.in.fname = BASEDIR;
1929
1930         status = smb_raw_open(cli->tree, tctx, &io);
1931         torture_assert_ntstatus_ok(tctx, status, "smb_raw_open");
1932         fnum = io.ntcreatex.out.file.fnum;
1933
1934         /* ask for a change notify, on file creation */
1935         notify.nttrans.level = RAW_NOTIFY_NTTRANS;
1936         notify.nttrans.in.buffer_size = 1000;
1937         notify.nttrans.in.completion_filter = FILE_NOTIFY_CHANGE_FILE_NAME;
1938         notify.nttrans.in.file.fnum = fnum;
1939         notify.nttrans.in.recursive = false;
1940
1941         /* start change tracking */
1942         req = smb_raw_changenotify_send(cli->tree, &notify);
1943
1944         fnum2 = smbcli_open(cli->tree, fname, O_CREAT|O_RDWR, DENY_NONE);
1945         torture_assert(tctx, fnum2 != -1, smbcli_errstr(cli->tree));
1946         smbcli_close(cli->tree, fnum2);
1947
1948         status = smb_raw_changenotify_recv(req, tctx, &notify);
1949         torture_assert_ntstatus_ok(tctx, status, "smb_raw_changenotify_recv");
1950
1951         /* create 4 files that will cause CHANGE_NOTIFY_INFO structures
1952          * to be returned in the same packet with all possible 4-byte padding
1953          * permutations.  As per MS-CIFS 2.2.7.4.2 these structures should be
1954          * 4-byte aligned. */
1955
1956         for (i = 0; i < num_names; i++) {
1957                 fpath = talloc_asprintf(tctx, "%s\\%s", BASEDIR, fnames[i]);
1958                 fnum2 = smbcli_open(cli->tree, fpath,
1959                     O_CREAT|O_RDWR, DENY_NONE);
1960                 torture_assert(tctx, fnum2 != -1, smbcli_errstr(cli->tree));
1961                 smbcli_close(cli->tree, fnum2);
1962                 talloc_free(fpath);
1963         }
1964
1965         /* We send a notify packet, and let smb_raw_changenotify_recv() do
1966          * the alignment checking for us. */
1967         req = smb_raw_changenotify_send(cli->tree, &notify);
1968         status = smb_raw_changenotify_recv(req, tctx, &notify);
1969         torture_assert_ntstatus_ok(tctx, status, "smb_raw_changenotify_recv");
1970
1971         /* Do basic checking for correctness. */
1972         torture_assert(tctx, notify.nttrans.out.num_changes == num_names, "");
1973         for (i = 0; i < num_names; i++) {
1974                 torture_assert(tctx, notify.nttrans.out.changes[i].action ==
1975                     NOTIFY_ACTION_ADDED, "");
1976                 CHECK_WSTR(tctx, notify.nttrans.out.changes[i].name, fnames[i],
1977                     STR_UNICODE);
1978         }
1979
1980         smb_raw_exit(cli->session);
1981         smbcli_deltree(cli->tree, BASEDIR);
1982         return true;
1983 }
1984
1985 struct torture_suite *torture_raw_notify(TALLOC_CTX *mem_ctx)
1986 {
1987         struct torture_suite *suite = torture_suite_create(mem_ctx, "notify");
1988
1989         torture_suite_add_1smb_test(suite, "tcon", test_notify_tcon);
1990         torture_suite_add_2smb_test(suite, "dir", test_notify_dir);
1991         torture_suite_add_2smb_test(suite, "mask", test_notify_mask);
1992         torture_suite_add_2smb_test(suite, "recursive", test_notify_recursive);
1993         torture_suite_add_1smb_test(suite, "mask_change",
1994                                     test_notify_mask_change);
1995         torture_suite_add_1smb_test(suite, "file", test_notify_file);
1996         torture_suite_add_1smb_test(suite, "tdis", test_notify_tdis);
1997         torture_suite_add_1smb_test(suite, "exit", test_notify_exit);
1998         torture_suite_add_1smb_test(suite, "ulogoff", test_notify_ulogoff);
1999         torture_suite_add_1smb_test(suite, "tcp_dis", test_notify_tcp_dis);
2000         torture_suite_add_1smb_test(suite, "double", test_notify_double);
2001         torture_suite_add_2smb_test(suite, "tree", test_notify_tree);
2002         torture_suite_add_1smb_test(suite, "overflow", test_notify_overflow);
2003         torture_suite_add_1smb_test(suite, "basedir", test_notify_basedir);
2004         torture_suite_add_1smb_test(suite, "alignment", test_notify_alignment);
2005
2006         return suite;
2007 }