include: Assorted spelling fixes.
[wine] / dlls / kernel32 / tests / mailslot.c
1 /*
2  *  Mailslot regression test
3  *
4  *  Copyright 2003 Mike McCormack
5  *
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.
10  *
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.
15  *
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
19  */
20
21 #include <stdarg.h>
22 #include <stdlib.h>
23 #include <stdio.h>
24
25 #include <windef.h>
26 #include <winbase.h>
27
28 #include "wine/test.h"
29
30 static const char szmspath[] = "\\\\.\\mailslot\\wine_mailslot_test";
31
32 static int mailslot_test(void)
33 {
34     HANDLE hSlot, hSlot2, hWriter, hWriter2;
35     unsigned char buffer[16];
36     DWORD count, dwMax, dwNext, dwMsgCount, dwTimeout;
37     BOOL ret;
38
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");
43
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");
48
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");
55
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");
60
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");
74
75     /* now open it for real */
76     hSlot = CreateMailslot( szmspath, 0, 0, NULL );
77     ok( hSlot != INVALID_HANDLE_VALUE , "valid mailslot failed\n");
78
79     /* try and read/write to it */
80     count = 0;
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),
87             "slot write\n");
88     ok( GetLastError() == ERROR_ACCESS_DENIED, "wrong error %u\n", GetLastError() );
89
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 );
96
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());
101
102     /*
103      * opening a client should make no difference to
104      * whether we can read or write the mailslot
105      */
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),
111             "slot write\n");
112     ok( GetLastError() == ERROR_ACCESS_DENIED, "wrong error %u\n", GetLastError() );
113
114     /*
115      * we can't read from this client, 
116      * but we should be able to write to it
117      */
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() );
128
129     /*
130      * seeing as there's something in the slot,
131      * we should be able to read it once
132      */
133     ok( ReadFile( hSlot, buffer, sizeof buffer, &count, NULL),
134             "slot read\n");
135     ok( count == (sizeof buffer/2), "short read\n" );
136
137     /* but not again */
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 );
142
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 );
148
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 );
154
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 );
160
161     /* now open another one */
162     hSlot2 = CreateMailslot( szmspath, 0, 0, NULL );
163     ok( hSlot2 == INVALID_HANDLE_VALUE , "opened two mailslots\n");
164
165     /* close the client again */
166     ok( CloseHandle( hWriter ), "closing the client\n");
167
168     /*
169      * now try reopen it with slightly different permissions ...
170      * shared writing
171      */
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");
175
176     /*
177      * now try open another as a writer ...
178      * but don't share with the first ... fail
179      */
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 );
184
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");
189
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");
198
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 );
204
205     /* write two messages */
206     buffer[0] = 'a';
207     ok( WriteFile( hWriter, buffer, 1, &count, NULL), "1st write failed\n");
208
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");
215
216     buffer[0] = 'b';
217     buffer[1] = 'c';
218     ok( WriteFile( hWriter2, buffer, 2, &count, NULL), "2nd write failed\n");
219
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");
225     todo_wine {
226     ok( dwMsgCount == 2, "dwMsgCount incorrect\n");
227     }
228
229     /* write a 3rd message with zero size */
230     ok( WriteFile( hWriter2, buffer, 0, &count, NULL), "3rd write failed\n");
231
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");
237     todo_wine
238         ok( dwMsgCount == 3, "dwMsgCount incorrect %u\n", dwMsgCount);
239
240     buffer[0]=buffer[1]=0;
241
242     /*
243      * then check that they come out with the correct order and size,
244      * then the slot is empty
245      */
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");
250
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");
256     todo_wine {
257         ok( dwMsgCount == 2, "dwMsgCount incorrect %u\n", dwMsgCount);
258     }
259
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");
265
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);
271     todo_wine {
272         ok( dwMsgCount == 1, "dwMsgCount incorrect %u\n", dwMsgCount);
273     }
274
275     /* read the 3rd (zero length) message */
276     todo_wine {
277     ok( ReadFile( hSlot, buffer, sizeof buffer, &count, NULL),
278         "3rd slot read failed\n");
279     }
280     ok( count == 0, "failed to get 3rd message\n");
281
282     /*
283      * now there should be no more messages
284      * check the mailslot info
285      */
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");
291
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 );
297
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");
302
303     /* test timeouts */
304     hSlot = CreateMailslot( szmspath, 0, 1000, NULL );
305     ok( hSlot != INVALID_HANDLE_VALUE , "valid mailslot failed\n");
306     count = 0;
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");
314
315     return 0;
316 }
317
318 START_TEST(mailslot)
319 {
320     mailslot_test();
321 }