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"
28 #define BASEDIR "\\teststreams"
30 #define CHECK_STATUS(status, correct) do { \
31 if (!NT_STATUS_EQUAL(status, correct)) { \
32 printf("(%s) Incorrect status %s - should be %s\n", \
33 __location__, nt_errstr(status), nt_errstr(correct)); \
38 #define CHECK_VALUE(v, correct) do { \
39 if ((v) != (correct)) { \
40 printf("(%s) Incorrect value %s=%d - should be %d\n", \
41 __location__, #v, (int)v, (int)correct); \
46 check that a stream has the right contents
48 static BOOL check_stream(struct smbcli_state *cli, TALLOC_CTX *mem_ctx,
49 const char *fname, const char *sname,
53 const char *full_name;
57 full_name = talloc_asprintf(mem_ctx, "%s:%s", fname, sname);
59 fnum = smbcli_open(cli->tree, full_name, O_RDONLY, DENY_NONE);
63 printf("should have failed stream open of %s\n", full_name);
70 printf("Failed to open stream '%s' - %s\n",
71 full_name, smbcli_errstr(cli->tree));
75 buf = talloc_size(mem_ctx, strlen(value)+11);
77 ret = smbcli_read(cli->tree, fnum, buf, 0, strlen(value)+11);
78 if (ret != strlen(value)) {
79 printf("Failed to read %lu bytes from stream '%s' - got %d\n",
80 (long)strlen(value), full_name, (int)ret);
84 if (memcmp(buf, value, strlen(value)) != 0) {
85 printf("Bad data in stream\n");
89 smbcli_close(cli->tree, fnum);
94 test basic io on streams
96 static BOOL test_stream_io(struct smbcli_state *cli, TALLOC_CTX *mem_ctx)
100 const char *fname = BASEDIR "\\stream.txt";
101 const char *sname1, *sname2;
106 sname1 = talloc_asprintf(mem_ctx, "%s:%s", fname, "Stream One");
107 sname2 = talloc_asprintf(mem_ctx, "%s:%s:$DaTa", fname, "Second Stream");
109 printf("opening non-existant directory stream\n");
110 io.generic.level = RAW_OPEN_NTCREATEX;
111 io.ntcreatex.in.root_fid = 0;
112 io.ntcreatex.in.flags = 0;
113 io.ntcreatex.in.access_mask = SEC_FILE_WRITE_DATA;
114 io.ntcreatex.in.create_options = NTCREATEX_OPTIONS_DIRECTORY;
115 io.ntcreatex.in.file_attr = FILE_ATTRIBUTE_NORMAL;
116 io.ntcreatex.in.share_access = 0;
117 io.ntcreatex.in.alloc_size = 0;
118 io.ntcreatex.in.open_disposition = NTCREATEX_DISP_CREATE;
119 io.ntcreatex.in.impersonation = NTCREATEX_IMPERSONATION_ANONYMOUS;
120 io.ntcreatex.in.security_flags = 0;
121 io.ntcreatex.in.fname = sname1;
122 status = smb_raw_open(cli->tree, mem_ctx, &io);
123 CHECK_STATUS(status, NT_STATUS_NOT_A_DIRECTORY);
125 printf("creating a stream on a non-existant file\n");
126 io.ntcreatex.in.create_options = 0;
127 io.ntcreatex.in.fname = sname1;
128 status = smb_raw_open(cli->tree, mem_ctx, &io);
129 CHECK_STATUS(status, NT_STATUS_OK);
130 fnum = io.ntcreatex.out.fnum;
132 ret &= check_stream(cli, mem_ctx, fname, "Stream One", NULL);
134 printf("check that open of base file is allowed\n");
135 io.ntcreatex.in.open_disposition = NTCREATEX_DISP_OPEN;
136 io.ntcreatex.in.fname = fname;
137 status = smb_raw_open(cli->tree, mem_ctx, &io);
138 CHECK_STATUS(status, NT_STATUS_OK);
139 smbcli_close(cli->tree, io.ntcreatex.out.fnum);
141 printf("writing to stream\n");
142 retsize = smbcli_write(cli->tree, fnum, 0, "test data", 0, 9);
143 CHECK_VALUE(retsize, 9);
145 smbcli_close(cli->tree, fnum);
147 ret &= check_stream(cli, mem_ctx, fname, "Stream One", "test data");
149 io.ntcreatex.in.open_disposition = NTCREATEX_DISP_OPEN;
150 io.ntcreatex.in.fname = sname1;
151 status = smb_raw_open(cli->tree, mem_ctx, &io);
152 CHECK_STATUS(status, NT_STATUS_OK);
153 fnum = io.ntcreatex.out.fnum;
155 printf("modifying stream\n");
156 retsize = smbcli_write(cli->tree, fnum, 0, "MORE DATA ", 5, 10);
157 CHECK_VALUE(retsize, 10);
159 smbcli_close(cli->tree, fnum);
161 ret &= check_stream(cli, mem_ctx, fname, "Stream One:$FOO", NULL);
163 printf("creating a stream2 on a existing file\n");
164 io.ntcreatex.in.fname = sname2;
165 io.ntcreatex.in.open_disposition = NTCREATEX_DISP_OPEN_IF;
166 status = smb_raw_open(cli->tree, mem_ctx, &io);
167 CHECK_STATUS(status, NT_STATUS_OK);
168 fnum = io.ntcreatex.out.fnum;
170 printf("modifying stream\n");
171 retsize = smbcli_write(cli->tree, fnum, 0, "SECOND STREAM", 0, 13);
172 CHECK_VALUE(retsize, 13);
174 smbcli_close(cli->tree, fnum);
176 ret &= check_stream(cli, mem_ctx, fname, "Stream One", "test MORE DATA ");
177 ret &= check_stream(cli, mem_ctx, fname, "Stream One:$DATA", "test MORE DATA ");
178 ret &= check_stream(cli, mem_ctx, fname, "Stream One:", NULL);
179 ret &= check_stream(cli, mem_ctx, fname, "Second Stream", "SECOND STREAM");
180 ret &= check_stream(cli, mem_ctx, fname, "Second Stream:$DATA", "SECOND STREAM");
181 ret &= check_stream(cli, mem_ctx, fname, "Second Stream:", NULL);
182 ret &= check_stream(cli, mem_ctx, fname, "Second Stream:$FOO", NULL);
184 printf("deleting stream\n");
185 status = smbcli_unlink(cli->tree, sname1);
186 CHECK_STATUS(status, NT_STATUS_OK);
188 printf("delete a stream via delete-on-close\n");
189 io.ntcreatex.in.fname = sname2;
190 io.ntcreatex.in.create_options = NTCREATEX_OPTIONS_DELETE_ON_CLOSE;
191 io.ntcreatex.in.share_access = NTCREATEX_SHARE_ACCESS_DELETE;
192 io.ntcreatex.in.access_mask = SEC_RIGHTS_FILE_ALL;
193 io.ntcreatex.in.open_disposition = NTCREATEX_DISP_OPEN;
194 status = smb_raw_open(cli->tree, mem_ctx, &io);
195 CHECK_STATUS(status, NT_STATUS_OK);
196 fnum = io.ntcreatex.out.fnum;
198 smbcli_close(cli->tree, fnum);
199 status = smbcli_unlink(cli->tree, sname2);
200 CHECK_STATUS(status, NT_STATUS_OBJECT_NAME_NOT_FOUND);
203 printf("deleting file\n");
204 status = smbcli_unlink(cli->tree, fname);
205 CHECK_STATUS(status, NT_STATUS_OK);
208 smbcli_close(cli->tree, fnum);
213 basic testing of streams calls
215 BOOL torture_raw_streams(void)
217 struct smbcli_state *cli;
221 if (!torture_open_connection(&cli)) {
225 mem_ctx = talloc_init("torture_raw_streams");
227 if (!torture_setup_dir(cli, BASEDIR)) {
231 ret &= test_stream_io(cli, mem_ctx);
233 smb_raw_exit(cli->session);
234 smbcli_deltree(cli->tree, BASEDIR);
236 torture_close_connection(cli);
237 talloc_free(mem_ctx);