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)) { \
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, TALLOC_CTX *mem_ctx)
62 struct smb_notify notify;
65 int i, count, fnum, fnum2;
66 struct smbcli_request *req, *req2;
67 extern int torture_numops;
69 printf("TESTING CHANGE NOTIFY ON DIRECTRIES\n");
72 get a handle on the directory
74 io.generic.level = RAW_OPEN_NTCREATEX;
75 io.ntcreatex.in.root_fid = 0;
76 io.ntcreatex.in.flags = 0;
77 io.ntcreatex.in.access_mask = SEC_FILE_ALL;
78 io.ntcreatex.in.create_options = NTCREATEX_OPTIONS_DIRECTORY;
79 io.ntcreatex.in.file_attr = FILE_ATTRIBUTE_NORMAL;
80 io.ntcreatex.in.share_access = NTCREATEX_SHARE_ACCESS_READ | NTCREATEX_SHARE_ACCESS_WRITE;
81 io.ntcreatex.in.alloc_size = 0;
82 io.ntcreatex.in.open_disposition = NTCREATEX_DISP_OPEN;
83 io.ntcreatex.in.impersonation = NTCREATEX_IMPERSONATION_ANONYMOUS;
84 io.ntcreatex.in.security_flags = 0;
85 io.ntcreatex.in.fname = BASEDIR;
87 status = smb_raw_open(cli->tree, mem_ctx, &io);
88 CHECK_STATUS(status, NT_STATUS_OK);
89 fnum = io.ntcreatex.out.file.fnum;
91 status = smb_raw_open(cli->tree, mem_ctx, &io);
92 CHECK_STATUS(status, NT_STATUS_OK);
93 fnum2 = io.ntcreatex.out.file.fnum;
95 /* ask for a change notify,
96 on file or directory name changes */
97 notify.in.buffer_size = 1000;
98 notify.in.completion_filter = FILE_NOTIFY_CHANGE_NAME;
99 notify.in.file.fnum = fnum;
100 notify.in.recursive = True;
102 printf("testing notify cancel\n");
104 req = smb_raw_changenotify_send(cli->tree, ¬ify);
105 smb_raw_ntcancel(req);
106 status = smb_raw_changenotify_recv(req, mem_ctx, ¬ify);
107 CHECK_STATUS(status, NT_STATUS_CANCELLED);
109 printf("testing notify mkdir\n");
111 req = smb_raw_changenotify_send(cli->tree, ¬ify);
112 smbcli_mkdir(cli->tree, BASEDIR "\\subdir-name");
114 status = smb_raw_changenotify_recv(req, mem_ctx, ¬ify);
115 CHECK_STATUS(status, NT_STATUS_OK);
117 CHECK_VAL(notify.out.num_changes, 1);
118 CHECK_VAL(notify.out.changes[0].action, NOTIFY_ACTION_ADDED);
119 CHECK_WSTR(notify.out.changes[0].name, "subdir-name", STR_UNICODE);
121 printf("testing notify rmdir\n");
123 req = smb_raw_changenotify_send(cli->tree, ¬ify);
124 smbcli_rmdir(cli->tree, BASEDIR "\\subdir-name");
126 status = smb_raw_changenotify_recv(req, mem_ctx, ¬ify);
127 CHECK_STATUS(status, NT_STATUS_OK);
128 CHECK_VAL(notify.out.num_changes, 1);
129 CHECK_VAL(notify.out.changes[0].action, NOTIFY_ACTION_REMOVED);
130 CHECK_WSTR(notify.out.changes[0].name, "subdir-name", STR_UNICODE);
132 printf("testing notify mkdir - rmdir - mkdir - rmdir\n");
134 smbcli_mkdir(cli->tree, BASEDIR "\\subdir-name");
135 smbcli_rmdir(cli->tree, BASEDIR "\\subdir-name");
136 smbcli_mkdir(cli->tree, BASEDIR "\\subdir-name");
137 smbcli_rmdir(cli->tree, BASEDIR "\\subdir-name");
138 req = smb_raw_changenotify_send(cli->tree, ¬ify);
139 status = smb_raw_changenotify_recv(req, mem_ctx, ¬ify);
140 CHECK_STATUS(status, NT_STATUS_OK);
141 CHECK_VAL(notify.out.num_changes, 4);
142 CHECK_VAL(notify.out.changes[0].action, NOTIFY_ACTION_ADDED);
143 CHECK_WSTR(notify.out.changes[0].name, "subdir-name", STR_UNICODE);
144 CHECK_VAL(notify.out.changes[1].action, NOTIFY_ACTION_REMOVED);
145 CHECK_WSTR(notify.out.changes[1].name, "subdir-name", STR_UNICODE);
146 CHECK_VAL(notify.out.changes[2].action, NOTIFY_ACTION_ADDED);
147 CHECK_WSTR(notify.out.changes[2].name, "subdir-name", STR_UNICODE);
148 CHECK_VAL(notify.out.changes[3].action, NOTIFY_ACTION_REMOVED);
149 CHECK_WSTR(notify.out.changes[3].name, "subdir-name", STR_UNICODE);
151 count = torture_numops;
152 printf("testing buffered notify on create of %d files\n", count);
153 for (i=0;i<count;i++) {
154 char *fname = talloc_asprintf(cli, BASEDIR "\\test%d.txt", i);
155 int fnum3 = smbcli_open(cli->tree, fname, O_CREAT|O_RDWR, DENY_NONE);
157 printf("Failed to create %s - %s\n",
158 fname, smbcli_errstr(cli->tree));
163 smbcli_close(cli->tree, fnum3);
166 /* (1st notify) setup a new notify on a different directory handle.
167 This new notify won't see the events above. */
168 notify.in.file.fnum = fnum2;
169 req2 = smb_raw_changenotify_send(cli->tree, ¬ify);
171 /* (2nd notify) whereas this notify will see the above buffered events,
172 and it directly returns the buffered events */
173 notify.in.file.fnum = fnum;
174 req = smb_raw_changenotify_send(cli->tree, ¬ify);
176 /* (1st unlink) as the 2nd notify directly returns,
177 this unlink is only seen by the 1st notify and
178 the 3rd notify (later) */
179 printf("testing notify on unlink for the first file\n");
180 status = smbcli_unlink(cli->tree, BASEDIR "\\test0.txt");
181 CHECK_STATUS(status, NT_STATUS_OK);
183 /* receive the reply from the 2nd notify */
184 status = smb_raw_changenotify_recv(req, mem_ctx, ¬ify);
185 CHECK_STATUS(status, NT_STATUS_OK);
187 CHECK_VAL(notify.out.num_changes, count);
188 for (i=1;i<notify.out.num_changes;i++) {
189 CHECK_VAL(notify.out.changes[i].action, NOTIFY_ACTION_ADDED);
191 CHECK_WSTR(notify.out.changes[0].name, "test0.txt", STR_UNICODE);
193 /* and now from the 1st notify */
194 status = smb_raw_changenotify_recv(req2, mem_ctx, ¬ify);
195 CHECK_STATUS(status, NT_STATUS_OK);
196 CHECK_VAL(notify.out.num_changes, 1);
197 CHECK_VAL(notify.out.changes[0].action, NOTIFY_ACTION_REMOVED);
198 CHECK_WSTR(notify.out.changes[0].name, "test0.txt", STR_UNICODE);
200 /* (3rd notify) this notify will only see the 1st unlink */
201 req = smb_raw_changenotify_send(cli->tree, ¬ify);
203 printf("testing notify on wildcard unlink for %d files\n", count-1);
204 /* (2nd unlink) do a wildcard unlink */
205 status = smbcli_unlink(cli->tree, BASEDIR "\\test*.txt");
206 CHECK_STATUS(status, NT_STATUS_OK);
208 /* receive the 3rd notify */
209 status = smb_raw_changenotify_recv(req, mem_ctx, ¬ify);
210 CHECK_STATUS(status, NT_STATUS_OK);
211 CHECK_VAL(notify.out.num_changes, 1);
212 CHECK_VAL(notify.out.changes[0].action, NOTIFY_ACTION_REMOVED);
213 CHECK_WSTR(notify.out.changes[0].name, "test0.txt", STR_UNICODE);
215 /* and we now see the rest of the unlink calls on both directory handles */
216 notify.in.file.fnum = fnum;
218 req = smb_raw_changenotify_send(cli->tree, ¬ify);
219 status = smb_raw_changenotify_recv(req, mem_ctx, ¬ify);
220 CHECK_STATUS(status, NT_STATUS_OK);
221 CHECK_VAL(notify.out.num_changes, count-1);
222 for (i=0;i<notify.out.num_changes;i++) {
223 CHECK_VAL(notify.out.changes[i].action, NOTIFY_ACTION_REMOVED);
225 notify.in.file.fnum = fnum2;
226 req = smb_raw_changenotify_send(cli->tree, ¬ify);
227 status = smb_raw_changenotify_recv(req, mem_ctx, ¬ify);
228 CHECK_STATUS(status, NT_STATUS_OK);
229 CHECK_VAL(notify.out.num_changes, count-1);
230 for (i=0;i<notify.out.num_changes;i++) {
231 CHECK_VAL(notify.out.changes[i].action, NOTIFY_ACTION_REMOVED);
234 printf("testing if a close() on the dir handle triggers the notify reply\n");
236 notify.in.file.fnum = fnum;
237 req = smb_raw_changenotify_send(cli->tree, ¬ify);
239 cl.close.level = RAW_CLOSE_CLOSE;
240 cl.close.in.file.fnum = fnum;
241 cl.close.in.write_time = 0;
242 status = smb_raw_close(cli->tree, &cl);
243 CHECK_STATUS(status, NT_STATUS_OK);
245 status = smb_raw_changenotify_recv(req, mem_ctx, ¬ify);
246 CHECK_STATUS(status, NT_STATUS_OK);
247 CHECK_VAL(notify.out.num_changes, 0);
250 smb_raw_exit(cli->session);
256 testing of recursive change notify
258 static BOOL test_notify_recursive(struct smbcli_state *cli, TALLOC_CTX *mem_ctx)
262 struct smb_notify notify;
265 struct smbcli_request *req1, *req2;
267 printf("TESTING CHANGE NOTIFY WITH RECURSION\n");
270 get a handle on the directory
272 io.generic.level = RAW_OPEN_NTCREATEX;
273 io.ntcreatex.in.root_fid = 0;
274 io.ntcreatex.in.flags = 0;
275 io.ntcreatex.in.access_mask = SEC_FILE_ALL;
276 io.ntcreatex.in.create_options = NTCREATEX_OPTIONS_DIRECTORY;
277 io.ntcreatex.in.file_attr = FILE_ATTRIBUTE_NORMAL;
278 io.ntcreatex.in.share_access = NTCREATEX_SHARE_ACCESS_READ | NTCREATEX_SHARE_ACCESS_WRITE;
279 io.ntcreatex.in.alloc_size = 0;
280 io.ntcreatex.in.open_disposition = NTCREATEX_DISP_OPEN;
281 io.ntcreatex.in.impersonation = NTCREATEX_IMPERSONATION_ANONYMOUS;
282 io.ntcreatex.in.security_flags = 0;
283 io.ntcreatex.in.fname = BASEDIR;
285 status = smb_raw_open(cli->tree, mem_ctx, &io);
286 CHECK_STATUS(status, NT_STATUS_OK);
287 fnum = io.ntcreatex.out.file.fnum;
289 /* ask for a change notify, on file or directory name
290 changes. Setup both with and without recursion */
291 notify.in.buffer_size = 1000;
292 notify.in.completion_filter = FILE_NOTIFY_CHANGE_NAME | FILE_NOTIFY_CHANGE_ATTRIBUTES | FILE_NOTIFY_CHANGE_CREATION;
293 notify.in.file.fnum = fnum;
295 notify.in.recursive = True;
296 req1 = smb_raw_changenotify_send(cli->tree, ¬ify);
298 notify.in.recursive = False;
299 req2 = smb_raw_changenotify_send(cli->tree, ¬ify);
301 /* cancel initial requests so the buffer is setup */
302 smb_raw_ntcancel(req1);
303 status = smb_raw_changenotify_recv(req1, mem_ctx, ¬ify);
304 CHECK_STATUS(status, NT_STATUS_CANCELLED);
306 smb_raw_ntcancel(req2);
307 status = smb_raw_changenotify_recv(req2, mem_ctx, ¬ify);
308 CHECK_STATUS(status, NT_STATUS_CANCELLED);
310 smbcli_mkdir(cli->tree, BASEDIR "\\subdir-name");
311 smbcli_mkdir(cli->tree, BASEDIR "\\subdir-name\\subname1");
312 smbcli_close(cli->tree,
313 smbcli_open(cli->tree, BASEDIR "\\subdir-name\\subname2", O_CREAT, 0));
314 smbcli_rename(cli->tree, BASEDIR "\\subdir-name\\subname1", BASEDIR "\\subdir-name\\subname1-r");
315 smbcli_rename(cli->tree, BASEDIR "\\subdir-name\\subname2", BASEDIR "\\subname2-r");
316 smbcli_rename(cli->tree, BASEDIR "\\subname2-r", BASEDIR "\\subname3-r");
318 notify.in.completion_filter = 0;
319 notify.in.recursive = True;
321 req1 = smb_raw_changenotify_send(cli->tree, ¬ify);
323 smbcli_rmdir(cli->tree, BASEDIR "\\subdir-name\\subname1-r");
324 smbcli_rmdir(cli->tree, BASEDIR "\\subdir-name");
325 smbcli_unlink(cli->tree, BASEDIR "\\subname3-r");
327 notify.in.recursive = False;
328 req2 = smb_raw_changenotify_send(cli->tree, ¬ify);
330 status = smb_raw_changenotify_recv(req1, mem_ctx, ¬ify);
331 CHECK_STATUS(status, NT_STATUS_OK);
333 CHECK_VAL(notify.out.num_changes, 11);
334 CHECK_VAL(notify.out.changes[0].action, NOTIFY_ACTION_ADDED);
335 CHECK_WSTR(notify.out.changes[0].name, "subdir-name", STR_UNICODE);
336 CHECK_VAL(notify.out.changes[1].action, NOTIFY_ACTION_ADDED);
337 CHECK_WSTR(notify.out.changes[1].name, "subdir-name\\subname1", STR_UNICODE);
338 CHECK_VAL(notify.out.changes[2].action, NOTIFY_ACTION_ADDED);
339 CHECK_WSTR(notify.out.changes[2].name, "subdir-name\\subname2", STR_UNICODE);
340 CHECK_VAL(notify.out.changes[3].action, NOTIFY_ACTION_OLD_NAME);
341 CHECK_WSTR(notify.out.changes[3].name, "subdir-name\\subname1", STR_UNICODE);
342 CHECK_VAL(notify.out.changes[4].action, NOTIFY_ACTION_NEW_NAME);
343 CHECK_WSTR(notify.out.changes[4].name, "subdir-name\\subname1-r", STR_UNICODE);
345 /* the remove/add between directories is acceptable in either order */
346 if (notify.out.changes[5].action == NOTIFY_ACTION_ADDED) {
347 CHECK_VAL(notify.out.changes[6].action, NOTIFY_ACTION_REMOVED);
348 CHECK_WSTR(notify.out.changes[6].name, "subdir-name\\subname2", STR_UNICODE);
349 CHECK_VAL(notify.out.changes[5].action, NOTIFY_ACTION_ADDED);
350 CHECK_WSTR(notify.out.changes[5].name, "subname2-r", STR_UNICODE);
352 CHECK_VAL(notify.out.changes[5].action, NOTIFY_ACTION_REMOVED);
353 CHECK_WSTR(notify.out.changes[5].name, "subdir-name\\subname2", STR_UNICODE);
354 CHECK_VAL(notify.out.changes[6].action, NOTIFY_ACTION_ADDED);
355 CHECK_WSTR(notify.out.changes[6].name, "subname2-r", STR_UNICODE);
358 CHECK_VAL(notify.out.changes[7].action, NOTIFY_ACTION_MODIFIED);
359 CHECK_WSTR(notify.out.changes[7].name, "subname2-r", STR_UNICODE);
361 CHECK_VAL(notify.out.changes[8].action, NOTIFY_ACTION_OLD_NAME);
362 CHECK_WSTR(notify.out.changes[8].name, "subname2-r", STR_UNICODE);
363 CHECK_VAL(notify.out.changes[9].action, NOTIFY_ACTION_NEW_NAME);
364 CHECK_WSTR(notify.out.changes[9].name, "subname3-r", STR_UNICODE);
365 CHECK_VAL(notify.out.changes[10].action, NOTIFY_ACTION_MODIFIED);
366 CHECK_WSTR(notify.out.changes[10].name, "subname3-r", STR_UNICODE);
368 status = smb_raw_changenotify_recv(req2, mem_ctx, ¬ify);
369 CHECK_STATUS(status, NT_STATUS_OK);
371 CHECK_VAL(notify.out.num_changes, 3);
372 CHECK_VAL(notify.out.changes[0].action, NOTIFY_ACTION_REMOVED);
373 CHECK_WSTR(notify.out.changes[0].name, "subdir-name\\subname1-r", STR_UNICODE);
374 CHECK_VAL(notify.out.changes[1].action, NOTIFY_ACTION_REMOVED);
375 CHECK_WSTR(notify.out.changes[1].name, "subdir-name", STR_UNICODE);
376 CHECK_VAL(notify.out.changes[2].action, NOTIFY_ACTION_REMOVED);
377 CHECK_WSTR(notify.out.changes[2].name, "subname3-r", STR_UNICODE);
380 smb_raw_exit(cli->session);
386 testing of mask bits for change notify
388 static BOOL test_notify_mask(struct smbcli_state *cli, TALLOC_CTX *mem_ctx)
392 struct smb_notify notify;
401 printf("TESTING CHANGE NOTIFY COMPLETION FILTERS\n");
403 tv = timeval_current_ofs(1000, 0);
404 t = timeval_to_nttime(&tv);
407 get a handle on the directory
409 io.generic.level = RAW_OPEN_NTCREATEX;
410 io.ntcreatex.in.root_fid = 0;
411 io.ntcreatex.in.flags = 0;
412 io.ntcreatex.in.access_mask = SEC_FILE_ALL;
413 io.ntcreatex.in.create_options = NTCREATEX_OPTIONS_DIRECTORY;
414 io.ntcreatex.in.file_attr = FILE_ATTRIBUTE_NORMAL;
415 io.ntcreatex.in.share_access = NTCREATEX_SHARE_ACCESS_READ | NTCREATEX_SHARE_ACCESS_WRITE;
416 io.ntcreatex.in.alloc_size = 0;
417 io.ntcreatex.in.open_disposition = NTCREATEX_DISP_OPEN;
418 io.ntcreatex.in.impersonation = NTCREATEX_IMPERSONATION_ANONYMOUS;
419 io.ntcreatex.in.security_flags = 0;
420 io.ntcreatex.in.fname = BASEDIR;
422 notify.in.buffer_size = 1000;
423 notify.in.recursive = True;
425 #define NOTIFY_MASK_TEST(setup, op, cleanup, Action, expected, nchanges) \
426 do { for (mask=i=0;i<32;i++) { \
427 struct smbcli_request *req; \
428 status = smb_raw_open(cli->tree, mem_ctx, &io); \
429 CHECK_STATUS(status, NT_STATUS_OK); \
430 fnum = io.ntcreatex.out.file.fnum; \
432 notify.in.file.fnum = fnum; \
433 notify.in.completion_filter = (1<<i); \
434 req = smb_raw_changenotify_send(cli->tree, ¬ify); \
436 msleep(10); smb_raw_ntcancel(req); \
437 status = smb_raw_changenotify_recv(req, mem_ctx, ¬ify); \
439 smbcli_close(cli->tree, fnum); \
440 if (NT_STATUS_EQUAL(status, NT_STATUS_CANCELLED)) continue; \
441 CHECK_STATUS(status, NT_STATUS_OK); \
442 /* special case to cope with file rename behaviour */ \
443 if (nchanges == 2 && notify.out.num_changes == 1 && \
444 notify.out.changes[0].action == NOTIFY_ACTION_MODIFIED && \
445 ((expected) & FILE_NOTIFY_CHANGE_ATTRIBUTES) && \
446 Action == NOTIFY_ACTION_OLD_NAME) { \
447 printf("(rename file special handling OK)\n"); \
448 } else if (nchanges != notify.out.num_changes || \
449 notify.out.changes[0].action != Action || \
450 strcmp(notify.out.changes[0].name.s, "tname1") != 0) { \
451 printf("ERROR: nchanges=%d action=%d filter=0x%08x\n", \
452 notify.out.num_changes, \
453 notify.out.changes[0].action, \
454 notify.in.completion_filter); \
459 if ((expected) != mask) { \
460 if (((expected) & ~mask) != 0) { \
461 printf("ERROR: trigger on too few bits. mask=0x%08x expected=0x%08x\n", \
465 printf("WARNING: trigger on too many bits. mask=0x%08x expected=0x%08x\n", \
471 printf("testing mkdir\n");
473 smbcli_mkdir(cli->tree, BASEDIR "\\tname1");,
474 smbcli_rmdir(cli->tree, BASEDIR "\\tname1");,
476 FILE_NOTIFY_CHANGE_DIR_NAME, 1);
478 printf("testing create file\n");
480 smbcli_close(cli->tree, smbcli_open(cli->tree, BASEDIR "\\tname1", O_CREAT, 0));,
481 smbcli_unlink(cli->tree, BASEDIR "\\tname1");,
483 FILE_NOTIFY_CHANGE_FILE_NAME, 1);
485 printf("testing unlink\n");
487 smbcli_close(cli->tree, smbcli_open(cli->tree, BASEDIR "\\tname1", O_CREAT, 0));,
488 smbcli_unlink(cli->tree, BASEDIR "\\tname1");,
490 NOTIFY_ACTION_REMOVED,
491 FILE_NOTIFY_CHANGE_FILE_NAME, 1);
493 printf("testing rmdir\n");
495 smbcli_mkdir(cli->tree, BASEDIR "\\tname1");,
496 smbcli_rmdir(cli->tree, BASEDIR "\\tname1");,
498 NOTIFY_ACTION_REMOVED,
499 FILE_NOTIFY_CHANGE_DIR_NAME, 1);
501 printf("testing rename file\n");
503 smbcli_close(cli->tree, smbcli_open(cli->tree, BASEDIR "\\tname1", O_CREAT, 0));,
504 smbcli_rename(cli->tree, BASEDIR "\\tname1", BASEDIR "\\tname2");,
505 smbcli_unlink(cli->tree, BASEDIR "\\tname2");,
506 NOTIFY_ACTION_OLD_NAME,
507 FILE_NOTIFY_CHANGE_FILE_NAME|FILE_NOTIFY_CHANGE_ATTRIBUTES|FILE_NOTIFY_CHANGE_CREATION, 2);
509 printf("testing rename dir\n");
511 smbcli_mkdir(cli->tree, BASEDIR "\\tname1");,
512 smbcli_rename(cli->tree, BASEDIR "\\tname1", BASEDIR "\\tname2");,
513 smbcli_rmdir(cli->tree, BASEDIR "\\tname2");,
514 NOTIFY_ACTION_OLD_NAME,
515 FILE_NOTIFY_CHANGE_DIR_NAME, 2);
517 printf("testing set path attribute\n");
519 smbcli_close(cli->tree, smbcli_open(cli->tree, BASEDIR "\\tname1", O_CREAT, 0));,
520 smbcli_setatr(cli->tree, BASEDIR "\\tname1", FILE_ATTRIBUTE_HIDDEN, 0);,
521 smbcli_unlink(cli->tree, BASEDIR "\\tname1");,
522 NOTIFY_ACTION_MODIFIED,
523 FILE_NOTIFY_CHANGE_ATTRIBUTES, 1);
525 printf("testing set path write time\n");
527 smbcli_close(cli->tree, smbcli_open(cli->tree, BASEDIR "\\tname1", O_CREAT, 0));,
528 smbcli_setatr(cli->tree, BASEDIR "\\tname1", FILE_ATTRIBUTE_NORMAL, 1000);,
529 smbcli_unlink(cli->tree, BASEDIR "\\tname1");,
530 NOTIFY_ACTION_MODIFIED,
531 FILE_NOTIFY_CHANGE_LAST_WRITE, 1);
533 printf("testing set file attribute\n");
535 fnum2 = create_complex_file(cli, mem_ctx, BASEDIR "\\tname1");,
536 smbcli_fsetatr(cli->tree, fnum2, FILE_ATTRIBUTE_HIDDEN, 0, 0, 0, 0);,
537 (smbcli_close(cli->tree, fnum2), smbcli_unlink(cli->tree, BASEDIR "\\tname1"));,
538 NOTIFY_ACTION_MODIFIED,
539 FILE_NOTIFY_CHANGE_ATTRIBUTES, 1);
541 printf("testing set file create time\n");
543 fnum2 = create_complex_file(cli, mem_ctx, BASEDIR "\\tname1");,
544 smbcli_fsetatr(cli->tree, fnum2, 0, t, 0, 0, 0);,
545 (smbcli_close(cli->tree, fnum2), smbcli_unlink(cli->tree, BASEDIR "\\tname1"));,
546 NOTIFY_ACTION_MODIFIED,
547 FILE_NOTIFY_CHANGE_CREATION, 1);
549 printf("testing set file access time\n");
551 fnum2 = create_complex_file(cli, mem_ctx, BASEDIR "\\tname1");,
552 smbcli_fsetatr(cli->tree, fnum2, 0, 0, t, 0, 0);,
553 (smbcli_close(cli->tree, fnum2), smbcli_unlink(cli->tree, BASEDIR "\\tname1"));,
554 NOTIFY_ACTION_MODIFIED,
555 FILE_NOTIFY_CHANGE_LAST_ACCESS, 1);
557 printf("testing set file write time\n");
559 fnum2 = create_complex_file(cli, mem_ctx, BASEDIR "\\tname1");,
560 smbcli_fsetatr(cli->tree, fnum2, 0, 0, 0, t, 0);,
561 (smbcli_close(cli->tree, fnum2), smbcli_unlink(cli->tree, BASEDIR "\\tname1"));,
562 NOTIFY_ACTION_MODIFIED,
563 FILE_NOTIFY_CHANGE_LAST_WRITE, 1);
565 printf("testing set file change time\n");
567 fnum2 = create_complex_file(cli, mem_ctx, BASEDIR "\\tname1");,
568 smbcli_fsetatr(cli->tree, fnum2, 0, 0, 0, 0, t);,
569 (smbcli_close(cli->tree, fnum2), smbcli_unlink(cli->tree, BASEDIR "\\tname1"));,
570 NOTIFY_ACTION_MODIFIED,
574 printf("testing write\n");
576 fnum2 = create_complex_file(cli, mem_ctx, BASEDIR "\\tname1");,
577 smbcli_write(cli->tree, fnum2, 1, &c, 10000, 1);,
578 (smbcli_close(cli->tree, fnum2), smbcli_unlink(cli->tree, BASEDIR "\\tname1"));,
579 NOTIFY_ACTION_MODIFIED,
582 printf("testing truncate\n");
584 fnum2 = create_complex_file(cli, mem_ctx, BASEDIR "\\tname1");,
585 smbcli_ftruncate(cli->tree, fnum2, 10000);,
586 (smbcli_close(cli->tree, fnum2), smbcli_unlink(cli->tree, BASEDIR "\\tname1"));,
587 NOTIFY_ACTION_MODIFIED,
588 FILE_NOTIFY_CHANGE_SIZE | FILE_NOTIFY_CHANGE_ATTRIBUTES, 1);
591 smb_raw_exit(cli->session);
596 basic testing of change notify on files
598 static BOOL test_notify_file(struct smbcli_state *cli, TALLOC_CTX *mem_ctx)
604 struct smb_notify notify;
605 struct smbcli_request *req;
607 const char *fname = BASEDIR "\\file.txt";
609 printf("TESTING CHANGE NOTIFY ON FILES\n");
611 io.generic.level = RAW_OPEN_NTCREATEX;
612 io.ntcreatex.in.root_fid = 0;
613 io.ntcreatex.in.flags = 0;
614 io.ntcreatex.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
615 io.ntcreatex.in.create_options = 0;
616 io.ntcreatex.in.file_attr = FILE_ATTRIBUTE_NORMAL;
617 io.ntcreatex.in.share_access = NTCREATEX_SHARE_ACCESS_READ | NTCREATEX_SHARE_ACCESS_WRITE;
618 io.ntcreatex.in.alloc_size = 0;
619 io.ntcreatex.in.open_disposition = NTCREATEX_DISP_CREATE;
620 io.ntcreatex.in.impersonation = NTCREATEX_IMPERSONATION_ANONYMOUS;
621 io.ntcreatex.in.security_flags = 0;
622 io.ntcreatex.in.fname = fname;
623 status = smb_raw_open(cli->tree, mem_ctx, &io);
624 CHECK_STATUS(status, NT_STATUS_OK);
625 fnum = io.ntcreatex.out.file.fnum;
627 /* ask for a change notify,
628 on file or directory name changes */
629 notify.in.file.fnum = fnum;
630 notify.in.buffer_size = 1000;
631 notify.in.completion_filter = FILE_NOTIFY_CHANGE_STREAM_NAME;
632 notify.in.recursive = False;
634 printf("testing if notifies on file handles are invalid (should be)\n");
636 req = smb_raw_changenotify_send(cli->tree, ¬ify);
637 status = smb_raw_changenotify_recv(req, mem_ctx, ¬ify);
638 CHECK_STATUS(status, NT_STATUS_INVALID_PARAMETER);
640 cl.close.level = RAW_CLOSE_CLOSE;
641 cl.close.in.file.fnum = fnum;
642 cl.close.in.write_time = 0;
643 status = smb_raw_close(cli->tree, &cl);
644 CHECK_STATUS(status, NT_STATUS_OK);
646 status = smbcli_unlink(cli->tree, fname);
647 CHECK_STATUS(status, NT_STATUS_OK);
650 smb_raw_exit(cli->session);
655 basic testing of change notifies followed by a tdis
657 static BOOL test_notify_tdis(TALLOC_CTX *mem_ctx)
661 struct smb_notify notify;
664 struct smbcli_request *req;
665 struct smbcli_state *cli = NULL;
667 printf("TESTING CHANGE NOTIFY FOLLOWED BY TDIS\n");
669 if (!torture_open_connection(&cli, 0)) {
674 get a handle on the directory
676 io.generic.level = RAW_OPEN_NTCREATEX;
677 io.ntcreatex.in.root_fid = 0;
678 io.ntcreatex.in.flags = 0;
679 io.ntcreatex.in.access_mask = SEC_FILE_ALL;
680 io.ntcreatex.in.create_options = NTCREATEX_OPTIONS_DIRECTORY;
681 io.ntcreatex.in.file_attr = FILE_ATTRIBUTE_NORMAL;
682 io.ntcreatex.in.share_access = NTCREATEX_SHARE_ACCESS_READ | NTCREATEX_SHARE_ACCESS_WRITE;
683 io.ntcreatex.in.alloc_size = 0;
684 io.ntcreatex.in.open_disposition = NTCREATEX_DISP_OPEN;
685 io.ntcreatex.in.impersonation = NTCREATEX_IMPERSONATION_ANONYMOUS;
686 io.ntcreatex.in.security_flags = 0;
687 io.ntcreatex.in.fname = BASEDIR;
689 status = smb_raw_open(cli->tree, mem_ctx, &io);
690 CHECK_STATUS(status, NT_STATUS_OK);
691 fnum = io.ntcreatex.out.file.fnum;
693 /* ask for a change notify,
694 on file or directory name changes */
695 notify.in.buffer_size = 1000;
696 notify.in.completion_filter = FILE_NOTIFY_CHANGE_NAME;
697 notify.in.file.fnum = fnum;
698 notify.in.recursive = True;
700 req = smb_raw_changenotify_send(cli->tree, ¬ify);
702 status = smbcli_tdis(cli);
703 CHECK_STATUS(status, NT_STATUS_OK);
705 status = smb_raw_changenotify_recv(req, mem_ctx, ¬ify);
706 CHECK_STATUS(status, NT_STATUS_OK);
707 CHECK_VAL(notify.out.num_changes, 0);
710 torture_close_connection(cli);
715 basic testing of change notifies followed by a exit
717 static BOOL test_notify_exit(TALLOC_CTX *mem_ctx)
721 struct smb_notify notify;
724 struct smbcli_request *req;
725 struct smbcli_state *cli = NULL;
727 printf("TESTING CHANGE NOTIFY FOLLOWED BY EXIT\n");
729 if (!torture_open_connection(&cli, 0)) {
734 get a handle on the directory
736 io.generic.level = RAW_OPEN_NTCREATEX;
737 io.ntcreatex.in.root_fid = 0;
738 io.ntcreatex.in.flags = 0;
739 io.ntcreatex.in.access_mask = SEC_FILE_ALL;
740 io.ntcreatex.in.create_options = NTCREATEX_OPTIONS_DIRECTORY;
741 io.ntcreatex.in.file_attr = FILE_ATTRIBUTE_NORMAL;
742 io.ntcreatex.in.share_access = NTCREATEX_SHARE_ACCESS_READ | NTCREATEX_SHARE_ACCESS_WRITE;
743 io.ntcreatex.in.alloc_size = 0;
744 io.ntcreatex.in.open_disposition = NTCREATEX_DISP_OPEN;
745 io.ntcreatex.in.impersonation = NTCREATEX_IMPERSONATION_ANONYMOUS;
746 io.ntcreatex.in.security_flags = 0;
747 io.ntcreatex.in.fname = BASEDIR;
749 status = smb_raw_open(cli->tree, mem_ctx, &io);
750 CHECK_STATUS(status, NT_STATUS_OK);
751 fnum = io.ntcreatex.out.file.fnum;
753 /* ask for a change notify,
754 on file or directory name changes */
755 notify.in.buffer_size = 1000;
756 notify.in.completion_filter = FILE_NOTIFY_CHANGE_NAME;
757 notify.in.file.fnum = fnum;
758 notify.in.recursive = True;
760 req = smb_raw_changenotify_send(cli->tree, ¬ify);
762 status = smb_raw_exit(cli->session);
763 CHECK_STATUS(status, NT_STATUS_OK);
765 status = smb_raw_changenotify_recv(req, mem_ctx, ¬ify);
766 CHECK_STATUS(status, NT_STATUS_OK);
767 CHECK_VAL(notify.out.num_changes, 0);
770 torture_close_connection(cli);
775 basic testing of change notifies followed by a ulogoff
777 static BOOL test_notify_ulogoff(TALLOC_CTX *mem_ctx)
781 struct smb_notify notify;
784 struct smbcli_request *req;
785 struct smbcli_state *cli = NULL;
787 printf("TESTING CHANGE NOTIFY FOLLOWED BY ULOGOFF\n");
789 if (!torture_open_connection(&cli, 0)) {
794 get a handle on the directory
796 io.generic.level = RAW_OPEN_NTCREATEX;
797 io.ntcreatex.in.root_fid = 0;
798 io.ntcreatex.in.flags = 0;
799 io.ntcreatex.in.access_mask = SEC_FILE_ALL;
800 io.ntcreatex.in.create_options = NTCREATEX_OPTIONS_DIRECTORY;
801 io.ntcreatex.in.file_attr = FILE_ATTRIBUTE_NORMAL;
802 io.ntcreatex.in.share_access = NTCREATEX_SHARE_ACCESS_READ | NTCREATEX_SHARE_ACCESS_WRITE;
803 io.ntcreatex.in.alloc_size = 0;
804 io.ntcreatex.in.open_disposition = NTCREATEX_DISP_OPEN;
805 io.ntcreatex.in.impersonation = NTCREATEX_IMPERSONATION_ANONYMOUS;
806 io.ntcreatex.in.security_flags = 0;
807 io.ntcreatex.in.fname = BASEDIR;
809 status = smb_raw_open(cli->tree, mem_ctx, &io);
810 CHECK_STATUS(status, NT_STATUS_OK);
811 fnum = io.ntcreatex.out.file.fnum;
813 /* ask for a change notify,
814 on file or directory name changes */
815 notify.in.buffer_size = 1000;
816 notify.in.completion_filter = FILE_NOTIFY_CHANGE_NAME;
817 notify.in.file.fnum = fnum;
818 notify.in.recursive = True;
820 req = smb_raw_changenotify_send(cli->tree, ¬ify);
822 status = smb_raw_ulogoff(cli->session);
823 CHECK_STATUS(status, NT_STATUS_OK);
825 status = smb_raw_changenotify_recv(req, mem_ctx, ¬ify);
826 CHECK_STATUS(status, NT_STATUS_OK);
827 CHECK_VAL(notify.out.num_changes, 0);
830 torture_close_connection(cli);
836 test setting up two change notify requests on one handle
838 static BOOL test_notify_double(struct smbcli_state *cli, TALLOC_CTX *mem_ctx)
842 struct smb_notify notify;
845 struct smbcli_request *req1, *req2;
847 printf("TESTING CHANGE NOTIFY TWICE ON ONE DIRECTORY\n");
850 get a handle on the directory
852 io.generic.level = RAW_OPEN_NTCREATEX;
853 io.ntcreatex.in.root_fid = 0;
854 io.ntcreatex.in.flags = 0;
855 io.ntcreatex.in.access_mask = SEC_FILE_ALL;
856 io.ntcreatex.in.create_options = NTCREATEX_OPTIONS_DIRECTORY;
857 io.ntcreatex.in.file_attr = FILE_ATTRIBUTE_NORMAL;
858 io.ntcreatex.in.share_access = NTCREATEX_SHARE_ACCESS_READ | NTCREATEX_SHARE_ACCESS_WRITE;
859 io.ntcreatex.in.alloc_size = 0;
860 io.ntcreatex.in.open_disposition = NTCREATEX_DISP_OPEN;
861 io.ntcreatex.in.impersonation = NTCREATEX_IMPERSONATION_ANONYMOUS;
862 io.ntcreatex.in.security_flags = 0;
863 io.ntcreatex.in.fname = BASEDIR;
865 status = smb_raw_open(cli->tree, mem_ctx, &io);
866 CHECK_STATUS(status, NT_STATUS_OK);
867 fnum = io.ntcreatex.out.file.fnum;
869 /* ask for a change notify,
870 on file or directory name changes */
871 notify.in.buffer_size = 1000;
872 notify.in.completion_filter = FILE_NOTIFY_CHANGE_NAME;
873 notify.in.file.fnum = fnum;
874 notify.in.recursive = True;
876 req1 = smb_raw_changenotify_send(cli->tree, ¬ify);
877 req2 = smb_raw_changenotify_send(cli->tree, ¬ify);
879 smbcli_mkdir(cli->tree, BASEDIR "\\subdir-name");
881 status = smb_raw_changenotify_recv(req1, mem_ctx, ¬ify);
882 CHECK_STATUS(status, NT_STATUS_OK);
883 CHECK_VAL(notify.out.num_changes, 1);
884 CHECK_WSTR(notify.out.changes[0].name, "subdir-name", STR_UNICODE);
886 smbcli_mkdir(cli->tree, BASEDIR "\\subdir-name2");
888 status = smb_raw_changenotify_recv(req2, mem_ctx, ¬ify);
889 CHECK_STATUS(status, NT_STATUS_OK);
890 CHECK_VAL(notify.out.num_changes, 1);
891 CHECK_WSTR(notify.out.changes[0].name, "subdir-name2", STR_UNICODE);
894 smb_raw_exit(cli->session);
900 test multiple change notifies at different depths and with/without recursion
902 static BOOL test_notify_tree(struct smbcli_state *cli, TALLOC_CTX *mem_ctx)
905 struct smb_notify notify;
907 struct smbcli_request *req;
915 {BASEDIR "\\abc", True, FILE_NOTIFY_CHANGE_NAME, 30 },
916 {BASEDIR "\\zqy", True, FILE_NOTIFY_CHANGE_NAME, 8 },
917 {BASEDIR "\\atsy", True, FILE_NOTIFY_CHANGE_NAME, 4 },
918 {BASEDIR "\\abc\\foo", True, FILE_NOTIFY_CHANGE_NAME, 2 },
919 {BASEDIR "\\abc\\blah", True, FILE_NOTIFY_CHANGE_NAME, 13 },
920 {BASEDIR "\\abc\\blah", False, FILE_NOTIFY_CHANGE_NAME, 7 },
921 {BASEDIR "\\abc\\blah\\a", True, FILE_NOTIFY_CHANGE_NAME, 2 },
922 {BASEDIR "\\abc\\blah\\b", True, FILE_NOTIFY_CHANGE_NAME, 2 },
923 {BASEDIR "\\abc\\blah\\c", True, FILE_NOTIFY_CHANGE_NAME, 2 },
924 {BASEDIR "\\abc\\fooblah", True, FILE_NOTIFY_CHANGE_NAME, 2 },
925 {BASEDIR "\\zqy\\xx", True, FILE_NOTIFY_CHANGE_NAME, 2 },
926 {BASEDIR "\\zqy\\yyy", True, FILE_NOTIFY_CHANGE_NAME, 2 },
927 {BASEDIR "\\zqy\\..", True, FILE_NOTIFY_CHANGE_NAME, 40 },
928 {BASEDIR, True, FILE_NOTIFY_CHANGE_NAME, 40 },
929 {BASEDIR, False,FILE_NOTIFY_CHANGE_NAME, 6 },
930 {BASEDIR "\\atsy", False,FILE_NOTIFY_CHANGE_NAME, 4 },
931 {BASEDIR "\\abc", True, FILE_NOTIFY_CHANGE_NAME, 24 },
932 {BASEDIR "\\abc", False,FILE_NOTIFY_CHANGE_FILE_NAME, 0 },
933 {BASEDIR "\\abc", True, FILE_NOTIFY_CHANGE_FILE_NAME, 0 },
934 {BASEDIR "\\abc", True, FILE_NOTIFY_CHANGE_NAME, 24 },
939 printf("TESTING CHANGE NOTIFY FOR DIFFERENT DEPTHS\n");
941 io.generic.level = RAW_OPEN_NTCREATEX;
942 io.ntcreatex.in.root_fid = 0;
943 io.ntcreatex.in.flags = 0;
944 io.ntcreatex.in.access_mask = SEC_FILE_ALL;
945 io.ntcreatex.in.create_options = NTCREATEX_OPTIONS_DIRECTORY;
946 io.ntcreatex.in.file_attr = FILE_ATTRIBUTE_NORMAL;
947 io.ntcreatex.in.share_access = NTCREATEX_SHARE_ACCESS_READ | NTCREATEX_SHARE_ACCESS_WRITE;
948 io.ntcreatex.in.alloc_size = 0;
949 io.ntcreatex.in.open_disposition = NTCREATEX_DISP_OPEN_IF;
950 io.ntcreatex.in.impersonation = NTCREATEX_IMPERSONATION_ANONYMOUS;
951 io.ntcreatex.in.security_flags = 0;
953 notify.in.buffer_size = 20000;
956 setup the directory tree, and the notify buffer on each directory
958 for (i=0;i<ARRAY_SIZE(dirs);i++) {
959 io.ntcreatex.in.fname = dirs[i].path;
960 status = smb_raw_open(cli->tree, mem_ctx, &io);
961 CHECK_STATUS(status, NT_STATUS_OK);
962 dirs[i].fnum = io.ntcreatex.out.file.fnum;
964 notify.in.completion_filter = dirs[i].filter;
965 notify.in.file.fnum = dirs[i].fnum;
966 notify.in.recursive = dirs[i].recursive;
967 req = smb_raw_changenotify_send(cli->tree, ¬ify);
968 smb_raw_ntcancel(req);
969 status = smb_raw_changenotify_recv(req, mem_ctx, ¬ify);
970 CHECK_STATUS(status, NT_STATUS_CANCELLED);
973 /* trigger 2 events in each dir */
974 for (i=0;i<ARRAY_SIZE(dirs);i++) {
975 char *path = talloc_asprintf(mem_ctx, "%s\\test.dir", dirs[i].path);
976 smbcli_mkdir(cli->tree, path);
977 smbcli_rmdir(cli->tree, path);
981 /* give a bit of time for all the events to propogate */
984 /* count events that have happened in each dir */
985 for (i=0;i<ARRAY_SIZE(dirs);i++) {
986 notify.in.file.fnum = dirs[i].fnum;
987 req = smb_raw_changenotify_send(cli->tree, ¬ify);
988 smb_raw_ntcancel(req);
989 notify.out.num_changes = 0;
990 status = smb_raw_changenotify_recv(req, mem_ctx, ¬ify);
991 if (notify.out.num_changes != dirs[i].expected) {
992 printf("ERROR: i=%d expected %d got %d for '%s'\n",
993 i, dirs[i].expected, notify.out.num_changes,
1000 run from the back, closing and deleting
1002 for (i=ARRAY_SIZE(dirs)-1;i>=0;i--) {
1003 smbcli_close(cli->tree, dirs[i].fnum);
1004 smbcli_rmdir(cli->tree, dirs[i].path);
1008 smb_raw_exit(cli->session);
1013 basic testing of change notify
1015 BOOL torture_raw_notify(struct torture_context *torture)
1017 struct smbcli_state *cli;
1019 TALLOC_CTX *mem_ctx;
1021 if (!torture_open_connection(&cli, 0)) {
1025 mem_ctx = talloc_init("torture_raw_notify");
1027 if (!torture_setup_dir(cli, BASEDIR)) {
1031 ret &= test_notify_dir(cli, mem_ctx);
1032 ret &= test_notify_mask(cli, mem_ctx);
1033 ret &= test_notify_recursive(cli, mem_ctx);
1034 ret &= test_notify_file(cli, mem_ctx);
1035 ret &= test_notify_tdis(mem_ctx);
1036 ret &= test_notify_exit(mem_ctx);
1037 ret &= test_notify_ulogoff(mem_ctx);
1038 ret &= test_notify_double(cli, mem_ctx);
1039 ret &= test_notify_tree(cli, mem_ctx);
1041 smb_raw_exit(cli->session);
1042 smbcli_deltree(cli->tree, BASEDIR);
1043 torture_close_connection(cli);
1044 talloc_free(mem_ctx);