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