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