ReactOS  0.4.13-dev-52-g0efcfec
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
14 typedef struct _TEST_CONNECTION_INFO
15 {
18 
19 #define TEST_MESSAGE_MESSAGE 0x4455cdef
20 typedef struct _TEST_MESSAGE
21 {
25 
26 static UNICODE_STRING PortName = RTL_CONSTANT_STRING(L"\\NtdllApitestNtAcceptConnectPortTestPort");
29 static UCHAR Context;
30 
31 UINT
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 
132 UINT
133 CALLBACK
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);
185  Message.Message = TEST_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,
222  ServerThread,
223  PortHandle,
224  0,
225  &ServerThreadId);
226  ok(ThreadHandles[0] != NULL, "_beginthreadex failed\n");
227 
228  ThreadHandles[1] = (HANDLE)_beginthreadex(NULL,
229  0,
230  ClientThread,
231  PortHandle,
232  0,
233  &ClientThreadId);
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 }
struct _TEST_CONNECTION_INFO TEST_CONNECTION_INFO
IN PUNICODE_STRING IN POBJECT_ATTRIBUTES ObjectAttributes
Definition: conport.c:35
struct _TEST_MESSAGE TEST_MESSAGE
#define TRUE
Definition: types.h:120
#define TEST_CONNECTION_INFO_SIGNATURE1
#define TEST_MESSAGE_MESSAGE
LONG NTSTATUS
Definition: precomp.h:26
static UINT ServerThreadId
#define CALLBACK
Definition: compat.h:27
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
static UCHAR Context
_In_ PVOID Parameter
Definition: ldrtypes.h:240
_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)
SECURITY_CONTEXT_TRACKING_MODE ContextTrackingMode
Definition: lsa.idl:66
uint32_t ULONG_PTR
Definition: typedefs.h:63
START_TEST(NtAcceptConnectPort)
#define STATUS_PORT_CONNECTION_REFUSED
Definition: ntstatus.h:287
struct _TEST_MESSAGE * PTEST_MESSAGE
#define ok(value,...)
UINT CALLBACK ServerThread(_Inout_ PVOID Parameter)
smooth NULL
Definition: ftsmooth.c:416
UINT CALLBACK ClientThread(_Inout_ PVOID Parameter)
void * PVOID
Definition: retypes.h:9
#define ok_hex(expression, result)
Definition: test.h:759
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 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:753
NTSTATUS NTAPI NtCreatePort(OUT PHANDLE PortHandle, IN POBJECT_ATTRIBUTES ObjectAttributes, IN ULONG MaxConnectInfoLength, IN ULONG MaxDataLength, IN ULONG MaxPoolUsage)
Definition: create.c:212
#define NT_SUCCESS(StatCode)
Definition: apphelp.c:32
#define UlongToHandle(ul)
Definition: basetsd.h:97
PVOID HANDLE
Definition: typedefs.h:71
#define RTL_SIZEOF_THROUGH_FIELD(type, field)
Definition: ntbasedef.h:679
#define _Inout_
Definition: no_sal2.h:244
#define TEST_CONNECTION_INFO_SIGNATURE2
NTSTATUS NTAPI NtClose(IN HANDLE Handle)
Definition: obhandle.c:3399
#define OBJ_CASE_INSENSITIVE
Definition: winternl.h:228
CHAR Message[80]
Definition: alive.c:5
unsigned char UCHAR
Definition: xmlstorage.h:181
NTSTATUS NTAPI NtReplyWaitReceivePort(IN HANDLE PortHandle, OUT PVOID *PortContext OPTIONAL, IN PPORT_MESSAGE ReplyMessage OPTIONAL, OUT PPORT_MESSAGE ReceiveMessage)
Definition: reply.c:743
static const WCHAR L[]
Definition: oid.c:1250
struct _TEST_CONNECTION_INFO * PTEST_CONNECTION_INFO
Status
Definition: gdiplustypes.h:24
#define RTL_NUMBER_OF(x)
Definition: RtlRegistry.c:12
#define SECURITY_STATIC_TRACKING
Definition: setypes.h:104
SECURITY_IMPERSONATION_LEVEL ImpersonationLevel
Definition: lsa.idl:65
PORT_MESSAGE Header
unsigned int UINT
Definition: ndis.h:50
static UINT ClientThreadId
#define skip(...)
NTSTATUS NTAPI NtCompleteConnectPort(IN HANDLE PortHandle)
Definition: complete.c:421
unsigned int ULONG
Definition: retypes.h:1
#define RtlZeroMemory(Destination, Length)
Definition: typedefs.h:261
#define InitializeObjectAttributes(p, n, a, r, s)
Definition: reg.c:106
NTSTATUS NTAPI NtRequestPort(IN HANDLE PortHandle, IN PPORT_MESSAGE LpcRequest)
Definition: send.c:440
static UNICODE_STRING PortName
return STATUS_SUCCESS
Definition: btrfs.c:2725
NTSTATUS NTAPI NtListenPort(IN HANDLE PortHandle, OUT PPORT_MESSAGE ConnectMessage)
Definition: listen.c:22
DWORD WINAPI GetCurrentProcessId(VOID)
Definition: proc.c:1188
#define RTL_CONSTANT_STRING(s)
Definition: tunneltest.c:14