ReactOS 0.4.15-dev-5667-ged97270
connect.c
Go to the documentation of this file.
1/*
2 * PROJECT: ReactOS kernel-mode tests
3 * LICENSE: LGPLv2+ - See COPYING.LIB in the top level directory
4 * PURPOSE: Kernel-Mode Test Suite for TCPIP.sys
5 * PROGRAMMER: Jérôme Gardou <jerome.gardou@reactos.org>
6 */
7
8#include <kmt_test.h>
9#include <tdikrnl.h>
10#include <ndk/rtlfuncs.h>
11
12#include <sys/param.h>
13
14#include "tcpip.h"
15
16#define TAG_TEST 'tseT'
17
18#if BYTE_ORDER == LITTLE_ENDIAN
21{
22 return ((x & 0x00FF) << 8) | ((x & 0xFF00) >> 8);
23}
24#else
25#define htons(x) (x)
26#endif
27
28static
35{
38
40
42}
43
44static
45VOID
47{
48 PIRP Irp;
49 HANDLE AddressHandle, ConnectionHandle;
50 FILE_OBJECT* ConnectionFileObject;
52 UNICODE_STRING TcpDeviceName = RTL_CONSTANT_STRING(L"\\Device\\Tcp");
55 TA_IP_ADDRESS* IpAddress;
56 TA_IP_ADDRESS ConnectAddress, ReturnAddress;
58 IO_STATUS_BLOCK StatusBlock;
59 ULONG FileInfoSize;
60 IN_ADDR InAddr;
61 LPCWSTR AddressTerminator;
62 CONNECTION_CONTEXT ConnectionContext = (CONNECTION_CONTEXT)(ULONG_PTR)0xC0CAC01AC0CAC01AULL;
64 TDI_CONNECTION_INFORMATION RequestInfo, ReturnInfo;
65
66 /* Create a TCP address file */
69 FileInfoSize,
70 TAG_TEST);
71 ok(FileInfo != NULL, "FileInfo is NULL!\n");
72 RtlZeroMemory(FileInfo, FileInfoSize);
73
75 FileInfo->EaValueLength = sizeof(TA_IP_ADDRESS);
77
78 IpAddress = (PTA_IP_ADDRESS)(&FileInfo->EaName[TDI_TRANSPORT_ADDRESS_LENGTH + 1]);
79 IpAddress->TAAddressCount = 1;
80 IpAddress->Address[0].AddressType = TDI_ADDRESS_TYPE_IP;
81 IpAddress->Address[0].AddressLength = TDI_ADDRESS_LENGTH_IP;
82 IpAddress->Address[0].Address[0].sin_port = htons(TEST_CONNECT_CLIENT_PORT);
83 Status = RtlIpv4StringToAddressW(L"127.0.0.1", TRUE, &AddressTerminator, &InAddr);
85 IpAddress->Address[0].Address[0].in_addr = InAddr.S_un.S_addr;
86
88 &TcpDeviceName,
90 NULL,
91 NULL);
92
93 Status = ZwCreateFile(
94 &AddressHandle,
97 &StatusBlock,
98 0,
102 0L,
103 FileInfo,
104 FileInfoSize);
106
108
109 /* Create a TCP connection file */
112 FileInfoSize,
113 TAG_TEST);
114 ok(FileInfo != NULL, "FileInfo is NULL!\n");
115 RtlZeroMemory(FileInfo, FileInfoSize);
116
118 FileInfo->EaValueLength = sizeof(CONNECTION_CONTEXT);
120 *((CONNECTION_CONTEXT*)&FileInfo->EaName[TDI_CONNECTION_CONTEXT_LENGTH + 1]) = ConnectionContext;
121
122 Status = ZwCreateFile(
126 &StatusBlock,
127 0,
131 0L,
132 FileInfo,
133 FileInfoSize);
135
137
138 /* Get the file and device object for the upcoming IRPs */
144 (PVOID*)&ConnectionFileObject,
145 NULL);
147 DeviceObject = IoGetRelatedDeviceObject(ConnectionFileObject);
148 ok(DeviceObject != NULL, "Device object is NULL!\n");
149
150 /* Associate the connection file and the address */
152 Irp = IoAllocateIrp(DeviceObject->StackSize, FALSE);
153 ok(Irp != NULL, "IoAllocateIrp failed.\n");
154
155 TdiBuildAssociateAddress(Irp, DeviceObject, ConnectionFileObject, NULL, NULL, AddressHandle);
157
159 if (Status == STATUS_PENDING)
160 {
161 trace("Associate address IRP is pending.\n");
163 &Event,
164 Executive,
166 FALSE,
167 NULL);
168 Status = Irp->IoStatus.Status;
169 trace("Associate address IRP completed.\n");
170 }
172 IoFreeIrp(Irp);
173
174
176
177 /* Build the connect IRP. */
178 Irp = IoAllocateIrp(DeviceObject->StackSize, FALSE);
179 ok(Irp != NULL, "IoAllocateIrp failed.\n");
180
181 /* Prepare the request */
182 RtlZeroMemory(&RequestInfo, sizeof(RequestInfo));
183 RtlZeroMemory(&ConnectAddress, sizeof(ConnectAddress));
184 RequestInfo.RemoteAddressLength = sizeof(TA_IP_ADDRESS);
185 RequestInfo.RemoteAddress = &ConnectAddress;
186 ConnectAddress.TAAddressCount = 1;
187 ConnectAddress.Address[0].AddressType = TDI_ADDRESS_TYPE_IP;
188 ConnectAddress.Address[0].AddressLength = TDI_ADDRESS_LENGTH_IP;
189 ConnectAddress.Address[0].Address[0].sin_port = htons(TEST_CONNECT_SERVER_PORT);
190 Status = RtlIpv4StringToAddressW(L"127.0.0.1", TRUE, &AddressTerminator, &InAddr);
191 ConnectAddress.Address[0].Address[0].in_addr = InAddr.S_un.S_addr;
192
193 /* See what we will get in exchange */
194 RtlZeroMemory(&ReturnInfo, sizeof(ReturnInfo));
195 RtlZeroMemory(&ReturnAddress, sizeof(ReturnAddress));
196 ReturnInfo.RemoteAddressLength = sizeof(TA_IP_ADDRESS);
197 ReturnInfo.RemoteAddress = &ReturnAddress;
198
201 ConnectionFileObject,
202 NULL,
203 NULL,
204 NULL,
205 &RequestInfo,
206 &ReturnInfo);
208
210 if (Status == STATUS_PENDING)
211 {
212 trace("Connect IRP is pending.\n");
214 &Event,
215 Executive,
217 FALSE,
218 NULL);
219 Status = Irp->IoStatus.Status;
220 trace("Connect IRP completed.\n");
221 }
223 IoFreeIrp(Irp);
224
225 /* The IRP doesn't touch the return info */
226 ok_eq_long(ReturnInfo.RemoteAddressLength, sizeof(TA_IP_ADDRESS));
227 ok_eq_pointer(ReturnInfo.RemoteAddress, &ReturnAddress);
228 ok_eq_long(ReturnInfo.OptionsLength, 0);
229 ok_eq_pointer(ReturnInfo.Options, NULL);
230 ok_eq_long(ReturnInfo.UserDataLength, 0);
231 ok_eq_pointer(ReturnInfo.UserData, NULL);
232
233 ok_eq_long(ReturnAddress.TAAddressCount, 1);
234 ok_eq_hex(ReturnAddress.Address[0].AddressType, TDI_ADDRESS_TYPE_IP);
235 ok_eq_hex(ReturnAddress.Address[0].AddressLength, TDI_ADDRESS_LENGTH_IP);
236 ok_eq_hex(ReturnAddress.Address[0].Address[0].sin_port, htons(TEST_CONNECT_SERVER_PORT));
237 ok_eq_hex(ReturnAddress.Address[0].Address[0].in_addr, InAddr.S_un.S_addr);
238
239 ObDereferenceObject(ConnectionFileObject);
240
242 ZwClose(AddressHandle);
243}
244
245static KSTART_ROUTINE RunTest;
246static
247VOID
248NTAPI
251{
253
255}
256
257KMT_MESSAGE_HANDLER TestConnect;
263 _In_ SIZE_T InLength,
264 _Inout_ PSIZE_T OutLength
265)
266{
268
271
272 return STATUS_SUCCESS;
273}
#define ok_eq_hex(value, expected)
Definition: NtContinue.c:18
#define ok_eq_pointer(value, expected)
#define trace
Definition: atltest.h:70
#define ok(value,...)
Definition: atltest.h:57
LONG NTSTATUS
Definition: precomp.h:26
Definition: bufpool.h:45
IN PUNICODE_STRING IN POBJECT_ATTRIBUTES ObjectAttributes
Definition: conport.c:36
_In_ PIRP Irp
Definition: csq.h:116
#define NULL
Definition: types.h:112
#define TRUE
Definition: types.h:120
#define FALSE
Definition: types.h:117
#define GENERIC_READ
Definition: compat.h:135
#define FILE_ATTRIBUTE_NORMAL
Definition: compat.h:137
#define FILE_SHARE_READ
Definition: compat.h:136
#define IO_NETWORK_INCREMENT
Definition: tcpip.h:43
#define ExAllocatePoolWithTag(hernya, size, tag)
Definition: env_spec_w32.h:350
#define KeWaitForSingleObject(pEvt, foo, a, b, c)
Definition: env_spec_w32.h:478
#define KeInitializeEvent(pEvt, foo, foo2)
Definition: env_spec_w32.h:477
#define KeSetEvent(pEvt, foo, foo2)
Definition: env_spec_w32.h:476
#define NonPagedPool
Definition: env_spec_w32.h:307
VOID NTAPI KeClearEvent(IN PKEVENT Event)
Definition: eventobj.c:22
_In_opt_ PFILE_OBJECT _In_opt_ PETHREAD Thread
Definition: fltkernel.h:2653
#define FILE_OPEN_IF
Definition: from_kernel.h:56
Status
Definition: gdiplustypes.h:25
GLint GLint GLint GLint GLint x
Definition: gl.h:1548
#define OBJ_KERNEL_HANDLE
Definition: winternl.h:231
#define OBJ_CASE_INSENSITIVE
Definition: winternl.h:228
POBJECT_TYPE IoFileObjectType
Definition: iomgr.c:36
#define IoSetCompletionRoutine(_Irp, _CompletionRoutine, _Context, _InvokeOnSuccess, _InvokeOnError, _InvokeOnCancel)
Definition: irp.cpp:498
#define ok_eq_long(value, expected)
Definition: kmt_test.h:242
PKTHREAD KmtStartThread(IN PKSTART_ROUTINE StartRoutine, IN PVOID StartContext OPTIONAL)
VOID KmtFinishThread(IN PKTHREAD Thread OPTIONAL, IN PKEVENT Event OPTIONAL)
_In_ PKSPIN_CONNECT _In_ ACCESS_MASK _Out_ PHANDLE ConnectionHandle
Definition: ks.h:4538
#define htons(x)
Definition: module.h:213
#define ExFreePoolWithTag(_P, _T)
Definition: module.h:1099
KMT_MESSAGE_HANDLER TestConnect
Definition: connect.c:257
#define TAG_TEST
Definition: connect.c:16
static NTSTATUS NTAPI IrpCompletionRoutine(_In_ PDEVICE_OBJECT DeviceObject, _In_ PIRP Irp, _In_ PVOID Context)
Definition: connect.c:31
static KSTART_ROUTINE RunTest
Definition: connect.c:245
static VOID TestTcpConnect(void)
Definition: connect.c:46
#define TEST_CONNECT_SERVER_PORT
Definition: tcpip.h:6
#define TEST_CONNECT_CLIENT_PORT
Definition: tcpip.h:7
#define InitializeObjectAttributes(p, n, a, r, s)
Definition: reg.c:106
#define _Inout_
Definition: ms_sal.h:378
#define _In_
Definition: ms_sal.h:308
#define _In_opt_
Definition: ms_sal.h:309
#define KernelMode
Definition: asm.h:34
NTSYSAPI NTSTATUS NTAPI ZwClose(_In_ HANDLE Handle)
NTSYSAPI NTSTATUS NTAPI RtlIpv4StringToAddressW(_In_ PCWSTR String, _In_ BOOLEAN Strict, _Out_ PCWSTR *Terminator, _Out_ struct in_addr *Addr)
Definition: network.c:405
#define FILE_SHARE_WRITE
Definition: nt_native.h:681
#define GENERIC_WRITE
Definition: nt_native.h:90
#define UNREFERENCED_PARAMETER(P)
Definition: ntbasedef.h:317
@ NotificationEvent
@ FILE_OBJECT
Definition: ntobjenum.h:17
PDEVICE_OBJECT NTAPI IoGetRelatedDeviceObject(IN PFILE_OBJECT FileObject)
Definition: device.c:1539
PIRP NTAPI IoAllocateIrp(IN CCHAR StackSize, IN BOOLEAN ChargeQuota)
Definition: irp.c:615
#define IoCallDriver
Definition: irp.c:1225
VOID NTAPI IoFreeIrp(IN PIRP Irp)
Definition: irp.c:1666
#define STATUS_PENDING
Definition: ntstatus.h:82
#define L(x)
Definition: ntvdm.h:50
NTSTATUS NTAPI ObReferenceObjectByHandle(IN HANDLE Handle, IN ACCESS_MASK DesiredAccess, IN POBJECT_TYPE ObjectType, IN KPROCESSOR_MODE AccessMode, OUT PVOID *Object, OUT POBJECT_HANDLE_INFORMATION HandleInformation OPTIONAL)
Definition: obref.c:494
unsigned short USHORT
Definition: pedump.c:61
#define STATUS_MORE_PROCESSING_REQUIRED
Definition: shellext.h:68
#define STATUS_SUCCESS
Definition: shellext.h:65
struct _TA_ADDRESS_IP::_AddrIp Address[1]
LONG TAAddressCount
Definition: tdi.h:523
Definition: tcpip.h:126
u_long S_addr
Definition: tcpip.h:131
union in_addr::@1002 S_un
PVOID CONNECTION_CONTEXT
Definition: tdi.h:37
struct _TA_ADDRESS_IP TA_IP_ADDRESS
struct _TA_ADDRESS_IP * PTA_IP_ADDRESS
#define TDI_CONNECTION_CONTEXT_LENGTH
Definition: tdi.h:373
#define TDI_TRANSPORT_ADDRESS_LENGTH
Definition: tdi.h:372
#define TDI_ADDRESS_LENGTH_IP
Definition: tdi.h:413
#define TDI_ADDRESS_TYPE_IP
Definition: tdi.h:345
#define TdiConnectionContext
Definition: tdi.h:371
#define TdiTransportAddress
Definition: tdi.h:370
#define TdiBuildAssociateAddress( Irp, DevObj, FileObj, CompRoutine, Contxt, AddrHandle)
Definition: tdikrnl.h:467
#define TdiBuildConnect( Irp, DevObj, FileObj, CompRoutine, Contxt, Time, RequestConnectionInfo, ReturnConnectionInfo)
Definition: tdikrnl.h:494
#define RTL_CONSTANT_STRING(s)
Definition: tunneltest.c:14
ULONG_PTR * PSIZE_T
Definition: typedefs.h:80
#define FIELD_OFFSET(t, f)
Definition: typedefs.h:255
#define NTAPI
Definition: typedefs.h:36
ULONG_PTR SIZE_T
Definition: typedefs.h:80
#define RtlCopyMemory(Destination, Source, Length)
Definition: typedefs.h:263
#define RtlZeroMemory(Destination, Length)
Definition: typedefs.h:262
uint32_t ULONG_PTR
Definition: typedefs.h:65
uint32_t ULONG
Definition: typedefs.h:59
_In_ PDEVICE_OBJECT DeviceObject
Definition: wdfdevice.h:2055
_IRQL_requires_same_ typedef _In_ ULONG ControlCode
Definition: wmitypes.h:55
@ Executive
Definition: ketypes.h:403
#define ObDereferenceObject
Definition: obfuncs.h:203
const WCHAR * LPCWSTR
Definition: xmlstorage.h:185