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