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 
17 static
18 INT
19 ReadRegistryValue(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 
58 static
59 INT
60 WriteRegistryValue(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 
87 static
88 INT
89 DeleteRegistryValue(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 
111 static
112 VOID
113 test_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     if(OrigDhcpDomainNameExists)
191         ok(strcmp(FixedInfo->DomainName, OrigDhcpDomainName) == 0, "FixedInfo->DomainName is wrong '%s' != '%s'\n", FixedInfo->DomainName, OrigDhcpDomainName);
192     else
193         ok(strcmp(FixedInfo->DomainName, OrigDomainName) == 0, "FixedInfo->DomainName is wrong '%s' != '%s'\n", FixedInfo->DomainName, OrigDomainName);
194     if (!OrigDhcpHostnameExists)
195     {
196         ErrorCode = WriteRegistryValue("DhcpHostname", ROSTESTDHCPHOST);
197         ok(ErrorCode == NO_ERROR, "Failed to write registry key DhcpHostname %d\n", ErrorCode);
198     }
199     else
200     {
201         ErrorCode = DeleteRegistryValue("DhcpHostname");
202         ok(ErrorCode == NO_ERROR, "Failed to remove registry key DhcpHostname %d\n", ErrorCode);
203     }
204     if (!OrigDhcpDomainNameExists)
205     {
206         ErrorCode = WriteRegistryValue("DhcpDomain", ROSTESTDHCPDOMAIN);
207         ok(ErrorCode == NO_ERROR, "Failed to write registry key DhcpDomainName %d\n", ErrorCode);
208     }
209     else
210     {
211         ErrorCode = DeleteRegistryValue("DhcpDomain");
212         ok(ErrorCode == NO_ERROR, "Failed to remove registry key DhcpDomainName %d\n", ErrorCode);
213     }
214 
215     HeapFree(GetProcessHeap(), 0, FixedInfo);
216     len = 0;
217     ApiReturn = GetNetworkParams(NULL, &len);
218     ok(ApiReturn == ERROR_BUFFER_OVERFLOW,
219         "GetNetworkParams returned %ld, expected ERROR_BUFFER_OVERFLOW\n",
220         ApiReturn);
221     if (ApiReturn != ERROR_BUFFER_OVERFLOW)
222         skip("Can't determine size of FIXED_INFO. Can't proceed\n");
223     FixedInfo = HeapAlloc(GetProcessHeap(), 0, len);
224     if (FixedInfo == NULL)
225         skip("FixedInfo is NULL. Can't proceed\n");
226     ApiReturn = GetNetworkParams(FixedInfo, &len);
227     ok(ApiReturn == NO_ERROR,
228         "GetNetworkParams(buf, &dwSize) returned %ld, expected NO_ERROR\n",
229         ApiReturn);
230     if (ApiReturn != NO_ERROR)
231     {
232         HeapFree(GetProcessHeap(), 0, FixedInfo);
233         skip("GetNetworkParams failed. Can't proceed\n");
234     }
235 
236     /* restore registry values */
237     if (OrigDhcpHostnameExists)
238         ErrorCode = WriteRegistryValue("DhcpHostname", OrigDhcpHostname);
239     else
240         ErrorCode = DeleteRegistryValue("DhcpHostname");
241     ok(ErrorCode == NO_ERROR, "Failed to restore registry key DhcpHostname %d\n", ErrorCode);
242     if (OrigDhcpDomainNameExists)
243         ErrorCode = WriteRegistryValue("DhcpDomain", OrigDhcpDomainName);
244     else
245         ErrorCode = DeleteRegistryValue("DhcpDomain");
246     ok(ErrorCode == NO_ERROR, "Failed to restore registry key DhcpDomainName %d\n", ErrorCode);
247 
248     ok(ApiReturn == NO_ERROR,
249         "GetNetworkParams(buf, &dwSize) returned %ld, expected NO_ERROR\n",
250         ApiReturn);
251     ok(FixedInfo->HostName != NULL, "FixedInfo->HostName is NULL\n");
252     if (FixedInfo->HostName == NULL)
253         skip("FixedInfo->HostName is NULL. Can't proceed\n");
254     if (!OrigDhcpHostnameExists)
255     {
256         /* Windows doesn't honor DHCP option 12 even if RFC requires it if it is returned by DHCP server! */
257         //ok(strcmp(FixedInfo->HostName, ROSTESTDHCPHOST) == 0, "FixedInfo->HostName is wrong '%s' != '%s'\n", FixedInfo->HostName, ROSTESTDHCPHOST);
258     }
259     else
260         ok(strcmp(FixedInfo->HostName, OrigHostname) == 0, "FixedInfo->HostName is wrong '%s' != '%s'\n", FixedInfo->HostName, OrigHostname);
261     ok(FixedInfo->DomainName != NULL, "FixedInfo->DomainName is NULL\n");
262     if (FixedInfo->DomainName == NULL)
263         skip("FixedInfo->DomainName is NULL. Can't proceed\n");
264     if (!OrigDhcpDomainNameExists)
265         ok(strcmp(FixedInfo->DomainName, ROSTESTDHCPDOMAIN) == 0, "FixedInfo->DomainName is wrong '%s' != '%s'\n", FixedInfo->DomainName, ROSTESTDHCPDOMAIN);
266     else
267         ok(strcmp(FixedInfo->DomainName, OrigDomainName) == 0, "FixedInfo->DomainName is wrong '%s' != '%s'\n", FixedInfo->DomainName, OrigDomainName);
268 
269     HeapFree(GetProcessHeap(), 0, FixedInfo);
270 }
271 
272 START_TEST(GetNetworkParams)
273 {
274     test_GetNetworkParams();
275 }
276