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"
27 #define BASEDIR "\\test_notify"
29 #define CHECK_STATUS(status, correct) do { \
30 if (!NT_STATUS_EQUAL(status, correct)) { \
31 printf("(%d) Incorrect status %s - should be %s\n", \
32 __LINE__, nt_errstr(status), nt_errstr(correct)); \
38 #define CHECK_VAL(v, correct) do { \
39 if ((v) != (correct)) { \
40 printf("(%d) wrong value for %s 0x%x should be 0x%x\n", \
41 __LINE__, #v, (int)v, (int)correct); \
46 #define CHECK_WSTR(field, value, flags) do { \
47 if (!field.s || strcmp(field.s, value) || wire_bad_flags(&field, flags, cli)) { \
48 printf("(%d) %s [%s] != %s\n", __LINE__, #field, field.s, value); \
55 basic testing of change notify
57 BOOL torture_raw_notify(void)
59 struct smbcli_state *cli;
63 union smb_notify notify;
65 int i, count, fnum, fnum2;
66 struct smbcli_request *req, *req2;
67 extern int torture_numops;
69 if (!torture_open_connection(&cli)) {
73 mem_ctx = talloc_init("torture_raw_notify");
75 if (!torture_setup_dir(cli, BASEDIR)) {
80 get a handle on the directory
82 io.generic.level = RAW_OPEN_NTCREATEX;
83 io.ntcreatex.in.root_fid = 0;
84 io.ntcreatex.in.flags = 0;
85 io.ntcreatex.in.access_mask = SEC_FILE_ALL;
86 io.ntcreatex.in.create_options = NTCREATEX_OPTIONS_DIRECTORY;
87 io.ntcreatex.in.file_attr = FILE_ATTRIBUTE_NORMAL;
88 io.ntcreatex.in.share_access = NTCREATEX_SHARE_ACCESS_READ | NTCREATEX_SHARE_ACCESS_WRITE;
89 io.ntcreatex.in.alloc_size = 0;
90 io.ntcreatex.in.open_disposition = NTCREATEX_DISP_OPEN;
91 io.ntcreatex.in.impersonation = NTCREATEX_IMPERSONATION_ANONYMOUS;
92 io.ntcreatex.in.security_flags = 0;
93 io.ntcreatex.in.fname = BASEDIR;
95 status = smb_raw_open(cli->tree, mem_ctx, &io);
96 CHECK_STATUS(status, NT_STATUS_OK);
97 fnum = io.ntcreatex.file.fnum;
99 status = smb_raw_open(cli->tree, mem_ctx, &io);
100 CHECK_STATUS(status, NT_STATUS_OK);
101 fnum2 = io.ntcreatex.file.fnum;
103 /* ask for a change notify */
104 notify.notify.in.buffer_size = 1000;
105 notify.notify.in.completion_filter = 0x3;
106 notify.notify.file.fnum = fnum;
107 notify.notify.in.recursive = True;
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.notify.out.num_changes, 1);
118 CHECK_VAL(notify.notify.out.changes[0].action, NOTIFY_ACTION_ADDED);
119 CHECK_WSTR(notify.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.notify.out.num_changes, 1);
129 CHECK_VAL(notify.notify.out.changes[0].action, NOTIFY_ACTION_REMOVED);
130 CHECK_WSTR(notify.notify.out.changes[0].name, "subdir-name", STR_UNICODE);
132 printf("testing notify cancel\n");
134 req = smb_raw_changenotify_send(cli->tree, ¬ify);
135 smb_raw_ntcancel(req);
136 smbcli_mkdir(cli->tree, BASEDIR "\\subdir-name");
137 status = smb_raw_changenotify_recv(req, mem_ctx, ¬ify);
138 CHECK_STATUS(status, NT_STATUS_CANCELLED);
140 count = torture_numops;
141 printf("testing buffered notify on create of %d files\n", count);
142 for (i=0;i<count;i++) {
143 char *fname = talloc_asprintf(cli, BASEDIR "\\test%d.txt", i);
144 int fnum3 = smbcli_open(cli->tree, fname, O_CREAT|O_RDWR, DENY_NONE);
146 printf("Failed to create %s - %s\n",
147 fname, smbcli_errstr(cli->tree));
152 smbcli_close(cli->tree, fnum3);
155 /* setup a new notify on a different directory handle. This
156 new notify won't see the events above. */
157 notify.notify.file.fnum = fnum2;
158 req2 = smb_raw_changenotify_send(cli->tree, ¬ify);
160 /* whereas this notify will see the above buffered events as
162 notify.notify.file.fnum = fnum;
163 req = smb_raw_changenotify_send(cli->tree, ¬ify);
165 status = smbcli_unlink(cli->tree, BASEDIR "\\test0.txt");
166 CHECK_STATUS(status, NT_STATUS_OK);
168 /* receive the reply from the 2nd notify */
169 status = smb_raw_changenotify_recv(req, mem_ctx, ¬ify);
170 CHECK_STATUS(status, NT_STATUS_OK);
172 CHECK_VAL(notify.notify.out.num_changes, count+1);
173 for (i=0;i<notify.notify.out.num_changes;i++) {
174 CHECK_VAL(notify.notify.out.changes[i].action, NOTIFY_ACTION_ADDED);
176 CHECK_WSTR(notify.notify.out.changes[0].name, "subdir-name", STR_UNICODE);
178 /* and now from the 1st notify */
179 status = smb_raw_changenotify_recv(req2, mem_ctx, ¬ify);
180 CHECK_STATUS(status, NT_STATUS_OK);
181 CHECK_VAL(notify.notify.out.num_changes, 1);
183 printf("testing notify on wildcard unlink for %d files\n", count);
185 req = smb_raw_changenotify_send(cli->tree, ¬ify);
186 status = smbcli_unlink(cli->tree, BASEDIR "\\test*.txt");
187 CHECK_STATUS(status, NT_STATUS_OK);
189 status = smb_raw_changenotify_recv(req, mem_ctx, ¬ify);
190 CHECK_STATUS(status, NT_STATUS_OK);
192 CHECK_VAL(notify.notify.out.num_changes, 1);
193 CHECK_VAL(notify.notify.out.changes[0].action, NOTIFY_ACTION_REMOVED);
194 CHECK_WSTR(notify.notify.out.changes[0].name, "test0.txt", STR_UNICODE);
196 /* and we now see the rest of the unlink calls */
197 req = smb_raw_changenotify_send(cli->tree, ¬ify);
198 status = smb_raw_changenotify_recv(req, mem_ctx, ¬ify);
199 CHECK_STATUS(status, NT_STATUS_OK);
200 CHECK_VAL(notify.notify.out.num_changes, count-1);
201 for (i=0;i<notify.notify.out.num_changes;i++) {
202 CHECK_VAL(notify.notify.out.changes[i].action, NOTIFY_ACTION_REMOVED);
205 notify.notify.file.fnum = fnum2;
206 req = smb_raw_changenotify_send(cli->tree, ¬ify);
207 status = smb_raw_changenotify_recv(req, mem_ctx, ¬ify);
208 CHECK_STATUS(status, NT_STATUS_OK);
209 CHECK_VAL(notify.notify.out.num_changes, count-1);
210 for (i=0;i<notify.notify.out.num_changes;i++) {
211 CHECK_VAL(notify.notify.out.changes[i].action, NOTIFY_ACTION_REMOVED);
215 smb_raw_exit(cli->session);
216 smbcli_deltree(cli->tree, BASEDIR);
217 torture_close_connection(cli);
218 talloc_free(mem_ctx);