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