2 * Mailslot regression test
4 * Copyright 2003 Mike McCormack
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
28 #include "wine/test.h"
30 static const char szmspath[] = "\\\\.\\mailslot\\wine_mailslot_test";
32 static int mailslot_test(void)
34 HANDLE hSlot, hSlot2, hWriter, hWriter2;
35 unsigned char buffer[16];
36 DWORD count, dwMax, dwNext, dwMsgCount, dwTimeout;
39 /* sanity check on GetMailslotInfo */
40 dwMax = dwNext = dwMsgCount = dwTimeout = 0;
41 ok( !GetMailslotInfo( INVALID_HANDLE_VALUE, &dwMax, &dwNext,
42 &dwMsgCount, &dwTimeout ), "getmailslotinfo succeeded\n");
44 /* open a mailslot that doesn't exist */
45 hWriter = CreateFile(szmspath, GENERIC_READ|GENERIC_WRITE,
46 FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, NULL);
47 ok( hWriter == INVALID_HANDLE_VALUE, "nonexistent mailslot\n");
49 /* open a mailslot without the right name */
50 hSlot = CreateMailslot( "blah", 0, 0, NULL );
51 ok( hSlot == INVALID_HANDLE_VALUE,
52 "Created mailslot with invalid name\n");
53 ok( GetLastError() == ERROR_INVALID_NAME,
54 "error should be ERROR_INVALID_NAME\n");
56 /* open a mailslot with a null name */
57 hSlot = CreateMailslot( NULL, 0, 0, NULL );
58 ok( hSlot == INVALID_HANDLE_VALUE, "Created mailslot with invalid name\n");
59 ok( GetLastError() == ERROR_PATH_NOT_FOUND, "error should be ERROR_PATH_NOT_FOUND\n");
61 /* valid open, but with wacky parameters ... then check them */
62 hSlot = CreateMailslot( szmspath, -1, -1, NULL );
63 ok( hSlot != INVALID_HANDLE_VALUE , "mailslot with valid name failed\n");
64 dwMax = dwNext = dwMsgCount = dwTimeout = 0;
65 ok( GetMailslotInfo( hSlot, &dwMax, &dwNext, &dwMsgCount, &dwTimeout ),
66 "getmailslotinfo failed\n");
67 ok( dwMax == ~0U, "dwMax incorrect\n");
68 ok( dwNext == MAILSLOT_NO_MESSAGE, "dwNext incorrect\n");
69 ok( dwMsgCount == 0, "dwMsgCount incorrect\n");
70 ok( dwTimeout == ~0U, "dwTimeout incorrect\n");
71 ok( GetMailslotInfo( hSlot, NULL, NULL, NULL, NULL ),
72 "getmailslotinfo failed\n");
73 ok( CloseHandle(hSlot), "failed to close mailslot\n");
75 /* now open it for real */
76 hSlot = CreateMailslot( szmspath, 0, 0, NULL );
77 ok( hSlot != INVALID_HANDLE_VALUE , "valid mailslot failed\n");
79 /* try and read/write to it */
81 memset(buffer, 0, sizeof buffer);
82 ret = ReadFile( hSlot, buffer, sizeof buffer, &count, NULL);
83 ok( !ret, "slot read\n");
84 if (!ret) ok( GetLastError() == ERROR_SEM_TIMEOUT, "wrong error %u\n", GetLastError() );
85 else ok( count == 0, "wrong count %u\n", count );
86 ok( !WriteFile( hSlot, buffer, sizeof buffer, &count, NULL),
88 ok( GetLastError() == ERROR_ACCESS_DENIED, "wrong error %u\n", GetLastError() );
90 /* now try and open the client, but with the wrong sharing mode */
91 hWriter = CreateFile(szmspath, GENERIC_WRITE,
92 0, NULL, OPEN_EXISTING, 0, NULL);
93 ok( hWriter != INVALID_HANDLE_VALUE /* vista */ || GetLastError() == ERROR_SHARING_VIOLATION,
94 "error should be ERROR_SHARING_VIOLATION got %p / %u\n", hWriter, GetLastError());
95 if (hWriter != INVALID_HANDLE_VALUE) CloseHandle( hWriter );
97 /* now open the client with the correct sharing mode */
98 hWriter = CreateFile(szmspath, GENERIC_READ|GENERIC_WRITE,
99 FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, NULL);
100 ok( hWriter != INVALID_HANDLE_VALUE, "existing mailslot err %u\n", GetLastError());
103 * opening a client should make no difference to
104 * whether we can read or write the mailslot
106 ret = ReadFile( hSlot, buffer, sizeof buffer/2, &count, NULL);
107 ok( !ret, "slot read\n");
108 if (!ret) ok( GetLastError() == ERROR_SEM_TIMEOUT, "wrong error %u\n", GetLastError() );
109 else ok( count == 0, "wrong count %u\n", count );
110 ok( !WriteFile( hSlot, buffer, sizeof buffer/2, &count, NULL),
112 ok( GetLastError() == ERROR_ACCESS_DENIED, "wrong error %u\n", GetLastError() );
115 * we can't read from this client,
116 * but we should be able to write to it
118 ok( !ReadFile( hWriter, buffer, sizeof buffer/2, &count, NULL),
119 "can read client\n");
120 ok( GetLastError() == ERROR_INVALID_PARAMETER || GetLastError() == ERROR_ACCESS_DENIED,
121 "wrong error %u\n", GetLastError() );
122 ok( WriteFile( hWriter, buffer, sizeof buffer/2, &count, NULL),
123 "can't write client\n");
124 ok( !ReadFile( hWriter, buffer, sizeof buffer/2, &count, NULL),
125 "can read client\n");
126 ok( GetLastError() == ERROR_INVALID_PARAMETER || GetLastError() == ERROR_ACCESS_DENIED,
127 "wrong error %u\n", GetLastError() );
130 * seeing as there's something in the slot,
131 * we should be able to read it once
133 ok( ReadFile( hSlot, buffer, sizeof buffer, &count, NULL),
135 ok( count == (sizeof buffer/2), "short read\n" );
138 ret = ReadFile( hSlot, buffer, sizeof buffer, &count, NULL);
139 ok( !ret, "slot read\n");
140 if (!ret) ok( GetLastError() == ERROR_SEM_TIMEOUT, "wrong error %u\n", GetLastError() );
141 else ok( count == 0, "wrong count %u\n", count );
143 /* now try open another writer... should fail */
144 hWriter2 = CreateFile(szmspath, GENERIC_READ|GENERIC_WRITE,
145 FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, NULL);
146 /* succeeds on vista, don't test */
147 if (hWriter2 != INVALID_HANDLE_VALUE) CloseHandle( hWriter2 );
149 /* now try open another as a reader ... also fails */
150 hWriter2 = CreateFile(szmspath, GENERIC_READ,
151 FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, NULL);
152 /* succeeds on vista, don't test */
153 if (hWriter2 != INVALID_HANDLE_VALUE) CloseHandle( hWriter2 );
155 /* now try open another as a writer ... still fails */
156 hWriter2 = CreateFile(szmspath, GENERIC_WRITE,
157 FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, NULL);
158 /* succeeds on vista, don't test */
159 if (hWriter2 != INVALID_HANDLE_VALUE) CloseHandle( hWriter2 );
161 /* now open another one */
162 hSlot2 = CreateMailslot( szmspath, 0, 0, NULL );
163 ok( hSlot2 == INVALID_HANDLE_VALUE , "opened two mailslots\n");
165 /* close the client again */
166 ok( CloseHandle( hWriter ), "closing the client\n");
169 * now try reopen it with slightly different permissions ...
172 hWriter = CreateFile(szmspath, GENERIC_WRITE,
173 FILE_SHARE_READ|FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, NULL);
174 ok( hWriter != INVALID_HANDLE_VALUE, "sharing writer\n");
177 * now try open another as a writer ...
178 * but don't share with the first ... fail
180 hWriter2 = CreateFile(szmspath, GENERIC_WRITE,
181 FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, NULL);
182 /* succeeds on vista, don't test */
183 if (hWriter2 != INVALID_HANDLE_VALUE) CloseHandle( hWriter2 );
185 /* now try open another as a writer ... and share with the first */
186 hWriter2 = CreateFile(szmspath, GENERIC_WRITE,
187 FILE_SHARE_READ|FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, NULL);
188 ok( hWriter2 != INVALID_HANDLE_VALUE, "2nd sharing writer\n");
190 /* check the mailslot info */
191 dwMax = dwNext = dwMsgCount = dwTimeout = 0;
192 ok( GetMailslotInfo( hSlot, &dwMax, &dwNext, &dwMsgCount, &dwTimeout ),
193 "getmailslotinfo failed\n");
194 ok( dwNext == MAILSLOT_NO_MESSAGE, "dwNext incorrect\n");
195 ok( dwMax == 0, "dwMax incorrect\n");
196 ok( dwMsgCount == 0, "dwMsgCount incorrect\n");
197 ok( dwTimeout == 0, "dwTimeout incorrect\n");
199 /* check there's still no data */
200 ret = ReadFile( hSlot, buffer, sizeof buffer, &count, NULL);
201 ok( !ret, "slot read\n");
202 if (!ret) ok( GetLastError() == ERROR_SEM_TIMEOUT, "wrong error %u\n", GetLastError() );
203 else ok( count == 0, "wrong count %u\n", count );
205 /* write two messages */
207 ok( WriteFile( hWriter, buffer, 1, &count, NULL), "1st write failed\n");
209 /* check the mailslot info */
210 dwNext = dwMsgCount = 0;
211 ok( GetMailslotInfo( hSlot, NULL, &dwNext, &dwMsgCount, NULL ),
212 "getmailslotinfo failed\n");
213 ok( dwNext == 1, "dwNext incorrect\n");
214 ok( dwMsgCount == 1, "dwMsgCount incorrect\n");
218 ok( WriteFile( hWriter2, buffer, 2, &count, NULL), "2nd write failed\n");
220 /* check the mailslot info */
221 dwNext = dwMsgCount = 0;
222 ok( GetMailslotInfo( hSlot, NULL, &dwNext, &dwMsgCount, NULL ),
223 "getmailslotinfo failed\n");
224 ok( dwNext == 1, "dwNext incorrect\n");
226 ok( dwMsgCount == 2, "dwMsgCount incorrect\n");
229 /* write a 3rd message with zero size */
230 ok( WriteFile( hWriter2, buffer, 0, &count, NULL), "3rd write failed\n");
232 /* check the mailslot info */
233 dwNext = dwMsgCount = 0;
234 ok( GetMailslotInfo( hSlot, NULL, &dwNext, &dwMsgCount, NULL ),
235 "getmailslotinfo failed\n");
236 ok( dwNext == 1, "dwNext incorrect\n");
238 ok( dwMsgCount == 3, "dwMsgCount incorrect %u\n", dwMsgCount);
240 buffer[0]=buffer[1]=0;
243 * then check that they come out with the correct order and size,
244 * then the slot is empty
246 ok( ReadFile( hSlot, buffer, sizeof buffer, &count, NULL),
247 "1st slot read failed\n");
248 ok( count == 1, "failed to get 1st message\n");
249 ok( buffer[0] == 'a', "1st message wrong\n");
251 /* check the mailslot info */
252 dwNext = dwMsgCount = 0;
253 ok( GetMailslotInfo( hSlot, NULL, &dwNext, &dwMsgCount, NULL ),
254 "getmailslotinfo failed\n");
255 ok( dwNext == 2, "dwNext incorrect\n");
257 ok( dwMsgCount == 2, "dwMsgCount incorrect %u\n", dwMsgCount);
260 /* read the second message */
261 ok( ReadFile( hSlot, buffer, sizeof buffer, &count, NULL),
262 "2nd slot read failed\n");
263 ok( count == 2, "failed to get 2nd message\n");
264 ok( ( buffer[0] == 'b' ) && ( buffer[1] == 'c' ), "2nd message wrong\n");
266 /* check the mailslot info */
267 dwNext = dwMsgCount = 0;
268 ok( GetMailslotInfo( hSlot, NULL, &dwNext, &dwMsgCount, NULL ),
269 "getmailslotinfo failed\n");
270 ok( dwNext == 0, "dwNext incorrect %u\n", dwNext);
272 ok( dwMsgCount == 1, "dwMsgCount incorrect %u\n", dwMsgCount);
275 /* read the 3rd (zero length) message */
277 ok( ReadFile( hSlot, buffer, sizeof buffer, &count, NULL),
278 "3rd slot read failed\n");
280 ok( count == 0, "failed to get 3rd message\n");
283 * now there should be no more messages
284 * check the mailslot info
286 dwNext = dwMsgCount = 0;
287 ok( GetMailslotInfo( hSlot, NULL, &dwNext, &dwMsgCount, NULL ),
288 "getmailslotinfo failed\n");
289 ok( dwNext == MAILSLOT_NO_MESSAGE, "dwNext incorrect\n");
290 ok( dwMsgCount == 0, "dwMsgCount incorrect\n");
292 /* check that reads fail */
293 ret = ReadFile( hSlot, buffer, sizeof buffer, &count, NULL);
294 ok( !ret, "3rd slot read succeeded\n");
295 if (!ret) ok( GetLastError() == ERROR_SEM_TIMEOUT, "wrong error %u\n", GetLastError() );
296 else ok( count == 0, "wrong count %u\n", count );
298 /* finally close the mailslot and its client */
299 ok( CloseHandle( hWriter2 ), "closing 2nd client\n");
300 ok( CloseHandle( hWriter ), "closing the client\n");
301 ok( CloseHandle( hSlot ), "closing the mailslot\n");
304 hSlot = CreateMailslot( szmspath, 0, 1000, NULL );
305 ok( hSlot != INVALID_HANDLE_VALUE , "valid mailslot failed\n");
307 memset(buffer, 0, sizeof buffer);
308 dwTimeout = GetTickCount();
309 ok( !ReadFile( hSlot, buffer, sizeof buffer, &count, NULL), "slot read\n");
310 ok( GetLastError() == ERROR_SEM_TIMEOUT, "wrong error %u\n", GetLastError() );
311 dwTimeout = GetTickCount() - dwTimeout;
312 ok( dwTimeout >= 990, "timeout too short %u\n", dwTimeout );
313 ok( CloseHandle( hSlot ), "closing the mailslot\n");