1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
/*
 * PROJECT:     ReactOS API Tests
 * LICENSE:     GPL-2.0+ (https://spdx.org/licenses/GPL-2.0+)
 * PURPOSE:     Tests for network interface name resolving functions
 * COPYRIGHT:   Copyright 2017 Stanislav Motylkov
 */

#include <apitest.h>

#define WIN32_NO_STATUS
#include <iphlpapi.h>
#include <ndk/rtlfuncs.h>
#include <initguid.h>

DEFINE_GUID(MY_TEST_GUID, 0x8D1AB70F, 0xADF0, 0x49FC, 0x9D, 0x07, 0x4E, 0x89, 0x29, 0x2D, 0xC5, 0x2B);
static DWORD (WINAPI * pNhGetInterfaceNameFromGuid)(PVOID, PVOID, PULONG, DWORD, DWORD);
static DWORD (WINAPI * pNhGetInterfaceNameFromDeviceGuid)(PVOID, PVOID, PULONG, DWORD, DWORD);

/*
 * Tests for NhGetInterfaceNameFromGuid
 */
static
VOID
test_NhGetInterfaceNameFromGuid(GUID AdapterGUID, DWORD par1, DWORD par2)
{
    DWORD ApiReturn, Error;
    ULONG ulOutBufLen;
    WCHAR Name[MAX_INTERFACE_NAME_LEN + 4];
    GUID UniqueGUID = MY_TEST_GUID;
    SIZE_T Length;

    // Test NULL GUID
    SetLastError(0xbeeffeed);
    Error = 0xbeeffeed;<--- Error is assigned
    ZeroMemory(&Name, sizeof(Name));
    ApiReturn = ERROR_SUCCESS;<--- ApiReturn is assigned
    ulOutBufLen = sizeof(Name);
    StartSeh()
        ApiReturn = pNhGetInterfaceNameFromGuid(NULL, &Name, &ulOutBufLen, par1, par2);<--- ApiReturn is overwritten
        Error = GetLastError();<--- Error is overwritten
    EndSeh(STATUS_SUCCESS);

    ok(ApiReturn == ERROR_INVALID_PARAMETER,
       "ApiReturn returned %ld, expected ERROR_INVALID_PARAMETER\n",
       ApiReturn);
    ok(Error == 0xbeeffeed,
       "GetLastError() returned %ld, expected 0xbeeffeed\n",
       Error);
    ok(ulOutBufLen == sizeof(Name),
       "ulOutBufLen is %ld, expected = sizeof(Name)\n",
       ulOutBufLen);
    ok_wstr(L"", Name);

    // Test correct GUID, but NULL name
    SetLastError(0xbeeffeed);
    ZeroMemory(&Name, sizeof(Name));
    ApiReturn = pNhGetInterfaceNameFromGuid(&AdapterGUID, NULL, &ulOutBufLen, par1, par2);
    Error = GetLastError();

    ok(ApiReturn == ERROR_SUCCESS,
       "ApiReturn returned %ld, expected ERROR_SUCCESS\n",
       ApiReturn);
    ok(Error == 0xbeeffeed,
       "GetLastError() returned %ld, expected 0xbeeffeed\n",
       Error);
    ok(ulOutBufLen > 0,
       "ulOutBufLen is %ld, expected > 0\n",
       ulOutBufLen);
    ok_wstr(L"", Name);

    // NhGetInterfaceNameFromGuid will throw exception if pOutBufLen is NULL
    SetLastError(0xbeeffeed);
    Error = 0xbeeffeed;<--- Error is assigned
    ZeroMemory(&Name, sizeof(Name));
    ApiReturn = ERROR_SUCCESS;<--- ApiReturn is assigned
    StartSeh()
        ApiReturn = pNhGetInterfaceNameFromGuid(&AdapterGUID, &Name, NULL, par1, par2);<--- ApiReturn is overwritten
        Error = GetLastError();<--- Error is overwritten
    EndSeh(STATUS_SUCCESS);

    ok(ApiReturn == ERROR_INVALID_PARAMETER,
       "ApiReturn returned %ld, expected ERROR_INVALID_PARAMETER\n",
       ApiReturn);
    ok(Error == 0xbeeffeed,
       "GetLastError() returned %ld, expected 0xbeeffeed\n",
       Error);
    ok(ulOutBufLen > 0,
       "ulOutBufLen is %ld, expected > 0\n",
       ulOutBufLen);
    ok_wstr(L"", Name);

    // Test correct values
    SetLastError(0xbeeffeed);
    ZeroMemory(&Name, sizeof(Name));
    ulOutBufLen = sizeof(Name);
    ApiReturn = pNhGetInterfaceNameFromGuid(&AdapterGUID, &Name, &ulOutBufLen, par1, par2);
    Error = GetLastError();

    ok(ApiReturn == ERROR_SUCCESS,
       "ApiReturn returned %ld, expected ERROR_SUCCESS\n",
       ApiReturn);
    ok(Error == 0xbeeffeed,
       "GetLastError() returned %ld, expected 0xbeeffeed\n",
       Error);
    ok(ulOutBufLen > 0,
       "ulOutBufLen is %ld, expected > 0\n",
       ulOutBufLen);
    Length = wcslen(Name);
    ok(Length > 0,
       "wcslen(Name) is %ld, expected > 0\n",
       Length);
    if (Length > 0)
        trace("Adapter name: \"%S\"\n", Name);

    // Test correct values, but with new unique GUID
    SetLastError(0xbeeffeed);
    ZeroMemory(&Name, sizeof(Name));
    ulOutBufLen = sizeof(Name);
    ApiReturn = pNhGetInterfaceNameFromGuid((PVOID)&UniqueGUID, &Name, &ulOutBufLen, par1, par2);
    Error = GetLastError();

    ok(ApiReturn == ERROR_NOT_FOUND,
       "ApiReturn returned %ld, expected ERROR_NOT_FOUND\n",
       ApiReturn);
    ok(Error == ERROR_PATH_NOT_FOUND,
       "GetLastError() returned %ld, expected ERROR_PATH_NOT_FOUND\n",
       Error);
    ok(ulOutBufLen == sizeof(Name),
       "ulOutBufLen is %ld, expected = sizeof(Name)\n",
       ulOutBufLen);
    ok_wstr(L"", Name);

    // Test correct values, but with small length
    SetLastError(0xbeeffeed);
    ZeroMemory(&Name, sizeof(Name));
    ulOutBufLen = 0;
    ApiReturn = pNhGetInterfaceNameFromGuid(&AdapterGUID, &Name, &ulOutBufLen, par1, par2);
    Error = GetLastError();

    ok(ApiReturn == ERROR_INSUFFICIENT_BUFFER,
       "ApiReturn returned %ld, expected ERROR_INSUFFICIENT_BUFFER\n",
       ApiReturn);
    ok(Error == 0xbeeffeed,
       "GetLastError() returned %ld, expected 0xbeeffeed\n",
       Error);
    ok(ulOutBufLen == MAX_INTERFACE_NAME_LEN * 2,
       "ulOutBufLen is %ld, expected = MAX_INTERFACE_NAME_LEN * 2\n",
       ulOutBufLen);
    ok_wstr(L"", Name);
}

