r14720: Add torture_context argument to all torture tests
[nivanova/samba-autobuild/.git] / source4 / torture / basic / charset.c
1 /* 
2    Unix SMB/CIFS implementation.
3
4    SMB torture tester - charset test routines
5
6    Copyright (C) Andrew Tridgell 2001
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 "libcli/libcli.h"
27 #include "torture/util.h"
28
29 #define BASEDIR "\\chartest\\"
30
31 /* 
32    open a file using a set of unicode code points for the name
33
34    the prefix BASEDIR is added before the name
35 */
36 static NTSTATUS unicode_open(struct smbcli_tree *tree,
37                              TALLOC_CTX *mem_ctx,
38                              uint32_t open_disposition, 
39                              const uint32_t *u_name, 
40                              size_t u_name_len)
41 {
42         union smb_open io;
43         char *fname, *fname2=NULL, *ucs_name;
44         int i;
45         NTSTATUS status;
46
47         ucs_name = talloc_size(mem_ctx, (1+u_name_len)*2);
48         if (!ucs_name) {
49                 printf("Failed to create UCS2 Name - talloc() failure\n");
50                 return NT_STATUS_NO_MEMORY;
51         }
52
53         for (i=0;i<u_name_len;i++) {
54                 SSVAL(ucs_name, i*2, u_name[i]);
55         }
56         SSVAL(ucs_name, i*2, 0);
57
58         i = convert_string_talloc(ucs_name, CH_UTF16, CH_UNIX, ucs_name, (1+u_name_len)*2, (void **)&fname);
59         if (i == -1) {
60                 printf("Failed to convert UCS2 Name into unix - convert_string_talloc() failure\n");
61                 talloc_free(ucs_name);
62                 return NT_STATUS_NO_MEMORY;
63         }
64
65         fname2 = talloc_asprintf(ucs_name, "%s%s", BASEDIR, fname);
66         if (!fname2) {
67                 talloc_free(ucs_name);
68                 return NT_STATUS_NO_MEMORY;
69         }
70
71         io.generic.level = RAW_OPEN_NTCREATEX;
72         io.ntcreatex.in.flags = NTCREATEX_FLAGS_EXTENDED;
73         io.ntcreatex.in.root_fid = 0;
74         io.ntcreatex.in.access_mask = SEC_RIGHTS_FILE_ALL;
75         io.ntcreatex.in.alloc_size = 0;
76         io.ntcreatex.in.file_attr = FILE_ATTRIBUTE_NORMAL;
77         io.ntcreatex.in.share_access = NTCREATEX_SHARE_ACCESS_NONE;
78         io.ntcreatex.in.open_disposition = NTCREATEX_DISP_CREATE;
79         io.ntcreatex.in.create_options = 0;
80         io.ntcreatex.in.impersonation = NTCREATEX_IMPERSONATION_ANONYMOUS;
81         io.ntcreatex.in.security_flags = 0;
82         io.ntcreatex.in.fname = fname2;
83         io.ntcreatex.in.open_disposition = open_disposition;
84
85         status = smb_raw_open(tree, mem_ctx, &io);
86
87         talloc_free(ucs_name);
88
89         return status;
90 }
91
92
93 /*
94   see if the server recognises composed characters
95 */
96 static BOOL test_composed(struct smbcli_state *cli, TALLOC_CTX *mem_ctx)
97 {
98         const uint32_t name1[] = {0x61, 0x308};
99         const uint32_t name2[] = {0xe4};
100         NTSTATUS status1, status2;
101
102         printf("Testing composite character (a umlaut)\n");
103         
104         status1 = unicode_open(cli->tree, mem_ctx, NTCREATEX_DISP_CREATE, name1, 2);
105         if (!NT_STATUS_IS_OK(status1)) {
106                 printf("Failed to create composed name - %s\n",
107                        nt_errstr(status1));
108                 return False;
109         }
110
111         status2 = unicode_open(cli->tree, mem_ctx, NTCREATEX_DISP_CREATE, name2, 1);
112
113         if (!NT_STATUS_IS_OK(status2)) {
114                 printf("Failed to create accented character - %s\n",
115                        nt_errstr(status2));
116                 return False;
117         }
118
119         return True;
120 }
121
122 /*
123   see if the server recognises a naked diacritical
124 */
125 static BOOL test_diacritical(struct smbcli_state *cli, TALLOC_CTX *mem_ctx)
126 {
127         const uint32_t name1[] = {0x308};
128         const uint32_t name2[] = {0x308, 0x308};
129         NTSTATUS status1, status2;
130
131         printf("Testing naked diacritical (umlaut)\n");
132         
133         status1 = unicode_open(cli->tree, mem_ctx, NTCREATEX_DISP_CREATE, name1, 1);
134
135         if (!NT_STATUS_IS_OK(status1)) {
136                 printf("Failed to create naked diacritical - %s\n",
137                        nt_errstr(status1));
138                 return False;
139         }
140
141         /* try a double diacritical */
142         status2 = unicode_open(cli->tree, mem_ctx, NTCREATEX_DISP_CREATE, name2, 2);
143
144         if (!NT_STATUS_IS_OK(status2)) {
145                 printf("Failed to create double naked diacritical - %s\n",
146                        nt_errstr(status2));
147                 return False;
148         }
149
150         return True;
151 }
152
153 /*
154   see if the server recognises a partial surrogate pair
155 */
156 static BOOL test_surrogate(struct smbcli_state *cli, TALLOC_CTX *mem_ctx)
157 {
158         const uint32_t name1[] = {0xd800};
159         const uint32_t name2[] = {0xdc00};
160         const uint32_t name3[] = {0xd800, 0xdc00};
161         NTSTATUS status;
162
163         printf("Testing partial surrogate\n");
164
165         status = unicode_open(cli->tree, mem_ctx, NTCREATEX_DISP_CREATE, name1, 1);
166
167         if (!NT_STATUS_IS_OK(status)) {
168                 printf("Failed to create partial surrogate 1 - %s\n",
169                        nt_errstr(status));
170                 return False;
171         }
172
173         status = unicode_open(cli->tree, mem_ctx, NTCREATEX_DISP_CREATE, name2, 1);
174
175         if (!NT_STATUS_IS_OK(status)) {
176                 printf("Failed to create partial surrogate 2 - %s\n",
177                        nt_errstr(status));
178                 return False;
179         }
180
181         status = unicode_open(cli->tree, mem_ctx, NTCREATEX_DISP_CREATE, name3, 2);
182
183         if (!NT_STATUS_IS_OK(status)) {
184                 printf("Failed to create full surrogate - %s\n",
185                        nt_errstr(status));
186                 return False;
187         }
188
189         return True;
190 }
191
192 /*
193   see if the server recognises wide-a characters
194 */
195 static BOOL test_widea(struct smbcli_state *cli, TALLOC_CTX *mem_ctx)
196 {
197         const uint32_t name1[] = {'a'};
198         const uint32_t name2[] = {0xff41};
199         const uint32_t name3[] = {0xff21};
200         NTSTATUS status;
201
202         printf("Testing wide-a\n");
203         
204         status = unicode_open(cli->tree, mem_ctx, NTCREATEX_DISP_CREATE, name1, 1);
205
206         if (!NT_STATUS_IS_OK(status)) {
207                 printf("Failed to create 'a' - %s\n",
208                        nt_errstr(status));
209                 return False;
210         }
211
212         status = unicode_open(cli->tree, mem_ctx, NTCREATEX_DISP_CREATE, name2, 1);
213
214         if (!NT_STATUS_IS_OK(status)) {
215                 printf("Failed to create wide-a - %s\n",
216                        nt_errstr(status));
217                 return False;
218         }
219
220         status = unicode_open(cli->tree, mem_ctx, NTCREATEX_DISP_CREATE, name3, 1);
221
222         if (!NT_STATUS_EQUAL(status, NT_STATUS_OBJECT_NAME_COLLISION)) {
223                 printf("Expected %s creating wide-A - %s\n",
224                        nt_errstr(NT_STATUS_OBJECT_NAME_COLLISION),
225                        nt_errstr(status));
226                 return False;
227         }
228
229         return True;
230 }
231
232 BOOL torture_charset(struct torture_context *torture)
233 {
234         static struct smbcli_state *cli;
235         BOOL ret = True;
236         TALLOC_CTX *mem_ctx;
237
238         mem_ctx = talloc_init("torture_charset");
239
240         if (!torture_open_connection(&cli)) {
241                 return False;
242         }
243
244         printf("Starting charset tests\n");
245
246         if (!torture_setup_dir(cli, BASEDIR)) {
247                 return False;
248         }
249
250         if (!test_composed(cli, mem_ctx)) {
251                 ret = False;
252         }
253
254         if (!test_diacritical(cli, mem_ctx)) {
255                 ret = False;
256         }
257
258         if (!test_surrogate(cli, mem_ctx)) {
259                 ret = False;
260         }
261
262         if (!test_widea(cli, mem_ctx)) {
263                 ret = False;
264         }
265
266         return ret;
267 }