Merge branch 'v4-0-test' of ssh://git.samba.org/data/git/samba into 4-0-abartlet
[kai/samba.git] / source4 / torture / smb2 / create.c
1 /* 
2    Unix SMB/CIFS implementation.
3
4    SMB2 create test suite
5
6    Copyright (C) Andrew Tridgell 2008
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 3 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, see <http://www.gnu.org/licenses/>.
20 */
21
22 #include "includes.h"
23 #include "libcli/smb2/smb2.h"
24 #include "libcli/smb2/smb2_calls.h"
25 #include "torture/torture.h"
26 #include "torture/smb2/proto.h"
27 #include "param/param.h"
28 #include "librpc/gen_ndr/ndr_security.h"
29
30 #define FNAME "test_create.dat"
31
32 #define CHECK_STATUS(status, correct) do { \
33         if (!NT_STATUS_EQUAL(status, correct)) { \
34                 printf("(%s) Incorrect status %s - should be %s\n", \
35                        __location__, nt_errstr(status), nt_errstr(correct)); \
36                 return false; \
37         }} while (0)
38
39 #define CHECK_EQUAL(v, correct) do { \
40         if (v != correct) { \
41                 printf("(%s) Incorrect value for %s 0x%08x - should be 0x%08x\n", \
42                        __location__, #v, v, correct); \
43                 return false; \
44         }} while (0)
45
46 /*
47   test some interesting combinations found by gentest
48  */
49 bool torture_smb2_create_gentest(struct torture_context *torture, struct smb2_tree *tree)
50 {
51         struct smb2_create io;
52         NTSTATUS status;
53         TALLOC_CTX *tmp_ctx = talloc_new(tree);
54         uint32_t access_mask, file_attributes, denied_mask;
55
56         ZERO_STRUCT(io);
57         io.in.desired_access     = SEC_FLAG_MAXIMUM_ALLOWED;
58         io.in.file_attributes    = FILE_ATTRIBUTE_NORMAL;
59         io.in.create_disposition = NTCREATEX_DISP_OVERWRITE_IF;
60         io.in.share_access = 
61                 NTCREATEX_SHARE_ACCESS_DELETE|
62                 NTCREATEX_SHARE_ACCESS_READ|
63                 NTCREATEX_SHARE_ACCESS_WRITE;
64         io.in.create_options = 0;
65         io.in.fname = FNAME;
66
67         status = smb2_create(tree, tmp_ctx, &io);
68         CHECK_STATUS(status, NT_STATUS_OK);
69
70         status = smb2_util_close(tree, io.out.file.handle);
71         CHECK_STATUS(status, NT_STATUS_OK);
72
73         io.in.create_options = 0xF0000000;
74         status = smb2_create(tree, tmp_ctx, &io);
75         CHECK_STATUS(status, NT_STATUS_INVALID_PARAMETER);
76
77         io.in.create_options = 0x00100000;
78         status = smb2_create(tree, tmp_ctx, &io);
79         CHECK_STATUS(status, NT_STATUS_NOT_SUPPORTED);
80
81         io.in.create_options = 0xF0100000;
82         status = smb2_create(tree, tmp_ctx, &io);
83         CHECK_STATUS(status, NT_STATUS_NOT_SUPPORTED);
84
85         io.in.create_options = 0;
86
87         io.in.file_attributes = FILE_ATTRIBUTE_DEVICE;
88         status = smb2_create(tree, tmp_ctx, &io);
89         CHECK_STATUS(status, NT_STATUS_INVALID_PARAMETER);
90
91         io.in.file_attributes = FILE_ATTRIBUTE_VOLUME;
92         status = smb2_create(tree, tmp_ctx, &io);
93         CHECK_STATUS(status, NT_STATUS_INVALID_PARAMETER);
94
95         io.in.create_disposition = NTCREATEX_DISP_OPEN;
96         io.in.file_attributes = FILE_ATTRIBUTE_VOLUME;
97         status = smb2_create(tree, tmp_ctx, &io);
98         CHECK_STATUS(status, NT_STATUS_INVALID_PARAMETER);
99         
100         io.in.create_disposition = NTCREATEX_DISP_CREATE;
101         io.in.desired_access = 0x08000000;
102         status = smb2_create(tree, tmp_ctx, &io);
103         CHECK_STATUS(status, NT_STATUS_ACCESS_DENIED);
104
105         io.in.desired_access = 0x04000000;
106         status = smb2_create(tree, tmp_ctx, &io);
107         CHECK_STATUS(status, NT_STATUS_ACCESS_DENIED);
108
109         io.in.create_disposition = NTCREATEX_DISP_OPEN_IF;
110         io.in.file_attributes = 0;
111         access_mask = 0;
112         {
113                 int i;
114                 for (i=0;i<32;i++) {
115                         io.in.desired_access = 1<<i;
116                         status = smb2_create(tree, tmp_ctx, &io);
117                         if (NT_STATUS_EQUAL(status, NT_STATUS_ACCESS_DENIED)) {
118                                 access_mask |= io.in.desired_access;
119                         } else {
120                                 CHECK_STATUS(status, NT_STATUS_OK);
121                                 status = smb2_util_close(tree, io.out.file.handle);
122                                 CHECK_STATUS(status, NT_STATUS_OK);
123                         }
124                 }
125         }
126
127         CHECK_EQUAL(access_mask, 0x0df0fe00);
128
129         io.in.create_disposition = NTCREATEX_DISP_OPEN_IF;
130         io.in.desired_access = SEC_FLAG_MAXIMUM_ALLOWED;
131         io.in.file_attributes = 0;
132         file_attributes = 0;
133         denied_mask = 0;
134         {
135                 int i;
136                 for (i=0;i<32;i++) {
137                         io.in.file_attributes = 1<<i;
138                         smb2_deltree(tree, FNAME);
139                         status = smb2_create(tree, tmp_ctx, &io);
140                         if (NT_STATUS_EQUAL(status, NT_STATUS_INVALID_PARAMETER)) {
141                                 file_attributes |= io.in.file_attributes;
142                         } else if (NT_STATUS_EQUAL(status, NT_STATUS_ACCESS_DENIED)) {
143                                 denied_mask |= io.in.file_attributes;
144                         } else {
145                                 CHECK_STATUS(status, NT_STATUS_OK);
146                                 status = smb2_util_close(tree, io.out.file.handle);
147                                 CHECK_STATUS(status, NT_STATUS_OK);
148                         }
149                 }
150         }
151
152         CHECK_EQUAL(file_attributes, 0xffff8048);
153         CHECK_EQUAL(denied_mask, 0x4000);
154
155         smb2_deltree(tree, FNAME);
156
157         ZERO_STRUCT(io);
158         io.in.desired_access     = SEC_FLAG_MAXIMUM_ALLOWED;
159         io.in.file_attributes    = 0;
160         io.in.create_disposition = NTCREATEX_DISP_OVERWRITE_IF;
161         io.in.share_access = 
162                 NTCREATEX_SHARE_ACCESS_READ|
163                 NTCREATEX_SHARE_ACCESS_WRITE;
164         io.in.create_options = 0;
165         io.in.fname = FNAME ":stream1";
166         status = smb2_create(tree, tmp_ctx, &io);
167         CHECK_STATUS(status, NT_STATUS_OK);
168
169         io.in.fname = FNAME;
170         io.in.file_attributes = 0x8040;
171         io.in.share_access = 
172                 NTCREATEX_SHARE_ACCESS_READ;
173         status = smb2_create(tree, tmp_ctx, &io);
174         CHECK_STATUS(status, NT_STATUS_INVALID_PARAMETER);
175
176         talloc_free(tmp_ctx);
177         
178         return true;
179 }
180
181 /* 
182    basic testing of SMB2 create calls
183 */
184 bool torture_smb2_create(struct torture_context *torture)
185 {
186         TALLOC_CTX *mem_ctx = talloc_new(NULL);
187         struct smb2_tree *tree;
188         bool ret = true;
189
190         if (!torture_smb2_connection(torture, &tree)) {
191                 return false;
192         }
193
194         ret &= torture_smb2_create_gentest(torture, tree);
195
196         smb2_deltree(tree, FNAME);
197
198         talloc_free(mem_ctx);
199
200         return ret;
201 }