talloc: Fix exports and increment talloc version
[Samba/ekacnet.git] / source4 / torture / smb2 / connect.c
blobfd32b52111b3f6c44f3343989a55fa4225a78cbd
1 /*
2 Unix SMB/CIFS implementation.
4 test suite for SMB2 connection operations
6 Copyright (C) Andrew Tridgell 2005
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/>.
22 #include "includes.h"
23 #include "librpc/gen_ndr/security.h"
24 #include "libcli/smb2/smb2.h"
25 #include "libcli/smb2/smb2_calls.h"
26 #include "torture/torture.h"
27 #include "torture/smb2/proto.h"
30 send a close
32 static NTSTATUS torture_smb2_close(struct smb2_tree *tree, struct smb2_handle handle)
34 struct smb2_close io;
35 NTSTATUS status;
36 TALLOC_CTX *tmp_ctx = talloc_new(tree);
38 ZERO_STRUCT(io);
39 io.in.file.handle = handle;
40 io.in.flags = SMB2_CLOSE_FLAGS_FULL_INFORMATION;
41 status = smb2_close(tree, &io);
42 if (!NT_STATUS_IS_OK(status)) {
43 printf("close failed - %s\n", nt_errstr(status));
44 return status;
47 if (DEBUGLVL(1)) {
48 printf("Close gave:\n");
49 printf("create_time = %s\n", nt_time_string(tmp_ctx, io.out.create_time));
50 printf("access_time = %s\n", nt_time_string(tmp_ctx, io.out.access_time));
51 printf("write_time = %s\n", nt_time_string(tmp_ctx, io.out.write_time));
52 printf("change_time = %s\n", nt_time_string(tmp_ctx, io.out.change_time));
53 printf("alloc_size = %lld\n", (long long)io.out.alloc_size);
54 printf("size = %lld\n", (long long)io.out.size);
55 printf("file_attr = 0x%x\n", io.out.file_attr);
58 talloc_free(tmp_ctx);
60 return status;
65 test writing
67 static NTSTATUS torture_smb2_write(struct torture_context *tctx, struct smb2_tree *tree, struct smb2_handle handle)
69 struct smb2_write w;
70 struct smb2_read r;
71 struct smb2_flush f;
72 NTSTATUS status;
73 DATA_BLOB data;
74 int i;
76 if (torture_setting_bool(tctx, "dangerous", false)) {
77 data = data_blob_talloc(tree, NULL, 160000);
78 } else if (torture_setting_bool(tctx, "samba4", false)) {
79 data = data_blob_talloc(tree, NULL, UINT16_MAX);
80 } else {
81 data = data_blob_talloc(tree, NULL, torture_setting_int(tctx, "smb2maxwrite", 120000));
83 for (i=0;i<data.length;i++) {
84 data.data[i] = i;
87 ZERO_STRUCT(w);
88 w.in.file.handle = handle;
89 w.in.offset = 0;
90 w.in.data = data;
92 status = smb2_write(tree, &w);
93 if (!NT_STATUS_IS_OK(status)) {
94 printf("write failed - %s\n", nt_errstr(status));
95 return status;
98 torture_smb2_all_info(tree, handle);
100 status = smb2_write(tree, &w);
101 if (!NT_STATUS_IS_OK(status)) {
102 printf("write failed - %s\n", nt_errstr(status));
103 return status;
106 torture_smb2_all_info(tree, handle);
108 ZERO_STRUCT(f);
109 f.in.file.handle = handle;
111 status = smb2_flush(tree, &f);
112 if (!NT_STATUS_IS_OK(status)) {
113 printf("flush failed - %s\n", nt_errstr(status));
114 return status;
117 ZERO_STRUCT(r);
118 r.in.file.handle = handle;
119 r.in.length = data.length;
120 r.in.offset = 0;
122 status = smb2_read(tree, tree, &r);
123 if (!NT_STATUS_IS_OK(status)) {
124 printf("read failed - %s\n", nt_errstr(status));
125 return status;
128 if (data.length != r.out.data.length ||
129 memcmp(data.data, r.out.data.data, data.length) != 0) {
130 printf("read data mismatch\n");
131 return NT_STATUS_NET_WRITE_FAULT;
134 return status;
139 send a create
141 static struct smb2_handle torture_smb2_createfile(struct smb2_tree *tree,
142 const char *fname)
144 struct smb2_create io;
145 NTSTATUS status;
146 TALLOC_CTX *tmp_ctx = talloc_new(tree);
148 ZERO_STRUCT(io);
149 io.in.oplock_level = 0;
150 io.in.desired_access = SEC_RIGHTS_FILE_ALL;
151 io.in.file_attributes = FILE_ATTRIBUTE_NORMAL;
152 io.in.create_disposition = NTCREATEX_DISP_OPEN_IF;
153 io.in.share_access =
154 NTCREATEX_SHARE_ACCESS_DELETE|
155 NTCREATEX_SHARE_ACCESS_READ|
156 NTCREATEX_SHARE_ACCESS_WRITE;
157 io.in.create_options = NTCREATEX_OPTIONS_WRITE_THROUGH;
158 io.in.fname = fname;
160 status = smb2_create(tree, tmp_ctx, &io);
161 if (!NT_STATUS_IS_OK(status)) {
162 printf("create1 failed - %s\n", nt_errstr(status));
163 return io.out.file.handle;
166 if (DEBUGLVL(1)) {
167 printf("Open gave:\n");
168 printf("oplock_flags = 0x%x\n", io.out.oplock_level);
169 printf("create_action = 0x%x\n", io.out.create_action);
170 printf("create_time = %s\n", nt_time_string(tmp_ctx, io.out.create_time));
171 printf("access_time = %s\n", nt_time_string(tmp_ctx, io.out.access_time));
172 printf("write_time = %s\n", nt_time_string(tmp_ctx, io.out.write_time));
173 printf("change_time = %s\n", nt_time_string(tmp_ctx, io.out.change_time));
174 printf("alloc_size = %lld\n", (long long)io.out.alloc_size);
175 printf("size = %lld\n", (long long)io.out.size);
176 printf("file_attr = 0x%x\n", io.out.file_attr);
177 printf("handle = %016llx%016llx\n",
178 (long long)io.out.file.handle.data[0],
179 (long long)io.out.file.handle.data[1]);
182 talloc_free(tmp_ctx);
184 return io.out.file.handle;
189 basic testing of SMB2 connection calls
191 bool torture_smb2_connect(struct torture_context *torture)
193 TALLOC_CTX *mem_ctx = talloc_new(NULL);
194 struct smb2_tree *tree;
195 struct smb2_request *req;
196 struct smb2_handle h1, h2;
197 NTSTATUS status;
199 if (!torture_smb2_connection(torture, &tree)) {
200 return false;
203 smb2_util_unlink(tree, "test9.dat");
205 h1 = torture_smb2_createfile(tree, "test9.dat");
206 h2 = torture_smb2_createfile(tree, "test9.dat");
207 status = torture_smb2_write(torture, tree, h1);
208 if (!NT_STATUS_IS_OK(status)) {
209 printf("Write failed - %s\n", nt_errstr(status));
210 return false;
212 status = torture_smb2_close(tree, h1);
213 if (!NT_STATUS_IS_OK(status)) {
214 printf("Close failed - %s\n", nt_errstr(status));
215 return false;
217 status = torture_smb2_close(tree, h2);
218 if (!NT_STATUS_IS_OK(status)) {
219 printf("Close failed - %s\n", nt_errstr(status));
220 return false;
223 status = smb2_util_close(tree, h1);
224 if (!NT_STATUS_EQUAL(status, NT_STATUS_FILE_CLOSED)) {
225 printf("close should have closed the handle - %s\n", nt_errstr(status));
226 return false;
229 status = smb2_tdis(tree);
230 if (!NT_STATUS_IS_OK(status)) {
231 printf("tdis failed - %s\n", nt_errstr(status));
232 return false;
235 status = smb2_tdis(tree);
236 if (!NT_STATUS_EQUAL(status, NT_STATUS_NETWORK_NAME_DELETED)) {
237 printf("tdis should have disabled session - %s\n", nt_errstr(status));
238 return false;
241 status = smb2_logoff(tree->session);
242 if (!NT_STATUS_IS_OK(status)) {
243 printf("Logoff failed - %s\n", nt_errstr(status));
244 return false;
247 req = smb2_logoff_send(tree->session);
248 if (!req) {
249 printf("smb2_logoff_send() failed\n");
250 return false;
253 req->session = NULL;
255 status = smb2_logoff_recv(req);
256 if (!NT_STATUS_EQUAL(status, NT_STATUS_USER_SESSION_DELETED)) {
257 printf("Logoff should have disabled session - %s\n", nt_errstr(status));
258 return false;
261 status = smb2_keepalive(tree->session->transport);
262 if (!NT_STATUS_IS_OK(status)) {
263 printf("keepalive failed? - %s\n", nt_errstr(status));
264 return false;
267 talloc_free(mem_ctx);
269 return true;