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 "libcli/raw/libcliraw.h"
25 #include "librpc/gen_ndr/ndr_security.h"
27 #define BASEDIR "\\teststreams"
29 #define CHECK_STATUS(status, correct) do { \
30 if (!NT_STATUS_EQUAL(status, correct)) { \
31 printf("(%s) Incorrect status %s - should be %s\n", \
32 __location__, nt_errstr(status), nt_errstr(correct)); \
37 #define CHECK_VALUE(v, correct) do { \
38 if ((v) != (correct)) { \
39 printf("(%s) Incorrect value %s=%d - should be %d\n", \
40 __location__, #v, v, correct); \
45 check that a stream has the right contents
47 static BOOL check_stream(struct smbcli_state *cli, TALLOC_CTX *mem_ctx,
48 const char *fname, const char *sname,
52 const char *full_name;
56 full_name = talloc_asprintf(mem_ctx, "%s:%s", fname, sname);
58 fnum = smbcli_open(cli->tree, full_name, O_RDONLY, DENY_NONE);
62 printf("should have failed stream open of %s\n", full_name);
69 printf("Failed to open stream '%s' - %s\n",
70 full_name, smbcli_errstr(cli->tree));
74 buf = talloc(mem_ctx, strlen(value)+11);
76 ret = smbcli_read(cli->tree, fnum, buf, 0, strlen(value)+11);
77 if (ret != strlen(value)) {
78 printf("Failed to read %d bytes from stream '%s' - got %d\n",
79 strlen(value), full_name, ret);
83 if (memcmp(buf, value, strlen(value)) != 0) {
84 printf("Bad data in stream\n");
88 smbcli_close(cli->tree, fnum);
93 test basic io on streams
95 static BOOL test_stream_io(struct smbcli_state *cli, TALLOC_CTX *mem_ctx)
99 const char *fname = BASEDIR "\\stream.txt";
100 const char *sname1, *sname2;
105 sname1 = talloc_asprintf(mem_ctx, "%s:%s", fname, "Stream One");
106 sname2 = talloc_asprintf(mem_ctx, "%s:%s:$DaTa", fname, "Second Stream");
108 printf("opening non-existant directory stream\n");
109 io.generic.level = RAW_OPEN_NTCREATEX;
110 io.ntcreatex.in.root_fid = 0;
111 io.ntcreatex.in.flags = 0;
112 io.ntcreatex.in.access_mask = SEC_FILE_WRITE_DATA;
113 io.ntcreatex.in.create_options = NTCREATEX_OPTIONS_DIRECTORY;
114 io.ntcreatex.in.file_attr = FILE_ATTRIBUTE_NORMAL;
115 io.ntcreatex.in.share_access = 0;
116 io.ntcreatex.in.alloc_size = 0;
117 io.ntcreatex.in.open_disposition = NTCREATEX_DISP_CREATE;
118 io.ntcreatex.in.impersonation = NTCREATEX_IMPERSONATION_ANONYMOUS;
119 io.ntcreatex.in.security_flags = 0;
120 io.ntcreatex.in.fname = sname1;
121 status = smb_raw_open(cli->tree, mem_ctx, &io);
122 CHECK_STATUS(status, NT_STATUS_NOT_A_DIRECTORY);
124 printf("creating a stream on a non-existant file\n");
125 io.ntcreatex.in.create_options = 0;
126 io.ntcreatex.in.fname = sname1;
127 status = smb_raw_open(cli->tree, mem_ctx, &io);
128 CHECK_STATUS(status, NT_STATUS_OK);
129 fnum = io.ntcreatex.out.fnum;
131 ret &= check_stream(cli, mem_ctx, fname, "Stream One", NULL);
133 printf("check that open of base file is allowed\n");
134 io.ntcreatex.in.open_disposition = NTCREATEX_DISP_OPEN;
135 io.ntcreatex.in.fname = fname;
136 status = smb_raw_open(cli->tree, mem_ctx, &io);
137 CHECK_STATUS(status, NT_STATUS_OK);
138 smbcli_close(cli->tree, io.ntcreatex.out.fnum);
140 printf("writing to stream\n");
141 retsize = smbcli_write(cli->tree, fnum, 0, "test data", 0, 9);
142 CHECK_VALUE(retsize, 9);
144 smbcli_close(cli->tree, fnum);
146 ret &= check_stream(cli, mem_ctx, fname, "Stream One", "test data");
148 io.ntcreatex.in.open_disposition = NTCREATEX_DISP_OPEN;
149 io.ntcreatex.in.fname = sname1;
150 status = smb_raw_open(cli->tree, mem_ctx, &io);
151 CHECK_STATUS(status, NT_STATUS_OK);
152 fnum = io.ntcreatex.out.fnum;
154 printf("modifying stream\n");
155 retsize = smbcli_write(cli->tree, fnum, 0, "MORE DATA ", 5, 10);
156 CHECK_VALUE(retsize, 10);
158 smbcli_close(cli->tree, fnum);
160 ret &= check_stream(cli, mem_ctx, fname, "Stream One:$FOO", NULL);
162 printf("creating a stream2 on a existing file\n");
163 io.ntcreatex.in.fname = sname2;
164 io.ntcreatex.in.open_disposition = NTCREATEX_DISP_OPEN_IF;
165 status = smb_raw_open(cli->tree, mem_ctx, &io);
166 CHECK_STATUS(status, NT_STATUS_OK);
167 fnum = io.ntcreatex.out.fnum;
169 printf("modifying stream\n");
170 retsize = smbcli_write(cli->tree, fnum, 0, "SECOND STREAM", 0, 13);
171 CHECK_VALUE(retsize, 13);
173 smbcli_close(cli->tree, fnum);
175 ret &= check_stream(cli, mem_ctx, fname, "Stream One", "test MORE DATA ");
176 ret &= check_stream(cli, mem_ctx, fname, "Stream One:$DATA", "test MORE DATA ");
177 ret &= check_stream(cli, mem_ctx, fname, "Stream One:", NULL);
178 ret &= check_stream(cli, mem_ctx, fname, "Second Stream", "SECOND STREAM");
179 ret &= check_stream(cli, mem_ctx, fname, "Second Stream:$DATA", "SECOND STREAM");
180 ret &= check_stream(cli, mem_ctx, fname, "Second Stream:", NULL);
181 ret &= check_stream(cli, mem_ctx, fname, "Second Stream:$FOO", NULL);
183 printf("deleting stream\n");
184 status = smbcli_unlink(cli->tree, sname1);
185 CHECK_STATUS(status, NT_STATUS_OK);
187 printf("delete a stream via delete-on-close\n");
188 io.ntcreatex.in.fname = sname2;
189 io.ntcreatex.in.create_options = NTCREATEX_OPTIONS_DELETE_ON_CLOSE;
190 io.ntcreatex.in.share_access = NTCREATEX_SHARE_ACCESS_DELETE;
191 io.ntcreatex.in.access_mask = SEC_RIGHTS_FILE_ALL;
192 io.ntcreatex.in.open_disposition = NTCREATEX_DISP_OPEN;
193 status = smb_raw_open(cli->tree, mem_ctx, &io);
194 CHECK_STATUS(status, NT_STATUS_OK);
195 fnum = io.ntcreatex.out.fnum;
197 smbcli_close(cli->tree, fnum);
198 status = smbcli_unlink(cli->tree, sname2);
199 CHECK_STATUS(status, NT_STATUS_OBJECT_NAME_NOT_FOUND);
202 printf("deleting file\n");
203 status = smbcli_unlink(cli->tree, fname);
204 CHECK_STATUS(status, NT_STATUS_OK);
207 smbcli_close(cli->tree, fnum);
212 basic testing of streams calls
214 BOOL torture_raw_streams(void)
216 struct smbcli_state *cli;
220 if (!torture_open_connection(&cli)) {
224 mem_ctx = talloc_init("torture_raw_streams");
226 if (!torture_setup_dir(cli, BASEDIR)) {
230 ret &= test_stream_io(cli, mem_ctx);
232 smb_raw_exit(cli->session);
233 smbcli_deltree(cli->tree, BASEDIR);
235 torture_close_connection(cli);
236 talloc_destroy(mem_ctx);