r11693: added a full async composite function for SMB2 that does:
[kai/samba-autobuild/.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 "libcli/raw/libcliraw.h"
25 #include "libcli/smb2/smb2.h"
26 #include "libcli/smb2/smb2_calls.h"
27 #include "librpc/gen_ndr/ndr_security.h"
28 #include "lib/cmdline/popt_common.h"
29 #include "lib/events/events.h"
30
31 #define BASEDIR "\\testsmb2"
32
33 #define CHECK_STATUS(status, correct) do { \
34         if (!NT_STATUS_EQUAL(status, correct)) { \
35                 printf("(%s) Incorrect status %s - should be %s\n", \
36                        __location__, nt_errstr(status), nt_errstr(correct)); \
37                 ret = False; \
38                 goto done; \
39         }} while (0)
40
41
42 /*
43   send a close
44 */
45 static NTSTATUS torture_smb2_close(struct smb2_tree *tree, struct smb2_handle handle)
46 {
47         struct smb2_close io;
48         NTSTATUS status;
49         TALLOC_CTX *tmp_ctx = talloc_new(tree);
50
51         ZERO_STRUCT(io);
52         io.in.buffer_code = 0x18;
53         io.in.flags       = SMB2_CLOSE_FLAGS_FULL_INFORMATION;
54         io.in.handle   = handle;
55         status = smb2_close(tree, &io);
56         if (!NT_STATUS_IS_OK(status)) {
57                 printf("close failed - %s\n", nt_errstr(status));
58                 return status;
59         }
60
61         printf("Close gave:\n");
62         printf("create_time     = %s\n", nt_time_string(tmp_ctx, io.out.create_time));
63         printf("access_time     = %s\n", nt_time_string(tmp_ctx, io.out.access_time));
64         printf("write_time      = %s\n", nt_time_string(tmp_ctx, io.out.write_time));
65         printf("change_time     = %s\n", nt_time_string(tmp_ctx, io.out.change_time));
66         printf("alloc_size      = %lld\n", io.out.alloc_size);
67         printf("size            = %lld\n", io.out.size);
68         printf("file_attr       = 0x%x\n", io.out.file_attr);
69
70         talloc_free(tmp_ctx);
71         
72         return status;
73 }
74
75
76 /*
77   send a create
78 */
79 static struct smb2_handle torture_smb2_create(struct smb2_tree *tree, 
80                                               const char *fname)
81 {
82         struct smb2_create io;
83         NTSTATUS status;
84         TALLOC_CTX *tmp_ctx = talloc_new(tree);
85
86         ZERO_STRUCT(io);
87         io.in.buffer_code = 0x39;
88         io.in.oplock_flags = 0;
89         io.in.access_mask = SEC_RIGHTS_FILE_ALL;
90         io.in.file_attr   = FILE_ATTRIBUTE_NORMAL;
91         io.in.open_disposition = NTCREATEX_DISP_OPEN_IF;
92         io.in.share_access = 
93                 NTCREATEX_SHARE_ACCESS_DELETE|
94                 NTCREATEX_SHARE_ACCESS_READ|
95                 NTCREATEX_SHARE_ACCESS_WRITE;
96         io.in.create_options = NTCREATEX_OPTIONS_WRITE_THROUGH;
97         io.in.fname = fname;
98
99         status = smb2_create(tree, &io);
100         if (!NT_STATUS_IS_OK(status)) {
101                 printf("create1 failed - %s\n", nt_errstr(status));
102                 return io.out.handle;
103         }
104
105         printf("Open gave:\n");
106         printf("oplock_flags    = 0x%x\n", io.out.oplock_flags);
107         printf("create_action   = 0x%x\n", io.out.create_action);
108         printf("create_time     = %s\n", nt_time_string(tmp_ctx, io.out.create_time));
109         printf("access_time     = %s\n", nt_time_string(tmp_ctx, io.out.access_time));
110         printf("write_time      = %s\n", nt_time_string(tmp_ctx, io.out.write_time));
111         printf("change_time     = %s\n", nt_time_string(tmp_ctx, io.out.change_time));
112         printf("alloc_size      = %lld\n", io.out.alloc_size);
113         printf("size            = %lld\n", io.out.size);
114         printf("file_attr       = 0x%x\n", io.out.file_attr);
115         printf("handle          = %016llx%016llx\n", 
116                io.out.handle.data[0], 
117                io.out.handle.data[1]);
118
119         talloc_free(tmp_ctx);
120         
121         return io.out.handle;
122 }
123
124 /* 
125    basic testing of SMB2 connection calls
126 */
127 BOOL torture_smb2_connect(void)
128 {
129         TALLOC_CTX *mem_ctx = talloc_new(NULL);
130         struct smb2_tree *tree;
131         const char *host = lp_parm_string(-1, "torture", "host");
132         const char *share = lp_parm_string(-1, "torture", "share");
133         struct cli_credentials *credentials = cmdline_credentials;
134         struct smb2_handle h1, h2;
135         NTSTATUS status;
136
137         status = smb2_connect(mem_ctx, host, share, credentials, &tree, 
138                               event_context_find(mem_ctx));
139         if (!NT_STATUS_IS_OK(status)) {
140                 printf("Connection failed - %s\n", nt_errstr(status));
141                 return False;
142         }
143
144         h1        = torture_smb2_create(tree, "test9.dat");
145         h2        = torture_smb2_create(tree, "test9.dat");
146         torture_smb2_close(tree, h1);
147         torture_smb2_close(tree, h2);
148
149         talloc_free(mem_ctx);
150
151         return True;
152 }