[IPHLPAPI_APITEST] Add tests for GetOwnerModuleFromUdpEntry
authorPierre Schweitzer <pierre@reactos.org>
Fri, 21 Dec 2018 18:16:25 +0000 (19:16 +0100)
committerPierre Schweitzer <pierre@reactos.org>
Fri, 21 Dec 2018 18:17:11 +0000 (19:17 +0100)
modules/rostests/apitests/iphlpapi/CMakeLists.txt
modules/rostests/apitests/iphlpapi/GetOwnerModuleFromUdpEntry.c [new file with mode: 0644]
modules/rostests/apitests/iphlpapi/testlist.c

index 69b44b8..8fa7232 100644 (file)
@@ -5,6 +5,7 @@ list(APPEND SOURCE
     GetInterfaceName.c
     GetNetworkParams.c
     GetOwnerModuleFromTcpEntry.c
     GetInterfaceName.c
     GetNetworkParams.c
     GetOwnerModuleFromTcpEntry.c
+    GetOwnerModuleFromUdpEntry.c
     icmp.c
     SendARP.c
     testlist.c)
     icmp.c
     SendARP.c
     testlist.c)
diff --git a/modules/rostests/apitests/iphlpapi/GetOwnerModuleFromUdpEntry.c b/modules/rostests/apitests/iphlpapi/GetOwnerModuleFromUdpEntry.c
new file mode 100644 (file)
index 0000000..bfe4efe
--- /dev/null
@@ -0,0 +1,155 @@
+/*
+ * PROJECT:     ReactOS API Tests
+ * LICENSE:     GPL-2.0+ (https://spdx.org/licenses/GPL-2.0+)
+ * PURPOSE:     Tests for UDP connections owner functions
+ * COPYRIGHT:   Copyright 2018 Pierre Schweitzer
+ */
+
+#include <apitest.h>
+
+#define WIN32_NO_STATUS
+#include <iphlpapi.h>
+#include <winsock2.h>
+
+static DWORD GetExtendedUdpTableWithAlloc(PVOID *UdpTable, BOOL Order, DWORD Family, UDP_TABLE_CLASS Class)
+{
+    DWORD ret;
+    DWORD Size = 0;
+
+    *UdpTable = NULL;
+
+    ret = GetExtendedUdpTable(*UdpTable, &Size, Order, Family, Class, 0);
+    if (ret == ERROR_INSUFFICIENT_BUFFER)
+    {
+        *UdpTable = HeapAlloc(GetProcessHeap(), 0, Size);
+        if (*UdpTable == NULL)
+        {
+            return ERROR_OUTOFMEMORY;
+        }
+
+        ret = GetExtendedUdpTable(*UdpTable, &Size, Order, Family, Class, 0);
+        if (ret != NO_ERROR)
+        {
+            HeapFree(GetProcessHeap(), 0, *UdpTable);
+            *UdpTable = NULL;
+        }
+    }
+
+    return ret;
+}
+
+START_TEST(GetOwnerModuleFromUdpEntry)
+{
+    WSADATA wsaData;
+    SOCKET sock;
+    SOCKADDR_IN server;
+    PMIB_UDPTABLE_OWNER_MODULE UdpTableOwnerMod;
+    DWORD i;
+    BOOLEAN Found;
+    FILETIME Creation;
+    LARGE_INTEGER CreationTime;
+    DWORD Pid = GetCurrentProcessId();
+
+    if (WSAStartup(MAKEWORD(2, 2), &wsaData) != 0)
+    {
+        skip("Failed to init WS2\n");
+        return;
+    }
+
+    GetSystemTimeAsFileTime(&Creation);
+    CreationTime.LowPart = Creation.dwLowDateTime;
+    CreationTime.HighPart = Creation.dwHighDateTime;
+
+    sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
+    if (sock == INVALID_SOCKET)
+    {
+        skip("Cannot create socket\n");
+        goto quit;
+    }
+
+    ZeroMemory(&server, sizeof(SOCKADDR_IN));
+    server.sin_family = AF_INET;
+    server.sin_addr.s_addr = htonl(INADDR_ANY);
+    server.sin_port = htons(9876);
+
+    if (bind(sock, (SOCKADDR*)&server, sizeof(SOCKADDR_IN)) == SOCKET_ERROR)
+    {
+        skip("Cannot bind socket\n");
+        goto quit2;
+    }
+
+    if (GetExtendedUdpTableWithAlloc((PVOID *)&UdpTableOwnerMod, TRUE, AF_INET, UDP_TABLE_OWNER_MODULE) == ERROR_SUCCESS)
+    {
+        ok(UdpTableOwnerMod->dwNumEntries > 0, "No UDP connections?!\n");
+
+        Found = FALSE;
+        for (i = 0; i < UdpTableOwnerMod->dwNumEntries; ++i)
+        {
+            if (UdpTableOwnerMod->table[i].dwLocalAddr == 0 &&
+                UdpTableOwnerMod->table[i].dwLocalPort == htons(9876))
+            {
+                Found = TRUE;
+                break;
+            }
+        }
+
+        if (!Found)
+        {
+            skip("Our socket wasn't found!\n");
+        }
+        else
+        {
+            DWORD Size = 0;
+            PTCPIP_OWNER_MODULE_BASIC_INFO BasicInfo = NULL;
+
+            ok(UdpTableOwnerMod->table[i].dwOwningPid == Pid, "Invalid owner\n");
+
+            ok(UdpTableOwnerMod->table[i].liCreateTimestamp.QuadPart >= CreationTime.QuadPart, "Invalid time\n");
+            ok(UdpTableOwnerMod->table[i].liCreateTimestamp.QuadPart <= CreationTime.QuadPart + 60000000000LL, "Invalid time\n");
+
+            if (GetOwnerModuleFromUdpEntry(&UdpTableOwnerMod->table[i], TCPIP_OWNER_MODULE_INFO_BASIC, BasicInfo, &Size) == ERROR_INSUFFICIENT_BUFFER)
+            {
+                BasicInfo = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, Size);
+                ok(BasicInfo != NULL, "HeapAlloc failed\n");
+
+                if (GetOwnerModuleFromUdpEntry(&UdpTableOwnerMod->table[i], TCPIP_OWNER_MODULE_INFO_BASIC, BasicInfo, &Size) == ERROR_SUCCESS)
+                {
+                    WCHAR CurrentModule[MAX_PATH];
+                    PWSTR FileName;
+
+                    if (GetModuleFileNameW(NULL, CurrentModule, MAX_PATH) != 0)
+                    {
+                        FileName = wcsrchr(CurrentModule, L'\\');
+                        ++FileName;
+
+                        ok(_wcsicmp(CurrentModule, BasicInfo->pModulePath) == 0, "Mismatching names (%S, %S)\n", CurrentModule, BasicInfo->pModulePath);
+                        ok(_wcsicmp(FileName, BasicInfo->pModuleName) == 0, "Mismatching names (%S, %S)\n", FileName, BasicInfo->pModuleName);
+                    }
+                    else
+                    {
+                        skip("GetModuleFileNameW failed\n");
+                    }
+                }
+                else
+                {
+                    skip("GetOwnerModuleFromTcpEntry failed\n");
+                }
+            }
+            else
+            {
+                skip("GetOwnerModuleFromTcpEntry failed\n");
+            }
+        }
+
+        HeapFree(GetProcessHeap(), 0, UdpTableOwnerMod);
+    }
+    else
+    {
+        skip("GetExtendedUdpTableWithAlloc failure\n");
+    }
+
+quit2:
+    closesocket(sock);
+quit:
+    WSACleanup();
+}
index 6690e3d..7699e60 100644 (file)
@@ -8,6 +8,7 @@ extern void func_GetExtendedUdpTable(void);
 extern void func_GetInterfaceName(void);
 extern void func_GetNetworkParams(void);
 extern void func_GetOwnerModuleFromTcpEntry(void);
 extern void func_GetInterfaceName(void);
 extern void func_GetNetworkParams(void);
 extern void func_GetOwnerModuleFromTcpEntry(void);
+extern void func_GetOwnerModuleFromUdpEntry(void);
 extern void func_icmp(void);
 extern void func_SendARP(void);
 
 extern void func_icmp(void);
 extern void func_SendARP(void);
 
@@ -18,6 +19,7 @@ const struct test winetest_testlist[] =
     { "GetInterfaceName",           func_GetInterfaceName },
     { "GetNetworkParams",           func_GetNetworkParams },
     { "GetOwnerModuleFromTcpEntry", func_GetOwnerModuleFromTcpEntry },
     { "GetInterfaceName",           func_GetInterfaceName },
     { "GetNetworkParams",           func_GetNetworkParams },
     { "GetOwnerModuleFromTcpEntry", func_GetOwnerModuleFromTcpEntry },
+    { "GetOwnerModuleFromUdpEntry", func_GetOwnerModuleFromUdpEntry },
     { "icmp",                       func_icmp },
     { "SendARP",                    func_SendARP },
 
     { "icmp",                       func_icmp },
     { "SendARP",                    func_SendARP },