349fcda9925afefec38cb02b291d477b9bebd17b
[metze/samba-autobuild/.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 2 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, write to the Free Software
20    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 */
22
23 #include "includes.h"
24 #include "smb.h"
25 #include "librpc/gen_ndr/security.h"
26 #include "libcli/smb2/smb2.h"
27 #include "libcli/smb2/smb2_calls.h"
28 #include "torture/torture.h"
29 #include "torture/smb2/proto.h"
30
31 /*
32   send a close
33 */
34 static NTSTATUS torture_smb2_close(struct smb2_tree *tree, struct smb2_handle handle)
35 {
36         struct smb2_close io;
37         NTSTATUS status;
38         TALLOC_CTX *tmp_ctx = talloc_new(tree);
39
40         ZERO_STRUCT(io);
41         io.in.flags       = SMB2_CLOSE_FLAGS_FULL_INFORMATION;
42         io.in.handle   = handle;
43         status = smb2_close(tree, &io);
44         if (!NT_STATUS_IS_OK(status)) {
45                 printf("close failed - %s\n", nt_errstr(status));
46                 return status;
47         }
48
49         if (DEBUGLVL(1)) {
50                 printf("Close gave:\n");
51                 printf("create_time     = %s\n", nt_time_string(tmp_ctx, io.out.create_time));
52                 printf("access_time     = %s\n", nt_time_string(tmp_ctx, io.out.access_time));
53                 printf("write_time      = %s\n", nt_time_string(tmp_ctx, io.out.write_time));
54                 printf("change_time     = %s\n", nt_time_string(tmp_ctx, io.out.change_time));
55                 printf("alloc_size      = %lld\n", (long long)io.out.alloc_size);
56                 printf("size            = %lld\n", (long long)io.out.size);
57                 printf("file_attr       = 0x%x\n", io.out.file_attr);
58         }
59
60         talloc_free(tmp_ctx);
61         
62         return status;
63 }
64
65
66 /*
67   test writing
68 */
69 static NTSTATUS torture_smb2_write(struct smb2_tree *tree, struct smb2_handle handle)
70 {
71         struct smb2_write w;
72         struct smb2_read r;
73         struct smb2_flush f;
74         NTSTATUS status;
75         DATA_BLOB data;
76         int i;
77         
78         if (lp_parm_bool(-1, "torture", "dangerous", False)) {
79                 data = data_blob_talloc(tree, NULL, 160000);
80         } else {
81                 data = data_blob_talloc(tree, NULL, 120000);
82         }
83         for (i=0;i<data.length;i++) {
84                 data.data[i] = i;
85         }
86
87         ZERO_STRUCT(w);
88         w.in.offset      = 0;
89         w.in.handle      = handle;
90         w.in.data        = data;
91
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;
96         }
97
98         torture_smb2_all_info(tree, handle);
99
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;
104         }
105
106         torture_smb2_all_info(tree, handle);
107
108         ZERO_STRUCT(f);
109         f.in.handle      = handle;
110
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;
115         }
116
117         ZERO_STRUCT(r);
118         r.in.length      = data.length;
119         r.in.offset      = 0;
120         r.in.handle      = handle;
121
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;
126         }
127
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;
132         }
133
134         return status;
135 }
136
137
138 /*
139   send a create
140 */
141 static struct smb2_handle torture_smb2_create(struct smb2_tree *tree, 
142                                               const char *fname)
143 {
144         struct smb2_create io;
145         NTSTATUS status;
146         TALLOC_CTX *tmp_ctx = talloc_new(tree);
147
148         ZERO_STRUCT(io);
149         io.in.oplock_flags = 0;
150         io.in.access_mask = SEC_RIGHTS_FILE_ALL;
151         io.in.file_attr   = FILE_ATTRIBUTE_NORMAL;
152         io.in.open_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;
159
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.handle;
164         }
165
166         if (DEBUGLVL(1)) {
167                 printf("Open gave:\n");
168                 printf("oplock_flags    = 0x%x\n", io.out.oplock_flags);
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.handle.data[0], 
179                        (long long)io.out.handle.data[1]);
180         }
181
182         talloc_free(tmp_ctx);
183         
184         return io.out.handle;
185 }
186
187
188 /* 
189    basic testing of SMB2 connection calls
190 */
191 BOOL torture_smb2_connect(struct torture_context *torture)
192 {
193         TALLOC_CTX *mem_ctx = talloc_new(NULL);
194         struct smb2_tree *tree;
195         struct smb2_handle h1, h2;
196         NTSTATUS status;
197
198         if (!torture_smb2_connection(mem_ctx, &tree)) {
199                 return False;
200         }
201
202         h1 = torture_smb2_create(tree, "test9.dat");
203         h2 = torture_smb2_create(tree, "test9.dat");
204         torture_smb2_write(tree, h1);
205         torture_smb2_close(tree, h1);
206         torture_smb2_close(tree, h2);
207
208         status = smb2_tdis(tree);
209         if (!NT_STATUS_IS_OK(status)) {
210                 printf("tdis failed - %s\n", nt_errstr(status));
211                 return False;
212         }
213
214         status = smb2_tdis(tree);
215         if (!NT_STATUS_EQUAL(status, NT_STATUS_NETWORK_NAME_DELETED)) {
216                 printf("tdis should have disabled session - %s\n", nt_errstr(status));
217                 return False;
218         }
219
220         status = smb2_logoff(tree->session);
221         if (!NT_STATUS_IS_OK(status)) {
222                 printf("Logoff failed - %s\n", nt_errstr(status));
223                 return False;
224         }
225
226         status = smb2_logoff(tree->session);
227         if (!NT_STATUS_EQUAL(status, NT_STATUS_USER_SESSION_DELETED)) {
228                 printf("Logoff should have disabled session - %s\n", nt_errstr(status));
229                 return False;
230         }
231
232         status = smb2_keepalive(tree->session->transport);
233         if (!NT_STATUS_IS_OK(status)) {
234                 printf("keepalive failed? - %s\n", nt_errstr(status));
235                 return False;
236         }
237
238         talloc_free(mem_ctx);
239
240         return True;
241 }