/*
 * Tests for NhGetInterfaceNameFromDeviceGuid
 */
static
VOID
test_NhGetInterfaceNameFromDeviceGuid(GUID AdapterGUID, DWORD par1, DWORD par2)
{
    DWORD ApiReturn, Error;
    ULONG ulOutBufLen;
    WCHAR Name[MAX_INTERFACE_NAME_LEN];
    GUID UniqueGUID = MY_TEST_GUID;
    SIZE_T Length;

    // Test NULL GUID
    // Windows XP: NhGetInterfaceNameFromDeviceGuid throws exception here
    SetLastError(0xbeeffeed);
    Error = 0xbeeffeed;<--- Error is assigned
    ZeroMemory(&Name, sizeof(Name));
    ApiReturn = ERROR_SUCCESS;<--- ApiReturn is assigned
    ulOutBufLen = sizeof(Name);
    StartSeh()
        ApiReturn = pNhGetInterfaceNameFromDeviceGuid(NULL, &Name, &ulOutBufLen, par1, par2);<--- ApiReturn is overwritten
        Error = GetLastError();<--- Error is overwritten
    EndSeh(STATUS_SUCCESS);

    ok(ApiReturn == ERROR_INVALID_PARAMETER,
       "ApiReturn returned %ld, expected ERROR_INVALID_PARAMETER\n",
       ApiReturn);
    ok(Error == ERROR_SUCCESS,
       "GetLastError() returned %ld, expected ERROR_SUCCESS\n",
       Error);
    ok(ulOutBufLen == sizeof(Name),
       "ulOutBufLen is %ld, expected = sizeof(Name)\n",
       ulOutBufLen);
    ok_wstr(L"", Name);

    // Test correct GUID, but NULL name
    SetLastError(0xbeeffeed);
    Error = 0xbeeffeed;<--- Error is assigned
    ZeroMemory(&Name, sizeof(Name));
    ApiReturn = ERROR_SUCCESS;<--- ApiReturn is assigned
    StartSeh()
        ApiReturn = pNhGetInterfaceNameFromDeviceGuid(&AdapterGUID, NULL, &ulOutBufLen, par1, par2);<--- ApiReturn is overwritten
        Error = GetLastError();<--- Error is overwritten
    EndSeh(STATUS_SUCCESS);

    ok(ApiReturn == ERROR_INVALID_PARAMETER,
       "ApiReturn returned %ld, expected ERROR_INVALID_PARAMETER\n",
       ApiReturn);
    ok(Error == ERROR_SUCCESS,
       "GetLastError() returned %ld, expected ERROR_SUCCESS\n",
       Error);
    ok(ulOutBufLen > 0,
       "ulOutBufLen is %ld, expected > 0\n",
       ulOutBufLen);
    ok_wstr(L"", Name);

    // NhGetInterfaceNameFromDeviceGuid will throw exception if pOutBufLen is NULL
    SetLastError(0xbeeffeed);
    Error = 0xbeeffeed;<--- Error is assigned
    ZeroMemory(&Name, sizeof(Name));
    ApiReturn = ERROR_SUCCESS;<--- ApiReturn is assigned
    StartSeh()
        ApiReturn = pNhGetInterfaceNameFromDeviceGuid(&AdapterGUID, &Name, NULL, par1, par2);<--- ApiReturn is overwritten
        Error = GetLastError();<--- Error is overwritten
    EndSeh(STATUS_SUCCESS);

    ok(ApiReturn == ERROR_INVALID_PARAMETER,
       "ApiReturn returned %ld, expected ERROR_INVALID_PARAMETER\n",
       ApiReturn);
    ok(Error == ERROR_SUCCESS,
       "GetLastError() returned %ld, expected ERROR_SUCCESS\n",
       Error);
    ok(ulOutBufLen > 0,
       "ulOutBufLen is %ld, expected > 0\n",
       ulOutBufLen);
    ok_wstr(L"", Name);

    // Test correct values
    SetLastError(0xbeeffeed);
    ZeroMemory(&Name, sizeof(Name));
    ulOutBufLen = sizeof(Name);
    ApiReturn = pNhGetInterfaceNameFromDeviceGuid(&AdapterGUID, &Name, &ulOutBufLen, par1, par2);
    Error = GetLastError();

    ok(ApiReturn == ERROR_SUCCESS,
       "ApiReturn returned %ld, expected ERROR_SUCCESS\n",
       ApiReturn);
    ok(Error == ERROR_SUCCESS,
       "GetLastError() returned %ld, expected ERROR_SUCCESS\n",
       Error);
    ok(ulOutBufLen > 0,
       "ulOutBufLen is %ld, expected > 0\n",
       ulOutBufLen);
    Length = wcslen(Name);
    ok(Length > 0,
       "wcslen(Name) is %ld, expected > 0\n",
       Length);
    if (Length > 0)
        trace("Adapter name: \"%S\"\n", Name);

    // Test correct values, but with new unique GUID
    SetLastError(0xbeeffeed);
    ZeroMemory(&Name, sizeof(Name));
    ulOutBufLen = sizeof(Name);
    ApiReturn = pNhGetInterfaceNameFromDeviceGuid((PVOID)&UniqueGUID, &Name, &ulOutBufLen, par1, par2);
    Error = GetLastError();

    ok(ApiReturn == ERROR_NOT_FOUND,
       "ApiReturn returned %ld, expected ERROR_NOT_FOUND\n",
       ApiReturn);
    ok(Error == ERROR_SUCCESS,
       "GetLastError() returned %ld, expected ERROR_SUCCESS\n",
       Error);
    ok(ulOutBufLen == sizeof(Name),
       "ulOutBufLen is %ld, expected = sizeof(Name)\n",
       ulOutBufLen);
    ok_wstr(L"", Name);

    // Test correct values, but with small length
    SetLastError(0xbeeffeed);
    ZeroMemory(&Name, sizeof(Name));
    ulOutBufLen = 0;
    ApiReturn = pNhGetInterfaceNameFromDeviceGuid(&AdapterGUID, &Name, &ulOutBufLen, par1, par2);
    Error = GetLastError();

    ok(ApiReturn == ERROR_INSUFFICIENT_BUFFER,
       "ApiReturn returned %ld, expected ERROR_INSUFFICIENT_BUFFER\n",
       ApiReturn);
    ok(Error == ERROR_SUCCESS,
       "GetLastError() returned %ld, expected ERROR_SUCCESS\n",
       Error);
    ok(ulOutBufLen == MAX_INTERFACE_NAME_LEN * 2,
       "ulOutBufLen is %ld, expected = MAX_INTERFACE_NAME_LEN * 2\n",
       ulOutBufLen);
    ok_wstr(L"", Name);
}

