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     DWORD Error;
34 
35     LoadTcpIpTestDriver();
36 
37     Error = KmtSendToDriver(IOCTL_TEST_TDI);
38     ok_eq_ulong(Error, ERROR_SUCCESS);
39 
40     UnloadTcpIpTestDriver();
41 }
42 
43 static
44 DWORD
45 WINAPI
46 AcceptProc(
47     _In_ LPVOID Parameter)
48 {
49     WORD WinsockVersion;
50     WSADATA WsaData;
51     int Error;
52     SOCKET ListenSocket, AcceptSocket;
53     struct sockaddr_in ListenAddress, AcceptAddress;
54     int AcceptAddressLength;
55     HANDLE ReadyToConnectEvent = (HANDLE)Parameter;
56 
57     /* Initialize winsock */
58     WinsockVersion = MAKEWORD(2, 0);
59     Error = WSAStartup(WinsockVersion, &WsaData);
60     ok_eq_int(Error, 0);
61 
62     ListenSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
63     ok(ListenSocket != INVALID_SOCKET, "socket failed\n");
64 
65     ZeroMemory(&ListenAddress, sizeof(ListenAddress));
66     ListenAddress.sin_addr.S_un.S_addr = inet_addr("127.0.0.1");
67     ListenAddress.sin_port = htons(TEST_CONNECT_SERVER_PORT);
68     ListenAddress.sin_family = AF_INET;
69 
70     Error = bind(ListenSocket, (struct sockaddr*)&ListenAddress, sizeof(ListenAddress));
71     ok_eq_int(Error, 0);
72 
73     Error = listen(ListenSocket, 1);
74     ok_eq_int(Error, 0);
75 
76     SetEvent(ReadyToConnectEvent);
77 
78     AcceptAddressLength = sizeof(AcceptAddress);
79     AcceptSocket = accept(ListenSocket, (struct sockaddr*)&AcceptAddress, &AcceptAddressLength);
80     ok(AcceptSocket != INVALID_SOCKET, "accept failed\n");
81     ok_eq_long(AcceptAddressLength, sizeof(AcceptAddress));
82     ok_eq_hex(AcceptAddress.sin_addr.S_un.S_addr, inet_addr("127.0.0.1"));
83     ok_eq_hex(AcceptAddress.sin_port, htons(TEST_CONNECT_CLIENT_PORT));
84 
85     return 0;
86 }
87 
88 START_TEST(TcpIpConnect)
89 {
90     HANDLE AcceptThread;
91     HANDLE ReadyToConnectEvent;
92     DWORD Error;
93 
94     ReadyToConnectEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
95     ok(ReadyToConnectEvent != NULL, "CreateEvent failed\n");
96 
97     AcceptThread = CreateThread(NULL, 0, AcceptProc, (PVOID)ReadyToConnectEvent, 0, NULL);
98     ok(AcceptThread != NULL, "CreateThread failed\n");
99 
100     WaitForSingleObject(ReadyToConnectEvent, INFINITE);
101 
102     LoadTcpIpTestDriver();
103 
104     Error = KmtSendToDriver(IOCTL_TEST_CONNECT);
105     ok_eq_ulong(Error, ERROR_SUCCESS);
106 
107     WaitForSingleObject(AcceptThread, INFINITE);
108 
109     UnloadTcpIpTestDriver();
110 
111     WSACleanup();
112 }
113