From: Bruno Jesus <00cpxxx@gmail.com> Subject: ws2_32/tests: Check if the first IP is a default route in gethostbyname Message-Id: Date: Wed, 25 Nov 2015 17:08:59 +0800 Signed-off-by: Bruno Jesus <00cpxxx@gmail.com> diff --git a/dlls/ws2_32/tests/sock.c b/dlls/ws2_32/tests/sock.c index 8676b07..c9ff289 100644 --- a/dlls/ws2_32/tests/sock.c +++ b/dlls/ws2_32/tests/sock.c @@ -30,6 +30,7 @@ #include #include #include +#include #include #include "wine/test.h" @@ -76,6 +77,10 @@ static int (WINAPI *pWSAEnumNameSpaceProvidersA)(LPDWORD,LPWSANAMESPACE_INFOA) static int (WINAPI *pWSAEnumNameSpaceProvidersW)(LPDWORD,LPWSANAMESPACE_INFOW); static int (WINAPI *pWSAPoll)(WSAPOLLFD *,ULONG,INT); +/* Function pointers from iphlpapi */ +static DWORD (WINAPI *pGetAdaptersInfo)(PIP_ADAPTER_INFO,PULONG); +static DWORD (WINAPI *pGetIpForwardTable)(PMIB_IPFORWARDTABLE,PULONG,BOOL); + /**************** Structs and typedefs ***************/ typedef struct thread_info @@ -1156,7 +1161,7 @@ static void Init (void) { WORD ver = MAKEWORD (2, 2); WSADATA data; - HMODULE hws2_32 = GetModuleHandleA("ws2_32.dll"); + HMODULE hws2_32 = GetModuleHandleA("ws2_32.dll"), hiphlpapi; pfreeaddrinfo = (void *)GetProcAddress(hws2_32, "freeaddrinfo"); pgetaddrinfo = (void *)GetProcAddress(hws2_32, "getaddrinfo"); @@ -1172,6 +1177,13 @@ static void Init (void) pWSAEnumNameSpaceProvidersW = (void *)GetProcAddress(hws2_32, "WSAEnumNameSpaceProvidersW"); pWSAPoll = (void *)GetProcAddress(hws2_32, "WSAPoll"); + hiphlpapi = LoadLibraryA("iphlpapi.dll"); + if (hiphlpapi) + { + pGetIpForwardTable = (void *)GetProcAddress(hiphlpapi, "GetIpForwardTable"); + pGetAdaptersInfo = (void *)GetProcAddress(hiphlpapi, "GetAdaptersInfo"); + } + ok ( WSAStartup ( ver, &data ) == 0, "WSAStartup failed\n" ); tls = TlsAlloc(); } @@ -4400,6 +4412,83 @@ static void test_dns(void) int WINAPI gethostname(char *name, int namelen); +static void test_gethostbyname(void) +{ + struct hostent *he; + struct in_addr **addr_list; + char name[256], first_ip[16]; + int ret, i; + PMIB_IPFORWARDTABLE routes = NULL; + PIP_ADAPTER_INFO adapters = NULL, k; + DWORD adap_size = 0, route_size = 0; + BOOL found_default = FALSE; + BOOL local_ip = FALSE; + + ret = gethostname(name, sizeof(name)); + ok(ret == 0, "gethostname() call failed: %d\n", WSAGetLastError()); + + he = gethostbyname(name); + ok(he != NULL, "gethostbyname(\"%s\") failed: %d\n", name, WSAGetLastError()); + addr_list = (struct in_addr **)he->h_addr_list; + strcpy(first_ip, inet_ntoa(*addr_list[0])); + + printf("List of local IPs:\n"); + for(i = 0; addr_list[i] != NULL; i++) + { + char *ip = inet_ntoa(*addr_list[i]); + if (!strcmp(ip, "127.0.0.1")) + local_ip = TRUE; + printf("%s\n", ip); + } + ok (!local_ip || (local_ip && i == 1), "expected 127.0.0.1 to be the only IP returned\n"); + + if (!pGetAdaptersInfo || !pGetIpForwardTable) + { + win_skip("GetAdaptersInfo and/or GetIpForwardTable not found, skipping tests\n"); + return; + } + + ret = pGetAdaptersInfo(NULL, &adap_size); + ok (ret == ERROR_BUFFER_OVERFLOW, "GetAdaptersInfo failed with a different error: %d\n", ret); + ret = pGetIpForwardTable(NULL, &route_size, FALSE); + ok (ret == ERROR_INSUFFICIENT_BUFFER, "GetIpForwardTable failed with a different error: %d\n", ret); + + adapters = HeapAlloc(GetProcessHeap(), 0, adap_size); + routes = HeapAlloc(GetProcessHeap(), 0, route_size); + + ret = pGetAdaptersInfo(adapters, &adap_size); + ok (ret == NO_ERROR, "GetAdaptersInfo failed, error: %d\n", ret); + ret = pGetIpForwardTable(routes, &route_size, FALSE); + ok (ret == NO_ERROR, "GetIpForwardTable failed, error: %d\n", ret); + + for (i = 0; i < routes->dwNumEntries; i++) + { + /* default route? */ + if (routes->table[i].dwForwardDest) continue; + + for (k = adapters; k != NULL; k = k->Next) + { + char *ip; + if (k->Index != routes->table[i].dwForwardIfIndex) continue; + ip = k->IpAddressList.IpAddress.String; + /* the first IP returned from gethostbyname must be a default route */ + printf("default route[%d]: %s\n", i, ip); + if (!strcmp(first_ip, ip)) + { + found_default = TRUE; + break; + } + } + ok (k != NULL, "failed to find interface index %d\n", routes->table[i].dwForwardIfIndex); + break; + } + ok (found_default, "failed to find the first IP from gethostbyname!\n"); + ok (i != routes->dwNumEntries, "failed to find the default route\n"); + + HeapFree(GetProcessHeap(), 0, adapters); + HeapFree(GetProcessHeap(), 0, routes); +} + static void test_gethostbyname_hack(void) { struct hostent *he; @@ -9372,7 +9461,7 @@ START_TEST( sock ) test_WithWSAStartup(); Init(); - +test_gethostbyname(); return; test_inet_ntoa(); test_inet_pton(); test_set_getsockopt(); @@ -9410,6 +9499,7 @@ START_TEST( sock ) test_addr_to_print(); test_ioctlsocket(); test_dns(); + test_gethostbyname(); test_gethostbyname_hack(); test_gethostname();