c9a5732d2a24a98329d4e2102d0f1f611ab7184f
[kai/samba.git] / source4 / torture / smb2 / connect.c
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 "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"
28 #include "param/param.h"
29
30 /*
31   send a close
32 */
33 static NTSTATUS torture_smb2_close(struct smb2_tree *tree, struct smb2_handle handle)
34 {
35         struct smb2_close io;
36         NTSTATUS status;
37         TALLOC_CTX *tmp_ctx = talloc_new(tree);
38
39         ZERO_STRUCT(io);
40         io.in.file.handle       = handle;
41         io.in.flags             = SMB2_CLOSE_FLAGS_FULL_INFORMATION;
42         status = smb2_close(tree, &io);
43         if (!NT_STATUS_IS_OK(status)) {
44                 printf("close failed - %s\n", nt_errstr(status));
45                 return status;
46         }
47
48         if (DEBUGLVL(1)) {
49                 printf("Close gave:\n");
50                 printf("create_time     = %s\n", nt_time_string(tmp_ctx, io.out.create_time));
51                 printf("access_time     = %s\n", nt_time_string(tmp_ctx, io.out.access_time));
52                 printf("write_time      = %s\n", nt_time_string(tmp_ctx, io.out.write_time));
53                 printf("change_time     = %s\n", nt_time_string(tmp_ctx, io.out.change_time));
54                 printf("alloc_size      = %lld\n", (long long)io.out.alloc_size);
55                 printf("size            = %lld\n", (long long)io.out.size);
56                 printf("file_attr       = 0x%x\n", io.out.file_attr);
57         }
58
59         talloc_free(tmp_ctx);
60         
61         return status;
62 }
63
64
65 /*
66   test writing
67 */
68 static NTSTATUS torture_smb2_write(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         
77         if (lp_parm_bool(NULL, "torture", "dangerous", false)) {
78                 data = data_blob_talloc(tree, NULL, 160000);
79         } else if (lp_parm_bool(NULL, "torture", "samba4", false)) {
80                 data = data_blob_talloc(tree, NULL, UINT16_MAX);
81         } else {
82                 data = data_blob_talloc(tree, NULL, 120000);
83         }
84         for (i=0;i<data.length;i++) {
85                 data.data[i] = i;
86         }
87
88         ZERO_STRUCT(w);
89         w.in.file.handle = handle;
90         w.in.offset      = 0;
91         w.in.data        = data;
92
93         status = smb2_write(tree, &w);
94         if (!NT_STATUS_IS_OK(status)) {
95                 printf("write failed - %s\n", nt_errstr(status));
96                 return status;
97         }
98
99         torture_smb2_all_info(tree, handle);
100
101         status = smb2_write(tree, &w);
102         if (!NT_STATUS_IS_OK(status)) {
103                 printf("write failed - %s\n", nt_errstr(status));
104                 return status;
105         }
106
107         torture_smb2_all_info(tree, handle);
108
109         ZERO_STRUCT(f);
110         f.in.file.handle = handle;
111
112         status = smb2_flush(tree, &f);
113         if (!NT_STATUS_IS_OK(status)) {
114                 printf("flush failed - %s\n", nt_errstr(status));
115                 return status;
116         }
117
118         ZERO_STRUCT(r);
119         r.in.file.handle = handle;
120         r.in.length      = data.length;
121         r.in.offset      = 0;
122
123         status = smb2_read(tree, tree, &r);
124         if (!NT_STATUS_IS_OK(status)) {
125                 printf("read failed - %s\n", nt_errstr(status));
126                 return status;
127         }
128
129         if (data.length != r.out.data.length ||
130             memcmp(data.data, r.out.data.data, data.length) != 0) {
131                 printf("read data mismatch\n");
132                 return NT_STATUS_NET_WRITE_FAULT;
133         }
134
135         return status;
136 }
137
138
139 /*
140   send a create
141 */
142 static struct smb2_handle torture_smb2_create(struct smb2_tree *tree, 
143                                               const char *fname)
144 {
145         struct smb2_create io;
146         NTSTATUS status;
147         TALLOC_CTX *tmp_ctx = talloc_new(tree);
148
149         ZERO_STRUCT(io);
150         io.in.oplock_flags = 0;
151         io.in.access_mask = SEC_RIGHTS_FILE_ALL;
152         io.in.file_attr   = FILE_ATTRIBUTE_NORMAL;
153         io.in.open_disposition = NTCREATEX_DISP_OPEN_IF;
154         io.in.share_access = 
155                 NTCREATEX_SHARE_ACCESS_DELETE|
156                 NTCREATEX_SHARE_ACCESS_READ|
157                 NTCREATEX_SHARE_ACCESS_WRITE;
158         io.in.create_options = NTCREATEX_OPTIONS_WRITE_THROUGH;
159         io.in.fname = fname;
160
161         status = smb2_create(tree, tmp_ctx, &io);
162         if (!NT_STATUS_IS_OK(status)) {
163                 printf("create1 failed - %s\n", nt_errstr(status));
164                 return io.out.file.handle;
165         }
166
167         if (DEBUGLVL(1)) {
168                 printf("Open gave:\n");
169                 printf("oplock_flags    = 0x%x\n", io.out.oplock_flags);
170                 printf("create_action   = 0x%x\n", io.out.create_action);
171                 printf("create_time     = %s\n", nt_time_string(tmp_ctx, io.out.create_time));
172                 printf("access_time     = %s\n", nt_time_string(tmp_ctx, io.out.access_time));
173                 printf("write_time      = %s\n", nt_time_string(tmp_ctx, io.out.write_time));
174                 printf("change_time     = %s\n", nt_time_string(tmp_ctx, io.out.change_time));
175                 printf("alloc_size      = %lld\n", (long long)io.out.alloc_size);
176                 printf("size            = %lld\n", (long long)io.out.size);
177                 printf("file_attr       = 0x%x\n", io.out.file_attr);
178                 printf("handle          = %016llx%016llx\n", 
179                        (long long)io.out.file.handle.data[0], 
180                        (long long)io.out.file.handle.data[1]);
181         }
182
183         talloc_free(tmp_ctx);
184         
185         return io.out.file.handle;
186 }
187
188
189 /* 
190    basic testing of SMB2 connection calls
191 */
192 BOOL torture_smb2_connect(struct torture_context *torture)
193 {
194         TALLOC_CTX *mem_ctx = talloc_new(NULL);
195         struct smb2_tree *tree;
196         struct smb2_handle h1, h2;
197         NTSTATUS status;
198
199         if (!torture_smb2_connection(mem_ctx, &tree)) {
200                 return False;
201         }
202
203         h1 = torture_smb2_create(tree, "test9.dat");
204         h2 = torture_smb2_create(tree, "test9.dat");
205         status = torture_smb2_write(tree, h1);
206         if (!NT_STATUS_IS_OK(status)) {
207                 printf("Write failed - %s\n", nt_errstr(status));
208                 return False;
209         }
210         status = torture_smb2_close(tree, h1);
211         if (!NT_STATUS_IS_OK(status)) {
212                 printf("Close failed - %s\n", nt_errstr(status));
213                 return False;
214         }
215         status = torture_smb2_close(tree, h2);
216         if (!NT_STATUS_IS_OK(status)) {
217                 printf("Close failed - %s\n", nt_errstr(status));
218                 return False;
219         }
220
221         status = smb2_util_close(tree, h1);
222         if (!NT_STATUS_EQUAL(status, NT_STATUS_FILE_CLOSED)) {
223                 printf("close should have closed the handle - %s\n", nt_errstr(status));
224                 return False;
225         }
226
227         status = smb2_tdis(tree);
228         if (!NT_STATUS_IS_OK(status)) {
229                 printf("tdis failed - %s\n", nt_errstr(status));
230                 return False;
231         }
232
233         status = smb2_tdis(tree);
234         if (!NT_STATUS_EQUAL(status, NT_STATUS_NETWORK_NAME_DELETED)) {
235                 printf("tdis should have disabled session - %s\n", nt_errstr(status));
236                 return False;
237         }
238
239         status = smb2_logoff(tree->session);
240         if (!NT_STATUS_IS_OK(status)) {
241                 printf("Logoff failed - %s\n", nt_errstr(status));
242                 return False;
243         }
244
245         status = smb2_logoff(tree->session);
246         if (!NT_STATUS_EQUAL(status, NT_STATUS_USER_SESSION_DELETED)) {
247                 printf("Logoff should have disabled session - %s\n", nt_errstr(status));
248                 return False;
249         }
250
251         status = smb2_keepalive(tree->session->transport);
252         if (!NT_STATUS_IS_OK(status)) {
253                 printf("keepalive failed? - %s\n", nt_errstr(status));
254                 return False;
255         }
256
257         talloc_free(mem_ctx);
258
259         return True;
260 }