urlmon: Don't create stgmed_obj for binding to object.
[wine] / dlls / msvcrt / tests / string.c
1 /*
2  * Unit test suite for string functions.
3  *
4  * Copyright 2004 Uwe Bonnes
5  *
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.
10  *
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.
15  *
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
19  */
20
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>
30
31 static char *buf_to_string(const unsigned char *bin, int len, int nr)
32 {
33     static char buf[2][1024];
34     char *w = buf[nr];
35     int i;
36
37     for (i = 0; i < len; i++)
38     {
39         sprintf(w, "%02x ", (unsigned char)bin[i]);
40         w += strlen(w);
41     }
42     return buf[nr];
43 }
44
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)); }
47
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
54 #define SETNOFAIL(x,y) x = (void*)GetProcAddress(hMsvcrt,y)
55 #define SET(x,y) SETNOFAIL(x,y); ok(x != NULL, "Export '%s' not found\n", y)
56
57 HMODULE hMsvcrt;
58
59 static void test_swab( void ) {
60     char original[]  = "BADCFEHGJILKNMPORQTSVUXWZY@#";
61     char expected1[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ@#";
62     char expected2[] = "ABCDEFGHIJKLMNOPQRSTUVWX$";
63     char expected3[] = "$";
64     
65     char from[30];
66     char to[30];
67     
68     int testsize;
69     
70     /* Test 1 - normal even case */                               
71     memset(to,'$', sizeof(to));
72     memset(from,'@', sizeof(from));
73     testsize = 26;
74     memcpy(from, original, testsize);
75     _swab( from, to, testsize );
76     ok(memcmp(to,expected1,testsize) == 0, "Testing even size %d returned '%*.*s'\n", testsize, testsize, testsize, to);
77
78     /* Test 2 - uneven case  */                               
79     memset(to,'$', sizeof(to));
80     memset(from,'@', sizeof(from));
81     testsize = 25;
82     memcpy(from, original, testsize);
83     _swab( from, to, testsize );
84     ok(memcmp(to,expected2,testsize) == 0, "Testing odd size %d returned '%*.*s'\n", testsize, testsize, testsize, to);
85
86     /* Test 3 - from = to */                               
87     memset(to,'$', sizeof(to));
88     memset(from,'@', sizeof(from));
89     testsize = 26;
90     memcpy(to, original, testsize);
91     _swab( to, to, testsize );
92     ok(memcmp(to,expected1,testsize) == 0, "Testing overlapped size %d returned '%*.*s'\n", testsize, testsize, testsize, to);
93
94     /* Test 4 - 1 bytes */                               
95     memset(to,'$', sizeof(to));
96     memset(from,'@', sizeof(from));
97     testsize = 1;
98     memcpy(from, original, testsize);
99     _swab( from, to, testsize );
100     ok(memcmp(to,expected3,testsize) == 0, "Testing small size %d returned '%*.*s'\n", testsize, testsize, testsize, to);
101 }
102
103 #if 0      /* use this to generate more tests */
104
105 static void test_codepage(int cp)
106 {
107     int i;
108     int prev;
109     int count = 1;
110
111     ok(_setmbcp(cp) == 0, "Couldn't set mbcp\n");
112
113     prev = _mbctype[0];
114     printf("static int result_cp_%d_mbctype[] = { ", cp);
115     for (i = 1; i < 257; i++)
116     {
117         if (_mbctype[i] != prev)
118         {
119             printf("0x%x,%d, ", prev, count);
120             prev = _mbctype[i];
121             count = 1;
122         }
123         else
124             count++;
125     }
126     printf("0x%x,%d };\n", prev, count);
127 }
128
129 #define test_codepage_todo(cp, todo) test_codepage(cp)
130
131 #else
132
133 /* RLE-encoded mbctype tables for given codepages */
134 static int result_cp_932_mbctype[] = { 0x0,65, 0x8,1, 0x18,26, 0x8,6, 0x28,26, 0x8,4,
135   0x0,1, 0x8,1, 0xc,31, 0x8,1, 0xa,5, 0x9,58, 0xc,29, 0,3 };
136 static int result_cp_936_mbctype[] = { 0x0,65, 0x8,1, 0x18,26, 0x8,6, 0x28,26, 0x8,6,
137   0xc,126, 0,1 };
138 static int result_cp_949_mbctype[] = { 0x0,66, 0x18,26, 0x8,6, 0x28,26, 0x8,6, 0xc,126,
139   0,1 };
140 static int result_cp_950_mbctype[] = { 0x0,65, 0x8,1, 0x18,26, 0x8,6, 0x28,26, 0x8,4,
141   0x0,2, 0x4,32, 0xc,94, 0,1 };
142
143 static int todo_none[] = { -2 };
144 static int todo_cp_932[] = { 254, -2 };
145
146 void test_cp_table(int cp, int *result, int *todo)
147 {
148     int i;
149     int count = 0;
150     int curr = 0;
151     _setmbcp(cp);
152     for (i = 0; i < 256; i++)
153     {
154         if (count == 0)
155         {
156             curr = result[0];
157             count = result[1];
158             result += 2;
159         }
160         if (i == *todo + 1)
161         {
162             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);
163             todo++;
164         }
165         else
166             ok(_mbctype[i] == curr, "CP%d: Mismatch in ctype for character %d - %d instead of %d\n", cp, i-1, _mbctype[i], curr);
167         count--;
168     }
169 }
170
171 #define test_codepage(num) test_cp_table(num, result_cp_##num##_mbctype, todo_none);
172 #define test_codepage_todo(num, todo) test_cp_table(num, result_cp_##num##_mbctype, todo);
173
174 #endif
175
176 static void test_mbcp(void)
177 {
178     int mb_orig_max = __mb_cur_max;
179     int curr_mbcp = _getmbcp();
180     unsigned char *mbstring = (unsigned char *)"\xb0\xb1\xb2 \xb3\xb4 \xb5"; /* incorrect string */
181     unsigned char *mbstring2 = (unsigned char *)"\xb0\xb1\xb2\xb3Q\xb4\xb5"; /* correct string */
182     unsigned char *mbsonlylead = (unsigned char *)"\xb0\0\xb1\xb2 \xb3";
183     unsigned char buf[16];
184     int step;
185
186     /* _mbtype tests */
187
188     /* An SBCS codepage test. The ctype of characters on e.g. CP1252 or CP1250 differs slightly
189      * between versions of Windows. Also Windows 9x seems to ignore the codepage and always uses
190      * CP1252 (or the ACP?) so we test only a few ASCII characters */
191     _setmbcp(1252);
192     expect_eq(_mbctype[10], 0, char, "%x");
193     expect_eq(_mbctype[50], 0, char, "%x");
194     expect_eq(_mbctype[66], _SBUP, char, "%x");
195     expect_eq(_mbctype[100], _SBLOW, char, "%x");
196     expect_eq(_mbctype[128], 0, char, "%x");
197     _setmbcp(1250);
198     expect_eq(_mbctype[10], 0, char, "%x");
199     expect_eq(_mbctype[50], 0, char, "%x");
200     expect_eq(_mbctype[66], _SBUP, char, "%x");
201     expect_eq(_mbctype[100], _SBLOW, char, "%x");
202     expect_eq(_mbctype[128], 0, char, "%x");
203
204     /* double byte code pages */
205     test_codepage_todo(932, todo_cp_932);
206     test_codepage(936);
207     test_codepage(949);
208     test_codepage(950);
209
210     _setmbcp(936);
211     ok(__mb_cur_max == mb_orig_max, "__mb_cur_max shouldn't be updated (is %d != %d)\n", __mb_cur_max, mb_orig_max);
212     ok(_ismbblead('\354'), "\354 should be a lead byte\n");
213     ok(_ismbblead(' ') == FALSE, "' ' should not be a lead byte\n");
214     ok(_ismbblead(0x1234b0), "0x1234b0 should not be a lead byte\n");
215     ok(_ismbblead(0x123420) == FALSE, "0x123420 should not be a lead byte\n");
216     ok(_ismbbtrail('\xb0'), "\xa0 should be a trail byte\n");
217     ok(_ismbbtrail(' ') == FALSE, "' ' should not be a trail byte\n");
218
219     /* _ismbslead */
220     expect_eq(_ismbslead(mbstring, &mbstring[0]), -1, int, "%d");
221     expect_eq(_ismbslead(mbstring, &mbstring[1]), FALSE, int, "%d");
222     expect_eq(_ismbslead(mbstring, &mbstring[2]), -1, int, "%d");
223     expect_eq(_ismbslead(mbstring, &mbstring[3]), FALSE, int, "%d");
224     expect_eq(_ismbslead(mbstring, &mbstring[4]), -1, int, "%d");
225     expect_eq(_ismbslead(mbstring, &mbstring[5]), FALSE, int, "%d");
226     expect_eq(_ismbslead(mbstring, &mbstring[6]), FALSE, int, "%d");
227     expect_eq(_ismbslead(mbstring, &mbstring[7]), -1, int, "%d");
228     expect_eq(_ismbslead(mbstring, &mbstring[8]), FALSE, int, "%d");
229
230     expect_eq(_ismbslead(mbsonlylead, &mbsonlylead[0]), -1, int, "%d");
231     expect_eq(_ismbslead(mbsonlylead, &mbsonlylead[1]), FALSE, int, "%d");
232     expect_eq(_ismbslead(mbsonlylead, &mbsonlylead[2]), FALSE, int, "%d");
233     expect_eq(_ismbslead(mbsonlylead, &mbsonlylead[5]), FALSE, int, "%d");
234
235     /* _ismbstrail */
236     expect_eq(_ismbstrail(mbstring, &mbstring[0]), FALSE, int, "%d");
237     expect_eq(_ismbstrail(mbstring, &mbstring[1]), -1, int, "%d");
238     expect_eq(_ismbstrail(mbstring, &mbstring[2]), FALSE, int, "%d");
239     expect_eq(_ismbstrail(mbstring, &mbstring[3]), -1, int, "%d");
240     expect_eq(_ismbstrail(mbstring, &mbstring[4]), FALSE, int, "%d");
241     expect_eq(_ismbstrail(mbstring, &mbstring[5]), -1, int, "%d");
242     expect_eq(_ismbstrail(mbstring, &mbstring[6]), FALSE, int, "%d");
243     expect_eq(_ismbstrail(mbstring, &mbstring[7]), FALSE, int, "%d");
244     expect_eq(_ismbstrail(mbstring, &mbstring[8]), -1, int, "%d");
245
246     expect_eq(_ismbstrail(mbsonlylead, &mbsonlylead[0]), FALSE, int, "%d");
247     expect_eq(_ismbstrail(mbsonlylead, &mbsonlylead[1]), -1, int, "%d");
248     expect_eq(_ismbstrail(mbsonlylead, &mbsonlylead[2]), FALSE, int, "%d");
249     expect_eq(_ismbstrail(mbsonlylead, &mbsonlylead[3]), FALSE, int, "%d");
250     expect_eq(_ismbstrail(mbsonlylead, &mbsonlylead[4]), FALSE, int, "%d");
251     expect_eq(_ismbstrail(mbsonlylead, &mbsonlylead[5]), FALSE, int, "%d");
252
253     /* _mbsbtype */
254     expect_eq(_mbsbtype(mbstring, 0), _MBC_LEAD, int, "%d");
255     expect_eq(_mbsbtype(mbstring, 1), _MBC_TRAIL, int, "%d");
256     expect_eq(_mbsbtype(mbstring, 2), _MBC_LEAD, int, "%d");
257     expect_eq(_mbsbtype(mbstring, 3), _MBC_ILLEGAL, int, "%d");
258     expect_eq(_mbsbtype(mbstring, 4), _MBC_LEAD, int, "%d");
259     expect_eq(_mbsbtype(mbstring, 5), _MBC_TRAIL, int, "%d");
260     expect_eq(_mbsbtype(mbstring, 6), _MBC_SINGLE, int, "%d");
261     expect_eq(_mbsbtype(mbstring, 7), _MBC_LEAD, int, "%d");
262     expect_eq(_mbsbtype(mbstring, 8), _MBC_ILLEGAL, int, "%d");
263
264     expect_eq(_mbsbtype(mbsonlylead, 0), _MBC_LEAD, int, "%d");
265     expect_eq(_mbsbtype(mbsonlylead, 1), _MBC_ILLEGAL, int, "%d");
266     expect_eq(_mbsbtype(mbsonlylead, 2), _MBC_ILLEGAL, int, "%d");
267     expect_eq(_mbsbtype(mbsonlylead, 3), _MBC_ILLEGAL, int, "%d");
268     expect_eq(_mbsbtype(mbsonlylead, 4), _MBC_ILLEGAL, int, "%d");
269     expect_eq(_mbsbtype(mbsonlylead, 5), _MBC_ILLEGAL, int, "%d");
270
271     /* _mbsnextc */
272     expect_eq(_mbsnextc(mbstring), 0xb0b1, int, "%x");
273     expect_eq(_mbsnextc(&mbstring[2]), 0xb220, int, "%x");  /* lead + invalid tail */
274     expect_eq(_mbsnextc(&mbstring[3]), 0x20, int, "%x");    /* single char */
275
276     /* _mbclen/_mbslen */
277     expect_eq(_mbclen(mbstring), 2, int, "%d");
278     expect_eq(_mbclen(&mbstring[2]), 2, int, "%d");
279     expect_eq(_mbclen(&mbstring[3]), 1, int, "%d");
280     expect_eq(_mbslen(mbstring2), 4, int, "%d");
281     expect_eq(_mbslen(mbsonlylead), 0, int, "%d");          /* lead + NUL not counted as character */
282     expect_eq(_mbslen(mbstring), 4, int, "%d");             /* lead + invalid trail counted */
283
284     /* _mbccpy/_mbsncpy */
285     memset(buf, 0xff, sizeof(buf));
286     _mbccpy(buf, mbstring);
287     expect_bin(buf, "\xb0\xb1\xff", 3);
288
289     memset(buf, 0xff, sizeof(buf));
290     _mbsncpy(buf, mbstring, 1);
291     expect_bin(buf, "\xb0\xb1\xff", 3);
292     memset(buf, 0xff, sizeof(buf));
293     _mbsncpy(buf, mbstring, 2);
294     expect_bin(buf, "\xb0\xb1\xb2 \xff", 5);
295     memset(buf, 0xff, sizeof(buf));
296     _mbsncpy(buf, mbstring, 3);
297     expect_bin(buf, "\xb0\xb1\xb2 \xb3\xb4\xff", 7);
298     memset(buf, 0xff, sizeof(buf));
299     _mbsncpy(buf, mbstring, 4);
300     expect_bin(buf, "\xb0\xb1\xb2 \xb3\xb4 \xff", 8);
301     memset(buf, 0xff, sizeof(buf));
302     _mbsncpy(buf, mbstring, 5);
303     expect_bin(buf, "\xb0\xb1\xb2 \xb3\xb4 \0\0\xff", 10);
304     memset(buf, 0xff, sizeof(buf));
305     _mbsncpy(buf, mbsonlylead, 6);
306     expect_bin(buf, "\0\0\0\0\0\0\0\xff", 8);
307
308     memset(buf, 0xff, sizeof(buf));
309     _mbsnbcpy(buf, mbstring2, 2);
310     expect_bin(buf, "\xb0\xb1\xff", 3);
311     _mbsnbcpy(buf, mbstring2, 3);
312     expect_bin(buf, "\xb0\xb1\0\xff", 4);
313     _mbsnbcpy(buf, mbstring2, 4);
314     expect_bin(buf, "\xb0\xb1\xb2\xb3\xff", 5);
315     memset(buf, 0xff, sizeof(buf));
316     _mbsnbcpy(buf, mbsonlylead, 5);
317     expect_bin(buf, "\0\0\0\0\0\xff", 6);
318
319     /* _mbsinc/mbsdec */
320     step = _mbsinc(mbstring) - mbstring;
321     ok(step == 2, "_mbsinc adds %d (exp. 2)\n", step);
322     step = _mbsinc(&mbstring[2]) - &mbstring[2];  /* lead + invalid tail */
323     ok(step == 2, "_mbsinc adds %d (exp. 2)\n", step);
324
325     step = _mbsninc(mbsonlylead, 1) - mbsonlylead;
326     ok(step == 0, "_mbsninc adds %d (exp. 0)\n", step);
327     step = _mbsninc(mbsonlylead, 2) - mbsonlylead;  /* lead + NUL byte + lead + char */
328     ok(step == 0, "_mbsninc adds %d (exp. 0)\n", step);
329     step = _mbsninc(mbstring2, 0) - mbstring2;
330     ok(step == 0, "_mbsninc adds %d (exp. 2)\n", step);
331     step = _mbsninc(mbstring2, 1) - mbstring2;
332     ok(step == 2, "_mbsninc adds %d (exp. 2)\n", step);
333     step = _mbsninc(mbstring2, 2) - mbstring2;
334     ok(step == 4, "_mbsninc adds %d (exp. 4)\n", step);
335     step = _mbsninc(mbstring2, 3) - mbstring2;
336     ok(step == 5, "_mbsninc adds %d (exp. 5)\n", step);
337     step = _mbsninc(mbstring2, 4) - mbstring2;
338     ok(step == 7, "_mbsninc adds %d (exp. 7)\n", step);
339     step = _mbsninc(mbstring2, 5) - mbstring2;
340     ok(step == 7, "_mbsninc adds %d (exp. 7)\n", step);
341     step = _mbsninc(mbstring2, 17) - mbstring2;
342     ok(step == 7, "_mbsninc adds %d (exp. 7)\n", step);
343
344     /* functions that depend on locale codepage, not mbcp.
345      * we hope the current locale to be SBCS because setlocale(LC_ALL, ".1252") seems not to work yet
346      * (as of Wine 0.9.43)
347      */
348     if (__mb_cur_max == 1)
349     {
350         expect_eq(mblen((char *)mbstring, 3), 1, int, "%x");
351         expect_eq(_mbstrlen((char *)mbstring2), 7, int, "%d");
352     }
353     else
354         skip("Current locale has double-byte charset - could leave to false positives\n");
355
356     _setmbcp(curr_mbcp);
357 }
358
359 static void test_mbsspn( void)
360 {
361     unsigned char str1[]="cabernet";
362     unsigned char str2[]="shiraz";
363     unsigned char set[]="abc";
364     unsigned char empty[]="";
365     int ret;
366     ret=_mbsspn( str1, set);
367     ok( ret==3, "_mbsspn returns %d should be 3\n", ret);
368     ret=_mbsspn( str2, set);
369     ok( ret==0, "_mbsspn returns %d should be 0\n", ret);
370     ret=_mbsspn( str1, empty);
371     ok( ret==0, "_mbsspn returns %d should be 0\n", ret);
372 }
373
374 static void test_mbsspnp( void)
375 {
376     unsigned char str1[]="cabernet";
377     unsigned char str2[]="shiraz";
378     unsigned char set[]="abc";
379     unsigned char empty[]="";
380     unsigned char full[]="abcenrt";
381     unsigned char* ret;
382     ret=_mbsspnp( str1, set);
383     ok( ret[0]=='e', "_mbsspnp returns %c should be e\n", ret[0]);
384     ret=_mbsspnp( str2, set);
385     ok( ret[0]=='s', "_mbsspnp returns %c should be s\n", ret[0]);
386     ret=_mbsspnp( str1, empty);
387     ok( ret[0]=='c', "_mbsspnp returns %c should be c\n", ret[0]);
388     ret=_mbsspnp( str1, full);
389     ok( ret==NULL, "_mbsspnp returns %p should be NULL\n", ret);
390 }
391
392 static void test_strdup(void)
393 {
394    char *str;
395    str = _strdup( 0 );
396    ok( str == 0, "strdup returns %s should be 0\n", str);
397    free( str );
398 }
399
400 static void test_strcpy_s(void)
401 {
402     char dest[8];
403     const char *small = "small";
404     const char *big = "atoolongstringforthislittledestination";
405     int ret;
406
407     if(!pstrcpy_s)
408     {
409         skip("strcpy_s not found\n");
410         return;
411     }
412
413     memset(dest, 'X', sizeof(dest));
414     ret = pstrcpy_s(dest, sizeof(dest), small);
415     ok(ret == 0, "Copying a string into a big enough destination returned %d, expected 0\n", ret);
416     ok(dest[0] == 's' && dest[1] == 'm' && dest[2] == 'a' && dest[3] == 'l' &&
417        dest[4] == 'l' && dest[5] == '\0'&& dest[6] == 'X' && dest[7] == 'X',
418        "Unexpected return data from strcpy_s: 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x\n",
419        dest[0], dest[1], dest[2], dest[3], dest[4], dest[5], dest[6], dest[7]);
420
421     memset(dest, 'X', sizeof(dest));
422     ret = pstrcpy_s(dest, 0, big);
423     ok(ret == EINVAL, "Copying into a destination of size 0 returned %d, expected EINVAL\n", ret);
424     ok(dest[0] == 'X' && dest[1] == 'X' && dest[2] == 'X' && dest[3] == 'X' &&
425        dest[4] == 'X' && dest[5] == 'X' && dest[6] == 'X' && dest[7] == 'X',
426        "Unexpected return data from strcpy_s: 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x\n",
427        dest[0], dest[1], dest[2], dest[3], dest[4], dest[5], dest[6], dest[7]);
428     ret = pstrcpy_s(dest, 0, NULL);
429     ok(ret == EINVAL, "Copying into a destination of size 0 returned %d, expected EINVAL\n", ret);
430     ok(dest[0] == 'X' && dest[1] == 'X' && dest[2] == 'X' && dest[3] == 'X' &&
431        dest[4] == 'X' && dest[5] == 'X' && dest[6] == 'X' && dest[7] == 'X',
432        "Unexpected return data from strcpy_s: 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x\n",
433        dest[0], dest[1], dest[2], dest[3], dest[4], dest[5], dest[6], dest[7]);
434
435     memset(dest, 'X', sizeof(dest));
436     ret = pstrcpy_s(dest, sizeof(dest), big);
437     ok(ret == ERANGE, "Copying a big string in a small location returned %d, expected ERANGE\n", ret);
438     ok(dest[0] == '\0'&& dest[1] == 't' && dest[2] == 'o' && dest[3] == 'o' &&
439        dest[4] == 'l' && dest[5] == 'o' && dest[6] == 'n' && dest[7] == 'g',
440        "Unexpected return data from strcpy_s: 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x\n",
441        dest[0], dest[1], dest[2], dest[3], dest[4], dest[5], dest[6], dest[7]);
442
443     memset(dest, 'X', sizeof(dest));
444     ret = pstrcpy_s(dest, sizeof(dest), NULL);
445     ok(ret == EINVAL, "Copying from a NULL source string returned %d, expected EINVAL\n", ret);
446     ok(dest[0] == '\0'&& dest[1] == 'X' && dest[2] == 'X' && dest[3] == 'X' &&
447        dest[4] == 'X' && dest[5] == 'X' && dest[6] == 'X' && dest[7] == 'X',
448        "Unexpected return data from strcpy_s: 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x\n",
449        dest[0], dest[1], dest[2], dest[3], dest[4], dest[5], dest[6], dest[7]);
450
451     ret = pstrcpy_s(NULL, sizeof(dest), small);
452     ok(ret == EINVAL, "Copying a big string a NULL dest returned %d, expected EINVAL\n", ret);
453 }
454
455 static void test_strcat_s(void)
456 {
457     char dest[8];
458     const char *small = "sma";
459     int ret;
460
461     if(!pstrcat_s)
462     {
463         skip("strcat_s not found\n");
464         return;
465     }
466
467     memset(dest, 'X', sizeof(dest));
468     dest[0] = '\0';
469     ret = pstrcat_s(dest, sizeof(dest), small);
470     ok(ret == 0, "strcat_s: Copying a string into a big enough destination returned %d, expected 0\n", ret);
471     ok(dest[0] == 's' && dest[1] == 'm' && dest[2] == 'a' && dest[3] == '\0'&&
472        dest[4] == 'X' && dest[5] == 'X' && dest[6] == 'X' && dest[7] == 'X',
473        "Unexpected return data from strcpy_s: 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x\n",
474        dest[0], dest[1], dest[2], dest[3], dest[4], dest[5], dest[6], dest[7]);
475     ret = pstrcat_s(dest, sizeof(dest), small);
476     ok(ret == 0, "strcat_s: Attaching a string to a big enough destination returned %d, expected 0\n", ret);
477     ok(dest[0] == 's' && dest[1] == 'm' && dest[2] == 'a' && dest[3] == 's' &&
478        dest[4] == 'm' && dest[5] == 'a' && dest[6] == '\0'&& dest[7] == 'X',
479        "Unexpected return data from strcpy_s: 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x\n",
480        dest[0], dest[1], dest[2], dest[3], dest[4], dest[5], dest[6], dest[7]);
481
482     ret = pstrcat_s(dest, sizeof(dest), small);
483     ok(ret == ERANGE, "strcat_s: Attaching a string to a filled up destination returned %d, expected ERANGE\n", ret);
484     ok(dest[0] == '\0'&& dest[1] == 'm' && dest[2] == 'a' && dest[3] == 's' &&
485        dest[4] == 'm' && dest[5] == 'a' && dest[6] == 's' && dest[7] == 'm',
486        "Unexpected return data from strcpy_s: 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x\n",
487        dest[0], dest[1], dest[2], dest[3], dest[4], dest[5], dest[6], dest[7]);
488
489     memset(dest, 'X', sizeof(dest));
490     dest[0] = 'a';
491     dest[1] = '\0';
492
493     ret = pstrcat_s(dest, 0, small);
494     ok(ret == EINVAL, "strcat_s: Source len = 0 returned %d, expected EINVAL\n", ret);
495     ok(dest[0] == 'a' && dest[1] == '\0'&& dest[2] == 'X' && dest[3] == 'X' &&
496        dest[4] == 'X' && dest[5] == 'X' && dest[6] == 'X' && dest[7] == 'X',
497        "Unexpected return data from strcpy_s: 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x\n",
498        dest[0], dest[1], dest[2], dest[3], dest[4], dest[5], dest[6], dest[7]);
499
500     ret = pstrcat_s(dest, 0, NULL);
501     ok(ret == EINVAL, "strcat_s: len = 0 and src = NULL returned %d, expected EINVAL\n", ret);
502     ok(dest[0] == 'a' && dest[1] == '\0'&& dest[2] == 'X' && dest[3] == 'X' &&
503        dest[4] == 'X' && dest[5] == 'X' && dest[6] == 'X' && dest[7] == 'X',
504        "Unexpected return data from strcpy_s: 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x\n",
505        dest[0], dest[1], dest[2], dest[3], dest[4], dest[5], dest[6], dest[7]);
506
507     ret = pstrcat_s(dest, sizeof(dest), NULL);
508     ok(ret == EINVAL, "strcat_s:  Sourcing from NULL returned %d, expected EINVAL\n", ret);
509     ok(dest[0] == '\0'&& dest[1] == '\0'&& dest[2] == 'X' && dest[3] == 'X' &&
510        dest[4] == 'X' && dest[5] == 'X' && dest[6] == 'X' && dest[7] == 'X',
511        "Unexpected return data from strcpy_s: 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x\n",
512        dest[0], dest[1], dest[2], dest[3], dest[4], dest[5], dest[6], dest[7]);
513
514     ret = pstrcat_s(NULL, sizeof(dest), small);
515     ok(ret == EINVAL, "strcat_s: Writing to a NULL string returned %d, expected EINVAL\n", ret);
516 }
517
518 static void test__mbsnbcpy_s(void)
519 {
520     unsigned char dest[8];
521     const unsigned char big[] = "atoolongstringforthislittledestination";
522     const unsigned char small[] = "small";
523     int ret;
524
525     if(!p_mbsnbcpy_s)
526     {
527         skip("_mbsnbcpy_s not found\n");
528         return;
529     }
530
531     memset(dest, 'X', sizeof(dest));
532     ret = p_mbsnbcpy_s(dest, sizeof(dest), small, sizeof(small));
533     ok(ret == 0, "_mbsnbcpy_s: Copying a string into a big enough destination returned %d, expected 0\n", ret);
534     ok(dest[0] == 's' && dest[1] == 'm' && dest[2] == 'a' && dest[3] == 'l' &&
535        dest[4] == 'l' && dest[5] == '\0'&& dest[6] == 'X' && dest[7] == 'X',
536        "Unexpected return data from _mbsnbcpy_s: 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x\n",
537        dest[0], dest[1], dest[2], dest[3], dest[4], dest[5], dest[6], dest[7]);
538
539     /* WTF? */
540     memset(dest, 'X', sizeof(dest));
541     ret = p_mbsnbcpy_s(dest, sizeof(dest) - 2, big, sizeof(small));
542     ok(ret == ERANGE, "_mbsnbcpy_s: Copying a too long string returned %d, expected ERANGE\n", ret);
543     ok(dest[0] == '\0'&& dest[1] == 't' && dest[2] == 'o' && dest[3] == 'o' &&
544        dest[4] == 'l' && dest[5] == 'o' && dest[6] == 'X' && dest[7] == 'X',
545        "Unexpected return data from _mbsnbcpy_s: 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x\n",
546        dest[0], dest[1], dest[2], dest[3], dest[4], dest[5], dest[6], dest[7]);
547
548     memset(dest, 'X', sizeof(dest));
549     ret = p_mbsnbcpy_s(dest, sizeof(dest) - 2, big, 4);
550     ok(ret == 0, "_mbsnbcpy_s: Copying a too long string with a count cap returned %d, expected 0\n", ret);
551     ok(dest[0] == 'a' && dest[1] == 't' && dest[2] == 'o' && dest[3] == 'o' &&
552        dest[4] == '\0'&& dest[5] == 'X' && dest[6] == 'X' && dest[7] == 'X',
553        "Unexpected return data from _mbsnbcpy_s: 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x\n",
554        dest[0], dest[1], dest[2], dest[3], dest[4], dest[5], dest[6], dest[7]);
555
556     memset(dest, 'X', sizeof(dest));
557     ret = p_mbsnbcpy_s(dest, sizeof(dest) - 2, small, sizeof(small) + 10);
558     ok(ret == 0, "_mbsnbcpy_s: Copying more data than the source string len returned %d, expected 0\n", ret);
559     ok(dest[0] == 's' && dest[1] == 'm' && dest[2] == 'a' && dest[3] == 'l' &&
560        dest[4] == 'l' && dest[5] == '\0'&& dest[6] == 'X' && dest[7] == 'X',
561        "Unexpected return data from _mbsnbcpy_s: 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x\n",
562        dest[0], dest[1], dest[2], dest[3], dest[4], dest[5], dest[6], dest[7]);
563 }
564
565 START_TEST(string)
566 {
567     char mem[100];
568     static const char xilstring[]="c:/xilinx";
569     int nLen;
570
571     hMsvcrt = GetModuleHandleA("msvcrt.dll");
572     if (!hMsvcrt)
573         hMsvcrt = GetModuleHandleA("msvcrtd.dll");
574     ok(hMsvcrt != 0, "GetModuleHandleA failed\n");
575     SET(pmemcpy,"memcpy");
576     SET(pmemcmp,"memcmp");
577     SET(pstrcpy_s,"strcpy_s");
578     SET(pstrcat_s,"strcat_s");
579     SET(p_mbsnbcpy_s,"_mbsnbcpy_s");
580
581     /* MSVCRT memcpy behaves like memmove for overlapping moves,
582        MFC42 CString::Insert seems to rely on that behaviour */
583     strcpy(mem,xilstring);
584     nLen=strlen(xilstring);
585     pmemcpy(mem+5, mem,nLen+1);
586     ok(pmemcmp(mem+5,xilstring, nLen) == 0,
587        "Got result %s\n",mem+5);
588
589     /* Test _swab function */
590     test_swab();
591
592     /* Test ismbblead*/
593     test_mbcp();
594    /* test _mbsspn */
595     test_mbsspn();
596     test_mbsspnp();
597    /* test _strdup */
598     test_strdup();
599     test_strcpy_s();
600     test_strcat_s();
601     test__mbsnbcpy_s();
602 }