s4:torture:raw:notify: remove superfluous conditional goto
[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 done:
679         smb_raw_exit(cli->session);
680         smbcli_deltree(cli->tree, BASEDIR);
681         return ret;
682 }
683
684
685 /* 
686    testing of mask bits for change notify
687 */
688 static bool test_notify_mask(struct torture_context *tctx,
689                              struct smbcli_state *cli,
690                              struct smbcli_state *cli2)
691 {
692         bool ret = true;
693         NTSTATUS status;
694         union smb_notify notify;
695         union smb_open io;
696         union smb_chkpath chkpath;
697         int fnum, fnum2;
698         uint32_t mask;
699         int i;
700         char c = 1;
701         struct timeval tv;
702         NTTIME t;
703
704         printf("TESTING CHANGE NOTIFY COMPLETION FILTERS\n");
705
706         torture_assert(tctx, torture_setup_dir(cli, BASEDIR),
707                        "Failed to setup up test directory: " BASEDIR);
708
709         tv = timeval_current_ofs(1000, 0);
710         t = timeval_to_nttime(&tv);
711
712         /*
713           get a handle on the directory
714         */
715         io.generic.level = RAW_OPEN_NTCREATEX;
716         io.ntcreatex.in.root_fid.fnum = 0;
717         io.ntcreatex.in.flags = 0;
718         io.ntcreatex.in.access_mask = SEC_FILE_ALL;
719         io.ntcreatex.in.create_options = NTCREATEX_OPTIONS_DIRECTORY;
720         io.ntcreatex.in.file_attr = FILE_ATTRIBUTE_NORMAL;
721         io.ntcreatex.in.share_access = NTCREATEX_SHARE_ACCESS_READ | NTCREATEX_SHARE_ACCESS_WRITE;
722         io.ntcreatex.in.alloc_size = 0;
723         io.ntcreatex.in.open_disposition = NTCREATEX_DISP_OPEN;
724         io.ntcreatex.in.impersonation = NTCREATEX_IMPERSONATION_ANONYMOUS;
725         io.ntcreatex.in.security_flags = 0;
726         io.ntcreatex.in.fname = BASEDIR;
727
728         notify.nttrans.level = RAW_NOTIFY_NTTRANS;
729         notify.nttrans.in.buffer_size = 1000;
730         notify.nttrans.in.recursive = true;
731
732         chkpath.chkpath.in.path = "\\";
733
734 #define NOTIFY_MASK_TEST(test_name, setup, op, cleanup, Action, expected, nchanges) \
735         do { \
736         smbcli_getatr(cli->tree, test_name, NULL, NULL, NULL); \
737         do { for (mask=i=0;i<32;i++) { \
738                 struct smbcli_request *req; \
739                 status = smb_raw_open(cli->tree, tctx, &io); \
740                 torture_assert_ntstatus_ok_goto(tctx, status, ret, done, \
741                                                 "smb_raw_open"); \
742                 fnum = io.ntcreatex.out.file.fnum; \
743                 setup \
744                 notify.nttrans.in.file.fnum = fnum;     \
745                 notify.nttrans.in.completion_filter = (1<<i); \
746                 req = smb_raw_changenotify_send(cli->tree, &notify); \
747                 smb_raw_chkpath(cli->tree, &chkpath); \
748                 op \
749                 smb_msleep(200); smb_raw_ntcancel(req); \
750                 status = smb_raw_changenotify_recv(req, tctx, &notify); \
751                 cleanup \
752                 smbcli_close(cli->tree, fnum); \
753                 if (NT_STATUS_EQUAL(status, NT_STATUS_CANCELLED)) continue; \
754                 torture_assert_ntstatus_ok_goto(tctx, status, ret, done, \
755                                                 "smbcli_close"); \
756                 /* special case to cope with file rename behaviour */ \
757                 if (nchanges == 2 && notify.nttrans.out.num_changes == 1 && \
758                     notify.nttrans.out.changes[0].action == NOTIFY_ACTION_MODIFIED && \
759                     ((expected) & FILE_NOTIFY_CHANGE_ATTRIBUTES) && \
760                     Action == NOTIFY_ACTION_OLD_NAME) { \
761                         printf("(rename file special handling OK)\n"); \
762                 } else if (nchanges != notify.nttrans.out.num_changes) { \
763                         printf("ERROR: nchanges=%d expected=%d action=%d filter=0x%08x\n", \
764                                notify.nttrans.out.num_changes, \
765                                nchanges, \
766                                notify.nttrans.out.changes[0].action, \
767                                notify.nttrans.in.completion_filter); \
768                         ret = false; \
769                 } else if (notify.nttrans.out.changes[0].action != Action) { \
770                         printf("ERROR: nchanges=%d action=%d expectedAction=%d filter=0x%08x\n", \
771                                notify.nttrans.out.num_changes, \
772                                notify.nttrans.out.changes[0].action, \
773                                Action, \
774                                notify.nttrans.in.completion_filter); \
775                         ret = false; \
776                 } else if (strcmp(notify.nttrans.out.changes[0].name.s, "tname1") != 0) { \
777                         printf("ERROR: nchanges=%d action=%d filter=0x%08x name=%s\n", \
778                                notify.nttrans.out.num_changes, \
779                                notify.nttrans.out.changes[0].action, \
780                                notify.nttrans.in.completion_filter, \
781                                notify.nttrans.out.changes[0].name.s);   \
782                         ret = false; \
783                 } \
784                 mask |= (1<<i); \
785         } \
786         if ((expected) != mask) { \
787                 if (((expected) & ~mask) != 0) { \
788                         printf("ERROR: trigger on too few bits. mask=0x%08x expected=0x%08x\n", \
789                                mask, expected); \
790                         ret = false; \
791                 } else { \
792                         printf("WARNING: trigger on too many bits. mask=0x%08x expected=0x%08x\n", \
793                                mask, expected); \
794                 } \
795         } \
796         } while (0); \
797         } while (0);
798
799         printf("Testing mkdir\n");
800         NOTIFY_MASK_TEST("Testing mkdir",;,
801                          smbcli_mkdir(cli->tree, BASEDIR "\\tname1");,
802                          smbcli_rmdir(cli2->tree, BASEDIR "\\tname1");,
803                          NOTIFY_ACTION_ADDED,
804                          FILE_NOTIFY_CHANGE_DIR_NAME, 1);
805
806         printf("Testing create file\n");
807         NOTIFY_MASK_TEST("Testing create file",;,
808                          smbcli_close(cli->tree, smbcli_open(cli->tree, BASEDIR "\\tname1", O_CREAT, 0));,
809                          smbcli_unlink(cli2->tree, BASEDIR "\\tname1");,
810                          NOTIFY_ACTION_ADDED,
811                          FILE_NOTIFY_CHANGE_FILE_NAME, 1);
812
813         printf("Testing unlink\n");
814         NOTIFY_MASK_TEST("Testing unlink",
815                          smbcli_close(cli->tree, smbcli_open(cli->tree, BASEDIR "\\tname1", O_CREAT, 0));,
816                          smbcli_unlink(cli2->tree, BASEDIR "\\tname1");,
817                          ;,
818                          NOTIFY_ACTION_REMOVED,
819                          FILE_NOTIFY_CHANGE_FILE_NAME, 1);
820
821         printf("Testing rmdir\n");
822         NOTIFY_MASK_TEST("Testing rmdir",
823                          smbcli_mkdir(cli->tree, BASEDIR "\\tname1");,
824                          smbcli_rmdir(cli2->tree, BASEDIR "\\tname1");,
825                          ;,
826                          NOTIFY_ACTION_REMOVED,
827                          FILE_NOTIFY_CHANGE_DIR_NAME, 1);
828
829         printf("Testing rename file\n");
830         NOTIFY_MASK_TEST("Testing rename file",
831                          smbcli_close(cli->tree, smbcli_open(cli->tree, BASEDIR "\\tname1", O_CREAT, 0));,
832                          smbcli_rename(cli2->tree, BASEDIR "\\tname1", BASEDIR "\\tname2");,
833                          smbcli_unlink(cli->tree, BASEDIR "\\tname2");,
834                          NOTIFY_ACTION_OLD_NAME,
835                          FILE_NOTIFY_CHANGE_FILE_NAME|FILE_NOTIFY_CHANGE_ATTRIBUTES|FILE_NOTIFY_CHANGE_CREATION, 2);
836
837         printf("Testing rename dir\n");
838         NOTIFY_MASK_TEST("Testing rename dir",
839                 smbcli_mkdir(cli->tree, BASEDIR "\\tname1");,
840                 smbcli_rename(cli2->tree, BASEDIR "\\tname1", BASEDIR "\\tname2");,
841                 smbcli_rmdir(cli->tree, BASEDIR "\\tname2");,
842                 NOTIFY_ACTION_OLD_NAME,
843                 FILE_NOTIFY_CHANGE_DIR_NAME, 2);
844
845         printf("Testing set path attribute\n");
846         NOTIFY_MASK_TEST("Testing set path attribute",
847                 smbcli_close(cli->tree, smbcli_open(cli->tree, BASEDIR "\\tname1", O_CREAT, 0));,
848                 smbcli_setatr(cli2->tree, BASEDIR "\\tname1", FILE_ATTRIBUTE_HIDDEN, 0);,
849                 smbcli_unlink(cli->tree, BASEDIR "\\tname1");,
850                 NOTIFY_ACTION_MODIFIED,
851                 FILE_NOTIFY_CHANGE_ATTRIBUTES, 1);
852
853         printf("Testing set path write time\n");
854         NOTIFY_MASK_TEST("Testing set path write time",
855                 smbcli_close(cli->tree, smbcli_open(cli->tree, BASEDIR "\\tname1", O_CREAT, 0));,
856                 smbcli_setatr(cli2->tree, BASEDIR "\\tname1", FILE_ATTRIBUTE_NORMAL, 1000);,
857                 smbcli_unlink(cli->tree, BASEDIR "\\tname1");,
858                 NOTIFY_ACTION_MODIFIED,
859                 FILE_NOTIFY_CHANGE_LAST_WRITE, 1);
860
861         printf("Testing set file attribute\n");
862         NOTIFY_MASK_TEST("Testing set file attribute",
863                 fnum2 = create_complex_file(cli2, tctx, BASEDIR "\\tname1");,
864                 smbcli_fsetatr(cli2->tree, fnum2, FILE_ATTRIBUTE_HIDDEN, 0, 0, 0, 0);,
865                 (smbcli_close(cli2->tree, fnum2), smbcli_unlink(cli2->tree, BASEDIR "\\tname1"));,
866                 NOTIFY_ACTION_MODIFIED,
867                 FILE_NOTIFY_CHANGE_ATTRIBUTES, 1);
868
869         if (torture_setting_bool(tctx, "samba3", false)) {
870                 printf("Samba3 does not yet support create times "
871                        "everywhere\n");
872         }
873         else {
874                 printf("Testing set file create time\n");
875                 NOTIFY_MASK_TEST("Testing set file create time",
876                         fnum2 = create_complex_file(cli, tctx,
877                                                     BASEDIR "\\tname1");,
878                         smbcli_fsetatr(cli->tree, fnum2, 0, t, 0, 0, 0);,
879                         (smbcli_close(cli->tree, fnum2),
880                          smbcli_unlink(cli->tree, BASEDIR "\\tname1"));,
881                         NOTIFY_ACTION_MODIFIED,
882                         FILE_NOTIFY_CHANGE_CREATION, 1);
883         }
884
885         printf("Testing set file access time\n");
886         NOTIFY_MASK_TEST("Testing set file access time",
887                 fnum2 = create_complex_file(cli, tctx, BASEDIR "\\tname1");,
888                 smbcli_fsetatr(cli->tree, fnum2, 0, 0, t, 0, 0);,
889                 (smbcli_close(cli->tree, fnum2), smbcli_unlink(cli->tree, BASEDIR "\\tname1"));,
890                 NOTIFY_ACTION_MODIFIED,
891                 FILE_NOTIFY_CHANGE_LAST_ACCESS, 1);
892
893         printf("Testing set file write time\n");
894         NOTIFY_MASK_TEST("Testing set file write time",
895                 fnum2 = create_complex_file(cli, tctx, BASEDIR "\\tname1");,
896                 smbcli_fsetatr(cli->tree, fnum2, 0, 0, 0, t, 0);,
897                 (smbcli_close(cli->tree, fnum2), smbcli_unlink(cli->tree, BASEDIR "\\tname1"));,
898                 NOTIFY_ACTION_MODIFIED,
899                 FILE_NOTIFY_CHANGE_LAST_WRITE, 1);
900
901         printf("Testing set file change time\n");
902         NOTIFY_MASK_TEST("Testing set file change time",
903                 fnum2 = create_complex_file(cli, tctx, BASEDIR "\\tname1");,
904                 smbcli_fsetatr(cli->tree, fnum2, 0, 0, 0, 0, t);,
905                 (smbcli_close(cli->tree, fnum2), smbcli_unlink(cli->tree, BASEDIR "\\tname1"));,
906                 NOTIFY_ACTION_MODIFIED,
907                 0, 1);
908
909
910         printf("Testing write\n");
911         NOTIFY_MASK_TEST("Testing write",
912                 fnum2 = create_complex_file(cli2, tctx, BASEDIR "\\tname1");,
913                 smbcli_write(cli2->tree, fnum2, 1, &c, 10000, 1);,
914                 (smbcli_close(cli2->tree, fnum2), smbcli_unlink(cli->tree, BASEDIR "\\tname1"));,
915                 NOTIFY_ACTION_MODIFIED,
916                 0, 1);
917
918         printf("Testing truncate\n");
919         NOTIFY_MASK_TEST("Testing truncate",
920                 fnum2 = create_complex_file(cli2, tctx, BASEDIR "\\tname1");,
921                 smbcli_ftruncate(cli2->tree, fnum2, 10000);,
922                 (smbcli_close(cli2->tree, fnum2), smbcli_unlink(cli2->tree, BASEDIR "\\tname1"));,
923                 NOTIFY_ACTION_MODIFIED,
924                 FILE_NOTIFY_CHANGE_SIZE | FILE_NOTIFY_CHANGE_ATTRIBUTES, 1);
925
926 done:
927         smb_raw_exit(cli->session);
928         smbcli_deltree(cli->tree, BASEDIR);
929         return ret;
930 }
931
932 /*
933   basic testing of change notify on files
934 */
935 static bool test_notify_file(struct torture_context *mem_ctx,
936                              struct smbcli_state *cli)
937 {
938         NTSTATUS status;
939         bool ret = true;
940         union smb_open io;
941         union smb_close cl;
942         union smb_notify notify;
943         struct smbcli_request *req;
944         int fnum;
945         const char *fname = BASEDIR "\\file.txt";
946
947         printf("TESTING CHANGE NOTIFY ON FILES\n");
948
949         torture_assert(mem_ctx, torture_setup_dir(cli, BASEDIR),
950                        "Failed to setup up test directory: " BASEDIR);
951
952         io.generic.level = RAW_OPEN_NTCREATEX;
953         io.ntcreatex.in.root_fid.fnum = 0;
954         io.ntcreatex.in.flags = 0;
955         io.ntcreatex.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
956         io.ntcreatex.in.create_options = 0;
957         io.ntcreatex.in.file_attr = FILE_ATTRIBUTE_NORMAL;
958         io.ntcreatex.in.share_access = NTCREATEX_SHARE_ACCESS_READ | NTCREATEX_SHARE_ACCESS_WRITE;
959         io.ntcreatex.in.alloc_size = 0;
960         io.ntcreatex.in.open_disposition = NTCREATEX_DISP_CREATE;
961         io.ntcreatex.in.impersonation = NTCREATEX_IMPERSONATION_ANONYMOUS;
962         io.ntcreatex.in.security_flags = 0;
963         io.ntcreatex.in.fname = fname;
964         status = smb_raw_open(cli->tree, mem_ctx, &io);
965         torture_assert_ntstatus_ok_goto(mem_ctx, status, ret, done,
966                                         "smb_raw_open");
967         fnum = io.ntcreatex.out.file.fnum;
968
969         /* ask for a change notify,
970            on file or directory name changes */
971         notify.nttrans.level = RAW_NOTIFY_NTTRANS;
972         notify.nttrans.in.file.fnum = fnum;
973         notify.nttrans.in.buffer_size = 1000;
974         notify.nttrans.in.completion_filter = FILE_NOTIFY_CHANGE_STREAM_NAME;
975         notify.nttrans.in.recursive = false;
976
977         printf("Testing if notifies on file handles are invalid (should be)\n");
978
979         req = smb_raw_changenotify_send(cli->tree, &notify);
980         status = smb_raw_changenotify_recv(req, mem_ctx, &notify);
981         torture_assert_ntstatus_equal_goto(mem_ctx, status,
982                                            NT_STATUS_INVALID_PARAMETER,
983                                            ret, done,
984                                            "smb_raw_changenotify_recv");
985
986         cl.close.level = RAW_CLOSE_CLOSE;
987         cl.close.in.file.fnum = fnum;
988         cl.close.in.write_time = 0;
989         status = smb_raw_close(cli->tree, &cl);
990         torture_assert_ntstatus_ok_goto(mem_ctx, status, ret, done,
991                                         "smb_raw_close");
992
993         status = smbcli_unlink(cli->tree, fname);
994         torture_assert_ntstatus_ok_goto(mem_ctx, status, ret, done,
995                                         "smbcli_unlink");
996
997 done:
998         smb_raw_exit(cli->session);
999         smbcli_deltree(cli->tree, BASEDIR);
1000         return ret;
1001 }
1002
1003 /*
1004   basic testing of change notifies followed by a tdis
1005 */
1006 static bool test_notify_tdis(struct torture_context *tctx,
1007                              struct smbcli_state *cli1)
1008 {
1009         bool ret = true;
1010         NTSTATUS status;
1011         union smb_notify notify;
1012         union smb_open io;
1013         int fnum;
1014         struct smbcli_request *req;
1015         struct smbcli_state *cli = NULL;
1016
1017         printf("TESTING CHANGE NOTIFY FOLLOWED BY TDIS\n");
1018
1019         torture_assert(tctx, torture_setup_dir(cli1, BASEDIR),
1020                        "Failed to setup up test directory: " BASEDIR);
1021
1022         torture_assert(tctx, torture_open_connection(&cli, tctx, 0),
1023                        "Failed to open connection.");
1024
1025         /*
1026           get a handle on the directory
1027         */
1028         io.generic.level = RAW_OPEN_NTCREATEX;
1029         io.ntcreatex.in.root_fid.fnum = 0;
1030         io.ntcreatex.in.flags = 0;
1031         io.ntcreatex.in.access_mask = SEC_FILE_ALL;
1032         io.ntcreatex.in.create_options = NTCREATEX_OPTIONS_DIRECTORY;
1033         io.ntcreatex.in.file_attr = FILE_ATTRIBUTE_NORMAL;
1034         io.ntcreatex.in.share_access = NTCREATEX_SHARE_ACCESS_READ | NTCREATEX_SHARE_ACCESS_WRITE;
1035         io.ntcreatex.in.alloc_size = 0;
1036         io.ntcreatex.in.open_disposition = NTCREATEX_DISP_OPEN;
1037         io.ntcreatex.in.impersonation = NTCREATEX_IMPERSONATION_ANONYMOUS;
1038         io.ntcreatex.in.security_flags = 0;
1039         io.ntcreatex.in.fname = BASEDIR;
1040
1041         status = smb_raw_open(cli->tree, tctx, &io);
1042         torture_assert_ntstatus_ok_goto(tctx, status, ret, done,
1043                                         "smb_raw_open");
1044         fnum = io.ntcreatex.out.file.fnum;
1045
1046         /* ask for a change notify,
1047            on file or directory name changes */
1048         notify.nttrans.level = RAW_NOTIFY_NTTRANS;
1049         notify.nttrans.in.buffer_size = 1000;
1050         notify.nttrans.in.completion_filter = FILE_NOTIFY_CHANGE_NAME;
1051         notify.nttrans.in.file.fnum = fnum;
1052         notify.nttrans.in.recursive = true;
1053
1054         req = smb_raw_changenotify_send(cli->tree, &notify);
1055
1056         status = smbcli_tdis(cli);
1057         torture_assert_ntstatus_ok_goto(tctx, status, ret, done,
1058                                         "smbcli_tdis");
1059         cli->tree = NULL;
1060
1061         status = smb_raw_changenotify_recv(req, tctx, &notify);
1062         torture_assert_ntstatus_ok_goto(tctx, status, ret, done,
1063                                         "smb_raw_changenotify_recv");
1064         torture_assert_int_equal_goto(tctx, notify.nttrans.out.num_changes,
1065                                       0, ret, done, "no changes expected");
1066
1067 done:
1068         torture_close_connection(cli);
1069         smbcli_deltree(cli1->tree, BASEDIR);
1070         return ret;
1071 }
1072
1073 /*
1074   basic testing of change notifies followed by a exit
1075 */
1076 static bool test_notify_exit(struct torture_context *tctx,
1077                              struct smbcli_state *cli1)
1078 {
1079         bool ret = true;
1080         NTSTATUS status;
1081         union smb_notify notify;
1082         union smb_open io;
1083         int fnum;
1084         struct smbcli_request *req;
1085         struct smbcli_state *cli = NULL;
1086
1087         printf("TESTING CHANGE NOTIFY FOLLOWED BY EXIT\n");
1088
1089         torture_assert(tctx, torture_setup_dir(cli1, BASEDIR),
1090                        "Failed to setup up test directory: " BASEDIR);
1091
1092         torture_assert(tctx, torture_open_connection(&cli, tctx, 0),
1093                        "Failed to open connection.");
1094
1095         /*
1096           get a handle on the directory
1097         */
1098         io.generic.level = RAW_OPEN_NTCREATEX;
1099         io.ntcreatex.in.root_fid.fnum = 0;
1100         io.ntcreatex.in.flags = 0;
1101         io.ntcreatex.in.access_mask = SEC_FILE_ALL;
1102         io.ntcreatex.in.create_options = NTCREATEX_OPTIONS_DIRECTORY;
1103         io.ntcreatex.in.file_attr = FILE_ATTRIBUTE_NORMAL;
1104         io.ntcreatex.in.share_access = NTCREATEX_SHARE_ACCESS_READ | NTCREATEX_SHARE_ACCESS_WRITE;
1105         io.ntcreatex.in.alloc_size = 0;
1106         io.ntcreatex.in.open_disposition = NTCREATEX_DISP_OPEN;
1107         io.ntcreatex.in.impersonation = NTCREATEX_IMPERSONATION_ANONYMOUS;
1108         io.ntcreatex.in.security_flags = 0;
1109         io.ntcreatex.in.fname = BASEDIR;
1110
1111         status = smb_raw_open(cli->tree, tctx, &io);
1112         torture_assert_ntstatus_ok_goto(tctx, status, ret, done,
1113                                         "smb_raw_open");
1114         fnum = io.ntcreatex.out.file.fnum;
1115
1116         /* ask for a change notify,
1117            on file or directory name changes */
1118         notify.nttrans.level = RAW_NOTIFY_NTTRANS;
1119         notify.nttrans.in.buffer_size = 1000;
1120         notify.nttrans.in.completion_filter = FILE_NOTIFY_CHANGE_NAME;
1121         notify.nttrans.in.file.fnum = fnum;
1122         notify.nttrans.in.recursive = true;
1123
1124         req = smb_raw_changenotify_send(cli->tree, &notify);
1125
1126         status = smb_raw_exit(cli->session);
1127         torture_assert_ntstatus_ok_goto(tctx, status, ret, done,
1128                                         "smb_raw_exit");
1129
1130         status = smb_raw_changenotify_recv(req, tctx, &notify);
1131         torture_assert_ntstatus_ok_goto(tctx, status, ret, done,
1132                                         "smb_raw_changenotify_recv");
1133         torture_assert_int_equal_goto(tctx, notify.nttrans.out.num_changes,
1134                                       0, ret, done, "no changes expected");
1135
1136 done:
1137         torture_close_connection(cli);
1138         smbcli_deltree(cli1->tree, BASEDIR);
1139         return ret;
1140 }
1141
1142 /*
1143   basic testing of change notifies followed by a ulogoff
1144 */
1145 static bool test_notify_ulogoff(struct torture_context *tctx,
1146                                 struct smbcli_state *cli1)
1147 {
1148         bool ret = true;
1149         NTSTATUS status;
1150         union smb_notify notify;
1151         union smb_open io;
1152         int fnum;
1153         struct smbcli_request *req;
1154         struct smbcli_state *cli = NULL;
1155
1156         printf("TESTING CHANGE NOTIFY FOLLOWED BY ULOGOFF\n");
1157
1158         torture_assert(tctx, torture_setup_dir(cli1, BASEDIR),
1159                        "Failed to setup up test directory: " BASEDIR);
1160
1161         torture_assert(tctx, torture_open_connection(&cli, tctx, 0),
1162                        "Failed to open connection.");
1163
1164         /*
1165           get a handle on the directory
1166         */
1167         io.generic.level = RAW_OPEN_NTCREATEX;
1168         io.ntcreatex.in.root_fid.fnum = 0;
1169         io.ntcreatex.in.flags = 0;
1170         io.ntcreatex.in.access_mask = SEC_FILE_ALL;
1171         io.ntcreatex.in.create_options = NTCREATEX_OPTIONS_DIRECTORY;
1172         io.ntcreatex.in.file_attr = FILE_ATTRIBUTE_NORMAL;
1173         io.ntcreatex.in.share_access = NTCREATEX_SHARE_ACCESS_READ | NTCREATEX_SHARE_ACCESS_WRITE;
1174         io.ntcreatex.in.alloc_size = 0;
1175         io.ntcreatex.in.open_disposition = NTCREATEX_DISP_OPEN;
1176         io.ntcreatex.in.impersonation = NTCREATEX_IMPERSONATION_ANONYMOUS;
1177         io.ntcreatex.in.security_flags = 0;
1178         io.ntcreatex.in.fname = BASEDIR;
1179
1180         status = smb_raw_open(cli->tree, tctx, &io);
1181         torture_assert_ntstatus_ok_goto(tctx, status, ret, done,
1182                                         "smb_raw_open");
1183         fnum = io.ntcreatex.out.file.fnum;
1184
1185         /* ask for a change notify,
1186            on file or directory name changes */
1187         notify.nttrans.level = RAW_NOTIFY_NTTRANS;
1188         notify.nttrans.in.buffer_size = 1000;
1189         notify.nttrans.in.completion_filter = FILE_NOTIFY_CHANGE_NAME;
1190         notify.nttrans.in.file.fnum = fnum;
1191         notify.nttrans.in.recursive = true;
1192
1193         req = smb_raw_changenotify_send(cli->tree, &notify);
1194
1195         status = smb_raw_ulogoff(cli->session);
1196         torture_assert_ntstatus_ok_goto(tctx, status, ret, done,
1197                                         "smb_raw_ulogoff");
1198
1199         status = smb_raw_changenotify_recv(req, tctx, &notify);
1200         torture_assert_ntstatus_ok_goto(tctx, status, ret, done,
1201                                         "smb_raw_changenotify_recv");
1202         torture_assert_int_equal_goto(tctx, notify.nttrans.out.num_changes,
1203                                       0, ret, done, "no changes expected");
1204
1205 done:
1206         torture_close_connection(cli);
1207         smbcli_deltree(cli1->tree, BASEDIR);
1208         return ret;
1209 }
1210
1211 static void tcp_dis_handler(struct smbcli_transport *t, void *p)
1212 {
1213         struct smbcli_state *cli = (struct smbcli_state *)p;
1214         smbcli_transport_dead(cli->transport, NT_STATUS_LOCAL_DISCONNECT);
1215         cli->transport = NULL;
1216         cli->tree = NULL;
1217 }
1218 /*
1219   basic testing of change notifies followed by tcp disconnect
1220 */
1221 static bool test_notify_tcp_dis(struct torture_context *tctx,
1222                                 struct smbcli_state *cli1)
1223 {
1224         bool ret = true;
1225         NTSTATUS status;
1226         union smb_notify notify;
1227         union smb_open io;
1228         int fnum;
1229         struct smbcli_request *req;
1230         struct smbcli_state *cli = NULL;
1231
1232         printf("TESTING CHANGE NOTIFY FOLLOWED BY TCP DISCONNECT\n");
1233
1234         torture_assert(tctx, torture_setup_dir(cli1, BASEDIR),
1235                        "Failed to setup up test directory: " BASEDIR);
1236
1237         torture_assert(tctx, torture_open_connection(&cli, tctx, 0),
1238                        "Failed to open connection.");
1239
1240         /*
1241           get a handle on the directory
1242         */
1243         io.generic.level = RAW_OPEN_NTCREATEX;
1244         io.ntcreatex.in.root_fid.fnum = 0;
1245         io.ntcreatex.in.flags = 0;
1246         io.ntcreatex.in.access_mask = SEC_FILE_ALL;
1247         io.ntcreatex.in.create_options = NTCREATEX_OPTIONS_DIRECTORY;
1248         io.ntcreatex.in.file_attr = FILE_ATTRIBUTE_NORMAL;
1249         io.ntcreatex.in.share_access = NTCREATEX_SHARE_ACCESS_READ | NTCREATEX_SHARE_ACCESS_WRITE;
1250         io.ntcreatex.in.alloc_size = 0;
1251         io.ntcreatex.in.open_disposition = NTCREATEX_DISP_OPEN;
1252         io.ntcreatex.in.impersonation = NTCREATEX_IMPERSONATION_ANONYMOUS;
1253         io.ntcreatex.in.security_flags = 0;
1254         io.ntcreatex.in.fname = BASEDIR;
1255
1256         status = smb_raw_open(cli->tree, tctx, &io);
1257         torture_assert_ntstatus_ok_goto(tctx, status, ret, done,
1258                                         "smb_raw_open");
1259         fnum = io.ntcreatex.out.file.fnum;
1260
1261         /* ask for a change notify,
1262            on file or directory name changes */
1263         notify.nttrans.level = RAW_NOTIFY_NTTRANS;
1264         notify.nttrans.in.buffer_size = 1000;
1265         notify.nttrans.in.completion_filter = FILE_NOTIFY_CHANGE_NAME;
1266         notify.nttrans.in.file.fnum = fnum;
1267         notify.nttrans.in.recursive = true;
1268
1269         req = smb_raw_changenotify_send(cli->tree, &notify);
1270
1271         smbcli_transport_idle_handler(cli->transport, tcp_dis_handler, 250, cli);
1272
1273         status = smb_raw_changenotify_recv(req, tctx, &notify);
1274         torture_assert_ntstatus_equal_goto(tctx, status,
1275                                            NT_STATUS_LOCAL_DISCONNECT,
1276                                            ret, done,
1277                                            "smb_raw_changenotify_recv");
1278
1279 done:
1280         torture_close_connection(cli);
1281         smbcli_deltree(cli1->tree, BASEDIR);
1282         return ret;
1283 }
1284
1285 /* 
1286    test setting up two change notify requests on one handle
1287 */
1288 static bool test_notify_double(struct torture_context *mem_ctx,
1289                                struct smbcli_state *cli)
1290 {
1291         bool ret = true;
1292         NTSTATUS status;
1293         union smb_notify notify;
1294         union smb_open io;
1295         int fnum;
1296         struct smbcli_request *req1, *req2;
1297
1298         printf("TESTING CHANGE NOTIFY TWICE ON ONE DIRECTORY\n");
1299
1300         torture_assert(mem_ctx, torture_setup_dir(cli, BASEDIR),
1301                        "Failed to setup up test directory: " BASEDIR);
1302
1303         /*
1304           get a handle on the directory
1305         */
1306         io.generic.level = RAW_OPEN_NTCREATEX;
1307         io.ntcreatex.in.root_fid.fnum = 0;
1308         io.ntcreatex.in.flags = 0;
1309         io.ntcreatex.in.access_mask = SEC_FILE_ALL;
1310         io.ntcreatex.in.create_options = NTCREATEX_OPTIONS_DIRECTORY;
1311         io.ntcreatex.in.file_attr = FILE_ATTRIBUTE_NORMAL;
1312         io.ntcreatex.in.share_access = NTCREATEX_SHARE_ACCESS_READ | NTCREATEX_SHARE_ACCESS_WRITE;
1313         io.ntcreatex.in.alloc_size = 0;
1314         io.ntcreatex.in.open_disposition = NTCREATEX_DISP_OPEN;
1315         io.ntcreatex.in.impersonation = NTCREATEX_IMPERSONATION_ANONYMOUS;
1316         io.ntcreatex.in.security_flags = 0;
1317         io.ntcreatex.in.fname = BASEDIR;
1318
1319         status = smb_raw_open(cli->tree, mem_ctx, &io);
1320         torture_assert_ntstatus_ok_goto(mem_ctx, status, ret, done,
1321                                         "smb_raw_open");
1322         fnum = io.ntcreatex.out.file.fnum;
1323
1324         /* ask for a change notify,
1325            on file or directory name changes */
1326         notify.nttrans.level = RAW_NOTIFY_NTTRANS;
1327         notify.nttrans.in.buffer_size = 1000;
1328         notify.nttrans.in.completion_filter = FILE_NOTIFY_CHANGE_NAME;
1329         notify.nttrans.in.file.fnum = fnum;
1330         notify.nttrans.in.recursive = true;
1331
1332         req1 = smb_raw_changenotify_send(cli->tree, &notify);
1333         req2 = smb_raw_changenotify_send(cli->tree, &notify);
1334
1335         smbcli_mkdir(cli->tree, BASEDIR "\\subdir-name");
1336
1337         status = smb_raw_changenotify_recv(req1, mem_ctx, &notify);
1338         torture_assert_ntstatus_ok_goto(mem_ctx, status, ret, done,
1339                                         "smb_raw_changenotify_recv");
1340         torture_assert_int_equal_goto(mem_ctx, notify.nttrans.out.num_changes,
1341                                       1, ret, done, "wrong number of changes");
1342         CHECK_WSTR(mem_ctx, notify.nttrans.out.changes[0].name, "subdir-name",
1343                    STR_UNICODE);
1344
1345         smbcli_mkdir(cli->tree, BASEDIR "\\subdir-name2");
1346
1347         status = smb_raw_changenotify_recv(req2, mem_ctx, &notify);
1348         torture_assert_ntstatus_ok_goto(mem_ctx, status, ret, done,
1349                                         "smb_raw_changenotify_recv");
1350         torture_assert_int_equal_goto(mem_ctx, notify.nttrans.out.num_changes,
1351                                       1, ret, done, "wrong number of changes");
1352         CHECK_WSTR(mem_ctx, notify.nttrans.out.changes[0].name, "subdir-name2",
1353                    STR_UNICODE);
1354
1355 done:
1356         smb_raw_exit(cli->session);
1357         smbcli_deltree(cli->tree, BASEDIR);
1358         return ret;
1359 }
1360
1361
1362 /* 
1363    test multiple change notifies at different depths and with/without recursion
1364 */
1365 static bool test_notify_tree(struct torture_context *mem_ctx,
1366                              struct smbcli_state *cli,
1367                              struct smbcli_state *cli2)
1368 {
1369         bool ret = true;
1370         union smb_notify notify;
1371         union smb_open io;
1372         struct smbcli_request *req;
1373         struct timeval tv;
1374         struct {
1375                 const char *path;
1376                 bool recursive;
1377                 uint32_t filter;
1378                 int expected;
1379                 int fnum;
1380                 int counted;
1381         } dirs[] = {
1382                 {BASEDIR "\\abc",               true, FILE_NOTIFY_CHANGE_NAME, 30 },
1383                 {BASEDIR "\\zqy",               true, FILE_NOTIFY_CHANGE_NAME, 8 },
1384                 {BASEDIR "\\atsy",              true, FILE_NOTIFY_CHANGE_NAME, 4 },
1385                 {BASEDIR "\\abc\\foo",          true,  FILE_NOTIFY_CHANGE_NAME, 2 },
1386                 {BASEDIR "\\abc\\blah",         true,  FILE_NOTIFY_CHANGE_NAME, 13 },
1387                 {BASEDIR "\\abc\\blah",         false, FILE_NOTIFY_CHANGE_NAME, 7 },
1388                 {BASEDIR "\\abc\\blah\\a",      true, FILE_NOTIFY_CHANGE_NAME, 2 },
1389                 {BASEDIR "\\abc\\blah\\b",      true, FILE_NOTIFY_CHANGE_NAME, 2 },
1390                 {BASEDIR "\\abc\\blah\\c",      true, FILE_NOTIFY_CHANGE_NAME, 2 },
1391                 {BASEDIR "\\abc\\fooblah",      true, FILE_NOTIFY_CHANGE_NAME, 2 },
1392                 {BASEDIR "\\zqy\\xx",           true, FILE_NOTIFY_CHANGE_NAME, 2 },
1393                 {BASEDIR "\\zqy\\yyy",          true, FILE_NOTIFY_CHANGE_NAME, 2 },
1394                 {BASEDIR "\\zqy\\..",           true, FILE_NOTIFY_CHANGE_NAME, 40 },
1395                 {BASEDIR,                       true, FILE_NOTIFY_CHANGE_NAME, 40 },
1396                 {BASEDIR,                       false,FILE_NOTIFY_CHANGE_NAME, 6 },
1397                 {BASEDIR "\\atsy",              false,FILE_NOTIFY_CHANGE_NAME, 4 },
1398                 {BASEDIR "\\abc",               true, FILE_NOTIFY_CHANGE_NAME, 24 },
1399                 {BASEDIR "\\abc",               false,FILE_NOTIFY_CHANGE_FILE_NAME, 0 },
1400                 {BASEDIR "\\abc",               true, FILE_NOTIFY_CHANGE_FILE_NAME, 0 },
1401                 {BASEDIR "\\abc",               true, FILE_NOTIFY_CHANGE_NAME, 24 },
1402         };
1403         int i;
1404         NTSTATUS status;
1405         bool all_done = false;
1406
1407         printf("TESTING CHANGE NOTIFY FOR DIFFERENT DEPTHS\n");
1408
1409         torture_assert(mem_ctx, torture_setup_dir(cli, BASEDIR),
1410                        "Failed to setup up test directory: " BASEDIR);
1411
1412         io.generic.level = RAW_OPEN_NTCREATEX;
1413         io.ntcreatex.in.root_fid.fnum = 0;
1414         io.ntcreatex.in.flags = 0;
1415         io.ntcreatex.in.access_mask = SEC_FILE_ALL;
1416         io.ntcreatex.in.create_options = NTCREATEX_OPTIONS_DIRECTORY;
1417         io.ntcreatex.in.file_attr = FILE_ATTRIBUTE_NORMAL;
1418         io.ntcreatex.in.share_access = NTCREATEX_SHARE_ACCESS_READ | NTCREATEX_SHARE_ACCESS_WRITE;
1419         io.ntcreatex.in.alloc_size = 0;
1420         io.ntcreatex.in.open_disposition = NTCREATEX_DISP_OPEN_IF;
1421         io.ntcreatex.in.impersonation = NTCREATEX_IMPERSONATION_ANONYMOUS;
1422         io.ntcreatex.in.security_flags = 0;
1423
1424         notify.nttrans.level = RAW_NOTIFY_NTTRANS;
1425         notify.nttrans.in.buffer_size = 20000;
1426
1427         /*
1428           setup the directory tree, and the notify buffer on each directory
1429         */
1430         for (i=0;i<ARRAY_SIZE(dirs);i++) {
1431                 io.ntcreatex.in.fname = dirs[i].path;
1432                 status = smb_raw_open(cli->tree, mem_ctx, &io);
1433                 torture_assert_ntstatus_ok_goto(mem_ctx, status, ret, done,
1434                                                 "smb_raw_open");
1435                 dirs[i].fnum = io.ntcreatex.out.file.fnum;
1436
1437                 notify.nttrans.in.completion_filter = dirs[i].filter;
1438                 notify.nttrans.in.file.fnum = dirs[i].fnum;
1439                 notify.nttrans.in.recursive = dirs[i].recursive;
1440                 req = smb_raw_changenotify_send(cli->tree, &notify);
1441                 smb_raw_ntcancel(req);
1442                 status = smb_raw_changenotify_recv(req, mem_ctx, &notify);
1443                 torture_assert_ntstatus_equal_goto(mem_ctx, status,
1444                                                    NT_STATUS_CANCELLED,
1445                                                    ret, done,
1446                                                    "smb_raw_changenotify_recv");
1447         }
1448
1449         /* trigger 2 events in each dir */
1450         for (i=0;i<ARRAY_SIZE(dirs);i++) {
1451                 char *path = talloc_asprintf(mem_ctx, "%s\\test.dir", dirs[i].path);
1452                 /*
1453                  * Make notifies a bit more interesting in a cluster
1454                  * by doing the changes against different nodes with
1455                  * --unclist
1456                  */
1457                 smbcli_mkdir(cli->tree, path);
1458                 smbcli_rmdir(cli2->tree, path);
1459                 talloc_free(path);
1460         }
1461
1462         /* give a bit of time for the events to propogate */
1463         tv = timeval_current();
1464
1465         do {
1466                 /* count events that have happened in each dir */
1467                 for (i=0;i<ARRAY_SIZE(dirs);i++) {
1468                         notify.nttrans.in.file.fnum = dirs[i].fnum;
1469                         req = smb_raw_changenotify_send(cli->tree, &notify);
1470                         smb_raw_ntcancel(req);
1471                         notify.nttrans.out.num_changes = 0;
1472                         status = smb_raw_changenotify_recv(req, mem_ctx, &notify);
1473                         dirs[i].counted += notify.nttrans.out.num_changes;
1474                 }
1475                 
1476                 all_done = true;
1477
1478                 for (i=0;i<ARRAY_SIZE(dirs);i++) {
1479                         if (dirs[i].counted != dirs[i].expected) {
1480                                 all_done = false;
1481                         }
1482                 }
1483         } while (!all_done && timeval_elapsed(&tv) < 20);
1484
1485         printf("took %.4f seconds to propogate all events\n", timeval_elapsed(&tv));
1486
1487         for (i=0;i<ARRAY_SIZE(dirs);i++) {
1488                 if (dirs[i].counted != dirs[i].expected) {
1489                         printf("ERROR: i=%d expected %d got %d for '%s'\n",
1490                                i, dirs[i].expected, dirs[i].counted, dirs[i].path);
1491                         ret = false;
1492                 }
1493         }
1494
1495         /*
1496           run from the back, closing and deleting
1497         */
1498         for (i=ARRAY_SIZE(dirs)-1;i>=0;i--) {
1499                 smbcli_close(cli->tree, dirs[i].fnum);
1500                 smbcli_rmdir(cli->tree, dirs[i].path);
1501         }
1502
1503 done:
1504         smb_raw_exit(cli->session);
1505         smbcli_deltree(cli->tree, BASEDIR);
1506         return ret;
1507 }
1508
1509 /*
1510    Test response when cached server events exceed single NT NOTFIY response
1511    packet size.
1512 */
1513 static bool test_notify_overflow(struct torture_context *mem_ctx,
1514                                  struct smbcli_state *cli)
1515 {
1516         bool ret = true;
1517         NTSTATUS status;
1518         union smb_notify notify;
1519         union smb_open io;
1520         int fnum;
1521         int count = 100;
1522         struct smbcli_request *req1;
1523         int i;
1524
1525         printf("TESTING CHANGE NOTIFY EVENT OVERFLOW\n");
1526
1527         torture_assert(mem_ctx, torture_setup_dir(cli, BASEDIR),
1528                        "Failed to setup up test directory: " BASEDIR);
1529
1530         /* get a handle on the directory */
1531         io.generic.level = RAW_OPEN_NTCREATEX;
1532         io.ntcreatex.in.root_fid.fnum = 0;
1533         io.ntcreatex.in.flags = 0;
1534         io.ntcreatex.in.access_mask = SEC_FILE_ALL;
1535         io.ntcreatex.in.create_options = NTCREATEX_OPTIONS_DIRECTORY;
1536         io.ntcreatex.in.file_attr = FILE_ATTRIBUTE_NORMAL;
1537         io.ntcreatex.in.share_access = NTCREATEX_SHARE_ACCESS_READ |
1538             NTCREATEX_SHARE_ACCESS_WRITE;
1539         io.ntcreatex.in.alloc_size = 0;
1540         io.ntcreatex.in.open_disposition = NTCREATEX_DISP_OPEN;
1541         io.ntcreatex.in.impersonation = NTCREATEX_IMPERSONATION_ANONYMOUS;
1542         io.ntcreatex.in.security_flags = 0;
1543         io.ntcreatex.in.fname = BASEDIR;
1544
1545         status = smb_raw_open(cli->tree, mem_ctx, &io);
1546         torture_assert_ntstatus_ok_goto(mem_ctx, status, ret, done,
1547                                         "smb_raw_open");
1548         fnum = io.ntcreatex.out.file.fnum;
1549
1550         /* ask for a change notify, on name changes. */
1551         notify.nttrans.level = RAW_NOTIFY_NTTRANS;
1552         notify.nttrans.in.buffer_size = 1000;
1553         notify.nttrans.in.completion_filter = FILE_NOTIFY_CHANGE_NAME;
1554         notify.nttrans.in.file.fnum = fnum;
1555
1556         notify.nttrans.in.recursive = true;
1557         req1 = smb_raw_changenotify_send(cli->tree, &notify);
1558
1559         /* cancel initial requests so the buffer is setup */
1560         smb_raw_ntcancel(req1);
1561         status = smb_raw_changenotify_recv(req1, mem_ctx, &notify);
1562         torture_assert_ntstatus_equal_goto(mem_ctx, status,
1563                                            NT_STATUS_CANCELLED,
1564                                            ret, done,
1565                                            "smb_raw_changenotify_recv");
1566
1567         /* open a lot of files, filling up the server side notify buffer */
1568         printf("Testing overflowed buffer notify on create of %d files\n",
1569                count);
1570         for (i=0;i<count;i++) {
1571                 char *fname = talloc_asprintf(cli, BASEDIR "\\test%d.txt", i);
1572                 int fnum2 = smbcli_open(cli->tree, fname, O_CREAT|O_RDWR,
1573                                         DENY_NONE);
1574                 if (fnum2 == -1) {
1575                         printf("Failed to create %s - %s\n",
1576                                fname, smbcli_errstr(cli->tree));
1577                         ret = false;
1578                         goto done;
1579                 }
1580                 talloc_free(fname);
1581                 smbcli_close(cli->tree, fnum2);
1582         }
1583
1584         /* expect that 0 events will be returned with NT_STATUS_OK */
1585         req1 = smb_raw_changenotify_send(cli->tree, &notify);
1586         status = smb_raw_changenotify_recv(req1, mem_ctx, &notify);
1587         torture_assert_ntstatus_ok_goto(mem_ctx, status, ret, done,
1588                                         "smb_raw_changenotify_recv");
1589         torture_assert_int_equal_goto(mem_ctx, notify.nttrans.out.num_changes,
1590                                       0, ret, done, "no changes expected");
1591
1592 done:
1593         smb_raw_exit(cli->session);
1594         smbcli_deltree(cli->tree, BASEDIR);
1595         return ret;
1596 }
1597
1598 /*
1599    Test if notifications are returned for changes to the base directory.
1600    They shouldn't be.
1601 */
1602 static bool test_notify_basedir(struct torture_context *mem_ctx,
1603                                 struct smbcli_state *cli)
1604 {
1605         bool ret = true;
1606         NTSTATUS status;
1607         union smb_notify notify;
1608         union smb_open io;
1609         int fnum;
1610         struct smbcli_request *req1;
1611
1612         printf("TESTING CHANGE NOTIFY BASEDIR EVENTS\n");
1613
1614         torture_assert(mem_ctx, torture_setup_dir(cli, BASEDIR),
1615                        "Failed to setup up test directory: " BASEDIR);
1616
1617         /* get a handle on the directory */
1618         io.generic.level = RAW_OPEN_NTCREATEX;
1619         io.ntcreatex.in.root_fid.fnum = 0;
1620         io.ntcreatex.in.flags = 0;
1621         io.ntcreatex.in.access_mask = SEC_FILE_ALL;
1622         io.ntcreatex.in.create_options = NTCREATEX_OPTIONS_DIRECTORY;
1623         io.ntcreatex.in.file_attr = FILE_ATTRIBUTE_NORMAL;
1624         io.ntcreatex.in.share_access = NTCREATEX_SHARE_ACCESS_READ |
1625             NTCREATEX_SHARE_ACCESS_WRITE;
1626         io.ntcreatex.in.alloc_size = 0;
1627         io.ntcreatex.in.open_disposition = NTCREATEX_DISP_OPEN;
1628         io.ntcreatex.in.impersonation = NTCREATEX_IMPERSONATION_ANONYMOUS;
1629         io.ntcreatex.in.security_flags = 0;
1630         io.ntcreatex.in.fname = BASEDIR;
1631
1632         status = smb_raw_open(cli->tree, mem_ctx, &io);
1633         torture_assert_ntstatus_ok_goto(mem_ctx, status, ret, done,
1634                                         "smb_raw_open");
1635         fnum = io.ntcreatex.out.file.fnum;
1636
1637         /* create a test file that will also be modified */
1638         smbcli_close(cli->tree, smbcli_open(cli->tree, BASEDIR "\\tname1",
1639                                             O_CREAT, 0));
1640
1641         /* ask for a change notify, on attribute changes. */
1642         notify.nttrans.level = RAW_NOTIFY_NTTRANS;
1643         notify.nttrans.in.buffer_size = 1000;
1644         notify.nttrans.in.completion_filter = FILE_NOTIFY_CHANGE_ATTRIBUTES;
1645         notify.nttrans.in.file.fnum = fnum;
1646         notify.nttrans.in.recursive = true;
1647
1648         req1 = smb_raw_changenotify_send(cli->tree, &notify);
1649
1650         /* set attribute on the base dir */
1651         smbcli_setatr(cli->tree, BASEDIR, FILE_ATTRIBUTE_HIDDEN, 0);
1652
1653         /* set attribute on a file to assure we receive a notification */
1654         smbcli_setatr(cli->tree, BASEDIR "\\tname1", FILE_ATTRIBUTE_HIDDEN, 0);
1655         smb_msleep(200);
1656
1657         /* check how many responses were given, expect only 1 for the file */
1658         status = smb_raw_changenotify_recv(req1, mem_ctx, &notify);
1659         torture_assert_ntstatus_ok_goto(mem_ctx, status, ret, done,
1660                                         "smb_raw_changenotify_recv");
1661         torture_assert_int_equal_goto(mem_ctx, notify.nttrans.out.num_changes,
1662                                       1, ret, done, "wrong number of  changes");
1663         torture_assert_int_equal_goto(mem_ctx,
1664                                       notify.nttrans.out.changes[0].action,
1665                                       NOTIFY_ACTION_MODIFIED, ret, done,
1666                                       "wrong action (exp: MODIFIED)");
1667         CHECK_WSTR(mem_ctx, notify.nttrans.out.changes[0].name, "tname1",
1668                    STR_UNICODE);
1669
1670 done:
1671         smb_raw_exit(cli->session);
1672         smbcli_deltree(cli->tree, BASEDIR);
1673         return ret;
1674 }
1675
1676
1677 /*
1678   create a secondary tree connect - used to test for a bug in Samba3 messaging
1679   with change notify
1680 */
1681 static struct smbcli_tree *secondary_tcon(struct smbcli_state *cli, 
1682                                           struct torture_context *tctx)
1683 {
1684         NTSTATUS status;
1685         const char *share, *host;
1686         struct smbcli_tree *tree;
1687         union smb_tcon tcon;
1688
1689         share = torture_setting_string(tctx, "share", NULL);
1690         host  = torture_setting_string(tctx, "host", NULL);
1691         
1692         printf("create a second tree context on the same session\n");
1693         tree = smbcli_tree_init(cli->session, tctx, false);
1694
1695         tcon.generic.level = RAW_TCON_TCONX;
1696         tcon.tconx.in.flags = TCONX_FLAG_EXTENDED_RESPONSE;
1697         tcon.tconx.in.password = data_blob(NULL, 0);
1698         tcon.tconx.in.path = talloc_asprintf(tctx, "\\\\%s\\%s", host, share);
1699         tcon.tconx.in.device = "A:";    
1700         status = smb_raw_tcon(tree, tctx, &tcon);
1701         if (!NT_STATUS_IS_OK(status)) {
1702                 talloc_free(tree);
1703                 printf("Failed to create secondary tree\n");
1704                 return NULL;
1705         }
1706
1707         tree->tid = tcon.tconx.out.tid;
1708         printf("tid1=%d tid2=%d\n", cli->tree->tid, tree->tid);
1709
1710         return tree;
1711 }
1712
1713
1714 /* 
1715    very simple change notify test
1716 */
1717 static bool test_notify_tcon(struct torture_context *torture,
1718                              struct smbcli_state *cli)
1719 {
1720         bool ret = true;
1721         NTSTATUS status;
1722         union smb_notify notify;
1723         union smb_open io;
1724         int fnum;
1725         struct smbcli_request *req;
1726         extern int torture_numops;
1727         struct smbcli_tree *tree = NULL;
1728                 
1729         printf("TESTING SIMPLE CHANGE NOTIFY\n");
1730
1731         torture_assert(torture, torture_setup_dir(cli, BASEDIR),
1732                        "Failed to setup up test directory: " BASEDIR);
1733
1734         /*
1735           get a handle on the directory
1736         */
1737         io.generic.level = RAW_OPEN_NTCREATEX;
1738         io.ntcreatex.in.root_fid.fnum = 0;
1739         io.ntcreatex.in.flags = 0;
1740         io.ntcreatex.in.access_mask = SEC_FILE_ALL;
1741         io.ntcreatex.in.create_options = NTCREATEX_OPTIONS_DIRECTORY;
1742         io.ntcreatex.in.file_attr = FILE_ATTRIBUTE_NORMAL;
1743         io.ntcreatex.in.share_access = NTCREATEX_SHARE_ACCESS_READ | NTCREATEX_SHARE_ACCESS_WRITE;
1744         io.ntcreatex.in.alloc_size = 0;
1745         io.ntcreatex.in.open_disposition = NTCREATEX_DISP_OPEN;
1746         io.ntcreatex.in.impersonation = NTCREATEX_IMPERSONATION_ANONYMOUS;
1747         io.ntcreatex.in.security_flags = 0;
1748         io.ntcreatex.in.fname = BASEDIR;
1749
1750         status = smb_raw_open(cli->tree, torture, &io);
1751         torture_assert_ntstatus_ok_goto(torture, status, ret, done,
1752                                         "smb_raw_open");
1753         fnum = io.ntcreatex.out.file.fnum;
1754
1755         status = smb_raw_open(cli->tree, torture, &io);
1756         torture_assert_ntstatus_ok_goto(torture, status, ret, done,
1757                                         "smb_raw_open");
1758
1759         /* ask for a change notify,
1760            on file or directory name changes */
1761         notify.nttrans.level = RAW_NOTIFY_NTTRANS;
1762         notify.nttrans.in.buffer_size = 1000;
1763         notify.nttrans.in.completion_filter = FILE_NOTIFY_CHANGE_NAME;
1764         notify.nttrans.in.file.fnum = fnum;
1765         notify.nttrans.in.recursive = true;
1766
1767         printf("Testing notify mkdir\n");
1768         req = smb_raw_changenotify_send(cli->tree, &notify);
1769         smbcli_mkdir(cli->tree, BASEDIR "\\subdir-name");
1770
1771         status = smb_raw_changenotify_recv(req, torture, &notify);
1772         torture_assert_ntstatus_ok_goto(torture, status, ret, done,
1773                                         "smb_raw_changenotify_recv");
1774
1775         torture_assert_int_equal_goto(torture, notify.nttrans.out.num_changes,
1776                                       1, ret, done, "wrong number of changes");
1777         torture_assert_int_equal_goto(torture,
1778                                       notify.nttrans.out.changes[0].action,
1779                                       NOTIFY_ACTION_ADDED, ret, done,
1780                                       "wrong action (exp: ADDED)");
1781         CHECK_WSTR(torture, notify.nttrans.out.changes[0].name, "subdir-name",
1782                    STR_UNICODE);
1783
1784         printf("Testing notify rmdir\n");
1785         req = smb_raw_changenotify_send(cli->tree, &notify);
1786         smbcli_rmdir(cli->tree, BASEDIR "\\subdir-name");
1787
1788         status = smb_raw_changenotify_recv(req, torture, &notify);
1789         torture_assert_ntstatus_ok_goto(torture, status, ret, done,
1790                                         "smb_raw_changenotify_recv");
1791         torture_assert_int_equal_goto(torture, notify.nttrans.out.num_changes,
1792                                       1, ret, done, "wrong number of changes");
1793         torture_assert_int_equal_goto(torture,
1794                                       notify.nttrans.out.changes[0].action,
1795                                       NOTIFY_ACTION_REMOVED, ret, done,
1796                                       "wrong action (exp: REMOVED)");
1797         CHECK_WSTR(torture, notify.nttrans.out.changes[0].name, "subdir-name",
1798                    STR_UNICODE);
1799
1800         printf("SIMPLE CHANGE NOTIFY OK\n");
1801
1802         printf("TESTING WITH SECONDARY TCON\n");
1803         tree = secondary_tcon(cli, torture);
1804
1805         printf("Testing notify mkdir\n");
1806         req = smb_raw_changenotify_send(cli->tree, &notify);
1807         smbcli_mkdir(cli->tree, BASEDIR "\\subdir-name");
1808
1809         status = smb_raw_changenotify_recv(req, torture, &notify);
1810         torture_assert_ntstatus_ok_goto(torture, status, ret, done,
1811                                         "smb_raw_changenotify_recv");
1812
1813         torture_assert_int_equal_goto(torture, notify.nttrans.out.num_changes,
1814                                       1, ret, done, "wrong number of changes");
1815         torture_assert_int_equal_goto(torture,
1816                                       notify.nttrans.out.changes[0].action,
1817                                       NOTIFY_ACTION_ADDED, ret, done,
1818                                       "wrong action (exp: ADDED)");
1819         CHECK_WSTR(torture, notify.nttrans.out.changes[0].name, "subdir-name",
1820                    STR_UNICODE);
1821
1822         printf("Testing notify rmdir\n");
1823         req = smb_raw_changenotify_send(cli->tree, &notify);
1824         smbcli_rmdir(cli->tree, BASEDIR "\\subdir-name");
1825
1826         status = smb_raw_changenotify_recv(req, torture, &notify);
1827         torture_assert_ntstatus_ok_goto(torture, status, ret, done,
1828                                         "smb_raw_changenotify_recv");
1829         torture_assert_int_equal_goto(torture, notify.nttrans.out.num_changes,
1830                                       1, ret, done, "wrong number of changes");
1831         torture_assert_int_equal_goto(torture,
1832                                       notify.nttrans.out.changes[0].action,
1833                                       NOTIFY_ACTION_REMOVED, ret, done,
1834                                       "wrong action (exp: REMOVED)");
1835         CHECK_WSTR(torture, notify.nttrans.out.changes[0].name, "subdir-name",
1836                    STR_UNICODE);
1837
1838         printf("CHANGE NOTIFY WITH TCON OK\n");
1839
1840         printf("Disconnecting secondary tree\n");
1841         status = smb_tree_disconnect(tree);
1842         torture_assert_ntstatus_ok_goto(torture, status, ret, done,
1843                                         "smb_tree_disconnect");
1844         talloc_free(tree);
1845
1846         printf("Testing notify mkdir\n");
1847         req = smb_raw_changenotify_send(cli->tree, &notify);
1848         smbcli_mkdir(cli->tree, BASEDIR "\\subdir-name");
1849
1850         status = smb_raw_changenotify_recv(req, torture, &notify);
1851         torture_assert_ntstatus_ok_goto(torture, status, ret, done,
1852                                         "smb_raw_changenotify_recv");
1853
1854         torture_assert_int_equal_goto(torture, notify.nttrans.out.num_changes,
1855                                       1, ret, done, "wrong number of changes");
1856         torture_assert_int_equal_goto(torture,
1857                                       notify.nttrans.out.changes[0].action,
1858                                       NOTIFY_ACTION_ADDED, ret, done,
1859                                       "wrong action (exp: ADDED)");
1860         CHECK_WSTR(torture, notify.nttrans.out.changes[0].name, "subdir-name",
1861                    STR_UNICODE);
1862
1863         printf("Testing notify rmdir\n");
1864         req = smb_raw_changenotify_send(cli->tree, &notify);
1865         smbcli_rmdir(cli->tree, BASEDIR "\\subdir-name");
1866
1867         status = smb_raw_changenotify_recv(req, torture, &notify);
1868         torture_assert_ntstatus_ok_goto(torture, status, ret, done,
1869                                         "smb_raw_changenotify_recv");
1870         torture_assert_int_equal_goto(torture, notify.nttrans.out.num_changes,
1871                                       1, ret, done, "wrong number of changes");
1872         torture_assert_int_equal_goto(torture,
1873                                       notify.nttrans.out.changes[0].action,
1874                                       NOTIFY_ACTION_REMOVED, ret, done,
1875                                       "wrong action (exp: REMOVED)");
1876         CHECK_WSTR(torture, notify.nttrans.out.changes[0].name, "subdir-name",
1877                    STR_UNICODE);
1878
1879         printf("CHANGE NOTIFY WITH TDIS OK\n");
1880 done:
1881         smb_raw_exit(cli->session);
1882         smbcli_deltree(cli->tree, BASEDIR);
1883         return ret;
1884 }
1885
1886
1887 /*
1888    testing alignment of multiple change notify infos
1889 */
1890 static bool test_notify_alignment(struct torture_context *tctx,
1891                                   struct smbcli_state *cli)
1892 {
1893         NTSTATUS status;
1894         union smb_notify notify;
1895         union smb_open io;
1896         int i, fnum, fnum2;
1897         struct smbcli_request *req;
1898         const char *fname = BASEDIR "\\starter";
1899         const char *fnames[] = { "a",
1900                                  "ab",
1901                                  "abc",
1902                                  "abcd" };
1903         int num_names = ARRAY_SIZE(fnames);
1904         char *fpath = NULL;
1905
1906         torture_comment(tctx, "TESTING CHANGE NOTIFY REPLY ALIGNMENT\n");
1907
1908         torture_assert(tctx, torture_setup_dir(cli, BASEDIR),
1909                        "Failed to setup up test directory: " BASEDIR);
1910
1911         /* get a handle on the directory */
1912         io.generic.level = RAW_OPEN_NTCREATEX;
1913         io.ntcreatex.in.root_fid.fnum = 0;
1914         io.ntcreatex.in.flags = 0;
1915         io.ntcreatex.in.access_mask = SEC_FILE_ALL;
1916         io.ntcreatex.in.create_options = NTCREATEX_OPTIONS_DIRECTORY;
1917         io.ntcreatex.in.file_attr = FILE_ATTRIBUTE_NORMAL;
1918         io.ntcreatex.in.share_access = NTCREATEX_SHARE_ACCESS_READ |
1919                                        NTCREATEX_SHARE_ACCESS_WRITE;
1920         io.ntcreatex.in.alloc_size = 0;
1921         io.ntcreatex.in.open_disposition = NTCREATEX_DISP_OPEN;
1922         io.ntcreatex.in.impersonation = NTCREATEX_IMPERSONATION_ANONYMOUS;
1923         io.ntcreatex.in.security_flags = 0;
1924         io.ntcreatex.in.fname = BASEDIR;
1925
1926         status = smb_raw_open(cli->tree, tctx, &io);
1927         torture_assert_ntstatus_ok(tctx, status, "smb_raw_open");
1928         fnum = io.ntcreatex.out.file.fnum;
1929
1930         /* ask for a change notify, on file creation */
1931         notify.nttrans.level = RAW_NOTIFY_NTTRANS;
1932         notify.nttrans.in.buffer_size = 1000;
1933         notify.nttrans.in.completion_filter = FILE_NOTIFY_CHANGE_FILE_NAME;
1934         notify.nttrans.in.file.fnum = fnum;
1935         notify.nttrans.in.recursive = false;
1936
1937         /* start change tracking */
1938         req = smb_raw_changenotify_send(cli->tree, &notify);
1939
1940         fnum2 = smbcli_open(cli->tree, fname, O_CREAT|O_RDWR, DENY_NONE);
1941         torture_assert(tctx, fnum2 != -1, smbcli_errstr(cli->tree));
1942         smbcli_close(cli->tree, fnum2);
1943
1944         status = smb_raw_changenotify_recv(req, tctx, &notify);
1945         torture_assert_ntstatus_ok(tctx, status, "smb_raw_changenotify_recv");
1946
1947         /* create 4 files that will cause CHANGE_NOTIFY_INFO structures
1948          * to be returned in the same packet with all possible 4-byte padding
1949          * permutations.  As per MS-CIFS 2.2.7.4.2 these structures should be
1950          * 4-byte aligned. */
1951
1952         for (i = 0; i < num_names; i++) {
1953                 fpath = talloc_asprintf(tctx, "%s\\%s", BASEDIR, fnames[i]);
1954                 fnum2 = smbcli_open(cli->tree, fpath,
1955                     O_CREAT|O_RDWR, DENY_NONE);
1956                 torture_assert(tctx, fnum2 != -1, smbcli_errstr(cli->tree));
1957                 smbcli_close(cli->tree, fnum2);
1958                 talloc_free(fpath);
1959         }
1960
1961         /* We send a notify packet, and let smb_raw_changenotify_recv() do
1962          * the alignment checking for us. */
1963         req = smb_raw_changenotify_send(cli->tree, &notify);
1964         status = smb_raw_changenotify_recv(req, tctx, &notify);
1965         torture_assert_ntstatus_ok(tctx, status, "smb_raw_changenotify_recv");
1966
1967         /* Do basic checking for correctness. */
1968         torture_assert(tctx, notify.nttrans.out.num_changes == num_names, "");
1969         for (i = 0; i < num_names; i++) {
1970                 torture_assert(tctx, notify.nttrans.out.changes[i].action ==
1971                     NOTIFY_ACTION_ADDED, "");
1972                 CHECK_WSTR(tctx, notify.nttrans.out.changes[i].name, fnames[i],
1973                     STR_UNICODE);
1974         }
1975
1976         smb_raw_exit(cli->session);
1977         smbcli_deltree(cli->tree, BASEDIR);
1978         return true;
1979 }
1980
1981 struct torture_suite *torture_raw_notify(TALLOC_CTX *mem_ctx)
1982 {
1983         struct torture_suite *suite = torture_suite_create(mem_ctx, "notify");
1984
1985         torture_suite_add_1smb_test(suite, "tcon", test_notify_tcon);
1986         torture_suite_add_2smb_test(suite, "dir", test_notify_dir);
1987         torture_suite_add_2smb_test(suite, "mask", test_notify_mask);
1988         torture_suite_add_2smb_test(suite, "recursive", test_notify_recursive);
1989         torture_suite_add_1smb_test(suite, "mask_change",
1990                                     test_notify_mask_change);
1991         torture_suite_add_1smb_test(suite, "file", test_notify_file);
1992         torture_suite_add_1smb_test(suite, "tdis", test_notify_tdis);
1993         torture_suite_add_1smb_test(suite, "exit", test_notify_exit);
1994         torture_suite_add_1smb_test(suite, "ulogoff", test_notify_ulogoff);
1995         torture_suite_add_1smb_test(suite, "tcp_dis", test_notify_tcp_dis);
1996         torture_suite_add_1smb_test(suite, "double", test_notify_double);
1997         torture_suite_add_2smb_test(suite, "tree", test_notify_tree);
1998         torture_suite_add_1smb_test(suite, "overflow", test_notify_overflow);
1999         torture_suite_add_1smb_test(suite, "basedir", test_notify_basedir);
2000         torture_suite_add_1smb_test(suite, "alignment", test_notify_alignment);
2001
2002         return suite;
2003 }