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