Line data Source code
1 : /*
2 : Unix SMB/CIFS implementation.
3 :
4 : test suite for SMB2 connection operations
5 :
6 : Copyright (C) Andrew Tridgell 2005
7 :
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.
12 :
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.
17 :
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/>.
20 : */
21 :
22 : #include "includes.h"
23 : #include "libcli/smb2/smb2.h"
24 : #include "libcli/smb2/smb2_calls.h"
25 : #include "torture/torture.h"
26 : #include "torture/smb2/proto.h"
27 :
28 : /*
29 : send a close
30 : */
31 14 : static NTSTATUS torture_smb2_close(struct torture_context *tctx,
32 : struct smb2_tree *tree,
33 : struct smb2_handle handle)
34 : {
35 0 : struct smb2_close io;
36 0 : NTSTATUS status;
37 14 : TALLOC_CTX *tmp_ctx = talloc_new(tree);
38 :
39 14 : ZERO_STRUCT(io);
40 14 : io.in.file.handle = handle;
41 14 : io.in.flags = SMB2_CLOSE_FLAGS_FULL_INFORMATION;
42 14 : status = smb2_close(tree, &io);
43 14 : if (!NT_STATUS_IS_OK(status)) {
44 0 : torture_comment(tctx, "close failed - %s\n", nt_errstr(status));
45 0 : return status;
46 : }
47 :
48 14 : if (DEBUGLVL(1)) {
49 14 : torture_comment(tctx, "Close gave:\n");
50 14 : torture_comment(tctx, "create_time = %s\n", nt_time_string(tmp_ctx, io.out.create_time));
51 14 : torture_comment(tctx, "access_time = %s\n", nt_time_string(tmp_ctx, io.out.access_time));
52 14 : torture_comment(tctx, "write_time = %s\n", nt_time_string(tmp_ctx, io.out.write_time));
53 14 : torture_comment(tctx, "change_time = %s\n", nt_time_string(tmp_ctx, io.out.change_time));
54 14 : torture_comment(tctx, "alloc_size = %lld\n", (long long)io.out.alloc_size);
55 14 : torture_comment(tctx, "size = %lld\n", (long long)io.out.size);
56 14 : torture_comment(tctx, "file_attr = 0x%x\n", io.out.file_attr);
57 : }
58 :
59 14 : talloc_free(tmp_ctx);
60 :
61 14 : return status;
62 : }
63 :
64 :
65 : /*
66 : test writing
67 : */
68 7 : static NTSTATUS torture_smb2_write(struct torture_context *tctx, struct smb2_tree *tree, struct smb2_handle handle)
69 : {
70 0 : struct smb2_write w;
71 0 : struct smb2_read r;
72 0 : struct smb2_flush f;
73 0 : NTSTATUS status;
74 0 : DATA_BLOB data;
75 0 : int i;
76 7 : uint32_t size = torture_setting_int(tctx, "smb2maxwrite", 64*1024);
77 :
78 7 : data = data_blob_talloc(tree, NULL, size);
79 7 : if (size != data.length) {
80 0 : torture_comment(tctx, "data_blob_talloc(%u) failed\n", (unsigned int)size);
81 0 : return NT_STATUS_NO_MEMORY;
82 : }
83 :
84 458759 : for (i=0;i<data.length;i++) {
85 458752 : data.data[i] = i;
86 : }
87 :
88 7 : ZERO_STRUCT(w);
89 7 : w.in.file.handle = handle;
90 7 : w.in.offset = 0;
91 7 : w.in.data = data;
92 :
93 7 : status = smb2_write(tree, &w);
94 7 : if (!NT_STATUS_IS_OK(status)) {
95 0 : torture_comment(tctx, "write 1 failed - %s\n", nt_errstr(status));
96 0 : return status;
97 : }
98 :
99 7 : torture_smb2_all_info(tctx, tree, handle);
100 :
101 7 : status = smb2_write(tree, &w);
102 7 : if (!NT_STATUS_IS_OK(status)) {
103 0 : torture_comment(tctx, "write 2 failed - %s\n", nt_errstr(status));
104 0 : return status;
105 : }
106 :
107 7 : torture_smb2_all_info(tctx, tree, handle);
108 :
109 7 : ZERO_STRUCT(f);
110 7 : f.in.file.handle = handle;
111 :
112 7 : status = smb2_flush(tree, &f);
113 7 : if (!NT_STATUS_IS_OK(status)) {
114 0 : torture_comment(tctx, "flush failed - %s\n", nt_errstr(status));
115 0 : return status;
116 : }
117 :
118 7 : ZERO_STRUCT(r);
119 7 : r.in.file.handle = handle;
120 7 : r.in.length = data.length;
121 7 : r.in.offset = 0;
122 :
123 7 : status = smb2_read(tree, tree, &r);
124 7 : if (!NT_STATUS_IS_OK(status)) {
125 0 : torture_comment(tctx, "read failed - %s\n", nt_errstr(status));
126 0 : return status;
127 : }
128 :
129 7 : if (data.length != r.out.data.length ||
130 7 : memcmp(data.data, r.out.data.data, data.length) != 0) {
131 0 : torture_comment(tctx, "read data mismatch\n");
132 0 : return NT_STATUS_NET_WRITE_FAULT;
133 : }
134 :
135 7 : return status;
136 : }
137 :
138 :
139 : /*
140 : send a create
141 : */
142 14 : NTSTATUS torture_smb2_createfile(struct torture_context *tctx,
143 : struct smb2_tree *tree,
144 : const char *fname,
145 : struct smb2_handle *handle)
146 : {
147 0 : struct smb2_create io;
148 0 : NTSTATUS status;
149 14 : TALLOC_CTX *tmp_ctx = talloc_new(tree);
150 :
151 14 : ZERO_STRUCT(io);
152 14 : io.in.oplock_level = 0;
153 14 : io.in.desired_access = SEC_RIGHTS_FILE_ALL;
154 14 : io.in.file_attributes = FILE_ATTRIBUTE_NORMAL;
155 14 : io.in.create_disposition = NTCREATEX_DISP_OPEN_IF;
156 14 : io.in.share_access =
157 : NTCREATEX_SHARE_ACCESS_DELETE|
158 : NTCREATEX_SHARE_ACCESS_READ|
159 : NTCREATEX_SHARE_ACCESS_WRITE;
160 14 : io.in.create_options = NTCREATEX_OPTIONS_WRITE_THROUGH;
161 14 : io.in.fname = fname;
162 :
163 14 : status = smb2_create(tree, tmp_ctx, &io);
164 14 : if (!NT_STATUS_IS_OK(status)) {
165 0 : TALLOC_FREE(tmp_ctx);
166 0 : torture_comment(tctx, "create1 failed - %s\n", nt_errstr(status));
167 0 : return status;
168 : }
169 :
170 14 : if (DEBUGLVL(1)) {
171 14 : torture_comment(tctx, "Open gave:\n");
172 14 : torture_comment(tctx, "oplock_flags = 0x%x\n", io.out.oplock_level);
173 14 : torture_comment(tctx, "create_action = 0x%x\n", io.out.create_action);
174 14 : torture_comment(tctx, "create_time = %s\n", nt_time_string(tmp_ctx, io.out.create_time));
175 14 : torture_comment(tctx, "access_time = %s\n", nt_time_string(tmp_ctx, io.out.access_time));
176 14 : torture_comment(tctx, "write_time = %s\n", nt_time_string(tmp_ctx, io.out.write_time));
177 14 : torture_comment(tctx, "change_time = %s\n", nt_time_string(tmp_ctx, io.out.change_time));
178 14 : torture_comment(tctx, "alloc_size = %lld\n", (long long)io.out.alloc_size);
179 14 : torture_comment(tctx, "size = %lld\n", (long long)io.out.size);
180 14 : torture_comment(tctx, "file_attr = 0x%x\n", io.out.file_attr);
181 14 : torture_comment(tctx, "handle = %016llx%016llx\n",
182 14 : (long long)io.out.file.handle.data[0],
183 14 : (long long)io.out.file.handle.data[1]);
184 : }
185 :
186 14 : talloc_free(tmp_ctx);
187 :
188 14 : *handle = io.out.file.handle;
189 :
190 14 : return NT_STATUS_OK;
191 : }
192 :
193 :
194 : /*
195 : basic testing of SMB2 connection calls
196 : */
197 7 : bool torture_smb2_connect(struct torture_context *tctx)
198 : {
199 7 : TALLOC_CTX *mem_ctx = talloc_new(tctx);
200 0 : struct smb2_tree *tree;
201 0 : struct smb2_request *req;
202 0 : struct smb2_handle h1, h2;
203 0 : NTSTATUS status;
204 0 : bool ok;
205 :
206 7 : ok = torture_smb2_connection(tctx, &tree);
207 7 : torture_assert(tctx, ok, "torture_smb2_connection failed");
208 :
209 7 : smb2_util_unlink(tree, "test9.dat");
210 :
211 7 : status = torture_smb2_createfile(tctx, tree, "test9.dat", &h1);
212 7 : torture_assert_ntstatus_ok(tctx, status, "create failed");
213 :
214 7 : status = torture_smb2_createfile(tctx, tree, "test9.dat", &h2);
215 7 : torture_assert_ntstatus_ok(tctx, status, "create failed");
216 :
217 7 : status = torture_smb2_write(tctx, tree, h1);
218 7 : torture_assert_ntstatus_ok(tctx, status, "write failed");
219 :
220 7 : status = torture_smb2_close(tctx, tree, h1);
221 7 : torture_assert_ntstatus_ok(tctx, status, "close failed");
222 :
223 7 : status = torture_smb2_close(tctx, tree, h2);
224 7 : torture_assert_ntstatus_ok(tctx, status, "close failed");
225 :
226 7 : status = smb2_util_close(tree, h1);
227 7 : torture_assert_ntstatus_equal(tctx, status, NT_STATUS_FILE_CLOSED,
228 : "close should have closed the handle");
229 :
230 7 : status = smb2_tdis(tree);
231 7 : torture_assert_ntstatus_ok(tctx, status, "tdis failed");
232 :
233 7 : status = smb2_tdis(tree);
234 7 : torture_assert_ntstatus_equal(tctx, status,
235 : NT_STATUS_NETWORK_NAME_DELETED,
236 : "tdis should have closed the tcon");
237 :
238 7 : status = smb2_logoff(tree->session);
239 7 : torture_assert_ntstatus_ok(tctx, status, "logoff failed");
240 :
241 7 : req = smb2_logoff_send(tree->session);
242 7 : torture_assert_not_null(tctx, req, "smb2_logoff_send failed");
243 :
244 7 : req->session = NULL;
245 :
246 7 : status = smb2_logoff_recv(req);
247 :
248 7 : torture_assert_ntstatus_equal(tctx, status, NT_STATUS_USER_SESSION_DELETED,
249 : "logoff should have disabled session");
250 :
251 7 : status = smb2_keepalive(tree->session->transport);
252 7 : torture_assert_ntstatus_ok(tctx, status, "keepalive failed");
253 :
254 7 : talloc_free(mem_ctx);
255 :
256 7 : return true;
257 : }
|