2 * PROJECT: ReactOS API Tests
3 * LICENSE: GPL-2.0+ (https://spdx.org/licenses/GPL-2.0+)
4 * PURPOSE: Tests for TCP connections owner functions
5 * COPYRIGHT: Copyright 2018 Pierre Schweitzer
10 #define WIN32_NO_STATUS
14 static DWORD
GetExtendedTcpTableWithAlloc(PVOID
*TcpTable
, BOOL Order
, DWORD Family
, TCP_TABLE_CLASS Class
)
21 ret
= GetExtendedTcpTable(*TcpTable
, &Size
, Order
, Family
, Class
, 0);
22 if (ret
== ERROR_INSUFFICIENT_BUFFER
)
24 *TcpTable
= HeapAlloc(GetProcessHeap(), 0, Size
);
25 if (*TcpTable
== NULL
)
27 return ERROR_OUTOFMEMORY
;
30 ret
= GetExtendedTcpTable(*TcpTable
, &Size
, Order
, Family
, Class
, 0);
33 HeapFree(GetProcessHeap(), 0, *TcpTable
);
41 START_TEST(GetOwnerModuleFromTcpEntry
)
46 PMIB_TCPTABLE_OWNER_MODULE TcpTableOwnerMod
;
50 LARGE_INTEGER CreationTime
;
51 DWORD Pid
= GetCurrentProcessId();
53 if (WSAStartup(MAKEWORD(2, 2), &wsaData
) != 0)
55 skip("Failed to init WS2\n");
59 GetSystemTimeAsFileTime(&Creation
);
60 CreationTime
.LowPart
= Creation
.dwLowDateTime
;
61 CreationTime
.HighPart
= Creation
.dwHighDateTime
;
63 sock
= socket(AF_INET
, SOCK_STREAM
, 0);
64 if (sock
== INVALID_SOCKET
)
66 skip("Cannot create socket\n");
70 ZeroMemory(&server
, sizeof(SOCKADDR_IN
));
71 server
.sin_family
= AF_INET
;
72 server
.sin_addr
.s_addr
= htonl(INADDR_ANY
);
73 server
.sin_port
= htons(9876);
75 if (bind(sock
, (SOCKADDR
*)&server
, sizeof(SOCKADDR_IN
)) == SOCKET_ERROR
)
77 skip("Cannot bind socket\n");
81 if (listen(sock
, SOMAXCONN
) == SOCKET_ERROR
)
83 skip("Cannot listen on socket\n");
87 if (GetExtendedTcpTableWithAlloc((PVOID
*)&TcpTableOwnerMod
, TRUE
, AF_INET
, TCP_TABLE_OWNER_MODULE_LISTENER
) == ERROR_SUCCESS
)
89 ok(TcpTableOwnerMod
->dwNumEntries
> 0, "No TCP connections?!\n");
92 for (i
= 0; i
< TcpTableOwnerMod
->dwNumEntries
; ++i
)
94 if (TcpTableOwnerMod
->table
[i
].dwState
== MIB_TCP_STATE_LISTEN
&&
95 TcpTableOwnerMod
->table
[i
].dwLocalAddr
== 0 &&
96 TcpTableOwnerMod
->table
[i
].dwLocalPort
== htons(9876) &&
97 TcpTableOwnerMod
->table
[i
].dwRemoteAddr
== 0)
106 skip("Our socket wasn't found!\n");
111 PTCPIP_OWNER_MODULE_BASIC_INFO BasicInfo
= NULL
;
113 ok(TcpTableOwnerMod
->table
[i
].dwOwningPid
== Pid
, "Invalid owner\n");
115 ok(TcpTableOwnerMod
->table
[i
].liCreateTimestamp
.QuadPart
>= CreationTime
.QuadPart
, "Invalid time\n");
116 ok(TcpTableOwnerMod
->table
[i
].liCreateTimestamp
.QuadPart
<= CreationTime
.QuadPart
+ 60000000000LL, "Invalid time\n");
118 if (GetOwnerModuleFromTcpEntry(&TcpTableOwnerMod
->table
[i
], TCPIP_OWNER_MODULE_INFO_BASIC
, BasicInfo
, &Size
) == ERROR_INSUFFICIENT_BUFFER
)
120 BasicInfo
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, Size
);
121 ok(BasicInfo
!= NULL
, "HeapAlloc failed\n");
123 if (GetOwnerModuleFromTcpEntry(&TcpTableOwnerMod
->table
[i
], TCPIP_OWNER_MODULE_INFO_BASIC
, BasicInfo
, &Size
) == ERROR_SUCCESS
)
125 WCHAR CurrentModule
[MAX_PATH
];
128 if (GetModuleFileNameW(NULL
, CurrentModule
, MAX_PATH
) != 0)
130 FileName
= wcsrchr(CurrentModule
, L
'\\');
133 ok(_wcsicmp(CurrentModule
, BasicInfo
->pModulePath
) == 0, "Mismatching names (%S, %S)\n", CurrentModule
, BasicInfo
->pModulePath
);
134 ok(_wcsicmp(FileName
, BasicInfo
->pModuleName
) == 0, "Mismatching names (%S, %S)\n", FileName
, BasicInfo
->pModuleName
);
138 skip("GetModuleFileNameW failed\n");
143 skip("GetOwnerModuleFromTcpEntry failed\n");
148 skip("GetOwnerModuleFromTcpEntry failed\n");
152 HeapFree(GetProcessHeap(), 0, TcpTableOwnerMod
);
156 skip("GetExtendedTcpTableWithAlloc failure\n");