ReactOS 0.4.16-dev-336-gb667d82
GetInterfaceName.c
Go to the documentation of this file.
1/*
2 * PROJECT: ReactOS API Tests
3 * LICENSE: GPL-2.0+ (https://spdx.org/licenses/GPL-2.0+)
4 * PURPOSE: Tests for network interface name resolving functions
5 * COPYRIGHT: Copyright 2017 Stanislav Motylkov
6 */
7
8#include <apitest.h>
9
10#define WIN32_NO_STATUS
11#include <iphlpapi.h>
12#include <ndk/rtlfuncs.h>
13#include <initguid.h>
14
15DEFINE_GUID(MY_TEST_GUID, 0x8D1AB70F, 0xADF0, 0x49FC, 0x9D, 0x07, 0x4E, 0x89, 0x29, 0x2D, 0xC5, 0x2B);
16static DWORD (WINAPI * pNhGetInterfaceNameFromGuid)(PVOID, PVOID, PULONG, DWORD, DWORD);
17static DWORD (WINAPI * pNhGetInterfaceNameFromDeviceGuid)(PVOID, PVOID, PULONG, DWORD, DWORD);
18
19/*
20 * Tests for NhGetInterfaceNameFromGuid
21 */
22static
23VOID
25{
26 DWORD ApiReturn, Error;
27 ULONG ulOutBufLen;
29 GUID UniqueGUID = MY_TEST_GUID;
31
32 // Test NULL GUID
33 SetLastError(0xbeeffeed);
34 Error = 0xbeeffeed;
35 ZeroMemory(&Name, sizeof(Name));
36 ApiReturn = ERROR_SUCCESS;
37 ulOutBufLen = sizeof(Name);
38 StartSeh()
39 ApiReturn = pNhGetInterfaceNameFromGuid(NULL, &Name, &ulOutBufLen, par1, par2);
42
43 ok_long(ApiReturn, ((GetVersion() & 0xFF) >= 6) ? ERROR_INVALID_PARAMETER : ERROR_SUCCESS);
44 ok(Error == 0xbeeffeed,
45 "GetLastError() returned %ld, expected 0xbeeffeed\n",
46 Error);
47 ok(ulOutBufLen == sizeof(Name),
48 "ulOutBufLen is %ld, expected = sizeof(Name)\n",
49 ulOutBufLen);
50 ok_wstr(L"", Name);
51
52 // Test correct GUID, but NULL name
53 SetLastError(0xbeeffeed);
54 ZeroMemory(&Name, sizeof(Name));
55 ApiReturn = pNhGetInterfaceNameFromGuid(&AdapterGUID, NULL, &ulOutBufLen, par1, par2);
57
58 ok_long(ApiReturn, ((GetVersion() & 0xFF) >= 6) ? ERROR_INVALID_PARAMETER : ERROR_SUCCESS);
59 ok_long(Error, ((GetVersion() & 0xFF) >= 6) ? ERROR_SUCCESS : 0xbeeffeed);
60 ok(ulOutBufLen > 0,
61 "ulOutBufLen is %ld, expected > 0\n",
62 ulOutBufLen);
63 ok_wstr(L"", Name);
64
65 // NhGetInterfaceNameFromGuid will throw exception if pOutBufLen is NULL
66 SetLastError(0xbeeffeed);
67 Error = 0xbeeffeed;
68 ZeroMemory(&Name, sizeof(Name));
69 ApiReturn = 0xdeadbeef;
70 StartSeh()
71 ApiReturn = pNhGetInterfaceNameFromGuid(&AdapterGUID, &Name, NULL, par1, par2);
74
75 ok(ApiReturn == 0xdeadbeef,
76 "ApiReturn returned %ld, expected 0xdeadbeef\n",
77 ApiReturn);
78 ok(Error == 0xbeeffeed,
79 "GetLastError() returned %ld, expected 0xbeeffeed\n",
80 Error);
81 ok(ulOutBufLen > 0,
82 "ulOutBufLen is %ld, expected > 0\n",
83 ulOutBufLen);
84 ok_wstr(L"", Name);
85
86 // Test correct values
87 SetLastError(0xbeeffeed);
88 ZeroMemory(&Name, sizeof(Name));
89 ulOutBufLen = sizeof(Name);
90 ApiReturn = pNhGetInterfaceNameFromGuid(&AdapterGUID, &Name, &ulOutBufLen, par1, par2);
92
93 ok(ApiReturn == ERROR_SUCCESS,
94 "ApiReturn returned %ld, expected ERROR_SUCCESS\n",
95 ApiReturn);
96 ok_long(Error, ((GetVersion() & 0xFF) >= 6) ? 0 : 0xbeeffeed);
97 ok(ulOutBufLen > 0,
98 "ulOutBufLen is %ld, expected > 0\n",
99 ulOutBufLen);
100 Length = wcslen(Name);
101 ok(Length > 0,
102 "wcslen(Name) is %ld, expected > 0\n",
103 Length);
104 if (Length > 0)
105 trace("Adapter name: \"%S\"\n", Name);
106
107 // Test correct values, but with new unique GUID
108 SetLastError(0xbeeffeed);
109 ZeroMemory(&Name, sizeof(Name));
110 ulOutBufLen = sizeof(Name);
111 ApiReturn = pNhGetInterfaceNameFromGuid((PVOID)&UniqueGUID, &Name, &ulOutBufLen, par1, par2);
113
114 ok_long(ApiReturn, ((GetVersion() & 0xFF) >= 6) ? ERROR_INVALID_PARAMETER : ERROR_NOT_FOUND);
115#ifdef _M_AMD64
117#else
118 ok_long(Error, 0);
119#endif
120 ok(ulOutBufLen == sizeof(Name),
121 "ulOutBufLen is %ld, expected = sizeof(Name)\n",
122 ulOutBufLen);
123 ok_wstr(L"", Name);
124
125 // Test correct values, but with small length
126 SetLastError(0xbeeffeed);
127 ZeroMemory(&Name, sizeof(Name));
128 ulOutBufLen = 0;
129 ApiReturn = pNhGetInterfaceNameFromGuid(&AdapterGUID, &Name, &ulOutBufLen, par1, par2);
131
133 ok_long(Error, ((GetVersion() & 0xFF) >= 6) ? 0 : 0xbeeffeed);
134 ok_long(ulOutBufLen, MAX_INTERFACE_NAME_LEN * 2 + ((GetVersion() & 0xFF) >= 6 ? 2 : 0));
135 ok_wstr(L"", Name);
136}
137
138/*
139 * Tests for NhGetInterfaceNameFromDeviceGuid
140 */
141static
142VOID
144{
145 DWORD ApiReturn, Error;
146 ULONG ulOutBufLen;
148 GUID UniqueGUID = MY_TEST_GUID;
150
151 // Test NULL GUID
152 // Windows XP: NhGetInterfaceNameFromDeviceGuid throws exception here
153 SetLastError(0xbeeffeed);
154 Error = 0xbeeffeed;
155 ZeroMemory(&Name, sizeof(Name));
156 ApiReturn = ERROR_SUCCESS;
157 ulOutBufLen = sizeof(Name);
158 StartSeh()
159 ApiReturn = pNhGetInterfaceNameFromDeviceGuid(NULL, &Name, &ulOutBufLen, par1, par2);
162
163 ok_long(ApiReturn, ((GetVersion() & 0xFF) >= 6) ? ERROR_INVALID_PARAMETER : 0);
164 ok(Error == 0xbeeffeed,
165 "GetLastError() returned %ld, expected ERROR_SUCCESS\n",
166 Error);
167 ok(ulOutBufLen == sizeof(Name),
168 "ulOutBufLen is %ld, expected = sizeof(Name)\n",
169 ulOutBufLen);
170 ok_wstr(L"", Name);
171
172 // Test correct GUID, but NULL name
173 SetLastError(0xbeeffeed);
174 Error = 0xbeeffeed;
175 ZeroMemory(&Name, sizeof(Name));
176 ApiReturn = ERROR_SUCCESS;
177 StartSeh()
178 ApiReturn = pNhGetInterfaceNameFromDeviceGuid(&AdapterGUID, NULL, &ulOutBufLen, par1, par2);
181
182 ok_long(ApiReturn, ((GetVersion() & 0xFF) >= 6) ? ERROR_INVALID_PARAMETER : 0);
183 ok_long(Error, ((GetVersion() & 0xFF) >= 6) ? ERROR_SUCCESS : 0xbeeffeed);
184 ok(ulOutBufLen > 0,
185 "ulOutBufLen is %ld, expected > 0\n",
186 ulOutBufLen);
187 ok_wstr(L"", Name);
188
189 // NhGetInterfaceNameFromDeviceGuid will throw exception if pOutBufLen is NULL
190 SetLastError(0xbeeffeed);
191 Error = 0xbeeffeed;
192 ZeroMemory(&Name, sizeof(Name));
193 ApiReturn = ERROR_SUCCESS;
194 StartSeh()
195 ApiReturn = pNhGetInterfaceNameFromDeviceGuid(&AdapterGUID, &Name, NULL, par1, par2);
198
199 ok_long(ApiReturn, 0);
200 ok_long(Error, 0xbeeffeed);
201 ok(ulOutBufLen > 0,
202 "ulOutBufLen is %ld, expected > 0\n",
203 ulOutBufLen);
204 ok_wstr(L"", Name);
205
206 // Test correct values
207 SetLastError(0xbeeffeed);
208 ZeroMemory(&Name, sizeof(Name));
209 ulOutBufLen = sizeof(Name);
210 ApiReturn = pNhGetInterfaceNameFromDeviceGuid(&AdapterGUID, &Name, &ulOutBufLen, par1, par2);
212
213 ok(ApiReturn == ERROR_SUCCESS,
214 "ApiReturn returned %ld, expected ERROR_SUCCESS\n",
215 ApiReturn);
217 "GetLastError() returned %ld, expected ERROR_SUCCESS\n",
218 Error);
219 ok(ulOutBufLen > 0,
220 "ulOutBufLen is %ld, expected > 0\n",
221 ulOutBufLen);
222 Length = wcslen(Name);
223 ok(Length > 0,
224 "wcslen(Name) is %ld, expected > 0\n",
225 Length);
226 if (Length > 0)
227 trace("Adapter name: \"%S\"\n", Name);
228
229 // Test correct values, but with new unique GUID
230 SetLastError(0xbeeffeed);
231 ZeroMemory(&Name, sizeof(Name));
232 ulOutBufLen = sizeof(Name);
233 ApiReturn = pNhGetInterfaceNameFromDeviceGuid((PVOID)&UniqueGUID, &Name, &ulOutBufLen, par1, par2);
235
236 ok_long(ApiReturn, ((GetVersion() & 0xFF) >= 6) ? ERROR_INVALID_PARAMETER : ERROR_NOT_FOUND);
238 "GetLastError() returned %ld, expected ERROR_SUCCESS\n",
239 Error);
240 ok(ulOutBufLen == sizeof(Name),
241 "ulOutBufLen is %ld, expected = sizeof(Name)\n",
242 ulOutBufLen);
243 ok_wstr(L"", Name);
244
245 // Test correct values, but with small length
246 SetLastError(0xbeeffeed);
247 ZeroMemory(&Name, sizeof(Name));
248 ulOutBufLen = 0;
249 ApiReturn = pNhGetInterfaceNameFromDeviceGuid(&AdapterGUID, &Name, &ulOutBufLen, par1, par2);
251
254 "GetLastError() returned %ld, expected ERROR_SUCCESS\n",
255 Error);
256 ok_long(ulOutBufLen, MAX_INTERFACE_NAME_LEN * 2 + (((GetVersion() & 0xFF) >= 6) ? 2 : 0));
257 ok_wstr(L"", Name);
258}
259
260static
261VOID
263{
264 PIP_INTERFACE_INFO pInfo = NULL;
265 ULONG ulOutBufLen = 0;
266 DWORD ApiReturn;
269 GUID AdapterGUID;
270 HINSTANCE hIpHlpApi;
271
272 ApiReturn = GetInterfaceInfo(pInfo, &ulOutBufLen);
273 ok(ApiReturn == ERROR_INSUFFICIENT_BUFFER,
274 "GetInterfaceInfo(pInfo, &ulOutBufLen) returned %ld, expected ERROR_INSUFFICIENT_BUFFER\n",
275 ApiReturn);
276 if (ApiReturn != ERROR_INSUFFICIENT_BUFFER)
277 {
278 skip("Can't determine size of IP_INTERFACE_INFO. Can't proceed\n");
279 return;
280 }
281
283 if (pInfo == NULL)
284 {
285 skip("pInfo is NULL. Can't proceed\n");
286 return;
287 }
288
289 ApiReturn = GetInterfaceInfo(pInfo, &ulOutBufLen);
290 ok(ApiReturn == NO_ERROR,
291 "GetInterfaceInfo(pInfo, &ulOutBufLen) returned %ld, expected NO_ERROR\n",
292 ApiReturn);
293 if (ApiReturn != NO_ERROR || ulOutBufLen == 0)
294 {
295 skip("GetInterfaceInfo failed with error %ld. Can't proceed\n", ApiReturn);
296 return;
297 }
298
299 if (pInfo->NumAdapters > 0)
300 CopyMemory(&Name, &pInfo->Adapter[0].Name, sizeof(Name));
301
302 if (pInfo->NumAdapters == 0)
303 {
304 HeapFree(GetProcessHeap(), 0, pInfo);
305 skip("pInfo->NumAdapters = 0. Can't proceed\n");
306 return;
307 }
308 trace("pInfo->NumAdapters: %lu\n", pInfo->NumAdapters);
309
310 HeapFree(GetProcessHeap(), 0, pInfo);
311
312 ApiReturn = wcsncmp(Name, L"\\DEVICE\\TCPIP_", 14);
313 ok(ApiReturn == 0,
314 "wcsncmp(Name, L\"\\DEVICE\\TCPIP_\", 14) returned %ld, expected 0\n",
315 ApiReturn);
316 if (ApiReturn != 0)
317 {
318 if (wcslen(Name) == 0)
319 {
320 skip("pInfo->Adapter[0].Name is empty. Can't proceed\n");
321 return;
322 }
323 else
324 {
325 // workaround for ReactOS
326 trace("pInfo->Adapter[0].Name = \"%ls\" is incorrect.\n", Name);
328 }
329 }
330 else
331 {
333 }
334
335 ApiReturn = RtlGUIDFromString(&GuidString, &AdapterGUID);
336 if (ApiReturn != 0)
337 {
338 skip("RtlGUIDFromString failed. Can't proceed\n");
339 return;
340 }
341
342 hIpHlpApi = GetModuleHandleW(L"iphlpapi.dll");
343 if (!hIpHlpApi)
344 {
345 skip("Failed to load iphlpapi.dll. Can't proceed\n");
346 return;
347 }
348
349 pNhGetInterfaceNameFromGuid = (void *)GetProcAddress(hIpHlpApi, "NhGetInterfaceNameFromGuid");
350
351 if (!pNhGetInterfaceNameFromGuid)
352 skip("NhGetInterfaceNameFromGuid not found. Can't proceed\n");
353 else
354 test_NhGetInterfaceNameFromGuid(AdapterGUID, 0, 0);
355
356 pNhGetInterfaceNameFromDeviceGuid = (void *)GetProcAddress(hIpHlpApi, "NhGetInterfaceNameFromDeviceGuid");
357
358 if (!pNhGetInterfaceNameFromDeviceGuid)
359 skip("NhGetInterfaceNameFromDeviceGuid not found. Can't proceed\n");
360 else
361 test_NhGetInterfaceNameFromDeviceGuid(AdapterGUID, 1, 0);
362}
363
365{
367}
bool GetInterfaceName(const WCHAR *InterfaceString, WCHAR *buf, size_t size)
Definition: CFontExt.cpp:46
static VOID test_NhGetInterfaceNameFromDeviceGuid(GUID AdapterGUID, DWORD par1, DWORD par2)
static DWORD
static PVOID
static VOID test_NhGetInterfaceNameFromGuid(GUID AdapterGUID, DWORD par1, DWORD par2)
static PULONG
static VOID test_GetInterfaceName(VOID)
#define StartSeh()
Definition: _sntprintf.h:16
#define EndSeh(ExpectedStatus)
Definition: _sntprintf.h:17
struct NameRec_ * Name
Definition: cdprocs.h:460
#define ok_long(expression, result)
Definition: atltest.h:133
#define trace
Definition: atltest.h:70
#define ok(value,...)
Definition: atltest.h:57
#define skip(...)
Definition: atltest.h:64
#define ok_wstr(x, y)
Definition: atltest.h:130
#define START_TEST(x)
Definition: atltest.h:75
BOOL Error
Definition: chkdsk.c:66
#define ERROR_NOT_ENOUGH_MEMORY
Definition: dderror.h:7
#define NO_ERROR
Definition: dderror.h:5
#define ERROR_INSUFFICIENT_BUFFER
Definition: dderror.h:10
#define ERROR_SUCCESS
Definition: deptool.c:10
#define NULL
Definition: types.h:112
#define GetProcessHeap()
Definition: compat.h:736
#define ERROR_INVALID_PARAMETER
Definition: compat.h:101
#define SetLastError(x)
Definition: compat.h:752
#define GetProcAddress(x, y)
Definition: compat.h:753
#define HeapAlloc
Definition: compat.h:733
#define HeapFree(x, y, z)
Definition: compat.h:735
#define HEAP_ZERO_MEMORY
Definition: compat.h:134
HMODULE WINAPI GetModuleHandleW(LPCWSTR lpModuleName)
Definition: loader.c:838
unsigned long DWORD
Definition: ntddk_ex.h:95
#define MAX_INTERFACE_NAME_LEN
Definition: ifmib.h:31
_CRTIMP size_t __cdecl wcslen(_In_z_ const wchar_t *_Str)
NTSYSAPI NTSTATUS WINAPI RtlGUIDFromString(PUNICODE_STRING, GUID *)
#define MAX_ADAPTER_NAME
Definition: ipexport.h:143
DWORD WINAPI GetInterfaceInfo(PIP_INTERFACE_INFO pIfTable, PULONG dwOutBufLen)
#define ERROR_FILE_NOT_FOUND
Definition: disk.h:79
static PWSTR GuidString
Definition: apphelp.c:93
NTSYSAPI VOID NTAPI RtlInitUnicodeString(PUNICODE_STRING DestinationString, PCWSTR SourceString)
_In_ ULONG _In_ ULONG _In_ ULONG Length
Definition: ntddpcm.h:102
#define STATUS_ACCESS_VIOLATION
Definition: ntstatus.h:242
#define L(x)
Definition: ntvdm.h:50
#define DEFINE_GUID(name, l, w1, w2, b1, b2, b3, b4, b5, b6, b7, b8)
Definition: guiddef.h:68
DWORD WINAPI GetVersion()
Definition: redirtest.c:5
_Check_return_ _CRTIMP int __cdecl wcsncmp(_In_reads_or_z_(_MaxCount) const wchar_t *_Str1, _In_reads_or_z_(_MaxCount) const wchar_t *_Str2, _In_ size_t _MaxCount)
#define STATUS_SUCCESS
Definition: shellext.h:65
WCHAR Name[MAX_ADAPTER_NAME]
Definition: ipexport.h:147
IP_ADAPTER_INDEX_MAP Adapter[1]
Definition: ipexport.h:152
ULONG_PTR SIZE_T
Definition: typedefs.h:80
uint32_t ULONG
Definition: typedefs.h:59
#define ZeroMemory
Definition: winbase.h:1737
DWORD WINAPI GetLastError(void)
Definition: except.c:1042
#define CopyMemory
Definition: winbase.h:1735
#define WINAPI
Definition: msvc.h:6
#define ERROR_NOT_FOUND
Definition: winerror.h:690
__wchar_t WCHAR
Definition: xmlstorage.h:180