[IPHLPAPI_APITEST] Add tests for AllocateAndGetTcpExTable2FromStack()
[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 SYSTEMTIME Creation;
28 DWORD Pid = GetCurrentProcessId();
29
30 hIpHlpApi = GetModuleHandleW(L"iphlpapi.dll");
31 if (!hIpHlpApi)
32 {
33 skip("Failed to load iphlpapi.dll\n");
34 return;
35 }
36
37 pAllocateAndGetTcpExTable2FromStack = (void *)GetProcAddress(hIpHlpApi, "AllocateAndGetTcpExTable2FromStack");
38 if (pAllocateAndGetTcpExTable2FromStack == NULL)
39 {
40 skip("AllocateAndGetTcpExTable2FromStack not found\n");
41 return;
42 }
43
44 if (WSAStartup(MAKEWORD(2, 2), &wsaData) != 0)
45 {
46 skip("Failed to init WS2\n");
47 return;
48 }
49
50 GetSystemTime(&Creation);
51
52 sock = socket(AF_INET, SOCK_STREAM, 0);
53 if (sock == INVALID_SOCKET)
54 {
55 skip("Cannot create socket\n");
56 goto quit;
57 }
58
59 ZeroMemory(&server, sizeof(SOCKADDR_IN));
60 server.sin_family = AF_INET;
61 server.sin_addr.s_addr = htonl(INADDR_ANY);
62 server.sin_port = htons(9876);
63
64 if (bind(sock, (SOCKADDR*)&server, sizeof(SOCKADDR_IN)) == SOCKET_ERROR)
65 {
66 skip("Cannot bind socket\n");
67 goto quit2;
68 }
69
70 if (listen(sock, SOMAXCONN) == SOCKET_ERROR)
71 {
72 skip("Cannot listen on socket\n");
73 goto quit2;
74 }
75
76 if (pAllocateAndGetTcpExTable2FromStack((PVOID *)&TcpTable, TRUE, GetProcessHeap(), 0, AF_INET, TCP_TABLE_BASIC_ALL) == ERROR_SUCCESS)
77 {
78 ok(TcpTable->dwNumEntries > 0, "No TCP connections?!\n");
79
80 Found = FALSE;
81 for (i = 0; i < TcpTable->dwNumEntries; ++i)
82 {
83 if (TcpTable->table[i].dwState == MIB_TCP_STATE_LISTEN &&
84 TcpTable->table[i].dwLocalAddr == 0 &&
85 TcpTable->table[i].dwLocalPort == htons(9876) &&
86 TcpTable->table[i].dwRemoteAddr == 0)
87 {
88 Found = TRUE;
89 break;
90 }
91 }
92 ok(Found, "Our socket wasn't found!\n");
93
94 HeapFree(GetProcessHeap(), 0, TcpTable);
95 }
96 else
97 {
98 skip("AllocateAndGetTcpExTable2FromStack failure\n");
99 }
100
101 if (pAllocateAndGetTcpExTable2FromStack((PVOID *)&TcpTable, TRUE, GetProcessHeap(), 0, AF_INET, TCP_TABLE_BASIC_CONNECTIONS) == ERROR_SUCCESS)
102 {
103 Found = FALSE;
104 for (i = 0; i < TcpTable->dwNumEntries; ++i)
105 {
106 if (TcpTable->table[i].dwState == MIB_TCP_STATE_LISTEN &&
107 TcpTable->table[i].dwLocalAddr == 0 &&
108 TcpTable->table[i].dwLocalPort == htons(9876) &&
109 TcpTable->table[i].dwRemoteAddr == 0)
110 {
111 Found = TRUE;
112 break;
113 }
114 }
115 ok(Found == FALSE, "Our socket was found!\n");
116
117 HeapFree(GetProcessHeap(), 0, TcpTable);
118 }
119 else
120 {
121 skip("AllocateAndGetTcpExTable2FromStack failure\n");
122 }
123
124 if (pAllocateAndGetTcpExTable2FromStack((PVOID *)&TcpTable, TRUE, GetProcessHeap(), 0, AF_INET, TCP_TABLE_BASIC_LISTENER) == ERROR_SUCCESS)
125 {
126 ok(TcpTable->dwNumEntries > 0, "No TCP connections?!\n");
127
128 Found = FALSE;
129 for (i = 0; i < TcpTable->dwNumEntries; ++i)
130 {
131 if (TcpTable->table[i].dwState == MIB_TCP_STATE_LISTEN &&
132 TcpTable->table[i].dwLocalAddr == 0 &&
133 TcpTable->table[i].dwLocalPort == htons(9876) &&
134 TcpTable->table[i].dwRemoteAddr == 0)
135 {
136 Found = TRUE;
137 break;
138 }
139 }
140 ok(Found, "Our socket wasn't found!\n");
141
142 HeapFree(GetProcessHeap(), 0, TcpTable);
143 }
144 else
145 {
146 skip("AllocateAndGetTcpExTable2FromStack failure\n");
147 }
148
149 if (pAllocateAndGetTcpExTable2FromStack((PVOID *)&TcpTableOwner, TRUE, GetProcessHeap(), 0, AF_INET, TCP_TABLE_OWNER_PID_ALL) == ERROR_SUCCESS)
150 {
151 ok(TcpTableOwner->dwNumEntries > 0, "No TCP connections?!\n");
152
153 Found = FALSE;
154 for (i = 0; i < TcpTableOwner->dwNumEntries; ++i)
155 {
156 if (TcpTableOwner->table[i].dwState == MIB_TCP_STATE_LISTEN &&
157 TcpTableOwner->table[i].dwLocalAddr == 0 &&
158 TcpTableOwner->table[i].dwLocalPort == htons(9876) &&
159 TcpTableOwner->table[i].dwRemoteAddr == 0)
160 {
161 Found = TRUE;
162 break;
163 }
164 }
165
166 if (!Found)
167 {
168 skip("Our socket wasn't found!\n");
169 }
170 else
171 {
172 ok(TcpTableOwner->table[i].dwOwningPid == Pid, "Invalid owner\n");
173 }
174
175 HeapFree(GetProcessHeap(), 0, TcpTableOwner);
176 }
177 else
178 {
179 skip("AllocateAndGetTcpExTable2FromStack failure\n");
180 }
181
182 if (pAllocateAndGetTcpExTable2FromStack((PVOID *)&TcpTableOwner, TRUE, GetProcessHeap(), 0, AF_INET, TCP_TABLE_OWNER_PID_CONNECTIONS) == ERROR_SUCCESS)
183 {
184 Found = FALSE;
185 for (i = 0; i < TcpTableOwner->dwNumEntries; ++i)
186 {
187 if (TcpTableOwner->table[i].dwState == MIB_TCP_STATE_LISTEN &&
188 TcpTableOwner->table[i].dwLocalAddr == 0 &&
189 TcpTableOwner->table[i].dwLocalPort == htons(9876) &&
190 TcpTableOwner->table[i].dwRemoteAddr == 0)
191 {
192 Found = TRUE;
193 break;
194 }
195 }
196 ok(Found == FALSE, "Our socket was found!\n");
197
198 HeapFree(GetProcessHeap(), 0, TcpTableOwner);
199 }
200 else
201 {
202 skip("AllocateAndGetTcpExTable2FromStack failure\n");
203 }
204
205 if (pAllocateAndGetTcpExTable2FromStack((PVOID *)&TcpTableOwner, TRUE, GetProcessHeap(), 0, AF_INET, TCP_TABLE_OWNER_PID_LISTENER) == ERROR_SUCCESS)
206 {
207 ok(TcpTableOwner->dwNumEntries > 0, "No TCP connections?!\n");
208
209 Found = FALSE;
210 for (i = 0; i < TcpTableOwner->dwNumEntries; ++i)
211 {
212 if (TcpTableOwner->table[i].dwState == MIB_TCP_STATE_LISTEN &&
213 TcpTableOwner->table[i].dwLocalAddr == 0 &&
214 TcpTableOwner->table[i].dwLocalPort == htons(9876) &&
215 TcpTableOwner->table[i].dwRemoteAddr == 0)
216 {
217 Found = TRUE;
218 break;
219 }
220 }
221
222 if (!Found)
223 {
224 skip("Our socket wasn't found!\n");
225 }
226 else
227 {
228 ok(TcpTableOwner->table[i].dwOwningPid == Pid, "Invalid owner\n");
229 }
230
231 HeapFree(GetProcessHeap(), 0, TcpTableOwner);
232 }
233 else
234 {
235 skip("AllocateAndGetTcpExTable2FromStack failure\n");
236 }
237
238 if (pAllocateAndGetTcpExTable2FromStack((PVOID *)&TcpTableOwnerMod, TRUE, GetProcessHeap(), 0, AF_INET, TCP_TABLE_OWNER_MODULE_ALL) == ERROR_SUCCESS)
239 {
240 ok(TcpTableOwnerMod->dwNumEntries > 0, "No TCP connections?!\n");
241
242 Found = FALSE;
243 for (i = 0; i < TcpTableOwnerMod->dwNumEntries; ++i)
244 {
245 if (TcpTableOwnerMod->table[i].dwState == MIB_TCP_STATE_LISTEN &&
246 TcpTableOwnerMod->table[i].dwLocalAddr == 0 &&
247 TcpTableOwnerMod->table[i].dwLocalPort == htons(9876) &&
248 TcpTableOwnerMod->table[i].dwRemoteAddr == 0)
249 {
250 Found = TRUE;
251 break;
252 }
253 }
254
255 if (!Found)
256 {
257 skip("Our socket wasn't found!\n");
258 }
259 else
260 {
261 SYSTEMTIME SockTime;
262
263 ok(TcpTableOwnerMod->table[i].dwOwningPid == Pid, "Invalid owner\n");
264
265 CopyMemory(&SockTime, &TcpTableOwnerMod->table[i].liCreateTimestamp, sizeof(SYSTEMTIME));
266 ok(Creation.wYear == SockTime.wYear, "Invalid year\n");
267 ok(Creation.wMonth == SockTime.wMonth, "Invalid month\n");
268 ok(Creation.wDayOfWeek == SockTime.wDayOfWeek, "Invalid day of week\n");
269 ok(Creation.wDay == SockTime.wDay, "Invalid day\n");
270 ok(Creation.wHour == SockTime.wHour, "Invalid hour\n");
271 ok(Creation.wMinute == SockTime.wMinute, "Invalid minute\n");
272 }
273
274 HeapFree(GetProcessHeap(), 0, TcpTableOwnerMod);
275 }
276 else
277 {
278 skip("AllocateAndGetTcpExTable2FromStack failure\n");
279 }
280
281 if (pAllocateAndGetTcpExTable2FromStack((PVOID *)&TcpTableOwnerMod, TRUE, GetProcessHeap(), 0, AF_INET, TCP_TABLE_OWNER_MODULE_CONNECTIONS) == ERROR_SUCCESS)
282 {
283 Found = FALSE;
284 for (i = 0; i < TcpTableOwnerMod->dwNumEntries; ++i)
285 {
286 if (TcpTableOwnerMod->table[i].dwState == MIB_TCP_STATE_LISTEN &&
287 TcpTableOwnerMod->table[i].dwLocalAddr == 0 &&
288 TcpTableOwnerMod->table[i].dwLocalPort == htons(9876) &&
289 TcpTableOwnerMod->table[i].dwRemoteAddr == 0)
290 {
291 Found = TRUE;
292 break;
293 }
294 }
295 ok(Found == FALSE, "Our socket was found!\n");
296
297 HeapFree(GetProcessHeap(), 0, TcpTableOwnerMod);
298 }
299 else
300 {
301 skip("AllocateAndGetTcpExTable2FromStack failure\n");
302 }
303
304 if (pAllocateAndGetTcpExTable2FromStack((PVOID *)&TcpTableOwnerMod, TRUE, GetProcessHeap(), 0, AF_INET, TCP_TABLE_OWNER_MODULE_LISTENER) == ERROR_SUCCESS)
305 {
306 ok(TcpTableOwnerMod->dwNumEntries > 0, "No TCP connections?!\n");
307
308 Found = FALSE;
309 for (i = 0; i < TcpTableOwnerMod->dwNumEntries; ++i)
310 {
311 if (TcpTableOwnerMod->table[i].dwState == MIB_TCP_STATE_LISTEN &&
312 TcpTableOwnerMod->table[i].dwLocalAddr == 0 &&
313 TcpTableOwnerMod->table[i].dwLocalPort == htons(9876) &&
314 TcpTableOwnerMod->table[i].dwRemoteAddr == 0)
315 {
316 Found = TRUE;
317 break;
318 }
319 }
320
321 if (!Found)
322 {
323 skip("Our socket wasn't found!\n");
324 }
325 else
326 {
327 SYSTEMTIME SockTime;
328
329 ok(TcpTableOwnerMod->table[i].dwOwningPid == Pid, "Invalid owner\n");
330
331 CopyMemory(&SockTime, &TcpTableOwnerMod->table[i].liCreateTimestamp, sizeof(SYSTEMTIME));
332 ok(Creation.wYear == SockTime.wYear, "Invalid year\n");
333 ok(Creation.wMonth == SockTime.wMonth, "Invalid month\n");
334 ok(Creation.wDayOfWeek == SockTime.wDayOfWeek, "Invalid day of week\n");
335 ok(Creation.wDay == SockTime.wDay, "Invalid day\n");
336 ok(Creation.wHour == SockTime.wHour, "Invalid hour\n");
337 ok(Creation.wMinute == SockTime.wMinute, "Invalid minute\n");
338 }
339
340 HeapFree(GetProcessHeap(), 0, TcpTableOwnerMod);
341 }
342 else
343 {
344 skip("AllocateAndGetTcpExTable2FromStack failure\n");
345 }
346
347 quit2:
348 closesocket(sock);
349 quit:
350 WSACleanup();
351 }