2 Unix SMB/CIFS implementation.
6 Copyright (C) Stefan Metzmacher 2006
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>.
23 #include "libcli/smb2/smb2.h"
24 #include "libcli/smb2/smb2_calls.h"
26 #include "torture/torture.h"
27 #include "torture/smb2/proto.h"
29 #include "libcli/raw/libcliraw.h"
30 #include "lib/events/events.h"
32 #define CHECK_STATUS(status, correct) do { \
33 if (!NT_STATUS_EQUAL(status, correct)) { \
34 printf("(%s) Incorrect status %s - should be %s\n", \
35 __location__, nt_errstr(status), nt_errstr(correct)); \
40 #define CHECK_VALUE(v, correct) do { \
41 if ((v) != (correct)) { \
42 printf("(%s) Incorrect value %s=%d - should be %d\n", \
43 __location__, #v, v, correct); \
48 #define CHECK_WIRE_STR(field, value) do { \
49 if (!field.s || strcmp(field.s, value)) { \
50 printf("(%s) %s [%s] != %s\n", \
51 __location__, #field, field.s, value); \
56 #define FNAME "smb2-notify01.dat"
58 static BOOL test_valid_request(TALLOC_CTX *mem_ctx, struct smb2_tree *tree)
62 struct smb2_handle dh;
64 struct smb2_request *req;
66 status = smb2_util_roothandle(tree, &dh);
67 CHECK_STATUS(status, NT_STATUS_OK);
69 n.in.recursive = 0x0000;
70 n.in.buffer_size = 0x00080000;
71 n.in.file.handle = dh;
72 n.in.completion_filter = 0x00000FFF;
73 n.in.unknown = 0x00000000;
74 req = smb2_notify_send(tree, &n);
76 while (!req->cancel.can_cancel && req->state <= SMB2_REQUEST_RECV) {
77 if (event_loop_once(req->transport->socket->event.ctx) != 0) {
82 status = torture_setup_complex_file(tree, FNAME);
83 CHECK_STATUS(status, NT_STATUS_OK);
85 status = smb2_notify_recv(req, mem_ctx, &n);
86 CHECK_STATUS(status, NT_STATUS_OK);
87 CHECK_VALUE(n.out.num_changes, 1);
88 CHECK_VALUE(n.out.changes[0].action, NOTIFY_ACTION_REMOVED);
89 CHECK_WIRE_STR(n.out.changes[0].name, FNAME);
92 * if the change response doesn't fit in the buffer
93 * NOTIFY_ENUM_DIR is returned.
95 n.in.buffer_size = 0x00000000;
96 req = smb2_notify_send(tree, &n);
98 while (!req->cancel.can_cancel && req->state <= SMB2_REQUEST_RECV) {
99 if (event_loop_once(req->transport->socket->event.ctx) != 0) {
104 status = torture_setup_complex_file(tree, FNAME);
105 CHECK_STATUS(status, NT_STATUS_OK);
107 status = smb2_notify_recv(req, mem_ctx, &n);
108 CHECK_STATUS(status, STATUS_NOTIFY_ENUM_DIR);
111 * if the change response fits in the buffer we get
114 n.in.buffer_size = 0x00080000;
115 req = smb2_notify_send(tree, &n);
117 while (!req->cancel.can_cancel && req->state <= SMB2_REQUEST_RECV) {
118 if (event_loop_once(req->transport->socket->event.ctx) != 0) {
123 status = torture_setup_complex_file(tree, FNAME);
124 CHECK_STATUS(status, NT_STATUS_OK);
126 status = smb2_notify_recv(req, mem_ctx, &n);
127 CHECK_STATUS(status, NT_STATUS_OK);
128 CHECK_VALUE(n.out.num_changes, 3);
129 CHECK_VALUE(n.out.changes[0].action, NOTIFY_ACTION_REMOVED);
130 CHECK_WIRE_STR(n.out.changes[0].name, FNAME);
131 CHECK_VALUE(n.out.changes[1].action, NOTIFY_ACTION_ADDED);
132 CHECK_WIRE_STR(n.out.changes[1].name, FNAME);
133 CHECK_VALUE(n.out.changes[2].action, NOTIFY_ACTION_MODIFIED);
134 CHECK_WIRE_STR(n.out.changes[2].name, FNAME);
136 /* if the first notify returns NOTIFY_ENUM_DIR, all do */
137 status = smb2_util_close(tree, dh);
138 CHECK_STATUS(status, NT_STATUS_OK);
139 status = smb2_util_roothandle(tree, &dh);
140 CHECK_STATUS(status, NT_STATUS_OK);
142 n.in.recursive = 0x0000;
143 n.in.buffer_size = 0x00000001;
144 n.in.file.handle = dh;
145 n.in.completion_filter = 0x00000FFF;
146 n.in.unknown = 0x00000000;
147 req = smb2_notify_send(tree, &n);
149 while (!req->cancel.can_cancel && req->state <= SMB2_REQUEST_RECV) {
150 if (event_loop_once(req->transport->socket->event.ctx) != 0) {
155 status = torture_setup_complex_file(tree, FNAME);
156 CHECK_STATUS(status, NT_STATUS_OK);
158 status = smb2_notify_recv(req, mem_ctx, &n);
159 CHECK_STATUS(status, STATUS_NOTIFY_ENUM_DIR);
161 n.in.buffer_size = 0x00080000;
162 req = smb2_notify_send(tree, &n);
163 while (!req->cancel.can_cancel && req->state <= SMB2_REQUEST_RECV) {
164 if (event_loop_once(req->transport->socket->event.ctx) != 0) {
169 status = torture_setup_complex_file(tree, FNAME);
170 CHECK_STATUS(status, NT_STATUS_OK);
172 status = smb2_notify_recv(req, mem_ctx, &n);
173 CHECK_STATUS(status, STATUS_NOTIFY_ENUM_DIR);
175 /* if the buffer size is too large, we get invalid parameter */
176 n.in.recursive = 0x0000;
177 n.in.buffer_size = 0x00080001;
178 n.in.file.handle = dh;
179 n.in.completion_filter = 0x00000FFF;
180 n.in.unknown = 0x00000000;
181 req = smb2_notify_send(tree, &n);
182 status = smb2_notify_recv(req, mem_ctx, &n);
183 CHECK_STATUS(status, NT_STATUS_INVALID_PARAMETER);
189 /* basic testing of SMB2 notify
191 BOOL torture_smb2_notify(struct torture_context *torture)
193 TALLOC_CTX *mem_ctx = talloc_new(NULL);
194 struct smb2_tree *tree;
197 if (!torture_smb2_connection(mem_ctx, &tree)) {
201 ret &= test_valid_request(mem_ctx, tree);
203 talloc_free(mem_ctx);