71ff9332fd78225a103bda87c87f02ba3fd4a58c
[samba.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
28 #define BASEDIR "\\teststreams"
29
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)); \
34                 ret = False; \
35                 goto done; \
36         }} while (0)
37
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); \
42                 ret = False; \
43         }} while (0)
44
45 /*
46   check that a stream has the right contents
47 */
48 static BOOL check_stream(struct smbcli_state *cli, TALLOC_CTX *mem_ctx,
49                          const char *fname, const char *sname, 
50                          const char *value)
51 {
52         int fnum;
53         const char *full_name;
54         uint8_t *buf;
55         ssize_t ret;
56
57         full_name = talloc_asprintf(mem_ctx, "%s:%s", fname, sname);
58
59         fnum = smbcli_open(cli->tree, full_name, O_RDONLY, DENY_NONE);
60
61         if (value == NULL) {
62                 if (fnum != -1) {
63                         printf("should have failed stream open of %s\n", full_name);
64                         return False;
65                 }
66                 return True;
67         }
68             
69         if (fnum == -1) {
70                 printf("Failed to open stream '%s' - %s\n", 
71                        full_name, smbcli_errstr(cli->tree));
72                 return False;
73         }
74
75         buf = talloc_size(mem_ctx, strlen(value)+11);
76         
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);
81                 return False;
82         }
83
84         if (memcmp(buf, value, strlen(value)) != 0) {
85                 printf("Bad data in stream\n");
86                 return False;
87         }
88
89         smbcli_close(cli->tree, fnum);
90         return True;
91 }
92
93 /*
94   test basic io on streams
95 */
96 static BOOL test_stream_io(struct smbcli_state *cli, TALLOC_CTX *mem_ctx)
97 {
98         NTSTATUS status;
99         union smb_open io;
100         const char *fname = BASEDIR "\\stream.txt";
101         const char *sname1, *sname2;
102         BOOL ret = True;
103         int fnum = -1;
104         ssize_t retsize;
105
106         sname1 = talloc_asprintf(mem_ctx, "%s:%s", fname, "Stream One");
107         sname2 = talloc_asprintf(mem_ctx, "%s:%s:$DaTa", fname, "Second Stream");
108
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);
124
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;
131
132         ret &= check_stream(cli, mem_ctx, fname, "Stream One", NULL);
133
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);
140
141         printf("writing to stream\n");
142         retsize = smbcli_write(cli->tree, fnum, 0, "test data", 0, 9);
143         CHECK_VALUE(retsize, 9);
144
145         smbcli_close(cli->tree, fnum);
146
147         ret &= check_stream(cli, mem_ctx, fname, "Stream One", "test data");
148
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;
154
155         printf("modifying stream\n");
156         retsize = smbcli_write(cli->tree, fnum, 0, "MORE DATA ", 5, 10);
157         CHECK_VALUE(retsize, 10);
158
159         smbcli_close(cli->tree, fnum);
160
161         ret &= check_stream(cli, mem_ctx, fname, "Stream One:$FOO", NULL);
162
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;
169
170         printf("modifying stream\n");
171         retsize = smbcli_write(cli->tree, fnum, 0, "SECOND STREAM", 0, 13);
172         CHECK_VALUE(retsize, 13);
173
174         smbcli_close(cli->tree, fnum);
175
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);
183
184         printf("deleting stream\n");
185         status = smbcli_unlink(cli->tree, sname1);
186         CHECK_STATUS(status, NT_STATUS_OK);
187
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;
197         
198         smbcli_close(cli->tree, fnum);
199         status = smbcli_unlink(cli->tree, sname2);
200         CHECK_STATUS(status, NT_STATUS_OBJECT_NAME_NOT_FOUND);
201
202
203         printf("deleting file\n");
204         status = smbcli_unlink(cli->tree, fname);
205         CHECK_STATUS(status, NT_STATUS_OK);
206
207 done:
208         smbcli_close(cli->tree, fnum);
209         return ret;
210 }
211
212 /* 
213    basic testing of streams calls
214 */
215 BOOL torture_raw_streams(void)
216 {
217         struct smbcli_state *cli;
218         BOOL ret = True;
219         TALLOC_CTX *mem_ctx;
220
221         if (!torture_open_connection(&cli)) {
222                 return False;
223         }
224
225         mem_ctx = talloc_init("torture_raw_streams");
226
227         if (!torture_setup_dir(cli, BASEDIR)) {
228                 return False;
229         }
230
231         ret &= test_stream_io(cli, mem_ctx);
232
233         smb_raw_exit(cli->session);
234         smbcli_deltree(cli->tree, BASEDIR);
235
236         torture_close_connection(cli);
237         talloc_free(mem_ctx);
238         return ret;
239 }