Reactos
1/*
2 * PROJECT: ReactOS API Tests
3 * LICENSE: LGPLv2.1+ - See COPYING.LIB in the top level directory
4 * PURPOSE: Tests for GetNetworkParams function
5 * PROGRAMMERS: Peter Hater
6 */
7
8#include <apitest.h>
9
10#define WIN32_NO_STATUS
11#include <iphlpapi.h>
12#include <winreg.h>
13
14#define ROSTESTDHCPHOST "testdhcproshost"
15#define ROSTESTDHCPDOMAIN "testrosdomain"
16
17static
18INT
19ReadRegistryValue(PCHAR ValueName, PCHAR Value)
20{
21 INT ErrorCode;
22 HKEY ParametersKey;
23 DWORD RegType;
24 DWORD RegSize = 0;
25
26 /* Open the database path key */
27 ErrorCode = RegOpenKeyExA(HKEY_LOCAL_MACHINE,
28 "System\\CurrentControlSet\\Services\\Tcpip\\Parameters",
29 0,
30 KEY_READ,
31 &ParametersKey);
32 if (ErrorCode == NO_ERROR)
33 {
34 /* Read the actual path */
35 ErrorCode = RegQueryValueExA(ParametersKey,
36 ValueName,
37 NULL,
38 &RegType,
39 NULL,
40 &RegSize);
41 if (RegSize)
42 {
43 /* Read the actual path */
44 ErrorCode = RegQueryValueExA(ParametersKey,
45 ValueName,
46 NULL,
47 &RegType,
48 (LPBYTE)Value,
49 &RegSize);
50 }
51
52 /* Close the key */
53 RegCloseKey(ParametersKey);
54 }
55 return ErrorCode;
56}
57
58static
59INT
60WriteRegistryValue(PCHAR ValueName, PCHAR Value)
61{
62 INT ErrorCode;
63 HKEY ParametersKey;
64
65 /* Open the database path key */
66 ErrorCode = RegOpenKeyExA(HKEY_LOCAL_MACHINE,
67 "System\\CurrentControlSet\\Services\\Tcpip\\Parameters",
68 0,
69 KEY_WRITE,
70 &ParametersKey);
71 if (ErrorCode == NO_ERROR)
72 {
73 /* Read the actual path */
74 ErrorCode = RegSetValueExA(ParametersKey,
75 ValueName,
76 0,
77 REG_SZ,
78 (LPBYTE)Value,
79 lstrlenA(Value) + 1);
80
81 /* Close the key */
82 RegCloseKey(ParametersKey);
83 }
84 return ErrorCode;
85}
86
87static
88INT
89DeleteRegistryValue(PCHAR ValueName)
90{
91 INT ErrorCode;
92 HKEY ParametersKey;
93
94 /* Open the database path key */
95 ErrorCode = RegOpenKeyExA(HKEY_LOCAL_MACHINE,
96 "System\\CurrentControlSet\\Services\\Tcpip\\Parameters",
97 0,
98 KEY_WRITE,
99 &ParametersKey);
100 if (ErrorCode == NO_ERROR)
101 {
102 /* Read the actual path */
103 ErrorCode = RegDeleteValueA(ParametersKey, ValueName);
104
105 /* Close the key */
106 RegCloseKey(ParametersKey);
107 }
108 return ErrorCode;
109}
110
111static
112VOID
113test_GetNetworkParams(VOID)
114{
115 DWORD len = 0;
116 INT ErrorCode;
117 CHAR OrigHostname[128];
118 CHAR OrigDomainName[128];
119 CHAR OrigDhcpHostname[128];
120 CHAR OrigDhcpDomainName[128];
121 BOOL OrigHostnameExists;
122 BOOL OrigDomainNameExists;
123 BOOL OrigDhcpHostnameExists;
124 BOOL OrigDhcpDomainNameExists;
125 PFIXED_INFO FixedInfo;
126 DWORD ApiReturn;
127
128 memset(OrigHostname, 0, sizeof(OrigHostname));
129 memset(OrigDomainName, 0, sizeof(OrigDomainName));
130 memset(OrigDhcpHostname, 0, sizeof(OrigDhcpHostname));
131 memset(OrigDhcpDomainName, 0, sizeof(OrigDhcpDomainName));
132 /* read current registry values */
133 ErrorCode = ReadRegistryValue("Hostname", OrigHostname);
134 ok(ErrorCode == ERROR_SUCCESS, "Failed to read registry key Hostname %d\n", ErrorCode);
135 OrigHostnameExists = ErrorCode == NO_ERROR;
136 ErrorCode = ReadRegistryValue("Domain", OrigDomainName);
137 ok(ErrorCode == ERROR_SUCCESS || ErrorCode == ERROR_FILE_NOT_FOUND, "Failed to read registry key DomainName %d\n", ErrorCode);
138 OrigDomainNameExists = ErrorCode == NO_ERROR;
139 ErrorCode = ReadRegistryValue("DhcpHostname", OrigDhcpHostname);
140 ok(ErrorCode == ERROR_SUCCESS || ErrorCode == ERROR_FILE_NOT_FOUND, "Failed to read registry key DhcpHostname %d\n", ErrorCode);
141 OrigDhcpHostnameExists = ErrorCode == NO_ERROR;
142 ErrorCode = ReadRegistryValue("DhcpDomain", OrigDhcpDomainName);
143 ok(ErrorCode == ERROR_SUCCESS || ErrorCode == ERROR_FILE_NOT_FOUND, "Failed to read registry key DhcpDomainName %d\n", ErrorCode);
144 OrigDhcpDomainNameExists = ErrorCode == NO_ERROR;
145
146 trace("Starting values:\n");
147 trace("Hostname: %s, exists %s\n", OrigHostname, OrigHostnameExists ? "yes" : "no");
148 trace("Domain: %s, exists %s\n", OrigDomainName, OrigDomainNameExists ? "yes" : "no");
149 trace("DhcpHostname: %s, exists %s\n", OrigDhcpHostnameExists ? OrigDhcpHostname : "", OrigDhcpHostnameExists ? "yes" : "no");
150 trace("DhcpDomain: %s, exists %s\n", OrigDhcpDomainNameExists ? OrigDhcpDomainName : "", OrigDhcpDomainNameExists ? "yes" : "no");
151
152 ApiReturn = GetNetworkParams(NULL, &len);
153 ok(ApiReturn == ERROR_BUFFER_OVERFLOW,
154 "GetNetworkParams returned %ld, expected ERROR_BUFFER_OVERFLOW\n",
155 ApiReturn);
156 if (ApiReturn != ERROR_BUFFER_OVERFLOW)
157 skip("Can't determine size of FIXED_INFO. Can't proceed\n");
158 FixedInfo = HeapAlloc(GetProcessHeap(), 0, len);
159 if (FixedInfo == NULL)
160 skip("FixedInfo is NULL. Can't proceed\n");
161
162 ApiReturn = GetNetworkParams(FixedInfo, &len);
163 ok(ApiReturn == NO_ERROR,
164 "GetNetworkParams(buf, &dwSize) returned %ld, expected NO_ERROR\n",
165 ApiReturn);
166 if (ApiReturn != NO_ERROR)
167 {
168 HeapFree(GetProcessHeap(), 0, FixedInfo);
169 skip("GetNetworkParams failed. Can't proceed\n");
170 }
171 ok(FixedInfo->HostName != NULL, "FixedInfo->HostName is NULL\n");
172 if (FixedInfo->HostName == NULL)
173 {
174 HeapFree(GetProcessHeap(), 0, FixedInfo);
175 skip("FixedInfo->HostName is NULL. Can't proceed\n");
176 }
177 if (OrigDhcpHostnameExists)
178 {
179 /* Windows doesn't honor DHCP option 12 even if RFC requires it if it is returned by DHCP server! */
180 //ok(strcmp(FixedInfo->HostName, OrigDhcpHostname) == 0, "FixedInfo->HostName is wrong '%s' != '%s'\n", FixedInfo->HostName, OrigDhcpHostname);
181 }
182 else
183 ok(strcmp(FixedInfo->HostName, OrigHostname) == 0, "FixedInfo->HostName is wrong '%s' != '%s'\n", FixedInfo->HostName, OrigHostname);
184 ok(FixedInfo->DomainName != NULL, "FixedInfo->DomainName is NULL\n");
185 if (FixedInfo->DomainName == NULL)
186 {
187 HeapFree(GetProcessHeap(), 0, FixedInfo);
188 skip("FixedInfo->DomainName is NULL. Can't proceed\n");
189 }
190 ok(strcmp(FixedInfo->DomainName, OrigDomainName) == 0, "FixedInfo->DomainName is wrong '%s' != '%s'\n", FixedInfo->DomainName, OrigDomainName);
191 if (!OrigDhcpHostnameExists)
192 {
193 ErrorCode = WriteRegistryValue("DhcpHostname", ROSTESTDHCPHOST);
194 ok(ErrorCode == NO_ERROR, "Failed to write registry key DhcpHostname %d\n", ErrorCode);
195 }
196 else
197 {
198 ErrorCode = DeleteRegistryValue("DhcpHostname");
199 ok(ErrorCode == NO_ERROR, "Failed to remove registry key DhcpHostname %d\n", ErrorCode);
200 }
201 if (!OrigDhcpDomainNameExists)
202 {
203 ErrorCode = WriteRegistryValue("DhcpDomain", ROSTESTDHCPDOMAIN);
204 ok(ErrorCode == NO_ERROR, "Failed to write registry key DhcpDomainName %d\n", ErrorCode);
205 }
206 else
207 {
208 ErrorCode = DeleteRegistryValue("DhcpDomain");
209 ok(ErrorCode == NO_ERROR, "Failed to remove registry key DhcpDomainName %d\n", ErrorCode);
210 }
211
212 HeapFree(GetProcessHeap(), 0, FixedInfo);
213 len = 0;
214 ApiReturn = GetNetworkParams(NULL, &len);
215 ok(ApiReturn == ERROR_BUFFER_OVERFLOW,
216 "GetNetworkParams returned %ld, expected ERROR_BUFFER_OVERFLOW\n",
217 ApiReturn);
218 if (ApiReturn != ERROR_BUFFER_OVERFLOW)
219 skip("Can't determine size of FIXED_INFO. Can't proceed\n");
220 FixedInfo = HeapAlloc(GetProcessHeap(), 0, len);
221 if (FixedInfo == NULL)
222 skip("FixedInfo is NULL. Can't proceed\n");
223 ApiReturn = GetNetworkParams(FixedInfo, &len);
224 ok(ApiReturn == NO_ERROR,
225 "GetNetworkParams(buf, &dwSize) returned %ld, expected NO_ERROR\n",
226 ApiReturn);
227 if (ApiReturn != NO_ERROR)
228 {
229 HeapFree(GetProcessHeap(), 0, FixedInfo);
230 skip("GetNetworkParams failed. Can't proceed\n");
231 }
232
233 /* restore registry values */
234 if (OrigDhcpHostnameExists)
235 ErrorCode = WriteRegistryValue("DhcpHostname", OrigDhcpHostname);
236 else
237 ErrorCode = DeleteRegistryValue("DhcpHostname");
238 ok(ErrorCode == NO_ERROR, "Failed to restore registry key DhcpHostname %d\n", ErrorCode);
239 if (OrigDhcpDomainNameExists)
240 ErrorCode = WriteRegistryValue("DhcpDomain", OrigDhcpDomainName);
241 else
242 ErrorCode = DeleteRegistryValue("DhcpDomain");
243 ok(ErrorCode == NO_ERROR, "Failed to restore registry key DhcpDomainName %d\n", ErrorCode);
244
245 ok(ApiReturn == NO_ERROR,
246 "GetNetworkParams(buf, &dwSize) returned %ld, expected NO_ERROR\n",
247 ApiReturn);
248 ok(FixedInfo->HostName != NULL, "FixedInfo->HostName is NULL\n");
249 if (FixedInfo->HostName == NULL)
250 skip("FixedInfo->HostName is NULL. Can't proceed\n");
251 if (!OrigDhcpHostnameExists)
252 {
253 /* Windows doesn't honor DHCP option 12 even if RFC requires it if it is returned by DHCP server! */
254 //ok(strcmp(FixedInfo->HostName, ROSTESTDHCPHOST) == 0, "FixedInfo->HostName is wrong '%s' != '%s'\n", FixedInfo->HostName, ROSTESTDHCPHOST);
255 }
256 else
257 ok(strcmp(FixedInfo->HostName, OrigHostname) == 0, "FixedInfo->HostName is wrong '%s' != '%s'\n", FixedInfo->HostName, OrigHostname);
258 ok(FixedInfo->DomainName != NULL, "FixedInfo->DomainName is NULL\n");
259 if (FixedInfo->DomainName == NULL)
260 skip("FixedInfo->DomainName is NULL. Can't proceed\n");
261 if (!OrigDhcpDomainNameExists)
262 ok(strcmp(FixedInfo->DomainName, ROSTESTDHCPDOMAIN) == 0, "FixedInfo->DomainName is wrong '%s' != '%s'\n", FixedInfo->DomainName, ROSTESTDHCPDOMAIN);
263 else
264 ok(strcmp(FixedInfo->DomainName, OrigDomainName) == 0, "FixedInfo->DomainName is wrong '%s' != '%s'\n", FixedInfo->DomainName, OrigDomainName);
265
266 HeapFree(GetProcessHeap(), 0, FixedInfo);
267}
268
269START_TEST(GetNetworkParams)
270{
271 test_GetNetworkParams();
272}