[IPHLPAPI_APITEST] CurrPorts seems to confirm that timestamp is really a timestamp
[reactos.git] / modules / rostests / apitests / iphlpapi / AllocateAndGetTcpExTable2FromStack.c
1 /*
2 * PROJECT: ReactOS API Tests
3 * LICENSE: GPL-2.0+ (https://spdx.org/licenses/GPL-2.0+)
4 * PURPOSE: Tests for TCP connections enumeration functions
5 * COPYRIGHT: Copyright 2018 Pierre Schweitzer
6 */
7
8 #include <apitest.h>
9
10 #define WIN32_NO_STATUS
11 #include <iphlpapi.h>
12 #include <winsock2.h>
13
14 static DWORD (WINAPI * pAllocateAndGetTcpExTable2FromStack)(PVOID*,BOOL,HANDLE,DWORD,DWORD,TCP_TABLE_CLASS);
15
16 START_TEST(AllocateAndGetTcpExTable2FromStack)
17 {
18 WSADATA wsaData;
19 SOCKET sock;
20 SOCKADDR_IN server;
21 PMIB_TCPTABLE TcpTable;
22 PMIB_TCPTABLE_OWNER_PID TcpTableOwner;
23 PMIB_TCPTABLE_OWNER_MODULE TcpTableOwnerMod;
24 DWORD i;
25 BOOLEAN Found;
26 HINSTANCE hIpHlpApi;
27 FILETIME Creation;
28 LARGE_INTEGER CreationTime;
29 DWORD Pid = GetCurrentProcessId();
30
31 hIpHlpApi = GetModuleHandleW(L"iphlpapi.dll");
32 if (!hIpHlpApi)
33 {
34 skip("Failed to load iphlpapi.dll\n");
35 return;
36 }
37
38 pAllocateAndGetTcpExTable2FromStack = (void *)GetProcAddress(hIpHlpApi, "AllocateAndGetTcpExTable2FromStack");
39 if (pAllocateAndGetTcpExTable2FromStack == NULL)
40 {
41 skip("AllocateAndGetTcpExTable2FromStack not found\n");
42 return;
43 }
44
45 if (WSAStartup(MAKEWORD(2, 2), &wsaData) != 0)
46 {
47 skip("Failed to init WS2\n");
48 return;
49 }
50
51 GetSystemTimeAsFileTime(&Creation);
52 CreationTime.LowPart = Creation.dwLowDateTime;
53 CreationTime.HighPart = Creation.dwHighDateTime;
54
55 sock = socket(AF_INET, SOCK_STREAM, 0);
56 if (sock == INVALID_SOCKET)
57 {
58 skip("Cannot create socket\n");
59 goto quit;
60 }
61
62 ZeroMemory(&server, sizeof(SOCKADDR_IN));
63 server.sin_family = AF_INET;
64 server.sin_addr.s_addr = htonl(INADDR_ANY);
65 server.sin_port = htons(9876);
66
67 if (bind(sock, (SOCKADDR*)&server, sizeof(SOCKADDR_IN)) == SOCKET_ERROR)
68 {
69 skip("Cannot bind socket\n");
70 goto quit2;
71 }
72
73 if (listen(sock, SOMAXCONN) == SOCKET_ERROR)
74 {
75 skip("Cannot listen on socket\n");
76 goto quit2;
77 }
78
79 if (pAllocateAndGetTcpExTable2FromStack((PVOID *)&TcpTable, TRUE, GetProcessHeap(), 0, AF_INET, TCP_TABLE_BASIC_ALL) == ERROR_SUCCESS)
80 {
81 ok(TcpTable->dwNumEntries > 0, "No TCP connections?!\n");
82
83 Found = FALSE;
84 for (i = 0; i < TcpTable->dwNumEntries; ++i)
85 {
86 if (TcpTable->table[i].dwState == MIB_TCP_STATE_LISTEN &&
87 TcpTable->table[i].dwLocalAddr == 0 &&
88 TcpTable->table[i].dwLocalPort == htons(9876) &&
89 TcpTable->table[i].dwRemoteAddr == 0)
90 {
91 Found = TRUE;
92 break;
93 }
94 }
95 ok(Found, "Our socket wasn't found!\n");
96
97 HeapFree(GetProcessHeap(), 0, TcpTable);
98 }
99 else
100 {
101 skip("AllocateAndGetTcpExTable2FromStack failure\n");
102 }
103
104 if (pAllocateAndGetTcpExTable2FromStack((PVOID *)&TcpTable, TRUE, GetProcessHeap(), 0, AF_INET, TCP_TABLE_BASIC_CONNECTIONS) == ERROR_SUCCESS)
105 {
106 Found = FALSE;
107 for (i = 0; i < TcpTable->dwNumEntries; ++i)
108 {
109 if (TcpTable->table[i].dwState == MIB_TCP_STATE_LISTEN &&
110 TcpTable->table[i].dwLocalAddr == 0 &&
111 TcpTable->table[i].dwLocalPort == htons(9876) &&
112 TcpTable->table[i].dwRemoteAddr == 0)
113 {
114 Found = TRUE;
115 break;
116 }
117 }
118 ok(Found == FALSE, "Our socket was found!\n");
119
120 HeapFree(GetProcessHeap(), 0, TcpTable);
121 }
122 else
123 {
124 skip("AllocateAndGetTcpExTable2FromStack failure\n");
125 }
126
127 if (pAllocateAndGetTcpExTable2FromStack((PVOID *)&TcpTable, TRUE, GetProcessHeap(), 0, AF_INET, TCP_TABLE_BASIC_LISTENER) == ERROR_SUCCESS)
128 {
129 ok(TcpTable->dwNumEntries > 0, "No TCP connections?!\n");
130
131 Found = FALSE;
132 for (i = 0; i < TcpTable->dwNumEntries; ++i)
133 {
134 if (TcpTable->table[i].dwState == MIB_TCP_STATE_LISTEN &&
135 TcpTable->table[i].dwLocalAddr == 0 &&
136 TcpTable->table[i].dwLocalPort == htons(9876) &&
137 TcpTable->table[i].dwRemoteAddr == 0)
138 {
139 Found = TRUE;
140 break;
141 }
142 }
143 ok(Found, "Our socket wasn't found!\n");
144
145 HeapFree(GetProcessHeap(), 0, TcpTable);
146 }
147 else
148 {
149 skip("AllocateAndGetTcpExTable2FromStack failure\n");
150 }
151
152 if (pAllocateAndGetTcpExTable2FromStack((PVOID *)&TcpTableOwner, TRUE, GetProcessHeap(), 0, AF_INET, TCP_TABLE_OWNER_PID_ALL) == ERROR_SUCCESS)
153 {
154 ok(TcpTableOwner->dwNumEntries > 0, "No TCP connections?!\n");
155
156 Found = FALSE;
157 for (i = 0; i < TcpTableOwner->dwNumEntries; ++i)
158 {
159 if (TcpTableOwner->table[i].dwState == MIB_TCP_STATE_LISTEN &&
160 TcpTableOwner->table[i].dwLocalAddr == 0 &&
161 TcpTableOwner->table[i].dwLocalPort == htons(9876) &&
162 TcpTableOwner->table[i].dwRemoteAddr == 0)
163 {
164 Found = TRUE;
165 break;
166 }
167 }
168
169 if (!Found)
170 {
171 skip("Our socket wasn't found!\n");
172 }
173 else
174 {
175 ok(TcpTableOwner->table[i].dwOwningPid == Pid, "Invalid owner\n");
176 }
177
178 HeapFree(GetProcessHeap(), 0, TcpTableOwner);
179 }
180 else
181 {
182 skip("AllocateAndGetTcpExTable2FromStack failure\n");
183 }
184
185 if (pAllocateAndGetTcpExTable2FromStack((PVOID *)&TcpTableOwner, TRUE, GetProcessHeap(), 0, AF_INET, TCP_TABLE_OWNER_PID_CONNECTIONS) == ERROR_SUCCESS)
186 {
187 Found = FALSE;
188 for (i = 0; i < TcpTableOwner->dwNumEntries; ++i)
189 {
190 if (TcpTableOwner->table[i].dwState == MIB_TCP_STATE_LISTEN &&
191 TcpTableOwner->table[i].dwLocalAddr == 0 &&
192 TcpTableOwner->table[i].dwLocalPort == htons(9876) &&
193 TcpTableOwner->table[i].dwRemoteAddr == 0)
194 {
195 Found = TRUE;
196 break;
197 }
198 }
199 ok(Found == FALSE, "Our socket was found!\n");
200
201 HeapFree(GetProcessHeap(), 0, TcpTableOwner);
202 }
203 else
204 {
205 skip("AllocateAndGetTcpExTable2FromStack failure\n");
206 }
207
208 if (pAllocateAndGetTcpExTable2FromStack((PVOID *)&TcpTableOwner, TRUE, GetProcessHeap(), 0, AF_INET, TCP_TABLE_OWNER_PID_LISTENER) == ERROR_SUCCESS)
209 {
210 ok(TcpTableOwner->dwNumEntries > 0, "No TCP connections?!\n");
211
212 Found = FALSE;
213 for (i = 0; i < TcpTableOwner->dwNumEntries; ++i)
214 {
215 if (TcpTableOwner->table[i].dwState == MIB_TCP_STATE_LISTEN &&
216 TcpTableOwner->table[i].dwLocalAddr == 0 &&
217 TcpTableOwner->table[i].dwLocalPort == htons(9876) &&
218 TcpTableOwner->table[i].dwRemoteAddr == 0)
219 {
220 Found = TRUE;
221 break;
222 }
223 }
224
225 if (!Found)
226 {
227 skip("Our socket wasn't found!\n");
228 }
229 else
230 {
231 ok(TcpTableOwner->table[i].dwOwningPid == Pid, "Invalid owner\n");
232 }
233
234 HeapFree(GetProcessHeap(), 0, TcpTableOwner);
235 }
236 else
237 {
238 skip("AllocateAndGetTcpExTable2FromStack failure\n");
239 }
240
241 if (pAllocateAndGetTcpExTable2FromStack((PVOID *)&TcpTableOwnerMod, TRUE, GetProcessHeap(), 0, AF_INET, TCP_TABLE_OWNER_MODULE_ALL) == ERROR_SUCCESS)
242 {
243 ok(TcpTableOwnerMod->dwNumEntries > 0, "No TCP connections?!\n");
244
245 Found = FALSE;
246 for (i = 0; i < TcpTableOwnerMod->dwNumEntries; ++i)
247 {
248 if (TcpTableOwnerMod->table[i].dwState == MIB_TCP_STATE_LISTEN &&
249 TcpTableOwnerMod->table[i].dwLocalAddr == 0 &&
250 TcpTableOwnerMod->table[i].dwLocalPort == htons(9876) &&
251 TcpTableOwnerMod->table[i].dwRemoteAddr == 0)
252 {
253 Found = TRUE;
254 break;
255 }
256 }
257
258 if (!Found)
259 {
260 skip("Our socket wasn't found!\n");
261 }
262 else
263 {
264 ok(TcpTableOwnerMod->table[i].dwOwningPid == Pid, "Invalid owner\n");
265
266 ok(TcpTableOwnerMod->table[i].liCreateTimestamp.QuadPart >= CreationTime.QuadPart, "Invalid time\n");
267 ok(TcpTableOwnerMod->table[i].liCreateTimestamp.QuadPart <= CreationTime.QuadPart + 60000000000LL, "Invalid time\n");
268 }
269
270 HeapFree(GetProcessHeap(), 0, TcpTableOwnerMod);
271 }
272 else
273 {
274 skip("AllocateAndGetTcpExTable2FromStack failure\n");
275 }
276
277 if (pAllocateAndGetTcpExTable2FromStack((PVOID *)&TcpTableOwnerMod, TRUE, GetProcessHeap(), 0, AF_INET, TCP_TABLE_OWNER_MODULE_CONNECTIONS) == ERROR_SUCCESS)
278 {
279 Found = FALSE;
280 for (i = 0; i < TcpTableOwnerMod->dwNumEntries; ++i)
281 {
282 if (TcpTableOwnerMod->table[i].dwState == MIB_TCP_STATE_LISTEN &&
283 TcpTableOwnerMod->table[i].dwLocalAddr == 0 &&
284 TcpTableOwnerMod->table[i].dwLocalPort == htons(9876) &&
285 TcpTableOwnerMod->table[i].dwRemoteAddr == 0)
286 {
287 Found = TRUE;
288 break;
289 }
290 }
291 ok(Found == FALSE, "Our socket was found!\n");
292
293 HeapFree(GetProcessHeap(), 0, TcpTableOwnerMod);
294 }
295 else
296 {
297 skip("AllocateAndGetTcpExTable2FromStack failure\n");
298 }
299
300 if (pAllocateAndGetTcpExTable2FromStack((PVOID *)&TcpTableOwnerMod, TRUE, GetProcessHeap(), 0, AF_INET, TCP_TABLE_OWNER_MODULE_LISTENER) == ERROR_SUCCESS)
301 {
302 ok(TcpTableOwnerMod->dwNumEntries > 0, "No TCP connections?!\n");
303
304 Found = FALSE;
305 for (i = 0; i < TcpTableOwnerMod->dwNumEntries; ++i)
306 {
307 if (TcpTableOwnerMod->table[i].dwState == MIB_TCP_STATE_LISTEN &&
308 TcpTableOwnerMod->table[i].dwLocalAddr == 0 &&
309 TcpTableOwnerMod->table[i].dwLocalPort == htons(9876) &&
310 TcpTableOwnerMod->table[i].dwRemoteAddr == 0)
311 {
312 Found = TRUE;
313 break;
314 }
315 }
316
317 if (!Found)
318 {
319 skip("Our socket wasn't found!\n");
320 }
321 else
322 {
323 ok(TcpTableOwnerMod->table[i].dwOwningPid == Pid, "Invalid owner\n");
324
325 ok(TcpTableOwnerMod->table[i].liCreateTimestamp.QuadPart >= CreationTime.QuadPart, "Invalid time\n");
326 ok(TcpTableOwnerMod->table[i].liCreateTimestamp.QuadPart <= CreationTime.QuadPart + 60000000000LL, "Invalid time\n");
327 }
328
329 HeapFree(GetProcessHeap(), 0, TcpTableOwnerMod);
330 }
331 else
332 {
333 skip("AllocateAndGetTcpExTable2FromStack failure\n");
334 }
335
336 quit2:
337 closesocket(sock);
338 quit:
339 WSACleanup();
340 }