[WINSOCK]
[reactos.git] / rostests / winetests / kernel32 / codepage.c
1 /*
2 * Unit tests for code page to/from unicode translations
3 *
4 * Copyright (c) 2002 Dmitry Timoshkov
5 * Copyright (c) 2008 Colin Finck
6 *
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
11 *
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
20 */
21
22 #include <stdarg.h>
23 #include <limits.h>
24
25 #include "wine/test.h"
26 #include "windef.h"
27 #include "winbase.h"
28 #include "winnls.h"
29
30 static const WCHAR foobarW[] = {'f','o','o','b','a','r',0};
31
32 static void test_destination_buffer(void)
33 {
34 LPSTR buffer;
35 INT maxsize;
36 INT needed;
37 INT len;
38
39 SetLastError(0xdeadbeef);
40 needed = WideCharToMultiByte(CP_ACP, 0, foobarW, -1, NULL, 0, NULL, NULL);
41 ok( (needed > 0), "returned %d with %u (expected '> 0')\n",
42 needed, GetLastError());
43
44 maxsize = needed*2;
45 buffer = HeapAlloc(GetProcessHeap(), 0, maxsize);
46 if (buffer == NULL) return;
47
48 maxsize--;
49 memset(buffer, 'x', maxsize);
50 buffer[maxsize] = '\0';
51 SetLastError(0xdeadbeef);
52 len = WideCharToMultiByte(CP_ACP, 0, foobarW, -1, buffer, needed+1, NULL, NULL);
53 ok( (len > 0), "returned %d with %u and '%s' (expected '> 0')\n",
54 len, GetLastError(), buffer);
55
56 memset(buffer, 'x', maxsize);
57 buffer[maxsize] = '\0';
58 SetLastError(0xdeadbeef);
59 len = WideCharToMultiByte(CP_ACP, 0, foobarW, -1, buffer, needed, NULL, NULL);
60 ok( (len > 0), "returned %d with %u and '%s' (expected '> 0')\n",
61 len, GetLastError(), buffer);
62
63 memset(buffer, 'x', maxsize);
64 buffer[maxsize] = '\0';
65 SetLastError(0xdeadbeef);
66 len = WideCharToMultiByte(CP_ACP, 0, foobarW, -1, buffer, needed-1, NULL, NULL);
67 ok( !len && (GetLastError() == ERROR_INSUFFICIENT_BUFFER),
68 "returned %d with %u and '%s' (expected '0' with "
69 "ERROR_INSUFFICIENT_BUFFER)\n", len, GetLastError(), buffer);
70
71 memset(buffer, 'x', maxsize);
72 buffer[maxsize] = '\0';
73 SetLastError(0xdeadbeef);
74 len = WideCharToMultiByte(CP_ACP, 0, foobarW, -1, buffer, 1, NULL, NULL);
75 ok( !len && (GetLastError() == ERROR_INSUFFICIENT_BUFFER),
76 "returned %d with %u and '%s' (expected '0' with "
77 "ERROR_INSUFFICIENT_BUFFER)\n", len, GetLastError(), buffer);
78
79 SetLastError(0xdeadbeef);
80 len = WideCharToMultiByte(CP_ACP, 0, foobarW, -1, buffer, 0, NULL, NULL);
81 ok( (len > 0), "returned %d with %u (expected '> 0')\n",
82 len, GetLastError());
83
84 SetLastError(0xdeadbeef);
85 len = WideCharToMultiByte(CP_ACP, 0, foobarW, -1, NULL, needed, NULL, NULL);
86 ok( !len && (GetLastError() == ERROR_INVALID_PARAMETER),
87 "returned %d with %u (expected '0' with "
88 "ERROR_INVALID_PARAMETER)\n", len, GetLastError());
89
90 HeapFree(GetProcessHeap(), 0, buffer);
91 }
92
93
94 static void test_null_source(void)
95 {
96 int len;
97 DWORD GLE;
98
99 SetLastError(0);
100 len = WideCharToMultiByte(CP_ACP, 0, NULL, 0, NULL, 0, NULL, NULL);
101 GLE = GetLastError();
102 ok(!len && GLE == ERROR_INVALID_PARAMETER,
103 "WideCharToMultiByte returned %d with GLE=%u (expected 0 with ERROR_INVALID_PARAMETER)\n",
104 len, GLE);
105
106 SetLastError(0);
107 len = WideCharToMultiByte(CP_ACP, 0, NULL, -1, NULL, 0, NULL, NULL);
108 GLE = GetLastError();
109 ok(!len && GLE == ERROR_INVALID_PARAMETER,
110 "WideCharToMultiByte returned %d with GLE=%u (expected 0 with ERROR_INVALID_PARAMETER)\n",
111 len, GLE);
112 }
113
114 /* lstrcmpW is not supported on Win9x! */
115 static int mylstrcmpW(const WCHAR* str1, const WCHAR* str2)
116 {
117 while (*str1 && *str1==*str2) {
118 str1++;
119 str2++;
120 }
121 return *str1-*str2;
122 }
123
124 static void test_negative_source_length(void)
125 {
126 int len;
127 char buf[10];
128 WCHAR bufW[10];
129
130 /* Test, whether any negative source length works as strlen() + 1 */
131 SetLastError( 0xdeadbeef );
132 memset(buf,'x',sizeof(buf));
133 len = WideCharToMultiByte(CP_ACP, 0, foobarW, -2002, buf, 10, NULL, NULL);
134 ok(len == 7 && GetLastError() == 0xdeadbeef,
135 "WideCharToMultiByte(-2002): len=%d error=%u\n", len, GetLastError());
136 ok(!lstrcmpA(buf, "foobar"),
137 "WideCharToMultiByte(-2002): expected \"foobar\" got \"%s\"\n", buf);
138
139 SetLastError( 0xdeadbeef );
140 memset(bufW,'x',sizeof(bufW));
141 len = MultiByteToWideChar(CP_ACP, 0, "foobar", -2002, bufW, 10);
142 ok(len == 7 && !mylstrcmpW(bufW, foobarW) && GetLastError() == 0xdeadbeef,
143 "MultiByteToWideChar(-2002): len=%d error=%u\n", len, GetLastError());
144
145 SetLastError(0xdeadbeef);
146 memset(bufW, 'x', sizeof(bufW));
147 len = MultiByteToWideChar(CP_ACP, 0, "foobar", -1, bufW, 6);
148 ok(len == 0 && GetLastError() == ERROR_INSUFFICIENT_BUFFER,
149 "MultiByteToWideChar(-1): len=%d error=%u\n", len, GetLastError());
150 }
151
152 #define LONGBUFLEN 100000
153 static void test_negative_dest_length(void)
154 {
155 int len, i;
156 static char buf[LONGBUFLEN];
157 static WCHAR originalW[LONGBUFLEN];
158 static char originalA[LONGBUFLEN];
159 DWORD theError;
160
161 /* Test return on -1 dest length */
162 SetLastError( 0xdeadbeef );
163 memset(buf,'x',sizeof(buf));
164 len = WideCharToMultiByte(CP_ACP, 0, foobarW, -1, buf, -1, NULL, NULL);
165 todo_wine {
166 ok(len == 0 && GetLastError() == ERROR_INVALID_PARAMETER,
167 "WideCharToMultiByte(destlen -1): len=%d error=%x\n", len, GetLastError());
168 }
169
170 /* Test return on -1000 dest length */
171 SetLastError( 0xdeadbeef );
172 memset(buf,'x',sizeof(buf));
173 len = WideCharToMultiByte(CP_ACP, 0, foobarW, -1, buf, -1000, NULL, NULL);
174 todo_wine {
175 ok(len == 0 && GetLastError() == ERROR_INVALID_PARAMETER,
176 "WideCharToMultiByte(destlen -1000): len=%d error=%x\n", len, GetLastError());
177 }
178
179 /* Test return on INT_MAX dest length */
180 SetLastError( 0xdeadbeef );
181 memset(buf,'x',sizeof(buf));
182 len = WideCharToMultiByte(CP_ACP, 0, foobarW, -1, buf, INT_MAX, NULL, NULL);
183 ok(len == 7 && !lstrcmpA(buf, "foobar") && GetLastError() == 0xdeadbeef,
184 "WideCharToMultiByte(destlen INT_MAX): len=%d error=%x\n", len, GetLastError());
185
186 /* Test return on INT_MAX dest length and very long input */
187 SetLastError( 0xdeadbeef );
188 memset(buf,'x',sizeof(buf));
189 for (i=0; i < LONGBUFLEN - 1; i++) {
190 originalW[i] = 'Q';
191 originalA[i] = 'Q';
192 }
193 originalW[LONGBUFLEN-1] = 0;
194 originalA[LONGBUFLEN-1] = 0;
195 len = WideCharToMultiByte(CP_ACP, 0, originalW, -1, buf, INT_MAX, NULL, NULL);
196 theError = GetLastError();
197 ok(len == LONGBUFLEN && !lstrcmpA(buf, originalA) && theError == 0xdeadbeef,
198 "WideCharToMultiByte(srclen %d, destlen INT_MAX): len %d error=%x\n", LONGBUFLEN, len, theError);
199
200 }
201
202 static void test_overlapped_buffers(void)
203 {
204 static const WCHAR strW[] = {'j','u','s','t',' ','a',' ','t','e','s','t',0};
205 static const char strA[] = "just a test";
206 char buf[256];
207 int ret;
208
209 SetLastError(0xdeadbeef);
210 memcpy(buf + 1, strW, sizeof(strW));
211 ret = WideCharToMultiByte(CP_ACP, 0, (WCHAR *)(buf + 1), -1, buf, sizeof(buf), NULL, NULL);
212 ok(ret == sizeof(strA), "unexpected ret %d\n", ret);
213 ok(!memcmp(buf, strA, sizeof(strA)), "conversion failed: %s\n", buf);
214 ok(GetLastError() == 0xdeadbeef, "GetLastError() is %u\n", GetLastError());
215 }
216
217 static void test_string_conversion(LPBOOL bUsedDefaultChar)
218 {
219 char mbc;
220 char mbs[5];
221 int ret;
222 WCHAR wc1 = 228; /* Western Windows-1252 character */
223 WCHAR wc2 = 1088; /* Russian Windows-1251 character not displayable for Windows-1252 */
224 WCHAR wcs[5] = {'T', 'h', 1088, 'i', 0}; /* String with ASCII characters and a Russian character */
225 WCHAR dbwcs[3] = {28953, 25152, 0}; /* String with Chinese (codepage 950) characters */
226
227 SetLastError(0xdeadbeef);
228 ret = WideCharToMultiByte(1252, 0, &wc1, 1, &mbc, 1, NULL, bUsedDefaultChar);
229 ok(ret == 1, "ret is %d\n", ret);
230 ok(mbc == -28, "mbc is %d\n", mbc);
231 if(bUsedDefaultChar) ok(*bUsedDefaultChar == FALSE, "bUsedDefaultChar is %d\n", *bUsedDefaultChar);
232 ok(GetLastError() == 0xdeadbeef, "GetLastError() is %u\n", GetLastError());
233
234 SetLastError(0xdeadbeef);
235 ret = WideCharToMultiByte(1252, 0, &wc2, 1, &mbc, 1, NULL, bUsedDefaultChar);
236 ok(ret == 1, "ret is %d\n", ret);
237 ok(mbc == 63, "mbc is %d\n", mbc);
238 if(bUsedDefaultChar) ok(*bUsedDefaultChar == TRUE, "bUsedDefaultChar is %d\n", *bUsedDefaultChar);
239 ok(GetLastError() == 0xdeadbeef, "GetLastError() is %u\n", GetLastError());
240
241 if (IsValidCodePage(1251))
242 {
243 SetLastError(0xdeadbeef);
244 ret = WideCharToMultiByte(1251, 0, &wc2, 1, &mbc, 1, NULL, bUsedDefaultChar);
245 ok(ret == 1, "ret is %d\n", ret);
246 ok(mbc == -16, "mbc is %d\n", mbc);
247 if(bUsedDefaultChar) ok(*bUsedDefaultChar == FALSE, "bUsedDefaultChar is %d\n", *bUsedDefaultChar);
248 ok(GetLastError() == 0xdeadbeef ||
249 broken(GetLastError() == 0), /* win95 */
250 "GetLastError() is %u\n", GetLastError());
251
252 SetLastError(0xdeadbeef);
253 ret = WideCharToMultiByte(1251, 0, &wc1, 1, &mbc, 1, NULL, bUsedDefaultChar);
254 ok(ret == 1, "ret is %d\n", ret);
255 ok(mbc == 97, "mbc is %d\n", mbc);
256 if(bUsedDefaultChar) ok(*bUsedDefaultChar == FALSE, "bUsedDefaultChar is %d\n", *bUsedDefaultChar);
257 ok(GetLastError() == 0xdeadbeef, "GetLastError() is %u\n", GetLastError());
258 }
259 else
260 skip("Codepage 1251 not available\n");
261
262 /* This call triggers the last Win32 error */
263 SetLastError(0xdeadbeef);
264 ret = WideCharToMultiByte(1252, 0, wcs, -1, &mbc, 1, NULL, bUsedDefaultChar);
265 ok(ret == 0, "ret is %d\n", ret);
266 ok(mbc == 84, "mbc is %d\n", mbc);
267 if(bUsedDefaultChar) ok(*bUsedDefaultChar == FALSE, "bUsedDefaultChar is %d\n", *bUsedDefaultChar);
268 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER, "GetLastError() is %u\n", GetLastError());
269
270 SetLastError(0xdeadbeef);
271 ret = WideCharToMultiByte(1252, 0, wcs, -1, mbs, sizeof(mbs), NULL, bUsedDefaultChar);
272 ok(ret == 5, "ret is %d\n", ret);
273 ok(!strcmp(mbs, "Th?i"), "mbs is %s\n", mbs);
274 if(bUsedDefaultChar) ok(*bUsedDefaultChar == TRUE, "bUsedDefaultChar is %d\n", *bUsedDefaultChar);
275 ok(GetLastError() == 0xdeadbeef, "GetLastError() is %u\n", GetLastError());
276 mbs[0] = 0;
277
278 /* WideCharToMultiByte mustn't add any null character automatically.
279 So in this case, we should get the same string again, even if we only copied the first three bytes. */
280 SetLastError(0xdeadbeef);
281 ret = WideCharToMultiByte(1252, 0, wcs, 3, mbs, sizeof(mbs), NULL, bUsedDefaultChar);
282 ok(ret == 3, "ret is %d\n", ret);
283 ok(!strcmp(mbs, "Th?i"), "mbs is %s\n", mbs);
284 if(bUsedDefaultChar) ok(*bUsedDefaultChar == TRUE, "bUsedDefaultChar is %d\n", *bUsedDefaultChar);
285 ok(GetLastError() == 0xdeadbeef, "GetLastError() is %u\n", GetLastError());
286 ZeroMemory(mbs, 5);
287
288 /* Now this shouldn't be the case like above as we zeroed the complete string buffer. */
289 SetLastError(0xdeadbeef);
290 ret = WideCharToMultiByte(1252, 0, wcs, 3, mbs, sizeof(mbs), NULL, bUsedDefaultChar);
291 ok(ret == 3, "ret is %d\n", ret);
292 ok(!strcmp(mbs, "Th?"), "mbs is %s\n", mbs);
293 if(bUsedDefaultChar) ok(*bUsedDefaultChar == TRUE, "bUsedDefaultChar is %d\n", *bUsedDefaultChar);
294 ok(GetLastError() == 0xdeadbeef, "GetLastError() is %u\n", GetLastError());
295
296 /* Double-byte tests */
297 ret = WideCharToMultiByte(1252, 0, dbwcs, 3, mbs, sizeof(mbs), NULL, bUsedDefaultChar);
298 ok(ret == 3, "ret is %d\n", ret);
299 ok(!strcmp(mbs, "??"), "mbs is %s\n", mbs);
300 if(bUsedDefaultChar) ok(*bUsedDefaultChar == TRUE, "bUsedDefaultChar is %d\n", *bUsedDefaultChar);
301
302 /* Length-only tests */
303 SetLastError(0xdeadbeef);
304 ret = WideCharToMultiByte(1252, 0, &wc2, 1, NULL, 0, NULL, bUsedDefaultChar);
305 ok(ret == 1, "ret is %d\n", ret);
306 if(bUsedDefaultChar) ok(*bUsedDefaultChar == TRUE, "bUsedDefaultChar is %d\n", *bUsedDefaultChar);
307 ok(GetLastError() == 0xdeadbeef, "GetLastError() is %u\n", GetLastError());
308
309 SetLastError(0xdeadbeef);
310 ret = WideCharToMultiByte(1252, 0, wcs, -1, NULL, 0, NULL, bUsedDefaultChar);
311 ok(ret == 5, "ret is %d\n", ret);
312 if(bUsedDefaultChar) ok(*bUsedDefaultChar == TRUE, "bUsedDefaultChar is %d\n", *bUsedDefaultChar);
313 ok(GetLastError() == 0xdeadbeef, "GetLastError() is %u\n", GetLastError());
314
315 if (!IsValidCodePage(950))
316 {
317 skip("Codepage 950 not available\n");
318 return;
319 }
320
321 /* Double-byte tests */
322 SetLastError(0xdeadbeef);
323 ret = WideCharToMultiByte(950, 0, dbwcs, -1, mbs, sizeof(mbs), NULL, bUsedDefaultChar);
324 ok(ret == 5, "ret is %d\n", ret);
325 ok(!strcmp(mbs, "\xb5H\xa9\xd2"), "mbs is %s\n", mbs);
326 if(bUsedDefaultChar) ok(*bUsedDefaultChar == FALSE, "bUsedDefaultChar is %d\n", *bUsedDefaultChar);
327 ok(GetLastError() == 0xdeadbeef, "GetLastError() is %u\n", GetLastError());
328
329 SetLastError(0xdeadbeef);
330 ret = WideCharToMultiByte(950, 0, dbwcs, 1, &mbc, 1, NULL, bUsedDefaultChar);
331 ok(ret == 0, "ret is %d\n", ret);
332 if(bUsedDefaultChar) ok(*bUsedDefaultChar == FALSE, "bUsedDefaultChar is %d\n", *bUsedDefaultChar);
333 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER, "GetLastError() is %u\n", GetLastError());
334 ZeroMemory(mbs, 5);
335
336 SetLastError(0xdeadbeef);
337 ret = WideCharToMultiByte(950, 0, dbwcs, 1, mbs, sizeof(mbs), NULL, bUsedDefaultChar);
338 ok(ret == 2, "ret is %d\n", ret);
339 ok(!strcmp(mbs, "\xb5H"), "mbs is %s\n", mbs);
340 if(bUsedDefaultChar) ok(*bUsedDefaultChar == FALSE, "bUsedDefaultChar is %d\n", *bUsedDefaultChar);
341 ok(GetLastError() == 0xdeadbeef, "GetLastError() is %u\n", GetLastError());
342
343 /* Length-only tests */
344 SetLastError(0xdeadbeef);
345 ret = WideCharToMultiByte(950, 0, dbwcs, 1, NULL, 0, NULL, bUsedDefaultChar);
346 ok(ret == 2, "ret is %d\n", ret);
347 if(bUsedDefaultChar) ok(*bUsedDefaultChar == FALSE, "bUsedDefaultChar is %d\n", *bUsedDefaultChar);
348 ok(GetLastError() == 0xdeadbeef, "GetLastError() is %u\n", GetLastError());
349
350 SetLastError(0xdeadbeef);
351 ret = WideCharToMultiByte(950, 0, dbwcs, -1, NULL, 0, NULL, bUsedDefaultChar);
352 ok(ret == 5, "ret is %d\n", ret);
353 if(bUsedDefaultChar) ok(*bUsedDefaultChar == FALSE, "bUsedDefaultChar is %d\n", *bUsedDefaultChar);
354 ok(GetLastError() == 0xdeadbeef, "GetLastError() is %u\n", GetLastError());
355 }
356
357 START_TEST(codepage)
358 {
359 BOOL bUsedDefaultChar;
360
361 test_destination_buffer();
362 test_null_source();
363 test_negative_source_length();
364 test_negative_dest_length();
365 test_overlapped_buffers();
366
367 /* WideCharToMultiByte has two code paths, test both here */
368 test_string_conversion(NULL);
369 test_string_conversion(&bUsedDefaultChar);
370 }