https://git.reactos.org/?p=reactos.git;a=commitdiff;h=e9c7c6fd2c26e02ed2c813f2c7f96802488ca332

commit e9c7c6fd2c26e02ed2c813f2c7f96802488ca332
Author:     Pierre Schweitzer <pie...@reactos.org>
AuthorDate: Thu Nov 29 19:58:36 2018 +0100
Commit:     Pierre Schweitzer <pie...@reactos.org>
CommitDate: Thu Nov 29 20:30:23 2018 +0100

    [IPHLPAPI_APITEST] Add tests for GetOwnerModuleFromTcpEntry()
---
 modules/rostests/apitests/iphlpapi/CMakeLists.txt  |   1 +
 .../apitests/iphlpapi/GetExtendedTcpTable.c        |   2 +-
 .../apitests/iphlpapi/GetOwnerModuleFromTcpEntry.c | 163 +++++++++++++++++++++
 modules/rostests/apitests/iphlpapi/testlist.c      |  12 +-
 4 files changed, 172 insertions(+), 6 deletions(-)

diff --git a/modules/rostests/apitests/iphlpapi/CMakeLists.txt 
b/modules/rostests/apitests/iphlpapi/CMakeLists.txt
index 23b8218972..81ca68cae9 100644
--- a/modules/rostests/apitests/iphlpapi/CMakeLists.txt
+++ b/modules/rostests/apitests/iphlpapi/CMakeLists.txt
@@ -3,6 +3,7 @@ list(APPEND SOURCE
     GetExtendedTcpTable.c
     GetInterfaceName.c
     GetNetworkParams.c
+    GetOwnerModuleFromTcpEntry.c
     icmp.c
     SendARP.c
     testlist.c)
diff --git a/modules/rostests/apitests/iphlpapi/GetExtendedTcpTable.c 
b/modules/rostests/apitests/iphlpapi/GetExtendedTcpTable.c
index 3beda7e8fd..87f3b65d3a 100644
--- a/modules/rostests/apitests/iphlpapi/GetExtendedTcpTable.c
+++ b/modules/rostests/apitests/iphlpapi/GetExtendedTcpTable.c
@@ -11,7 +11,7 @@
 #include <iphlpapi.h>
 #include <winsock2.h>
 
