Implement/document @17,18,19,20,21,22 (Compact list API).
[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 static char * sTestpath1 = "%LONGSYSTEMVAR%\\subdir1";
32 static char * sTestpath2 = "%FOO%\\subdir1";
33
34 static char sExpTestpath1[MAX_PATH];
35 static char sExpTestpath2[MAX_PATH];
36 static unsigned sExpLen1;
37 static unsigned sExpLen2;
38
39 static char * sEmptyBuffer ="0123456789";
40
41 static void create_test_entrys(void)
42 {
43         HKEY hKey;
44
45         SetEnvironmentVariableA("LONGSYSTEMVAR", "bar");
46         SetEnvironmentVariableA("FOO", "ImARatherLongButIndeedNeededString");
47
48         ok(!RegCreateKeyA(HKEY_CURRENT_USER, "Software\\Wine\\Test", &hKey), "RegCreateKeyA failed");
49
50         if (hKey)
51         {
52            ok(!RegSetValueExA(hKey,"Test1",0,REG_EXPAND_SZ, sTestpath1, strlen(sTestpath1)), "RegSetValueExA failed");
53            ok(!RegSetValueExA(hKey,"Test2",0,REG_SZ, sTestpath1, strlen(sTestpath1)), "RegSetValueExA failed");
54            ok(!RegSetValueExA(hKey,"Test3",0,REG_EXPAND_SZ, sTestpath2, strlen(sTestpath2)), "RegSetValueExA failed");
55            RegCloseKey(hKey);
56         }
57
58         sExpLen1 = ExpandEnvironmentStringsA(sTestpath1, sExpTestpath1, sizeof(sExpTestpath1));
59         sExpLen2 = ExpandEnvironmentStringsA(sTestpath2, sExpTestpath2, sizeof(sExpTestpath2));
60
61         ok(sExpLen1 > 0, "Couldn't expand %s\n", sTestpath1);
62         ok(sExpLen2 > 0, "Couldn't expand %s\n", sTestpath2);
63 }
64
65 static void test_SHGetValue(void)
66 {
67         DWORD dwSize;
68         DWORD dwType;
69         char buf[MAX_PATH];
70
71         strcpy(buf, sEmptyBuffer);
72         dwSize = MAX_PATH;
73         dwType = -1;
74         ok(! SHGetValueA(HKEY_CURRENT_USER, "Software\\Wine\\Test", "Test1", &dwType, buf, &dwSize), "SHGetValueA failed");
75         ok( 0 == strcmp(sExpTestpath1, buf), "(%s,%s)", buf, sExpTestpath1);
76         ok( REG_SZ == dwType, "(%lx)", dwType);
77
78         strcpy(buf, sEmptyBuffer);
79         dwSize = MAX_PATH;
80         dwType = -1;
81         ok(! SHGetValueA(HKEY_CURRENT_USER, "Software\\Wine\\Test", "Test2", &dwType, buf, &dwSize), "SHGetValueA failed");
82         ok( 0 == strcmp(sTestpath1, buf) , "(%s)", buf);
83         ok( REG_SZ == dwType , "(%lx)", dwType);
84 }
85
86 static void test_SHGetTegPath(void)
87 {
88         char buf[MAX_PATH];
89
90         strcpy(buf, sEmptyBuffer);
91         ok(! SHRegGetPathA(HKEY_CURRENT_USER, "Software\\Wine\\Test", "Test1", buf, 0), "SHRegGetPathA failed");
92         ok( 0 == strcmp(sExpTestpath1, buf) , "(%s)", buf);
93 }
94
95 static void test_SHQUeryValueEx(void)
96 {
97         HKEY hKey;
98         DWORD dwSize;
99         DWORD dwType;
100         char buf[MAX_PATH];
101         DWORD dwRet;
102         char * sTestedFunction = "";
103         int nUsedBuffer1;
104         int nUsedBuffer2;
105
106         ok(! RegOpenKeyExA(HKEY_CURRENT_USER, "Software\\Wine\\Test", 0,  KEY_QUERY_VALUE, &hKey), "test4 RegOpenKey");
107
108         /****** SHQueryValueExA ******/
109
110         sTestedFunction = "SHQueryValueExA";
111         nUsedBuffer1 = max(strlen(sExpTestpath1)+1, strlen(sTestpath1));
112         nUsedBuffer2 = max(strlen(sExpTestpath2)+1, strlen(sTestpath2));
113         /*
114          * Case 1.1 All arguments are NULL
115          */
116         ok(! SHQueryValueExA( hKey, "Test1", NULL, NULL, NULL, NULL), "SHQueryValueExA failed");
117
118         /*
119          * Case 1.2 dwType is set
120          */
121         dwType = -1;
122         ok(! SHQueryValueExA( hKey, "Test1", NULL, &dwType, NULL, NULL), "SHQueryValueExA failed");
123         ok( dwType == REG_SZ, "(%lu)", dwType);
124
125         /*
126          * dwSize is set
127          * dwExpanded < dwUnExpanded
128          */
129         dwSize = 6;
130         ok(! SHQueryValueExA( hKey, "Test1", NULL, NULL, NULL, &dwSize), "SHQueryValueExA failed");
131         ok( dwSize == nUsedBuffer1, "(%lu,%u)", dwSize, nUsedBuffer1);
132
133         /*
134          * dwExpanded > dwUnExpanded
135          */
136         dwSize = 6;
137         ok(! SHQueryValueExA( hKey, "Test3", NULL, NULL, NULL, &dwSize), "SHQueryValueExA failed");
138         ok( dwSize == nUsedBuffer2, "(%lu,%u)", dwSize, nUsedBuffer2);
139
140
141         /*
142          * Case 1 string shrinks during expanding
143          */
144         strcpy(buf, sEmptyBuffer);
145         dwSize = 6;
146         dwType = -1;
147         dwRet = SHQueryValueExA( hKey, "Test1", NULL, &dwType, buf, &dwSize);
148         ok( dwRet == ERROR_MORE_DATA, "(%lu)", dwRet);
149         ok( 0 == strcmp(sEmptyBuffer, buf), "(%s)", buf);
150         ok( dwType == REG_SZ, "(%lu)" , dwType);
151         ok( dwSize == nUsedBuffer1, "(%lu,%u)" , dwSize, nUsedBuffer1);
152
153         /*
154          * string grows during expanding
155          */
156         strcpy(buf, sEmptyBuffer);
157         dwSize = 6;
158         dwType = -1;
159         dwRet = SHQueryValueExA( hKey, "Test3", NULL, &dwType, buf, &dwSize);
160         ok( ERROR_MORE_DATA == dwRet, "ERROR_MORE_DATA");
161         ok( 0 == strcmp(sEmptyBuffer, buf), "(%s)", buf);
162         ok( dwSize == nUsedBuffer2, "(%lu,%u)" , dwSize, nUsedBuffer2);
163         ok( dwType == REG_SZ, "(%lu)" , dwType);
164
165         /*
166          * if the unexpanded string fits into the buffer it can get cut when expanded
167          */
168         strcpy(buf, sEmptyBuffer);
169         dwSize = sExpLen2 - 4;
170         dwType = -1;
171         ok( ERROR_MORE_DATA == SHQueryValueExA( hKey, "Test3", NULL, &dwType, buf, &dwSize), "Expected ERROR_MORE_DATA");
172         ok( 0 == strncmp(sExpTestpath2, buf, sExpLen2 - 4 - 1), "(%s)", buf);
173         ok( sExpLen2 - 4 - 1 == strlen(buf), "(%s)", buf);
174         ok( dwSize == nUsedBuffer2, "(%lu,%u)" , dwSize, nUsedBuffer2);
175         ok( dwType == REG_SZ, "(%lu)" , dwType);
176
177         /*
178          * The buffer is NULL but the size is set
179          */
180         strcpy(buf, sEmptyBuffer);
181         dwSize = 6;
182         dwType = -1;
183         dwRet = SHQueryValueExA( hKey, "Test3", NULL, &dwType, NULL, &dwSize);
184         ok( ERROR_SUCCESS == dwRet, "(%lu)", dwRet);
185         ok( dwSize == nUsedBuffer2, "(%lu,%u)" , dwSize, nUsedBuffer2);
186         ok( dwType == REG_SZ, "(%lu)" , dwType);
187
188
189         RegCloseKey(hKey);
190 }
191
192 START_TEST(shreg)
193 {
194         create_test_entrys();
195         test_SHGetValue();
196         test_SHQUeryValueEx();
197         test_SHGetTegPath();
198 }