1 /*
2  * PROJECT:     ReactOS API Tests
3  * LICENSE:     GPL-2.0+ (https://spdx.org/licenses/GPL-2.0+)
4  * PURPOSE:     Tests for UDP connections owner 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 GetExtendedUdpTableWithAlloc(PVOID *UdpTable, BOOL Order, DWORD Family, UDP_TABLE_CLASS Class)
15 {
16     DWORD ret;
17     DWORD Size = 0;
18 
19     *UdpTable = NULL;
20 
21     ret = GetExtendedUdpTable(*UdpTable, &Size, Order, Family, Class, 0);
22     if (ret == ERROR_INSUFFICIENT_BUFFER)
23     {
24         *UdpTable = HeapAlloc(GetProcessHeap(), 0, Size);
25         if (*UdpTable == NULL)
26         {
27             return ERROR_OUTOFMEMORY;
28         }
29 
30         ret = GetExtendedUdpTable(*UdpTable, &Size, Order, Family, Class, 0);
31         if (ret != NO_ERROR)
32         {
33             HeapFree(GetProcessHeap(), 0, *UdpTable);
34             *UdpTable = NULL;
35         }
36     }
37 
38     return ret;
39 }
40 
41 START_TEST(GetOwnerModuleFromUdpEntry)
42 {
43     WSADATA wsaData;
44     SOCKET sock;
45     SOCKADDR_IN server;
46     PMIB_UDPTABLE_OWNER_MODULE UdpTableOwnerMod;
47     DWORD i;
48     BOOLEAN Found;
49     FILETIME Creation;
50     LARGE_INTEGER CreationTime;
51     DWORD Pid = GetCurrentProcessId();
52 
53     if (WSAStartup(MAKEWORD(2, 2), &wsaData) != 0)
54     {
55         skip("Failed to init WS2\n");
56         return;
57     }
58 
59     GetSystemTimeAsFileTime(&Creation);
60     CreationTime.LowPart = Creation.dwLowDateTime;
61     CreationTime.HighPart = Creation.dwHighDateTime;
62 
63     sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
64     if (sock == INVALID_SOCKET)
65     {
66         skip("Cannot create socket\n");
67         goto quit;
68     }
69 
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);
74 
75     if (bind(sock, (SOCKADDR*)&server, sizeof(SOCKADDR_IN)) == SOCKET_ERROR)
76     {
77         skip("Cannot bind socket\n");
78         goto quit2;
79     }
80 
81     if (GetExtendedUdpTableWithAlloc((PVOID *)&UdpTableOwnerMod, TRUE, AF_INET, UDP_TABLE_OWNER_MODULE) == ERROR_SUCCESS)
82     {
83         ok(UdpTableOwnerMod->dwNumEntries > 0, "No UDP connections?!\n");
84 
85         Found = FALSE;
86         for (i = 0; i < UdpTableOwnerMod->dwNumEntries; ++i)
87         {
88             if (UdpTableOwnerMod->table[i].dwLocalAddr == 0 &&
89                 UdpTableOwnerMod->table[i].dwLocalPort == htons(9876))
90             {
91                 Found = TRUE;
92                 break;
93             }
94         }
95 
96         if (!Found)
97         {
98             skip("Our socket wasn't found!\n");
99         }
100         else
101         {
102             DWORD Size = 0;
103             PTCPIP_OWNER_MODULE_BASIC_INFO BasicInfo = NULL;
104 
105             ok(UdpTableOwnerMod->table[i].dwOwningPid == Pid, "Invalid owner\n");
106 
107             ok(UdpTableOwnerMod->table[i].liCreateTimestamp.QuadPart >= CreationTime.QuadPart, "Invalid time\n");
108             ok(UdpTableOwnerMod->table[i].liCreateTimestamp.QuadPart <= CreationTime.QuadPart + 60000000000LL, "Invalid time\n");
109 
110             if (GetOwnerModuleFromUdpEntry(&UdpTableOwnerMod->table[i], TCPIP_OWNER_MODULE_INFO_BASIC, BasicInfo, &Size) == ERROR_INSUFFICIENT_BUFFER)
111             {
112                 BasicInfo = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, Size);
113                 ok(BasicInfo != NULL, "HeapAlloc failed\n");
114 
115                 if (GetOwnerModuleFromUdpEntry(&UdpTableOwnerMod->table[i], TCPIP_OWNER_MODULE_INFO_BASIC, BasicInfo, &Size) == ERROR_SUCCESS)
116                 {
117                     WCHAR CurrentModule[MAX_PATH];
118                     PWSTR FileName;
119 
120                     if (GetModuleFileNameW(NULL, CurrentModule, MAX_PATH) != 0)
121                     {
122                         FileName = wcsrchr(CurrentModule, L'\\');
123                         ++FileName;
124 
125                         ok(_wcsicmp(CurrentModule, BasicInfo->pModulePath) == 0, "Mismatching names (%S, %S)\n", CurrentModule, BasicInfo->pModulePath);
126                         ok(_wcsicmp(FileName, BasicInfo->pModuleName) == 0, "Mismatching names (%S, %S)\n", FileName, BasicInfo->pModuleName);
127                     }
128                     else
129                     {
130                         skip("GetModuleFileNameW failed\n");
131                     }
132                 }
133                 else
134                 {
135                     skip("GetOwnerModuleFromTcpEntry failed\n");
136                 }
137             }
138             else
139             {
140                 skip("GetOwnerModuleFromTcpEntry failed\n");
141             }
142         }
143 
144         HeapFree(GetProcessHeap(), 0, UdpTableOwnerMod);
145     }
146     else
147     {
148         skip("GetExtendedUdpTableWithAlloc failure\n");
149     }
150 
151 quit2:
152     closesocket(sock);
153 quit:
154     WSACleanup();
155 }
156