s4-torture: Separate out the notify mask_change subtest
[Samba/gebeck_regimport.git] / source4 / torture / raw / notify.c
blobe87452da00efe0d79b9cc584d62b2ec558c606cb
1 /*
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 3 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, see <http://www.gnu.org/licenses/>.
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"
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)); \
34 ret = false; \
35 goto done; \
36 }} while (0)
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); \
43 ret = false; \
44 goto done; \
45 }} while (0)
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); \
50 ret = false; \
51 goto done; \
52 }} while (0)
54 #define CHECK_WSTR2(tctx, field, value, flags) \
55 do { \
56 if (!field.s || strcmp(field.s, value) || \
57 wire_bad_flags(&field, flags, cli->transport)) { \
58 torture_result(tctx, TORTURE_FAIL, \
59 "(%d) %s [%s] != %s\n", __LINE__, #field, field.s, value); \
60 } \
61 } while (0)
63 /*
64 basic testing of change notify on directories
66 static bool test_notify_dir(struct torture_context *mem_ctx,
67 struct smbcli_state *cli,
68 struct smbcli_state *cli2)
70 bool ret = true;
71 NTSTATUS status;
72 union smb_notify notify;
73 union smb_open io;
74 union smb_close cl;
75 int i, count, fnum, fnum2;
76 struct smbcli_request *req, *req2;
77 extern int torture_numops;
79 printf("TESTING CHANGE NOTIFY ON DIRECTORIES\n");
81 if (!torture_setup_dir(cli, BASEDIR)) {
82 return false;
86 get a handle on the directory
88 io.generic.level = RAW_OPEN_NTCREATEX;
89 io.ntcreatex.in.root_fid.fnum = 0;
90 io.ntcreatex.in.flags = 0;
91 io.ntcreatex.in.access_mask = SEC_FILE_ALL;
92 io.ntcreatex.in.create_options = NTCREATEX_OPTIONS_DIRECTORY;
93 io.ntcreatex.in.file_attr = FILE_ATTRIBUTE_NORMAL;
94 io.ntcreatex.in.share_access = NTCREATEX_SHARE_ACCESS_READ | NTCREATEX_SHARE_ACCESS_WRITE;
95 io.ntcreatex.in.alloc_size = 0;
96 io.ntcreatex.in.open_disposition = NTCREATEX_DISP_OPEN;
97 io.ntcreatex.in.impersonation = NTCREATEX_IMPERSONATION_ANONYMOUS;
98 io.ntcreatex.in.security_flags = 0;
99 io.ntcreatex.in.fname = BASEDIR;
101 status = smb_raw_open(cli->tree, mem_ctx, &io);
102 CHECK_STATUS(status, NT_STATUS_OK);
103 fnum = io.ntcreatex.out.file.fnum;
105 status = smb_raw_open(cli->tree, mem_ctx, &io);
106 CHECK_STATUS(status, NT_STATUS_OK);
107 fnum2 = io.ntcreatex.out.file.fnum;
109 /* ask for a change notify,
110 on file or directory name changes */
111 notify.nttrans.level = RAW_NOTIFY_NTTRANS;
112 notify.nttrans.in.buffer_size = 1000;
113 notify.nttrans.in.completion_filter = FILE_NOTIFY_CHANGE_NAME;
114 notify.nttrans.in.file.fnum = fnum;
115 notify.nttrans.in.recursive = true;
117 printf("Testing notify cancel\n");
119 req = smb_raw_changenotify_send(cli->tree, &notify);
120 smb_raw_ntcancel(req);
121 status = smb_raw_changenotify_recv(req, mem_ctx, &notify);
122 CHECK_STATUS(status, NT_STATUS_CANCELLED);
124 printf("Testing notify mkdir\n");
126 req = smb_raw_changenotify_send(cli->tree, &notify);
127 smbcli_mkdir(cli2->tree, BASEDIR "\\subdir-name");
129 status = smb_raw_changenotify_recv(req, mem_ctx, &notify);
130 CHECK_STATUS(status, NT_STATUS_OK);
132 CHECK_VAL(notify.nttrans.out.num_changes, 1);
133 CHECK_VAL(notify.nttrans.out.changes[0].action, NOTIFY_ACTION_ADDED);
134 CHECK_WSTR(notify.nttrans.out.changes[0].name, "subdir-name", STR_UNICODE);
136 printf("Testing notify rmdir\n");
138 req = smb_raw_changenotify_send(cli->tree, &notify);
139 smbcli_rmdir(cli2->tree, BASEDIR "\\subdir-name");
141 status = smb_raw_changenotify_recv(req, mem_ctx, &notify);
142 CHECK_STATUS(status, NT_STATUS_OK);
143 CHECK_VAL(notify.nttrans.out.num_changes, 1);
144 CHECK_VAL(notify.nttrans.out.changes[0].action, NOTIFY_ACTION_REMOVED);
145 CHECK_WSTR(notify.nttrans.out.changes[0].name, "subdir-name", STR_UNICODE);
147 printf("Testing notify mkdir - rmdir - mkdir - rmdir\n");
149 smbcli_mkdir(cli2->tree, BASEDIR "\\subdir-name");
150 smbcli_rmdir(cli2->tree, BASEDIR "\\subdir-name");
151 smbcli_mkdir(cli2->tree, BASEDIR "\\subdir-name");
152 smbcli_rmdir(cli2->tree, BASEDIR "\\subdir-name");
153 smb_msleep(200);
154 req = smb_raw_changenotify_send(cli->tree, &notify);
155 status = smb_raw_changenotify_recv(req, mem_ctx, &notify);
156 CHECK_STATUS(status, NT_STATUS_OK);
157 CHECK_VAL(notify.nttrans.out.num_changes, 4);
158 CHECK_VAL(notify.nttrans.out.changes[0].action, NOTIFY_ACTION_ADDED);
159 CHECK_WSTR(notify.nttrans.out.changes[0].name, "subdir-name", STR_UNICODE);
160 CHECK_VAL(notify.nttrans.out.changes[1].action, NOTIFY_ACTION_REMOVED);
161 CHECK_WSTR(notify.nttrans.out.changes[1].name, "subdir-name", STR_UNICODE);
162 CHECK_VAL(notify.nttrans.out.changes[2].action, NOTIFY_ACTION_ADDED);
163 CHECK_WSTR(notify.nttrans.out.changes[2].name, "subdir-name", STR_UNICODE);
164 CHECK_VAL(notify.nttrans.out.changes[3].action, NOTIFY_ACTION_REMOVED);
165 CHECK_WSTR(notify.nttrans.out.changes[3].name, "subdir-name", STR_UNICODE);
167 count = torture_numops;
168 printf("Testing buffered notify on create of %d files\n", count);
169 for (i=0;i<count;i++) {
170 char *fname = talloc_asprintf(cli, BASEDIR "\\test%d.txt", i);
171 int fnum3 = smbcli_open(cli->tree, fname, O_CREAT|O_RDWR, DENY_NONE);
172 if (fnum3 == -1) {
173 printf("Failed to create %s - %s\n",
174 fname, smbcli_errstr(cli->tree));
175 ret = false;
176 goto done;
178 talloc_free(fname);
179 smbcli_close(cli->tree, fnum3);
182 /* (1st notify) setup a new notify on a different directory handle.
183 This new notify won't see the events above. */
184 notify.nttrans.in.file.fnum = fnum2;
185 req2 = smb_raw_changenotify_send(cli->tree, &notify);
187 /* (2nd notify) whereas this notify will see the above buffered events,
188 and it directly returns the buffered events */
189 notify.nttrans.in.file.fnum = fnum;
190 req = smb_raw_changenotify_send(cli->tree, &notify);
192 status = smbcli_unlink(cli->tree, BASEDIR "\\nonexistant.txt");
193 CHECK_STATUS(status, NT_STATUS_OBJECT_NAME_NOT_FOUND);
195 /* (1st unlink) as the 2nd notify directly returns,
196 this unlink is only seen by the 1st notify and
197 the 3rd notify (later) */
198 printf("Testing notify on unlink for the first file\n");
199 status = smbcli_unlink(cli2->tree, BASEDIR "\\test0.txt");
200 CHECK_STATUS(status, NT_STATUS_OK);
202 /* receive the reply from the 2nd notify */
203 status = smb_raw_changenotify_recv(req, mem_ctx, &notify);
204 CHECK_STATUS(status, NT_STATUS_OK);
206 CHECK_VAL(notify.nttrans.out.num_changes, count);
207 for (i=1;i<count;i++) {
208 CHECK_VAL(notify.nttrans.out.changes[i].action, NOTIFY_ACTION_ADDED);
210 CHECK_WSTR(notify.nttrans.out.changes[0].name, "test0.txt", STR_UNICODE);
212 printf("and now from the 1st notify\n");
213 status = smb_raw_changenotify_recv(req2, mem_ctx, &notify);
214 CHECK_STATUS(status, NT_STATUS_OK);
215 CHECK_VAL(notify.nttrans.out.num_changes, 1);
216 CHECK_VAL(notify.nttrans.out.changes[0].action, NOTIFY_ACTION_REMOVED);
217 CHECK_WSTR(notify.nttrans.out.changes[0].name, "test0.txt", STR_UNICODE);
219 printf("(3rd notify) this notify will only see the 1st unlink\n");
220 req = smb_raw_changenotify_send(cli->tree, &notify);
222 status = smbcli_unlink(cli->tree, BASEDIR "\\nonexistant.txt");
223 CHECK_STATUS(status, NT_STATUS_OBJECT_NAME_NOT_FOUND);
225 printf("Testing notify on wildcard unlink for %d files\n", count-1);
226 /* (2nd unlink) do a wildcard unlink */
227 status = smbcli_unlink(cli2->tree, BASEDIR "\\test*.txt");
228 CHECK_STATUS(status, NT_STATUS_OK);
230 /* receive the 3rd notify */
231 status = smb_raw_changenotify_recv(req, mem_ctx, &notify);
232 CHECK_STATUS(status, NT_STATUS_OK);
233 CHECK_VAL(notify.nttrans.out.num_changes, 1);
234 CHECK_VAL(notify.nttrans.out.changes[0].action, NOTIFY_ACTION_REMOVED);
235 CHECK_WSTR(notify.nttrans.out.changes[0].name, "test0.txt", STR_UNICODE);
237 /* and we now see the rest of the unlink calls on both directory handles */
238 notify.nttrans.in.file.fnum = fnum;
239 sleep(3);
240 req = smb_raw_changenotify_send(cli->tree, &notify);
241 status = smb_raw_changenotify_recv(req, mem_ctx, &notify);
242 CHECK_STATUS(status, NT_STATUS_OK);
243 CHECK_VAL(notify.nttrans.out.num_changes, count-1);
244 for (i=0;i<notify.nttrans.out.num_changes;i++) {
245 CHECK_VAL(notify.nttrans.out.changes[i].action, NOTIFY_ACTION_REMOVED);
247 notify.nttrans.in.file.fnum = fnum2;
248 req = smb_raw_changenotify_send(cli->tree, &notify);
249 status = smb_raw_changenotify_recv(req, mem_ctx, &notify);
250 CHECK_STATUS(status, NT_STATUS_OK);
251 CHECK_VAL(notify.nttrans.out.num_changes, count-1);
252 for (i=0;i<notify.nttrans.out.num_changes;i++) {
253 CHECK_VAL(notify.nttrans.out.changes[i].action, NOTIFY_ACTION_REMOVED);
256 printf("Testing if a close() on the dir handle triggers the notify reply\n");
258 notify.nttrans.in.file.fnum = fnum;
259 req = smb_raw_changenotify_send(cli->tree, &notify);
261 cl.close.level = RAW_CLOSE_CLOSE;
262 cl.close.in.file.fnum = fnum;
263 cl.close.in.write_time = 0;
264 status = smb_raw_close(cli->tree, &cl);
265 CHECK_STATUS(status, NT_STATUS_OK);
267 status = smb_raw_changenotify_recv(req, mem_ctx, &notify);
268 CHECK_STATUS(status, NT_STATUS_OK);
269 CHECK_VAL(notify.nttrans.out.num_changes, 0);
271 done:
272 smb_raw_exit(cli->session);
273 smbcli_deltree(cli->tree, BASEDIR);
274 return ret;
278 * Check notify reply for a rename action. Not sure if this is a valid thing
279 * to do, but depending on timing between inotify and messaging we get the
280 * add/remove/modify in any order. This routines tries to find the action/name
281 * pair in any of the three following notify_changes.
284 static bool check_rename_reply(struct smbcli_state *cli,
285 int line,
286 struct notify_changes *actions,
287 uint32_t action, const char *name)
289 int i;
291 for (i=0; i<3; i++) {
292 if (actions[i].action == action) {
293 if ((actions[i].name.s == NULL)
294 || (strcmp(actions[i].name.s, name) != 0)
295 || (wire_bad_flags(&actions[i].name, STR_UNICODE,
296 cli->transport))) {
297 printf("(%d) name [%s] != %s\n", line,
298 actions[i].name.s, name);
299 return false;
301 return true;
305 printf("(%d) expected action %d, not found\n", line, action);
306 return false;
310 testing of recursive change notify
312 static bool test_notify_recursive(struct torture_context *mem_ctx,
313 struct smbcli_state *cli)
315 bool ret = true;
316 NTSTATUS status;
317 union smb_notify notify;
318 union smb_open io;
319 int fnum;
320 struct smbcli_request *req1, *req2;
322 printf("TESTING CHANGE NOTIFY WITH RECURSION\n");
324 if (!torture_setup_dir(cli, BASEDIR)) {
325 return false;
329 get a handle on the directory
331 io.generic.level = RAW_OPEN_NTCREATEX;
332 io.ntcreatex.in.root_fid.fnum = 0;
333 io.ntcreatex.in.flags = 0;
334 io.ntcreatex.in.access_mask = SEC_FILE_ALL;
335 io.ntcreatex.in.create_options = NTCREATEX_OPTIONS_DIRECTORY;
336 io.ntcreatex.in.file_attr = FILE_ATTRIBUTE_NORMAL;
337 io.ntcreatex.in.share_access = NTCREATEX_SHARE_ACCESS_READ | NTCREATEX_SHARE_ACCESS_WRITE;
338 io.ntcreatex.in.alloc_size = 0;
339 io.ntcreatex.in.open_disposition = NTCREATEX_DISP_OPEN;
340 io.ntcreatex.in.impersonation = NTCREATEX_IMPERSONATION_ANONYMOUS;
341 io.ntcreatex.in.security_flags = 0;
342 io.ntcreatex.in.fname = BASEDIR;
344 status = smb_raw_open(cli->tree, mem_ctx, &io);
345 CHECK_STATUS(status, NT_STATUS_OK);
346 fnum = io.ntcreatex.out.file.fnum;
348 /* ask for a change notify, on file or directory name
349 changes. Setup both with and without recursion */
350 notify.nttrans.level = RAW_NOTIFY_NTTRANS;
351 notify.nttrans.in.buffer_size = 1000;
352 notify.nttrans.in.completion_filter = FILE_NOTIFY_CHANGE_NAME | FILE_NOTIFY_CHANGE_ATTRIBUTES | FILE_NOTIFY_CHANGE_CREATION;
353 notify.nttrans.in.file.fnum = fnum;
355 notify.nttrans.in.recursive = true;
356 req1 = smb_raw_changenotify_send(cli->tree, &notify);
358 notify.nttrans.in.recursive = false;
359 req2 = smb_raw_changenotify_send(cli->tree, &notify);
361 /* cancel initial requests so the buffer is setup */
362 smb_raw_ntcancel(req1);
363 status = smb_raw_changenotify_recv(req1, mem_ctx, &notify);
364 CHECK_STATUS(status, NT_STATUS_CANCELLED);
366 smb_raw_ntcancel(req2);
367 status = smb_raw_changenotify_recv(req2, mem_ctx, &notify);
368 CHECK_STATUS(status, NT_STATUS_CANCELLED);
370 smbcli_mkdir(cli->tree, BASEDIR "\\subdir-name");
371 smbcli_mkdir(cli->tree, BASEDIR "\\subdir-name\\subname1");
372 smbcli_close(cli->tree,
373 smbcli_open(cli->tree, BASEDIR "\\subdir-name\\subname2", O_CREAT, 0));
374 smbcli_rename(cli->tree, BASEDIR "\\subdir-name\\subname1", BASEDIR "\\subdir-name\\subname1-r");
375 smbcli_rename(cli->tree, BASEDIR "\\subdir-name\\subname2", BASEDIR "\\subname2-r");
376 smbcli_rename(cli->tree, BASEDIR "\\subname2-r", BASEDIR "\\subname3-r");
378 notify.nttrans.in.completion_filter = 0;
379 notify.nttrans.in.recursive = true;
380 smb_msleep(200);
381 req1 = smb_raw_changenotify_send(cli->tree, &notify);
383 smbcli_rmdir(cli->tree, BASEDIR "\\subdir-name\\subname1-r");
384 smbcli_rmdir(cli->tree, BASEDIR "\\subdir-name");
385 smbcli_unlink(cli->tree, BASEDIR "\\subname3-r");
387 notify.nttrans.in.recursive = false;
388 req2 = smb_raw_changenotify_send(cli->tree, &notify);
390 status = smb_raw_changenotify_recv(req1, mem_ctx, &notify);
391 CHECK_STATUS(status, NT_STATUS_OK);
393 CHECK_VAL(notify.nttrans.out.num_changes, 11);
394 CHECK_VAL(notify.nttrans.out.changes[0].action, NOTIFY_ACTION_ADDED);
395 CHECK_WSTR(notify.nttrans.out.changes[0].name, "subdir-name", STR_UNICODE);
396 CHECK_VAL(notify.nttrans.out.changes[1].action, NOTIFY_ACTION_ADDED);
397 CHECK_WSTR(notify.nttrans.out.changes[1].name, "subdir-name\\subname1", STR_UNICODE);
398 CHECK_VAL(notify.nttrans.out.changes[2].action, NOTIFY_ACTION_ADDED);
399 CHECK_WSTR(notify.nttrans.out.changes[2].name, "subdir-name\\subname2", STR_UNICODE);
400 CHECK_VAL(notify.nttrans.out.changes[3].action, NOTIFY_ACTION_OLD_NAME);
401 CHECK_WSTR(notify.nttrans.out.changes[3].name, "subdir-name\\subname1", STR_UNICODE);
402 CHECK_VAL(notify.nttrans.out.changes[4].action, NOTIFY_ACTION_NEW_NAME);
403 CHECK_WSTR(notify.nttrans.out.changes[4].name, "subdir-name\\subname1-r", STR_UNICODE);
405 ret &= check_rename_reply(
406 cli, __LINE__, &notify.nttrans.out.changes[5],
407 NOTIFY_ACTION_ADDED, "subname2-r");
408 ret &= check_rename_reply(
409 cli, __LINE__, &notify.nttrans.out.changes[5],
410 NOTIFY_ACTION_REMOVED, "subdir-name\\subname2");
411 ret &= check_rename_reply(
412 cli, __LINE__, &notify.nttrans.out.changes[5],
413 NOTIFY_ACTION_MODIFIED, "subname2-r");
415 ret &= check_rename_reply(
416 cli, __LINE__, &notify.nttrans.out.changes[8],
417 NOTIFY_ACTION_OLD_NAME, "subname2-r");
418 ret &= check_rename_reply(
419 cli, __LINE__, &notify.nttrans.out.changes[8],
420 NOTIFY_ACTION_NEW_NAME, "subname3-r");
421 ret &= check_rename_reply(
422 cli, __LINE__, &notify.nttrans.out.changes[8],
423 NOTIFY_ACTION_MODIFIED, "subname3-r");
425 if (!ret) {
426 goto done;
429 status = smb_raw_changenotify_recv(req2, mem_ctx, &notify);
430 CHECK_STATUS(status, NT_STATUS_OK);
432 CHECK_VAL(notify.nttrans.out.num_changes, 3);
433 CHECK_VAL(notify.nttrans.out.changes[0].action, NOTIFY_ACTION_REMOVED);
434 CHECK_WSTR(notify.nttrans.out.changes[0].name, "subdir-name\\subname1-r", STR_UNICODE);
435 CHECK_VAL(notify.nttrans.out.changes[1].action, NOTIFY_ACTION_REMOVED);
436 CHECK_WSTR(notify.nttrans.out.changes[1].name, "subdir-name", STR_UNICODE);
437 CHECK_VAL(notify.nttrans.out.changes[2].action, NOTIFY_ACTION_REMOVED);
438 CHECK_WSTR(notify.nttrans.out.changes[2].name, "subname3-r", STR_UNICODE);
440 done:
441 smb_raw_exit(cli->session);
442 smbcli_deltree(cli->tree, BASEDIR);
443 return ret;
447 testing of change notify mask change
449 static bool test_notify_mask_change(struct torture_context *mem_ctx,
450 struct smbcli_state *cli)
452 bool ret = true;
453 NTSTATUS status;
454 union smb_notify notify;
455 union smb_open io;
456 int fnum;
457 struct smbcli_request *req1, *req2;
459 printf("TESTING CHANGE NOTIFY WITH MASK CHANGE\n");
461 if (!torture_setup_dir(cli, BASEDIR)) {
462 return false;
466 get a handle on the directory
468 io.generic.level = RAW_OPEN_NTCREATEX;
469 io.ntcreatex.in.root_fid.fnum = 0;
470 io.ntcreatex.in.flags = 0;
471 io.ntcreatex.in.access_mask = SEC_FILE_ALL;
472 io.ntcreatex.in.create_options = NTCREATEX_OPTIONS_DIRECTORY;
473 io.ntcreatex.in.file_attr = FILE_ATTRIBUTE_NORMAL;
474 io.ntcreatex.in.share_access = NTCREATEX_SHARE_ACCESS_READ | NTCREATEX_SHARE_ACCESS_WRITE;
475 io.ntcreatex.in.alloc_size = 0;
476 io.ntcreatex.in.open_disposition = NTCREATEX_DISP_OPEN;
477 io.ntcreatex.in.impersonation = NTCREATEX_IMPERSONATION_ANONYMOUS;
478 io.ntcreatex.in.security_flags = 0;
479 io.ntcreatex.in.fname = BASEDIR;
481 status = smb_raw_open(cli->tree, mem_ctx, &io);
482 CHECK_STATUS(status, NT_STATUS_OK);
483 fnum = io.ntcreatex.out.file.fnum;
485 /* ask for a change notify, on file or directory name
486 changes. Setup both with and without recursion */
487 notify.nttrans.level = RAW_NOTIFY_NTTRANS;
488 notify.nttrans.in.buffer_size = 1000;
489 notify.nttrans.in.completion_filter = FILE_NOTIFY_CHANGE_ATTRIBUTES;
490 notify.nttrans.in.file.fnum = fnum;
492 notify.nttrans.in.recursive = true;
493 req1 = smb_raw_changenotify_send(cli->tree, &notify);
495 notify.nttrans.in.recursive = false;
496 req2 = smb_raw_changenotify_send(cli->tree, &notify);
498 /* cancel initial requests so the buffer is setup */
499 smb_raw_ntcancel(req1);
500 status = smb_raw_changenotify_recv(req1, mem_ctx, &notify);
501 CHECK_STATUS(status, NT_STATUS_CANCELLED);
503 smb_raw_ntcancel(req2);
504 status = smb_raw_changenotify_recv(req2, mem_ctx, &notify);
505 CHECK_STATUS(status, NT_STATUS_CANCELLED);
507 notify.nttrans.in.recursive = true;
508 req1 = smb_raw_changenotify_send(cli->tree, &notify);
510 /* Set to hidden then back again. */
511 smbcli_close(cli->tree, smbcli_open(cli->tree, BASEDIR "\\tname1", O_CREAT, 0));
512 smbcli_setatr(cli->tree, BASEDIR "\\tname1", FILE_ATTRIBUTE_HIDDEN, 0);
513 smbcli_unlink(cli->tree, BASEDIR "\\tname1");
515 status = smb_raw_changenotify_recv(req1, mem_ctx, &notify);
516 CHECK_STATUS(status, NT_STATUS_OK);
518 CHECK_VAL(notify.nttrans.out.num_changes, 1);
519 CHECK_VAL(notify.nttrans.out.changes[0].action, NOTIFY_ACTION_MODIFIED);
520 CHECK_WSTR(notify.nttrans.out.changes[0].name, "tname1", STR_UNICODE);
522 /* Now try and change the mask to include other events.
523 * This should not work - once the mask is set on a directory
524 * fnum it seems to be fixed until the fnum is closed. */
526 notify.nttrans.in.completion_filter = FILE_NOTIFY_CHANGE_NAME | FILE_NOTIFY_CHANGE_ATTRIBUTES | FILE_NOTIFY_CHANGE_CREATION;
527 notify.nttrans.in.recursive = true;
528 req1 = smb_raw_changenotify_send(cli->tree, &notify);
530 notify.nttrans.in.recursive = false;
531 req2 = smb_raw_changenotify_send(cli->tree, &notify);
533 smbcli_mkdir(cli->tree, BASEDIR "\\subdir-name");
534 smbcli_mkdir(cli->tree, BASEDIR "\\subdir-name\\subname1");
535 smbcli_close(cli->tree,
536 smbcli_open(cli->tree, BASEDIR "\\subdir-name\\subname2", O_CREAT, 0));
537 smbcli_rename(cli->tree, BASEDIR "\\subdir-name\\subname1", BASEDIR "\\subdir-name\\subname1-r");
538 smbcli_rename(cli->tree, BASEDIR "\\subdir-name\\subname2", BASEDIR "\\subname2-r");
539 smbcli_rename(cli->tree, BASEDIR "\\subname2-r", BASEDIR "\\subname3-r");
541 smbcli_rmdir(cli->tree, BASEDIR "\\subdir-name\\subname1-r");
542 smbcli_rmdir(cli->tree, BASEDIR "\\subdir-name");
543 smbcli_unlink(cli->tree, BASEDIR "\\subname3-r");
545 status = smb_raw_changenotify_recv(req1, mem_ctx, &notify);
546 CHECK_STATUS(status, NT_STATUS_OK);
548 CHECK_VAL(notify.nttrans.out.num_changes, 1);
549 CHECK_VAL(notify.nttrans.out.changes[0].action, NOTIFY_ACTION_MODIFIED);
550 CHECK_WSTR(notify.nttrans.out.changes[0].name, "subname2-r", STR_UNICODE);
552 status = smb_raw_changenotify_recv(req2, mem_ctx, &notify);
553 CHECK_STATUS(status, NT_STATUS_OK);
555 CHECK_VAL(notify.nttrans.out.num_changes, 1);
556 CHECK_VAL(notify.nttrans.out.changes[0].action, NOTIFY_ACTION_MODIFIED);
557 CHECK_WSTR(notify.nttrans.out.changes[0].name, "subname3-r", STR_UNICODE);
559 if (!ret) {
560 goto done;
563 done:
564 smb_raw_exit(cli->session);
565 smbcli_deltree(cli->tree, BASEDIR);
566 return ret;
571 testing of mask bits for change notify
573 static bool test_notify_mask(struct torture_context *tctx,
574 struct smbcli_state *cli)
576 bool ret = true;
577 NTSTATUS status;
578 union smb_notify notify;
579 union smb_open io;
580 int fnum, fnum2;
581 uint32_t mask;
582 int i;
583 char c = 1;
584 struct timeval tv;
585 NTTIME t;
587 printf("TESTING CHANGE NOTIFY COMPLETION FILTERS\n");
589 if (!torture_setup_dir(cli, BASEDIR)) {
590 return false;
593 tv = timeval_current_ofs(1000, 0);
594 t = timeval_to_nttime(&tv);
597 get a handle on the directory
599 io.generic.level = RAW_OPEN_NTCREATEX;
600 io.ntcreatex.in.root_fid.fnum = 0;
601 io.ntcreatex.in.flags = 0;
602 io.ntcreatex.in.access_mask = SEC_FILE_ALL;
603 io.ntcreatex.in.create_options = NTCREATEX_OPTIONS_DIRECTORY;
604 io.ntcreatex.in.file_attr = FILE_ATTRIBUTE_NORMAL;
605 io.ntcreatex.in.share_access = NTCREATEX_SHARE_ACCESS_READ | NTCREATEX_SHARE_ACCESS_WRITE;
606 io.ntcreatex.in.alloc_size = 0;
607 io.ntcreatex.in.open_disposition = NTCREATEX_DISP_OPEN;
608 io.ntcreatex.in.impersonation = NTCREATEX_IMPERSONATION_ANONYMOUS;
609 io.ntcreatex.in.security_flags = 0;
610 io.ntcreatex.in.fname = BASEDIR;
612 notify.nttrans.level = RAW_NOTIFY_NTTRANS;
613 notify.nttrans.in.buffer_size = 1000;
614 notify.nttrans.in.recursive = true;
616 #define NOTIFY_MASK_TEST(test_name, setup, op, cleanup, Action, expected, nchanges) \
617 do { \
618 smbcli_getatr(cli->tree, test_name, NULL, NULL, NULL); \
619 do { for (mask=i=0;i<32;i++) { \
620 struct smbcli_request *req; \
621 status = smb_raw_open(cli->tree, tctx, &io); \
622 CHECK_STATUS(status, NT_STATUS_OK); \
623 fnum = io.ntcreatex.out.file.fnum; \
624 setup \
625 notify.nttrans.in.file.fnum = fnum; \
626 notify.nttrans.in.completion_filter = (1<<i); \
627 req = smb_raw_changenotify_send(cli->tree, &notify); \
628 op \
629 smb_msleep(200); smb_raw_ntcancel(req); \
630 status = smb_raw_changenotify_recv(req, tctx, &notify); \
631 cleanup \
632 smbcli_close(cli->tree, fnum); \
633 if (NT_STATUS_EQUAL(status, NT_STATUS_CANCELLED)) continue; \
634 CHECK_STATUS(status, NT_STATUS_OK); \
635 /* special case to cope with file rename behaviour */ \
636 if (nchanges == 2 && notify.nttrans.out.num_changes == 1 && \
637 notify.nttrans.out.changes[0].action == NOTIFY_ACTION_MODIFIED && \
638 ((expected) & FILE_NOTIFY_CHANGE_ATTRIBUTES) && \
639 Action == NOTIFY_ACTION_OLD_NAME) { \
640 printf("(rename file special handling OK)\n"); \
641 } else if (nchanges != notify.nttrans.out.num_changes) { \
642 printf("ERROR: nchanges=%d expected=%d action=%d filter=0x%08x\n", \
643 notify.nttrans.out.num_changes, \
644 nchanges, \
645 notify.nttrans.out.changes[0].action, \
646 notify.nttrans.in.completion_filter); \
647 ret = false; \
648 } else if (notify.nttrans.out.changes[0].action != Action) { \
649 printf("ERROR: nchanges=%d action=%d expectedAction=%d filter=0x%08x\n", \
650 notify.nttrans.out.num_changes, \
651 notify.nttrans.out.changes[0].action, \
652 Action, \
653 notify.nttrans.in.completion_filter); \
654 ret = false; \
655 } else if (strcmp(notify.nttrans.out.changes[0].name.s, "tname1") != 0) { \
656 printf("ERROR: nchanges=%d action=%d filter=0x%08x name=%s\n", \
657 notify.nttrans.out.num_changes, \
658 notify.nttrans.out.changes[0].action, \
659 notify.nttrans.in.completion_filter, \
660 notify.nttrans.out.changes[0].name.s); \
661 ret = false; \
663 mask |= (1<<i); \
665 if ((expected) != mask) { \
666 if (((expected) & ~mask) != 0) { \
667 printf("ERROR: trigger on too few bits. mask=0x%08x expected=0x%08x\n", \
668 mask, expected); \
669 ret = false; \
670 } else { \
671 printf("WARNING: trigger on too many bits. mask=0x%08x expected=0x%08x\n", \
672 mask, expected); \
675 } while (0); \
676 } while (0);
678 printf("Testing mkdir\n");
679 NOTIFY_MASK_TEST("Testing mkdir",;,
680 smbcli_mkdir(cli->tree, BASEDIR "\\tname1");,
681 smbcli_rmdir(cli->tree, BASEDIR "\\tname1");,
682 NOTIFY_ACTION_ADDED,
683 FILE_NOTIFY_CHANGE_DIR_NAME, 1);
685 printf("Testing create file\n");
686 NOTIFY_MASK_TEST("Testing create file",;,
687 smbcli_close(cli->tree, smbcli_open(cli->tree, BASEDIR "\\tname1", O_CREAT, 0));,
688 smbcli_unlink(cli->tree, BASEDIR "\\tname1");,
689 NOTIFY_ACTION_ADDED,
690 FILE_NOTIFY_CHANGE_FILE_NAME, 1);
692 printf("Testing unlink\n");
693 NOTIFY_MASK_TEST("Testing unlink",
694 smbcli_close(cli->tree, smbcli_open(cli->tree, BASEDIR "\\tname1", O_CREAT, 0));,
695 smbcli_unlink(cli->tree, BASEDIR "\\tname1");,
697 NOTIFY_ACTION_REMOVED,
698 FILE_NOTIFY_CHANGE_FILE_NAME, 1);
700 printf("Testing rmdir\n");
701 NOTIFY_MASK_TEST("Testing rmdir",
702 smbcli_mkdir(cli->tree, BASEDIR "\\tname1");,
703 smbcli_rmdir(cli->tree, BASEDIR "\\tname1");,
705 NOTIFY_ACTION_REMOVED,
706 FILE_NOTIFY_CHANGE_DIR_NAME, 1);
708 printf("Testing rename file\n");
709 NOTIFY_MASK_TEST("Testing rename file",
710 smbcli_close(cli->tree, smbcli_open(cli->tree, BASEDIR "\\tname1", O_CREAT, 0));,
711 smbcli_rename(cli->tree, BASEDIR "\\tname1", BASEDIR "\\tname2");,
712 smbcli_unlink(cli->tree, BASEDIR "\\tname2");,
713 NOTIFY_ACTION_OLD_NAME,
714 FILE_NOTIFY_CHANGE_FILE_NAME|FILE_NOTIFY_CHANGE_ATTRIBUTES|FILE_NOTIFY_CHANGE_CREATION, 2);
716 printf("Testing rename dir\n");
717 NOTIFY_MASK_TEST("Testing rename dir",
718 smbcli_mkdir(cli->tree, BASEDIR "\\tname1");,
719 smbcli_rename(cli->tree, BASEDIR "\\tname1", BASEDIR "\\tname2");,
720 smbcli_rmdir(cli->tree, BASEDIR "\\tname2");,
721 NOTIFY_ACTION_OLD_NAME,
722 FILE_NOTIFY_CHANGE_DIR_NAME, 2);
724 printf("Testing set path attribute\n");
725 NOTIFY_MASK_TEST("Testing set path attribute",
726 smbcli_close(cli->tree, smbcli_open(cli->tree, BASEDIR "\\tname1", O_CREAT, 0));,
727 smbcli_setatr(cli->tree, BASEDIR "\\tname1", FILE_ATTRIBUTE_HIDDEN, 0);,
728 smbcli_unlink(cli->tree, BASEDIR "\\tname1");,
729 NOTIFY_ACTION_MODIFIED,
730 FILE_NOTIFY_CHANGE_ATTRIBUTES, 1);
732 printf("Testing set path write time\n");
733 NOTIFY_MASK_TEST("Testing set path write time",
734 smbcli_close(cli->tree, smbcli_open(cli->tree, BASEDIR "\\tname1", O_CREAT, 0));,
735 smbcli_setatr(cli->tree, BASEDIR "\\tname1", FILE_ATTRIBUTE_NORMAL, 1000);,
736 smbcli_unlink(cli->tree, BASEDIR "\\tname1");,
737 NOTIFY_ACTION_MODIFIED,
738 FILE_NOTIFY_CHANGE_LAST_WRITE, 1);
740 printf("Testing set file attribute\n");
741 NOTIFY_MASK_TEST("Testing set file attribute",
742 fnum2 = create_complex_file(cli, tctx, BASEDIR "\\tname1");,
743 smbcli_fsetatr(cli->tree, fnum2, FILE_ATTRIBUTE_HIDDEN, 0, 0, 0, 0);,
744 (smbcli_close(cli->tree, fnum2), smbcli_unlink(cli->tree, BASEDIR "\\tname1"));,
745 NOTIFY_ACTION_MODIFIED,
746 FILE_NOTIFY_CHANGE_ATTRIBUTES, 1);
748 if (torture_setting_bool(tctx, "samba3", false)) {
749 printf("Samba3 does not yet support create times "
750 "everywhere\n");
752 else {
753 printf("Testing set file create time\n");
754 NOTIFY_MASK_TEST("Testing set file create time",
755 fnum2 = create_complex_file(cli, tctx,
756 BASEDIR "\\tname1");,
757 smbcli_fsetatr(cli->tree, fnum2, 0, t, 0, 0, 0);,
758 (smbcli_close(cli->tree, fnum2),
759 smbcli_unlink(cli->tree, BASEDIR "\\tname1"));,
760 NOTIFY_ACTION_MODIFIED,
761 FILE_NOTIFY_CHANGE_CREATION, 1);
764 printf("Testing set file access time\n");
765 NOTIFY_MASK_TEST("Testing set file access time",
766 fnum2 = create_complex_file(cli, tctx, BASEDIR "\\tname1");,
767 smbcli_fsetatr(cli->tree, fnum2, 0, 0, t, 0, 0);,
768 (smbcli_close(cli->tree, fnum2), smbcli_unlink(cli->tree, BASEDIR "\\tname1"));,
769 NOTIFY_ACTION_MODIFIED,
770 FILE_NOTIFY_CHANGE_LAST_ACCESS, 1);
772 printf("Testing set file write time\n");
773 NOTIFY_MASK_TEST("Testing set file write time",
774 fnum2 = create_complex_file(cli, tctx, BASEDIR "\\tname1");,
775 smbcli_fsetatr(cli->tree, fnum2, 0, 0, 0, t, 0);,
776 (smbcli_close(cli->tree, fnum2), smbcli_unlink(cli->tree, BASEDIR "\\tname1"));,
777 NOTIFY_ACTION_MODIFIED,
778 FILE_NOTIFY_CHANGE_LAST_WRITE, 1);
780 printf("Testing set file change time\n");
781 NOTIFY_MASK_TEST("Testing set file change time",
782 fnum2 = create_complex_file(cli, tctx, BASEDIR "\\tname1");,
783 smbcli_fsetatr(cli->tree, fnum2, 0, 0, 0, 0, t);,
784 (smbcli_close(cli->tree, fnum2), smbcli_unlink(cli->tree, BASEDIR "\\tname1"));,
785 NOTIFY_ACTION_MODIFIED,
786 0, 1);
789 printf("Testing write\n");
790 NOTIFY_MASK_TEST("Testing write",
791 fnum2 = create_complex_file(cli, tctx, BASEDIR "\\tname1");,
792 smbcli_write(cli->tree, fnum2, 1, &c, 10000, 1);,
793 (smbcli_close(cli->tree, fnum2), smbcli_unlink(cli->tree, BASEDIR "\\tname1"));,
794 NOTIFY_ACTION_MODIFIED,
795 0, 1);
797 printf("Testing truncate\n");
798 NOTIFY_MASK_TEST("Testing truncate",
799 fnum2 = create_complex_file(cli, tctx, BASEDIR "\\tname1");,
800 smbcli_ftruncate(cli->tree, fnum2, 10000);,
801 (smbcli_close(cli->tree, fnum2), smbcli_unlink(cli->tree, BASEDIR "\\tname1"));,
802 NOTIFY_ACTION_MODIFIED,
803 FILE_NOTIFY_CHANGE_SIZE | FILE_NOTIFY_CHANGE_ATTRIBUTES, 1);
805 done:
806 smb_raw_exit(cli->session);
807 smbcli_deltree(cli->tree, BASEDIR);
808 return ret;
812 basic testing of change notify on files
814 static bool test_notify_file(struct smbcli_state *cli, TALLOC_CTX *mem_ctx)
816 NTSTATUS status;
817 bool ret = true;
818 union smb_open io;
819 union smb_close cl;
820 union smb_notify notify;
821 struct smbcli_request *req;
822 int fnum;
823 const char *fname = BASEDIR "\\file.txt";
825 printf("TESTING CHANGE NOTIFY ON FILES\n");
827 io.generic.level = RAW_OPEN_NTCREATEX;
828 io.ntcreatex.in.root_fid.fnum = 0;
829 io.ntcreatex.in.flags = 0;
830 io.ntcreatex.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
831 io.ntcreatex.in.create_options = 0;
832 io.ntcreatex.in.file_attr = FILE_ATTRIBUTE_NORMAL;
833 io.ntcreatex.in.share_access = NTCREATEX_SHARE_ACCESS_READ | NTCREATEX_SHARE_ACCESS_WRITE;
834 io.ntcreatex.in.alloc_size = 0;
835 io.ntcreatex.in.open_disposition = NTCREATEX_DISP_CREATE;
836 io.ntcreatex.in.impersonation = NTCREATEX_IMPERSONATION_ANONYMOUS;
837 io.ntcreatex.in.security_flags = 0;
838 io.ntcreatex.in.fname = fname;
839 status = smb_raw_open(cli->tree, mem_ctx, &io);
840 CHECK_STATUS(status, NT_STATUS_OK);
841 fnum = io.ntcreatex.out.file.fnum;
843 /* ask for a change notify,
844 on file or directory name changes */
845 notify.nttrans.level = RAW_NOTIFY_NTTRANS;
846 notify.nttrans.in.file.fnum = fnum;
847 notify.nttrans.in.buffer_size = 1000;
848 notify.nttrans.in.completion_filter = FILE_NOTIFY_CHANGE_STREAM_NAME;
849 notify.nttrans.in.recursive = false;
851 printf("Testing if notifies on file handles are invalid (should be)\n");
853 req = smb_raw_changenotify_send(cli->tree, &notify);
854 status = smb_raw_changenotify_recv(req, mem_ctx, &notify);
855 CHECK_STATUS(status, NT_STATUS_INVALID_PARAMETER);
857 cl.close.level = RAW_CLOSE_CLOSE;
858 cl.close.in.file.fnum = fnum;
859 cl.close.in.write_time = 0;
860 status = smb_raw_close(cli->tree, &cl);
861 CHECK_STATUS(status, NT_STATUS_OK);
863 status = smbcli_unlink(cli->tree, fname);
864 CHECK_STATUS(status, NT_STATUS_OK);
866 done:
867 smb_raw_exit(cli->session);
868 return ret;
872 basic testing of change notifies followed by a tdis
874 static bool test_notify_tdis(struct torture_context *tctx)
876 bool ret = true;
877 NTSTATUS status;
878 union smb_notify notify;
879 union smb_open io;
880 int fnum;
881 struct smbcli_request *req;
882 struct smbcli_state *cli = NULL;
884 printf("TESTING CHANGE NOTIFY FOLLOWED BY TDIS\n");
886 if (!torture_open_connection(&cli, tctx, 0)) {
887 return false;
891 get a handle on the directory
893 io.generic.level = RAW_OPEN_NTCREATEX;
894 io.ntcreatex.in.root_fid.fnum = 0;
895 io.ntcreatex.in.flags = 0;
896 io.ntcreatex.in.access_mask = SEC_FILE_ALL;
897 io.ntcreatex.in.create_options = NTCREATEX_OPTIONS_DIRECTORY;
898 io.ntcreatex.in.file_attr = FILE_ATTRIBUTE_NORMAL;
899 io.ntcreatex.in.share_access = NTCREATEX_SHARE_ACCESS_READ | NTCREATEX_SHARE_ACCESS_WRITE;
900 io.ntcreatex.in.alloc_size = 0;
901 io.ntcreatex.in.open_disposition = NTCREATEX_DISP_OPEN;
902 io.ntcreatex.in.impersonation = NTCREATEX_IMPERSONATION_ANONYMOUS;
903 io.ntcreatex.in.security_flags = 0;
904 io.ntcreatex.in.fname = BASEDIR;
906 status = smb_raw_open(cli->tree, tctx, &io);
907 CHECK_STATUS(status, NT_STATUS_OK);
908 fnum = io.ntcreatex.out.file.fnum;
910 /* ask for a change notify,
911 on file or directory name changes */
912 notify.nttrans.level = RAW_NOTIFY_NTTRANS;
913 notify.nttrans.in.buffer_size = 1000;
914 notify.nttrans.in.completion_filter = FILE_NOTIFY_CHANGE_NAME;
915 notify.nttrans.in.file.fnum = fnum;
916 notify.nttrans.in.recursive = true;
918 req = smb_raw_changenotify_send(cli->tree, &notify);
920 status = smbcli_tdis(cli);
921 CHECK_STATUS(status, NT_STATUS_OK);
922 cli->tree = NULL;
924 status = smb_raw_changenotify_recv(req, tctx, &notify);
925 CHECK_STATUS(status, NT_STATUS_OK);
926 CHECK_VAL(notify.nttrans.out.num_changes, 0);
928 done:
929 torture_close_connection(cli);
930 return ret;
934 basic testing of change notifies followed by a exit
936 static bool test_notify_exit(struct torture_context *tctx)
938 bool ret = true;
939 NTSTATUS status;
940 union smb_notify notify;
941 union smb_open io;
942 int fnum;
943 struct smbcli_request *req;
944 struct smbcli_state *cli = NULL;
946 printf("TESTING CHANGE NOTIFY FOLLOWED BY EXIT\n");
948 if (!torture_open_connection(&cli, tctx, 0)) {
949 return false;
953 get a handle on the directory
955 io.generic.level = RAW_OPEN_NTCREATEX;
956 io.ntcreatex.in.root_fid.fnum = 0;
957 io.ntcreatex.in.flags = 0;
958 io.ntcreatex.in.access_mask = SEC_FILE_ALL;
959 io.ntcreatex.in.create_options = NTCREATEX_OPTIONS_DIRECTORY;
960 io.ntcreatex.in.file_attr = FILE_ATTRIBUTE_NORMAL;
961 io.ntcreatex.in.share_access = NTCREATEX_SHARE_ACCESS_READ | NTCREATEX_SHARE_ACCESS_WRITE;
962 io.ntcreatex.in.alloc_size = 0;
963 io.ntcreatex.in.open_disposition = NTCREATEX_DISP_OPEN;
964 io.ntcreatex.in.impersonation = NTCREATEX_IMPERSONATION_ANONYMOUS;
965 io.ntcreatex.in.security_flags = 0;
966 io.ntcreatex.in.fname = BASEDIR;
968 status = smb_raw_open(cli->tree, tctx, &io);
969 CHECK_STATUS(status, NT_STATUS_OK);
970 fnum = io.ntcreatex.out.file.fnum;
972 /* ask for a change notify,
973 on file or directory name changes */
974 notify.nttrans.level = RAW_NOTIFY_NTTRANS;
975 notify.nttrans.in.buffer_size = 1000;
976 notify.nttrans.in.completion_filter = FILE_NOTIFY_CHANGE_NAME;
977 notify.nttrans.in.file.fnum = fnum;
978 notify.nttrans.in.recursive = true;
980 req = smb_raw_changenotify_send(cli->tree, &notify);
982 status = smb_raw_exit(cli->session);
983 CHECK_STATUS(status, NT_STATUS_OK);
985 status = smb_raw_changenotify_recv(req, tctx, &notify);
986 CHECK_STATUS(status, NT_STATUS_OK);
987 CHECK_VAL(notify.nttrans.out.num_changes, 0);
989 done:
990 torture_close_connection(cli);
991 return ret;
995 basic testing of change notifies followed by a ulogoff
997 static bool test_notify_ulogoff(struct torture_context *tctx)
999 bool ret = true;
1000 NTSTATUS status;
1001 union smb_notify notify;
1002 union smb_open io;
1003 int fnum;
1004 struct smbcli_request *req;
1005 struct smbcli_state *cli = NULL;
1007 printf("TESTING CHANGE NOTIFY FOLLOWED BY ULOGOFF\n");
1009 if (!torture_open_connection(&cli, tctx, 0)) {
1010 return false;
1014 get a handle on the directory
1016 io.generic.level = RAW_OPEN_NTCREATEX;
1017 io.ntcreatex.in.root_fid.fnum = 0;
1018 io.ntcreatex.in.flags = 0;
1019 io.ntcreatex.in.access_mask = SEC_FILE_ALL;
1020 io.ntcreatex.in.create_options = NTCREATEX_OPTIONS_DIRECTORY;
1021 io.ntcreatex.in.file_attr = FILE_ATTRIBUTE_NORMAL;
1022 io.ntcreatex.in.share_access = NTCREATEX_SHARE_ACCESS_READ | NTCREATEX_SHARE_ACCESS_WRITE;
1023 io.ntcreatex.in.alloc_size = 0;
1024 io.ntcreatex.in.open_disposition = NTCREATEX_DISP_OPEN;
1025 io.ntcreatex.in.impersonation = NTCREATEX_IMPERSONATION_ANONYMOUS;
1026 io.ntcreatex.in.security_flags = 0;
1027 io.ntcreatex.in.fname = BASEDIR;
1029 status = smb_raw_open(cli->tree, tctx, &io);
1030 CHECK_STATUS(status, NT_STATUS_OK);
1031 fnum = io.ntcreatex.out.file.fnum;
1033 /* ask for a change notify,
1034 on file or directory name changes */
1035 notify.nttrans.level = RAW_NOTIFY_NTTRANS;
1036 notify.nttrans.in.buffer_size = 1000;
1037 notify.nttrans.in.completion_filter = FILE_NOTIFY_CHANGE_NAME;
1038 notify.nttrans.in.file.fnum = fnum;
1039 notify.nttrans.in.recursive = true;
1041 req = smb_raw_changenotify_send(cli->tree, &notify);
1043 status = smb_raw_ulogoff(cli->session);
1044 CHECK_STATUS(status, NT_STATUS_OK);
1046 status = smb_raw_changenotify_recv(req, tctx, &notify);
1047 CHECK_STATUS(status, NT_STATUS_OK);
1048 CHECK_VAL(notify.nttrans.out.num_changes, 0);
1050 done:
1051 torture_close_connection(cli);
1052 return ret;
1055 static void tcp_dis_handler(struct smbcli_transport *t, void *p)
1057 struct smbcli_state *cli = (struct smbcli_state *)p;
1058 smbcli_transport_dead(cli->transport, NT_STATUS_LOCAL_DISCONNECT);
1059 cli->transport = NULL;
1060 cli->tree = NULL;
1063 basic testing of change notifies followed by tcp disconnect
1065 static bool test_notify_tcp_dis(struct torture_context *tctx)
1067 bool ret = true;
1068 NTSTATUS status;
1069 union smb_notify notify;
1070 union smb_open io;
1071 int fnum;
1072 struct smbcli_request *req;
1073 struct smbcli_state *cli = NULL;
1075 printf("TESTING CHANGE NOTIFY FOLLOWED BY TCP DISCONNECT\n");
1077 if (!torture_open_connection(&cli, tctx, 0)) {
1078 return false;
1082 get a handle on the directory
1084 io.generic.level = RAW_OPEN_NTCREATEX;
1085 io.ntcreatex.in.root_fid.fnum = 0;
1086 io.ntcreatex.in.flags = 0;
1087 io.ntcreatex.in.access_mask = SEC_FILE_ALL;
1088 io.ntcreatex.in.create_options = NTCREATEX_OPTIONS_DIRECTORY;
1089 io.ntcreatex.in.file_attr = FILE_ATTRIBUTE_NORMAL;
1090 io.ntcreatex.in.share_access = NTCREATEX_SHARE_ACCESS_READ | NTCREATEX_SHARE_ACCESS_WRITE;
1091 io.ntcreatex.in.alloc_size = 0;
1092 io.ntcreatex.in.open_disposition = NTCREATEX_DISP_OPEN;
1093 io.ntcreatex.in.impersonation = NTCREATEX_IMPERSONATION_ANONYMOUS;
1094 io.ntcreatex.in.security_flags = 0;
1095 io.ntcreatex.in.fname = BASEDIR;
1097 status = smb_raw_open(cli->tree, tctx, &io);
1098 CHECK_STATUS(status, NT_STATUS_OK);
1099 fnum = io.ntcreatex.out.file.fnum;
1101 /* ask for a change notify,
1102 on file or directory name changes */
1103 notify.nttrans.level = RAW_NOTIFY_NTTRANS;
1104 notify.nttrans.in.buffer_size = 1000;
1105 notify.nttrans.in.completion_filter = FILE_NOTIFY_CHANGE_NAME;
1106 notify.nttrans.in.file.fnum = fnum;
1107 notify.nttrans.in.recursive = true;
1109 req = smb_raw_changenotify_send(cli->tree, &notify);
1111 smbcli_transport_idle_handler(cli->transport, tcp_dis_handler, 250, cli);
1113 status = smb_raw_changenotify_recv(req, tctx, &notify);
1114 CHECK_STATUS(status, NT_STATUS_LOCAL_DISCONNECT);
1116 done:
1117 torture_close_connection(cli);
1118 return ret;
1122 test setting up two change notify requests on one handle
1124 static bool test_notify_double(struct smbcli_state *cli, TALLOC_CTX *mem_ctx)
1126 bool ret = true;
1127 NTSTATUS status;
1128 union smb_notify notify;
1129 union smb_open io;
1130 int fnum;
1131 struct smbcli_request *req1, *req2;
1133 printf("TESTING CHANGE NOTIFY TWICE ON ONE DIRECTORY\n");
1136 get a handle on the directory
1138 io.generic.level = RAW_OPEN_NTCREATEX;
1139 io.ntcreatex.in.root_fid.fnum = 0;
1140 io.ntcreatex.in.flags = 0;
1141 io.ntcreatex.in.access_mask = SEC_FILE_ALL;
1142 io.ntcreatex.in.create_options = NTCREATEX_OPTIONS_DIRECTORY;
1143 io.ntcreatex.in.file_attr = FILE_ATTRIBUTE_NORMAL;
1144 io.ntcreatex.in.share_access = NTCREATEX_SHARE_ACCESS_READ | NTCREATEX_SHARE_ACCESS_WRITE;
1145 io.ntcreatex.in.alloc_size = 0;
1146 io.ntcreatex.in.open_disposition = NTCREATEX_DISP_OPEN;
1147 io.ntcreatex.in.impersonation = NTCREATEX_IMPERSONATION_ANONYMOUS;
1148 io.ntcreatex.in.security_flags = 0;
1149 io.ntcreatex.in.fname = BASEDIR;
1151 status = smb_raw_open(cli->tree, mem_ctx, &io);
1152 CHECK_STATUS(status, NT_STATUS_OK);
1153 fnum = io.ntcreatex.out.file.fnum;
1155 /* ask for a change notify,
1156 on file or directory name changes */
1157 notify.nttrans.level = RAW_NOTIFY_NTTRANS;
1158 notify.nttrans.in.buffer_size = 1000;
1159 notify.nttrans.in.completion_filter = FILE_NOTIFY_CHANGE_NAME;
1160 notify.nttrans.in.file.fnum = fnum;
1161 notify.nttrans.in.recursive = true;
1163 req1 = smb_raw_changenotify_send(cli->tree, &notify);
1164 req2 = smb_raw_changenotify_send(cli->tree, &notify);
1166 smbcli_mkdir(cli->tree, BASEDIR "\\subdir-name");
1168 status = smb_raw_changenotify_recv(req1, mem_ctx, &notify);
1169 CHECK_STATUS(status, NT_STATUS_OK);
1170 CHECK_VAL(notify.nttrans.out.num_changes, 1);
1171 CHECK_WSTR(notify.nttrans.out.changes[0].name, "subdir-name", STR_UNICODE);
1173 smbcli_mkdir(cli->tree, BASEDIR "\\subdir-name2");
1175 status = smb_raw_changenotify_recv(req2, mem_ctx, &notify);
1176 CHECK_STATUS(status, NT_STATUS_OK);
1177 CHECK_VAL(notify.nttrans.out.num_changes, 1);
1178 CHECK_WSTR(notify.nttrans.out.changes[0].name, "subdir-name2", STR_UNICODE);
1180 done:
1181 smb_raw_exit(cli->session);
1182 return ret;
1187 test multiple change notifies at different depths and with/without recursion
1189 static bool test_notify_tree(struct smbcli_state *cli, TALLOC_CTX *mem_ctx)
1191 bool ret = true;
1192 union smb_notify notify;
1193 union smb_open io;
1194 struct smbcli_request *req;
1195 struct timeval tv;
1196 struct {
1197 const char *path;
1198 bool recursive;
1199 uint32_t filter;
1200 int expected;
1201 int fnum;
1202 int counted;
1203 } dirs[] = {
1204 {BASEDIR "\\abc", true, FILE_NOTIFY_CHANGE_NAME, 30 },
1205 {BASEDIR "\\zqy", true, FILE_NOTIFY_CHANGE_NAME, 8 },
1206 {BASEDIR "\\atsy", true, FILE_NOTIFY_CHANGE_NAME, 4 },
1207 {BASEDIR "\\abc\\foo", true, FILE_NOTIFY_CHANGE_NAME, 2 },
1208 {BASEDIR "\\abc\\blah", true, FILE_NOTIFY_CHANGE_NAME, 13 },
1209 {BASEDIR "\\abc\\blah", false, FILE_NOTIFY_CHANGE_NAME, 7 },
1210 {BASEDIR "\\abc\\blah\\a", true, FILE_NOTIFY_CHANGE_NAME, 2 },
1211 {BASEDIR "\\abc\\blah\\b", true, FILE_NOTIFY_CHANGE_NAME, 2 },
1212 {BASEDIR "\\abc\\blah\\c", true, FILE_NOTIFY_CHANGE_NAME, 2 },
1213 {BASEDIR "\\abc\\fooblah", true, FILE_NOTIFY_CHANGE_NAME, 2 },
1214 {BASEDIR "\\zqy\\xx", true, FILE_NOTIFY_CHANGE_NAME, 2 },
1215 {BASEDIR "\\zqy\\yyy", true, FILE_NOTIFY_CHANGE_NAME, 2 },
1216 {BASEDIR "\\zqy\\..", true, FILE_NOTIFY_CHANGE_NAME, 40 },
1217 {BASEDIR, true, FILE_NOTIFY_CHANGE_NAME, 40 },
1218 {BASEDIR, false,FILE_NOTIFY_CHANGE_NAME, 6 },
1219 {BASEDIR "\\atsy", false,FILE_NOTIFY_CHANGE_NAME, 4 },
1220 {BASEDIR "\\abc", true, FILE_NOTIFY_CHANGE_NAME, 24 },
1221 {BASEDIR "\\abc", false,FILE_NOTIFY_CHANGE_FILE_NAME, 0 },
1222 {BASEDIR "\\abc", true, FILE_NOTIFY_CHANGE_FILE_NAME, 0 },
1223 {BASEDIR "\\abc", true, FILE_NOTIFY_CHANGE_NAME, 24 },
1225 int i;
1226 NTSTATUS status;
1227 bool all_done = false;
1229 printf("TESTING CHANGE NOTIFY FOR DIFFERENT DEPTHS\n");
1231 io.generic.level = RAW_OPEN_NTCREATEX;
1232 io.ntcreatex.in.root_fid.fnum = 0;
1233 io.ntcreatex.in.flags = 0;
1234 io.ntcreatex.in.access_mask = SEC_FILE_ALL;
1235 io.ntcreatex.in.create_options = NTCREATEX_OPTIONS_DIRECTORY;
1236 io.ntcreatex.in.file_attr = FILE_ATTRIBUTE_NORMAL;
1237 io.ntcreatex.in.share_access = NTCREATEX_SHARE_ACCESS_READ | NTCREATEX_SHARE_ACCESS_WRITE;
1238 io.ntcreatex.in.alloc_size = 0;
1239 io.ntcreatex.in.open_disposition = NTCREATEX_DISP_OPEN_IF;
1240 io.ntcreatex.in.impersonation = NTCREATEX_IMPERSONATION_ANONYMOUS;
1241 io.ntcreatex.in.security_flags = 0;
1243 notify.nttrans.level = RAW_NOTIFY_NTTRANS;
1244 notify.nttrans.in.buffer_size = 20000;
1247 setup the directory tree, and the notify buffer on each directory
1249 for (i=0;i<ARRAY_SIZE(dirs);i++) {
1250 io.ntcreatex.in.fname = dirs[i].path;
1251 status = smb_raw_open(cli->tree, mem_ctx, &io);
1252 CHECK_STATUS(status, NT_STATUS_OK);
1253 dirs[i].fnum = io.ntcreatex.out.file.fnum;
1255 notify.nttrans.in.completion_filter = dirs[i].filter;
1256 notify.nttrans.in.file.fnum = dirs[i].fnum;
1257 notify.nttrans.in.recursive = dirs[i].recursive;
1258 req = smb_raw_changenotify_send(cli->tree, &notify);
1259 smb_raw_ntcancel(req);
1260 status = smb_raw_changenotify_recv(req, mem_ctx, &notify);
1261 CHECK_STATUS(status, NT_STATUS_CANCELLED);
1264 /* trigger 2 events in each dir */
1265 for (i=0;i<ARRAY_SIZE(dirs);i++) {
1266 char *path = talloc_asprintf(mem_ctx, "%s\\test.dir", dirs[i].path);
1267 smbcli_mkdir(cli->tree, path);
1268 smbcli_rmdir(cli->tree, path);
1269 talloc_free(path);
1272 /* give a bit of time for the events to propogate */
1273 tv = timeval_current();
1275 do {
1276 /* count events that have happened in each dir */
1277 for (i=0;i<ARRAY_SIZE(dirs);i++) {
1278 notify.nttrans.in.file.fnum = dirs[i].fnum;
1279 req = smb_raw_changenotify_send(cli->tree, &notify);
1280 smb_raw_ntcancel(req);
1281 notify.nttrans.out.num_changes = 0;
1282 status = smb_raw_changenotify_recv(req, mem_ctx, &notify);
1283 dirs[i].counted += notify.nttrans.out.num_changes;
1286 all_done = true;
1288 for (i=0;i<ARRAY_SIZE(dirs);i++) {
1289 if (dirs[i].counted != dirs[i].expected) {
1290 all_done = false;
1293 } while (!all_done && timeval_elapsed(&tv) < 20);
1295 printf("took %.4f seconds to propogate all events\n", timeval_elapsed(&tv));
1297 for (i=0;i<ARRAY_SIZE(dirs);i++) {
1298 if (dirs[i].counted != dirs[i].expected) {
1299 printf("ERROR: i=%d expected %d got %d for '%s'\n",
1300 i, dirs[i].expected, dirs[i].counted, dirs[i].path);
1301 ret = false;
1306 run from the back, closing and deleting
1308 for (i=ARRAY_SIZE(dirs)-1;i>=0;i--) {
1309 smbcli_close(cli->tree, dirs[i].fnum);
1310 smbcli_rmdir(cli->tree, dirs[i].path);
1313 done:
1314 smb_raw_exit(cli->session);
1315 return ret;
1319 Test response when cached server events exceed single NT NOTFIY response
1320 packet size.
1322 static bool test_notify_overflow(struct smbcli_state *cli, TALLOC_CTX *mem_ctx)
1324 bool ret = true;
1325 NTSTATUS status;
1326 union smb_notify notify;
1327 union smb_open io;
1328 int fnum;
1329 int count = 100;
1330 struct smbcli_request *req1;
1331 int i;
1333 printf("TESTING CHANGE NOTIFY EVENT OVERFLOW\n");
1335 /* get a handle on the directory */
1336 io.generic.level = RAW_OPEN_NTCREATEX;
1337 io.ntcreatex.in.root_fid.fnum = 0;
1338 io.ntcreatex.in.flags = 0;
1339 io.ntcreatex.in.access_mask = SEC_FILE_ALL;
1340 io.ntcreatex.in.create_options = NTCREATEX_OPTIONS_DIRECTORY;
1341 io.ntcreatex.in.file_attr = FILE_ATTRIBUTE_NORMAL;
1342 io.ntcreatex.in.share_access = NTCREATEX_SHARE_ACCESS_READ |
1343 NTCREATEX_SHARE_ACCESS_WRITE;
1344 io.ntcreatex.in.alloc_size = 0;
1345 io.ntcreatex.in.open_disposition = NTCREATEX_DISP_OPEN;
1346 io.ntcreatex.in.impersonation = NTCREATEX_IMPERSONATION_ANONYMOUS;
1347 io.ntcreatex.in.security_flags = 0;
1348 io.ntcreatex.in.fname = BASEDIR;
1350 status = smb_raw_open(cli->tree, mem_ctx, &io);
1351 CHECK_STATUS(status, NT_STATUS_OK);
1352 fnum = io.ntcreatex.out.file.fnum;
1354 /* ask for a change notify, on name changes. */
1355 notify.nttrans.level = RAW_NOTIFY_NTTRANS;
1356 notify.nttrans.in.buffer_size = 1000;
1357 notify.nttrans.in.completion_filter = FILE_NOTIFY_CHANGE_NAME;
1358 notify.nttrans.in.file.fnum = fnum;
1360 notify.nttrans.in.recursive = true;
1361 req1 = smb_raw_changenotify_send(cli->tree, &notify);
1363 /* cancel initial requests so the buffer is setup */
1364 smb_raw_ntcancel(req1);
1365 status = smb_raw_changenotify_recv(req1, mem_ctx, &notify);
1366 CHECK_STATUS(status, NT_STATUS_CANCELLED);
1368 /* open a lot of files, filling up the server side notify buffer */
1369 printf("Testing overflowed buffer notify on create of %d files\n",
1370 count);
1371 for (i=0;i<count;i++) {
1372 char *fname = talloc_asprintf(cli, BASEDIR "\\test%d.txt", i);
1373 int fnum2 = smbcli_open(cli->tree, fname, O_CREAT|O_RDWR,
1374 DENY_NONE);
1375 if (fnum2 == -1) {
1376 printf("Failed to create %s - %s\n",
1377 fname, smbcli_errstr(cli->tree));
1378 ret = false;
1379 goto done;
1381 talloc_free(fname);
1382 smbcli_close(cli->tree, fnum2);
1385 /* expect that 0 events will be returned with NT_STATUS_OK */
1386 req1 = smb_raw_changenotify_send(cli->tree, &notify);
1387 status = smb_raw_changenotify_recv(req1, mem_ctx, &notify);
1388 CHECK_STATUS(status, NT_STATUS_OK);
1389 CHECK_VAL(notify.nttrans.out.num_changes, 0);
1391 done:
1392 smb_raw_exit(cli->session);
1393 return ret;
1397 Test if notifications are returned for changes to the base directory.
1398 They shouldn't be.
1400 static bool test_notify_basedir(struct smbcli_state *cli, TALLOC_CTX *mem_ctx)
1402 bool ret = true;
1403 NTSTATUS status;
1404 union smb_notify notify;
1405 union smb_open io;
1406 int fnum;
1407 struct smbcli_request *req1;
1409 printf("TESTING CHANGE NOTIFY BASEDIR EVENTS\n");
1411 /* get a handle on the directory */
1412 io.generic.level = RAW_OPEN_NTCREATEX;
1413 io.ntcreatex.in.root_fid.fnum = 0;
1414 io.ntcreatex.in.flags = 0;
1415 io.ntcreatex.in.access_mask = SEC_FILE_ALL;
1416 io.ntcreatex.in.create_options = NTCREATEX_OPTIONS_DIRECTORY;
1417 io.ntcreatex.in.file_attr = FILE_ATTRIBUTE_NORMAL;
1418 io.ntcreatex.in.share_access = NTCREATEX_SHARE_ACCESS_READ |
1419 NTCREATEX_SHARE_ACCESS_WRITE;
1420 io.ntcreatex.in.alloc_size = 0;
1421 io.ntcreatex.in.open_disposition = NTCREATEX_DISP_OPEN;
1422 io.ntcreatex.in.impersonation = NTCREATEX_IMPERSONATION_ANONYMOUS;
1423 io.ntcreatex.in.security_flags = 0;
1424 io.ntcreatex.in.fname = BASEDIR;
1426 status = smb_raw_open(cli->tree, mem_ctx, &io);
1427 CHECK_STATUS(status, NT_STATUS_OK);
1428 fnum = io.ntcreatex.out.file.fnum;
1430 /* create a test file that will also be modified */
1431 smbcli_close(cli->tree, smbcli_open(cli->tree, BASEDIR "\\tname1",
1432 O_CREAT, 0));
1434 /* ask for a change notify, on attribute changes. */
1435 notify.nttrans.level = RAW_NOTIFY_NTTRANS;
1436 notify.nttrans.in.buffer_size = 1000;
1437 notify.nttrans.in.completion_filter = FILE_NOTIFY_CHANGE_ATTRIBUTES;
1438 notify.nttrans.in.file.fnum = fnum;
1439 notify.nttrans.in.recursive = true;
1441 req1 = smb_raw_changenotify_send(cli->tree, &notify);
1443 /* set attribute on the base dir */
1444 smbcli_setatr(cli->tree, BASEDIR, FILE_ATTRIBUTE_HIDDEN, 0);
1446 /* set attribute on a file to assure we receive a notification */
1447 smbcli_setatr(cli->tree, BASEDIR "\\tname1", FILE_ATTRIBUTE_HIDDEN, 0);
1448 smb_msleep(200);
1450 /* check how many responses were given, expect only 1 for the file */
1451 status = smb_raw_changenotify_recv(req1, mem_ctx, &notify);
1452 CHECK_STATUS(status, NT_STATUS_OK);
1453 CHECK_VAL(notify.nttrans.out.num_changes, 1);
1454 CHECK_VAL(notify.nttrans.out.changes[0].action, NOTIFY_ACTION_MODIFIED);
1455 CHECK_WSTR(notify.nttrans.out.changes[0].name, "tname1", STR_UNICODE);
1457 done:
1458 smb_raw_exit(cli->session);
1459 return ret;
1464 create a secondary tree connect - used to test for a bug in Samba3 messaging
1465 with change notify
1467 static struct smbcli_tree *secondary_tcon(struct smbcli_state *cli,
1468 struct torture_context *tctx)
1470 NTSTATUS status;
1471 const char *share, *host;
1472 struct smbcli_tree *tree;
1473 union smb_tcon tcon;
1475 share = torture_setting_string(tctx, "share", NULL);
1476 host = torture_setting_string(tctx, "host", NULL);
1478 printf("create a second tree context on the same session\n");
1479 tree = smbcli_tree_init(cli->session, tctx, false);
1481 tcon.generic.level = RAW_TCON_TCONX;
1482 tcon.tconx.in.flags = 0;
1483 tcon.tconx.in.password = data_blob(NULL, 0);
1484 tcon.tconx.in.path = talloc_asprintf(tctx, "\\\\%s\\%s", host, share);
1485 tcon.tconx.in.device = "A:";
1486 status = smb_raw_tcon(tree, tctx, &tcon);
1487 if (!NT_STATUS_IS_OK(status)) {
1488 talloc_free(tree);
1489 printf("Failed to create secondary tree\n");
1490 return NULL;
1493 tree->tid = tcon.tconx.out.tid;
1494 printf("tid1=%d tid2=%d\n", cli->tree->tid, tree->tid);
1496 return tree;
1501 very simple change notify test
1503 static bool test_notify_tcon(struct torture_context *torture,
1504 struct smbcli_state *cli)
1506 bool ret = true;
1507 NTSTATUS status;
1508 union smb_notify notify;
1509 union smb_open io;
1510 int fnum;
1511 struct smbcli_request *req;
1512 extern int torture_numops;
1513 struct smbcli_tree *tree = NULL;
1515 printf("TESTING SIMPLE CHANGE NOTIFY\n");
1517 if (!torture_setup_dir(cli, BASEDIR)) {
1518 return false;
1522 get a handle on the directory
1524 io.generic.level = RAW_OPEN_NTCREATEX;
1525 io.ntcreatex.in.root_fid.fnum = 0;
1526 io.ntcreatex.in.flags = 0;
1527 io.ntcreatex.in.access_mask = SEC_FILE_ALL;
1528 io.ntcreatex.in.create_options = NTCREATEX_OPTIONS_DIRECTORY;
1529 io.ntcreatex.in.file_attr = FILE_ATTRIBUTE_NORMAL;
1530 io.ntcreatex.in.share_access = NTCREATEX_SHARE_ACCESS_READ | NTCREATEX_SHARE_ACCESS_WRITE;
1531 io.ntcreatex.in.alloc_size = 0;
1532 io.ntcreatex.in.open_disposition = NTCREATEX_DISP_OPEN;
1533 io.ntcreatex.in.impersonation = NTCREATEX_IMPERSONATION_ANONYMOUS;
1534 io.ntcreatex.in.security_flags = 0;
1535 io.ntcreatex.in.fname = BASEDIR;
1537 status = smb_raw_open(cli->tree, torture, &io);
1538 CHECK_STATUS(status, NT_STATUS_OK);
1539 fnum = io.ntcreatex.out.file.fnum;
1541 status = smb_raw_open(cli->tree, torture, &io);
1542 CHECK_STATUS(status, NT_STATUS_OK);
1544 /* ask for a change notify,
1545 on file or directory name changes */
1546 notify.nttrans.level = RAW_NOTIFY_NTTRANS;
1547 notify.nttrans.in.buffer_size = 1000;
1548 notify.nttrans.in.completion_filter = FILE_NOTIFY_CHANGE_NAME;
1549 notify.nttrans.in.file.fnum = fnum;
1550 notify.nttrans.in.recursive = true;
1552 printf("Testing notify mkdir\n");
1553 req = smb_raw_changenotify_send(cli->tree, &notify);
1554 smbcli_mkdir(cli->tree, BASEDIR "\\subdir-name");
1556 status = smb_raw_changenotify_recv(req, torture, &notify);
1557 CHECK_STATUS(status, NT_STATUS_OK);
1559 CHECK_VAL(notify.nttrans.out.num_changes, 1);
1560 CHECK_VAL(notify.nttrans.out.changes[0].action, NOTIFY_ACTION_ADDED);
1561 CHECK_WSTR(notify.nttrans.out.changes[0].name, "subdir-name", STR_UNICODE);
1563 printf("Testing notify rmdir\n");
1564 req = smb_raw_changenotify_send(cli->tree, &notify);
1565 smbcli_rmdir(cli->tree, BASEDIR "\\subdir-name");
1567 status = smb_raw_changenotify_recv(req, torture, &notify);
1568 CHECK_STATUS(status, NT_STATUS_OK);
1569 CHECK_VAL(notify.nttrans.out.num_changes, 1);
1570 CHECK_VAL(notify.nttrans.out.changes[0].action, NOTIFY_ACTION_REMOVED);
1571 CHECK_WSTR(notify.nttrans.out.changes[0].name, "subdir-name", STR_UNICODE);
1573 printf("SIMPLE CHANGE NOTIFY OK\n");
1575 printf("TESTING WITH SECONDARY TCON\n");
1576 tree = secondary_tcon(cli, torture);
1578 printf("Testing notify mkdir\n");
1579 req = smb_raw_changenotify_send(cli->tree, &notify);
1580 smbcli_mkdir(cli->tree, BASEDIR "\\subdir-name");
1582 status = smb_raw_changenotify_recv(req, torture, &notify);
1583 CHECK_STATUS(status, NT_STATUS_OK);
1585 CHECK_VAL(notify.nttrans.out.num_changes, 1);
1586 CHECK_VAL(notify.nttrans.out.changes[0].action, NOTIFY_ACTION_ADDED);
1587 CHECK_WSTR(notify.nttrans.out.changes[0].name, "subdir-name", STR_UNICODE);
1589 printf("Testing notify rmdir\n");
1590 req = smb_raw_changenotify_send(cli->tree, &notify);
1591 smbcli_rmdir(cli->tree, BASEDIR "\\subdir-name");
1593 status = smb_raw_changenotify_recv(req, torture, &notify);
1594 CHECK_STATUS(status, NT_STATUS_OK);
1595 CHECK_VAL(notify.nttrans.out.num_changes, 1);
1596 CHECK_VAL(notify.nttrans.out.changes[0].action, NOTIFY_ACTION_REMOVED);
1597 CHECK_WSTR(notify.nttrans.out.changes[0].name, "subdir-name", STR_UNICODE);
1599 printf("CHANGE NOTIFY WITH TCON OK\n");
1601 printf("Disconnecting secondary tree\n");
1602 status = smb_tree_disconnect(tree);
1603 CHECK_STATUS(status, NT_STATUS_OK);
1604 talloc_free(tree);
1606 printf("Testing notify mkdir\n");
1607 req = smb_raw_changenotify_send(cli->tree, &notify);
1608 smbcli_mkdir(cli->tree, BASEDIR "\\subdir-name");
1610 status = smb_raw_changenotify_recv(req, torture, &notify);
1611 CHECK_STATUS(status, NT_STATUS_OK);
1613 CHECK_VAL(notify.nttrans.out.num_changes, 1);
1614 CHECK_VAL(notify.nttrans.out.changes[0].action, NOTIFY_ACTION_ADDED);
1615 CHECK_WSTR(notify.nttrans.out.changes[0].name, "subdir-name", STR_UNICODE);
1617 printf("Testing notify rmdir\n");
1618 req = smb_raw_changenotify_send(cli->tree, &notify);
1619 smbcli_rmdir(cli->tree, BASEDIR "\\subdir-name");
1621 status = smb_raw_changenotify_recv(req, torture, &notify);
1622 CHECK_STATUS(status, NT_STATUS_OK);
1623 CHECK_VAL(notify.nttrans.out.num_changes, 1);
1624 CHECK_VAL(notify.nttrans.out.changes[0].action, NOTIFY_ACTION_REMOVED);
1625 CHECK_WSTR(notify.nttrans.out.changes[0].name, "subdir-name", STR_UNICODE);
1627 printf("CHANGE NOTIFY WITH TDIS OK\n");
1628 done:
1629 smb_raw_exit(cli->session);
1630 smbcli_deltree(cli->tree, BASEDIR);
1631 return ret;
1636 testing alignment of multiple change notify infos
1638 static bool test_notify_alignment(struct smbcli_state *cli,
1639 struct torture_context *tctx)
1641 NTSTATUS status;
1642 union smb_notify notify;
1643 union smb_open io;
1644 int i, fnum, fnum2;
1645 struct smbcli_request *req;
1646 const char *fname = BASEDIR "\\starter";
1647 const char *fnames[] = { "a",
1648 "ab",
1649 "abc",
1650 "abcd" };
1651 int num_names = ARRAY_SIZE(fnames);
1652 char *fpath = NULL;
1654 torture_comment(tctx, "TESTING CHANGE NOTIFY REPLY ALIGNMENT\n");
1656 /* get a handle on the directory */
1657 io.generic.level = RAW_OPEN_NTCREATEX;
1658 io.ntcreatex.in.root_fid.fnum = 0;
1659 io.ntcreatex.in.flags = 0;
1660 io.ntcreatex.in.access_mask = SEC_FILE_ALL;
1661 io.ntcreatex.in.create_options = NTCREATEX_OPTIONS_DIRECTORY;
1662 io.ntcreatex.in.file_attr = FILE_ATTRIBUTE_NORMAL;
1663 io.ntcreatex.in.share_access = NTCREATEX_SHARE_ACCESS_READ |
1664 NTCREATEX_SHARE_ACCESS_WRITE;
1665 io.ntcreatex.in.alloc_size = 0;
1666 io.ntcreatex.in.open_disposition = NTCREATEX_DISP_OPEN;
1667 io.ntcreatex.in.impersonation = NTCREATEX_IMPERSONATION_ANONYMOUS;
1668 io.ntcreatex.in.security_flags = 0;
1669 io.ntcreatex.in.fname = BASEDIR;
1671 status = smb_raw_open(cli->tree, tctx, &io);
1672 torture_assert_ntstatus_ok(tctx, status, "");
1673 fnum = io.ntcreatex.out.file.fnum;
1675 /* ask for a change notify, on file creation */
1676 notify.nttrans.level = RAW_NOTIFY_NTTRANS;
1677 notify.nttrans.in.buffer_size = 1000;
1678 notify.nttrans.in.completion_filter = FILE_NOTIFY_CHANGE_FILE_NAME;
1679 notify.nttrans.in.file.fnum = fnum;
1680 notify.nttrans.in.recursive = false;
1682 /* start change tracking */
1683 req = smb_raw_changenotify_send(cli->tree, &notify);
1685 fnum2 = smbcli_open(cli->tree, fname, O_CREAT|O_RDWR, DENY_NONE);
1686 torture_assert(tctx, fnum2 != -1, smbcli_errstr(cli->tree));
1687 smbcli_close(cli->tree, fnum2);
1689 status = smb_raw_changenotify_recv(req, tctx, &notify);
1690 torture_assert_ntstatus_ok(tctx, status, "");
1692 /* create 4 files that will cause CHANGE_NOTIFY_INFO structures
1693 * to be returned in the same packet with all possible 4-byte padding
1694 * permutations. As per MS-CIFS 2.2.7.4.2 these structures should be
1695 * 4-byte aligned. */
1697 for (i = 0; i < num_names; i++) {
1698 fpath = talloc_asprintf(tctx, "%s\\%s", BASEDIR, fnames[i]);
1699 fnum2 = smbcli_open(cli->tree, fpath,
1700 O_CREAT|O_RDWR, DENY_NONE);
1701 torture_assert(tctx, fnum2 != -1, smbcli_errstr(cli->tree));
1702 smbcli_close(cli->tree, fnum2);
1703 talloc_free(fpath);
1706 /* We send a notify packet, and let smb_raw_changenotify_recv() do
1707 * the alignment checking for us. */
1708 req = smb_raw_changenotify_send(cli->tree, &notify);
1709 status = smb_raw_changenotify_recv(req, tctx, &notify);
1710 torture_assert_ntstatus_ok(tctx, status, "");
1712 /* Do basic checking for correctness. */
1713 torture_assert(tctx, notify.nttrans.out.num_changes == num_names, "");
1714 for (i = 0; i < num_names; i++) {
1715 torture_assert(tctx, notify.nttrans.out.changes[i].action ==
1716 NOTIFY_ACTION_ADDED, "");
1717 CHECK_WSTR2(tctx, notify.nttrans.out.changes[i].name, fnames[i],
1718 STR_UNICODE);
1721 return true;
1725 basic testing of change notify
1727 static bool test_raw_notify_all(struct torture_context *torture,
1728 struct smbcli_state *cli,
1729 struct smbcli_state *cli2)
1731 bool ret = true;
1733 if (!torture_setup_dir(cli, BASEDIR)) {
1734 return false;
1737 ret &= test_notify_file(cli, torture);
1738 ret &= test_notify_tdis(torture);
1739 ret &= test_notify_exit(torture);
1740 ret &= test_notify_ulogoff(torture);
1741 ret &= test_notify_tcp_dis(torture);
1742 ret &= test_notify_double(cli, torture);
1743 ret &= test_notify_tree(cli, torture);
1744 ret &= test_notify_overflow(cli, torture);
1745 ret &= test_notify_basedir(cli, torture);
1746 ret &= test_notify_alignment(cli, torture);
1748 smb_raw_exit(cli->session);
1749 smbcli_deltree(cli->tree, BASEDIR);
1750 return ret;
1753 struct torture_suite *torture_raw_notify(TALLOC_CTX *mem_ctx)
1755 struct torture_suite *suite = torture_suite_create(mem_ctx, "notify");
1757 torture_suite_add_1smb_test(suite, "tcon", test_notify_tcon);
1758 torture_suite_add_2smb_test(suite, "dir", test_notify_dir);
1759 torture_suite_add_1smb_test(suite, "mask", test_notify_mask);
1760 torture_suite_add_1smb_test(suite, "recursive", test_notify_recursive);
1761 torture_suite_add_1smb_test(suite, "mask_change",
1762 test_notify_mask_change);
1763 torture_suite_add_2smb_test(suite, "all", test_raw_notify_all);
1765 return suite;