54991e27bab6626d2bea6b6c6021cbca1dbe2c21
[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/raw/libcliraw.h"
25 #include "libcli/smb2/smb2.h"
26 #include "libcli/smb2/smb2_calls.h"
27 #include "lib/cmdline/popt_common.h"
28 #include "lib/events/events.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.buffer_code = 0x18;
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", io.out.alloc_size);
56                 printf("size            = %lld\n", 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   send a create
68 */
69 static struct smb2_handle torture_smb2_create(struct smb2_tree *tree, 
70                                               const char *fname)
71 {
72         struct smb2_create io;
73         NTSTATUS status;
74         TALLOC_CTX *tmp_ctx = talloc_new(tree);
75
76         ZERO_STRUCT(io);
77         io.in.buffer_code = 0x39;
78         io.in.oplock_flags = 0;
79         io.in.access_mask = SEC_RIGHTS_FILE_ALL;
80         io.in.file_attr   = FILE_ATTRIBUTE_NORMAL;
81         io.in.open_disposition = NTCREATEX_DISP_OPEN_IF;
82         io.in.share_access = 
83                 NTCREATEX_SHARE_ACCESS_DELETE|
84                 NTCREATEX_SHARE_ACCESS_READ|
85                 NTCREATEX_SHARE_ACCESS_WRITE;
86         io.in.create_options = NTCREATEX_OPTIONS_WRITE_THROUGH;
87         io.in.fname = fname;
88
89         status = smb2_create(tree, &io);
90         if (!NT_STATUS_IS_OK(status)) {
91                 printf("create1 failed - %s\n", nt_errstr(status));
92                 return io.out.handle;
93         }
94
95         if (DEBUGLVL(1)) {
96                 printf("Open gave:\n");
97                 printf("oplock_flags    = 0x%x\n", io.out.oplock_flags);
98                 printf("create_action   = 0x%x\n", io.out.create_action);
99                 printf("create_time     = %s\n", nt_time_string(tmp_ctx, io.out.create_time));
100                 printf("access_time     = %s\n", nt_time_string(tmp_ctx, io.out.access_time));
101                 printf("write_time      = %s\n", nt_time_string(tmp_ctx, io.out.write_time));
102                 printf("change_time     = %s\n", nt_time_string(tmp_ctx, io.out.change_time));
103                 printf("alloc_size      = %lld\n", io.out.alloc_size);
104                 printf("size            = %lld\n", io.out.size);
105                 printf("file_attr       = 0x%x\n", io.out.file_attr);
106                 printf("handle          = %016llx%016llx\n", 
107                        io.out.handle.data[0], 
108                        io.out.handle.data[1]);
109         }
110
111         talloc_free(tmp_ctx);
112         
113         return io.out.handle;
114 }
115
116
117 /* 
118    basic testing of SMB2 connection calls
119 */
120 BOOL torture_smb2_connect(void)
121 {
122         TALLOC_CTX *mem_ctx = talloc_new(NULL);
123         struct smb2_tree *tree;
124         const char *host = lp_parm_string(-1, "torture", "host");
125         const char *share = lp_parm_string(-1, "torture", "share");
126         struct cli_credentials *credentials = cmdline_credentials;
127         struct smb2_handle h1, h2;
128         NTSTATUS status;
129
130         status = smb2_connect(mem_ctx, host, share, credentials, &tree, 
131                               event_context_find(mem_ctx));
132         if (!NT_STATUS_IS_OK(status)) {
133                 printf("Connection failed - %s\n", nt_errstr(status));
134                 return False;
135         }
136
137         h1 = torture_smb2_create(tree, "test9.dat");
138         h2 = torture_smb2_create(tree, "test9.dat");
139         torture_smb2_close(tree, h1);
140         torture_smb2_close(tree, h2);
141
142         talloc_free(mem_ctx);
143
144         return True;
145 }