msvcrt/tests: Fix 3 tests that always fail on Vista.
[wine/hacks.git] / dlls / msvcrt / tests / string.c
blob5247515037b9a2394103c09f4eb4eff16592155d
1 /*
2 * Unit test suite for string functions.
4 * Copyright 2004 Uwe Bonnes
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21 #include "wine/test.h"
22 #include "winbase.h"
23 #include <string.h>
24 #include <mbstring.h>
25 #include <stdlib.h>
26 #include <stdio.h>
27 #include <mbctype.h>
28 #include <locale.h>
29 #include <errno.h>
31 static char *buf_to_string(const unsigned char *bin, int len, int nr)
33 static char buf[2][1024];
34 char *w = buf[nr];
35 int i;
37 for (i = 0; i < len; i++)
39 sprintf(w, "%02x ", (unsigned char)bin[i]);
40 w += strlen(w);
42 return buf[nr];
45 #define expect_eq(expr, value, type, format) { type ret = (expr); ok((value) == ret, #expr " expected " format " got " format "\n", value, ret); }
46 #define expect_bin(buf, value, len) { ok(memcmp((buf), value, len) == 0, "Binary buffer mismatch - expected %s, got %s\n", buf_to_string((unsigned char *)value, len, 1), buf_to_string((buf), len, 0)); }
48 static void* (*pmemcpy)(void *, const void *, size_t n);
49 static int* (*pmemcmp)(void *, const void *, size_t n);
50 static int (*pstrcpy_s)(char *dst, size_t len, const char *src);
51 static int (*pstrcat_s)(char *dst, size_t len, const char *src);
52 static int (*p_mbsnbcpy_s)(unsigned char * dst, size_t size, const unsigned char * src, size_t count);
53 static int (*p_wcscpy_s)(wchar_t *wcDest, size_t size, const wchar_t *wcSrc);
55 #define SETNOFAIL(x,y) x = (void*)GetProcAddress(hMsvcrt,y)
56 #define SET(x,y) SETNOFAIL(x,y); ok(x != NULL, "Export '%s' not found\n", y)
58 HMODULE hMsvcrt;
60 static void test_swab( void ) {
61 char original[] = "BADCFEHGJILKNMPORQTSVUXWZY@#";
62 char expected1[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ@#";
63 char expected2[] = "ABCDEFGHIJKLMNOPQRSTUVWX$";
64 char expected3[] = "$";
66 char from[30];
67 char to[30];
69 int testsize;
71 /* Test 1 - normal even case */
72 memset(to,'$', sizeof(to));
73 memset(from,'@', sizeof(from));
74 testsize = 26;
75 memcpy(from, original, testsize);
76 _swab( from, to, testsize );
77 ok(memcmp(to,expected1,testsize) == 0, "Testing even size %d returned '%*.*s'\n", testsize, testsize, testsize, to);
79 /* Test 2 - uneven case */
80 memset(to,'$', sizeof(to));
81 memset(from,'@', sizeof(from));
82 testsize = 25;
83 memcpy(from, original, testsize);
84 _swab( from, to, testsize );
85 ok(memcmp(to,expected2,testsize) == 0, "Testing odd size %d returned '%*.*s'\n", testsize, testsize, testsize, to);
87 /* Test 3 - from = to */
88 memset(to,'$', sizeof(to));
89 memset(from,'@', sizeof(from));
90 testsize = 26;
91 memcpy(to, original, testsize);
92 _swab( to, to, testsize );
93 ok(memcmp(to,expected1,testsize) == 0, "Testing overlapped size %d returned '%*.*s'\n", testsize, testsize, testsize, to);
95 /* Test 4 - 1 bytes */
96 memset(to,'$', sizeof(to));
97 memset(from,'@', sizeof(from));
98 testsize = 1;
99 memcpy(from, original, testsize);
100 _swab( from, to, testsize );
101 ok(memcmp(to,expected3,testsize) == 0, "Testing small size %d returned '%*.*s'\n", testsize, testsize, testsize, to);
104 #if 0 /* use this to generate more tests */
106 static void test_codepage(int cp)
108 int i;
109 int prev;
110 int count = 1;
112 ok(_setmbcp(cp) == 0, "Couldn't set mbcp\n");
114 prev = _mbctype[0];
115 printf("static int result_cp_%d_mbctype[] = { ", cp);
116 for (i = 1; i < 257; i++)
118 if (_mbctype[i] != prev)
120 printf("0x%x,%d, ", prev, count);
121 prev = _mbctype[i];
122 count = 1;
124 else
125 count++;
127 printf("0x%x,%d };\n", prev, count);
130 #define test_codepage_todo(cp, todo) test_codepage(cp)
132 #else
134 /* RLE-encoded mbctype tables for given codepages */
135 static int result_cp_932_mbctype[] = { 0x0,65, 0x8,1, 0x18,26, 0x8,6, 0x28,26, 0x8,4,
136 0x0,1, 0x8,1, 0xc,31, 0x8,1, 0xa,5, 0x9,58, 0xc,29, 0,3 };
137 static int result_cp_936_mbctype[] = { 0x0,65, 0x8,1, 0x18,26, 0x8,6, 0x28,26, 0x8,6,
138 0xc,126, 0,1 };
139 static int result_cp_949_mbctype[] = { 0x0,66, 0x18,26, 0x8,6, 0x28,26, 0x8,6, 0xc,126,
140 0,1 };
141 static int result_cp_950_mbctype[] = { 0x0,65, 0x8,1, 0x18,26, 0x8,6, 0x28,26, 0x8,4,
142 0x0,2, 0x4,32, 0xc,94, 0,1 };
144 static int todo_none[] = { -2 };
145 static int todo_cp_932[] = { 254, -2 };
147 void test_cp_table(int cp, int *result, int *todo)
149 int i;
150 int count = 0;
151 int curr = 0;
152 _setmbcp(cp);
153 for (i = 0; i < 256; i++)
155 if (count == 0)
157 curr = result[0];
158 count = result[1];
159 result += 2;
161 if (i == *todo + 1)
163 todo_wine ok(_mbctype[i] == curr, "CP%d: Mismatch in ctype for character %d - %d instead of %d\n", cp, i-1, _mbctype[i], curr);
164 todo++;
166 else
167 ok(_mbctype[i] == curr, "CP%d: Mismatch in ctype for character %d - %d instead of %d\n", cp, i-1, _mbctype[i], curr);
168 count--;
172 #define test_codepage(num) test_cp_table(num, result_cp_##num##_mbctype, todo_none);
173 #define test_codepage_todo(num, todo) test_cp_table(num, result_cp_##num##_mbctype, todo);
175 #endif
177 static void test_mbcp(void)
179 int mb_orig_max = __mb_cur_max;
180 int curr_mbcp = _getmbcp();
181 unsigned char *mbstring = (unsigned char *)"\xb0\xb1\xb2 \xb3\xb4 \xb5"; /* incorrect string */
182 unsigned char *mbstring2 = (unsigned char *)"\xb0\xb1\xb2\xb3Q\xb4\xb5"; /* correct string */
183 unsigned char *mbsonlylead = (unsigned char *)"\xb0\0\xb1\xb2 \xb3";
184 unsigned char buf[16];
185 int step;
187 /* _mbtype tests */
189 /* An SBCS codepage test. The ctype of characters on e.g. CP1252 or CP1250 differs slightly
190 * between versions of Windows. Also Windows 9x seems to ignore the codepage and always uses
191 * CP1252 (or the ACP?) so we test only a few ASCII characters */
192 _setmbcp(1252);
193 expect_eq(_mbctype[10], 0, char, "%x");
194 expect_eq(_mbctype[50], 0, char, "%x");
195 expect_eq(_mbctype[66], _SBUP, char, "%x");
196 expect_eq(_mbctype[100], _SBLOW, char, "%x");
197 expect_eq(_mbctype[128], 0, char, "%x");
198 _setmbcp(1250);
199 expect_eq(_mbctype[10], 0, char, "%x");
200 expect_eq(_mbctype[50], 0, char, "%x");
201 expect_eq(_mbctype[66], _SBUP, char, "%x");
202 expect_eq(_mbctype[100], _SBLOW, char, "%x");
203 expect_eq(_mbctype[128], 0, char, "%x");
205 /* double byte code pages */
206 test_codepage_todo(932, todo_cp_932);
207 test_codepage(936);
208 test_codepage(949);
209 test_codepage(950);
211 _setmbcp(936);
212 ok(__mb_cur_max == mb_orig_max, "__mb_cur_max shouldn't be updated (is %d != %d)\n", __mb_cur_max, mb_orig_max);
213 ok(_ismbblead('\354'), "\354 should be a lead byte\n");
214 ok(_ismbblead(' ') == FALSE, "' ' should not be a lead byte\n");
215 ok(_ismbblead(0x1234b0), "0x1234b0 should not be a lead byte\n");
216 ok(_ismbblead(0x123420) == FALSE, "0x123420 should not be a lead byte\n");
217 ok(_ismbbtrail('\xb0'), "\xa0 should be a trail byte\n");
218 ok(_ismbbtrail(' ') == FALSE, "' ' should not be a trail byte\n");
220 /* _ismbslead */
221 expect_eq(_ismbslead(mbstring, &mbstring[0]), -1, int, "%d");
222 expect_eq(_ismbslead(mbstring, &mbstring[1]), FALSE, int, "%d");
223 expect_eq(_ismbslead(mbstring, &mbstring[2]), -1, int, "%d");
224 expect_eq(_ismbslead(mbstring, &mbstring[3]), FALSE, int, "%d");
225 expect_eq(_ismbslead(mbstring, &mbstring[4]), -1, int, "%d");
226 expect_eq(_ismbslead(mbstring, &mbstring[5]), FALSE, int, "%d");
227 expect_eq(_ismbslead(mbstring, &mbstring[6]), FALSE, int, "%d");
228 expect_eq(_ismbslead(mbstring, &mbstring[7]), -1, int, "%d");
229 expect_eq(_ismbslead(mbstring, &mbstring[8]), FALSE, int, "%d");
231 expect_eq(_ismbslead(mbsonlylead, &mbsonlylead[0]), -1, int, "%d");
232 expect_eq(_ismbslead(mbsonlylead, &mbsonlylead[1]), FALSE, int, "%d");
233 expect_eq(_ismbslead(mbsonlylead, &mbsonlylead[2]), FALSE, int, "%d");
234 expect_eq(_ismbslead(mbsonlylead, &mbsonlylead[5]), FALSE, int, "%d");
236 /* _ismbstrail */
237 expect_eq(_ismbstrail(mbstring, &mbstring[0]), FALSE, int, "%d");
238 expect_eq(_ismbstrail(mbstring, &mbstring[1]), -1, int, "%d");
239 expect_eq(_ismbstrail(mbstring, &mbstring[2]), FALSE, int, "%d");
240 expect_eq(_ismbstrail(mbstring, &mbstring[3]), -1, int, "%d");
241 expect_eq(_ismbstrail(mbstring, &mbstring[4]), FALSE, int, "%d");
242 expect_eq(_ismbstrail(mbstring, &mbstring[5]), -1, int, "%d");
243 expect_eq(_ismbstrail(mbstring, &mbstring[6]), FALSE, int, "%d");
244 expect_eq(_ismbstrail(mbstring, &mbstring[7]), FALSE, int, "%d");
245 expect_eq(_ismbstrail(mbstring, &mbstring[8]), -1, int, "%d");
247 expect_eq(_ismbstrail(mbsonlylead, &mbsonlylead[0]), FALSE, int, "%d");
248 expect_eq(_ismbstrail(mbsonlylead, &mbsonlylead[1]), -1, int, "%d");
249 expect_eq(_ismbstrail(mbsonlylead, &mbsonlylead[2]), FALSE, int, "%d");
250 expect_eq(_ismbstrail(mbsonlylead, &mbsonlylead[3]), FALSE, int, "%d");
251 expect_eq(_ismbstrail(mbsonlylead, &mbsonlylead[4]), FALSE, int, "%d");
252 expect_eq(_ismbstrail(mbsonlylead, &mbsonlylead[5]), FALSE, int, "%d");
254 /* _mbsbtype */
255 expect_eq(_mbsbtype(mbstring, 0), _MBC_LEAD, int, "%d");
256 expect_eq(_mbsbtype(mbstring, 1), _MBC_TRAIL, int, "%d");
257 expect_eq(_mbsbtype(mbstring, 2), _MBC_LEAD, int, "%d");
258 expect_eq(_mbsbtype(mbstring, 3), _MBC_ILLEGAL, int, "%d");
259 expect_eq(_mbsbtype(mbstring, 4), _MBC_LEAD, int, "%d");
260 expect_eq(_mbsbtype(mbstring, 5), _MBC_TRAIL, int, "%d");
261 expect_eq(_mbsbtype(mbstring, 6), _MBC_SINGLE, int, "%d");
262 expect_eq(_mbsbtype(mbstring, 7), _MBC_LEAD, int, "%d");
263 expect_eq(_mbsbtype(mbstring, 8), _MBC_ILLEGAL, int, "%d");
265 expect_eq(_mbsbtype(mbsonlylead, 0), _MBC_LEAD, int, "%d");
266 expect_eq(_mbsbtype(mbsonlylead, 1), _MBC_ILLEGAL, int, "%d");
267 expect_eq(_mbsbtype(mbsonlylead, 2), _MBC_ILLEGAL, int, "%d");
268 expect_eq(_mbsbtype(mbsonlylead, 3), _MBC_ILLEGAL, int, "%d");
269 expect_eq(_mbsbtype(mbsonlylead, 4), _MBC_ILLEGAL, int, "%d");
270 expect_eq(_mbsbtype(mbsonlylead, 5), _MBC_ILLEGAL, int, "%d");
272 /* _mbsnextc */
273 expect_eq(_mbsnextc(mbstring), 0xb0b1, int, "%x");
274 expect_eq(_mbsnextc(&mbstring[2]), 0xb220, int, "%x"); /* lead + invalid tail */
275 expect_eq(_mbsnextc(&mbstring[3]), 0x20, int, "%x"); /* single char */
277 /* _mbclen/_mbslen */
278 expect_eq(_mbclen(mbstring), 2, int, "%d");
279 expect_eq(_mbclen(&mbstring[2]), 2, int, "%d");
280 expect_eq(_mbclen(&mbstring[3]), 1, int, "%d");
281 expect_eq(_mbslen(mbstring2), 4, int, "%d");
282 expect_eq(_mbslen(mbsonlylead), 0, int, "%d"); /* lead + NUL not counted as character */
283 expect_eq(_mbslen(mbstring), 4, int, "%d"); /* lead + invalid trail counted */
285 /* _mbccpy/_mbsncpy */
286 memset(buf, 0xff, sizeof(buf));
287 _mbccpy(buf, mbstring);
288 expect_bin(buf, "\xb0\xb1\xff", 3);
290 memset(buf, 0xff, sizeof(buf));
291 _mbsncpy(buf, mbstring, 1);
292 expect_bin(buf, "\xb0\xb1\xff", 3);
293 memset(buf, 0xff, sizeof(buf));
294 _mbsncpy(buf, mbstring, 2);
295 expect_bin(buf, "\xb0\xb1\xb2 \xff", 5);
296 memset(buf, 0xff, sizeof(buf));
297 _mbsncpy(buf, mbstring, 3);
298 expect_bin(buf, "\xb0\xb1\xb2 \xb3\xb4\xff", 7);
299 memset(buf, 0xff, sizeof(buf));
300 _mbsncpy(buf, mbstring, 4);
301 expect_bin(buf, "\xb0\xb1\xb2 \xb3\xb4 \xff", 8);
302 memset(buf, 0xff, sizeof(buf));
303 _mbsncpy(buf, mbstring, 5);
304 expect_bin(buf, "\xb0\xb1\xb2 \xb3\xb4 \0\0\xff", 10);
305 memset(buf, 0xff, sizeof(buf));
306 _mbsncpy(buf, mbsonlylead, 6);
307 expect_bin(buf, "\0\0\0\0\0\0\0\xff", 8);
309 memset(buf, 0xff, sizeof(buf));
310 _mbsnbcpy(buf, mbstring2, 2);
311 expect_bin(buf, "\xb0\xb1\xff", 3);
312 _mbsnbcpy(buf, mbstring2, 3);
313 expect_bin(buf, "\xb0\xb1\0\xff", 4);
314 _mbsnbcpy(buf, mbstring2, 4);
315 expect_bin(buf, "\xb0\xb1\xb2\xb3\xff", 5);
316 memset(buf, 0xff, sizeof(buf));
317 _mbsnbcpy(buf, mbsonlylead, 5);
318 expect_bin(buf, "\0\0\0\0\0\xff", 6);
320 /* _mbsinc/mbsdec */
321 step = _mbsinc(mbstring) - mbstring;
322 ok(step == 2, "_mbsinc adds %d (exp. 2)\n", step);
323 step = _mbsinc(&mbstring[2]) - &mbstring[2]; /* lead + invalid tail */
324 ok(step == 2, "_mbsinc adds %d (exp. 2)\n", step);
326 step = _mbsninc(mbsonlylead, 1) - mbsonlylead;
327 ok(step == 0, "_mbsninc adds %d (exp. 0)\n", step);
328 step = _mbsninc(mbsonlylead, 2) - mbsonlylead; /* lead + NUL byte + lead + char */
329 ok(step == 0, "_mbsninc adds %d (exp. 0)\n", step);
330 step = _mbsninc(mbstring2, 0) - mbstring2;
331 ok(step == 0, "_mbsninc adds %d (exp. 2)\n", step);
332 step = _mbsninc(mbstring2, 1) - mbstring2;
333 ok(step == 2, "_mbsninc adds %d (exp. 2)\n", step);
334 step = _mbsninc(mbstring2, 2) - mbstring2;
335 ok(step == 4, "_mbsninc adds %d (exp. 4)\n", step);
336 step = _mbsninc(mbstring2, 3) - mbstring2;
337 ok(step == 5, "_mbsninc adds %d (exp. 5)\n", step);
338 step = _mbsninc(mbstring2, 4) - mbstring2;
339 ok(step == 7, "_mbsninc adds %d (exp. 7)\n", step);
340 step = _mbsninc(mbstring2, 5) - mbstring2;
341 ok(step == 7, "_mbsninc adds %d (exp. 7)\n", step);
342 step = _mbsninc(mbstring2, 17) - mbstring2;
343 ok(step == 7, "_mbsninc adds %d (exp. 7)\n", step);
345 /* functions that depend on locale codepage, not mbcp.
346 * we hope the current locale to be SBCS because setlocale(LC_ALL, ".1252") seems not to work yet
347 * (as of Wine 0.9.43)
349 if (__mb_cur_max == 1)
351 expect_eq(mblen((char *)mbstring, 3), 1, int, "%x");
352 expect_eq(_mbstrlen((char *)mbstring2), 7, int, "%d");
354 else
355 skip("Current locale has double-byte charset - could leave to false positives\n");
357 _setmbcp(curr_mbcp);
360 static void test_mbsspn( void)
362 unsigned char str1[]="cabernet";
363 unsigned char str2[]="shiraz";
364 unsigned char set[]="abc";
365 unsigned char empty[]="";
366 int ret;
367 ret=_mbsspn( str1, set);
368 ok( ret==3, "_mbsspn returns %d should be 3\n", ret);
369 ret=_mbsspn( str2, set);
370 ok( ret==0, "_mbsspn returns %d should be 0\n", ret);
371 ret=_mbsspn( str1, empty);
372 ok( ret==0, "_mbsspn returns %d should be 0\n", ret);
375 static void test_mbsspnp( void)
377 unsigned char str1[]="cabernet";
378 unsigned char str2[]="shiraz";
379 unsigned char set[]="abc";
380 unsigned char empty[]="";
381 unsigned char full[]="abcenrt";
382 unsigned char* ret;
383 ret=_mbsspnp( str1, set);
384 ok( ret[0]=='e', "_mbsspnp returns %c should be e\n", ret[0]);
385 ret=_mbsspnp( str2, set);
386 ok( ret[0]=='s', "_mbsspnp returns %c should be s\n", ret[0]);
387 ret=_mbsspnp( str1, empty);
388 ok( ret[0]=='c', "_mbsspnp returns %c should be c\n", ret[0]);
389 ret=_mbsspnp( str1, full);
390 ok( ret==NULL, "_mbsspnp returns %p should be NULL\n", ret);
393 static void test_strdup(void)
395 char *str;
396 str = _strdup( 0 );
397 ok( str == 0, "strdup returns %s should be 0\n", str);
398 free( str );
401 static void test_strcpy_s(void)
403 char dest[8];
404 const char *small = "small";
405 const char *big = "atoolongstringforthislittledestination";
406 int ret;
408 if(!pstrcpy_s)
410 skip("strcpy_s not found\n");
411 return;
414 memset(dest, 'X', sizeof(dest));
415 ret = pstrcpy_s(dest, sizeof(dest), small);
416 ok(ret == 0, "Copying a string into a big enough destination returned %d, expected 0\n", ret);
417 ok(dest[0] == 's' && dest[1] == 'm' && dest[2] == 'a' && dest[3] == 'l' &&
418 dest[4] == 'l' && dest[5] == '\0'&& dest[6] == 'X' && dest[7] == 'X',
419 "Unexpected return data from strcpy_s: 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x\n",
420 dest[0], dest[1], dest[2], dest[3], dest[4], dest[5], dest[6], dest[7]);
422 memset(dest, 'X', sizeof(dest));
423 ret = pstrcpy_s(dest, 0, big);
424 ok(ret == EINVAL, "Copying into a destination of size 0 returned %d, expected EINVAL\n", ret);
425 ok(dest[0] == 'X' && dest[1] == 'X' && dest[2] == 'X' && dest[3] == 'X' &&
426 dest[4] == 'X' && dest[5] == 'X' && dest[6] == 'X' && dest[7] == 'X',
427 "Unexpected return data from strcpy_s: 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x\n",
428 dest[0], dest[1], dest[2], dest[3], dest[4], dest[5], dest[6], dest[7]);
429 ret = pstrcpy_s(dest, 0, NULL);
430 ok(ret == EINVAL, "Copying into a destination of size 0 returned %d, expected EINVAL\n", ret);
431 ok(dest[0] == 'X' && dest[1] == 'X' && dest[2] == 'X' && dest[3] == 'X' &&
432 dest[4] == 'X' && dest[5] == 'X' && dest[6] == 'X' && dest[7] == 'X',
433 "Unexpected return data from strcpy_s: 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x\n",
434 dest[0], dest[1], dest[2], dest[3], dest[4], dest[5], dest[6], dest[7]);
436 memset(dest, 'X', sizeof(dest));
437 ret = pstrcpy_s(dest, sizeof(dest), big);
438 ok(ret == ERANGE, "Copying a big string in a small location returned %d, expected ERANGE\n", ret);
439 ok(dest[0] == '\0'&& dest[1] == 't' && dest[2] == 'o' && dest[3] == 'o' &&
440 dest[4] == 'l' && dest[5] == 'o' && dest[6] == 'n' && dest[7] == 'g',
441 "Unexpected return data from strcpy_s: 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x\n",
442 dest[0], dest[1], dest[2], dest[3], dest[4], dest[5], dest[6], dest[7]);
444 memset(dest, 'X', sizeof(dest));
445 ret = pstrcpy_s(dest, sizeof(dest), NULL);
446 ok(ret == EINVAL, "Copying from a NULL source string returned %d, expected EINVAL\n", ret);
447 ok(dest[0] == '\0'&& dest[1] == 'X' && dest[2] == 'X' && dest[3] == 'X' &&
448 dest[4] == 'X' && dest[5] == 'X' && dest[6] == 'X' && dest[7] == 'X',
449 "Unexpected return data from strcpy_s: 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x\n",
450 dest[0], dest[1], dest[2], dest[3], dest[4], dest[5], dest[6], dest[7]);
452 ret = pstrcpy_s(NULL, sizeof(dest), small);
453 ok(ret == EINVAL, "Copying a big string a NULL dest returned %d, expected EINVAL\n", ret);
456 static void test_strcat_s(void)
458 char dest[8];
459 const char *small = "sma";
460 int ret;
462 if(!pstrcat_s)
464 skip("strcat_s not found\n");
465 return;
468 memset(dest, 'X', sizeof(dest));
469 dest[0] = '\0';
470 ret = pstrcat_s(dest, sizeof(dest), small);
471 ok(ret == 0, "strcat_s: Copying a string into a big enough destination returned %d, expected 0\n", ret);
472 ok(dest[0] == 's' && dest[1] == 'm' && dest[2] == 'a' && dest[3] == '\0'&&
473 dest[4] == 'X' && dest[5] == 'X' && dest[6] == 'X' && dest[7] == 'X',
474 "Unexpected return data from strcpy_s: 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x\n",
475 dest[0], dest[1], dest[2], dest[3], dest[4], dest[5], dest[6], dest[7]);
476 ret = pstrcat_s(dest, sizeof(dest), small);
477 ok(ret == 0, "strcat_s: Attaching a string to a big enough destination returned %d, expected 0\n", ret);
478 ok(dest[0] == 's' && dest[1] == 'm' && dest[2] == 'a' && dest[3] == 's' &&
479 dest[4] == 'm' && dest[5] == 'a' && dest[6] == '\0'&& dest[7] == 'X',
480 "Unexpected return data from strcpy_s: 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x\n",
481 dest[0], dest[1], dest[2], dest[3], dest[4], dest[5], dest[6], dest[7]);
483 ret = pstrcat_s(dest, sizeof(dest), small);
484 ok(ret == ERANGE, "strcat_s: Attaching a string to a filled up destination returned %d, expected ERANGE\n", ret);
485 ok(dest[0] == '\0'&& dest[1] == 'm' && dest[2] == 'a' && dest[3] == 's' &&
486 dest[4] == 'm' && dest[5] == 'a' && dest[6] == 's' && dest[7] == 'm',
487 "Unexpected return data from strcpy_s: 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x\n",
488 dest[0], dest[1], dest[2], dest[3], dest[4], dest[5], dest[6], dest[7]);
490 memset(dest, 'X', sizeof(dest));
491 dest[0] = 'a';
492 dest[1] = '\0';
494 ret = pstrcat_s(dest, 0, small);
495 ok(ret == EINVAL, "strcat_s: Source len = 0 returned %d, expected EINVAL\n", ret);
496 ok(dest[0] == 'a' && dest[1] == '\0'&& dest[2] == 'X' && dest[3] == 'X' &&
497 dest[4] == 'X' && dest[5] == 'X' && dest[6] == 'X' && dest[7] == 'X',
498 "Unexpected return data from strcpy_s: 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x\n",
499 dest[0], dest[1], dest[2], dest[3], dest[4], dest[5], dest[6], dest[7]);
501 ret = pstrcat_s(dest, 0, NULL);
502 ok(ret == EINVAL, "strcat_s: len = 0 and src = NULL returned %d, expected EINVAL\n", ret);
503 ok(dest[0] == 'a' && dest[1] == '\0'&& dest[2] == 'X' && dest[3] == 'X' &&
504 dest[4] == 'X' && dest[5] == 'X' && dest[6] == 'X' && dest[7] == 'X',
505 "Unexpected return data from strcpy_s: 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x\n",
506 dest[0], dest[1], dest[2], dest[3], dest[4], dest[5], dest[6], dest[7]);
508 ret = pstrcat_s(dest, sizeof(dest), NULL);
509 ok(ret == EINVAL, "strcat_s: Sourcing from NULL returned %d, expected EINVAL\n", ret);
510 ok(dest[0] == '\0'&& dest[1] == '\0'&& dest[2] == 'X' && dest[3] == 'X' &&
511 dest[4] == 'X' && dest[5] == 'X' && dest[6] == 'X' && dest[7] == 'X',
512 "Unexpected return data from strcpy_s: 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x\n",
513 dest[0], dest[1], dest[2], dest[3], dest[4], dest[5], dest[6], dest[7]);
515 ret = pstrcat_s(NULL, sizeof(dest), small);
516 ok(ret == EINVAL, "strcat_s: Writing to a NULL string returned %d, expected EINVAL\n", ret);
519 static void test__mbsnbcpy_s(void)
521 unsigned char dest[8];
522 const unsigned char big[] = "atoolongstringforthislittledestination";
523 const unsigned char small[] = "small";
524 int ret;
526 if(!p_mbsnbcpy_s)
528 skip("_mbsnbcpy_s not found\n");
529 return;
532 memset(dest, 'X', sizeof(dest));
533 ret = p_mbsnbcpy_s(dest, sizeof(dest), small, sizeof(small));
534 ok(ret == 0, "_mbsnbcpy_s: Copying a string into a big enough destination returned %d, expected 0\n", ret);
535 ok(dest[0] == 's' && dest[1] == 'm' && dest[2] == 'a' && dest[3] == 'l' &&
536 dest[4] == 'l' && dest[5] == '\0'&& dest[6] == 'X' && dest[7] == 'X',
537 "Unexpected return data from _mbsnbcpy_s: 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x\n",
538 dest[0], dest[1], dest[2], dest[3], dest[4], dest[5], dest[6], dest[7]);
540 /* WTF? */
541 memset(dest, 'X', sizeof(dest));
542 ret = p_mbsnbcpy_s(dest, sizeof(dest) - 2, big, sizeof(small));
543 ok(ret == ERANGE, "_mbsnbcpy_s: Copying a too long string returned %d, expected ERANGE\n", ret);
544 ok(dest[0] == '\0'&& dest[1] == 't' && dest[2] == 'o' && dest[3] == 'o' &&
545 dest[4] == 'l' && dest[5] == 'o' && dest[6] == 'X' && dest[7] == 'X',
546 "Unexpected return data from _mbsnbcpy_s: 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x\n",
547 dest[0], dest[1], dest[2], dest[3], dest[4], dest[5], dest[6], dest[7]);
549 memset(dest, 'X', sizeof(dest));
550 ret = p_mbsnbcpy_s(dest, sizeof(dest) - 2, big, 4);
551 ok(ret == 0, "_mbsnbcpy_s: Copying a too long string with a count cap returned %d, expected 0\n", ret);
552 ok(dest[0] == 'a' && dest[1] == 't' && dest[2] == 'o' && dest[3] == 'o' &&
553 dest[4] == '\0'&& dest[5] == 'X' && dest[6] == 'X' && dest[7] == 'X',
554 "Unexpected return data from _mbsnbcpy_s: 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x\n",
555 dest[0], dest[1], dest[2], dest[3], dest[4], dest[5], dest[6], dest[7]);
557 memset(dest, 'X', sizeof(dest));
558 ret = p_mbsnbcpy_s(dest, sizeof(dest) - 2, small, sizeof(small) + 10);
559 ok(ret == 0, "_mbsnbcpy_s: Copying more data than the source string len returned %d, expected 0\n", ret);
560 ok(dest[0] == 's' && dest[1] == 'm' && dest[2] == 'a' && dest[3] == 'l' &&
561 dest[4] == 'l' && dest[5] == '\0'&& dest[6] == 'X' && dest[7] == 'X',
562 "Unexpected return data from _mbsnbcpy_s: 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x\n",
563 dest[0], dest[1], dest[2], dest[3], dest[4], dest[5], dest[6], dest[7]);
566 static void test_wcscpy_s(void)
568 static const WCHAR szLongText[] = { 'T','h','i','s','A','L','o','n','g','s','t','r','i','n','g',0 };
569 static WCHAR szDest[18];
570 static WCHAR szDestShort[8];
571 int ret;
573 if(!p_wcscpy_s)
575 skip("wcscpy_s not found\n");
576 return;
579 /* Test NULL Dest */
580 ret = p_wcscpy_s(NULL, 18, szLongText);
581 ok(ret == EINVAL, "p_wcscpy_s expect EINVAL got %d\n", ret);
583 /* Test NULL Source */
584 szDest[0] = 'A';
585 ret = p_wcscpy_s(szDest, 18, NULL);
586 ok(ret == EINVAL, "expected EINVAL got %d\n", ret);
587 ok(szDest[0] == 0, "szDest[0] not 0\n");
589 /* Test invalid size */
590 szDest[0] = 'A';
591 ret = p_wcscpy_s(szDest, 0, szLongText);
592 /* Later versions changed the return value for this case to EINVAL,
593 * and don't modify the result if the dest size is 0.
595 ok(ret == ERANGE || ret == EINVAL, "expected ERANGE/EINVAL got %d\n", ret);
596 ok(szDest[0] == 0 || ret == EINVAL, "szDest[0] not 0\n");
598 /* Copy same buffer size */
599 ret = p_wcscpy_s(szDest, 18, szLongText);
600 ok(ret == 0, "expected 0 got %d\n", ret);
601 ok(lstrcmpW(szDest, szLongText) == 0, "szDest != szLongText\n");
603 /* Copy smaller buffer size */
604 szDest[0] = 'A';
605 ret = p_wcscpy_s(szDestShort, 8, szLongText);
606 ok(ret == ERANGE || ret == EINVAL, "expected ERANGE/EINVAL got %d\n", ret);
607 ok(szDestShort[0] == 0, "szDestShort[0] not 0\n");
610 START_TEST(string)
612 char mem[100];
613 static const char xilstring[]="c:/xilinx";
614 int nLen;
616 hMsvcrt = GetModuleHandleA("msvcrt.dll");
617 if (!hMsvcrt)
618 hMsvcrt = GetModuleHandleA("msvcrtd.dll");
619 ok(hMsvcrt != 0, "GetModuleHandleA failed\n");
620 SET(pmemcpy,"memcpy");
621 SET(pmemcmp,"memcmp");
622 pstrcpy_s = (void *)GetProcAddress( hMsvcrt,"strcpy_s" );
623 pstrcat_s = (void *)GetProcAddress( hMsvcrt,"strcat_s" );
624 p_mbsnbcpy_s = (void *)GetProcAddress( hMsvcrt,"_mbsnbcpy_s" );
625 p_wcscpy_s = (void *)GetProcAddress( hMsvcrt,"wcscpy_s" );
627 /* MSVCRT memcpy behaves like memmove for overlapping moves,
628 MFC42 CString::Insert seems to rely on that behaviour */
629 strcpy(mem,xilstring);
630 nLen=strlen(xilstring);
631 pmemcpy(mem+5, mem,nLen+1);
632 ok(pmemcmp(mem+5,xilstring, nLen) == 0,
633 "Got result %s\n",mem+5);
635 /* Test _swab function */
636 test_swab();
638 /* Test ismbblead*/
639 test_mbcp();
640 /* test _mbsspn */
641 test_mbsspn();
642 test_mbsspnp();
643 /* test _strdup */
644 test_strdup();
645 test_strcpy_s();
646 test_strcat_s();
647 test__mbsnbcpy_s();
649 test_wcscpy_s();