1 /*
2  * PROJECT:         ReactOS kernel-mode tests
3  * LICENSE:         GPLv2+ - See COPYING in the top level directory
4  * PURPOSE:         User mode part of the TcpIp.sys test suite
5  * PROGRAMMER:      Jérôme Gardou <jerome.gardou@reactos.org>
6  */
7 
8 #include <kmt_test.h>
9 #include <winsock2.h>
10 
11 #include "tcpip.h"
12 
13 static
14 void
15 LoadTcpIpTestDriver(void)
16 {
17     /* Start the special-purpose driver */
18     KmtLoadDriver(L"TcpIp", FALSE);
19     KmtOpenDriver();
20 }
21 
22 static
23 void
24 UnloadTcpIpTestDriver(void)
25 {
26     /* Stop the driver. */
27     KmtCloseDriver();
28     KmtUnloadDriver();
29 }
30 
31 START_TEST(TcpIpTdi)
32 {
33     LoadTcpIpTestDriver();
34 
35     ok(KmtSendToDriver(IOCTL_TEST_TDI) == ERROR_SUCCESS, "\n");
36 
37     UnloadTcpIpTestDriver();
38 }
39 
40 static
41 DWORD
42 WINAPI
43 AcceptProc(
44     _In_ LPVOID Parameter)
45 {
46     WORD WinsockVersion;
47     WSADATA WsaData;
48     int Error;
49     SOCKET ListenSocket, AcceptSocket;
50     struct sockaddr_in ListenAddress, AcceptAddress;
51     int AcceptAddressLength;
52     HANDLE ReadyToConnectEvent = (HANDLE)Parameter;
53 
54     /* Initialize winsock */
55     WinsockVersion = MAKEWORD(2, 0);
56     Error = WSAStartup(WinsockVersion, &WsaData);
57     ok(Error == 0, "");
58 
59     ListenSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
60     ok_bool_true(ListenSocket != INVALID_SOCKET, "socket failed");
61 
62     ZeroMemory(&ListenAddress, sizeof(ListenAddress));
63     ListenAddress.sin_addr.S_un.S_addr = inet_addr("127.0.0.1");
64     ListenAddress.sin_port = htons(TEST_CONNECT_SERVER_PORT);
65     ListenAddress.sin_family = AF_INET;
66 
67     Error = bind(ListenSocket, (struct sockaddr*)&ListenAddress, sizeof(ListenAddress));
68     ok(Error == 0, "");
69 
70     Error = listen(ListenSocket, 1);
71     ok(Error == 0, "");
72 
73     SetEvent(ReadyToConnectEvent);
74 
75     AcceptAddressLength = sizeof(AcceptAddress);
76     AcceptSocket = accept(ListenSocket, (struct sockaddr*)&AcceptAddress, &AcceptAddressLength);
77     ok(AcceptSocket != INVALID_SOCKET, "\n");
78     ok_eq_long(AcceptAddressLength, sizeof(AcceptAddress));
79     ok_eq_hex(AcceptAddress.sin_addr.S_un.S_addr, inet_addr("127.0.0.1"));
80     ok_eq_hex(AcceptAddress.sin_port, ntohs(TEST_CONNECT_CLIENT_PORT));
81 
82     return 0;
83 }
84 
85 START_TEST(TcpIpConnect)
86 {
87     HANDLE AcceptThread;
88     HANDLE ReadyToConnectEvent;
89 
90     ReadyToConnectEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
91     ok(ReadyToConnectEvent != NULL, "\n");
92 
93     AcceptThread = CreateThread(NULL, 0, AcceptProc, (PVOID)ReadyToConnectEvent, 0, NULL);
94     ok(AcceptThread != NULL, "");
95 
96     WaitForSingleObject(ReadyToConnectEvent, INFINITE);
97 
98     LoadTcpIpTestDriver();
99 
100     ok(KmtSendToDriver(IOCTL_TEST_CONNECT) == ERROR_SUCCESS, "\n");
101 
102     WaitForSingleObject(AcceptThread, INFINITE);
103 
104     UnloadTcpIpTestDriver();
105 
106     WSACleanup();
107 }
108