ReactOS 0.4.15-dev-8102-g108db8f
NtAcceptConnectPort.c
Go to the documentation of this file.
1/*
2 * PROJECT: ReactOS API tests
3 * LICENSE: LGPLv2.1+ - See COPYING.LIB in the top level directory
4 * PURPOSE: Test for NtAcceptConnectPort
5 * PROGRAMMERS: Thomas Faber <thomas.faber@reactos.org>
6 */
7
8#include "precomp.h"
9
10#include <process.h>
11
12#define TEST_CONNECTION_INFO_SIGNATURE1 0xaabb0123
13#define TEST_CONNECTION_INFO_SIGNATURE2 0xaabb0124
15{
18
19#define TEST_MESSAGE_MESSAGE 0x4455cdef
20typedef struct _TEST_MESSAGE
21{
25
26static UNICODE_STRING PortName = RTL_CONSTANT_STRING(L"\\NtdllApitestNtAcceptConnectPortTestPort");
30
31UINT
35{
38 HANDLE PortHandle;
39 HANDLE ServerPortHandle = Parameter;
40
41 /* Listen, but refuse the connection */
42 RtlZeroMemory(&Message, sizeof(Message));
43 Status = NtListenPort(ServerPortHandle,
44 &Message.Header);
46
47 ok(Message.Header.u1.s1.TotalLength == RTL_SIZEOF_THROUGH_FIELD(TEST_MESSAGE, Message),
48 "TotalLength = %u, expected %lu\n",
49 Message.Header.u1.s1.TotalLength, RTL_SIZEOF_THROUGH_FIELD(TEST_MESSAGE, Message));
50 ok(Message.Header.u1.s1.DataLength == sizeof(TEST_CONNECTION_INFO),
51 "DataLength = %u\n", Message.Header.u1.s1.DataLength);
52 ok(Message.Header.u2.s2.Type == LPC_CONNECTION_REQUEST,
53 "Type = %x\n", Message.Header.u2.s2.Type);
54 ok(Message.Header.ClientId.UniqueProcess == UlongToHandle(GetCurrentProcessId()),
55 "UniqueProcess = %p, expected %lx\n",
56 Message.Header.ClientId.UniqueProcess, GetCurrentProcessId());
57 ok(Message.Header.ClientId.UniqueThread == UlongToHandle(ClientThreadId),
58 "UniqueThread = %p, expected %x\n",
59 Message.Header.ClientId.UniqueThread, ClientThreadId);
60 ok(Message.Message == TEST_CONNECTION_INFO_SIGNATURE1, "Message = %lx\n", Message.Message);
61
62 PortHandle = (PVOID)(ULONG_PTR)0x55555555;
63 Status = NtAcceptConnectPort(&PortHandle,
64 &Context,
65 &Message.Header,
66 FALSE,
67 NULL,
68 NULL);
70 ok(PortHandle == (PVOID)(ULONG_PTR)0x55555555, "PortHandle = %p\n", PortHandle);
71
72 /* Listen a second time, then accept */
73 RtlZeroMemory(&Message, sizeof(Message));
74 Status = NtListenPort(ServerPortHandle,
75 &Message.Header);
77
78 ok(Message.Header.u1.s1.TotalLength == RTL_SIZEOF_THROUGH_FIELD(TEST_MESSAGE, Message),
79 "TotalLength = %u, expected %lu\n",
80 Message.Header.u1.s1.TotalLength, RTL_SIZEOF_THROUGH_FIELD(TEST_MESSAGE, Message));
81 ok(Message.Header.u1.s1.DataLength == sizeof(TEST_CONNECTION_INFO),
82 "DataLength = %u\n", Message.Header.u1.s1.DataLength);
83 ok(Message.Header.u2.s2.Type == LPC_CONNECTION_REQUEST,
84 "Type = %x\n", Message.Header.u2.s2.Type);
85 ok(Message.Header.ClientId.UniqueProcess == UlongToHandle(GetCurrentProcessId()),
86 "UniqueProcess = %p, expected %lx\n",
87 Message.Header.ClientId.UniqueProcess, GetCurrentProcessId());
88 ok(Message.Header.ClientId.UniqueThread == UlongToHandle(ClientThreadId),
89 "UniqueThread = %p, expected %x\n",
90 Message.Header.ClientId.UniqueThread, ClientThreadId);
91 ok(Message.Message == TEST_CONNECTION_INFO_SIGNATURE2, "Message = %lx\n", Message.Message);
92
93 Status = NtAcceptConnectPort(&PortHandle,
94 &Context,
95 &Message.Header,
96 TRUE,
97 NULL,
98 NULL);
100
101 Status = NtCompleteConnectPort(PortHandle);
103
104 RtlZeroMemory(&Message, sizeof(Message));
105 Status = NtReplyWaitReceivePort(PortHandle,
106 NULL,
107 NULL,
108 &Message.Header);
110
111 ok(Message.Header.u1.s1.TotalLength == sizeof(Message),
112 "TotalLength = %u, expected %Iu\n",
113 Message.Header.u1.s1.TotalLength, sizeof(Message));
114 ok(Message.Header.u1.s1.DataLength == sizeof(Message.Message),
115 "DataLength = %u\n", Message.Header.u1.s1.DataLength);
116 ok(Message.Header.u2.s2.Type == LPC_DATAGRAM,
117 "Type = %x\n", Message.Header.u2.s2.Type);
118 ok(Message.Header.ClientId.UniqueProcess == UlongToHandle(GetCurrentProcessId()),
119 "UniqueProcess = %p, expected %lx\n",
120 Message.Header.ClientId.UniqueProcess, GetCurrentProcessId());
121 ok(Message.Header.ClientId.UniqueThread == UlongToHandle(ClientThreadId),
122 "UniqueThread = %p, expected %x\n",
123 Message.Header.ClientId.UniqueThread, ClientThreadId);
124 ok(Message.Message == TEST_MESSAGE_MESSAGE, "Message = %lx\n", Message.Message);
125
126 Status = NtClose(PortHandle);
128
129 return 0;
130}
131
132UINT
136{
138 HANDLE PortHandle;
139 TEST_CONNECTION_INFO ConnectInfo;
140 ULONG ConnectInfoLength;
141 SECURITY_QUALITY_OF_SERVICE SecurityQos;
143
144 SecurityQos.Length = sizeof(SecurityQos);
146 SecurityQos.EffectiveOnly = TRUE;
148
149 /* Attempt to connect -- will be rejected */
151 ConnectInfoLength = sizeof(ConnectInfo);
152 PortHandle = (PVOID)(ULONG_PTR)0x55555555;
153 Status = NtConnectPort(&PortHandle,
154 &PortName,
155 &SecurityQos,
156 NULL,
157 NULL,
158 NULL,
159 &ConnectInfo,
160 &ConnectInfoLength);
162 ok(PortHandle == (PVOID)(ULONG_PTR)0x55555555, "PortHandle = %p\n", PortHandle);
163
164 /* Try again, this time it will be accepted */
166 ConnectInfoLength = sizeof(ConnectInfo);
167 Status = NtConnectPort(&PortHandle,
168 &PortName,
169 &SecurityQos,
170 NULL,
171 NULL,
172 NULL,
173 &ConnectInfo,
174 &ConnectInfoLength);
176 if (!NT_SUCCESS(Status))
177 {
178 skip("Failed to connect\n");
179 return 0;
180 }
181
182 RtlZeroMemory(&Message, sizeof(Message));
183 Message.Header.u1.s1.TotalLength = sizeof(Message);
184 Message.Header.u1.s1.DataLength = sizeof(Message.Message);
186 Status = NtRequestPort(PortHandle,
187 &Message.Header);
189
190 Status = NtClose(PortHandle);
192
193 return 0;
194}
195
197{
200 HANDLE PortHandle;
201 HANDLE ThreadHandles[2];
202
204 &PortName,
206 NULL,
207 NULL);
208 Status = NtCreatePort(&PortHandle,
210 sizeof(TEST_CONNECTION_INFO),
211 sizeof(TEST_MESSAGE),
212 2 * sizeof(TEST_MESSAGE));
214 if (!NT_SUCCESS(Status))
215 {
216 skip("Failed to create port\n");
217 return;
218 }
219
220 ThreadHandles[0] = (HANDLE)_beginthreadex(NULL,
221 0,
223 PortHandle,
224 0,
226 ok(ThreadHandles[0] != NULL, "_beginthreadex failed\n");
227
228 ThreadHandles[1] = (HANDLE)_beginthreadex(NULL,
229 0,
231 PortHandle,
232 0,
234 ok(ThreadHandles[1] != NULL, "_beginthreadex failed\n");
235
237 ThreadHandles,
238 WaitAll,
239 FALSE,
240 NULL);
242
243 Status = NtClose(ThreadHandles[0]);
245 Status = NtClose(ThreadHandles[1]);
247
248 Status = NtClose(PortHandle);
250}
static UINT ClientThreadId
static UNICODE_STRING PortName
#define TEST_MESSAGE_MESSAGE
static UINT ServerThreadId
struct _TEST_MESSAGE TEST_MESSAGE
struct _TEST_CONNECTION_INFO TEST_CONNECTION_INFO
#define TEST_CONNECTION_INFO_SIGNATURE2
#define TEST_CONNECTION_INFO_SIGNATURE1
struct _TEST_CONNECTION_INFO * PTEST_CONNECTION_INFO
UINT CALLBACK ClientThread(_Inout_ PVOID Parameter)
UINT CALLBACK ServerThread(_Inout_ PVOID Parameter)
static UCHAR Context
struct _TEST_MESSAGE * PTEST_MESSAGE
#define RTL_NUMBER_OF(x)
Definition: RtlRegistry.c:12
#define ok_hex(expression, result)
Definition: atltest.h:94
#define ok(value,...)
Definition: atltest.h:57
#define skip(...)
Definition: atltest.h:64
#define START_TEST(x)
Definition: atltest.h:75
LONG NTSTATUS
Definition: precomp.h:26
#define UlongToHandle(ul)
Definition: basetsd.h:97
NTSTATUS NTAPI NtAcceptConnectPort(OUT PHANDLE PortHandle, IN PVOID PortContext OPTIONAL, IN PPORT_MESSAGE ReplyMessage, IN BOOLEAN AcceptConnection, IN OUT PPORT_VIEW ServerView OPTIONAL, OUT PREMOTE_PORT_VIEW ClientView OPTIONAL)
Definition: complete.c:40
NTSTATUS NTAPI NtCompleteConnectPort(IN HANDLE PortHandle)
Definition: complete.c:423
IN PUNICODE_STRING IN POBJECT_ATTRIBUTES ObjectAttributes
Definition: conport.c:36
#define NULL
Definition: types.h:112
#define TRUE
Definition: types.h:120
#define FALSE
Definition: types.h:117
#define NT_SUCCESS(StatCode)
Definition: apphelp.c:32
#define CALLBACK
Definition: compat.h:35
static const WCHAR Message[]
Definition: register.c:74
Status
Definition: gdiplustypes.h:25
#define OBJ_CASE_INSENSITIVE
Definition: winternl.h:228
@ SecurityIdentification
Definition: lsa.idl:56
#define LPC_DATAGRAM
Definition: port.c:95
#define LPC_CONNECTION_REQUEST
Definition: port.c:102
#define InitializeObjectAttributes(p, n, a, r, s)
Definition: reg.c:106
#define _Inout_
Definition: ms_sal.h:378
unsigned int UINT
Definition: ndis.h:50
NTSTATUS NTAPI NtClose(IN HANDLE Handle)
Definition: obhandle.c:3402
#define RTL_SIZEOF_THROUGH_FIELD(type, field)
Definition: ntbasedef.h:672
@ WaitAll
NTSTATUS NTAPI NtConnectPort(OUT PHANDLE PortHandle, IN PUNICODE_STRING PortName, IN PSECURITY_QUALITY_OF_SERVICE SecurityQos, IN OUT PPORT_VIEW ClientView OPTIONAL, IN OUT PREMOTE_PORT_VIEW ServerView OPTIONAL, OUT PULONG MaxMessageLength OPTIONAL, IN OUT PVOID ConnectionInformation OPTIONAL, IN OUT PULONG ConnectionInformationLength OPTIONAL)
Definition: connect.c:777
NTSTATUS NTAPI NtCreatePort(OUT PHANDLE PortHandle, IN POBJECT_ATTRIBUTES ObjectAttributes, IN ULONG MaxConnectInfoLength, IN ULONG MaxDataLength, IN ULONG MaxPoolUsage)
Definition: create.c:222
NTSTATUS NTAPI NtListenPort(IN HANDLE PortHandle, OUT PPORT_MESSAGE ConnectMessage)
Definition: listen.c:22
NTSTATUS NTAPI NtReplyWaitReceivePort(IN HANDLE PortHandle, OUT PVOID *PortContext OPTIONAL, IN PPORT_MESSAGE ReplyMessage OPTIONAL, OUT PPORT_MESSAGE ReceiveMessage)
Definition: reply.c:743
NTSTATUS NTAPI NtRequestPort(IN HANDLE PortHandle, IN PPORT_MESSAGE LpcRequest)
Definition: send.c:440
#define STATUS_PORT_CONNECTION_REFUSED
Definition: ntstatus.h:301
#define L(x)
Definition: ntvdm.h:50
NTSTATUS NTAPI NtWaitForMultipleObjects(IN ULONG ObjectCount, IN PHANDLE HandleArray, IN WAIT_TYPE WaitType, IN BOOLEAN Alertable, IN PLARGE_INTEGER TimeOut OPTIONAL)
Definition: obwait.c:46
_CRTIMP uintptr_t __cdecl _beginthreadex(_In_opt_ void *_Security, _In_ unsigned _StackSize, _In_ unsigned(__stdcall *_StartAddress)(void *), _In_opt_ void *_ArgList, _In_ unsigned _InitFlag, _Out_opt_ unsigned *_ThrdAddr)
#define STATUS_SUCCESS
Definition: shellext.h:65
SECURITY_CONTEXT_TRACKING_MODE ContextTrackingMode
Definition: lsa.idl:66
SECURITY_IMPERSONATION_LEVEL ImpersonationLevel
Definition: lsa.idl:65
PORT_MESSAGE Header
#define RTL_CONSTANT_STRING(s)
Definition: tunneltest.c:14
void * PVOID
Definition: typedefs.h:50
PVOID HANDLE
Definition: typedefs.h:73
#define RtlZeroMemory(Destination, Length)
Definition: typedefs.h:262
uint32_t ULONG_PTR
Definition: typedefs.h:65
uint32_t ULONG
Definition: typedefs.h:59
DWORD WINAPI GetCurrentProcessId(void)
Definition: proc.c:1158
_Inout_opt_ PVOID Parameter
Definition: rtltypes.h:323
#define SECURITY_STATIC_TRACKING
Definition: setypes.h:104
unsigned char UCHAR
Definition: xmlstorage.h:181