2 Unix SMB/CIFS implementation.
4 test alternate data streams
6 Copyright (C) Andrew Tridgell 2004
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.
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.
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/>.
23 #include "torture/torture.h"
24 #include "libcli/raw/libcliraw.h"
25 #include "system/filesys.h"
26 #include "libcli/libcli.h"
27 #include "torture/util.h"
29 #define BASEDIR "\\teststreams"
31 #define CHECK_STATUS(status, correct) do { \
32 if (!NT_STATUS_EQUAL(status, correct)) { \
33 printf("(%s) Incorrect status %s - should be %s\n", \
34 __location__, nt_errstr(status), nt_errstr(correct)); \
39 #define CHECK_VALUE(v, correct) do { \
40 if ((v) != (correct)) { \
41 printf("(%s) Incorrect value %s=%d - should be %d\n", \
42 __location__, #v, (int)v, (int)correct); \
47 check that a stream has the right contents
49 static bool check_stream(struct smbcli_state *cli, const char *location,
51 const char *fname, const char *sname,
55 const char *full_name;
59 full_name = talloc_asprintf(mem_ctx, "%s:%s", fname, sname);
61 fnum = smbcli_open(cli->tree, full_name, O_RDONLY, DENY_NONE);
65 printf("(%s) should have failed stream open of %s\n",
73 printf("(%s) Failed to open stream '%s' - %s\n",
74 location, full_name, smbcli_errstr(cli->tree));
78 buf = talloc_array(mem_ctx, uint8_t, strlen(value)+11);
80 ret = smbcli_read(cli->tree, fnum, buf, 0, strlen(value)+11);
81 if (ret != strlen(value)) {
82 printf("(%s) Failed to read %lu bytes from stream '%s' - got %d\n",
83 location, (long)strlen(value), full_name, (int)ret);
87 if (memcmp(buf, value, strlen(value)) != 0) {
88 printf("(%s) Bad data in stream\n", location);
92 smbcli_close(cli->tree, fnum);
96 static int qsort_string(const void *v1, const void *v2)
98 char * const *s1 = v1;
99 char * const *s2 = v2;
100 return strcmp(*s1, *s2);
103 static int qsort_stream(const void *v1, const void *v2)
105 const struct stream_struct * s1 = v1;
106 const struct stream_struct * s2 = v2;
107 return strcmp(s1->stream_name.s, s2->stream_name.s);
110 static bool check_stream_list(struct smbcli_state *cli, const char *fname,
111 int num_exp, const char **exp)
113 union smb_fileinfo finfo;
116 TALLOC_CTX *tmp_ctx = talloc_new(cli);
118 struct stream_struct *stream_sort;
121 finfo.generic.level = RAW_FILEINFO_STREAM_INFO;
122 finfo.generic.in.file.path = fname;
124 status = smb_raw_pathinfo(cli->tree, tmp_ctx, &finfo);
125 if (!NT_STATUS_IS_OK(status)) {
126 d_fprintf(stderr, "(%s) smb_raw_pathinfo failed: %s\n",
127 __location__, nt_errstr(status));
131 if (finfo.stream_info.out.num_streams != num_exp) {
132 d_fprintf(stderr, "(%s) expected %d streams, got %d\n",
133 __location__, num_exp,
134 finfo.stream_info.out.num_streams);
138 exp_sort = talloc_memdup(tmp_ctx, exp, num_exp * sizeof(*exp));
140 if (exp_sort == NULL) {
144 qsort(exp_sort, num_exp, sizeof(*exp_sort), qsort_string);
146 stream_sort = talloc_memdup(tmp_ctx, finfo.stream_info.out.streams,
147 finfo.stream_info.out.num_streams *
148 sizeof(*stream_sort));
150 if (stream_sort == NULL) {
154 qsort(stream_sort, finfo.stream_info.out.num_streams,
155 sizeof(*stream_sort), qsort_stream);
157 for (i=0; i<num_exp; i++) {
158 if (strcmp(exp_sort[i], stream_sort[i].stream_name.s) != 0) {
159 d_fprintf(stderr, "(%s) expected stream name %s, got "
160 "%s\n", __location__, exp_sort[i],
161 stream_sort[i].stream_name.s);
168 talloc_free(tmp_ctx);
173 test basic io on streams
175 static bool test_stream_io(struct smbcli_state *cli, TALLOC_CTX *mem_ctx)
179 const char *fname = BASEDIR "\\stream.txt";
180 const char *sname1, *sname2;
185 const char *one[] = { "::$DATA" };
186 const char *two[] = { "::$DATA", ":Second Stream:$DATA" };
187 const char *three[] = { "::$DATA", ":Stream One:$DATA",
188 ":Second Stream:$DATA" };
190 sname1 = talloc_asprintf(mem_ctx, "%s:%s", fname, "Stream One");
191 sname2 = talloc_asprintf(mem_ctx, "%s:%s:$DaTa", fname, "Second Stream");
193 printf("(%s) opening non-existant directory stream\n", __location__);
194 io.generic.level = RAW_OPEN_NTCREATEX;
195 io.ntcreatex.in.root_fid = 0;
196 io.ntcreatex.in.flags = 0;
197 io.ntcreatex.in.access_mask = SEC_FILE_WRITE_DATA;
198 io.ntcreatex.in.create_options = NTCREATEX_OPTIONS_DIRECTORY;
199 io.ntcreatex.in.file_attr = FILE_ATTRIBUTE_NORMAL;
200 io.ntcreatex.in.share_access = 0;
201 io.ntcreatex.in.alloc_size = 0;
202 io.ntcreatex.in.open_disposition = NTCREATEX_DISP_CREATE;
203 io.ntcreatex.in.impersonation = NTCREATEX_IMPERSONATION_ANONYMOUS;
204 io.ntcreatex.in.security_flags = 0;
205 io.ntcreatex.in.fname = sname1;
206 status = smb_raw_open(cli->tree, mem_ctx, &io);
207 CHECK_STATUS(status, NT_STATUS_NOT_A_DIRECTORY);
209 printf("(%s) creating a stream on a non-existant file\n", __location__);
210 io.ntcreatex.in.create_options = 0;
211 io.ntcreatex.in.fname = sname1;
212 status = smb_raw_open(cli->tree, mem_ctx, &io);
213 CHECK_STATUS(status, NT_STATUS_OK);
214 fnum = io.ntcreatex.out.file.fnum;
216 ret &= check_stream(cli, __location__, mem_ctx, fname, "Stream One", NULL);
218 printf("(%s) check that open of base file is allowed\n", __location__);
219 io.ntcreatex.in.open_disposition = NTCREATEX_DISP_OPEN;
220 io.ntcreatex.in.fname = fname;
221 status = smb_raw_open(cli->tree, mem_ctx, &io);
222 CHECK_STATUS(status, NT_STATUS_OK);
223 smbcli_close(cli->tree, io.ntcreatex.out.file.fnum);
225 printf("(%s) writing to stream\n", __location__);
226 retsize = smbcli_write(cli->tree, fnum, 0, "test data", 0, 9);
227 CHECK_VALUE(retsize, 9);
229 smbcli_close(cli->tree, fnum);
231 ret &= check_stream(cli, __location__, mem_ctx, fname, "Stream One", "test data");
233 io.ntcreatex.in.open_disposition = NTCREATEX_DISP_OPEN;
234 io.ntcreatex.in.fname = sname1;
235 status = smb_raw_open(cli->tree, mem_ctx, &io);
236 CHECK_STATUS(status, NT_STATUS_OK);
237 fnum = io.ntcreatex.out.file.fnum;
239 printf("(%s) modifying stream\n", __location__);
240 retsize = smbcli_write(cli->tree, fnum, 0, "MORE DATA ", 5, 10);
241 CHECK_VALUE(retsize, 10);
243 smbcli_close(cli->tree, fnum);
245 ret &= check_stream(cli, __location__, mem_ctx, fname, "Stream One:$FOO", NULL);
247 printf("(%s) creating a stream2 on a existing file\n", __location__);
248 io.ntcreatex.in.fname = sname2;
249 io.ntcreatex.in.open_disposition = NTCREATEX_DISP_OPEN_IF;
250 status = smb_raw_open(cli->tree, mem_ctx, &io);
251 CHECK_STATUS(status, NT_STATUS_OK);
252 fnum = io.ntcreatex.out.file.fnum;
254 printf("(%s) modifying stream\n", __location__);
255 retsize = smbcli_write(cli->tree, fnum, 0, "SECOND STREAM", 0, 13);
256 CHECK_VALUE(retsize, 13);
258 smbcli_close(cli->tree, fnum);
260 ret &= check_stream(cli, __location__, mem_ctx, fname, "Stream One", "test MORE DATA ");
261 ret &= check_stream(cli, __location__, mem_ctx, fname, "Stream One:$DATA", "test MORE DATA ");
262 ret &= check_stream(cli, __location__, mem_ctx, fname, "Stream One:", NULL);
263 ret &= check_stream(cli, __location__, mem_ctx, fname, "Second Stream", "SECOND STREAM");
264 ret &= check_stream(cli, __location__, mem_ctx, fname,
265 "SECOND STREAM:$DATA", "SECOND STREAM");
266 ret &= check_stream(cli, __location__, mem_ctx, fname, "Second Stream:$DATA", "SECOND STREAM");
267 ret &= check_stream(cli, __location__, mem_ctx, fname, "Second Stream:", NULL);
268 ret &= check_stream(cli, __location__, mem_ctx, fname, "Second Stream:$FOO", NULL);
270 check_stream_list(cli, fname, 3, three);
272 printf("(%s) deleting stream\n", __location__);
273 status = smbcli_unlink(cli->tree, sname1);
274 CHECK_STATUS(status, NT_STATUS_OK);
276 check_stream_list(cli, fname, 2, two);
278 printf("(%s) delete a stream via delete-on-close\n", __location__);
279 io.ntcreatex.in.fname = sname2;
280 io.ntcreatex.in.create_options = NTCREATEX_OPTIONS_DELETE_ON_CLOSE;
281 io.ntcreatex.in.share_access = NTCREATEX_SHARE_ACCESS_DELETE;
282 io.ntcreatex.in.access_mask = SEC_RIGHTS_FILE_ALL;
283 io.ntcreatex.in.open_disposition = NTCREATEX_DISP_OPEN;
285 status = smb_raw_open(cli->tree, mem_ctx, &io);
286 CHECK_STATUS(status, NT_STATUS_OK);
287 fnum = io.ntcreatex.out.file.fnum;
289 smbcli_close(cli->tree, fnum);
290 status = smbcli_unlink(cli->tree, sname2);
291 CHECK_STATUS(status, NT_STATUS_OBJECT_NAME_NOT_FOUND);
293 check_stream_list(cli, fname, 1, one);
295 io.ntcreatex.in.open_disposition = NTCREATEX_DISP_CREATE;
296 io.ntcreatex.in.fname = sname1;
297 status = smb_raw_open(cli->tree, mem_ctx, &io);
298 CHECK_STATUS(status, NT_STATUS_OK);
299 smbcli_close(cli->tree, io.ntcreatex.out.file.fnum);
300 io.ntcreatex.in.fname = sname2;
301 status = smb_raw_open(cli->tree, mem_ctx, &io);
302 CHECK_STATUS(status, NT_STATUS_OK);
303 smbcli_close(cli->tree, io.ntcreatex.out.file.fnum);
305 printf("(%s) deleting file\n", __location__);
306 status = smbcli_unlink(cli->tree, fname);
307 CHECK_STATUS(status, NT_STATUS_OK);
310 smbcli_close(cli->tree, fnum);
315 basic testing of streams calls
317 bool torture_raw_streams(struct torture_context *torture,
318 struct smbcli_state *cli)
322 if (!torture_setup_dir(cli, BASEDIR)) {
326 ret &= test_stream_io(cli, torture);
328 smb_raw_exit(cli->session);
329 smbcli_deltree(cli->tree, BASEDIR);