r12693: Move core data structures out of smb.h into core.h
[jra/samba/.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 #include "torture/torture.h"
23 #include "system/filesys.h"
24 #include "libcli/raw/libcliraw.h"
25
26 #define CHECK_STATUS(status, correct) do { \
27         if (!NT_STATUS_EQUAL(status, correct)) { \
28                 printf("(%d) Incorrect status %s - should be %s\n", \
29                        __LINE__, nt_errstr(status), nt_errstr(correct)); \
30                 ret = False; \
31                 goto done; \
32         }} while (0)
33
34 #define CHECK_VALUE(v, correct) do { \
35         if ((v) != (correct)) { \
36                 printf("(%d) Incorrect value %s=%d - should be %d\n", \
37                        __LINE__, #v, (int)v, (int)correct); \
38                 ret = False; \
39                 goto done; \
40         }} while (0)
41
42 #define BASEDIR "\\testseek"
43
44 /*
45   test seek ops
46 */
47 static BOOL test_seek(struct smbcli_state *cli, TALLOC_CTX *mem_ctx)
48 {
49         struct smb_seek io;
50         union smb_fileinfo finfo;
51         union smb_setfileinfo sfinfo;
52         NTSTATUS status;
53         BOOL ret = True;
54         int fnum, fnum2;
55         const char *fname = BASEDIR "\\test.txt";
56         uint8_t c[2];
57
58         if (!torture_setup_dir(cli, BASEDIR)) {
59                 return False;
60         }
61
62         fnum = smbcli_open(cli->tree, fname, O_RDWR|O_CREAT|O_TRUNC, DENY_NONE);
63         if (fnum == -1) {
64                 printf("Failed to open test.txt - %s\n", smbcli_errstr(cli->tree));
65                 ret = False;
66                 goto done;
67         }
68
69         finfo.generic.level = RAW_FILEINFO_POSITION_INFORMATION;
70         finfo.position_information.in.fnum = fnum;
71         
72         printf("Trying bad handle\n");
73         io.in.fnum = fnum+1;
74         io.in.mode = SEEK_MODE_START;
75         io.in.offset = 0;
76         status = smb_raw_seek(cli->tree, &io);
77         CHECK_STATUS(status, NT_STATUS_INVALID_HANDLE);
78
79         printf("Trying simple seek\n");
80         io.in.fnum = fnum;
81         io.in.mode = SEEK_MODE_START;
82         io.in.offset = 17;
83         status = smb_raw_seek(cli->tree, &io);
84         CHECK_STATUS(status, NT_STATUS_OK);
85         CHECK_VALUE(io.out.offset, 17);
86         status = smb_raw_fileinfo(cli->tree, mem_ctx, &finfo);
87         CHECK_STATUS(status, NT_STATUS_OK);
88         CHECK_VALUE(finfo.position_information.out.position, 0);
89         
90         printf("Trying relative seek\n");
91         io.in.fnum = fnum;
92         io.in.mode = SEEK_MODE_CURRENT;
93         io.in.offset = -3;
94         status = smb_raw_seek(cli->tree, &io);
95         CHECK_STATUS(status, NT_STATUS_OK);
96         CHECK_VALUE(io.out.offset, 14);
97
98         printf("Trying end seek\n");
99         io.in.fnum = fnum;
100         io.in.mode = SEEK_MODE_END;
101         io.in.offset = 0;
102         status = smb_raw_seek(cli->tree, &io);
103         CHECK_STATUS(status, NT_STATUS_OK);
104         finfo.generic.level = RAW_FILEINFO_ALL_INFO;
105         finfo.all_info.in.fnum = fnum;
106         status = smb_raw_fileinfo(cli->tree, mem_ctx, &finfo);
107         CHECK_STATUS(status, NT_STATUS_OK);
108         CHECK_VALUE(io.out.offset, finfo.all_info.out.size);
109
110         printf("Trying max seek\n");
111         io.in.fnum = fnum;
112         io.in.mode = SEEK_MODE_START;
113         io.in.offset = -1;
114         status = smb_raw_seek(cli->tree, &io);
115         CHECK_STATUS(status, NT_STATUS_OK);
116         CHECK_VALUE(io.out.offset, 0xffffffff);
117
118         printf("Testing position information change\n");
119         finfo.generic.level = RAW_FILEINFO_POSITION_INFORMATION;
120         finfo.position_information.in.fnum = fnum;
121         status = smb_raw_fileinfo(cli->tree, mem_ctx, &finfo);
122         CHECK_STATUS(status, NT_STATUS_OK);
123         CHECK_VALUE(finfo.position_information.out.position, 0);
124
125         printf("Trying max overflow\n");
126         io.in.fnum = fnum;
127         io.in.mode = SEEK_MODE_CURRENT;
128         io.in.offset = 1000;
129         status = smb_raw_seek(cli->tree, &io);
130         CHECK_STATUS(status, NT_STATUS_OK);
131         CHECK_VALUE(io.out.offset, 999);
132
133         printf("Testing position information change\n");
134         finfo.generic.level = RAW_FILEINFO_POSITION_INFORMATION;
135         finfo.position_information.in.fnum = fnum;
136         status = smb_raw_fileinfo(cli->tree, mem_ctx, &finfo);
137         CHECK_STATUS(status, NT_STATUS_OK);
138         CHECK_VALUE(finfo.position_information.out.position, 0);
139
140         printf("trying write to update offset\n");
141         ZERO_STRUCT(c);
142         if (smbcli_write(cli->tree, fnum, 0, c, 0, 2) != 2) {
143                 printf("Write failed - %s\n", smbcli_errstr(cli->tree));
144                 ret = False;
145                 goto done;              
146         }
147
148         printf("Testing position information change\n");
149         finfo.generic.level = RAW_FILEINFO_POSITION_INFORMATION;
150         finfo.position_information.in.fnum = fnum;
151         status = smb_raw_fileinfo(cli->tree, mem_ctx, &finfo);
152         CHECK_STATUS(status, NT_STATUS_OK);
153         CHECK_VALUE(finfo.position_information.out.position, 0);
154
155         io.in.fnum = fnum;
156         io.in.mode = SEEK_MODE_CURRENT;
157         io.in.offset = 0;
158         status = smb_raw_seek(cli->tree, &io);
159         CHECK_STATUS(status, NT_STATUS_OK);
160         CHECK_VALUE(io.out.offset, 2);
161         
162         if (smbcli_read(cli->tree, fnum, c, 0, 1) != 1) {
163                 printf("Read failed - %s\n", smbcli_errstr(cli->tree));
164                 ret = False;
165                 goto done;              
166         }
167
168         printf("Testing position information change\n");
169         finfo.generic.level = RAW_FILEINFO_POSITION_INFORMATION;
170         finfo.position_information.in.fnum = fnum;
171         status = smb_raw_fileinfo(cli->tree, mem_ctx, &finfo);
172         CHECK_STATUS(status, NT_STATUS_OK);
173         CHECK_VALUE(finfo.position_information.out.position, 1);
174
175         status = smb_raw_seek(cli->tree, &io);
176         CHECK_STATUS(status, NT_STATUS_OK);
177         CHECK_VALUE(io.out.offset, 1);
178
179         printf("Testing position information\n");
180         fnum2 = smbcli_open(cli->tree, fname, O_RDWR, DENY_NONE);
181         if (fnum2 == -1) {
182                 printf("2nd open failed - %s\n", smbcli_errstr(cli->tree));
183                 ret = False;
184                 goto done;
185         }
186         sfinfo.generic.level = RAW_SFILEINFO_POSITION_INFORMATION;
187         sfinfo.position_information.file.fnum = fnum2;
188         sfinfo.position_information.in.position = 25;
189         status = smb_raw_setfileinfo(cli->tree, &sfinfo);
190         CHECK_STATUS(status, NT_STATUS_OK);
191
192         finfo.generic.level = RAW_FILEINFO_POSITION_INFORMATION;
193         finfo.position_information.in.fnum = fnum2;
194         status = smb_raw_fileinfo(cli->tree, mem_ctx, &finfo);
195         CHECK_STATUS(status, NT_STATUS_OK);
196         CHECK_VALUE(finfo.position_information.out.position, 25);
197
198         finfo.generic.level = RAW_FILEINFO_POSITION_INFORMATION;
199         finfo.position_information.in.fnum = fnum;
200         status = smb_raw_fileinfo(cli->tree, mem_ctx, &finfo);
201         CHECK_STATUS(status, NT_STATUS_OK);
202         CHECK_VALUE(finfo.position_information.out.position, 1);
203
204         printf("position_information via paths\n");
205
206         sfinfo.generic.level = RAW_SFILEINFO_POSITION_INFORMATION;
207         sfinfo.position_information.file.fname = fname;
208         sfinfo.position_information.in.position = 32;
209         status = smb_raw_setpathinfo(cli->tree, &sfinfo);
210         CHECK_STATUS(status, NT_STATUS_OK);
211
212         finfo.generic.level = RAW_FILEINFO_POSITION_INFORMATION;
213         finfo.position_information.in.fnum = fnum2;
214         status = smb_raw_fileinfo(cli->tree, mem_ctx, &finfo);
215         CHECK_STATUS(status, NT_STATUS_OK);
216         CHECK_VALUE(finfo.position_information.out.position, 25);
217
218         finfo.generic.level = RAW_FILEINFO_POSITION_INFORMATION;
219         finfo.position_information.in.fname = fname;
220         status = smb_raw_pathinfo(cli->tree, mem_ctx, &finfo);
221         CHECK_STATUS(status, NT_STATUS_OK);
222         CHECK_VALUE(finfo.position_information.out.position, 0);
223         
224
225 done:
226         smb_raw_exit(cli->session);
227         smbcli_deltree(cli->tree, BASEDIR);
228         return ret;
229 }
230
231
232 /* 
233    basic testing of seek calls
234 */
235 BOOL torture_raw_seek(void)
236 {
237         struct smbcli_state *cli;
238         BOOL ret = True;
239         TALLOC_CTX *mem_ctx;
240
241         if (!torture_open_connection(&cli)) {
242                 return False;
243         }
244
245         mem_ctx = talloc_init("torture_raw_seek");
246
247         if (!test_seek(cli, mem_ctx)) {
248                 ret = False;
249         }
250
251         torture_close_connection(cli);
252         talloc_free(mem_ctx);
253         return ret;
254 }