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