r4063: - change char * -> uint8_t in struct request_buffer
[gd/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 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 "libcli/raw/libcliraw.h"
25 #include "librpc/gen_ndr/ndr_security.h"
26
27 #define BASEDIR "\\teststreams"
28
29 #define CHECK_STATUS(status, correct) do { \
30         if (!NT_STATUS_EQUAL(status, correct)) { \
31                 printf("(%s) Incorrect status %s - should be %s\n", \
32                        __location__, nt_errstr(status), nt_errstr(correct)); \
33                 ret = False; \
34                 goto done; \
35         }} while (0)
36
37 #define CHECK_VALUE(v, correct) do { \
38         if ((v) != (correct)) { \
39                 printf("(%s) Incorrect value %s=%d - should be %d\n", \
40                        __location__, #v, v, correct); \
41                 ret = False; \
42         }} while (0)
43
44 /*
45   check that a stream has the right contents
46 */
47 static BOOL check_stream(struct smbcli_state *cli, TALLOC_CTX *mem_ctx,
48                          const char *fname, const char *sname, 
49                          const char *value)
50 {
51         int fnum;
52         const char *full_name;
53         uint8_t *buf;
54         ssize_t ret;
55
56         full_name = talloc_asprintf(mem_ctx, "%s:%s", fname, sname);
57
58         fnum = smbcli_open(cli->tree, full_name, O_RDONLY, DENY_NONE);
59
60         if (value == NULL) {
61                 if (fnum != -1) {
62                         printf("should have failed stream open of %s\n", full_name);
63                         return False;
64                 }
65                 return True;
66         }
67             
68         if (fnum == -1) {
69                 printf("Failed to open stream '%s' - %s\n", 
70                        full_name, smbcli_errstr(cli->tree));
71                 return False;
72         }
73
74         buf = talloc(mem_ctx, strlen(value)+11);
75         
76         ret = smbcli_read(cli->tree, fnum, buf, 0, strlen(value)+11);
77         if (ret != strlen(value)) {
78                 printf("Failed to read %d bytes from stream '%s' - got %d\n",
79                        strlen(value), full_name, ret);
80                 return False;
81         }
82
83         if (memcmp(buf, value, strlen(value)) != 0) {
84                 printf("Bad data in stream\n");
85                 return False;
86         }
87
88         smbcli_close(cli->tree, fnum);
89         return True;
90 }
91
92 /*
93   test basic io on streams
94 */
95 static BOOL test_stream_io(struct smbcli_state *cli, TALLOC_CTX *mem_ctx)
96 {
97         NTSTATUS status;
98         union smb_open io;
99         const char *fname = BASEDIR "\\stream.txt";
100         const char *sname1, *sname2;
101         BOOL ret = True;
102         int fnum = -1;
103         ssize_t retsize;
104
105         sname1 = talloc_asprintf(mem_ctx, "%s:%s", fname, "Stream One");
106         sname2 = talloc_asprintf(mem_ctx, "%s:%s:$DaTa", fname, "Second Stream");
107
108         printf("opening non-existant directory stream\n");
109         io.generic.level = RAW_OPEN_NTCREATEX;
110         io.ntcreatex.in.root_fid = 0;
111         io.ntcreatex.in.flags = 0;
112         io.ntcreatex.in.access_mask = SEC_FILE_WRITE_DATA;
113         io.ntcreatex.in.create_options = NTCREATEX_OPTIONS_DIRECTORY;
114         io.ntcreatex.in.file_attr = FILE_ATTRIBUTE_NORMAL;
115         io.ntcreatex.in.share_access = 0;
116         io.ntcreatex.in.alloc_size = 0;
117         io.ntcreatex.in.open_disposition = NTCREATEX_DISP_CREATE;
118         io.ntcreatex.in.impersonation = NTCREATEX_IMPERSONATION_ANONYMOUS;
119         io.ntcreatex.in.security_flags = 0;
120         io.ntcreatex.in.fname = sname1;
121         status = smb_raw_open(cli->tree, mem_ctx, &io);
122         CHECK_STATUS(status, NT_STATUS_NOT_A_DIRECTORY);
123
124         printf("creating a stream on a non-existant file\n");
125         io.ntcreatex.in.create_options = 0;
126         io.ntcreatex.in.fname = sname1;
127         status = smb_raw_open(cli->tree, mem_ctx, &io);
128         CHECK_STATUS(status, NT_STATUS_OK);
129         fnum = io.ntcreatex.out.fnum;
130
131         ret &= check_stream(cli, mem_ctx, fname, "Stream One", NULL);
132
133         printf("check that open of base file is allowed\n");
134         io.ntcreatex.in.open_disposition = NTCREATEX_DISP_OPEN;
135         io.ntcreatex.in.fname = fname;
136         status = smb_raw_open(cli->tree, mem_ctx, &io);
137         CHECK_STATUS(status, NT_STATUS_OK);
138         smbcli_close(cli->tree, io.ntcreatex.out.fnum);
139
140         printf("writing to stream\n");
141         retsize = smbcli_write(cli->tree, fnum, 0, "test data", 0, 9);
142         CHECK_VALUE(retsize, 9);
143
144         smbcli_close(cli->tree, fnum);
145
146         ret &= check_stream(cli, mem_ctx, fname, "Stream One", "test data");
147
148         io.ntcreatex.in.open_disposition = NTCREATEX_DISP_OPEN;
149         io.ntcreatex.in.fname = sname1;
150         status = smb_raw_open(cli->tree, mem_ctx, &io);
151         CHECK_STATUS(status, NT_STATUS_OK);
152         fnum = io.ntcreatex.out.fnum;
153
154         printf("modifying stream\n");
155         retsize = smbcli_write(cli->tree, fnum, 0, "MORE DATA ", 5, 10);
156         CHECK_VALUE(retsize, 10);
157
158         smbcli_close(cli->tree, fnum);
159
160         ret &= check_stream(cli, mem_ctx, fname, "Stream One:$FOO", NULL);
161
162         printf("creating a stream2 on a existing file\n");
163         io.ntcreatex.in.fname = sname2;
164         io.ntcreatex.in.open_disposition = NTCREATEX_DISP_OPEN_IF;
165         status = smb_raw_open(cli->tree, mem_ctx, &io);
166         CHECK_STATUS(status, NT_STATUS_OK);
167         fnum = io.ntcreatex.out.fnum;
168
169         printf("modifying stream\n");
170         retsize = smbcli_write(cli->tree, fnum, 0, "SECOND STREAM", 0, 13);
171         CHECK_VALUE(retsize, 13);
172
173         smbcli_close(cli->tree, fnum);
174
175         ret &= check_stream(cli, mem_ctx, fname, "Stream One", "test MORE DATA ");
176         ret &= check_stream(cli, mem_ctx, fname, "Stream One:$DATA", "test MORE DATA ");
177         ret &= check_stream(cli, mem_ctx, fname, "Stream One:", NULL);
178         ret &= check_stream(cli, mem_ctx, fname, "Second Stream", "SECOND STREAM");
179         ret &= check_stream(cli, mem_ctx, fname, "Second Stream:$DATA", "SECOND STREAM");
180         ret &= check_stream(cli, mem_ctx, fname, "Second Stream:", NULL);
181         ret &= check_stream(cli, mem_ctx, fname, "Second Stream:$FOO", NULL);
182
183         printf("deleting stream\n");
184         status = smbcli_unlink(cli->tree, sname1);
185         CHECK_STATUS(status, NT_STATUS_OK);
186
187         printf("delete a stream via delete-on-close\n");
188         io.ntcreatex.in.fname = sname2;
189         io.ntcreatex.in.create_options = NTCREATEX_OPTIONS_DELETE_ON_CLOSE;
190         io.ntcreatex.in.share_access = NTCREATEX_SHARE_ACCESS_DELETE;
191         io.ntcreatex.in.access_mask = SEC_RIGHTS_FILE_ALL;
192         io.ntcreatex.in.open_disposition = NTCREATEX_DISP_OPEN;
193         status = smb_raw_open(cli->tree, mem_ctx, &io);
194         CHECK_STATUS(status, NT_STATUS_OK);
195         fnum = io.ntcreatex.out.fnum;
196         
197         smbcli_close(cli->tree, fnum);
198         status = smbcli_unlink(cli->tree, sname2);
199         CHECK_STATUS(status, NT_STATUS_OBJECT_NAME_NOT_FOUND);
200
201
202         printf("deleting file\n");
203         status = smbcli_unlink(cli->tree, fname);
204         CHECK_STATUS(status, NT_STATUS_OK);
205
206 done:
207         smbcli_close(cli->tree, fnum);
208         return True;
209 }
210
211 /* 
212    basic testing of streams calls
213 */
214 BOOL torture_raw_streams(void)
215 {
216         struct smbcli_state *cli;
217         BOOL ret = True;
218         TALLOC_CTX *mem_ctx;
219
220         if (!torture_open_connection(&cli)) {
221                 return False;
222         }
223
224         mem_ctx = talloc_init("torture_raw_streams");
225
226         if (!torture_setup_dir(cli, BASEDIR)) {
227                 return False;
228         }
229
230         ret &= test_stream_io(cli, mem_ctx);
231
232         smb_raw_exit(cli->session);
233         smbcli_deltree(cli->tree, BASEDIR);
234
235         torture_close_connection(cli);
236         talloc_destroy(mem_ctx);
237         return ret;
238 }