r16907: Add an index parameter to torture_open_connection. Next step is to enable the
[metze/samba/wip.git] / source4 / torture / raw / streams.c
1 /* 
2    Unix SMB/CIFS implementation.
3
4    test alternate data streams
5
6    Copyright (C) Andrew Tridgell 2004
7    
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.
12    
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.
17    
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.
21 */
22
23 #include "includes.h"
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"
29
30 #define BASEDIR "\\teststreams"
31
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)); \
36                 ret = False; \
37                 goto done; \
38         }} while (0)
39
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); \
44                 ret = False; \
45         }} while (0)
46
47 /*
48   check that a stream has the right contents
49 */
50 static BOOL check_stream(struct smbcli_state *cli, TALLOC_CTX *mem_ctx,
51                          const char *fname, const char *sname, 
52                          const char *value)
53 {
54         int fnum;
55         const char *full_name;
56         uint8_t *buf;
57         ssize_t ret;
58
59         full_name = talloc_asprintf(mem_ctx, "%s:%s", fname, sname);
60
61         fnum = smbcli_open(cli->tree, full_name, O_RDONLY, DENY_NONE);
62
63         if (value == NULL) {
64                 if (fnum != -1) {
65                         printf("should have failed stream open of %s\n", full_name);
66                         return False;
67                 }
68                 return True;
69         }
70             
71         if (fnum == -1) {
72                 printf("Failed to open stream '%s' - %s\n", 
73                        full_name, smbcli_errstr(cli->tree));
74                 return False;
75         }
76
77         buf = talloc_size(mem_ctx, strlen(value)+11);
78         
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);
83                 return False;
84         }
85
86         if (memcmp(buf, value, strlen(value)) != 0) {
87                 printf("Bad data in stream\n");
88                 return False;
89         }
90
91         smbcli_close(cli->tree, fnum);
92         return True;
93 }
94
95 /*
96   test basic io on streams
97 */
98 static BOOL test_stream_io(struct smbcli_state *cli, TALLOC_CTX *mem_ctx)
99 {
100         NTSTATUS status;
101         union smb_open io;
102         const char *fname = BASEDIR "\\stream.txt";
103         const char *sname1, *sname2;
104         BOOL ret = True;
105         int fnum = -1;
106         ssize_t retsize;
107
108         sname1 = talloc_asprintf(mem_ctx, "%s:%s", fname, "Stream One");
109         sname2 = talloc_asprintf(mem_ctx, "%s:%s:$DaTa", fname, "Second Stream");
110
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);
126
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;
133
134         ret &= check_stream(cli, mem_ctx, fname, "Stream One", NULL);
135
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);
142
143         printf("writing to stream\n");
144         retsize = smbcli_write(cli->tree, fnum, 0, "test data", 0, 9);
145         CHECK_VALUE(retsize, 9);
146
147         smbcli_close(cli->tree, fnum);
148
149         ret &= check_stream(cli, mem_ctx, fname, "Stream One", "test data");
150
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;
156
157         printf("modifying stream\n");
158         retsize = smbcli_write(cli->tree, fnum, 0, "MORE DATA ", 5, 10);
159         CHECK_VALUE(retsize, 10);
160
161         smbcli_close(cli->tree, fnum);
162
163         ret &= check_stream(cli, mem_ctx, fname, "Stream One:$FOO", NULL);
164
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;
171
172         printf("modifying stream\n");
173         retsize = smbcli_write(cli->tree, fnum, 0, "SECOND STREAM", 0, 13);
174         CHECK_VALUE(retsize, 13);
175
176         smbcli_close(cli->tree, fnum);
177
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);
185
186         printf("deleting stream\n");
187         status = smbcli_unlink(cli->tree, sname1);
188         CHECK_STATUS(status, NT_STATUS_OK);
189
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;
199         
200         smbcli_close(cli->tree, fnum);
201         status = smbcli_unlink(cli->tree, sname2);
202         CHECK_STATUS(status, NT_STATUS_OBJECT_NAME_NOT_FOUND);
203
204
205         printf("deleting file\n");
206         status = smbcli_unlink(cli->tree, fname);
207         CHECK_STATUS(status, NT_STATUS_OK);
208
209 done:
210         smbcli_close(cli->tree, fnum);
211         return ret;
212 }
213
214 /* 
215    basic testing of streams calls
216 */
217 BOOL torture_raw_streams(struct torture_context *torture)
218 {
219         struct smbcli_state *cli;
220         BOOL ret = True;
221         TALLOC_CTX *mem_ctx;
222
223         if (!torture_open_connection(&cli, 0)) {
224                 return False;
225         }
226
227         mem_ctx = talloc_init("torture_raw_streams");
228
229         if (!torture_setup_dir(cli, BASEDIR)) {
230                 return False;
231         }
232
233         ret &= test_stream_io(cli, mem_ctx);
234
235         smb_raw_exit(cli->session);
236         smbcli_deltree(cli->tree, BASEDIR);
237
238         torture_close_connection(cli);
239         talloc_free(mem_ctx);
240         return ret;
241 }