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