shlwapi: Cast-qual warnings fix.
[wine] / dlls / shlwapi / tests / shreg.c
1 /* Unit test suite for SHReg* functions
2  *
3  * Copyright 2002 Juergen Schmied
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 <assert.h>
21 #include <stdarg.h>
22 #include <stdio.h>
23
24 #include "wine/test.h"
25 #include "windef.h"
26 #include "winbase.h"
27 #include "winerror.h"
28 #include "winreg.h"
29 #include "winuser.h"
30 #include "shlwapi.h"
31
32 /* Keys used for testing */
33 #define REG_TEST_KEY        "Software\\Wine\\Test"
34 #define REG_CURRENT_VERSION "Software\\Microsoft\\Windows\\CurrentVersion"
35
36 static HMODULE hshlwapi;
37 typedef DWORD (WINAPI *SHCopyKeyA_func)(HKEY,LPCSTR,HKEY,DWORD);
38 static SHCopyKeyA_func pSHCopyKeyA;
39 typedef DWORD (WINAPI *SHRegGetPathA_func)(HKEY,LPCSTR,LPCSTR,LPSTR,DWORD);
40 static SHRegGetPathA_func pSHRegGetPathA;
41
42 static char sTestpath1[] = "%LONGSYSTEMVAR%\\subdir1";
43 static char sTestpath2[] = "%FOO%\\subdir1";
44
45 static const char * sEnvvar1 = "bar";
46 static const char * sEnvvar2 = "ImARatherLongButIndeedNeededString";
47
48 static char sExpTestpath1[MAX_PATH];
49 static char sExpTestpath2[MAX_PATH];
50 static DWORD nExpLen1;
51 static DWORD nExpLen2;
52
53 static const char * sEmptyBuffer ="0123456789";
54
55 /* delete key and all its subkeys */
56 static DWORD delete_key( HKEY hkey, LPCSTR parent, LPCSTR keyname )
57 {
58     HKEY parentKey;
59     DWORD ret;
60
61     RegCloseKey(hkey);
62
63     /* open the parent of the key to close */
64     ret = RegOpenKeyExA( HKEY_CURRENT_USER, parent, 0, KEY_ALL_ACCESS, &parentKey);
65     if (ret != ERROR_SUCCESS)
66         return ret;
67
68     ret = SHDeleteKeyA( parentKey, keyname );
69     RegCloseKey(parentKey);
70
71     return ret;
72 }
73
74 static HKEY create_test_entries(void)
75 {
76         HKEY hKey;
77         DWORD ret;
78         DWORD nExpectedLen1, nExpectedLen2;
79
80         SetEnvironmentVariableA("LONGSYSTEMVAR", sEnvvar1);
81         SetEnvironmentVariableA("FOO", sEnvvar2);
82
83         ret = RegCreateKeyA(HKEY_CURRENT_USER, REG_TEST_KEY, &hKey);
84         ok( ERROR_SUCCESS == ret, "RegCreateKeyA failed, ret=%u\n", ret);
85
86         if (hKey)
87         {
88            ok(!RegSetValueExA(hKey,"Test1",0,REG_EXPAND_SZ, (LPBYTE) sTestpath1, strlen(sTestpath1)+1), "RegSetValueExA failed\n");
89            ok(!RegSetValueExA(hKey,"Test2",0,REG_SZ, (LPBYTE) sTestpath1, strlen(sTestpath1)+1), "RegSetValueExA failed\n");
90            ok(!RegSetValueExA(hKey,"Test3",0,REG_EXPAND_SZ, (LPBYTE) sTestpath2, strlen(sTestpath2)+1), "RegSetValueExA failed\n");
91         }
92
93         nExpLen1 = ExpandEnvironmentStringsA(sTestpath1, sExpTestpath1, sizeof(sExpTestpath1));
94         nExpLen2 = ExpandEnvironmentStringsA(sTestpath2, sExpTestpath2, sizeof(sExpTestpath2));
95
96         nExpectedLen1 = strlen(sTestpath1) - strlen("%LONGSYSTEMVAR%") + strlen(sEnvvar1) + 1;
97         nExpectedLen2 = strlen(sTestpath2) - strlen("%FOO%") + strlen(sEnvvar2) + 1;
98         /* ExpandEnvironmentStringsA on NT4 returns 2x the correct result */
99         trace("sExplen1 = (%d)\n", nExpLen1);
100         if (nExpectedLen1 != nExpLen1)
101             trace( "Expanding %s failed (expected %d) - known bug in NT4\n", sTestpath1, nExpectedLen1 );
102
103         trace("sExplen2 = (%d)\n", nExpLen2);
104         if (nExpectedLen2 != nExpLen2)
105             trace( "Expanding %s failed (expected %d) - known bug in NT4\n", sTestpath2, nExpectedLen2 );       
106
107         /* Make sure we carry on with correct values */
108         nExpLen1 = nExpectedLen1; 
109         nExpLen2 = nExpectedLen2;
110         return hKey;
111 }
112
113 static void test_SHGetValue(void)
114 {
115         DWORD dwSize;
116         DWORD dwType;
117         DWORD dwRet;
118         char buf[MAX_PATH];
119
120         strcpy(buf, sEmptyBuffer);
121         dwSize = MAX_PATH;
122         dwType = -1;
123         dwRet = SHGetValueA(HKEY_CURRENT_USER, REG_TEST_KEY, "Test1", &dwType, buf, &dwSize);
124         ok( ERROR_SUCCESS == dwRet, "SHGetValueA failed, ret=%u\n", dwRet);
125         ok( 0 == strcmp(sExpTestpath1, buf), "Comparing of (%s) with (%s) failed\n", buf, sExpTestpath1);
126         ok( REG_SZ == dwType, "Expected REG_SZ, got (%u)\n", dwType);
127
128         strcpy(buf, sEmptyBuffer);
129         dwSize = MAX_PATH;
130         dwType = -1;
131         dwRet = SHGetValueA(HKEY_CURRENT_USER, REG_TEST_KEY, "Test2", &dwType, buf, &dwSize);
132         ok( ERROR_SUCCESS == dwRet, "SHGetValueA failed, ret=%u\n", dwRet);
133         ok( 0 == strcmp(sTestpath1, buf) , "Comparing of (%s) with (%s) failed\n", buf, sTestpath1);
134         ok( REG_SZ == dwType , "Expected REG_SZ, got (%u)\n", dwType);
135 }
136
137 static void test_SHGetRegPath(void)
138 {
139         char buf[MAX_PATH];
140         DWORD dwRet;
141
142         if (!pSHRegGetPathA)
143                 return;
144
145         strcpy(buf, sEmptyBuffer);
146         dwRet = (*pSHRegGetPathA)(HKEY_CURRENT_USER, REG_TEST_KEY, "Test1", buf, 0);
147         ok( ERROR_SUCCESS == dwRet, "SHRegGetPathA failed, ret=%u\n", dwRet);
148         ok( 0 == strcmp(sExpTestpath1, buf) , "Comparing (%s) with (%s) failed\n", buf, sExpTestpath1);
149 }
150
151 static void test_SHQUeryValueEx(void)
152 {
153         HKEY hKey;
154         DWORD dwSize;
155         DWORD dwType;
156         char buf[MAX_PATH];
157         DWORD dwRet;
158         const char * sTestedFunction = "";
159         DWORD nUsedBuffer1,nUsedBuffer2;
160
161         sTestedFunction = "RegOpenKeyExA";
162         dwRet = RegOpenKeyExA(HKEY_CURRENT_USER, REG_TEST_KEY, 0,  KEY_QUERY_VALUE, &hKey);
163         ok( ERROR_SUCCESS == dwRet, "%s failed, ret=%u\n", sTestedFunction, dwRet);
164
165         /****** SHQueryValueExA ******/
166
167         sTestedFunction = "SHQueryValueExA";
168         nUsedBuffer1 = max(strlen(sExpTestpath1)+1, strlen(sTestpath1)+1);
169         nUsedBuffer2 = max(strlen(sExpTestpath2)+1, strlen(sTestpath2)+1);
170         /*
171          * Case 1.1 All arguments are NULL
172          */
173         dwRet = SHQueryValueExA( hKey, "Test1", NULL, NULL, NULL, NULL);
174         ok( ERROR_SUCCESS == dwRet, "%s failed, ret=%u\n", sTestedFunction, dwRet);
175
176         /*
177          * Case 1.2 dwType is set
178          */
179         dwType = -1;
180         dwRet = SHQueryValueExA( hKey, "Test1", NULL, &dwType, NULL, NULL);
181         ok( ERROR_SUCCESS == dwRet, "%s failed, ret=%u\n", sTestedFunction, dwRet);
182         ok( REG_SZ == dwType , "Expected REG_SZ, got (%u)\n", dwType);
183
184         /*
185          * dwSize is set
186          * dwExpanded < dwUnExpanded
187          */
188         dwSize = 6;
189         dwRet = SHQueryValueExA( hKey, "Test1", NULL, NULL, NULL, &dwSize);
190         ok( ERROR_SUCCESS == dwRet, "%s failed, ret=%u\n", sTestedFunction, dwRet);
191         ok( dwSize == nUsedBuffer1, "Buffer sizes (%u) and (%u) are not equal\n", dwSize, nUsedBuffer1);
192
193         /*
194          * dwExpanded > dwUnExpanded
195          */
196         dwSize = 6;
197         dwRet = SHQueryValueExA( hKey, "Test3", NULL, NULL, NULL, &dwSize);
198         ok( ERROR_SUCCESS == dwRet, "%s failed, ret=%u\n", sTestedFunction, dwRet);
199         ok( dwSize >= nUsedBuffer2, "Buffer size (%u) should be >= (%u)\n", dwSize, nUsedBuffer2);
200
201         /*
202          * Case 1 string shrinks during expanding
203          */
204         strcpy(buf, sEmptyBuffer);
205         dwSize = 6;
206         dwType = -1;
207         dwRet = SHQueryValueExA( hKey, "Test1", NULL, &dwType, buf, &dwSize);
208         ok( ERROR_MORE_DATA == dwRet, "Expected ERROR_MORE_DATA, got (%u)\n", dwRet);
209         ok( 0 == strcmp(sEmptyBuffer, buf) , "Comparing (%s) with (%s) failed\n", buf, sEmptyBuffer);
210         ok( dwSize == nUsedBuffer1, "Buffer sizes (%u) and (%u) are not equal\n", dwSize, nUsedBuffer1);
211         ok( REG_SZ == dwType , "Expected REG_SZ, got (%u)\n", dwType);
212
213         /*
214          * string grows during expanding
215          * dwSize is smaller then the size of the unexpanded string
216          */
217         strcpy(buf, sEmptyBuffer);
218         dwSize = 6;
219         dwType = -1;
220         dwRet = SHQueryValueExA( hKey, "Test3", NULL, &dwType, buf, &dwSize);
221         ok( ERROR_MORE_DATA == dwRet, "Expected ERROR_MORE_DATA, got (%u)\n", dwRet);
222         ok( 0 == strcmp(sEmptyBuffer, buf) , "Comparing (%s) with (%s) failed\n", buf, sEmptyBuffer);
223         ok( dwSize >= nUsedBuffer2, "Buffer size (%u) should be >= (%u)\n", dwSize, nUsedBuffer2);
224         ok( REG_SZ == dwType , "Expected REG_SZ, got (%u)\n", dwType);
225
226         /*
227          * string grows during expanding
228          * dwSize is larger then the size of the unexpanded string but smaller than the part before the backslash
229          * if the unexpanded string fits into the buffer it can get cut when expanded
230          */
231         strcpy(buf, sEmptyBuffer);
232         dwSize = strlen(sEnvvar2) - 2;
233         dwType = -1;
234         dwRet = SHQueryValueExA( hKey, "Test3", NULL, &dwType, buf, &dwSize);
235         ok( ERROR_MORE_DATA == dwRet, "Expected ERROR_MORE_DATA, got (%u)\n", dwRet);
236
237         todo_wine
238         {
239                 ok( (0 == strcmp("", buf)) || (0 == strcmp(sTestpath2, buf)),
240                     "Expected empty or unexpanded string (win98), got (%s)\n", buf); 
241         }
242
243         ok( dwSize >= nUsedBuffer2, "Buffer size (%u) should be >= (%u)\n", dwSize, nUsedBuffer2);
244         ok( REG_SZ == dwType , "Expected REG_SZ, got (%u)\n", dwType);
245
246         /*
247          * string grows during expanding
248          * dwSize is larger then the size of the part before the backslash but smaller then the expanded string
249          * if the unexpanded string fits into the buffer it can get cut when expanded
250          */
251         strcpy(buf, sEmptyBuffer);
252         dwSize = nExpLen2 - 4;
253         dwType = -1;
254         dwRet = SHQueryValueExA( hKey, "Test3", NULL, &dwType, buf, &dwSize);
255         ok( ERROR_MORE_DATA == dwRet, "Expected ERROR_MORE_DATA, got (%u)\n", dwRet);
256
257         todo_wine
258         {
259             ok( (0 == strcmp("", buf)) || (0 == strcmp(sEnvvar2, buf)),
260                     "Expected empty or first part of the string \"%s\", got \"%s\"\n", sEnvvar2, buf);
261         }
262
263         ok( dwSize >= nUsedBuffer2, "Buffer size (%u) should be >= (%u)\n", dwSize, nUsedBuffer2);
264         ok( REG_SZ == dwType , "Expected REG_SZ, got (%u)\n", dwType);
265
266         /*
267          * The buffer is NULL but the size is set
268          */
269         strcpy(buf, sEmptyBuffer);
270         dwSize = 6;
271         dwType = -1;
272         dwRet = SHQueryValueExA( hKey, "Test3", NULL, &dwType, NULL, &dwSize);
273         ok( ERROR_SUCCESS == dwRet, "%s failed, ret=%u\n", sTestedFunction, dwRet);
274         ok( dwSize >= nUsedBuffer2, "Buffer size (%u) should be >= (%u)\n", dwSize, nUsedBuffer2);
275         ok( REG_SZ == dwType , "Expected REG_SZ, got (%u)\n", dwType);
276
277         RegCloseKey(hKey);
278 }
279
280 static void test_SHCopyKey(void)
281 {
282         HKEY hKeySrc, hKeyDst;
283         DWORD dwRet;
284
285         /* Delete existing destination sub keys */
286         hKeyDst = NULL;
287         if (!RegOpenKeyA(HKEY_CURRENT_USER, REG_TEST_KEY "\\CopyDestination", &hKeyDst) && hKeyDst)
288         {
289                 SHDeleteKeyA(hKeyDst, NULL);
290                 RegCloseKey(hKeyDst);
291         }
292
293         hKeyDst = NULL;
294         dwRet = RegCreateKeyA(HKEY_CURRENT_USER, REG_TEST_KEY "\\CopyDestination", &hKeyDst);
295         if (dwRet || !hKeyDst)
296         {
297                 ok( 0, "Destination couldn't be created, RegCreateKeyA returned (%u)\n", dwRet);
298                 return;
299         }
300
301         hKeySrc = NULL;
302         dwRet = RegOpenKeyA(HKEY_LOCAL_MACHINE, REG_CURRENT_VERSION, &hKeySrc);
303         if (dwRet || !hKeySrc)
304         {
305                 ok( 0, "Source couldn't be opened, RegOpenKeyA returned (%u)\n", dwRet);
306                 return;
307         }
308
309
310         if (pSHCopyKeyA)
311         {
312                 dwRet = (*pSHCopyKeyA)(hKeySrc, NULL, hKeyDst, 0);
313                 ok ( ERROR_SUCCESS == dwRet, "Copy failed, ret=(%u)\n", dwRet);
314         }
315
316         RegCloseKey(hKeySrc);
317         RegCloseKey(hKeyDst);
318
319         /* Check we copied the sub keys, i.e. something that's on every windows system (including Wine) */
320         hKeyDst = NULL;
321         dwRet = RegOpenKeyA(HKEY_CURRENT_USER, REG_TEST_KEY "\\CopyDestination\\Setup", &hKeyDst);
322         if (dwRet || !hKeyDst)
323         {
324                 ok ( 0, "Copy couldn't be opened, RegOpenKeyA returned (%u)\n", dwRet);
325                 return;
326         }
327
328         /* And the we copied the values too */
329         ok(!SHQueryValueExA(hKeyDst, "BootDir", NULL, NULL, NULL, NULL), "SHQueryValueExA failed\n");
330
331         RegCloseKey(hKeyDst);
332 }
333
334 static void test_SHDeleteKey(void)
335 {
336     HKEY hKeyTest, hKeyS;
337     DWORD dwRet;
338     int sysfail = 1;
339
340     if (!RegOpenKeyA(HKEY_CURRENT_USER, REG_TEST_KEY, &hKeyTest))
341     {
342         if (!RegCreateKey(hKeyTest, "ODBC", &hKeyS))
343         {
344             HKEY hKeyO;
345
346             if (!RegCreateKey(hKeyS, "ODBC.INI", &hKeyO))
347             {
348                 RegCloseKey (hKeyO);
349
350                 if (!RegCreateKey(hKeyS, "ODBCINST.INI", &hKeyO))
351                 {
352                     RegCloseKey (hKeyO);
353                     sysfail = 0;
354                 }
355             }
356             RegCloseKey (hKeyS);
357         }
358         RegCloseKey (hKeyTest);
359     }
360
361     if (!sysfail)
362     {
363
364         dwRet = SHDeleteKeyA(HKEY_CURRENT_USER, REG_TEST_KEY "\\ODBC");
365         ok ( ERROR_SUCCESS == dwRet, "SHDeleteKey failed, ret=(%u)\n", dwRet);
366
367         dwRet = RegOpenKeyA(HKEY_CURRENT_USER, REG_TEST_KEY "\\ODBC", &hKeyS);
368         ok ( ERROR_FILE_NOT_FOUND == dwRet, "SHDeleteKey did not delete\n");
369
370         if (dwRet == ERROR_SUCCESS)
371             RegCloseKey (hKeyS);
372     }
373     else
374         ok( 0, "Could not set up SHDeleteKey test\n");
375 }
376
377 START_TEST(shreg)
378 {
379         HKEY hkey = create_test_entries();
380
381         if (!hkey) return;
382
383         hshlwapi = GetModuleHandleA("shlwapi.dll");
384         if (hshlwapi)
385         {
386                 pSHCopyKeyA=(SHCopyKeyA_func)GetProcAddress(hshlwapi,"SHCopyKeyA");
387                 pSHRegGetPathA=(SHRegGetPathA_func)GetProcAddress(hshlwapi,"SHRegGetPathA");
388         }
389         test_SHGetValue();
390         test_SHQUeryValueEx();
391         test_SHGetRegPath();
392         test_SHCopyKey();
393         test_SHDeleteKey();
394         delete_key( hkey, "Software\\Wine", "Test" );
395 }