ntdll: Support both 32-bit and 64-bit modules in RtlImageDirectoryEntryToData.
[wine] / dlls / ntdll / tests / port.c
1 /* Unit test suite for Ntdll Port API functions
2  *
3  * Copyright 2006 James Hawkins
4  *
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.
9  *
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.
14  *
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
18  */
19
20 #include <stdio.h>
21 #include <stdarg.h>
22
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"
32
33 #ifndef __WINE_WINTERNL_H
34
35 typedef struct _CLIENT_ID
36 {
37    HANDLE UniqueProcess;
38    HANDLE UniqueThread;
39 } CLIENT_ID, *PCLIENT_ID;
40
41 typedef struct _LPC_SECTION_WRITE
42 {
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;
50
51 typedef struct _LPC_SECTION_READ
52 {
53   ULONG Length;
54   ULONG ViewSize;
55   PVOID ViewBase;
56 } LPC_SECTION_READ, *PLPC_SECTION_READ;
57
58 typedef struct _LPC_MESSAGE
59 {
60   USHORT DataSize;
61   USHORT MessageSize;
62   USHORT MessageType;
63   USHORT VirtualRangesOffset;
64   CLIENT_ID ClientId;
65   ULONG_PTR MessageId;
66   ULONG_PTR SectionSize;
67   UCHAR Data[ANYSIZE_ARRAY];
68 } LPC_MESSAGE, *PLPC_MESSAGE;
69
70 #endif
71
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
84
85 static const WCHAR PORTNAME[] = {'\\','M','y','P','o','r','t',0};
86
87 #define REQUEST1    "Request1"
88 #define REQUEST2    "Request2"
89 #define REPLY       "Reply"
90
91 #define MAX_MESSAGE_LEN    30
92
93 static UNICODE_STRING port;
94
95 /* Function pointers for ntdll calls */
96 static HMODULE hntdll = 0;
97 static NTSTATUS (WINAPI *pNtCompleteConnectPort)(HANDLE);
98 static NTSTATUS (WINAPI *pNtAcceptConnectPort)(PHANDLE,ULONG,PLPC_MESSAGE,ULONG,
99                                                ULONG,PLPC_SECTION_READ);
100 static NTSTATUS (WINAPI *pNtReplyPort)(HANDLE,PLPC_MESSAGE);
101 static NTSTATUS (WINAPI *pNtReplyWaitReceivePort)(PHANDLE,PULONG,PLPC_MESSAGE,
102                                                   PLPC_MESSAGE);
103 static NTSTATUS (WINAPI *pNtCreatePort)(PHANDLE,POBJECT_ATTRIBUTES,ULONG,ULONG,ULONG);
104 static NTSTATUS (WINAPI *pNtRequestWaitReplyPort)(HANDLE,PLPC_MESSAGE,PLPC_MESSAGE);
105 static NTSTATUS (WINAPI *pNtRequestPort)(HANDLE,PLPC_MESSAGE);
106 static NTSTATUS (WINAPI *pNtRegisterThreadTerminatePort)(HANDLE);
107 static NTSTATUS (WINAPI *pNtConnectPort)(PHANDLE,PUNICODE_STRING,
108                                          PSECURITY_QUALITY_OF_SERVICE,
109                                          PLPC_SECTION_WRITE,PLPC_SECTION_READ,
110                                          PVOID,PVOID,PULONG);
111 static NTSTATUS (WINAPI *pRtlInitUnicodeString)(PUNICODE_STRING,LPCWSTR);
112 static NTSTATUS (WINAPI *pNtWaitForSingleObject)(HANDLE,BOOLEAN,PLARGE_INTEGER);
113
114 static BOOL init_function_ptrs(void)
115 {
116     hntdll = LoadLibraryA("ntdll.dll");
117
118     if (!hntdll)
119         return FALSE;
120
121     pNtCompleteConnectPort = (void *)GetProcAddress(hntdll, "NtCompleteConnectPort");
122     pNtAcceptConnectPort = (void *)GetProcAddress(hntdll, "NtAcceptConnectPort");
123     pNtReplyPort = (void *)GetProcAddress(hntdll, "NtReplyPort");
124     pNtReplyWaitReceivePort = (void *)GetProcAddress(hntdll, "NtReplyWaitReceivePort");
125     pNtCreatePort = (void *)GetProcAddress(hntdll, "NtCreatePort");
126     pNtRequestWaitReplyPort = (void *)GetProcAddress(hntdll, "NtRequestWaitReplyPort");
127     pNtRequestPort = (void *)GetProcAddress(hntdll, "NtRequestPort");
128     pNtRegisterThreadTerminatePort = (void *)GetProcAddress(hntdll, "NtRegisterThreadTerminatePort");
129     pNtConnectPort = (void *)GetProcAddress(hntdll, "NtConnectPort");
130     pRtlInitUnicodeString = (void *)GetProcAddress(hntdll, "RtlInitUnicodeString");
131     pNtWaitForSingleObject = (void *)GetProcAddress(hntdll, "NtWaitForSingleObject");
132
133     if (!pNtCompleteConnectPort || !pNtAcceptConnectPort ||
134         !pNtReplyWaitReceivePort || !pNtCreatePort || !pNtRequestWaitReplyPort ||
135         !pNtRequestPort || !pNtRegisterThreadTerminatePort ||
136         !pNtConnectPort || !pRtlInitUnicodeString)
137     {
138         win_skip("Needed port functions are not available\n");
139         FreeLibrary(hntdll);
140         return FALSE;
141     }
142
143     return TRUE;
144 }
145
146 static void ProcessConnectionRequest(PLPC_MESSAGE LpcMessage, PHANDLE pAcceptPortHandle)
147 {
148     NTSTATUS status;
149
150     ok(LpcMessage->MessageType == LPC_CONNECTION_REQUEST,
151        "Expected LPC_CONNECTION_REQUEST, got %d\n", LpcMessage->MessageType);
152     ok(!*LpcMessage->Data, "Expected empty string!\n");
153
154     status = pNtAcceptConnectPort(pAcceptPortHandle, 0, LpcMessage, 1, 0, NULL);
155     ok(status == STATUS_SUCCESS, "Expected STATUS_SUCCESS, got %x\n", status);
156     
157     status = pNtCompleteConnectPort(*pAcceptPortHandle);
158     ok(status == STATUS_SUCCESS, "Expected STATUS_SUCCESS, got %x\n", status);
159 }
160
161 static void ProcessLpcRequest(HANDLE PortHandle, PLPC_MESSAGE LpcMessage)
162 {
163     NTSTATUS status;
164
165     ok(LpcMessage->MessageType == LPC_REQUEST,
166        "Expected LPC_REQUEST, got %d\n", LpcMessage->MessageType);
167     ok(!lstrcmp((LPSTR)LpcMessage->Data, REQUEST2),
168        "Expected %s, got %s\n", REQUEST2, LpcMessage->Data);
169
170     lstrcpy((LPSTR)LpcMessage->Data, REPLY);
171
172     status = pNtReplyPort(PortHandle, LpcMessage);
173     ok(status == STATUS_SUCCESS, "Expected STATUS_SUCCESS, got %x\n", status);
174     ok(LpcMessage->MessageType == LPC_REQUEST,
175        "Expected LPC_REQUEST, got %d\n", LpcMessage->MessageType);
176     ok(!lstrcmp((LPSTR)LpcMessage->Data, REPLY),
177        "Expected %s, got %s\n", REPLY, LpcMessage->Data);
178 }
179
180 static DWORD WINAPI test_ports_client(LPVOID arg)
181 {
182     SECURITY_QUALITY_OF_SERVICE sqos;
183     LPC_MESSAGE *LpcMessage, *out;
184     HANDLE PortHandle;
185     ULONG len, size;
186     NTSTATUS status;
187
188     sqos.Length = sizeof(SECURITY_QUALITY_OF_SERVICE);
189     sqos.ImpersonationLevel = SecurityImpersonation;
190     sqos.ContextTrackingMode = SECURITY_STATIC_TRACKING;
191     sqos.EffectiveOnly = TRUE;
192
193     status = pNtConnectPort(&PortHandle, &port, &sqos, 0, 0, &len, NULL, NULL);
194     todo_wine ok(status == STATUS_SUCCESS, "Expected STATUS_SUCCESS, got %x\n", status);
195     if (status != STATUS_SUCCESS) return 1;
196
197     status = pNtRegisterThreadTerminatePort(PortHandle);
198     ok(status == STATUS_SUCCESS, "Expected STATUS_SUCCESS, got %x\n", status);
199
200     size = FIELD_OFFSET(LPC_MESSAGE, Data) + MAX_MESSAGE_LEN;
201     LpcMessage = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, size);
202     out = HeapAlloc(GetProcessHeap(), 0, size);
203
204     LpcMessage->DataSize = lstrlen(REQUEST1) + 1;
205     LpcMessage->MessageSize = FIELD_OFFSET(LPC_MESSAGE, Data) + LpcMessage->DataSize;
206     lstrcpy((LPSTR)LpcMessage->Data, REQUEST1);
207
208     status = pNtRequestPort(PortHandle, LpcMessage);
209     ok(status == STATUS_SUCCESS, "Expected STATUS_SUCCESS, got %x\n", status);
210     ok(LpcMessage->MessageType == 0, "Expected 0, got %d\n", LpcMessage->MessageType);
211     ok(!lstrcmp((LPSTR)LpcMessage->Data, REQUEST1),
212        "Expected %s, got %s\n", REQUEST1, LpcMessage->Data);
213
214     /* Fill in the message */
215     memset(LpcMessage, 0, size);
216     LpcMessage->DataSize = lstrlen(REQUEST2) + 1;
217     LpcMessage->MessageSize = FIELD_OFFSET(LPC_MESSAGE, Data) + LpcMessage->DataSize;
218     lstrcpy((LPSTR)LpcMessage->Data, REQUEST2);
219
220     /* Send the message and wait for the reply */
221     status = pNtRequestWaitReplyPort(PortHandle, LpcMessage, out);
222     ok(status == STATUS_SUCCESS, "Expected STATUS_SUCCESS, got %x\n", status);
223     ok(!lstrcmp((LPSTR)out->Data, REPLY), "Expected %s, got %s\n", REPLY, out->Data);
224     ok(out->MessageType == LPC_REPLY, "Expected LPC_REPLY, got %d\n", out->MessageType);
225
226     HeapFree(GetProcessHeap(), 0, out);
227     HeapFree(GetProcessHeap(), 0, LpcMessage);
228
229     return 0;
230 }
231
232 static void test_ports_server( HANDLE PortHandle )
233 {
234     HANDLE AcceptPortHandle;
235     PLPC_MESSAGE LpcMessage;
236     ULONG size;
237     NTSTATUS status;
238     BOOL done = FALSE;
239
240     size = FIELD_OFFSET(LPC_MESSAGE, Data) + MAX_MESSAGE_LEN;
241     LpcMessage = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, size);
242
243     while (TRUE)
244     {
245         status = pNtReplyWaitReceivePort(PortHandle, NULL, NULL, LpcMessage);
246         todo_wine
247         {
248             ok(status == STATUS_SUCCESS, "Expected STATUS_SUCCESS, got %d(%x)\n", status, status);
249         }
250         /* STATUS_INVALID_HANDLE: win2k without admin rights will perform an
251          *                        endless loop here
252          */
253         if ((status == STATUS_NOT_IMPLEMENTED) ||
254             (status == STATUS_INVALID_HANDLE)) return;
255
256         switch (LpcMessage->MessageType)
257         {
258             case LPC_CONNECTION_REQUEST:
259                 ProcessConnectionRequest(LpcMessage, &AcceptPortHandle);
260                 break;
261
262             case LPC_REQUEST:
263                 ProcessLpcRequest(PortHandle, LpcMessage);
264                 done = TRUE;
265                 break;
266
267             case LPC_DATAGRAM:
268                 ok(!lstrcmp((LPSTR)LpcMessage->Data, REQUEST1),
269                    "Expected %s, got %s\n", REQUEST1, LpcMessage->Data);
270                 break;
271
272             case LPC_CLIENT_DIED:
273                 ok(done, "Expected LPC request to be completed!\n");
274                 HeapFree(GetProcessHeap(), 0, LpcMessage);
275                 return;
276
277             default:
278                 ok(FALSE, "Unexpected message: %d\n", LpcMessage->MessageType);
279                 break;
280         }
281     }
282
283     HeapFree(GetProcessHeap(), 0, LpcMessage);
284 }
285
286 START_TEST(port)
287 {
288     OBJECT_ATTRIBUTES obj;
289     HANDLE port_handle;
290     NTSTATUS status;
291
292     if (!init_function_ptrs())
293         return;
294
295     pRtlInitUnicodeString(&port, PORTNAME);
296
297     memset(&obj, 0, sizeof(OBJECT_ATTRIBUTES));
298     obj.Length = sizeof(OBJECT_ATTRIBUTES);
299     obj.ObjectName = &port;
300
301     status = pNtCreatePort(&port_handle, &obj, 100, 100, 0);
302     if (status == STATUS_ACCESS_DENIED) skip("Not enough rights\n");
303     else todo_wine ok(status == STATUS_SUCCESS, "Expected STATUS_SUCCESS, got %d\n", status);
304
305     if (status == STATUS_SUCCESS)
306     {
307         DWORD id;
308         HANDLE thread = CreateThread(NULL, 0, test_ports_client, NULL, 0, &id);
309         ok(thread != NULL, "Expected non-NULL thread handle!\n");
310
311         test_ports_server( port_handle );
312         ok( WaitForSingleObject( thread, 10000 ) == 0, "thread didn't exit\n" );
313         CloseHandle(thread);
314     }
315     FreeLibrary(hntdll);
316 }