secur32/tests: Add acceptable error code for Win7 ntlm test.
[wine] / dlls / shlwapi / tests / string.c
1 /* Unit test suite for SHLWAPI string functions
2  *
3  * Copyright 2003 Jon Griffiths
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
18  */
19
20 #include <stdio.h>
21
22 #include "wine/test.h"
23 #include "winbase.h"
24 #include "winerror.h"
25 #include "winnls.h"
26 #define NO_SHLWAPI_REG
27 #define NO_SHLWAPI_PATH
28 #define NO_SHLWAPI_GDI
29 #define NO_SHLWAPI_STREAM
30 #include "shlwapi.h"
31 #include "shtypes.h"
32
33 #define expect_eq(expr, val, type, fmt) do { \
34     type ret = expr; \
35     ok(ret == val, "Unexpected value of '" #expr "': " #fmt " instead of " #val "\n", ret); \
36 } while (0);
37
38 #define expect_eq2(expr, val1, val2, type, fmt) do { \
39     type ret = expr; \
40     ok(ret == val1 || ret == val2, "Unexpected value of '" #expr "': " #fmt " instead of " #val1 " or " #val2 "\n", ret); \
41 } while (0);
42
43 static BOOL    (WINAPI *pIntlStrEqWorkerA)(BOOL,LPCSTR,LPCSTR,int);
44 static BOOL    (WINAPI *pIntlStrEqWorkerW)(BOOL,LPCWSTR,LPCWSTR,int);
45 static DWORD   (WINAPI *pSHAnsiToAnsi)(LPCSTR,LPSTR,int);
46 static DWORD   (WINAPI *pSHUnicodeToUnicode)(LPCWSTR,LPWSTR,int);
47 static LPSTR   (WINAPI *pStrCatBuffA)(LPSTR,LPCSTR,INT);
48 static LPWSTR  (WINAPI *pStrCatBuffW)(LPWSTR,LPCWSTR,INT);
49 static LPSTR   (WINAPI *pStrCpyNXA)(LPSTR,LPCSTR,int);
50 static LPWSTR  (WINAPI *pStrCpyNXW)(LPWSTR,LPCWSTR,int);
51 static LPSTR   (WINAPI *pStrFormatByteSize64A)(LONGLONG,LPSTR,UINT);
52 static LPSTR   (WINAPI *pStrFormatKBSizeA)(LONGLONG,LPSTR,UINT);
53 static LPWSTR  (WINAPI *pStrFormatKBSizeW)(LONGLONG,LPWSTR,UINT);
54 static BOOL    (WINAPI *pStrIsIntlEqualA)(BOOL,LPCSTR,LPCSTR,int);
55 static BOOL    (WINAPI *pStrIsIntlEqualW)(BOOL,LPCWSTR,LPCWSTR,int);
56 static HRESULT (WINAPI *pStrRetToBSTR)(STRRET*,void*,BSTR*);
57 static HRESULT (WINAPI *pStrRetToBufA)(STRRET*,LPCITEMIDLIST,LPSTR,UINT);
58 static HRESULT (WINAPI *pStrRetToBufW)(STRRET*,LPCITEMIDLIST,LPWSTR,UINT);
59 static INT     (WINAPIV *pwnsprintfA)(LPSTR,INT,LPCSTR, ...);
60 static INT     (WINAPIV *pwnsprintfW)(LPWSTR,INT,LPCWSTR, ...);
61 static LPWSTR  (WINAPI *pStrChrNW)(LPWSTR,WCHAR,UINT);
62
63 static int strcmpW(const WCHAR *str1, const WCHAR *str2)
64 {
65     while (*str1 && (*str1 == *str2)) { str1++; str2++; }
66     return *str1 - *str2;
67 }
68
69 /* StrToInt/StrToIntEx results */
70 typedef struct tagStrToIntResult
71 {
72   const char* string;
73   int str_to_int;
74   int str_to_int_ex;
75   int str_to_int_hex;
76 } StrToIntResult;
77
78 static const StrToIntResult StrToInt_results[] = {
79      { "1099", 1099, 1099, 1099 },
80      { "+88987", 0, 88987, 88987 },
81      { "012", 12, 12, 12 },
82      { "-55", -55, -55, -55 },
83      { "-0", 0, 0, 0 },
84      { "0x44ff", 0, 0, 0x44ff },
85      { "+0x44f4", 0, 0, 0x44f4 },
86      { "-0x44fd", 0, 0, 0x44fd },
87      { "+ 88987", 0, 0, 0 },
88      { "- 55", 0, 0, 0 },
89      { "- 0", 0, 0, 0 },
90      { "+ 0x44f4", 0, 0, 0 },
91      { "--0x44fd", 0, 0, 0 },
92      { " 1999", 0, 1999, 1999 },
93      { " +88987", 0, 88987, 88987 },
94      { " 012", 0, 12, 12 },
95      { " -55", 0, -55, -55 },
96      { " 0x44ff", 0, 0, 0x44ff },
97      { " +0x44f4", 0, 0, 0x44f4 },
98      { " -0x44fd", 0, 0, 0x44fd },
99      { NULL, 0, 0, 0 }
100 };
101
102 /* pStrFormatByteSize64/StrFormatKBSize results */
103 typedef struct tagStrFormatSizeResult
104 {
105   LONGLONG value;
106   const char* byte_size_64;
107   const char* kb_size;
108 } StrFormatSizeResult;
109
110
111 static const StrFormatSizeResult StrFormatSize_results[] = {
112   { -1023, "-1023 bytes", "0 KB"},
113   { -24, "-24 bytes", "0 KB"},
114   { 309, "309 bytes", "1 KB"},
115   { 10191, "9.95 KB", "10 KB"},
116   { 100353, "98.0 KB", "99 KB"},
117   { 1022286, "998 KB", "999 KB"},
118   { 1046862, "0.99 MB", "1,023 KB"},
119   { 1048574619, "999 MB", "1,023,999 KB"},
120   { 1073741775, "0.99 GB", "1,048,576 KB"},
121   { ((LONGLONG)0x000000f9 << 32) | 0xfffff94e, "999 GB", "1,048,575,999 KB"},
122   { ((LONGLONG)0x000000ff << 32) | 0xfffffa9b, "0.99 TB", "1,073,741,823 KB"},
123   { ((LONGLONG)0x0003e7ff << 32) | 0xfffffa9b, "999 TB", "1,073,741,823,999 KB"},
124   { ((LONGLONG)0x0003ffff << 32) | 0xfffffbe8, "0.99 PB", "1,099,511,627,775 KB"},
125   { ((LONGLONG)0x0f9fffff << 32) | 0xfffffd35, "999 PB", "1,099,511,627,776,000 KB"},
126   { ((LONGLONG)0x0fffffff << 32) | 0xfffffa9b, "0.99 EB", "1,125,899,906,842,623 KB"},
127   { 0, NULL, NULL }
128 };
129
130 /* StrFormatByteSize64/StrFormatKBSize results */
131 typedef struct tagStrFromTimeIntervalResult
132 {
133   DWORD ms;
134   int   digits;
135   const char* time_interval;
136 } StrFromTimeIntervalResult;
137
138
139 static const StrFromTimeIntervalResult StrFromTimeInterval_results[] = {
140   { 1, 1, " 0 sec" },
141   { 1, 2, " 0 sec" },
142   { 1, 3, " 0 sec" },
143   { 1, 4, " 0 sec" },
144   { 1, 5, " 0 sec" },
145   { 1, 6, " 0 sec" },
146   { 1, 7, " 0 sec" },
147
148   { 1000000, 1, " 10 min" },
149   { 1000000, 2, " 16 min" },
150   { 1000000, 3, " 16 min 40 sec" },
151   { 1000000, 4, " 16 min 40 sec" },
152   { 1000000, 5, " 16 min 40 sec" },
153   { 1000000, 6, " 16 min 40 sec" },
154   { 1000000, 7, " 16 min 40 sec" },
155
156   { 1999999, 1, " 30 min" },
157   { 1999999, 2, " 33 min" },
158   { 1999999, 3, " 33 min 20 sec" },
159   { 1999999, 4, " 33 min 20 sec" },
160   { 1999999, 5, " 33 min 20 sec" },
161   { 1999999, 6, " 33 min 20 sec" },
162   { 1999999, 7, " 33 min 20 sec" },
163
164   { 3999997, 1, " 1 hr" },
165   { 3999997, 2, " 1 hr 6 min" },
166   { 3999997, 3, " 1 hr 6 min 40 sec" },
167   { 3999997, 4, " 1 hr 6 min 40 sec" },
168   { 3999997, 5, " 1 hr 6 min 40 sec" },
169   { 3999997, 6, " 1 hr 6 min 40 sec" },
170   { 3999997, 7, " 1 hr 6 min 40 sec" },
171
172   { 149999851, 7, " 41 hr 40 min 0 sec" },
173   { 150999850, 1, " 40 hr" },
174   { 150999850, 2, " 41 hr" },
175   { 150999850, 3, " 41 hr 50 min" },
176   { 150999850, 4, " 41 hr 56 min" },
177   { 150999850, 5, " 41 hr 56 min 40 sec" },
178   { 150999850, 6, " 41 hr 56 min 40 sec" },
179   { 150999850, 7, " 41 hr 56 min 40 sec" },
180
181   { 493999507, 1, " 100 hr" },
182   { 493999507, 2, " 130 hr" },
183   { 493999507, 3, " 137 hr" },
184   { 493999507, 4, " 137 hr 10 min" },
185   { 493999507, 5, " 137 hr 13 min" },
186   { 493999507, 6, " 137 hr 13 min 20 sec" },
187   { 493999507, 7, " 137 hr 13 min 20 sec" },
188
189   { 0, 0, NULL }
190 };
191
192 static void test_StrChrA(void)
193 {
194   char string[129];
195   WORD count;
196
197   /* this test crashes on win2k SP4 */
198   /*ok(!StrChrA(NULL,'\0'), "found a character in a NULL string!\n");*/
199
200   for (count = 32; count < 128; count++)
201     string[count] = (char)count;
202   string[128] = '\0';
203
204   for (count = 32; count < 128; count++)
205   {
206     LPSTR result = StrChrA(string+32, count);
207     INT pos = result - string;
208     ok(pos == count, "found char '%c' in wrong place: got %d, expected %d\n", count, pos, count);
209   }
210
211   for (count = 32; count < 128; count++)
212   {
213     LPSTR result = StrChrA(string+count+1, count);
214     ok(!result, "found char '%c' not in the string\n", count);
215   }
216 }
217
218 static void test_StrChrW(void)
219 {
220   WCHAR string[16385];
221   WORD count;
222
223   /* this test crashes on win2k SP4 */
224   /*ok(!StrChrW(NULL,'\0'), "found a character in a NULL string!\n");*/
225
226   for (count = 32; count < 16384; count++)
227     string[count] = count;
228   string[16384] = '\0';
229
230   for (count = 32; count < 16384; count++)
231   {
232     LPWSTR result = StrChrW(string+32, count);
233     ok((result - string) == count, "found char %d in wrong place\n", count);
234   }
235
236   for (count = 32; count < 16384; count++)
237   {
238     LPWSTR result = StrChrW(string+count+1, count);
239     ok(!result, "found char not in the string\n");
240   }
241 }
242
243 static void test_StrChrIA(void)
244 {
245   char string[129];
246   WORD count;
247
248   /* this test crashes on win2k SP4 */
249   /*ok(!StrChrIA(NULL,'\0'), "found a character in a NULL string!\n");*/
250
251   for (count = 32; count < 128; count++)
252     string[count] = (char)count;
253   string[128] = '\0';
254
255   for (count = 'A'; count <= 'X'; count++)
256   {
257     LPSTR result = StrChrIA(string+32, count);
258
259     ok(result - string == count, "found char '%c' in wrong place\n", count);
260     ok(StrChrIA(result, count)!=NULL, "didn't find lowercase '%c'\n", count);
261   }
262
263   for (count = 'a'; count < 'z'; count++)
264   {
265     LPSTR result = StrChrIA(string+count+1, count);
266     ok(!result, "found char not in the string\n");
267   }
268 }
269
270 static void test_StrChrIW(void)
271 {
272   WCHAR string[129];
273   WORD count;
274
275   /* this test crashes on win2k SP4 */
276   /*ok(!StrChrIA(NULL,'\0'), "found a character in a NULL string!\n");*/
277
278   for (count = 32; count < 128; count++)
279     string[count] = count;
280   string[128] = '\0';
281
282   for (count = 'A'; count <= 'X'; count++)
283   {
284     LPWSTR result = StrChrIW(string+32, count);
285
286     ok(result - string == count, "found char '%c' in wrong place\n", count);
287     ok(StrChrIW(result, count)!=NULL, "didn't find lowercase '%c'\n", count);
288   }
289
290   for (count = 'a'; count < 'z'; count++)
291   {
292     LPWSTR result = StrChrIW(string+count+1, count);
293     ok(!result, "found char not in the string\n");
294   }
295 }
296
297 static void test_StrRChrA(void)
298 {
299   char string[129];
300   WORD count;
301
302   /* this test crashes on win2k SP4 */
303   /*ok(!StrRChrA(NULL, NULL,'\0'), "found a character in a NULL string!\n");*/
304
305   for (count = 32; count < 128; count++)
306     string[count] = (char)count;
307   string[128] = '\0';
308
309   for (count = 32; count < 128; count++)
310   {
311     LPSTR result = StrRChrA(string+32, NULL, count);
312     ok(result - string == count, "found char %d in wrong place\n", count);
313   }
314
315   for (count = 32; count < 128; count++)
316   {
317     LPSTR result = StrRChrA(string+count+1, NULL, count);
318     ok(!result, "found char not in the string\n");
319   }
320
321   for (count = 32; count < 128; count++)
322   {
323     LPSTR result = StrRChrA(string+count+1, string + 127, count);
324     ok(!result, "found char not in the string\n");
325   }
326 }
327
328 static void test_StrRChrW(void)
329 {
330   WCHAR string[129];
331   WORD count;
332
333   /* this test crashes on win2k SP4 */
334   /*ok(!StrRChrW(NULL, NULL,'\0'), "found a character in a NULL string!\n");*/
335
336   for (count = 32; count < 128; count++)
337     string[count] = count;
338   string[128] = '\0';
339
340   for (count = 32; count < 128; count++)
341   {
342     LPWSTR result = StrRChrW(string+32, NULL, count);
343     INT pos = result - string;
344     ok(pos == count, "found char %d in wrong place: got %d, expected %d\n", count, pos, count);
345   }
346
347   for (count = 32; count < 128; count++)
348   {
349     LPWSTR result = StrRChrW(string+count+1, NULL, count);
350     ok(!result, "found char %d not in the string\n", count);
351   }
352
353   for (count = 32; count < 128; count++)
354   {
355     LPWSTR result = StrRChrW(string+count+1, string + 127, count);
356     ok(!result, "found char %d not in the string\n", count);
357   }
358 }
359
360 static void test_StrCpyW(void)
361 {
362   WCHAR szSrc[256];
363   WCHAR szBuff[256];
364   const StrFormatSizeResult* result = StrFormatSize_results;
365
366
367   while(result->value)
368   {
369     MultiByteToWideChar(0,0,result->byte_size_64,-1,szSrc,sizeof(szSrc)/sizeof(WCHAR));
370
371     StrCpyW(szBuff, szSrc);
372     ok(!StrCmpW(szSrc, szBuff), "Copied string %s wrong\n", result->byte_size_64);
373     result++;
374   }
375 }
376
377 static void test_StrChrNW(void)
378 {
379     static WCHAR string[] = {'T','e','s','t','i','n','g',' ','S','t','r','i','n','g',0};
380     LPWSTR p;
381
382     if (!pStrChrNW)
383     {
384         win_skip("StrChrNW not available\n");
385         return;
386     }
387
388     p = pStrChrNW(string,'t',10);
389     ok(*p=='t',"Found wrong 't'\n");
390     ok(*(p+1)=='i',"next should be 'i'\n");
391
392     p = pStrChrNW(string,'S',10);
393     ok(*p=='S',"Found wrong 'S'\n");
394
395     p = pStrChrNW(string,'r',10);
396     ok(p==NULL,"Should not have found 'r'\n");
397 }
398
399 static void test_StrToIntA(void)
400 {
401   const StrToIntResult *result = StrToInt_results;
402   int return_val;
403
404   while (result->string)
405   {
406     return_val = StrToIntA(result->string);
407     ok(return_val == result->str_to_int, "converted '%s' wrong (%d)\n",
408        result->string, return_val);
409     result++;
410   }
411 }
412
413 static void test_StrToIntW(void)
414 {
415   WCHAR szBuff[256];
416   const StrToIntResult *result = StrToInt_results;
417   int return_val;
418
419   while (result->string)
420   {
421     MultiByteToWideChar(0,0,result->string,-1,szBuff,sizeof(szBuff)/sizeof(WCHAR));
422     return_val = StrToIntW(szBuff);
423     ok(return_val == result->str_to_int, "converted '%s' wrong (%d)\n",
424        result->string, return_val);
425     result++;
426   }
427 }
428
429 static void test_StrToIntExA(void)
430 {
431   const StrToIntResult *result = StrToInt_results;
432   int return_val;
433   BOOL bRet;
434
435   while (result->string)
436   {
437     return_val = -1;
438     bRet = StrToIntExA(result->string,0,&return_val);
439     ok(!bRet || return_val != -1, "No result returned from '%s'\n",
440        result->string);
441     if (bRet)
442       ok(return_val == result->str_to_int_ex, "converted '%s' wrong (%d)\n",
443          result->string, return_val);
444     result++;
445   }
446
447   result = StrToInt_results;
448   while (result->string)
449   {
450     return_val = -1;
451     bRet = StrToIntExA(result->string,STIF_SUPPORT_HEX,&return_val);
452     ok(!bRet || return_val != -1, "No result returned from '%s'\n",
453        result->string);
454     if (bRet)
455       ok(return_val == result->str_to_int_hex, "converted '%s' wrong (%d)\n",
456          result->string, return_val);
457     result++;
458   }
459 }
460
461 static void test_StrToIntExW(void)
462 {
463   WCHAR szBuff[256];
464   const StrToIntResult *result = StrToInt_results;
465   int return_val;
466   BOOL bRet;
467
468   while (result->string)
469   {
470     return_val = -1;
471     MultiByteToWideChar(0,0,result->string,-1,szBuff,sizeof(szBuff)/sizeof(WCHAR));
472     bRet = StrToIntExW(szBuff, 0, &return_val);
473     ok(!bRet || return_val != -1, "No result returned from '%s'\n",
474        result->string);
475     if (bRet)
476       ok(return_val == result->str_to_int_ex, "converted '%s' wrong (%d)\n",
477          result->string, return_val);
478     result++;
479   }
480
481   result = StrToInt_results;
482   while (result->string)
483   {
484     return_val = -1;
485     MultiByteToWideChar(0,0,result->string,-1,szBuff,sizeof(szBuff)/sizeof(WCHAR));
486     bRet = StrToIntExW(szBuff, STIF_SUPPORT_HEX, &return_val);
487     ok(!bRet || return_val != -1, "No result returned from '%s'\n",
488        result->string);
489     if (bRet)
490       ok(return_val == result->str_to_int_hex, "converted '%s' wrong (%d)\n",
491          result->string, return_val);
492     result++;
493   }
494 }
495
496 static void test_StrDupA(void)
497 {
498   LPSTR lpszStr;
499   const StrFormatSizeResult* result = StrFormatSize_results;
500
501   while(result->value)
502   {
503     lpszStr = StrDupA(result->byte_size_64);
504
505     ok(lpszStr != NULL, "Dup failed\n");
506     if (lpszStr)
507     {
508       ok(!strcmp(result->byte_size_64, lpszStr), "Copied string wrong\n");
509       LocalFree(lpszStr);
510     }
511     result++;
512   }
513
514   /* Later versions of shlwapi return NULL for this, but earlier versions
515    * returned an empty string (as Wine does).
516    */
517   lpszStr = StrDupA(NULL);
518   ok(lpszStr == NULL || *lpszStr == '\0', "NULL string returned %p\n", lpszStr);
519   LocalFree(lpszStr);
520 }
521
522 static void test_StrFormatByteSize64A(void)
523 {
524   char szBuff[256];
525   const StrFormatSizeResult* result = StrFormatSize_results;
526
527   if (!pStrFormatByteSize64A)
528   {
529     win_skip("StrFormatByteSize64A() is not available\n");
530     return;
531   }
532
533   while(result->value)
534   {
535     pStrFormatByteSize64A(result->value, szBuff, 256);
536
537     ok(!strcmp(result->byte_size_64, szBuff),
538         "Formatted %x%08x wrong: got %s, expected %s\n",
539        (LONG)(result->value >> 32), (LONG)result->value, szBuff, result->byte_size_64);
540
541     result++;
542   }
543 }
544
545 static void test_StrFormatKBSizeW(void)
546 {
547   WCHAR szBuffW[256];
548   char szBuff[256];
549   const StrFormatSizeResult* result = StrFormatSize_results;
550
551   if (!pStrFormatKBSizeW)
552   {
553     win_skip("StrFormatKBSizeW() is not available\n");
554     return;
555   }
556
557   while(result->value)
558   {
559     pStrFormatKBSizeW(result->value, szBuffW, 256);
560     WideCharToMultiByte(0,0,szBuffW,-1,szBuff,sizeof(szBuff)/sizeof(WCHAR),0,0);
561     ok(!strcmp(result->kb_size, szBuff),
562         "Formatted %x%08x wrong: got %s, expected %s\n",
563        (LONG)(result->value >> 32), (LONG)result->value, szBuff, result->kb_size);
564     result++;
565   }
566 }
567
568 static void test_StrFormatKBSizeA(void)
569 {
570   char szBuff[256];
571   const StrFormatSizeResult* result = StrFormatSize_results;
572
573   if (!pStrFormatKBSizeA)
574   {
575     win_skip("StrFormatKBSizeA() is not available\n");
576     return;
577   }
578
579   while(result->value)
580   {
581     pStrFormatKBSizeA(result->value, szBuff, 256);
582
583     ok(!strcmp(result->kb_size, szBuff),
584         "Formatted %x%08x wrong: got %s, expected %s\n",
585        (LONG)(result->value >> 32), (LONG)result->value, szBuff, result->kb_size);
586     result++;
587   }
588 }
589
590 static void test_StrFromTimeIntervalA(void)
591 {
592   char szBuff[256];
593   const StrFromTimeIntervalResult* result = StrFromTimeInterval_results;
594
595   while(result->ms)
596   {
597     StrFromTimeIntervalA(szBuff, 256, result->ms, result->digits);
598
599     ok(!strcmp(result->time_interval, szBuff), "Formatted %d %d wrong\n",
600        result->ms, result->digits);
601     result++;
602   }
603 }
604
605 static void test_StrCmpA(void)
606 {
607   static const char str1[] = {'a','b','c','d','e','f'};
608   static const char str2[] = {'a','B','c','d','e','f'};
609   ok(0 != StrCmpNA(str1, str2, 6), "StrCmpNA is case-insensitive\n");
610   ok(0 == StrCmpNIA(str1, str2, 6), "StrCmpNIA is case-sensitive\n");
611   ok(!ChrCmpIA('a', 'a'), "ChrCmpIA doesn't work at all!\n");
612   ok(!ChrCmpIA('b', 'B'), "ChrCmpIA is not case-insensitive\n");
613   ok(ChrCmpIA('a', 'z'), "ChrCmpIA believes that a == z!\n");
614
615   if (pStrIsIntlEqualA)
616   {
617     ok(pStrIsIntlEqualA(FALSE, str1, str2, 5), "StrIsIntlEqualA(FALSE,...) isn't case-insensitive\n");
618     ok(!pStrIsIntlEqualA(TRUE, str1, str2, 5), "StrIsIntlEqualA(TRUE,...) isn't case-sensitive\n");
619   }
620   else
621     win_skip("StrIsIntlEqualA() is not available\n");
622
623   if (pIntlStrEqWorkerA)
624   {
625     ok(pIntlStrEqWorkerA(FALSE, str1, str2, 5), "IntlStrEqWorkerA(FALSE,...) isn't case-insensitive\n");
626     ok(!pIntlStrEqWorkerA(TRUE, str1, str2, 5), "pIntlStrEqWorkerA(TRUE,...) isn't case-sensitive\n");
627   }
628   else
629     win_skip("IntlStrEqWorkerA() is not available\n");
630 }
631
632 static void test_StrCmpW(void)
633 {
634   static const WCHAR str1[] = {'a','b','c','d','e','f'};
635   static const WCHAR str2[] = {'a','B','c','d','e','f'};
636   ok(0 != StrCmpNW(str1, str2, 5), "StrCmpNW is case-insensitive\n");
637   ok(0 == StrCmpNIW(str1, str2, 5), "StrCmpNIW is case-sensitive\n");
638   ok(!ChrCmpIW('a', 'a'), "ChrCmpIW doesn't work at all!\n");
639   ok(!ChrCmpIW('b', 'B'), "ChrCmpIW is not case-insensitive\n");
640   ok(ChrCmpIW('a', 'z'), "ChrCmpIW believes that a == z!\n");
641
642   if (pStrIsIntlEqualW)
643   {
644     ok(pStrIsIntlEqualW(FALSE, str1, str2, 5), "StrIsIntlEqualW(FALSE,...) isn't case-insensitive\n");
645     ok(!pStrIsIntlEqualW(TRUE, str1, str2, 5), "StrIsIntlEqualW(TRUE,...) isn't case-sensitive\n");
646   }
647   else
648     win_skip("StrIsIntlEqualW() is not available\n");
649
650   if (pIntlStrEqWorkerW)
651   {
652     ok(pIntlStrEqWorkerW(FALSE, str1, str2, 5), "IntlStrEqWorkerW(FALSE,...) isn't case-insensitive\n");
653     ok(!pIntlStrEqWorkerW(TRUE, str1, str2, 5), "IntlStrEqWorkerW(TRUE,...) isn't case-sensitive\n");
654   }
655   else
656     win_skip("IntlStrEqWorkerW() is not available\n");
657 }
658
659 static WCHAR *CoDupStrW(const char* src)
660 {
661   INT len = MultiByteToWideChar(CP_ACP, 0, src, -1, NULL, 0);
662   WCHAR* szTemp = CoTaskMemAlloc(len * sizeof(WCHAR));
663   MultiByteToWideChar(CP_ACP, 0, src, -1, szTemp, len);
664   return szTemp;
665 }
666
667 static void test_StrRetToBSTR(void)
668 {
669     static const WCHAR szTestW[] = { 'T','e','s','t','\0' };
670     ITEMIDLIST iidl[10];
671     BSTR bstr;
672     STRRET strret;
673     HRESULT ret;
674
675     if (!pStrRetToBSTR)
676     {
677         win_skip("StrRetToBSTR() is not available\n");
678         return;
679     }
680
681     strret.uType = STRRET_WSTR;
682     U(strret).pOleStr = CoDupStrW("Test");
683     bstr = 0;
684     ret = pStrRetToBSTR(&strret, NULL, &bstr);
685     ok(ret == S_OK && bstr && !strcmpW(bstr, szTestW),
686        "STRRET_WSTR: dup failed, ret=0x%08x, bstr %p\n", ret, bstr);
687     SysFreeString(bstr);
688
689     strret.uType = STRRET_CSTR;
690     lstrcpyA(U(strret).cStr, "Test");
691     ret = pStrRetToBSTR(&strret, NULL, &bstr);
692     ok(ret == S_OK && bstr && !strcmpW(bstr, szTestW),
693        "STRRET_CSTR: dup failed, ret=0x%08x, bstr %p\n", ret, bstr);
694     SysFreeString(bstr);
695
696     strret.uType = STRRET_OFFSET;
697     U(strret).uOffset = 1;
698     strcpy((char*)&iidl, " Test");
699     ret = pStrRetToBSTR(&strret, iidl, &bstr);
700     ok(ret == S_OK && bstr && !strcmpW(bstr, szTestW),
701        "STRRET_OFFSET: dup failed, ret=0x%08x, bstr %p\n", ret, bstr);
702     SysFreeString(bstr);
703
704     /* Native crashes if str is NULL */
705 }
706
707 static void test_StrCpyNXA(void)
708 {
709   LPCSTR lpSrc = "hello";
710   LPSTR lpszRes;
711   char dest[8];
712
713   if (!pStrCpyNXA)
714   {
715     win_skip("StrCpyNXA() is not available\n");
716     return;
717   }
718
719   memset(dest, '\n', sizeof(dest));
720   lpszRes = pStrCpyNXA(dest, lpSrc, sizeof(dest)/sizeof(dest[0]));
721   ok(lpszRes == dest + 5 && !memcmp(dest, "hello\0\n\n", sizeof(dest)),
722        "StrCpyNXA: expected %p, \"hello\\0\\n\\n\", got %p, \"%d,%d,%d,%d,%d,%d,%d,%d\"\n",
723        dest + 5, lpszRes, dest[0], dest[1], dest[2], dest[3], dest[4], dest[5], dest[6], dest[7]);
724 }
725
726 static void test_StrCpyNXW(void)
727 {
728   static const WCHAR lpInit[] = { '\n','\n','\n','\n','\n','\n','\n','\n' };
729   static const WCHAR lpSrc[] = { 'h','e','l','l','o','\0' };
730   static const WCHAR lpRes[] = { 'h','e','l','l','o','\0','\n','\n' };
731   LPWSTR lpszRes;
732   WCHAR dest[8];
733
734   if (!pStrCpyNXW)
735   {
736     win_skip("StrCpyNXW() is not available\n");
737     return;
738   }
739
740   memcpy(dest, lpInit, sizeof(lpInit));
741   lpszRes = pStrCpyNXW(dest, lpSrc, sizeof(dest)/sizeof(dest[0]));
742   ok(lpszRes == dest + 5 && !memcmp(dest, lpRes, sizeof(dest)),
743        "StrCpyNXW: expected %p, \"hello\\0\\n\\n\", got %p, \"%d,%d,%d,%d,%d,%d,%d,%d\"\n",
744        dest + 5, lpszRes, dest[0], dest[1], dest[2], dest[3], dest[4], dest[5], dest[6], dest[7]);
745 }
746
747 #define check_strrstri(type, str, pos, needle, exp) \
748     ret##type = StrRStrI##type(str, str+pos, needle); \
749     ok(ret##type == (exp), "Type " #type ", expected %p but got %p (string base %p)\n", \
750     (exp), ret##type, str);
751
752 static void test_StrRStrI(void)
753 {
754     static const CHAR szTest[] = "yAxxxxAy";
755     static const CHAR szTest2[] = "ABABABAB";
756     static const WCHAR wszTest[] = {'y','A','x','x','x','x','A','y',0};
757     static const WCHAR wszTest2[] = {'A','B','A','B','A','B','A','B',0};
758
759     static const WCHAR wszPattern1[] = {'A',0};
760     static const WCHAR wszPattern2[] = {'a','X',0};
761     static const WCHAR wszPattern3[] = {'A','y',0};
762     static const WCHAR wszPattern4[] = {'a','b',0};
763     LPWSTR retW;
764     LPSTR retA;
765     
766     check_strrstri(A, szTest, 4, "A", szTest+1);
767     check_strrstri(A, szTest, 4, "aX", szTest+1);
768     check_strrstri(A, szTest, 4, "Ay", NULL);
769     check_strrstri(W, wszTest, 4, wszPattern1, wszTest+1);
770     check_strrstri(W, wszTest, 4, wszPattern2, wszTest+1);
771     check_strrstri(W, wszTest, 4, wszPattern3, NULL);
772
773     check_strrstri(A, szTest2, 4, "ab", szTest2+2);
774     check_strrstri(A, szTest2, 3, "ab", szTest2+2);
775     check_strrstri(A, szTest2, 2, "ab", szTest2);
776     check_strrstri(A, szTest2, 1, "ab", szTest2);
777     check_strrstri(A, szTest2, 0, "ab", NULL);
778     check_strrstri(W, wszTest2, 4, wszPattern4, wszTest2+2);
779     check_strrstri(W, wszTest2, 3, wszPattern4, wszTest2+2);
780     check_strrstri(W, wszTest2, 2, wszPattern4, wszTest2);
781     check_strrstri(W, wszTest2, 1, wszPattern4, wszTest2);
782     check_strrstri(W, wszTest2, 0, wszPattern4, NULL);
783
784 }
785
786 static void test_SHAnsiToAnsi(void)
787 {
788   char dest[8];
789   DWORD dwRet;
790
791   if (!pSHAnsiToAnsi)
792   {
793     win_skip("SHAnsiToAnsi() is not available\n");
794     return;
795   }
796
797   memset(dest, '\n', sizeof(dest));
798   dwRet = pSHAnsiToAnsi("hello", dest, sizeof(dest)/sizeof(dest[0]));
799   ok(dwRet == 6 && !memcmp(dest, "hello\0\n\n", sizeof(dest)),
800      "SHAnsiToAnsi: expected 6, \"hello\\0\\n\\n\", got %d, \"%d,%d,%d,%d,%d,%d,%d,%d\"\n",
801      dwRet, dest[0], dest[1], dest[2], dest[3], dest[4], dest[5], dest[6], dest[7]);
802 }
803
804 static void test_SHUnicodeToUnicode(void)
805 {
806   static const WCHAR lpInit[] = { '\n','\n','\n','\n','\n','\n','\n','\n' };
807   static const WCHAR lpSrc[] = { 'h','e','l','l','o','\0' };
808   static const WCHAR lpRes[] = { 'h','e','l','l','o','\0','\n','\n' };
809   WCHAR dest[8];
810   DWORD dwRet;
811
812   if (!pSHUnicodeToUnicode)
813   {
814     win_skip("SHUnicodeToUnicode() is not available\n");
815     return;
816   }
817
818   memcpy(dest, lpInit, sizeof(lpInit));
819   dwRet = pSHUnicodeToUnicode(lpSrc, dest, sizeof(dest)/sizeof(dest[0]));
820   ok(dwRet == 6 && !memcmp(dest, lpRes, sizeof(dest)),
821      "SHUnicodeToUnicode: expected 6, \"hello\\0\\n\\n\", got %d, \"%d,%d,%d,%d,%d,%d,%d,%d\"\n",
822      dwRet, dest[0], dest[1], dest[2], dest[3], dest[4], dest[5], dest[6], dest[7]);
823 }
824
825 static void test_StrXXX_overflows(void)
826 {
827     CHAR str1[2*MAX_PATH+1], buf[2*MAX_PATH];
828     WCHAR wstr1[2*MAX_PATH+1], wbuf[2*MAX_PATH];
829     const WCHAR fmt[] = {'%','s',0};
830     STRRET strret;
831     int ret;
832     int i;
833
834     for (i=0; i<2*MAX_PATH; i++)
835     {
836         str1[i] = '0'+(i%10);
837         wstr1[i] = '0'+(i%10);
838     }
839     str1[2*MAX_PATH] = 0;
840     wstr1[2*MAX_PATH] = 0;
841
842     memset(buf, 0xbf, sizeof(buf));
843     expect_eq(StrCpyNA(buf, str1, 10), buf, PCHAR, "%p");
844     expect_eq(buf[9], 0, CHAR, "%x");
845     expect_eq(buf[10], '\xbf', CHAR, "%x");
846
847     if (pStrCatBuffA)
848     {
849         expect_eq(pStrCatBuffA(buf, str1, 100), buf, PCHAR, "%p");
850         expect_eq(buf[99], 0, CHAR, "%x");
851         expect_eq(buf[100], '\xbf', CHAR, "%x");
852     }
853     else
854         win_skip("StrCatBuffA() is not available\n");
855
856     memset(wbuf, 0xbf, sizeof(wbuf));
857     expect_eq(StrCpyNW(wbuf, wstr1, 10), wbuf, PWCHAR, "%p");
858     expect_eq(wbuf[9], 0, WCHAR, "%x");
859     expect_eq(wbuf[10], (WCHAR)0xbfbf, WCHAR, "%x");
860
861     if (pStrCatBuffW)
862     {
863         expect_eq(pStrCatBuffW(wbuf, wstr1, 100), wbuf, PWCHAR, "%p");
864         expect_eq(wbuf[99], 0, WCHAR, "%x");
865         expect_eq(wbuf[100], (WCHAR)0xbfbf, WCHAR, "%x");
866     }
867     else
868         win_skip("StrCatBuffW() is not available\n");
869
870     if (pStrRetToBufW)
871     {
872         memset(wbuf, 0xbf, sizeof(wbuf));
873         strret.uType = STRRET_WSTR;
874         U(strret).pOleStr = StrDupW(wstr1);
875         expect_eq2(pStrRetToBufW(&strret, NULL, wbuf, 10), S_OK, HRESULT_FROM_WIN32(ERROR_INSUFFICIENT_BUFFER) /* Vista */, HRESULT, "%x");
876         expect_eq(wbuf[9], 0, WCHAR, "%x");
877         expect_eq(wbuf[10], (WCHAR)0xbfbf, WCHAR, "%x");
878     }
879     else
880         win_skip("StrRetToBufW() is not available\n");
881
882     if (pStrRetToBufA)
883     {
884         memset(buf, 0xbf, sizeof(buf));
885         strret.uType = STRRET_CSTR;
886         StrCpyN(U(strret).cStr, str1, MAX_PATH);
887         expect_eq2(pStrRetToBufA(&strret, NULL, buf, 10), S_OK, HRESULT_FROM_WIN32(ERROR_INSUFFICIENT_BUFFER) /* Vista */, HRESULT, "%x");
888         expect_eq(buf[9], 0, CHAR, "%x");
889         expect_eq(buf[10], (CHAR)0xbf, CHAR, "%x");
890     }
891     else
892         win_skip("StrRetToBufA() is not available\n");
893
894     if (pwnsprintfA)
895     {
896         memset(buf, 0xbf, sizeof(buf));
897         ret = pwnsprintfA(buf, 10, "%s", str1);
898         ok(broken(ret == 9) || ret == -1 /* Vista */, "Unexpected wsnprintfA return %d, expected 9 or -1\n", ret);
899         expect_eq(buf[9], 0, CHAR, "%x");
900         expect_eq(buf[10], (CHAR)0xbf, CHAR, "%x");
901     }
902     else
903         win_skip("wnsprintfA() is not available\n");
904
905     if (pwnsprintfW)
906     {
907         memset(wbuf, 0xbf, sizeof(wbuf));
908         ret = pwnsprintfW(wbuf, 10, fmt, wstr1);
909         ok(broken(ret == 9) || ret == -1 /* Vista */, "Unexpected wsnprintfW return %d, expected 9 or -1\n", ret);
910         expect_eq(wbuf[9], 0, WCHAR, "%x");
911         expect_eq(wbuf[10], (WCHAR)0xbfbf, WCHAR, "%x");
912     }
913     else
914         win_skip("wnsprintfW() is not available\n");
915 }
916
917 START_TEST(string)
918 {
919   HMODULE hShlwapi;
920   TCHAR thousandDelim[8];
921   TCHAR decimalDelim[8];
922   CoInitialize(0);
923
924   GetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_STHOUSAND, thousandDelim, 8);
925   GetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_SDECIMAL, decimalDelim, 8);
926
927   hShlwapi = GetModuleHandleA("shlwapi");
928   pIntlStrEqWorkerA = (void *)GetProcAddress(hShlwapi, "IntlStrEqWorkerA");
929   pIntlStrEqWorkerW = (void *)GetProcAddress(hShlwapi, "IntlStrEqWorkerW");
930   pSHAnsiToAnsi = (void *)GetProcAddress(hShlwapi, (LPSTR)345);
931   pSHUnicodeToUnicode = (void *)GetProcAddress(hShlwapi, (LPSTR)346);
932   pStrCatBuffA = (void *)GetProcAddress(hShlwapi, "StrCatBuffA");
933   pStrCatBuffW = (void *)GetProcAddress(hShlwapi, "StrCatBuffW");
934   pStrCpyNXA = (void *)GetProcAddress(hShlwapi, (LPSTR)399);
935   pStrCpyNXW = (void *)GetProcAddress(hShlwapi, (LPSTR)400);
936   pStrChrNW = (void *)GetProcAddress(hShlwapi, "StrChrNW");
937   pStrFormatByteSize64A = (void *)GetProcAddress(hShlwapi, "StrFormatByteSize64A");
938   pStrFormatKBSizeA = (void *)GetProcAddress(hShlwapi, "StrFormatKBSizeA");
939   pStrFormatKBSizeW = (void *)GetProcAddress(hShlwapi, "StrFormatKBSizeW");
940   pStrIsIntlEqualA = (void *)GetProcAddress(hShlwapi, "StrIsIntlEqualA");
941   pStrIsIntlEqualW = (void *)GetProcAddress(hShlwapi, "StrIsIntlEqualW");
942   pStrRetToBSTR = (void *)GetProcAddress(hShlwapi, "StrRetToBSTR");
943   pStrRetToBufA = (void *)GetProcAddress(hShlwapi, "StrRetToBufA");
944   pStrRetToBufW = (void *)GetProcAddress(hShlwapi, "StrRetToBufW");
945   pwnsprintfA = (void *)GetProcAddress(hShlwapi, "wnsprintfA");
946   pwnsprintfW = (void *)GetProcAddress(hShlwapi, "wnsprintfW");
947
948   test_StrChrA();
949   test_StrChrW();
950   test_StrChrIA();
951   test_StrChrIW();
952   test_StrRChrA();
953   test_StrRChrW();
954   test_StrCpyW();
955   test_StrChrNW();
956   test_StrToIntA();
957   test_StrToIntW();
958   test_StrToIntExA();
959   test_StrToIntExW();
960   test_StrDupA();
961   if (lstrcmp(thousandDelim, ",")==0 && lstrcmp(decimalDelim, ".")==0)
962   {
963     /* these tests are locale-dependent */
964     test_StrFormatByteSize64A();
965     test_StrFormatKBSizeA();
966     test_StrFormatKBSizeW();
967   }
968
969   /* language-dependent test */
970   if (PRIMARYLANGID(GetUserDefaultLangID()) != LANG_ENGLISH)
971     trace("Skipping StrFromTimeInterval test for non English language\n");
972   else
973     test_StrFromTimeIntervalA();
974
975   test_StrCmpA();
976   test_StrCmpW();
977   test_StrRetToBSTR();
978   test_StrCpyNXA();
979   test_StrCpyNXW();
980   test_StrRStrI();
981   test_SHAnsiToAnsi();
982   test_SHUnicodeToUnicode();
983   test_StrXXX_overflows();
984
985   CoUninitialize();
986 }