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 2 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, write to the Free Software
20 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 #include "torture/torture.h"
25 #include "libcli/raw/libcliraw.h"
26 #include "system/filesys.h"
27 #include "libcli/libcli.h"
28 #include "torture/util.h"
30 #define BASEDIR "\\teststreams"
32 #define CHECK_STATUS(status, correct) do { \
33 if (!NT_STATUS_EQUAL(status, correct)) { \
34 printf("(%s) Incorrect status %s - should be %s\n", \
35 __location__, nt_errstr(status), nt_errstr(correct)); \
40 #define CHECK_VALUE(v, correct) do { \
41 if ((v) != (correct)) { \
42 printf("(%s) Incorrect value %s=%d - should be %d\n", \
43 __location__, #v, (int)v, (int)correct); \
48 check that a stream has the right contents
50 static BOOL check_stream(struct smbcli_state *cli, TALLOC_CTX *mem_ctx,
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("should have failed stream open of %s\n", full_name);
72 printf("Failed to open stream '%s' - %s\n",
73 full_name, smbcli_errstr(cli->tree));
77 buf = talloc_size(mem_ctx, strlen(value)+11);
79 ret = smbcli_read(cli->tree, fnum, buf, 0, strlen(value)+11);
80 if (ret != strlen(value)) {
81 printf("Failed to read %lu bytes from stream '%s' - got %d\n",
82 (long)strlen(value), full_name, (int)ret);
86 if (memcmp(buf, value, strlen(value)) != 0) {
87 printf("Bad data in stream\n");
91 smbcli_close(cli->tree, fnum);
96 test basic io on streams
98 static BOOL test_stream_io(struct smbcli_state *cli, TALLOC_CTX *mem_ctx)
102 const char *fname = BASEDIR "\\stream.txt";
103 const char *sname1, *sname2;
108 sname1 = talloc_asprintf(mem_ctx, "%s:%s", fname, "Stream One");
109 sname2 = talloc_asprintf(mem_ctx, "%s:%s:$DaTa", fname, "Second Stream");
111 printf("opening non-existant directory stream\n");
112 io.generic.level = RAW_OPEN_NTCREATEX;
113 io.ntcreatex.in.root_fid = 0;
114 io.ntcreatex.in.flags = 0;
115 io.ntcreatex.in.access_mask = SEC_FILE_WRITE_DATA;
116 io.ntcreatex.in.create_options = NTCREATEX_OPTIONS_DIRECTORY;
117 io.ntcreatex.in.file_attr = FILE_ATTRIBUTE_NORMAL;
118 io.ntcreatex.in.share_access = 0;
119 io.ntcreatex.in.alloc_size = 0;
120 io.ntcreatex.in.open_disposition = NTCREATEX_DISP_CREATE;
121 io.ntcreatex.in.impersonation = NTCREATEX_IMPERSONATION_ANONYMOUS;
122 io.ntcreatex.in.security_flags = 0;
123 io.ntcreatex.in.fname = sname1;
124 status = smb_raw_open(cli->tree, mem_ctx, &io);
125 CHECK_STATUS(status, NT_STATUS_NOT_A_DIRECTORY);
127 printf("creating a stream on a non-existant file\n");
128 io.ntcreatex.in.create_options = 0;
129 io.ntcreatex.in.fname = sname1;
130 status = smb_raw_open(cli->tree, mem_ctx, &io);
131 CHECK_STATUS(status, NT_STATUS_OK);
132 fnum = io.ntcreatex.out.file.fnum;
134 ret &= check_stream(cli, mem_ctx, fname, "Stream One", NULL);
136 printf("check that open of base file is allowed\n");
137 io.ntcreatex.in.open_disposition = NTCREATEX_DISP_OPEN;
138 io.ntcreatex.in.fname = fname;
139 status = smb_raw_open(cli->tree, mem_ctx, &io);
140 CHECK_STATUS(status, NT_STATUS_OK);
141 smbcli_close(cli->tree, io.ntcreatex.out.file.fnum);
143 printf("writing to stream\n");
144 retsize = smbcli_write(cli->tree, fnum, 0, "test data", 0, 9);
145 CHECK_VALUE(retsize, 9);
147 smbcli_close(cli->tree, fnum);
149 ret &= check_stream(cli, mem_ctx, fname, "Stream One", "test data");
151 io.ntcreatex.in.open_disposition = NTCREATEX_DISP_OPEN;
152 io.ntcreatex.in.fname = sname1;
153 status = smb_raw_open(cli->tree, mem_ctx, &io);
154 CHECK_STATUS(status, NT_STATUS_OK);
155 fnum = io.ntcreatex.out.file.fnum;
157 printf("modifying stream\n");
158 retsize = smbcli_write(cli->tree, fnum, 0, "MORE DATA ", 5, 10);
159 CHECK_VALUE(retsize, 10);
161 smbcli_close(cli->tree, fnum);
163 ret &= check_stream(cli, mem_ctx, fname, "Stream One:$FOO", NULL);
165 printf("creating a stream2 on a existing file\n");
166 io.ntcreatex.in.fname = sname2;
167 io.ntcreatex.in.open_disposition = NTCREATEX_DISP_OPEN_IF;
168 status = smb_raw_open(cli->tree, mem_ctx, &io);
169 CHECK_STATUS(status, NT_STATUS_OK);
170 fnum = io.ntcreatex.out.file.fnum;
172 printf("modifying stream\n");
173 retsize = smbcli_write(cli->tree, fnum, 0, "SECOND STREAM", 0, 13);
174 CHECK_VALUE(retsize, 13);
176 smbcli_close(cli->tree, fnum);
178 ret &= check_stream(cli, mem_ctx, fname, "Stream One", "test MORE DATA ");
179 ret &= check_stream(cli, mem_ctx, fname, "Stream One:$DATA", "test MORE DATA ");
180 ret &= check_stream(cli, mem_ctx, fname, "Stream One:", NULL);
181 ret &= check_stream(cli, mem_ctx, fname, "Second Stream", "SECOND STREAM");
182 ret &= check_stream(cli, mem_ctx, fname, "Second Stream:$DATA", "SECOND STREAM");
183 ret &= check_stream(cli, mem_ctx, fname, "Second Stream:", NULL);
184 ret &= check_stream(cli, mem_ctx, fname, "Second Stream:$FOO", NULL);
186 printf("deleting stream\n");
187 status = smbcli_unlink(cli->tree, sname1);
188 CHECK_STATUS(status, NT_STATUS_OK);
190 printf("delete a stream via delete-on-close\n");
191 io.ntcreatex.in.fname = sname2;
192 io.ntcreatex.in.create_options = NTCREATEX_OPTIONS_DELETE_ON_CLOSE;
193 io.ntcreatex.in.share_access = NTCREATEX_SHARE_ACCESS_DELETE;
194 io.ntcreatex.in.access_mask = SEC_RIGHTS_FILE_ALL;
195 io.ntcreatex.in.open_disposition = NTCREATEX_DISP_OPEN;
196 status = smb_raw_open(cli->tree, mem_ctx, &io);
197 CHECK_STATUS(status, NT_STATUS_OK);
198 fnum = io.ntcreatex.out.file.fnum;
200 smbcli_close(cli->tree, fnum);
201 status = smbcli_unlink(cli->tree, sname2);
202 CHECK_STATUS(status, NT_STATUS_OBJECT_NAME_NOT_FOUND);
205 printf("deleting file\n");
206 status = smbcli_unlink(cli->tree, fname);
207 CHECK_STATUS(status, NT_STATUS_OK);
210 smbcli_close(cli->tree, fnum);
215 basic testing of streams calls
217 BOOL torture_raw_streams(struct torture_context *torture)
219 struct smbcli_state *cli;
223 if (!torture_open_connection(&cli)) {
227 mem_ctx = talloc_init("torture_raw_streams");
229 if (!torture_setup_dir(cli, BASEDIR)) {
233 ret &= test_stream_io(cli, mem_ctx);
235 smb_raw_exit(cli->session);
236 smbcli_deltree(cli->tree, BASEDIR);
238 torture_close_connection(cli);
239 talloc_free(mem_ctx);