static
VOID
test_GetInterfaceName(VOID)
{
    PIP_INTERFACE_INFO pInfo = NULL;
    ULONG ulOutBufLen = 0;
    DWORD ApiReturn;
    WCHAR Name[MAX_ADAPTER_NAME];
    UNICODE_STRING GuidString;
    GUID AdapterGUID;
    HINSTANCE hIpHlpApi;

    ApiReturn = GetInterfaceInfo(pInfo, &ulOutBufLen);
    ok(ApiReturn == ERROR_INSUFFICIENT_BUFFER,
       "GetInterfaceInfo(pInfo, &ulOutBufLen) returned %ld, expected ERROR_INSUFFICIENT_BUFFER\n",
       ApiReturn);
    if (ApiReturn != ERROR_INSUFFICIENT_BUFFER)
    {
        skip("Can't determine size of IP_INTERFACE_INFO. Can't proceed\n");
        return;
    }

    pInfo = (IP_INTERFACE_INFO *)HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, ulOutBufLen);
    if (pInfo == NULL)
    {
        skip("pInfo is NULL. Can't proceed\n");
        return;
    }

    ApiReturn = GetInterfaceInfo(pInfo, &ulOutBufLen);
    ok(ApiReturn == NO_ERROR,
        "GetInterfaceInfo(pInfo, &ulOutBufLen) returned %ld, expected NO_ERROR\n",
        ApiReturn);
    if (ApiReturn != NO_ERROR || ulOutBufLen == 0)
    {
        skip("GetInterfaceInfo failed with error %ld. Can't proceed\n", ApiReturn);
        return;
    }

    if (pInfo->NumAdapters > 0)
        CopyMemory(&Name, &pInfo->Adapter[0].Name, sizeof(Name));

    if (pInfo->NumAdapters == 0)
    {
        HeapFree(GetProcessHeap(), 0, pInfo);
        skip("pInfo->NumAdapters = 0. Can't proceed\n");
        return;
    }
    trace("pInfo->NumAdapters: %lu\n", pInfo->NumAdapters);

    HeapFree(GetProcessHeap(), 0, pInfo);

    ApiReturn = wcsncmp(Name, L"\\DEVICE\\TCPIP_", 14);
    ok(ApiReturn == 0,
       "wcsncmp(Name, L\"\\DEVICE\\TCPIP_\", 14) returned %ld, expected 0\n",
       ApiReturn);
    if (ApiReturn != 0)
    {
        if (wcslen(Name) == 0)
        {
            skip("pInfo->Adapter[0].Name is empty. Can't proceed\n");
            return;
        }
        else
        {
            // workaround for ReactOS
            trace("pInfo->Adapter[0].Name = \"%ls\" is incorrect.\n", Name);
            RtlInitUnicodeString(&GuidString, &Name[0]);
        }
    }
    else
    {
        RtlInitUnicodeString(&GuidString, &Name[14]);
    }

    ApiReturn = RtlGUIDFromString(&GuidString, &AdapterGUID);
    if (ApiReturn != 0)
    {
        skip("RtlGUIDFromString failed. Can't proceed\n");
        return;
    }

    hIpHlpApi = GetModuleHandleW(L"iphlpapi.dll");
    if (!hIpHlpApi)
    {
        skip("Failed to load iphlpapi.dll. Can't proceed\n");
        return;
    }

    pNhGetInterfaceNameFromGuid = (void *)GetProcAddress(hIpHlpApi, "NhGetInterfaceNameFromGuid");

    if (!pNhGetInterfaceNameFromGuid)
        skip("NhGetInterfaceNameFromGuid not found. Can't proceed\n");
    else
        test_NhGetInterfaceNameFromGuid(AdapterGUID, 0, 0);

    pNhGetInterfaceNameFromDeviceGuid = (void *)GetProcAddress(hIpHlpApi, "NhGetInterfaceNameFromDeviceGuid");

    if (!pNhGetInterfaceNameFromDeviceGuid)
        skip("NhGetInterfaceNameFromDeviceGuid not found. Can't proceed\n");
    else
        test_NhGetInterfaceNameFromDeviceGuid(AdapterGUID, 1, 0);
}

START_TEST(GetInterfaceName)
{
    test_GetInterfaceName();
}