r14542: Remove librpc, libndr and libnbt from includes.h
[jelmer/samba4-debian.git] / source / 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/smb2/proto.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.flags       = SMB2_CLOSE_FLAGS_FULL_INFORMATION;
41         io.in.handle   = handle;
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(-1, "torture", "dangerous", False)) {
78                 data = data_blob_talloc(tree, NULL, 160000);
79         } else {
80                 data = data_blob_talloc(tree, NULL, 120000);
81         }
82         for (i=0;i<data.length;i++) {
83                 data.data[i] = i;
84         }
85
86         ZERO_STRUCT(w);
87         w.in.offset      = 0;
88         w.in.handle      = handle;
89         w.in.data        = data;
90
91         status = smb2_write(tree, &w);
92         if (!NT_STATUS_IS_OK(status)) {
93                 printf("write failed - %s\n", nt_errstr(status));
94                 return status;
95         }
96
97         torture_smb2_all_info(tree, handle);
98
99         status = smb2_write(tree, &w);
100         if (!NT_STATUS_IS_OK(status)) {
101                 printf("write failed - %s\n", nt_errstr(status));
102                 return status;
103         }
104
105         torture_smb2_all_info(tree, handle);
106
107         ZERO_STRUCT(f);
108         f.in.handle      = handle;
109
110         status = smb2_flush(tree, &f);
111         if (!NT_STATUS_IS_OK(status)) {
112                 printf("flush failed - %s\n", nt_errstr(status));
113                 return status;
114         }
115
116         ZERO_STRUCT(r);
117         r.in.length      = data.length;
118         r.in.offset      = 0;
119         r.in.handle      = handle;
120
121         status = smb2_read(tree, tree, &r);
122         if (!NT_STATUS_IS_OK(status)) {
123                 printf("read failed - %s\n", nt_errstr(status));
124                 return status;
125         }
126
127         if (data.length != r.out.data.length ||
128             memcmp(data.data, r.out.data.data, data.length) != 0) {
129                 printf("read data mismatch\n");
130                 return NT_STATUS_NET_WRITE_FAULT;
131         }
132
133         return status;
134 }
135
136
137 /*
138   send a create
139 */
140 static struct smb2_handle torture_smb2_create(struct smb2_tree *tree, 
141                                               const char *fname)
142 {
143         struct smb2_create io;
144         NTSTATUS status;
145         TALLOC_CTX *tmp_ctx = talloc_new(tree);
146
147         ZERO_STRUCT(io);
148         io.in.oplock_flags = 0;
149         io.in.access_mask = SEC_RIGHTS_FILE_ALL;
150         io.in.file_attr   = FILE_ATTRIBUTE_NORMAL;
151         io.in.open_disposition = NTCREATEX_DISP_OPEN_IF;
152         io.in.share_access = 
153                 NTCREATEX_SHARE_ACCESS_DELETE|
154                 NTCREATEX_SHARE_ACCESS_READ|
155                 NTCREATEX_SHARE_ACCESS_WRITE;
156         io.in.create_options = NTCREATEX_OPTIONS_WRITE_THROUGH;
157         io.in.fname = fname;
158
159         status = smb2_create(tree, tmp_ctx, &io);
160         if (!NT_STATUS_IS_OK(status)) {
161                 printf("create1 failed - %s\n", nt_errstr(status));
162                 return io.out.handle;
163         }
164
165         if (DEBUGLVL(1)) {
166                 printf("Open gave:\n");
167                 printf("oplock_flags    = 0x%x\n", io.out.oplock_flags);
168                 printf("create_action   = 0x%x\n", io.out.create_action);
169                 printf("create_time     = %s\n", nt_time_string(tmp_ctx, io.out.create_time));
170                 printf("access_time     = %s\n", nt_time_string(tmp_ctx, io.out.access_time));
171                 printf("write_time      = %s\n", nt_time_string(tmp_ctx, io.out.write_time));
172                 printf("change_time     = %s\n", nt_time_string(tmp_ctx, io.out.change_time));
173                 printf("alloc_size      = %lld\n", (long long)io.out.alloc_size);
174                 printf("size            = %lld\n", (long long)io.out.size);
175                 printf("file_attr       = 0x%x\n", io.out.file_attr);
176                 printf("handle          = %016llx%016llx\n", 
177                        (long long)io.out.handle.data[0], 
178                        (long long)io.out.handle.data[1]);
179         }
180
181         talloc_free(tmp_ctx);
182         
183         return io.out.handle;
184 }
185
186
187 /* 
188    basic testing of SMB2 connection calls
189 */
190 BOOL torture_smb2_connect(void)
191 {
192         TALLOC_CTX *mem_ctx = talloc_new(NULL);
193         struct smb2_tree *tree;
194         struct smb2_handle h1, h2;
195         NTSTATUS status;
196
197         if (!torture_smb2_connection(mem_ctx, &tree)) {
198                 return False;
199         }
200
201         h1 = torture_smb2_create(tree, "test9.dat");
202         h2 = torture_smb2_create(tree, "test9.dat");
203         torture_smb2_write(tree, h1);
204         torture_smb2_close(tree, h1);
205         torture_smb2_close(tree, h2);
206
207         status = smb2_tdis(tree);
208         if (!NT_STATUS_IS_OK(status)) {
209                 printf("tdis failed - %s\n", nt_errstr(status));
210                 return False;
211         }
212
213         status = smb2_tdis(tree);
214         if (!NT_STATUS_EQUAL(status, NT_STATUS_NETWORK_NAME_DELETED)) {
215                 printf("tdis should have disabled session - %s\n", nt_errstr(status));
216                 return False;
217         }
218
219         status = smb2_logoff(tree->session);
220         if (!NT_STATUS_IS_OK(status)) {
221                 printf("Logoff failed - %s\n", nt_errstr(status));
222                 return False;
223         }
224
225         status = smb2_logoff(tree->session);
226         if (!NT_STATUS_EQUAL(status, NT_STATUS_USER_SESSION_DELETED)) {
227                 printf("Logoff should have disabled session - %s\n", nt_errstr(status));
228                 return False;
229         }
230
231         status = smb2_keepalive(tree->session->transport);
232         if (!NT_STATUS_IS_OK(status)) {
233                 printf("keepalive failed? - %s\n", nt_errstr(status));
234                 return False;
235         }
236
237         talloc_free(mem_ctx);
238
239         return True;
240 }