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 "lib/events/events.h"
31 #define CHECK_STATUS(status, correct) do { \
32 if (!NT_STATUS_EQUAL(status, correct)) { \
33 printf("(%s) Incorrect status %s - should be %s\n", \
34 __location__, nt_errstr(status), nt_errstr(correct)); \
39 #define CHECK_VALUE(v, correct) do { \
40 if ((v) != (correct)) { \
41 printf("(%s) Incorrect value %s=%d - should be %d\n", \
42 __location__, #v, v, correct); \
47 #define CHECK_WIRE_STR(field, value) do { \
48 if (!field.s || strcmp(field.s, value)) { \
49 printf("(%s) %s [%s] != %s\n", \
50 __location__, #field, field.s, value); \
55 #define FNAME "smb2-notify01.dat"
57 #define TARGET_IS_WIN7(_tctx) (torture_setting_bool(_tctx, "win7", false))
59 static bool test_valid_request(struct torture_context *tctx, struct smb2_tree *tree)
63 struct smb2_handle dh;
65 struct smb2_request *req;
66 uint32_t max_buffer_size = 0x00080000;
68 if (TARGET_IS_WIN7(tctx)) {
69 max_buffer_size = 0x00010000;
72 smb2_util_unlink(tree, FNAME);
74 status = smb2_util_roothandle(tree, &dh);
75 CHECK_STATUS(status, NT_STATUS_OK);
77 n.in.recursive = 0x0000;
78 n.in.buffer_size = max_buffer_size;
79 n.in.file.handle = dh;
80 n.in.completion_filter = 0x00000FFF;
81 n.in.unknown = 0x00000000;
82 req = smb2_notify_send(tree, &n);
84 while (!req->cancel.can_cancel && req->state <= SMB2_REQUEST_RECV) {
85 if (event_loop_once(req->transport->socket->event.ctx) != 0) {
90 status = torture_setup_complex_file(tree, FNAME);
91 CHECK_STATUS(status, NT_STATUS_OK);
93 status = smb2_notify_recv(req, tctx, &n);
94 CHECK_STATUS(status, NT_STATUS_OK);
95 CHECK_VALUE(n.out.num_changes, 1);
96 CHECK_VALUE(n.out.changes[0].action, NOTIFY_ACTION_ADDED);
97 CHECK_WIRE_STR(n.out.changes[0].name, FNAME);
100 * if the change response doesn't fit in the buffer
101 * NOTIFY_ENUM_DIR is returned.
103 n.in.buffer_size = 0x00000000;
104 req = smb2_notify_send(tree, &n);
106 while (!req->cancel.can_cancel && req->state <= SMB2_REQUEST_RECV) {
107 if (event_loop_once(req->transport->socket->event.ctx) != 0) {
112 status = torture_setup_complex_file(tree, FNAME);
113 CHECK_STATUS(status, NT_STATUS_OK);
115 status = smb2_notify_recv(req, tctx, &n);
116 CHECK_STATUS(status, STATUS_NOTIFY_ENUM_DIR);
119 * if the change response fits in the buffer we get
122 n.in.buffer_size = max_buffer_size;
123 req = smb2_notify_send(tree, &n);
125 while (!req->cancel.can_cancel && req->state <= SMB2_REQUEST_RECV) {
126 if (event_loop_once(req->transport->socket->event.ctx) != 0) {
131 status = torture_setup_complex_file(tree, FNAME);
132 CHECK_STATUS(status, NT_STATUS_OK);
134 status = smb2_notify_recv(req, tctx, &n);
135 CHECK_STATUS(status, NT_STATUS_OK);
136 CHECK_VALUE(n.out.num_changes, 3);
137 CHECK_VALUE(n.out.changes[0].action, NOTIFY_ACTION_REMOVED);
138 CHECK_WIRE_STR(n.out.changes[0].name, FNAME);
139 CHECK_VALUE(n.out.changes[1].action, NOTIFY_ACTION_ADDED);
140 CHECK_WIRE_STR(n.out.changes[1].name, FNAME);
141 CHECK_VALUE(n.out.changes[2].action, NOTIFY_ACTION_MODIFIED);
142 CHECK_WIRE_STR(n.out.changes[2].name, FNAME);
144 /* if the first notify returns NOTIFY_ENUM_DIR, all do */
145 status = smb2_util_close(tree, dh);
146 CHECK_STATUS(status, NT_STATUS_OK);
147 status = smb2_util_roothandle(tree, &dh);
148 CHECK_STATUS(status, NT_STATUS_OK);
150 n.in.recursive = 0x0000;
151 n.in.buffer_size = 0x00000001;
152 n.in.file.handle = dh;
153 n.in.completion_filter = 0x00000FFF;
154 n.in.unknown = 0x00000000;
155 req = smb2_notify_send(tree, &n);
157 while (!req->cancel.can_cancel && req->state <= SMB2_REQUEST_RECV) {
158 if (event_loop_once(req->transport->socket->event.ctx) != 0) {
163 status = torture_setup_complex_file(tree, FNAME);
164 CHECK_STATUS(status, NT_STATUS_OK);
166 status = smb2_notify_recv(req, tctx, &n);
167 CHECK_STATUS(status, STATUS_NOTIFY_ENUM_DIR);
169 n.in.buffer_size = max_buffer_size;
170 req = smb2_notify_send(tree, &n);
171 while (!req->cancel.can_cancel && req->state <= SMB2_REQUEST_RECV) {
172 if (event_loop_once(req->transport->socket->event.ctx) != 0) {
177 status = torture_setup_complex_file(tree, FNAME);
178 CHECK_STATUS(status, NT_STATUS_OK);
180 status = smb2_notify_recv(req, tctx, &n);
181 CHECK_STATUS(status, STATUS_NOTIFY_ENUM_DIR);
183 /* if the buffer size is too large, we get invalid parameter */
184 n.in.recursive = 0x0000;
185 n.in.buffer_size = max_buffer_size + 1;
186 n.in.file.handle = dh;
187 n.in.completion_filter = 0x00000FFF;
188 n.in.unknown = 0x00000000;
189 req = smb2_notify_send(tree, &n);
190 status = smb2_notify_recv(req, tctx, &n);
191 CHECK_STATUS(status, NT_STATUS_INVALID_PARAMETER);
197 /* basic testing of SMB2 notify
199 bool torture_smb2_notify(struct torture_context *torture)
201 struct smb2_tree *tree;
204 if (!torture_smb2_connection(torture, &tree)) {
208 ret &= test_valid_request(torture, tree);