2 Unix SMB/CIFS implementation.
3 basic raw test suite for change notify
4 Copyright (C) Andrew Tridgell 2003
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 2 of the License, or
9 (at your option) any later version.
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.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 #include "torture/torture.h"
23 #include "libcli/raw/libcliraw.h"
24 #include "libcli/libcli.h"
25 #include "system/filesys.h"
26 #include "torture/util.h"
28 #define BASEDIR "\\test_notify"
30 #define CHECK_STATUS(status, correct) do { \
31 if (!NT_STATUS_EQUAL(status, correct)) { \
32 printf("(%d) Incorrect status %s - should be %s\n", \
33 __LINE__, nt_errstr(status), nt_errstr(correct)); \
39 #define CHECK_VAL(v, correct) do { \
40 if ((v) != (correct)) { \
41 printf("(%d) wrong value for %s 0x%x should be 0x%x\n", \
42 __LINE__, #v, (int)v, (int)correct); \
47 #define CHECK_WSTR(field, value, flags) do { \
48 if (!field.s || strcmp(field.s, value) || wire_bad_flags(&field, flags, cli->transport)) { \
49 printf("(%d) %s [%s] != %s\n", __LINE__, #field, field.s, value); \
56 basic testing of change notify on directories
58 static BOOL test_notify_dir(struct smbcli_state *cli, struct smbcli_state *cli2,
63 union smb_notify notify;
66 int i, count, fnum, fnum2;
67 struct smbcli_request *req, *req2;
68 extern int torture_numops;
70 printf("TESTING CHANGE NOTIFY ON DIRECTRIES\n");
73 get a handle on the directory
75 io.generic.level = RAW_OPEN_NTCREATEX;
76 io.ntcreatex.in.root_fid = 0;
77 io.ntcreatex.in.flags = 0;
78 io.ntcreatex.in.access_mask = SEC_FILE_ALL;
79 io.ntcreatex.in.create_options = NTCREATEX_OPTIONS_DIRECTORY;
80 io.ntcreatex.in.file_attr = FILE_ATTRIBUTE_NORMAL;
81 io.ntcreatex.in.share_access = NTCREATEX_SHARE_ACCESS_READ | NTCREATEX_SHARE_ACCESS_WRITE;
82 io.ntcreatex.in.alloc_size = 0;
83 io.ntcreatex.in.open_disposition = NTCREATEX_DISP_OPEN;
84 io.ntcreatex.in.impersonation = NTCREATEX_IMPERSONATION_ANONYMOUS;
85 io.ntcreatex.in.security_flags = 0;
86 io.ntcreatex.in.fname = BASEDIR;
88 status = smb_raw_open(cli->tree, mem_ctx, &io);
89 CHECK_STATUS(status, NT_STATUS_OK);
90 fnum = io.ntcreatex.out.file.fnum;
92 status = smb_raw_open(cli->tree, mem_ctx, &io);
93 CHECK_STATUS(status, NT_STATUS_OK);
94 fnum2 = io.ntcreatex.out.file.fnum;
96 /* ask for a change notify,
97 on file or directory name changes */
98 notify.nttrans.level = RAW_NOTIFY_NTTRANS;
99 notify.nttrans.in.buffer_size = 1000;
100 notify.nttrans.in.completion_filter = FILE_NOTIFY_CHANGE_NAME;
101 notify.nttrans.in.file.fnum = fnum;
102 notify.nttrans.in.recursive = True;
104 printf("testing notify cancel\n");
106 req = smb_raw_changenotify_send(cli->tree, ¬ify);
107 smb_raw_ntcancel(req);
108 status = smb_raw_changenotify_recv(req, mem_ctx, ¬ify);
109 CHECK_STATUS(status, NT_STATUS_CANCELLED);
111 printf("testing notify mkdir\n");
113 req = smb_raw_changenotify_send(cli->tree, ¬ify);
114 smbcli_mkdir(cli2->tree, BASEDIR "\\subdir-name");
116 status = smb_raw_changenotify_recv(req, mem_ctx, ¬ify);
117 CHECK_STATUS(status, NT_STATUS_OK);
119 CHECK_VAL(notify.nttrans.out.num_changes, 1);
120 CHECK_VAL(notify.nttrans.out.changes[0].action, NOTIFY_ACTION_ADDED);
121 CHECK_WSTR(notify.nttrans.out.changes[0].name, "subdir-name", STR_UNICODE);
123 printf("testing notify rmdir\n");
125 req = smb_raw_changenotify_send(cli->tree, ¬ify);
126 smbcli_rmdir(cli2->tree, BASEDIR "\\subdir-name");
128 status = smb_raw_changenotify_recv(req, mem_ctx, ¬ify);
129 CHECK_STATUS(status, NT_STATUS_OK);
130 CHECK_VAL(notify.nttrans.out.num_changes, 1);
131 CHECK_VAL(notify.nttrans.out.changes[0].action, NOTIFY_ACTION_REMOVED);
132 CHECK_WSTR(notify.nttrans.out.changes[0].name, "subdir-name", STR_UNICODE);
134 printf("testing notify mkdir - rmdir - mkdir - rmdir\n");
136 smbcli_mkdir(cli2->tree, BASEDIR "\\subdir-name");
137 smbcli_rmdir(cli2->tree, BASEDIR "\\subdir-name");
138 smbcli_mkdir(cli2->tree, BASEDIR "\\subdir-name");
139 smbcli_rmdir(cli2->tree, BASEDIR "\\subdir-name");
141 req = smb_raw_changenotify_send(cli->tree, ¬ify);
142 status = smb_raw_changenotify_recv(req, mem_ctx, ¬ify);
143 CHECK_STATUS(status, NT_STATUS_OK);
144 CHECK_VAL(notify.nttrans.out.num_changes, 4);
145 CHECK_VAL(notify.nttrans.out.changes[0].action, NOTIFY_ACTION_ADDED);
146 CHECK_WSTR(notify.nttrans.out.changes[0].name, "subdir-name", STR_UNICODE);
147 CHECK_VAL(notify.nttrans.out.changes[1].action, NOTIFY_ACTION_REMOVED);
148 CHECK_WSTR(notify.nttrans.out.changes[1].name, "subdir-name", STR_UNICODE);
149 CHECK_VAL(notify.nttrans.out.changes[2].action, NOTIFY_ACTION_ADDED);
150 CHECK_WSTR(notify.nttrans.out.changes[2].name, "subdir-name", STR_UNICODE);
151 CHECK_VAL(notify.nttrans.out.changes[3].action, NOTIFY_ACTION_REMOVED);
152 CHECK_WSTR(notify.nttrans.out.changes[3].name, "subdir-name", STR_UNICODE);
154 count = torture_numops;
155 printf("testing buffered notify on create of %d files\n", count);
156 for (i=0;i<count;i++) {
157 char *fname = talloc_asprintf(cli, BASEDIR "\\test%d.txt", i);
158 int fnum3 = smbcli_open(cli->tree, fname, O_CREAT|O_RDWR, DENY_NONE);
160 printf("Failed to create %s - %s\n",
161 fname, smbcli_errstr(cli->tree));
166 smbcli_close(cli->tree, fnum3);
169 /* (1st notify) setup a new notify on a different directory handle.
170 This new notify won't see the events above. */
171 notify.nttrans.in.file.fnum = fnum2;
172 req2 = smb_raw_changenotify_send(cli->tree, ¬ify);
174 /* (2nd notify) whereas this notify will see the above buffered events,
175 and it directly returns the buffered events */
176 notify.nttrans.in.file.fnum = fnum;
177 req = smb_raw_changenotify_send(cli->tree, ¬ify);
179 status = smbcli_unlink(cli->tree, BASEDIR "\\nonexistant.txt");
180 CHECK_STATUS(status, NT_STATUS_OBJECT_NAME_NOT_FOUND);
182 /* (1st unlink) as the 2nd notify directly returns,
183 this unlink is only seen by the 1st notify and
184 the 3rd notify (later) */
185 printf("testing notify on unlink for the first file\n");
186 status = smbcli_unlink(cli2->tree, BASEDIR "\\test0.txt");
187 CHECK_STATUS(status, NT_STATUS_OK);
189 /* receive the reply from the 2nd notify */
190 status = smb_raw_changenotify_recv(req, mem_ctx, ¬ify);
191 CHECK_STATUS(status, NT_STATUS_OK);
193 CHECK_VAL(notify.nttrans.out.num_changes, count);
194 for (i=1;i<count;i++) {
195 CHECK_VAL(notify.nttrans.out.changes[i].action, NOTIFY_ACTION_ADDED);
197 CHECK_WSTR(notify.nttrans.out.changes[0].name, "test0.txt", STR_UNICODE);
199 printf("and now from the 1st notify\n");
200 status = smb_raw_changenotify_recv(req2, mem_ctx, ¬ify);
201 CHECK_STATUS(status, NT_STATUS_OK);
202 CHECK_VAL(notify.nttrans.out.num_changes, 1);
203 CHECK_VAL(notify.nttrans.out.changes[0].action, NOTIFY_ACTION_REMOVED);
204 CHECK_WSTR(notify.nttrans.out.changes[0].name, "test0.txt", STR_UNICODE);
206 printf("(3rd notify) this notify will only see the 1st unlink\n");
207 req = smb_raw_changenotify_send(cli->tree, ¬ify);
209 status = smbcli_unlink(cli->tree, BASEDIR "\\nonexistant.txt");
210 CHECK_STATUS(status, NT_STATUS_OBJECT_NAME_NOT_FOUND);
212 printf("testing notify on wildcard unlink for %d files\n", count-1);
213 /* (2nd unlink) do a wildcard unlink */
214 status = smbcli_unlink(cli2->tree, BASEDIR "\\test*.txt");
215 CHECK_STATUS(status, NT_STATUS_OK);
217 /* receive the 3rd notify */
218 status = smb_raw_changenotify_recv(req, mem_ctx, ¬ify);
219 CHECK_STATUS(status, NT_STATUS_OK);
220 CHECK_VAL(notify.nttrans.out.num_changes, 1);
221 CHECK_VAL(notify.nttrans.out.changes[0].action, NOTIFY_ACTION_REMOVED);
222 CHECK_WSTR(notify.nttrans.out.changes[0].name, "test0.txt", STR_UNICODE);
224 /* and we now see the rest of the unlink calls on both directory handles */
225 notify.nttrans.in.file.fnum = fnum;
227 req = smb_raw_changenotify_send(cli->tree, ¬ify);
228 status = smb_raw_changenotify_recv(req, mem_ctx, ¬ify);
229 CHECK_STATUS(status, NT_STATUS_OK);
230 CHECK_VAL(notify.nttrans.out.num_changes, count-1);
231 for (i=0;i<notify.nttrans.out.num_changes;i++) {
232 CHECK_VAL(notify.nttrans.out.changes[i].action, NOTIFY_ACTION_REMOVED);
234 notify.nttrans.in.file.fnum = fnum2;
235 req = smb_raw_changenotify_send(cli->tree, ¬ify);
236 status = smb_raw_changenotify_recv(req, mem_ctx, ¬ify);
237 CHECK_STATUS(status, NT_STATUS_OK);
238 CHECK_VAL(notify.nttrans.out.num_changes, count-1);
239 for (i=0;i<notify.nttrans.out.num_changes;i++) {
240 CHECK_VAL(notify.nttrans.out.changes[i].action, NOTIFY_ACTION_REMOVED);
243 printf("testing if a close() on the dir handle triggers the notify reply\n");
245 notify.nttrans.in.file.fnum = fnum;
246 req = smb_raw_changenotify_send(cli->tree, ¬ify);
248 cl.close.level = RAW_CLOSE_CLOSE;
249 cl.close.in.file.fnum = fnum;
250 cl.close.in.write_time = 0;
251 status = smb_raw_close(cli->tree, &cl);
252 CHECK_STATUS(status, NT_STATUS_OK);
254 status = smb_raw_changenotify_recv(req, mem_ctx, ¬ify);
255 CHECK_STATUS(status, NT_STATUS_OK);
256 CHECK_VAL(notify.nttrans.out.num_changes, 0);
259 smb_raw_exit(cli->session);
264 * Check notify reply for a rename action. Not sure if this is a valid thing
265 * to do, but depending on timing between inotify and messaging we get the
266 * add/remove/modify in any order. This routines tries to find the action/name
267 * pair in any of the three following notify_changes.
270 static BOOL check_rename_reply(struct smbcli_state *cli,
272 struct notify_changes *actions,
273 uint32_t action, const char *name)
277 for (i=0; i<3; i++) {
278 if (actions[i].action == action) {
279 if ((actions[i].name.s == NULL)
280 || (strcmp(actions[i].name.s, name) != 0)
281 || (wire_bad_flags(&actions[i].name, STR_UNICODE,
283 printf("(%d) name [%s] != %s\n", line,
284 actions[i].name.s, name);
291 printf("(%d) expected action %d, not found\n", line, action);
296 testing of recursive change notify
298 static BOOL test_notify_recursive(struct smbcli_state *cli, TALLOC_CTX *mem_ctx)
302 union smb_notify notify;
305 struct smbcli_request *req1, *req2;
307 printf("TESTING CHANGE NOTIFY WITH RECURSION\n");
310 get a handle on the directory
312 io.generic.level = RAW_OPEN_NTCREATEX;
313 io.ntcreatex.in.root_fid = 0;
314 io.ntcreatex.in.flags = 0;
315 io.ntcreatex.in.access_mask = SEC_FILE_ALL;
316 io.ntcreatex.in.create_options = NTCREATEX_OPTIONS_DIRECTORY;
317 io.ntcreatex.in.file_attr = FILE_ATTRIBUTE_NORMAL;
318 io.ntcreatex.in.share_access = NTCREATEX_SHARE_ACCESS_READ | NTCREATEX_SHARE_ACCESS_WRITE;
319 io.ntcreatex.in.alloc_size = 0;
320 io.ntcreatex.in.open_disposition = NTCREATEX_DISP_OPEN;
321 io.ntcreatex.in.impersonation = NTCREATEX_IMPERSONATION_ANONYMOUS;
322 io.ntcreatex.in.security_flags = 0;
323 io.ntcreatex.in.fname = BASEDIR;
325 status = smb_raw_open(cli->tree, mem_ctx, &io);
326 CHECK_STATUS(status, NT_STATUS_OK);
327 fnum = io.ntcreatex.out.file.fnum;
329 /* ask for a change notify, on file or directory name
330 changes. Setup both with and without recursion */
331 notify.nttrans.level = RAW_NOTIFY_NTTRANS;
332 notify.nttrans.in.buffer_size = 1000;
333 notify.nttrans.in.completion_filter = FILE_NOTIFY_CHANGE_NAME | FILE_NOTIFY_CHANGE_ATTRIBUTES | FILE_NOTIFY_CHANGE_CREATION;
334 notify.nttrans.in.file.fnum = fnum;
336 notify.nttrans.in.recursive = True;
337 req1 = smb_raw_changenotify_send(cli->tree, ¬ify);
339 notify.nttrans.in.recursive = False;
340 req2 = smb_raw_changenotify_send(cli->tree, ¬ify);
342 /* cancel initial requests so the buffer is setup */
343 smb_raw_ntcancel(req1);
344 status = smb_raw_changenotify_recv(req1, mem_ctx, ¬ify);
345 CHECK_STATUS(status, NT_STATUS_CANCELLED);
347 smb_raw_ntcancel(req2);
348 status = smb_raw_changenotify_recv(req2, mem_ctx, ¬ify);
349 CHECK_STATUS(status, NT_STATUS_CANCELLED);
351 smbcli_mkdir(cli->tree, BASEDIR "\\subdir-name");
352 smbcli_mkdir(cli->tree, BASEDIR "\\subdir-name\\subname1");
353 smbcli_close(cli->tree,
354 smbcli_open(cli->tree, BASEDIR "\\subdir-name\\subname2", O_CREAT, 0));
355 smbcli_rename(cli->tree, BASEDIR "\\subdir-name\\subname1", BASEDIR "\\subdir-name\\subname1-r");
356 smbcli_rename(cli->tree, BASEDIR "\\subdir-name\\subname2", BASEDIR "\\subname2-r");
357 smbcli_rename(cli->tree, BASEDIR "\\subname2-r", BASEDIR "\\subname3-r");
359 notify.nttrans.in.completion_filter = 0;
360 notify.nttrans.in.recursive = True;
362 req1 = smb_raw_changenotify_send(cli->tree, ¬ify);
364 smbcli_rmdir(cli->tree, BASEDIR "\\subdir-name\\subname1-r");
365 smbcli_rmdir(cli->tree, BASEDIR "\\subdir-name");
366 smbcli_unlink(cli->tree, BASEDIR "\\subname3-r");
368 notify.nttrans.in.recursive = False;
369 req2 = smb_raw_changenotify_send(cli->tree, ¬ify);
371 status = smb_raw_changenotify_recv(req1, mem_ctx, ¬ify);
372 CHECK_STATUS(status, NT_STATUS_OK);
374 CHECK_VAL(notify.nttrans.out.num_changes, 11);
375 CHECK_VAL(notify.nttrans.out.changes[0].action, NOTIFY_ACTION_ADDED);
376 CHECK_WSTR(notify.nttrans.out.changes[0].name, "subdir-name", STR_UNICODE);
377 CHECK_VAL(notify.nttrans.out.changes[1].action, NOTIFY_ACTION_ADDED);
378 CHECK_WSTR(notify.nttrans.out.changes[1].name, "subdir-name\\subname1", STR_UNICODE);
379 CHECK_VAL(notify.nttrans.out.changes[2].action, NOTIFY_ACTION_ADDED);
380 CHECK_WSTR(notify.nttrans.out.changes[2].name, "subdir-name\\subname2", STR_UNICODE);
381 CHECK_VAL(notify.nttrans.out.changes[3].action, NOTIFY_ACTION_OLD_NAME);
382 CHECK_WSTR(notify.nttrans.out.changes[3].name, "subdir-name\\subname1", STR_UNICODE);
383 CHECK_VAL(notify.nttrans.out.changes[4].action, NOTIFY_ACTION_NEW_NAME);
384 CHECK_WSTR(notify.nttrans.out.changes[4].name, "subdir-name\\subname1-r", STR_UNICODE);
386 ret &= check_rename_reply(
387 cli, __LINE__, ¬ify.nttrans.out.changes[5],
388 NOTIFY_ACTION_ADDED, "subname2-r");
389 ret &= check_rename_reply(
390 cli, __LINE__, ¬ify.nttrans.out.changes[5],
391 NOTIFY_ACTION_REMOVED, "subdir-name\\subname2");
392 ret &= check_rename_reply(
393 cli, __LINE__, ¬ify.nttrans.out.changes[5],
394 NOTIFY_ACTION_MODIFIED, "subname2-r");
396 ret &= check_rename_reply(
397 cli, __LINE__, ¬ify.nttrans.out.changes[8],
398 NOTIFY_ACTION_OLD_NAME, "subname2-r");
399 ret &= check_rename_reply(
400 cli, __LINE__, ¬ify.nttrans.out.changes[8],
401 NOTIFY_ACTION_NEW_NAME, "subname3-r");
402 ret &= check_rename_reply(
403 cli, __LINE__, ¬ify.nttrans.out.changes[8],
404 NOTIFY_ACTION_MODIFIED, "subname3-r");
410 status = smb_raw_changenotify_recv(req2, mem_ctx, ¬ify);
411 CHECK_STATUS(status, NT_STATUS_OK);
413 CHECK_VAL(notify.nttrans.out.num_changes, 3);
414 CHECK_VAL(notify.nttrans.out.changes[0].action, NOTIFY_ACTION_REMOVED);
415 CHECK_WSTR(notify.nttrans.out.changes[0].name, "subdir-name\\subname1-r", STR_UNICODE);
416 CHECK_VAL(notify.nttrans.out.changes[1].action, NOTIFY_ACTION_REMOVED);
417 CHECK_WSTR(notify.nttrans.out.changes[1].name, "subdir-name", STR_UNICODE);
418 CHECK_VAL(notify.nttrans.out.changes[2].action, NOTIFY_ACTION_REMOVED);
419 CHECK_WSTR(notify.nttrans.out.changes[2].name, "subname3-r", STR_UNICODE);
422 smb_raw_exit(cli->session);
427 testing of change notify mask change
429 static BOOL test_notify_mask_change(struct smbcli_state *cli, TALLOC_CTX *mem_ctx)
433 union smb_notify notify;
436 struct smbcli_request *req1, *req2;
437 union smb_setfileinfo sfinfo;
439 printf("TESTING CHANGE NOTIFY WITH MASK CHANGE\n");
442 get a handle on the directory
444 io.generic.level = RAW_OPEN_NTCREATEX;
445 io.ntcreatex.in.root_fid = 0;
446 io.ntcreatex.in.flags = 0;
447 io.ntcreatex.in.access_mask = SEC_FILE_ALL;
448 io.ntcreatex.in.create_options = NTCREATEX_OPTIONS_DIRECTORY;
449 io.ntcreatex.in.file_attr = FILE_ATTRIBUTE_NORMAL;
450 io.ntcreatex.in.share_access = NTCREATEX_SHARE_ACCESS_READ | NTCREATEX_SHARE_ACCESS_WRITE;
451 io.ntcreatex.in.alloc_size = 0;
452 io.ntcreatex.in.open_disposition = NTCREATEX_DISP_OPEN;
453 io.ntcreatex.in.impersonation = NTCREATEX_IMPERSONATION_ANONYMOUS;
454 io.ntcreatex.in.security_flags = 0;
455 io.ntcreatex.in.fname = BASEDIR;
457 status = smb_raw_open(cli->tree, mem_ctx, &io);
458 CHECK_STATUS(status, NT_STATUS_OK);
459 fnum = io.ntcreatex.out.file.fnum;
461 /* ask for a change notify, on file or directory name
462 changes. Setup both with and without recursion */
463 notify.nttrans.level = RAW_NOTIFY_NTTRANS;
464 notify.nttrans.in.buffer_size = 1000;
465 notify.nttrans.in.completion_filter = FILE_NOTIFY_CHANGE_ATTRIBUTES;
466 notify.nttrans.in.file.fnum = fnum;
468 notify.nttrans.in.recursive = True;
469 req1 = smb_raw_changenotify_send(cli->tree, ¬ify);
471 notify.nttrans.in.recursive = False;
472 req2 = smb_raw_changenotify_send(cli->tree, ¬ify);
474 /* cancel initial requests so the buffer is setup */
475 smb_raw_ntcancel(req1);
476 status = smb_raw_changenotify_recv(req1, mem_ctx, ¬ify);
477 CHECK_STATUS(status, NT_STATUS_CANCELLED);
479 smb_raw_ntcancel(req2);
480 status = smb_raw_changenotify_recv(req2, mem_ctx, ¬ify);
481 CHECK_STATUS(status, NT_STATUS_CANCELLED);
483 notify.nttrans.in.recursive = True;
484 req1 = smb_raw_changenotify_send(cli->tree, ¬ify);
486 /* Set to hidden then back again. */
487 smbcli_close(cli->tree, smbcli_open(cli->tree, BASEDIR "\\tname1", O_CREAT, 0));
488 smbcli_setatr(cli->tree, BASEDIR "\\tname1", FILE_ATTRIBUTE_HIDDEN, 0);
489 smbcli_unlink(cli->tree, BASEDIR "\\tname1");
491 status = smb_raw_changenotify_recv(req1, mem_ctx, ¬ify);
492 CHECK_STATUS(status, NT_STATUS_OK);
494 CHECK_VAL(notify.nttrans.out.num_changes, 1);
495 CHECK_VAL(notify.nttrans.out.changes[0].action, NOTIFY_ACTION_MODIFIED);
496 CHECK_WSTR(notify.nttrans.out.changes[0].name, "tname1", STR_UNICODE);
498 /* Now try and change the mask to include other events.
499 * This should not work - once the mask is set on a directory
500 * fnum it seems to be fixed until the fnum is closed. */
502 notify.nttrans.in.completion_filter = FILE_NOTIFY_CHANGE_NAME | FILE_NOTIFY_CHANGE_ATTRIBUTES | FILE_NOTIFY_CHANGE_CREATION;
503 notify.nttrans.in.recursive = True;
504 req1 = smb_raw_changenotify_send(cli->tree, ¬ify);
506 notify.nttrans.in.recursive = False;
507 req2 = smb_raw_changenotify_send(cli->tree, ¬ify);
509 smbcli_mkdir(cli->tree, BASEDIR "\\subdir-name");
510 smbcli_mkdir(cli->tree, BASEDIR "\\subdir-name\\subname1");
511 smbcli_close(cli->tree,
512 smbcli_open(cli->tree, BASEDIR "\\subdir-name\\subname2", O_CREAT, 0));
513 smbcli_rename(cli->tree, BASEDIR "\\subdir-name\\subname1", BASEDIR "\\subdir-name\\subname1-r");
514 smbcli_rename(cli->tree, BASEDIR "\\subdir-name\\subname2", BASEDIR "\\subname2-r");
515 smbcli_rename(cli->tree, BASEDIR "\\subname2-r", BASEDIR "\\subname3-r");
517 smbcli_rmdir(cli->tree, BASEDIR "\\subdir-name\\subname1-r");
518 smbcli_rmdir(cli->tree, BASEDIR "\\subdir-name");
519 smbcli_unlink(cli->tree, BASEDIR "\\subname3-r");
521 status = smb_raw_changenotify_recv(req1, mem_ctx, ¬ify);
522 CHECK_STATUS(status, NT_STATUS_OK);
524 CHECK_VAL(notify.nttrans.out.num_changes, 1);
525 CHECK_VAL(notify.nttrans.out.changes[0].action, NOTIFY_ACTION_MODIFIED);
526 CHECK_WSTR(notify.nttrans.out.changes[0].name, "subname2-r", STR_UNICODE);
528 status = smb_raw_changenotify_recv(req2, mem_ctx, ¬ify);
529 CHECK_STATUS(status, NT_STATUS_OK);
531 CHECK_VAL(notify.nttrans.out.num_changes, 1);
532 CHECK_VAL(notify.nttrans.out.changes[0].action, NOTIFY_ACTION_MODIFIED);
533 CHECK_WSTR(notify.nttrans.out.changes[0].name, "subname3-r", STR_UNICODE);
540 smb_raw_exit(cli->session);
546 testing of mask bits for change notify
548 static BOOL test_notify_mask(struct smbcli_state *cli, TALLOC_CTX *mem_ctx)
552 union smb_notify notify;
561 printf("TESTING CHANGE NOTIFY COMPLETION FILTERS\n");
563 tv = timeval_current_ofs(1000, 0);
564 t = timeval_to_nttime(&tv);
567 get a handle on the directory
569 io.generic.level = RAW_OPEN_NTCREATEX;
570 io.ntcreatex.in.root_fid = 0;
571 io.ntcreatex.in.flags = 0;
572 io.ntcreatex.in.access_mask = SEC_FILE_ALL;
573 io.ntcreatex.in.create_options = NTCREATEX_OPTIONS_DIRECTORY;
574 io.ntcreatex.in.file_attr = FILE_ATTRIBUTE_NORMAL;
575 io.ntcreatex.in.share_access = NTCREATEX_SHARE_ACCESS_READ | NTCREATEX_SHARE_ACCESS_WRITE;
576 io.ntcreatex.in.alloc_size = 0;
577 io.ntcreatex.in.open_disposition = NTCREATEX_DISP_OPEN;
578 io.ntcreatex.in.impersonation = NTCREATEX_IMPERSONATION_ANONYMOUS;
579 io.ntcreatex.in.security_flags = 0;
580 io.ntcreatex.in.fname = BASEDIR;
582 notify.nttrans.level = RAW_NOTIFY_NTTRANS;
583 notify.nttrans.in.buffer_size = 1000;
584 notify.nttrans.in.recursive = True;
586 #define NOTIFY_MASK_TEST(setup, op, cleanup, Action, expected, nchanges) \
587 do { for (mask=i=0;i<32;i++) { \
588 struct smbcli_request *req; \
589 status = smb_raw_open(cli->tree, mem_ctx, &io); \
590 CHECK_STATUS(status, NT_STATUS_OK); \
591 fnum = io.ntcreatex.out.file.fnum; \
593 notify.nttrans.in.file.fnum = fnum; \
594 notify.nttrans.in.completion_filter = (1<<i); \
595 req = smb_raw_changenotify_send(cli->tree, ¬ify); \
597 msleep(200); smb_raw_ntcancel(req); \
598 status = smb_raw_changenotify_recv(req, mem_ctx, ¬ify); \
600 smbcli_close(cli->tree, fnum); \
601 if (NT_STATUS_EQUAL(status, NT_STATUS_CANCELLED)) continue; \
602 CHECK_STATUS(status, NT_STATUS_OK); \
603 /* special case to cope with file rename behaviour */ \
604 if (nchanges == 2 && notify.nttrans.out.num_changes == 1 && \
605 notify.nttrans.out.changes[0].action == NOTIFY_ACTION_MODIFIED && \
606 ((expected) & FILE_NOTIFY_CHANGE_ATTRIBUTES) && \
607 Action == NOTIFY_ACTION_OLD_NAME) { \
608 printf("(rename file special handling OK)\n"); \
609 } else if (nchanges != notify.nttrans.out.num_changes) { \
610 printf("ERROR: nchanges=%d expected=%d action=%d filter=0x%08x\n", \
611 notify.nttrans.out.num_changes, \
613 notify.nttrans.out.changes[0].action, \
614 notify.nttrans.in.completion_filter); \
616 } else if (notify.nttrans.out.changes[0].action != Action) { \
617 printf("ERROR: nchanges=%d action=%d expectedAction=%d filter=0x%08x\n", \
618 notify.nttrans.out.num_changes, \
619 notify.nttrans.out.changes[0].action, \
621 notify.nttrans.in.completion_filter); \
623 } else if (strcmp(notify.nttrans.out.changes[0].name.s, "tname1") != 0) { \
624 printf("ERROR: nchanges=%d action=%d filter=0x%08x name=%s\n", \
625 notify.nttrans.out.num_changes, \
626 notify.nttrans.out.changes[0].action, \
627 notify.nttrans.in.completion_filter, \
628 notify.nttrans.out.changes[0].name.s); \
633 if ((expected) != mask) { \
634 if (((expected) & ~mask) != 0) { \
635 printf("ERROR: trigger on too few bits. mask=0x%08x expected=0x%08x\n", \
639 printf("WARNING: trigger on too many bits. mask=0x%08x expected=0x%08x\n", \
645 printf("testing mkdir\n");
647 smbcli_mkdir(cli->tree, BASEDIR "\\tname1");,
648 smbcli_rmdir(cli->tree, BASEDIR "\\tname1");,
650 FILE_NOTIFY_CHANGE_DIR_NAME, 1);
652 printf("testing create file\n");
654 smbcli_close(cli->tree, smbcli_open(cli->tree, BASEDIR "\\tname1", O_CREAT, 0));,
655 smbcli_unlink(cli->tree, BASEDIR "\\tname1");,
657 FILE_NOTIFY_CHANGE_FILE_NAME, 1);
659 printf("testing unlink\n");
661 smbcli_close(cli->tree, smbcli_open(cli->tree, BASEDIR "\\tname1", O_CREAT, 0));,
662 smbcli_unlink(cli->tree, BASEDIR "\\tname1");,
664 NOTIFY_ACTION_REMOVED,
665 FILE_NOTIFY_CHANGE_FILE_NAME, 1);
667 printf("testing rmdir\n");
669 smbcli_mkdir(cli->tree, BASEDIR "\\tname1");,
670 smbcli_rmdir(cli->tree, BASEDIR "\\tname1");,
672 NOTIFY_ACTION_REMOVED,
673 FILE_NOTIFY_CHANGE_DIR_NAME, 1);
675 printf("testing rename file\n");
677 smbcli_close(cli->tree, smbcli_open(cli->tree, BASEDIR "\\tname1", O_CREAT, 0));,
678 smbcli_rename(cli->tree, BASEDIR "\\tname1", BASEDIR "\\tname2");,
679 smbcli_unlink(cli->tree, BASEDIR "\\tname2");,
680 NOTIFY_ACTION_OLD_NAME,
681 FILE_NOTIFY_CHANGE_FILE_NAME|FILE_NOTIFY_CHANGE_ATTRIBUTES|FILE_NOTIFY_CHANGE_CREATION, 2);
683 printf("testing rename dir\n");
685 smbcli_mkdir(cli->tree, BASEDIR "\\tname1");,
686 smbcli_rename(cli->tree, BASEDIR "\\tname1", BASEDIR "\\tname2");,
687 smbcli_rmdir(cli->tree, BASEDIR "\\tname2");,
688 NOTIFY_ACTION_OLD_NAME,
689 FILE_NOTIFY_CHANGE_DIR_NAME, 2);
691 printf("testing set path attribute\n");
693 smbcli_close(cli->tree, smbcli_open(cli->tree, BASEDIR "\\tname1", O_CREAT, 0));,
694 smbcli_setatr(cli->tree, BASEDIR "\\tname1", FILE_ATTRIBUTE_HIDDEN, 0);,
695 smbcli_unlink(cli->tree, BASEDIR "\\tname1");,
696 NOTIFY_ACTION_MODIFIED,
697 FILE_NOTIFY_CHANGE_ATTRIBUTES, 1);
699 printf("testing set path write time\n");
701 smbcli_close(cli->tree, smbcli_open(cli->tree, BASEDIR "\\tname1", O_CREAT, 0));,
702 smbcli_setatr(cli->tree, BASEDIR "\\tname1", FILE_ATTRIBUTE_NORMAL, 1000);,
703 smbcli_unlink(cli->tree, BASEDIR "\\tname1");,
704 NOTIFY_ACTION_MODIFIED,
705 FILE_NOTIFY_CHANGE_LAST_WRITE, 1);
707 printf("testing set file attribute\n");
709 fnum2 = create_complex_file(cli, mem_ctx, BASEDIR "\\tname1");,
710 smbcli_fsetatr(cli->tree, fnum2, FILE_ATTRIBUTE_HIDDEN, 0, 0, 0, 0);,
711 (smbcli_close(cli->tree, fnum2), smbcli_unlink(cli->tree, BASEDIR "\\tname1"));,
712 NOTIFY_ACTION_MODIFIED,
713 FILE_NOTIFY_CHANGE_ATTRIBUTES, 1);
715 if (lp_parm_bool(-1, "torture", "samba3", False)) {
716 printf("Samba3 does not yet support create times "
720 printf("testing set file create time\n");
722 fnum2 = create_complex_file(cli, mem_ctx,
723 BASEDIR "\\tname1");,
724 smbcli_fsetatr(cli->tree, fnum2, 0, t, 0, 0, 0);,
725 (smbcli_close(cli->tree, fnum2),
726 smbcli_unlink(cli->tree, BASEDIR "\\tname1"));,
727 NOTIFY_ACTION_MODIFIED,
728 FILE_NOTIFY_CHANGE_CREATION, 1);
731 printf("testing set file access time\n");
733 fnum2 = create_complex_file(cli, mem_ctx, BASEDIR "\\tname1");,
734 smbcli_fsetatr(cli->tree, fnum2, 0, 0, t, 0, 0);,
735 (smbcli_close(cli->tree, fnum2), smbcli_unlink(cli->tree, BASEDIR "\\tname1"));,
736 NOTIFY_ACTION_MODIFIED,
737 FILE_NOTIFY_CHANGE_LAST_ACCESS, 1);
739 printf("testing set file write time\n");
741 fnum2 = create_complex_file(cli, mem_ctx, BASEDIR "\\tname1");,
742 smbcli_fsetatr(cli->tree, fnum2, 0, 0, 0, t, 0);,
743 (smbcli_close(cli->tree, fnum2), smbcli_unlink(cli->tree, BASEDIR "\\tname1"));,
744 NOTIFY_ACTION_MODIFIED,
745 FILE_NOTIFY_CHANGE_LAST_WRITE, 1);
747 printf("testing set file change time\n");
749 fnum2 = create_complex_file(cli, mem_ctx, BASEDIR "\\tname1");,
750 smbcli_fsetatr(cli->tree, fnum2, 0, 0, 0, 0, t);,
751 (smbcli_close(cli->tree, fnum2), smbcli_unlink(cli->tree, BASEDIR "\\tname1"));,
752 NOTIFY_ACTION_MODIFIED,
756 printf("testing write\n");
758 fnum2 = create_complex_file(cli, mem_ctx, BASEDIR "\\tname1");,
759 smbcli_write(cli->tree, fnum2, 1, &c, 10000, 1);,
760 (smbcli_close(cli->tree, fnum2), smbcli_unlink(cli->tree, BASEDIR "\\tname1"));,
761 NOTIFY_ACTION_MODIFIED,
764 printf("testing truncate\n");
766 fnum2 = create_complex_file(cli, mem_ctx, BASEDIR "\\tname1");,
767 smbcli_ftruncate(cli->tree, fnum2, 10000);,
768 (smbcli_close(cli->tree, fnum2), smbcli_unlink(cli->tree, BASEDIR "\\tname1"));,
769 NOTIFY_ACTION_MODIFIED,
770 FILE_NOTIFY_CHANGE_SIZE | FILE_NOTIFY_CHANGE_ATTRIBUTES, 1);
773 smb_raw_exit(cli->session);
778 basic testing of change notify on files
780 static BOOL test_notify_file(struct smbcli_state *cli, TALLOC_CTX *mem_ctx)
786 union smb_notify notify;
787 struct smbcli_request *req;
789 const char *fname = BASEDIR "\\file.txt";
791 printf("TESTING CHANGE NOTIFY ON FILES\n");
793 io.generic.level = RAW_OPEN_NTCREATEX;
794 io.ntcreatex.in.root_fid = 0;
795 io.ntcreatex.in.flags = 0;
796 io.ntcreatex.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
797 io.ntcreatex.in.create_options = 0;
798 io.ntcreatex.in.file_attr = FILE_ATTRIBUTE_NORMAL;
799 io.ntcreatex.in.share_access = NTCREATEX_SHARE_ACCESS_READ | NTCREATEX_SHARE_ACCESS_WRITE;
800 io.ntcreatex.in.alloc_size = 0;
801 io.ntcreatex.in.open_disposition = NTCREATEX_DISP_CREATE;
802 io.ntcreatex.in.impersonation = NTCREATEX_IMPERSONATION_ANONYMOUS;
803 io.ntcreatex.in.security_flags = 0;
804 io.ntcreatex.in.fname = fname;
805 status = smb_raw_open(cli->tree, mem_ctx, &io);
806 CHECK_STATUS(status, NT_STATUS_OK);
807 fnum = io.ntcreatex.out.file.fnum;
809 /* ask for a change notify,
810 on file or directory name changes */
811 notify.nttrans.level = RAW_NOTIFY_NTTRANS;
812 notify.nttrans.in.file.fnum = fnum;
813 notify.nttrans.in.buffer_size = 1000;
814 notify.nttrans.in.completion_filter = FILE_NOTIFY_CHANGE_STREAM_NAME;
815 notify.nttrans.in.recursive = False;
817 printf("testing if notifies on file handles are invalid (should be)\n");
819 req = smb_raw_changenotify_send(cli->tree, ¬ify);
820 status = smb_raw_changenotify_recv(req, mem_ctx, ¬ify);
821 CHECK_STATUS(status, NT_STATUS_INVALID_PARAMETER);
823 cl.close.level = RAW_CLOSE_CLOSE;
824 cl.close.in.file.fnum = fnum;
825 cl.close.in.write_time = 0;
826 status = smb_raw_close(cli->tree, &cl);
827 CHECK_STATUS(status, NT_STATUS_OK);
829 status = smbcli_unlink(cli->tree, fname);
830 CHECK_STATUS(status, NT_STATUS_OK);
833 smb_raw_exit(cli->session);
838 basic testing of change notifies followed by a tdis
840 static BOOL test_notify_tdis(TALLOC_CTX *mem_ctx)
844 union smb_notify notify;
847 struct smbcli_request *req;
848 struct smbcli_state *cli = NULL;
850 printf("TESTING CHANGE NOTIFY FOLLOWED BY TDIS\n");
852 if (!torture_open_connection(&cli, 0)) {
857 get a handle on the directory
859 io.generic.level = RAW_OPEN_NTCREATEX;
860 io.ntcreatex.in.root_fid = 0;
861 io.ntcreatex.in.flags = 0;
862 io.ntcreatex.in.access_mask = SEC_FILE_ALL;
863 io.ntcreatex.in.create_options = NTCREATEX_OPTIONS_DIRECTORY;
864 io.ntcreatex.in.file_attr = FILE_ATTRIBUTE_NORMAL;
865 io.ntcreatex.in.share_access = NTCREATEX_SHARE_ACCESS_READ | NTCREATEX_SHARE_ACCESS_WRITE;
866 io.ntcreatex.in.alloc_size = 0;
867 io.ntcreatex.in.open_disposition = NTCREATEX_DISP_OPEN;
868 io.ntcreatex.in.impersonation = NTCREATEX_IMPERSONATION_ANONYMOUS;
869 io.ntcreatex.in.security_flags = 0;
870 io.ntcreatex.in.fname = BASEDIR;
872 status = smb_raw_open(cli->tree, mem_ctx, &io);
873 CHECK_STATUS(status, NT_STATUS_OK);
874 fnum = io.ntcreatex.out.file.fnum;
876 /* ask for a change notify,
877 on file or directory name changes */
878 notify.nttrans.level = RAW_NOTIFY_NTTRANS;
879 notify.nttrans.in.buffer_size = 1000;
880 notify.nttrans.in.completion_filter = FILE_NOTIFY_CHANGE_NAME;
881 notify.nttrans.in.file.fnum = fnum;
882 notify.nttrans.in.recursive = True;
884 req = smb_raw_changenotify_send(cli->tree, ¬ify);
886 status = smbcli_tdis(cli);
887 CHECK_STATUS(status, NT_STATUS_OK);
890 status = smb_raw_changenotify_recv(req, mem_ctx, ¬ify);
891 CHECK_STATUS(status, NT_STATUS_OK);
892 CHECK_VAL(notify.nttrans.out.num_changes, 0);
895 torture_close_connection(cli);
900 basic testing of change notifies followed by a exit
902 static BOOL test_notify_exit(TALLOC_CTX *mem_ctx)
906 union smb_notify notify;
909 struct smbcli_request *req;
910 struct smbcli_state *cli = NULL;
912 printf("TESTING CHANGE NOTIFY FOLLOWED BY EXIT\n");
914 if (!torture_open_connection(&cli, 0)) {
919 get a handle on the directory
921 io.generic.level = RAW_OPEN_NTCREATEX;
922 io.ntcreatex.in.root_fid = 0;
923 io.ntcreatex.in.flags = 0;
924 io.ntcreatex.in.access_mask = SEC_FILE_ALL;
925 io.ntcreatex.in.create_options = NTCREATEX_OPTIONS_DIRECTORY;
926 io.ntcreatex.in.file_attr = FILE_ATTRIBUTE_NORMAL;
927 io.ntcreatex.in.share_access = NTCREATEX_SHARE_ACCESS_READ | NTCREATEX_SHARE_ACCESS_WRITE;
928 io.ntcreatex.in.alloc_size = 0;
929 io.ntcreatex.in.open_disposition = NTCREATEX_DISP_OPEN;
930 io.ntcreatex.in.impersonation = NTCREATEX_IMPERSONATION_ANONYMOUS;
931 io.ntcreatex.in.security_flags = 0;
932 io.ntcreatex.in.fname = BASEDIR;
934 status = smb_raw_open(cli->tree, mem_ctx, &io);
935 CHECK_STATUS(status, NT_STATUS_OK);
936 fnum = io.ntcreatex.out.file.fnum;
938 /* ask for a change notify,
939 on file or directory name changes */
940 notify.nttrans.level = RAW_NOTIFY_NTTRANS;
941 notify.nttrans.in.buffer_size = 1000;
942 notify.nttrans.in.completion_filter = FILE_NOTIFY_CHANGE_NAME;
943 notify.nttrans.in.file.fnum = fnum;
944 notify.nttrans.in.recursive = True;
946 req = smb_raw_changenotify_send(cli->tree, ¬ify);
948 status = smb_raw_exit(cli->session);
949 CHECK_STATUS(status, NT_STATUS_OK);
951 status = smb_raw_changenotify_recv(req, mem_ctx, ¬ify);
952 CHECK_STATUS(status, NT_STATUS_OK);
953 CHECK_VAL(notify.nttrans.out.num_changes, 0);
956 torture_close_connection(cli);
961 basic testing of change notifies followed by a ulogoff
963 static BOOL test_notify_ulogoff(TALLOC_CTX *mem_ctx)
967 union smb_notify notify;
970 struct smbcli_request *req;
971 struct smbcli_state *cli = NULL;
973 printf("TESTING CHANGE NOTIFY FOLLOWED BY ULOGOFF\n");
975 if (!torture_open_connection(&cli, 0)) {
980 get a handle on the directory
982 io.generic.level = RAW_OPEN_NTCREATEX;
983 io.ntcreatex.in.root_fid = 0;
984 io.ntcreatex.in.flags = 0;
985 io.ntcreatex.in.access_mask = SEC_FILE_ALL;
986 io.ntcreatex.in.create_options = NTCREATEX_OPTIONS_DIRECTORY;
987 io.ntcreatex.in.file_attr = FILE_ATTRIBUTE_NORMAL;
988 io.ntcreatex.in.share_access = NTCREATEX_SHARE_ACCESS_READ | NTCREATEX_SHARE_ACCESS_WRITE;
989 io.ntcreatex.in.alloc_size = 0;
990 io.ntcreatex.in.open_disposition = NTCREATEX_DISP_OPEN;
991 io.ntcreatex.in.impersonation = NTCREATEX_IMPERSONATION_ANONYMOUS;
992 io.ntcreatex.in.security_flags = 0;
993 io.ntcreatex.in.fname = BASEDIR;
995 status = smb_raw_open(cli->tree, mem_ctx, &io);
996 CHECK_STATUS(status, NT_STATUS_OK);
997 fnum = io.ntcreatex.out.file.fnum;
999 /* ask for a change notify,
1000 on file or directory name changes */
1001 notify.nttrans.level = RAW_NOTIFY_NTTRANS;
1002 notify.nttrans.in.buffer_size = 1000;
1003 notify.nttrans.in.completion_filter = FILE_NOTIFY_CHANGE_NAME;
1004 notify.nttrans.in.file.fnum = fnum;
1005 notify.nttrans.in.recursive = True;
1007 req = smb_raw_changenotify_send(cli->tree, ¬ify);
1009 status = smb_raw_ulogoff(cli->session);
1010 CHECK_STATUS(status, NT_STATUS_OK);
1012 status = smb_raw_changenotify_recv(req, mem_ctx, ¬ify);
1013 CHECK_STATUS(status, NT_STATUS_OK);
1014 CHECK_VAL(notify.nttrans.out.num_changes, 0);
1017 torture_close_connection(cli);
1021 static void tcp_dis_handler(struct smbcli_transport *t, void *p)
1023 struct smbcli_state *cli = p;
1024 smbcli_transport_dead(cli->transport, NT_STATUS_LOCAL_DISCONNECT);
1025 cli->transport = NULL;
1029 basic testing of change notifies followed by tcp disconnect
1031 static BOOL test_notify_tcp_dis(TALLOC_CTX *mem_ctx)
1035 union smb_notify notify;
1038 struct smbcli_request *req;
1039 struct smbcli_state *cli = NULL;
1041 printf("TESTING CHANGE NOTIFY FOLLOWED BY TCP DISCONNECT\n");
1043 if (!torture_open_connection(&cli, 0)) {
1048 get a handle on the directory
1050 io.generic.level = RAW_OPEN_NTCREATEX;
1051 io.ntcreatex.in.root_fid = 0;
1052 io.ntcreatex.in.flags = 0;
1053 io.ntcreatex.in.access_mask = SEC_FILE_ALL;
1054 io.ntcreatex.in.create_options = NTCREATEX_OPTIONS_DIRECTORY;
1055 io.ntcreatex.in.file_attr = FILE_ATTRIBUTE_NORMAL;
1056 io.ntcreatex.in.share_access = NTCREATEX_SHARE_ACCESS_READ | NTCREATEX_SHARE_ACCESS_WRITE;
1057 io.ntcreatex.in.alloc_size = 0;
1058 io.ntcreatex.in.open_disposition = NTCREATEX_DISP_OPEN;
1059 io.ntcreatex.in.impersonation = NTCREATEX_IMPERSONATION_ANONYMOUS;
1060 io.ntcreatex.in.security_flags = 0;
1061 io.ntcreatex.in.fname = BASEDIR;
1063 status = smb_raw_open(cli->tree, mem_ctx, &io);
1064 CHECK_STATUS(status, NT_STATUS_OK);
1065 fnum = io.ntcreatex.out.file.fnum;
1067 /* ask for a change notify,
1068 on file or directory name changes */
1069 notify.nttrans.level = RAW_NOTIFY_NTTRANS;
1070 notify.nttrans.in.buffer_size = 1000;
1071 notify.nttrans.in.completion_filter = FILE_NOTIFY_CHANGE_NAME;
1072 notify.nttrans.in.file.fnum = fnum;
1073 notify.nttrans.in.recursive = True;
1075 req = smb_raw_changenotify_send(cli->tree, ¬ify);
1077 smbcli_transport_idle_handler(cli->transport, tcp_dis_handler, 250, cli);
1079 status = smb_raw_changenotify_recv(req, mem_ctx, ¬ify);
1080 CHECK_STATUS(status, NT_STATUS_LOCAL_DISCONNECT);
1083 torture_close_connection(cli);
1088 test setting up two change notify requests on one handle
1090 static BOOL test_notify_double(struct smbcli_state *cli, TALLOC_CTX *mem_ctx)
1094 union smb_notify notify;
1097 struct smbcli_request *req1, *req2;
1099 printf("TESTING CHANGE NOTIFY TWICE ON ONE DIRECTORY\n");
1102 get a handle on the directory
1104 io.generic.level = RAW_OPEN_NTCREATEX;
1105 io.ntcreatex.in.root_fid = 0;
1106 io.ntcreatex.in.flags = 0;
1107 io.ntcreatex.in.access_mask = SEC_FILE_ALL;
1108 io.ntcreatex.in.create_options = NTCREATEX_OPTIONS_DIRECTORY;
1109 io.ntcreatex.in.file_attr = FILE_ATTRIBUTE_NORMAL;
1110 io.ntcreatex.in.share_access = NTCREATEX_SHARE_ACCESS_READ | NTCREATEX_SHARE_ACCESS_WRITE;
1111 io.ntcreatex.in.alloc_size = 0;
1112 io.ntcreatex.in.open_disposition = NTCREATEX_DISP_OPEN;
1113 io.ntcreatex.in.impersonation = NTCREATEX_IMPERSONATION_ANONYMOUS;
1114 io.ntcreatex.in.security_flags = 0;
1115 io.ntcreatex.in.fname = BASEDIR;
1117 status = smb_raw_open(cli->tree, mem_ctx, &io);
1118 CHECK_STATUS(status, NT_STATUS_OK);
1119 fnum = io.ntcreatex.out.file.fnum;
1121 /* ask for a change notify,
1122 on file or directory name changes */
1123 notify.nttrans.level = RAW_NOTIFY_NTTRANS;
1124 notify.nttrans.in.buffer_size = 1000;
1125 notify.nttrans.in.completion_filter = FILE_NOTIFY_CHANGE_NAME;
1126 notify.nttrans.in.file.fnum = fnum;
1127 notify.nttrans.in.recursive = True;
1129 req1 = smb_raw_changenotify_send(cli->tree, ¬ify);
1130 req2 = smb_raw_changenotify_send(cli->tree, ¬ify);
1132 smbcli_mkdir(cli->tree, BASEDIR "\\subdir-name");
1134 status = smb_raw_changenotify_recv(req1, mem_ctx, ¬ify);
1135 CHECK_STATUS(status, NT_STATUS_OK);
1136 CHECK_VAL(notify.nttrans.out.num_changes, 1);
1137 CHECK_WSTR(notify.nttrans.out.changes[0].name, "subdir-name", STR_UNICODE);
1139 smbcli_mkdir(cli->tree, BASEDIR "\\subdir-name2");
1141 status = smb_raw_changenotify_recv(req2, mem_ctx, ¬ify);
1142 CHECK_STATUS(status, NT_STATUS_OK);
1143 CHECK_VAL(notify.nttrans.out.num_changes, 1);
1144 CHECK_WSTR(notify.nttrans.out.changes[0].name, "subdir-name2", STR_UNICODE);
1147 smb_raw_exit(cli->session);
1153 test multiple change notifies at different depths and with/without recursion
1155 static BOOL test_notify_tree(struct smbcli_state *cli, TALLOC_CTX *mem_ctx)
1158 union smb_notify notify;
1160 struct smbcli_request *req;
1170 {BASEDIR "\\abc", True, FILE_NOTIFY_CHANGE_NAME, 30 },
1171 {BASEDIR "\\zqy", True, FILE_NOTIFY_CHANGE_NAME, 8 },
1172 {BASEDIR "\\atsy", True, FILE_NOTIFY_CHANGE_NAME, 4 },
1173 {BASEDIR "\\abc\\foo", True, FILE_NOTIFY_CHANGE_NAME, 2 },
1174 {BASEDIR "\\abc\\blah", True, FILE_NOTIFY_CHANGE_NAME, 13 },
1175 {BASEDIR "\\abc\\blah", False, FILE_NOTIFY_CHANGE_NAME, 7 },
1176 {BASEDIR "\\abc\\blah\\a", True, FILE_NOTIFY_CHANGE_NAME, 2 },
1177 {BASEDIR "\\abc\\blah\\b", True, FILE_NOTIFY_CHANGE_NAME, 2 },
1178 {BASEDIR "\\abc\\blah\\c", True, FILE_NOTIFY_CHANGE_NAME, 2 },
1179 {BASEDIR "\\abc\\fooblah", True, FILE_NOTIFY_CHANGE_NAME, 2 },
1180 {BASEDIR "\\zqy\\xx", True, FILE_NOTIFY_CHANGE_NAME, 2 },
1181 {BASEDIR "\\zqy\\yyy", True, FILE_NOTIFY_CHANGE_NAME, 2 },
1182 {BASEDIR "\\zqy\\..", True, FILE_NOTIFY_CHANGE_NAME, 40 },
1183 {BASEDIR, True, FILE_NOTIFY_CHANGE_NAME, 40 },
1184 {BASEDIR, False,FILE_NOTIFY_CHANGE_NAME, 6 },
1185 {BASEDIR "\\atsy", False,FILE_NOTIFY_CHANGE_NAME, 4 },
1186 {BASEDIR "\\abc", True, FILE_NOTIFY_CHANGE_NAME, 24 },
1187 {BASEDIR "\\abc", False,FILE_NOTIFY_CHANGE_FILE_NAME, 0 },
1188 {BASEDIR "\\abc", True, FILE_NOTIFY_CHANGE_FILE_NAME, 0 },
1189 {BASEDIR "\\abc", True, FILE_NOTIFY_CHANGE_NAME, 24 },
1193 BOOL all_done = False;
1195 printf("TESTING CHANGE NOTIFY FOR DIFFERENT DEPTHS\n");
1197 io.generic.level = RAW_OPEN_NTCREATEX;
1198 io.ntcreatex.in.root_fid = 0;
1199 io.ntcreatex.in.flags = 0;
1200 io.ntcreatex.in.access_mask = SEC_FILE_ALL;
1201 io.ntcreatex.in.create_options = NTCREATEX_OPTIONS_DIRECTORY;
1202 io.ntcreatex.in.file_attr = FILE_ATTRIBUTE_NORMAL;
1203 io.ntcreatex.in.share_access = NTCREATEX_SHARE_ACCESS_READ | NTCREATEX_SHARE_ACCESS_WRITE;
1204 io.ntcreatex.in.alloc_size = 0;
1205 io.ntcreatex.in.open_disposition = NTCREATEX_DISP_OPEN_IF;
1206 io.ntcreatex.in.impersonation = NTCREATEX_IMPERSONATION_ANONYMOUS;
1207 io.ntcreatex.in.security_flags = 0;
1209 notify.nttrans.level = RAW_NOTIFY_NTTRANS;
1210 notify.nttrans.in.buffer_size = 20000;
1213 setup the directory tree, and the notify buffer on each directory
1215 for (i=0;i<ARRAY_SIZE(dirs);i++) {
1216 io.ntcreatex.in.fname = dirs[i].path;
1217 status = smb_raw_open(cli->tree, mem_ctx, &io);
1218 CHECK_STATUS(status, NT_STATUS_OK);
1219 dirs[i].fnum = io.ntcreatex.out.file.fnum;
1221 notify.nttrans.in.completion_filter = dirs[i].filter;
1222 notify.nttrans.in.file.fnum = dirs[i].fnum;
1223 notify.nttrans.in.recursive = dirs[i].recursive;
1224 req = smb_raw_changenotify_send(cli->tree, ¬ify);
1225 smb_raw_ntcancel(req);
1226 status = smb_raw_changenotify_recv(req, mem_ctx, ¬ify);
1227 CHECK_STATUS(status, NT_STATUS_CANCELLED);
1230 /* trigger 2 events in each dir */
1231 for (i=0;i<ARRAY_SIZE(dirs);i++) {
1232 char *path = talloc_asprintf(mem_ctx, "%s\\test.dir", dirs[i].path);
1233 smbcli_mkdir(cli->tree, path);
1234 smbcli_rmdir(cli->tree, path);
1238 /* give a bit of time for the events to propogate */
1239 tv = timeval_current();
1242 /* count events that have happened in each dir */
1243 for (i=0;i<ARRAY_SIZE(dirs);i++) {
1244 notify.nttrans.in.file.fnum = dirs[i].fnum;
1245 req = smb_raw_changenotify_send(cli->tree, ¬ify);
1246 smb_raw_ntcancel(req);
1247 notify.nttrans.out.num_changes = 0;
1248 status = smb_raw_changenotify_recv(req, mem_ctx, ¬ify);
1249 dirs[i].counted += notify.nttrans.out.num_changes;
1254 for (i=0;i<ARRAY_SIZE(dirs);i++) {
1255 if (dirs[i].counted != dirs[i].expected) {
1259 } while (!all_done && timeval_elapsed(&tv) < 20);
1261 printf("took %.4f seconds to propogate all events\n", timeval_elapsed(&tv));
1263 for (i=0;i<ARRAY_SIZE(dirs);i++) {
1264 if (dirs[i].counted != dirs[i].expected) {
1265 printf("ERROR: i=%d expected %d got %d for '%s'\n",
1266 i, dirs[i].expected, dirs[i].counted, dirs[i].path);
1272 run from the back, closing and deleting
1274 for (i=ARRAY_SIZE(dirs)-1;i>=0;i--) {
1275 smbcli_close(cli->tree, dirs[i].fnum);
1276 smbcli_rmdir(cli->tree, dirs[i].path);
1280 smb_raw_exit(cli->session);
1285 basic testing of change notify
1287 BOOL torture_raw_notify(struct torture_context *torture)
1289 struct smbcli_state *cli, *cli2;
1291 TALLOC_CTX *mem_ctx;
1293 if (!torture_open_connection(&cli, 0)) {
1296 if (!torture_open_connection(&cli2, 0)) {
1300 mem_ctx = talloc_init("torture_raw_notify");
1302 if (!torture_setup_dir(cli, BASEDIR)) {
1306 ret &= test_notify_dir(cli, cli2, mem_ctx);
1307 ret &= test_notify_mask(cli, mem_ctx);
1308 ret &= test_notify_recursive(cli, mem_ctx);
1309 ret &= test_notify_mask_change(cli, mem_ctx);
1310 ret &= test_notify_file(cli, mem_ctx);
1311 ret &= test_notify_tdis(mem_ctx);
1312 ret &= test_notify_exit(mem_ctx);
1313 ret &= test_notify_ulogoff(mem_ctx);
1314 ret &= test_notify_tcp_dis(mem_ctx);
1315 ret &= test_notify_double(cli, mem_ctx);
1316 ret &= test_notify_tree(cli, mem_ctx);
1318 smb_raw_exit(cli->session);
1319 smbcli_deltree(cli->tree, BASEDIR);
1320 torture_close_connection(cli);
1321 torture_close_connection(cli2);
1322 talloc_free(mem_ctx);