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 10 : static NTSTATUS torture_smb2_close(struct torture_context *tctx,
32 : struct smb2_tree *tree,
33 : struct smb2_handle handle)
34 : {
35 : struct smb2_close io;
36 : NTSTATUS status;
37 10 : TALLOC_CTX *tmp_ctx = talloc_new(tree);
38 :
39 10 : ZERO_STRUCT(io);
40 10 : io.in.file.handle = handle;
41 10 : io.in.flags = SMB2_CLOSE_FLAGS_FULL_INFORMATION;
42 10 : status = smb2_close(tree, &io);
43 10 : 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 10 : if (DEBUGLVL(1)) {
49 10 : torture_comment(tctx, "Close gave:\n");
50 10 : torture_comment(tctx, "create_time = %s\n", nt_time_string(tmp_ctx, io.out.create_time));
51 10 : torture_comment(tctx, "access_time = %s\n", nt_time_string(tmp_ctx, io.out.access_time));
52 10 : torture_comment(tctx, "write_time = %s\n", nt_time_string(tmp_ctx, io.out.write_time));
53 10 : torture_comment(tctx, "change_time = %s\n", nt_time_string(tmp_ctx, io.out.change_time));
54 10 : torture_comment(tctx, "alloc_size = %lld\n", (long long)io.out.alloc_size);
55 10 : torture_comment(tctx, "size = %lld\n", (long long)io.out.size);
56 10 : torture_comment(tctx, "file_attr = 0x%x\n", io.out.file_attr);
57 : }
58 :
59 10 : talloc_free(tmp_ctx);
60 :
61 10 : return status;
62 : }
63 :
64 :
65 : /*
66 : test writing
67 : */
68 5 : static NTSTATUS torture_smb2_write(struct torture_context *tctx, struct smb2_tree *tree, struct smb2_handle handle)
69 : {
70 : struct smb2_write w;
71 : struct smb2_read r;
72 : struct smb2_flush f;
73 : NTSTATUS status;
74 : DATA_BLOB data;
75 : int i;
76 5 : uint32_t size = torture_setting_int(tctx, "smb2maxwrite", 64*1024);
77 :
78 5 : data = data_blob_talloc(tree, NULL, size);
79 5 : 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 327685 : for (i=0;i<data.length;i++) {
85 327680 : data.data[i] = i;
86 : }
87 :
88 5 : ZERO_STRUCT(w);
89 5 : w.in.file.handle = handle;
90 5 : w.in.offset = 0;
91 5 : w.in.data = data;
92 :
93 5 : status = smb2_write(tree, &w);
94 5 : 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 5 : torture_smb2_all_info(tctx, tree, handle);
100 :
101 5 : status = smb2_write(tree, &w);
102 5 : 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 5 : torture_smb2_all_info(tctx, tree, handle);
108 :
109 5 : ZERO_STRUCT(f);
110 5 : f.in.file.handle = handle;
111 :
112 5 : status = smb2_flush(tree, &f);
113 5 : 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 5 : ZERO_STRUCT(r);
119 5 : r.in.file.handle = handle;
120 5 : r.in.length = data.length;
121 5 : r.in.offset = 0;
122 :
123 5 : status = smb2_read(tree, tree, &r);
124 5 : 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 9 : if (data.length != r.out.data.length ||
130 5 : 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 5 : return status;
136 : }
137 :
138 :
139 : /*
140 : send a create
141 : */
142 10 : NTSTATUS torture_smb2_createfile(struct torture_context *tctx,
143 : struct smb2_tree *tree,
144 : const char *fname,
145 : struct smb2_handle *handle)
146 : {
147 : struct smb2_create io;
148 : NTSTATUS status;
149 10 : TALLOC_CTX *tmp_ctx = talloc_new(tree);
150 :
151 10 : ZERO_STRUCT(io);
152 10 : io.in.oplock_level = 0;
153 10 : io.in.desired_access = SEC_RIGHTS_FILE_ALL;
154 10 : io.in.file_attributes = FILE_ATTRIBUTE_NORMAL;
155 10 : io.in.create_disposition = NTCREATEX_DISP_OPEN_IF;
156 10 : io.in.share_access =
157 : NTCREATEX_SHARE_ACCESS_DELETE|
158 : NTCREATEX_SHARE_ACCESS_READ|
159 : NTCREATEX_SHARE_ACCESS_WRITE;
160 10 : io.in.create_options = NTCREATEX_OPTIONS_WRITE_THROUGH;
161 10 : io.in.fname = fname;
162 :
163 10 : status = smb2_create(tree, tmp_ctx, &io);
164 10 : 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 10 : if (DEBUGLVL(1)) {
171 10 : torture_comment(tctx, "Open gave:\n");
172 10 : torture_comment(tctx, "oplock_flags = 0x%x\n", io.out.oplock_level);
173 10 : torture_comment(tctx, "create_action = 0x%x\n", io.out.create_action);
174 10 : torture_comment(tctx, "create_time = %s\n", nt_time_string(tmp_ctx, io.out.create_time));
175 10 : torture_comment(tctx, "access_time = %s\n", nt_time_string(tmp_ctx, io.out.access_time));
176 10 : torture_comment(tctx, "write_time = %s\n", nt_time_string(tmp_ctx, io.out.write_time));
177 10 : torture_comment(tctx, "change_time = %s\n", nt_time_string(tmp_ctx, io.out.change_time));
178 10 : torture_comment(tctx, "alloc_size = %lld\n", (long long)io.out.alloc_size);
179 10 : torture_comment(tctx, "size = %lld\n", (long long)io.out.size);
180 10 : torture_comment(tctx, "file_attr = 0x%x\n", io.out.file_attr);
181 18 : torture_comment(tctx, "handle = %016llx%016llx\n",
182 10 : (long long)io.out.file.handle.data[0],
183 10 : (long long)io.out.file.handle.data[1]);
184 : }
185 :
186 10 : talloc_free(tmp_ctx);
187 :
188 10 : *handle = io.out.file.handle;
189 :
190 10 : return NT_STATUS_OK;
191 : }
192 :
193 :
194 : /*
195 : basic testing of SMB2 connection calls
196 : */
197 5 : bool torture_smb2_connect(struct torture_context *tctx)
198 : {
199 5 : TALLOC_CTX *mem_ctx = talloc_new(tctx);
200 : struct smb2_tree *tree;
201 : struct smb2_request *req;
202 : struct smb2_handle h1, h2;
203 : NTSTATUS status;
204 : bool ok;
205 :
206 5 : ok = torture_smb2_connection(tctx, &tree);
207 5 : torture_assert(tctx, ok, "torture_smb2_connection failed");
208 :
209 5 : smb2_util_unlink(tree, "test9.dat");
210 :
211 5 : status = torture_smb2_createfile(tctx, tree, "test9.dat", &h1);
212 5 : torture_assert_ntstatus_ok(tctx, status, "create failed");
213 :
214 5 : status = torture_smb2_createfile(tctx, tree, "test9.dat", &h2);
215 5 : torture_assert_ntstatus_ok(tctx, status, "create failed");
216 :
217 5 : status = torture_smb2_write(tctx, tree, h1);
218 5 : torture_assert_ntstatus_ok(tctx, status, "write failed");
219 :
220 5 : status = torture_smb2_close(tctx, tree, h1);
221 5 : torture_assert_ntstatus_ok(tctx, status, "close failed");
222 :
223 5 : status = torture_smb2_close(tctx, tree, h2);
224 5 : torture_assert_ntstatus_ok(tctx, status, "close failed");
225 :
226 5 : status = smb2_util_close(tree, h1);
227 5 : torture_assert_ntstatus_equal(tctx, status, NT_STATUS_FILE_CLOSED,
228 : "close should have closed the handle");
229 :
230 5 : status = smb2_tdis(tree);
231 5 : torture_assert_ntstatus_ok(tctx, status, "tdis failed");
232 :
233 5 : status = smb2_tdis(tree);
234 5 : torture_assert_ntstatus_equal(tctx, status,
235 : NT_STATUS_NETWORK_NAME_DELETED,
236 : "tdis should have closed the tcon");
237 :
238 5 : status = smb2_logoff(tree->session);
239 5 : torture_assert_ntstatus_ok(tctx, status, "logoff failed");
240 :
241 5 : req = smb2_logoff_send(tree->session);
242 5 : torture_assert_not_null(tctx, req, "smb2_logoff_send failed");
243 :
244 5 : req->session = NULL;
245 :
246 5 : status = smb2_logoff_recv(req);
247 :
248 5 : torture_assert_ntstatus_equal(tctx, status, NT_STATUS_USER_SESSION_DELETED,
249 : "logoff should have disabled session");
250 :
251 5 : status = smb2_keepalive(tree->session->transport);
252 5 : torture_assert_ntstatus_ok(tctx, status, "keepalive failed");
253 :
254 5 : talloc_free(mem_ctx);
255 :
256 5 : return true;
257 : }
|