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