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