-DWORD GetExtendedTcpTableWithAlloc(PVOID *TcpTable, BOOL Order, DWORD Family, 
TCP_TABLE_CLASS Class)
+static DWORD GetExtendedTcpTableWithAlloc(PVOID *TcpTable, BOOL Order, DWORD 
Family, TCP_TABLE_CLASS Class)
 {
     DWORD ret;
     DWORD Size = 0;
diff --git a/modules/rostests/apitests/iphlpapi/GetOwnerModuleFromTcpEntry.c 
b/modules/rostests/apitests/iphlpapi/GetOwnerModuleFromTcpEntry.c
new file mode 100644
index 0000000000..a57142118f
--- /dev/null
+++ b/modules/rostests/apitests/iphlpapi/GetOwnerModuleFromTcpEntry.c
@@ -0,0 +1,163 @@
+/*
+ * PROJECT:     ReactOS API Tests
+ * LICENSE:     GPL-2.0+ (https://spdx.org/licenses/GPL-2.0+)
+ * PURPOSE:     Tests for TCP connections owner functions
+ * COPYRIGHT:   Copyright 2018 Pierre Schweitzer
+ */
+
+#include <apitest.h>
+
+#define WIN32_NO_STATUS
+#include <iphlpapi.h>
+#include <winsock2.h>
+
+static DWORD GetExtendedTcpTableWithAlloc(PVOID *TcpTable, BOOL Order, DWORD 
Family, TCP_TABLE_CLASS Class)
+{
+    DWORD ret;
+    DWORD Size = 0;
+
+    *TcpTable = NULL;
+
+    ret = GetExtendedTcpTable(*TcpTable, &Size, Order, Family, Class, 0);
+    if (ret == ERROR_INSUFFICIENT_BUFFER)
+    {
+        *TcpTable = HeapAlloc(GetProcessHeap(), 0, Size);
+        if (*TcpTable == NULL)
+        {
+            return ERROR_OUTOFMEMORY;
+        }
+
+        ret = GetExtendedTcpTable(*TcpTable, &Size, Order, Family, Class, 0);
+        if (ret != NO_ERROR)
+        {
+            HeapFree(GetProcessHeap(), 0, *TcpTable);
+            *TcpTable = NULL;
+        }
+    }
+
+    return ret;
+}
+
+START_TEST(GetOwnerModuleFromTcpEntry)
+{
+    WSADATA wsaData;
+    SOCKET sock;
+    SOCKADDR_IN server;
+    PMIB_TCPTABLE_OWNER_MODULE TcpTableOwnerMod;
+    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_STREAM, 0);
+    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 (listen(sock, SOMAXCONN) == SOCKET_ERROR)
+    {
+        skip("Cannot listen on socket\n");
+        goto quit2;
+    }
+
+    if (GetExtendedTcpTableWithAlloc((PVOID *)&TcpTableOwnerMod, TRUE, 
AF_INET, TCP_TABLE_OWNER_MODULE_LISTENER) == ERROR_SUCCESS)
+    {
+        ok(TcpTableOwnerMod->dwNumEntries > 0, "No TCP connections?!\n");
+
+        Found = FALSE;
+        for (i = 0; i < TcpTableOwnerMod->dwNumEntries; ++i)
+        {
+            if (TcpTableOwnerMod->table[i].dwState == MIB_TCP_STATE_LISTEN &&
+                TcpTableOwnerMod->table[i].dwLocalAddr == 0 &&
+                TcpTableOwnerMod->table[i].dwLocalPort == htons(9876) &&
+                TcpTableOwnerMod->table[i].dwRemoteAddr == 0)
+            {
+                Found = TRUE;
+                break;
+            }
+        }
+
+        if (!Found)
+        {
+            skip("Our socket wasn't found!\n");
+        }
+        else
+        {
+            DWORD Size = 0;
+            PTCPIP_OWNER_MODULE_BASIC_INFO BasicInfo = NULL;
+
+            ok(TcpTableOwnerMod->table[i].dwOwningPid == Pid, "Invalid 
owner\n");
+
+            ok(TcpTableOwnerMod->table[i].liCreateTimestamp.QuadPart >= 
CreationTime.QuadPart, "Invalid time\n");
+            ok(TcpTableOwnerMod->table[i].liCreateTimestamp.QuadPart <= 
CreationTime.QuadPart + 60000000000LL, "Invalid time\n");
+
+            if (GetOwnerModuleFromTcpEntry(&TcpTableOwnerMod->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 (GetOwnerModuleFromTcpEntry(&TcpTableOwnerMod->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, TcpTableOwnerMod);
+    }
+    else
+    {
+        skip("GetExtendedTcpTableWithAlloc failure\n");
+    }
+
+quit2:
+    closesocket(sock);
+quit:
+    WSACleanup();
+}
diff --git a/modules/rostests/apitests/iphlpapi/testlist.c 
b/modules/rostests/apitests/iphlpapi/testlist.c
index fb2e999445..4f56d9da55 100644
--- a/modules/rostests/apitests/iphlpapi/testlist.c
+++ b/modules/rostests/apitests/iphlpapi/testlist.c
@@ -6,16 +6,18 @@
 extern void func_GetExtendedTcpTable(void);
 extern void func_GetInterfaceName(void);
 extern void func_GetNetworkParams(void);
+extern void func_GetOwnerModuleFromTcpEntry(void);
 extern void func_icmp(void);
 extern void func_SendARP(void);
 
 const struct test winetest_testlist[] =
 {
-    { "GetExtendedTcpTable", func_GetExtendedTcpTable },
-    { "GetInterfaceName",    func_GetInterfaceName },
-    { "GetNetworkParams",    func_GetNetworkParams },
-    { "icmp",                func_icmp },
-    { "SendARP",             func_SendARP },
+    { "GetExtendedTcpTable",        func_GetExtendedTcpTable },
+    { "GetInterfaceName",           func_GetInterfaceName },
+    { "GetNetworkParams",           func_GetNetworkParams },
+    { "GetOwnerModuleFromTcpEntry", func_GetOwnerModuleFromTcpEntry },
+    { "icmp",                       func_icmp },
+    { "SendARP",                    func_SendARP },
 
     { 0, 0 }
 };

Reply via email to