1 /*
2  * Unit tests for shell32 string operations
3  *
4  * Copyright 2004 Jon Griffiths
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 "precomp.h"
22 
23 static HMODULE hShell32;
24 static BOOL (WINAPI *pStrRetToStrNAW)(LPVOID,DWORD,LPSTRRET,const ITEMIDLIST *);
25 
26 static WCHAR *CoDupStrW(const char* src)
27 {
28   INT len = MultiByteToWideChar(CP_ACP, 0, src, -1, NULL, 0);
29   WCHAR* szTemp = CoTaskMemAlloc(len * sizeof(WCHAR));
30   MultiByteToWideChar(CP_ACP, 0, src, -1, szTemp, len);
31   return szTemp;
32 }
33 
34 static inline int strcmpW(const WCHAR *str1, const WCHAR *str2)
35 {
36     while (*str1 && (*str1 == *str2)) { str1++; str2++; }
37     return *str1 - *str2;
38 }
39 
40 static void test_StrRetToStringNA(void)
41 {
42     trace("StrRetToStringNAW is Ascii\n");
43     /* FIXME */
44 }
45 
46 static void test_StrRetToStringNW(void)
47 {
48     static const WCHAR szTestW[] = { 'T','e','s','t','\0' };
49     ITEMIDLIST iidl[10];
50     WCHAR buff[128];
51     STRRET strret;
52     BOOL ret;
53 
54     trace("StrRetToStringNAW is Unicode\n");
55 
56     strret.uType = STRRET_WSTR;
57     U(strret).pOleStr = CoDupStrW("Test");
58     memset(buff, 0xff, sizeof(buff));
59     ret = pStrRetToStrNAW(buff, sizeof(buff)/sizeof(WCHAR), &strret, NULL);
60     ok(ret == TRUE && !strcmpW(buff, szTestW),
61        "STRRET_WSTR: dup failed, ret=%d\n", ret);
62 
63     strret.uType = STRRET_CSTR;
64     lstrcpyA(U(strret).cStr, "Test");
65     memset(buff, 0xff, sizeof(buff));
66     ret = pStrRetToStrNAW(buff, sizeof(buff)/sizeof(WCHAR), &strret, NULL);
67     ok(ret == TRUE && !strcmpW(buff, szTestW),
68        "STRRET_CSTR: dup failed, ret=%d\n", ret);
69 
70     strret.uType = STRRET_OFFSET;
71     U(strret).uOffset = 1;
72     strcpy((char*)&iidl, " Test");
73     memset(buff, 0xff, sizeof(buff));
74     ret = pStrRetToStrNAW(buff, sizeof(buff)/sizeof(WCHAR), &strret, iidl);
75     ok(ret == TRUE && !strcmpW(buff, szTestW),
76        "STRRET_OFFSET: dup failed, ret=%d\n", ret);
77 
78     /* The next test crashes on W2K, WinXP and W2K3, so we don't test. */
79 if (0)
80 {
81     /* Invalid dest - should return FALSE, except NT4 does not, so we don't check. */
82     strret.uType = STRRET_WSTR;
83     U(strret).pOleStr = CoDupStrW("Test");
84     pStrRetToStrNAW(NULL, sizeof(buff)/sizeof(WCHAR), &strret, NULL);
85     trace("NULL dest: ret=%d\n", ret);
86 }
87 }
88 
89 START_TEST(string)
90 {
91     CoInitialize(0);
92 
93     hShell32 = GetModuleHandleA("shell32.dll");
94 
95     pStrRetToStrNAW = (void*)GetProcAddress(hShell32, (LPSTR)96);
96     if (pStrRetToStrNAW)
97     {
98         if (!(GetVersion() & 0x80000000))
99             test_StrRetToStringNW();
100         else
101             test_StrRetToStringNA();
102     }
103 
104     CoUninitialize();
105 }
106