Merge branch 'v4-0-test' of ssh://git.samba.org/data/git/samba into registry
[ira/wip.git] / source4 / torture / smb2 / persistent_handles.c
1 /*
2    Unix SMB/CIFS implementation.
3
4    test suite for SMB2 persistent file handles
5
6    Copyright (C) Stefan Metzmacher 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 "librpc/gen_ndr/security.h"
24 #include "libcli/smb2/smb2.h"
25 #include "libcli/smb2/smb2_calls.h"
26 #include "torture/torture.h"
27 #include "torture/smb2/proto.h"
28 #include "param/param.h"
29
30 #define CHECK_VAL(v, correct) do { \
31         if ((v) != (correct)) { \
32                 torture_result(tctx, TORTURE_FAIL, "(%s): wrong value for %s got 0x%x - should be 0x%x\n", \
33                                 __location__, #v, (int)v, (int)correct); \
34                 ret = false; \
35         }} while (0)
36
37 #define CHECK_STATUS(status, correct) do { \
38         if (!NT_STATUS_EQUAL(status, correct)) { \
39                 torture_result(tctx, TORTURE_FAIL, __location__": Incorrect status %s - should be %s", \
40                        nt_errstr(status), nt_errstr(correct)); \
41                 ret = false; \
42                 goto done; \
43         }} while (0)
44
45 /* 
46    basic testing of SMB2 persistent file handles
47    regarding the position information on the handle
48 */
49 bool torture_smb2_persistent_handles1(struct torture_context *tctx,
50                                       struct smb2_tree *tree1,
51                                       struct smb2_tree *tree2)
52 {
53         TALLOC_CTX *mem_ctx = talloc_new(tctx);
54         struct smb2_handle h1, h2;
55         struct smb2_create io1, io2;
56         NTSTATUS status;
57         const char *fname = "persistent_handles.dat";
58         DATA_BLOB b;
59         union smb_fileinfo qfinfo;
60         union smb_setfileinfo sfinfo;
61         bool ret = true;
62         uint64_t pos;
63
64         smb2_util_unlink(tree1, fname);
65
66         ZERO_STRUCT(io1);
67         io1.in.security_flags           = 0x00;
68         io1.in.oplock_level             = SMB2_OPLOCK_LEVEL_BATCH;
69         io1.in.impersonation_level      = NTCREATEX_IMPERSONATION_IMPERSONATION;
70         io1.in.create_flags             = 0x00000000;
71         io1.in.reserved                 = 0x00000000;
72         io1.in.desired_access           = SEC_RIGHTS_FILE_ALL;
73         io1.in.file_attributes          = FILE_ATTRIBUTE_NORMAL;
74         io1.in.share_access             = NTCREATEX_SHARE_ACCESS_READ |
75                                           NTCREATEX_SHARE_ACCESS_WRITE |
76                                           NTCREATEX_SHARE_ACCESS_DELETE;
77         io1.in.create_disposition       = NTCREATEX_DISP_OPEN_IF;
78         io1.in.create_options           = NTCREATEX_OPTIONS_SEQUENTIAL_ONLY |
79                                           NTCREATEX_OPTIONS_ASYNC_ALERT |
80                                           NTCREATEX_OPTIONS_NON_DIRECTORY_FILE |
81                                           0x00200000;
82         io1.in.fname                    = fname;
83
84         b = data_blob_talloc(mem_ctx, NULL, 16);
85         SBVAL(b.data, 0, 0);
86         SBVAL(b.data, 8, 0);
87
88         status = smb2_create_blob_add(tree1, &io1.in.blobs,
89                                       SMB2_CREATE_TAG_DHNQ,
90                                       b);
91         CHECK_STATUS(status, NT_STATUS_OK);
92
93         status = smb2_create(tree1, mem_ctx, &io1);
94         CHECK_STATUS(status, NT_STATUS_OK);
95         CHECK_VAL(io1.out.oplock_level, SMB2_OPLOCK_LEVEL_BATCH);
96         /*CHECK_VAL(io1.out.reserved, 0);*/
97         CHECK_VAL(io1.out.create_action, NTCREATEX_ACTION_CREATED);
98         CHECK_VAL(io1.out.alloc_size, 0);
99         CHECK_VAL(io1.out.size, 0);
100         CHECK_VAL(io1.out.file_attr, FILE_ATTRIBUTE_ARCHIVE);
101         CHECK_VAL(io1.out.reserved2, 0);
102
103         /* TODO: check extra blob content */
104
105         h1 = io1.out.file.handle;
106
107         ZERO_STRUCT(qfinfo);
108         qfinfo.generic.level = RAW_FILEINFO_POSITION_INFORMATION;
109         qfinfo.generic.in.file.handle = h1;
110         status = smb2_getinfo_file(tree1, mem_ctx, &qfinfo);
111         CHECK_STATUS(status, NT_STATUS_OK);
112         CHECK_VAL(qfinfo.position_information.out.position, 0);
113         pos = qfinfo.position_information.out.position;
114         torture_comment(tctx, "position: %llu\n",
115                         (unsigned long long)pos);
116
117         ZERO_STRUCT(sfinfo);
118         sfinfo.generic.level = RAW_SFILEINFO_POSITION_INFORMATION;
119         sfinfo.generic.in.file.handle = h1;
120         sfinfo.position_information.in.position = 0x1000;
121         status = smb2_setinfo_file(tree1, &sfinfo);
122         CHECK_STATUS(status, NT_STATUS_OK);
123
124         ZERO_STRUCT(qfinfo);
125         qfinfo.generic.level = RAW_FILEINFO_POSITION_INFORMATION;
126         qfinfo.generic.in.file.handle = h1;
127         status = smb2_getinfo_file(tree1, mem_ctx, &qfinfo);
128         CHECK_STATUS(status, NT_STATUS_OK);
129         CHECK_VAL(qfinfo.position_information.out.position, 0x1000);
130         pos = qfinfo.position_information.out.position;
131         torture_comment(tctx, "position: %llu\n",
132                         (unsigned long long)pos);
133
134         talloc_free(tree1);
135         tree1 = NULL;
136
137         ZERO_STRUCT(qfinfo);
138         qfinfo.generic.level = RAW_FILEINFO_POSITION_INFORMATION;
139         qfinfo.generic.in.file.handle = h1;
140         status = smb2_getinfo_file(tree2, mem_ctx, &qfinfo);
141         CHECK_STATUS(status, NT_STATUS_FILE_CLOSED);
142
143         ZERO_STRUCT(io2);
144         io2.in.fname = fname;
145
146         b = data_blob_talloc(tctx, NULL, 16);
147         SBVAL(b.data, 0, h1.data[0]);
148         SBVAL(b.data, 8, h1.data[1]);
149
150         status = smb2_create_blob_add(tree2, &io2.in.blobs,
151                                       SMB2_CREATE_TAG_DHNC,
152                                       b);
153         CHECK_STATUS(status, NT_STATUS_OK);
154
155         status = smb2_create(tree2, mem_ctx, &io2);
156         CHECK_STATUS(status, NT_STATUS_OK);
157         CHECK_VAL(io2.out.oplock_level, SMB2_OPLOCK_LEVEL_BATCH);
158         CHECK_VAL(io2.out.reserved, 0x00);
159         CHECK_VAL(io2.out.create_action, NTCREATEX_ACTION_EXISTED);
160         CHECK_VAL(io2.out.alloc_size, 0);
161         CHECK_VAL(io2.out.size, 0);
162         CHECK_VAL(io2.out.file_attr, FILE_ATTRIBUTE_ARCHIVE);
163         CHECK_VAL(io2.out.reserved2, 0);
164
165         h2 = io2.out.file.handle;
166
167         ZERO_STRUCT(qfinfo);
168         qfinfo.generic.level = RAW_FILEINFO_POSITION_INFORMATION;
169         qfinfo.generic.in.file.handle = h2;
170         status = smb2_getinfo_file(tree2, mem_ctx, &qfinfo);
171         CHECK_STATUS(status, NT_STATUS_OK);
172         CHECK_VAL(qfinfo.position_information.out.position, 0x1000);
173         pos = qfinfo.position_information.out.position;
174         torture_comment(tctx, "position: %llu\n",
175                         (unsigned long long)pos);
176
177         smb2_util_close(tree2, h2);
178
179         talloc_free(mem_ctx);
180
181         smb2_util_unlink(tree2, fname);
182 done:
183         return ret;
184 }