push 4e98c31ec75caed2ea3040ac2e710b58ba1ca0e1
[wine/hacks.git] / dlls / ntdll / tests / port.c
blobc63820a37fd42e805bf910cb0ebff6cfe186cd00
1 /* Unit test suite for Ntdll Port API functions
3 * Copyright 2006 James Hawkins
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
20 #include <stdio.h>
21 #include <stdarg.h>
23 #include "ntstatus.h"
24 #define WIN32_NO_STATUS
25 #include "windef.h"
26 #include "winbase.h"
27 #include "winuser.h"
28 #include "winreg.h"
29 #include "winnls.h"
30 #include "wine/test.h"
31 #include "winternl.h"
33 #ifndef __WINE_WINTERNL_H
35 typedef struct _CLIENT_ID
37 HANDLE UniqueProcess;
38 HANDLE UniqueThread;
39 } CLIENT_ID, *PCLIENT_ID;
41 typedef struct _LPC_SECTION_WRITE
43 ULONG Length;
44 HANDLE SectionHandle;
45 ULONG SectionOffset;
46 ULONG ViewSize;
47 PVOID ViewBase;
48 PVOID TargetViewBase;
49 } LPC_SECTION_WRITE, *PLPC_SECTION_WRITE;
51 typedef struct _LPC_SECTION_READ
53 ULONG Length;
54 ULONG ViewSize;
55 PVOID ViewBase;
56 } LPC_SECTION_READ, *PLPC_SECTION_READ;
58 typedef struct _LPC_MESSAGE
60 USHORT DataSize;
61 USHORT MessageSize;
62 USHORT MessageType;
63 USHORT VirtualRangesOffset;
64 CLIENT_ID ClientId;
65 ULONG MessageId;
66 ULONG SectionSize;
67 UCHAR Data[ANYSIZE_ARRAY];
68 } LPC_MESSAGE, *PLPC_MESSAGE;
70 #endif
72 /* Types of LPC messages */
73 #define UNUSED_MSG_TYPE 0
74 #define LPC_REQUEST 1
75 #define LPC_REPLY 2
76 #define LPC_DATAGRAM 3
77 #define LPC_LOST_REPLY 4
78 #define LPC_PORT_CLOSED 5
79 #define LPC_CLIENT_DIED 6
80 #define LPC_EXCEPTION 7
81 #define LPC_DEBUG_EVENT 8
82 #define LPC_ERROR_EVENT 9
83 #define LPC_CONNECTION_REQUEST 10
85 static const WCHAR PORTNAME[] = {'\\','M','y','P','o','r','t',0};
87 #define REQUEST1 "Request1"
88 #define REQUEST2 "Request2"
89 #define REPLY "Reply"
91 #define MAX_MESSAGE_LEN 30
93 UNICODE_STRING port;
94 static char selfname[MAX_PATH];
95 static int myARGC;
96 static char** myARGV;
98 /* Function pointers for ntdll calls */
99 static HMODULE hntdll = 0;
100 static NTSTATUS (WINAPI *pNtCompleteConnectPort)(HANDLE);
101 static NTSTATUS (WINAPI *pNtAcceptConnectPort)(PHANDLE,ULONG,PLPC_MESSAGE,ULONG,
102 ULONG,PLPC_SECTION_READ);
103 static NTSTATUS (WINAPI *pNtReplyPort)(HANDLE,PLPC_MESSAGE);
104 static NTSTATUS (WINAPI *pNtReplyWaitReceivePort)(PHANDLE,PULONG,PLPC_MESSAGE,
105 PLPC_MESSAGE);
106 static NTSTATUS (WINAPI *pNtCreatePort)(PHANDLE,POBJECT_ATTRIBUTES,ULONG,ULONG,ULONG);
107 static NTSTATUS (WINAPI *pNtRequestWaitReplyPort)(HANDLE,PLPC_MESSAGE,PLPC_MESSAGE);
108 static NTSTATUS (WINAPI *pNtRequestPort)(HANDLE,PLPC_MESSAGE);
109 static NTSTATUS (WINAPI *pNtRegisterThreadTerminatePort)(HANDLE);
110 static NTSTATUS (WINAPI *pNtConnectPort)(PHANDLE,PUNICODE_STRING,
111 PSECURITY_QUALITY_OF_SERVICE,
112 PLPC_SECTION_WRITE,PLPC_SECTION_READ,
113 PVOID,PVOID,PULONG);
114 static NTSTATUS (WINAPI *pRtlInitUnicodeString)(PUNICODE_STRING,LPCWSTR);
115 static NTSTATUS (WINAPI *pNtWaitForSingleObject)(HANDLE,BOOLEAN,PLARGE_INTEGER);
117 static BOOL init_function_ptrs(void)
119 hntdll = LoadLibraryA("ntdll.dll");
121 if (!hntdll)
122 return FALSE;
124 pNtCompleteConnectPort = (void *)GetProcAddress(hntdll, "NtCompleteConnectPort");
125 pNtAcceptConnectPort = (void *)GetProcAddress(hntdll, "NtAcceptConnectPort");
126 pNtReplyPort = (void *)GetProcAddress(hntdll, "NtReplyPort");
127 pNtReplyWaitReceivePort = (void *)GetProcAddress(hntdll, "NtReplyWaitReceivePort");
128 pNtCreatePort = (void *)GetProcAddress(hntdll, "NtCreatePort");
129 pNtRequestWaitReplyPort = (void *)GetProcAddress(hntdll, "NtRequestWaitReplyPort");
130 pNtRequestPort = (void *)GetProcAddress(hntdll, "NtRequestPort");
131 pNtRegisterThreadTerminatePort = (void *)GetProcAddress(hntdll, "NtRegisterThreadTerminatePort");
132 pNtConnectPort = (void *)GetProcAddress(hntdll, "NtConnectPort");
133 pRtlInitUnicodeString = (void *)GetProcAddress(hntdll, "RtlInitUnicodeString");
134 pNtWaitForSingleObject = (void *)GetProcAddress(hntdll, "NtWaitForSingleObject");
136 if (!pNtCompleteConnectPort || !pNtAcceptConnectPort ||
137 !pNtReplyWaitReceivePort || !pNtCreatePort || !pNtRequestWaitReplyPort ||
138 !pNtRequestPort || !pNtRegisterThreadTerminatePort ||
139 !pNtConnectPort || !pRtlInitUnicodeString)
141 win_skip("Needed port functions are not available\n");
142 FreeLibrary(hntdll);
143 return FALSE;
146 return TRUE;
149 static void ProcessConnectionRequest(PLPC_MESSAGE LpcMessage, PHANDLE pAcceptPortHandle)
151 NTSTATUS status;
153 ok(LpcMessage->MessageType == LPC_CONNECTION_REQUEST,
154 "Expected LPC_CONNECTION_REQUEST, got %d\n", LpcMessage->MessageType);
155 ok(!*LpcMessage->Data, "Expected empty string!\n");
157 status = pNtAcceptConnectPort(pAcceptPortHandle, 0, LpcMessage, 1, 0, NULL);
158 ok(status == STATUS_SUCCESS, "Expected STATUS_SUCCESS, got %d\n", status);
160 status = pNtCompleteConnectPort(*pAcceptPortHandle);
161 ok(status == STATUS_SUCCESS, "Expected STATUS_SUCCESS, got %d\n", status);
164 static void ProcessLpcRequest(HANDLE PortHandle, PLPC_MESSAGE LpcMessage)
166 NTSTATUS status;
168 ok(LpcMessage->MessageType == LPC_REQUEST,
169 "Expected LPC_REQUEST, got %d\n", LpcMessage->MessageType);
170 ok(!lstrcmp((LPSTR)LpcMessage->Data, REQUEST2),
171 "Expected %s, got %s\n", REQUEST2, LpcMessage->Data);
173 lstrcpy((LPSTR)LpcMessage->Data, REPLY);
175 status = pNtReplyPort(PortHandle, LpcMessage);
176 ok(status == STATUS_SUCCESS, "Expected STATUS_SUCCESS, got %d\n", status);
177 ok(LpcMessage->MessageType == LPC_REQUEST,
178 "Expected LPC_REQUEST, got %d\n", LpcMessage->MessageType);
179 ok(!lstrcmp((LPSTR)LpcMessage->Data, REPLY),
180 "Expected %s, got %s\n", REPLY, LpcMessage->Data);
183 static DWORD WINAPI test_ports_client(LPVOID arg)
185 SECURITY_QUALITY_OF_SERVICE sqos;
186 LPC_MESSAGE *LpcMessage, *out;
187 HANDLE PortHandle;
188 ULONG len, size;
189 NTSTATUS status;
191 sqos.Length = sizeof(SECURITY_QUALITY_OF_SERVICE);
192 sqos.ImpersonationLevel = SecurityImpersonation;
193 sqos.ContextTrackingMode = SECURITY_STATIC_TRACKING;
194 sqos.EffectiveOnly = TRUE;
196 status = pNtConnectPort(&PortHandle, &port, &sqos, 0, 0, &len, NULL, NULL);
197 todo_wine ok(status == STATUS_SUCCESS, "Expected STATUS_SUCCESS, got %d\n", status);
198 if (status != STATUS_SUCCESS) return 1;
200 status = pNtRegisterThreadTerminatePort(PortHandle);
201 ok(status == STATUS_SUCCESS, "Expected STATUS_SUCCESS, got %d\n", status);
203 size = FIELD_OFFSET(LPC_MESSAGE, Data) + MAX_MESSAGE_LEN;
204 LpcMessage = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, size);
205 out = HeapAlloc(GetProcessHeap(), 0, size);
207 LpcMessage->DataSize = lstrlen(REQUEST1) + 1;
208 LpcMessage->MessageSize = FIELD_OFFSET(LPC_MESSAGE, Data) + LpcMessage->DataSize;
209 lstrcpy((LPSTR)LpcMessage->Data, REQUEST1);
211 status = pNtRequestPort(PortHandle, LpcMessage);
212 ok(status == STATUS_SUCCESS, "Expected STATUS_SUCCESS, got %d\n", status);
213 ok(LpcMessage->MessageType == 0, "Expected 0, got %d\n", LpcMessage->MessageType);
214 ok(!lstrcmp((LPSTR)LpcMessage->Data, REQUEST1),
215 "Expected %s, got %s\n", REQUEST1, LpcMessage->Data);
217 /* Fill in the message */
218 memset(LpcMessage, 0, size);
219 LpcMessage->DataSize = lstrlen(REQUEST2) + 1;
220 LpcMessage->MessageSize = FIELD_OFFSET(LPC_MESSAGE, Data) + LpcMessage->DataSize;
221 lstrcpy((LPSTR)LpcMessage->Data, REQUEST2);
223 /* Send the message and wait for the reply */
224 status = pNtRequestWaitReplyPort(PortHandle, LpcMessage, out);
225 ok(status == STATUS_SUCCESS, "Expected STATUS_SUCCESS, got %d\n", status);
226 ok(!lstrcmp((LPSTR)out->Data, REPLY), "Expected %s, got %s\n", REPLY, out->Data);
227 ok(out->MessageType == LPC_REPLY, "Expected LPC_REPLY, got %d\n", out->MessageType);
229 HeapFree(GetProcessHeap(), 0, out);
230 HeapFree(GetProcessHeap(), 0, LpcMessage);
232 return 0;
235 static void test_ports_server(void)
237 OBJECT_ATTRIBUTES obj;
238 HANDLE PortHandle;
239 HANDLE AcceptPortHandle;
240 PLPC_MESSAGE LpcMessage;
241 ULONG size;
242 NTSTATUS status;
243 BOOL done = FALSE;
245 pRtlInitUnicodeString(&port, PORTNAME);
247 memset(&obj, 0, sizeof(OBJECT_ATTRIBUTES));
248 obj.Length = sizeof(OBJECT_ATTRIBUTES);
249 obj.ObjectName = &port;
251 status = pNtCreatePort(&PortHandle, &obj, 100, 100, 0);
252 if (status == STATUS_ACCESS_DENIED)
254 skip("Not enough rights\n");
255 return;
257 todo_wine
259 ok(status == STATUS_SUCCESS, "Expected STATUS_SUCCESS, got %d\n", status);
261 if (status != STATUS_SUCCESS) return;
263 size = FIELD_OFFSET(LPC_MESSAGE, Data) + MAX_MESSAGE_LEN;
264 LpcMessage = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, size);
266 while (TRUE)
268 status = pNtReplyWaitReceivePort(PortHandle, NULL, NULL, LpcMessage);
269 todo_wine
271 ok(status == STATUS_SUCCESS, "Expected STATUS_SUCCESS, got %d(%x)\n", status, status);
273 /* STATUS_INVALID_HANDLE: win2k without admin rights will perform an
274 * endless loop here
276 if ((status == STATUS_NOT_IMPLEMENTED) ||
277 (status == STATUS_INVALID_HANDLE)) return;
279 switch (LpcMessage->MessageType)
281 case LPC_CONNECTION_REQUEST:
282 ProcessConnectionRequest(LpcMessage, &AcceptPortHandle);
283 break;
285 case LPC_REQUEST:
286 ProcessLpcRequest(PortHandle, LpcMessage);
287 done = TRUE;
288 break;
290 case LPC_DATAGRAM:
291 ok(!lstrcmp((LPSTR)LpcMessage->Data, REQUEST1),
292 "Expected %s, got %s\n", REQUEST1, LpcMessage->Data);
293 break;
295 case LPC_CLIENT_DIED:
296 ok(done, "Expected LPC request to be completed!\n");
297 HeapFree(GetProcessHeap(), 0, LpcMessage);
298 return;
300 default:
301 ok(FALSE, "Unexpected message: %d\n", LpcMessage->MessageType);
302 break;
306 HeapFree(GetProcessHeap(), 0, LpcMessage);
309 START_TEST(port)
311 HANDLE thread;
312 DWORD id;
314 if (!init_function_ptrs())
315 return;
317 myARGC = winetest_get_mainargs(&myARGV);
318 strcpy(selfname, myARGV[0]);
320 thread = CreateThread(NULL, 0, test_ports_client, NULL, 0, &id);
321 ok(thread != NULL, "Expected non-NULL thread handle!\n");
323 test_ports_server();
324 CloseHandle(thread);
326 FreeLibrary(hntdll);