ReactOS  0.4.14-dev-317-g96040ec
GetNetworkParams.c
Go to the documentation of this file.
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
20 {
21  INT ErrorCode;
22  HKEY ParametersKey;
23  DWORD RegType;
24  DWORD RegSize = 0;
25 
26  /* Open the database path key */
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
61 {
62  INT ErrorCode;
63  HKEY ParametersKey;
64 
65  /* Open the database path key */
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  strlen(Value) + 1);
80 
81  /* Close the key */
82  RegCloseKey(ParametersKey);
83  }
84  return ErrorCode;
85 }
86 
87 static
88 INT
90 {
91  INT ErrorCode;
92  HKEY ParametersKey;
93 
94  /* Open the database path key */
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
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  {
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 
273 {
275 }
_In_opt_ ULONG _Out_ PULONG Value
Definition: rtlfuncs.h:2343
signed char * PCHAR
Definition: retypes.h:7
LONG WINAPI RegQueryValueExA(_In_ HKEY hkeyorg, _In_ LPCSTR name, _In_ LPDWORD reserved, _Out_opt_ LPDWORD type, _Out_opt_ LPBYTE data, _Inout_opt_ LPDWORD count)
Definition: reg.c:4023
static INT DeleteRegistryValue(PCHAR ValueName)
_In_ NDIS_ERROR_CODE ErrorCode
Definition: ndis.h:4436
#define ERROR_SUCCESS
Definition: deptool.c:10
ACPI_SIZE strlen(const char *String)
Definition: utclib.c:269
#define KEY_READ
Definition: nt_native.h:1023
#define ERROR_BUFFER_OVERFLOW
Definition: winerror.h:185
char CHAR
Definition: xmlstorage.h:175
static INT ReadRegistryValue(PCHAR ValueName, PCHAR Value)
LONG WINAPI RegOpenKeyExA(_In_ HKEY hKey, _In_ LPCSTR lpSubKey, _In_ DWORD ulOptions, _In_ REGSAM samDesired, _Out_ PHKEY phkResult)
Definition: reg.c:3331
static INT WriteRegistryValue(PCHAR ValueName, PCHAR Value)
#define NO_ERROR
Definition: dderror.h:5
int32_t INT
Definition: typedefs.h:56
_In_ PUNICODE_STRING ValueName
Definition: cmfuncs.h:264
LONG WINAPI RegCloseKey(HKEY hKey)
Definition: reg.c:423
unsigned char * LPBYTE
Definition: typedefs.h:52
unsigned int BOOL
Definition: ntddk_ex.h:94
DWORD WINAPI GetNetworkParams(PFIXED_INFO pFixedInfo, PULONG pOutBufLen)
smooth NULL
Definition: ftsmooth.c:416
char DomainName[MAX_DOMAIN_NAME_LEN+4]
Definition: iptypes.h:82
#define ERROR_FILE_NOT_FOUND
Definition: disk.h:79
#define KEY_WRITE
Definition: nt_native.h:1031
#define GetProcessHeap()
Definition: compat.h:395
#define trace
Definition: atltest.h:70
PVOID WINAPI HeapAlloc(HANDLE, DWORD, SIZE_T)
unsigned long DWORD
Definition: ntddk_ex.h:95
GLenum GLsizei len
Definition: glext.h:6722
char HostName[MAX_HOSTNAME_LEN+4]
Definition: iptypes.h:81
#define ok(value,...)
Definition: atltest.h:57
LONG WINAPI RegDeleteValueA(HKEY hKey, LPCSTR lpValueName)
Definition: reg.c:2319
#define skip(...)
Definition: atltest.h:64
#define ROSTESTDHCPHOST
int strcmp(const char *String1, const char *String2)
Definition: utclib.c:469
START_TEST(GetNetworkParams)
LONG WINAPI RegSetValueExA(HKEY hKey, LPCSTR lpValueName, DWORD Reserved, DWORD dwType, CONST BYTE *lpData, DWORD cbData)
Definition: reg.c:4812
#define memset(x, y, z)
Definition: compat.h:39
#define HeapFree(x, y, z)
Definition: compat.h:394
#define ROSTESTDHCPDOMAIN
#define HKEY_LOCAL_MACHINE
Definition: winreg.h:12
static VOID test_GetNetworkParams(VOID)
#define REG_SZ
Definition: layer.c:22