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