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"
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, TALLOC_CTX *mem_ctx,
50 const char *fname, const char *sname,
54 const char *full_name;
58 full_name = talloc_asprintf(mem_ctx, "%s:%s", fname, sname);
60 fnum = smbcli_open(cli->tree, full_name, O_RDONLY, DENY_NONE);
64 printf("should have failed stream open of %s\n", full_name);
71 printf("Failed to open stream '%s' - %s\n",
72 full_name, smbcli_errstr(cli->tree));
76 buf = talloc_size(mem_ctx, strlen(value)+11);
78 ret = smbcli_read(cli->tree, fnum, buf, 0, strlen(value)+11);
79 if (ret != strlen(value)) {
80 printf("Failed to read %lu bytes from stream '%s' - got %d\n",
81 (long)strlen(value), full_name, (int)ret);
85 if (memcmp(buf, value, strlen(value)) != 0) {
86 printf("Bad data in stream\n");
90 smbcli_close(cli->tree, fnum);
95 test basic io on streams
97 static BOOL test_stream_io(struct smbcli_state *cli, TALLOC_CTX *mem_ctx)
101 const char *fname = BASEDIR "\\stream.txt";
102 const char *sname1, *sname2;
107 sname1 = talloc_asprintf(mem_ctx, "%s:%s", fname, "Stream One");
108 sname2 = talloc_asprintf(mem_ctx, "%s:%s:$DaTa", fname, "Second Stream");
110 printf("opening non-existant directory stream\n");
111 io.generic.level = RAW_OPEN_NTCREATEX;
112 io.ntcreatex.in.root_fid = 0;
113 io.ntcreatex.in.flags = 0;
114 io.ntcreatex.in.access_mask = SEC_FILE_WRITE_DATA;
115 io.ntcreatex.in.create_options = NTCREATEX_OPTIONS_DIRECTORY;
116 io.ntcreatex.in.file_attr = FILE_ATTRIBUTE_NORMAL;
117 io.ntcreatex.in.share_access = 0;
118 io.ntcreatex.in.alloc_size = 0;
119 io.ntcreatex.in.open_disposition = NTCREATEX_DISP_CREATE;
120 io.ntcreatex.in.impersonation = NTCREATEX_IMPERSONATION_ANONYMOUS;
121 io.ntcreatex.in.security_flags = 0;
122 io.ntcreatex.in.fname = sname1;
123 status = smb_raw_open(cli->tree, mem_ctx, &io);
124 CHECK_STATUS(status, NT_STATUS_NOT_A_DIRECTORY);
126 printf("creating a stream on a non-existant file\n");
127 io.ntcreatex.in.create_options = 0;
128 io.ntcreatex.in.fname = sname1;
129 status = smb_raw_open(cli->tree, mem_ctx, &io);
130 CHECK_STATUS(status, NT_STATUS_OK);
131 fnum = io.ntcreatex.out.fnum;
133 ret &= check_stream(cli, mem_ctx, fname, "Stream One", NULL);
135 printf("check that open of base file is allowed\n");
136 io.ntcreatex.in.open_disposition = NTCREATEX_DISP_OPEN;
137 io.ntcreatex.in.fname = fname;
138 status = smb_raw_open(cli->tree, mem_ctx, &io);
139 CHECK_STATUS(status, NT_STATUS_OK);
140 smbcli_close(cli->tree, io.ntcreatex.out.fnum);
142 printf("writing to stream\n");
143 retsize = smbcli_write(cli->tree, fnum, 0, "test data", 0, 9);
144 CHECK_VALUE(retsize, 9);
146 smbcli_close(cli->tree, fnum);
148 ret &= check_stream(cli, mem_ctx, fname, "Stream One", "test data");
150 io.ntcreatex.in.open_disposition = NTCREATEX_DISP_OPEN;
151 io.ntcreatex.in.fname = sname1;
152 status = smb_raw_open(cli->tree, mem_ctx, &io);
153 CHECK_STATUS(status, NT_STATUS_OK);
154 fnum = io.ntcreatex.out.fnum;
156 printf("modifying stream\n");
157 retsize = smbcli_write(cli->tree, fnum, 0, "MORE DATA ", 5, 10);
158 CHECK_VALUE(retsize, 10);
160 smbcli_close(cli->tree, fnum);
162 ret &= check_stream(cli, mem_ctx, fname, "Stream One:$FOO", NULL);
164 printf("creating a stream2 on a existing file\n");
165 io.ntcreatex.in.fname = sname2;
166 io.ntcreatex.in.open_disposition = NTCREATEX_DISP_OPEN_IF;
167 status = smb_raw_open(cli->tree, mem_ctx, &io);
168 CHECK_STATUS(status, NT_STATUS_OK);
169 fnum = io.ntcreatex.out.fnum;
171 printf("modifying stream\n");
172 retsize = smbcli_write(cli->tree, fnum, 0, "SECOND STREAM", 0, 13);
173 CHECK_VALUE(retsize, 13);
175 smbcli_close(cli->tree, fnum);
177 ret &= check_stream(cli, mem_ctx, fname, "Stream One", "test MORE DATA ");
178 ret &= check_stream(cli, mem_ctx, fname, "Stream One:$DATA", "test MORE DATA ");
179 ret &= check_stream(cli, mem_ctx, fname, "Stream One:", NULL);
180 ret &= check_stream(cli, mem_ctx, fname, "Second Stream", "SECOND STREAM");
181 ret &= check_stream(cli, mem_ctx, fname, "Second Stream:$DATA", "SECOND STREAM");
182 ret &= check_stream(cli, mem_ctx, fname, "Second Stream:", NULL);
183 ret &= check_stream(cli, mem_ctx, fname, "Second Stream:$FOO", NULL);
185 printf("deleting stream\n");
186 status = smbcli_unlink(cli->tree, sname1);
187 CHECK_STATUS(status, NT_STATUS_OK);
189 printf("delete a stream via delete-on-close\n");
190 io.ntcreatex.in.fname = sname2;
191 io.ntcreatex.in.create_options = NTCREATEX_OPTIONS_DELETE_ON_CLOSE;
192 io.ntcreatex.in.share_access = NTCREATEX_SHARE_ACCESS_DELETE;
193 io.ntcreatex.in.access_mask = SEC_RIGHTS_FILE_ALL;
194 io.ntcreatex.in.open_disposition = NTCREATEX_DISP_OPEN;
195 status = smb_raw_open(cli->tree, mem_ctx, &io);
196 CHECK_STATUS(status, NT_STATUS_OK);
197 fnum = io.ntcreatex.out.fnum;
199 smbcli_close(cli->tree, fnum);
200 status = smbcli_unlink(cli->tree, sname2);
201 CHECK_STATUS(status, NT_STATUS_OBJECT_NAME_NOT_FOUND);
204 printf("deleting file\n");
205 status = smbcli_unlink(cli->tree, fname);
206 CHECK_STATUS(status, NT_STATUS_OK);
209 smbcli_close(cli->tree, fnum);
214 basic testing of streams calls
216 BOOL torture_raw_streams(void)
218 struct smbcli_state *cli;
222 if (!torture_open_connection(&cli)) {
226 mem_ctx = talloc_init("torture_raw_streams");
228 if (!torture_setup_dir(cli, BASEDIR)) {
232 ret &= test_stream_io(cli, mem_ctx);
234 smb_raw_exit(cli->session);
235 smbcli_deltree(cli->tree, BASEDIR);
237 torture_close_connection(cli);
238 talloc_free(mem_ctx);