r5037: got rid of all of the TALLOC_DEPRECATED stuff. My apologies for the
[ira/wip.git] / source4 / torture / raw / seek.c
1 /* 
2    Unix SMB/CIFS implementation.
3    seek test suite
4    Copyright (C) Andrew Tridgell 2003
5    
6    This program is free software; you can redistribute it and/or modify
7    it under the terms of the GNU General Public License as published by
8    the Free Software Foundation; either version 2 of the License, or
9    (at your option) any later version.
10    
11    This program is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14    GNU General Public License for more details.
15    
16    You should have received a copy of the GNU General Public License
17    along with this program; if not, write to the Free Software
18    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 */
20
21 #include "includes.h"
22
23 #define CHECK_STATUS(status, correct) do { \
24         if (!NT_STATUS_EQUAL(status, correct)) { \
25                 printf("(%d) Incorrect status %s - should be %s\n", \
26                        __LINE__, nt_errstr(status), nt_errstr(correct)); \
27                 ret = False; \
28                 goto done; \
29         }} while (0)
30
31 #define CHECK_VALUE(v, correct) do { \
32         if ((v) != (correct)) { \
33                 printf("(%d) Incorrect value %s=%d - should be %d\n", \
34                        __LINE__, #v, (int)v, (int)correct); \
35                 ret = False; \
36                 goto done; \
37         }} while (0)
38
39 #define BASEDIR "\\testseek"
40
41 /*
42   test seek ops
43 */
44 static BOOL test_seek(struct smbcli_state *cli, TALLOC_CTX *mem_ctx)
45 {
46         struct smb_seek io;
47         union smb_fileinfo finfo;
48         union smb_setfileinfo sfinfo;
49         NTSTATUS status;
50         BOOL ret = True;
51         int fnum, fnum2;
52         const char *fname = BASEDIR "\\test.txt";
53         uint8_t c[2];
54
55         if (!torture_setup_dir(cli, BASEDIR)) {
56                 return False;
57         }
58
59         fnum = smbcli_open(cli->tree, fname, O_RDWR|O_CREAT|O_TRUNC, DENY_NONE);
60         if (fnum == -1) {
61                 printf("Failed to open test.txt - %s\n", smbcli_errstr(cli->tree));
62                 ret = False;
63                 goto done;
64         }
65
66         finfo.generic.level = RAW_FILEINFO_POSITION_INFORMATION;
67         finfo.position_information.in.fnum = fnum;
68         
69         printf("Trying bad handle\n");
70         io.in.fnum = fnum+1;
71         io.in.mode = SEEK_MODE_START;
72         io.in.offset = 0;
73         status = smb_raw_seek(cli->tree, &io);
74         CHECK_STATUS(status, NT_STATUS_INVALID_HANDLE);
75
76         printf("Trying simple seek\n");
77         io.in.fnum = fnum;
78         io.in.mode = SEEK_MODE_START;
79         io.in.offset = 17;
80         status = smb_raw_seek(cli->tree, &io);
81         CHECK_STATUS(status, NT_STATUS_OK);
82         CHECK_VALUE(io.out.offset, 17);
83         status = smb_raw_fileinfo(cli->tree, mem_ctx, &finfo);
84         CHECK_STATUS(status, NT_STATUS_OK);
85         CHECK_VALUE(finfo.position_information.out.position, 0);
86         
87         printf("Trying relative seek\n");
88         io.in.fnum = fnum;
89         io.in.mode = SEEK_MODE_CURRENT;
90         io.in.offset = -3;
91         status = smb_raw_seek(cli->tree, &io);
92         CHECK_STATUS(status, NT_STATUS_OK);
93         CHECK_VALUE(io.out.offset, 14);
94
95         printf("Trying end seek\n");
96         io.in.fnum = fnum;
97         io.in.mode = SEEK_MODE_END;
98         io.in.offset = 0;
99         status = smb_raw_seek(cli->tree, &io);
100         CHECK_STATUS(status, NT_STATUS_OK);
101         finfo.generic.level = RAW_FILEINFO_ALL_INFO;
102         finfo.all_info.in.fnum = fnum;
103         status = smb_raw_fileinfo(cli->tree, mem_ctx, &finfo);
104         CHECK_STATUS(status, NT_STATUS_OK);
105         CHECK_VALUE(io.out.offset, finfo.all_info.out.size);
106
107         printf("Trying max seek\n");
108         io.in.fnum = fnum;
109         io.in.mode = SEEK_MODE_START;
110         io.in.offset = -1;
111         status = smb_raw_seek(cli->tree, &io);
112         CHECK_STATUS(status, NT_STATUS_OK);
113         CHECK_VALUE(io.out.offset, 0xffffffff);
114
115         printf("Testing position information change\n");
116         finfo.generic.level = RAW_FILEINFO_POSITION_INFORMATION;
117         finfo.position_information.in.fnum = fnum;
118         status = smb_raw_fileinfo(cli->tree, mem_ctx, &finfo);
119         CHECK_STATUS(status, NT_STATUS_OK);
120         CHECK_VALUE(finfo.position_information.out.position, 0);
121
122         printf("Trying max overflow\n");
123         io.in.fnum = fnum;
124         io.in.mode = SEEK_MODE_CURRENT;
125         io.in.offset = 1000;
126         status = smb_raw_seek(cli->tree, &io);
127         CHECK_STATUS(status, NT_STATUS_OK);
128         CHECK_VALUE(io.out.offset, 999);
129
130         printf("Testing position information change\n");
131         finfo.generic.level = RAW_FILEINFO_POSITION_INFORMATION;
132         finfo.position_information.in.fnum = fnum;
133         status = smb_raw_fileinfo(cli->tree, mem_ctx, &finfo);
134         CHECK_STATUS(status, NT_STATUS_OK);
135         CHECK_VALUE(finfo.position_information.out.position, 0);
136
137         printf("trying write to update offset\n");
138         ZERO_STRUCT(c);
139         if (smbcli_write(cli->tree, fnum, 0, c, 0, 2) != 2) {
140                 printf("Write failed - %s\n", smbcli_errstr(cli->tree));
141                 ret = False;
142                 goto done;              
143         }
144
145         printf("Testing position information change\n");
146         finfo.generic.level = RAW_FILEINFO_POSITION_INFORMATION;
147         finfo.position_information.in.fnum = fnum;
148         status = smb_raw_fileinfo(cli->tree, mem_ctx, &finfo);
149         CHECK_STATUS(status, NT_STATUS_OK);
150         CHECK_VALUE(finfo.position_information.out.position, 0);
151
152         io.in.fnum = fnum;
153         io.in.mode = SEEK_MODE_CURRENT;
154         io.in.offset = 0;
155         status = smb_raw_seek(cli->tree, &io);
156         CHECK_STATUS(status, NT_STATUS_OK);
157         CHECK_VALUE(io.out.offset, 2);
158         
159         if (smbcli_read(cli->tree, fnum, c, 0, 1) != 1) {
160                 printf("Read failed - %s\n", smbcli_errstr(cli->tree));
161                 ret = False;
162                 goto done;              
163         }
164
165         printf("Testing position information change\n");
166         finfo.generic.level = RAW_FILEINFO_POSITION_INFORMATION;
167         finfo.position_information.in.fnum = fnum;
168         status = smb_raw_fileinfo(cli->tree, mem_ctx, &finfo);
169         CHECK_STATUS(status, NT_STATUS_OK);
170         CHECK_VALUE(finfo.position_information.out.position, 1);
171
172         status = smb_raw_seek(cli->tree, &io);
173         CHECK_STATUS(status, NT_STATUS_OK);
174         CHECK_VALUE(io.out.offset, 1);
175
176         printf("Testing position information\n");
177         fnum2 = smbcli_open(cli->tree, fname, O_RDWR, DENY_NONE);
178         if (fnum2 == -1) {
179                 printf("2nd open failed - %s\n", smbcli_errstr(cli->tree));
180                 ret = False;
181                 goto done;
182         }
183         sfinfo.generic.level = RAW_SFILEINFO_POSITION_INFORMATION;
184         sfinfo.position_information.file.fnum = fnum2;
185         sfinfo.position_information.in.position = 25;
186         status = smb_raw_setfileinfo(cli->tree, &sfinfo);
187         CHECK_STATUS(status, NT_STATUS_OK);
188
189         finfo.generic.level = RAW_FILEINFO_POSITION_INFORMATION;
190         finfo.position_information.in.fnum = fnum2;
191         status = smb_raw_fileinfo(cli->tree, mem_ctx, &finfo);
192         CHECK_STATUS(status, NT_STATUS_OK);
193         CHECK_VALUE(finfo.position_information.out.position, 25);
194
195         finfo.generic.level = RAW_FILEINFO_POSITION_INFORMATION;
196         finfo.position_information.in.fnum = fnum;
197         status = smb_raw_fileinfo(cli->tree, mem_ctx, &finfo);
198         CHECK_STATUS(status, NT_STATUS_OK);
199         CHECK_VALUE(finfo.position_information.out.position, 1);
200
201         printf("position_information via paths\n");
202
203         sfinfo.generic.level = RAW_SFILEINFO_POSITION_INFORMATION;
204         sfinfo.position_information.file.fname = fname;
205         sfinfo.position_information.in.position = 32;
206         status = smb_raw_setpathinfo(cli->tree, &sfinfo);
207         CHECK_STATUS(status, NT_STATUS_OK);
208
209         finfo.generic.level = RAW_FILEINFO_POSITION_INFORMATION;
210         finfo.position_information.in.fnum = fnum2;
211         status = smb_raw_fileinfo(cli->tree, mem_ctx, &finfo);
212         CHECK_STATUS(status, NT_STATUS_OK);
213         CHECK_VALUE(finfo.position_information.out.position, 25);
214
215         finfo.generic.level = RAW_FILEINFO_POSITION_INFORMATION;
216         finfo.position_information.in.fname = fname;
217         status = smb_raw_pathinfo(cli->tree, mem_ctx, &finfo);
218         CHECK_STATUS(status, NT_STATUS_OK);
219         CHECK_VALUE(finfo.position_information.out.position, 0);
220         
221
222 done:
223         smb_raw_exit(cli->session);
224         smbcli_deltree(cli->tree, BASEDIR);
225         return ret;
226 }
227
228
229 /* 
230    basic testing of seek calls
231 */
232 BOOL torture_raw_seek(void)
233 {
234         struct smbcli_state *cli;
235         BOOL ret = True;
236         TALLOC_CTX *mem_ctx;
237
238         if (!torture_open_connection(&cli)) {
239                 return False;
240         }
241
242         mem_ctx = talloc_init("torture_raw_seek");
243
244         if (!test_seek(cli, mem_ctx)) {
245                 ret = False;
246         }
247
248         torture_close_connection(cli);
249         talloc_free(mem_ctx);
250         return ret;
251 }