Keep track of per-column information inside the listview.
[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 <stdio.h>
23
24 #include "wine/test.h"
25 #include "winbase.h"
26 #include "winerror.h"
27 #include "winreg.h"
28 #include "winuser.h"
29 #include "shlwapi.h"
30
31 /* Keys used for testing */
32 #define REG_TEST_KEY        "Software\\Wine\\Test"
33 #define REG_CURRENT_VERSION "Software\\Microsoft\\Windows NT\\CurrentVersion"
34
35 static HMODULE hshlwapi;
36 typedef DWORD (WINAPI *SHCopyKeyA_func)(HKEY,LPCSTR,HKEY,DWORD);
37 static SHCopyKeyA_func pSHCopyKeyA;
38 typedef DWORD (WINAPI *SHRegGetPathA_func)(HKEY,LPCSTR,LPCSTR,LPSTR,DWORD);
39 static SHRegGetPathA_func pSHRegGetPathA;
40
41 static char * sTestpath1 = "%LONGSYSTEMVAR%\\subdir1";
42 static char * sTestpath2 = "%FOO%\\subdir1";
43
44 static char sExpTestpath1[MAX_PATH];
45 static char sExpTestpath2[MAX_PATH];
46 static unsigned sExpLen1;
47 static unsigned sExpLen2;
48
49 static char * sEmptyBuffer ="0123456789";
50
51 /* delete key and all its subkeys */
52 static DWORD delete_key( HKEY hkey )
53 {
54     WCHAR name[MAX_PATH];
55     DWORD ret;
56
57     while (!(ret = RegEnumKeyW(hkey, 0, name, sizeof(name))))
58     {
59         HKEY tmp;
60         if (!(ret = RegOpenKeyExW( hkey, name, 0, KEY_ENUMERATE_SUB_KEYS, &tmp )))
61         {
62             ret = delete_key( tmp );
63             RegCloseKey( tmp );
64         }
65         if (ret) break;
66     }
67     if (ret != ERROR_NO_MORE_ITEMS) return ret;
68     RegDeleteKeyA( hkey, NULL );
69     return 0;
70 }
71
72 static HKEY create_test_entries(void)
73 {
74         HKEY hKey;
75
76         SetEnvironmentVariableA("LONGSYSTEMVAR", "bar");
77         SetEnvironmentVariableA("FOO", "ImARatherLongButIndeedNeededString");
78
79         ok(!RegCreateKeyA(HKEY_CURRENT_USER, REG_TEST_KEY, &hKey), "RegCreateKeyA failed");
80
81         if (hKey)
82         {
83            ok(!RegSetValueExA(hKey,"Test1",0,REG_EXPAND_SZ, sTestpath1, strlen(sTestpath1)+1), "RegSetValueExA failed");
84            ok(!RegSetValueExA(hKey,"Test2",0,REG_SZ, sTestpath1, strlen(sTestpath1)+1), "RegSetValueExA failed");
85            ok(!RegSetValueExA(hKey,"Test3",0,REG_EXPAND_SZ, sTestpath2, strlen(sTestpath2)+1), "RegSetValueExA failed");
86         }
87
88         sExpLen1 = ExpandEnvironmentStringsA(sTestpath1, sExpTestpath1, sizeof(sExpTestpath1));
89         sExpLen2 = ExpandEnvironmentStringsA(sTestpath2, sExpTestpath2, sizeof(sExpTestpath2));
90
91         ok(sExpLen1 > 0, "Couldn't expand %s\n", sTestpath1);
92         ok(sExpLen2 > 0, "Couldn't expand %s\n", sTestpath2);
93         return hKey;
94 }
95
96 static void test_SHGetValue(void)
97 {
98         DWORD dwSize;
99         DWORD dwType;
100         char buf[MAX_PATH];
101
102         strcpy(buf, sEmptyBuffer);
103         dwSize = MAX_PATH;
104         dwType = -1;
105         ok(! SHGetValueA(HKEY_CURRENT_USER, REG_TEST_KEY, "Test1", &dwType, buf, &dwSize), "SHGetValueA failed");
106         ok( 0 == strcmp(sExpTestpath1, buf), "(%s,%s)", buf, sExpTestpath1);
107         ok( REG_SZ == dwType, "(%lx)", dwType);
108
109         strcpy(buf, sEmptyBuffer);
110         dwSize = MAX_PATH;
111         dwType = -1;
112         ok(! SHGetValueA(HKEY_CURRENT_USER, REG_TEST_KEY, "Test2", &dwType, buf, &dwSize), "SHGetValueA failed");
113         ok( 0 == strcmp(sTestpath1, buf) , "(%s)", buf);
114         ok( REG_SZ == dwType , "(%lx)", dwType);
115 }
116
117 static void test_SHGetRegPath(void)
118 {
119         char buf[MAX_PATH];
120
121         if (!pSHRegGetPathA)
122                 return;
123
124         strcpy(buf, sEmptyBuffer);
125         ok(! (*pSHRegGetPathA)(HKEY_CURRENT_USER, REG_TEST_KEY, "Test1", buf, 0), "SHRegGetPathA failed");
126         ok( 0 == strcmp(sExpTestpath1, buf) , "(%s)", buf);
127 }
128
129 static void test_SHQUeryValueEx(void)
130 {
131         HKEY hKey;
132         DWORD dwSize;
133         DWORD dwType;
134         char buf[MAX_PATH];
135         DWORD dwRet;
136         char * sTestedFunction = "";
137         int nUsedBuffer1;
138         int nUsedBuffer2;
139
140         ok(! RegOpenKeyExA(HKEY_CURRENT_USER, REG_TEST_KEY, 0,  KEY_QUERY_VALUE, &hKey), "test4 RegOpenKey");
141
142         /****** SHQueryValueExA ******/
143
144         sTestedFunction = "SHQueryValueExA";
145         nUsedBuffer1 = max(strlen(sExpTestpath1)+1, strlen(sTestpath1)+1);
146         nUsedBuffer2 = max(strlen(sExpTestpath2)+1, strlen(sTestpath2)+1);
147         /*
148          * Case 1.1 All arguments are NULL
149          */
150         ok(! SHQueryValueExA( hKey, "Test1", NULL, NULL, NULL, NULL), "SHQueryValueExA failed");
151
152         /*
153          * Case 1.2 dwType is set
154          */
155         dwType = -1;
156         ok(! SHQueryValueExA( hKey, "Test1", NULL, &dwType, NULL, NULL), "SHQueryValueExA failed");
157         ok( dwType == REG_SZ, "(%lu)", dwType);
158
159         /*
160          * dwSize is set
161          * dwExpanded < dwUnExpanded
162          */
163         dwSize = 6;
164         ok(! SHQueryValueExA( hKey, "Test1", NULL, NULL, NULL, &dwSize), "SHQueryValueExA failed");
165         ok( dwSize == nUsedBuffer1, "(%lu,%u)", dwSize, nUsedBuffer1);
166
167         /*
168          * dwExpanded > dwUnExpanded
169          */
170         dwSize = 6;
171         ok(! SHQueryValueExA( hKey, "Test3", NULL, NULL, NULL, &dwSize), "SHQueryValueExA failed");
172         ok( dwSize == nUsedBuffer2, "(%lu,%u)", dwSize, nUsedBuffer2);
173
174
175         /*
176          * Case 1 string shrinks during expanding
177          */
178         strcpy(buf, sEmptyBuffer);
179         dwSize = 6;
180         dwType = -1;
181         dwRet = SHQueryValueExA( hKey, "Test1", NULL, &dwType, buf, &dwSize);
182         ok( dwRet == ERROR_MORE_DATA, "(%lu)", dwRet);
183         ok( 0 == strcmp(sEmptyBuffer, buf), "(%s)", buf);
184         ok( dwType == REG_SZ, "(%lu)" , dwType);
185         ok( dwSize == nUsedBuffer1, "(%lu,%u)" , dwSize, nUsedBuffer1);
186
187         /*
188          * string grows during expanding
189          */
190         strcpy(buf, sEmptyBuffer);
191         dwSize = 6;
192         dwType = -1;
193         dwRet = SHQueryValueExA( hKey, "Test3", NULL, &dwType, buf, &dwSize);
194         ok( ERROR_MORE_DATA == dwRet, "ERROR_MORE_DATA");
195         ok( 0 == strcmp(sEmptyBuffer, buf), "(%s)", buf);
196         ok( dwSize == nUsedBuffer2, "(%lu,%u)" , dwSize, nUsedBuffer2);
197         ok( dwType == REG_SZ, "(%lu)" , dwType);
198
199         /*
200          * if the unexpanded string fits into the buffer it can get cut when expanded
201          */
202         strcpy(buf, sEmptyBuffer);
203         dwSize = sExpLen2 - 4;
204         dwType = -1;
205         ok( ERROR_MORE_DATA == SHQueryValueExA( hKey, "Test3", NULL, &dwType, buf, &dwSize), "Expected ERROR_MORE_DATA");
206         ok( 0 == strncmp(sExpTestpath2, buf, sExpLen2 - 4 - 1), "(%s)", buf);
207         ok( sExpLen2 - 4 - 1 == strlen(buf), "(%s)", buf);
208         ok( dwSize == nUsedBuffer2, "(%lu,%u)" , dwSize, nUsedBuffer2);
209         ok( dwType == REG_SZ, "(%lu)" , dwType);
210
211         /*
212          * The buffer is NULL but the size is set
213          */
214         strcpy(buf, sEmptyBuffer);
215         dwSize = 6;
216         dwType = -1;
217         dwRet = SHQueryValueExA( hKey, "Test3", NULL, &dwType, NULL, &dwSize);
218         ok( ERROR_SUCCESS == dwRet, "(%lu)", dwRet);
219         ok( dwSize == nUsedBuffer2, "(%lu,%u)" , dwSize, nUsedBuffer2);
220         ok( dwType == REG_SZ, "(%lu)" , dwType);
221
222
223         RegCloseKey(hKey);
224 }
225
226 static void test_SHCopyKey(void)
227 {
228         HKEY hKeySrc, hKeyDst;
229
230         /* Delete existing destination sub keys */
231         hKeyDst = (HKEY)0;
232         if (!RegOpenKeyA(HKEY_CURRENT_USER, REG_TEST_KEY "\\CopyDestination", &hKeyDst) && hKeyDst)
233         {
234                 SHDeleteKeyA(hKeyDst, NULL);
235                 RegCloseKey(hKeyDst);
236         }
237
238         hKeyDst = (HKEY)0;
239         if (RegCreateKeyA(HKEY_CURRENT_USER, REG_TEST_KEY "\\CopyDestination", &hKeyDst) || !hKeyDst)
240         {
241                 ok(0, "didn't open dest");
242                 return;
243         }
244
245         hKeySrc = (HKEY)0;
246         if (RegOpenKeyA(HKEY_LOCAL_MACHINE, REG_CURRENT_VERSION, &hKeySrc) || !hKeySrc)
247         {
248                 ok(0, "didn't open source");
249                 return;
250         }
251
252
253         if (pSHCopyKeyA)
254                 ok (!(*pSHCopyKeyA)(hKeyDst, NULL, hKeySrc, 0), "failed copy");
255
256         RegCloseKey(hKeySrc);
257         RegCloseKey(hKeyDst);
258
259         /* Check we copied the sub keys, i.e. AeDebug from the default wine registry */
260         hKeyDst = (HKEY)0;
261         if (RegOpenKeyA(HKEY_CURRENT_USER, REG_TEST_KEY "\\CopyDestination\\AeDebug", &hKeyDst) || !hKeyDst)
262         {
263                 ok(0, "didn't open copy");
264                 return;
265         }
266
267         /* And the we copied the values too */
268         ok(!SHQueryValueExA(hKeyDst, "Debugger", NULL, NULL, NULL, NULL), "SHQueryValueExA failed");
269
270         RegCloseKey(hKeyDst);
271 }
272
273
274 START_TEST(shreg)
275 {
276         HKEY hkey = create_test_entries();
277         hshlwapi = GetModuleHandleA("shlwapi.dll");
278         if (hshlwapi)
279         {
280                 pSHCopyKeyA=(SHCopyKeyA_func)GetProcAddress(hshlwapi,"SHCopyKeyA");
281                 pSHRegGetPathA=(SHRegGetPathA_func)GetProcAddress(hshlwapi,"SHRegGetPathA");
282         }
283         test_SHGetValue();
284         test_SHQUeryValueEx();
285         test_SHGetRegPath();
286         test_SHCopyKey();
287         delete_key( hkey );
288 }