Fix segmentation fault caused by incorrect referencing of client audio
[wine] / dlls / kernel / 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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  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 #ifndef STANDALONE
29 #include "wine/test.h"
30 #else
31 #define START_TEST(name) main(int argc, char **argv)
32
33 #define ok(cond,str) do{ if(!(cond)) printf("line %d: %s\n",__LINE__,str); }while (0)
34 #define todo_wine
35
36 #endif
37
38 static const char szmspath[] = "\\\\.\\mailslot\\wine_mailslot_test";
39
40 static int mailslot_test()
41 {
42     HANDLE hSlot, hSlot2, hWriter, hWriter2;
43     unsigned char buffer[16];
44     DWORD count, dwMax, dwNext, dwMsgCount, dwTimeout;
45
46     /* sanity check on GetMailslotInfo */
47     dwMax = dwNext = dwMsgCount = dwTimeout = 0;
48     ok( !GetMailslotInfo( INVALID_HANDLE_VALUE, &dwMax, &dwNext,
49             &dwMsgCount, &dwTimeout ), "getmailslotinfo succeeded\n");
50
51     /* open a mailslot that doesn't exist */
52     hWriter = CreateFile(szmspath, GENERIC_READ|GENERIC_WRITE,
53                              FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, NULL);
54     ok( hWriter == INVALID_HANDLE_VALUE, "nonexistent mailslot\n");
55
56     /* open a mailslot without the right name */
57     hSlot = CreateMailslot( "blah", 0, 0, NULL );
58     ok( hSlot == INVALID_HANDLE_VALUE,
59             "Created mailslot with invalid name\n");
60     ok( GetLastError() == ERROR_INVALID_NAME,
61             "error should be ERROR_INVALID_NAME\n");
62
63     /* open a mailslot with a null name */
64     hSlot = CreateMailslot( NULL, 0, 0, NULL );
65     ok( hSlot == INVALID_HANDLE_VALUE,
66             "Created mailslot with invalid name\n");
67     ok( GetLastError() == ERROR_PATH_NOT_FOUND,
68             "error should be ERROR_PATH_NOT_FOUND\n");
69
70     /* valid open, but with wacky parameters ... then check them */
71     hSlot = CreateMailslot( szmspath, -1, -1, NULL );
72     ok( hSlot != INVALID_HANDLE_VALUE , "mailslot with valid name failed\n");
73     dwMax = dwNext = dwMsgCount = dwTimeout = 0;
74     ok( GetMailslotInfo( hSlot, &dwMax, &dwNext, &dwMsgCount, &dwTimeout ),
75            "getmailslotinfo failed\n");
76     ok( dwMax == ~0UL, "dwMax incorrect\n");
77     ok( dwNext == MAILSLOT_NO_MESSAGE, "dwNext incorrect\n");
78     ok( dwMsgCount == 0, "dwMsgCount incorrect\n");
79     ok( dwTimeout == ~0UL, "dwTimeout incorrect\n");
80     ok( GetMailslotInfo( hSlot, NULL, NULL, NULL, NULL ),
81             "getmailslotinfo failed\n");
82     ok( CloseHandle(hSlot), "failed to close mailslot\n");
83
84     /* now open it for real */
85     hSlot = CreateMailslot( szmspath, 0, 0, NULL );
86     ok( hSlot != INVALID_HANDLE_VALUE , "valid mailslot failed\n");
87
88     /* try and read/write to it */
89     count = 0;
90     memset(buffer, 0, sizeof buffer);
91     ok( !ReadFile( hSlot, buffer, sizeof buffer, &count, NULL),
92             "slot read\n");
93     ok( !WriteFile( hSlot, buffer, sizeof buffer, &count, NULL),
94             "slot write\n");
95
96     /* now try and openthe client, but with the wrong sharing mode */
97     hWriter = CreateFile(szmspath, GENERIC_READ|GENERIC_WRITE,
98                              0, NULL, OPEN_EXISTING, 0, NULL);
99     ok( hWriter == INVALID_HANDLE_VALUE, "bad sharing mode\n");
100     ok( GetLastError() == ERROR_SHARING_VIOLATION,
101             "error should be ERROR_SHARING_VIOLATION\n");
102
103     /* now open the client with the correct sharing mode */
104     hWriter = CreateFile(szmspath, GENERIC_READ|GENERIC_WRITE,
105                              FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, NULL);
106     ok( hWriter != INVALID_HANDLE_VALUE, "existing mailslot\n");
107
108     /*
109      * opening a client should make no difference to
110      * whether we can read or write the mailslot
111      */
112     ok( !ReadFile( hSlot, buffer, sizeof buffer/2, &count, NULL),
113             "slot read\n");
114     ok( !WriteFile( hSlot, buffer, sizeof buffer/2, &count, NULL),
115             "slot write\n");
116
117     /*
118      * we can't read from this client, 
119      * but we should be able to write to it
120      */
121     ok( !ReadFile( hWriter, buffer, sizeof buffer/2, &count, NULL),
122             "can read client\n");
123     ok( WriteFile( hWriter, buffer, sizeof buffer/2, &count, NULL),
124             "can't write client\n");
125     ok( !ReadFile( hWriter, buffer, sizeof buffer/2, &count, NULL),
126             "can read client\n");
127
128     /*
129      * seeing as there's something in the slot,
130      * we should be able to read it once
131      */
132     ok( ReadFile( hSlot, buffer, sizeof buffer, &count, NULL),
133             "slot read\n");
134     ok( count == (sizeof buffer/2), "short read\n" );
135
136     /* but not again */
137     ok( !ReadFile( hSlot, buffer, sizeof buffer, &count, NULL),
138             "slot read\n");
139
140     /* now try open another writer... should fail */
141     hWriter2 = CreateFile(szmspath, GENERIC_READ|GENERIC_WRITE,
142                      FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, NULL);
143     ok( hWriter2 == INVALID_HANDLE_VALUE, "two writers\n");
144
145     /* now try open another as a reader ... also fails */
146     hWriter2 = CreateFile(szmspath, GENERIC_READ,
147                      FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, NULL);
148     ok( hWriter2 == INVALID_HANDLE_VALUE, "writer + reader\n");
149
150     /* now try open another as a writer ... still fails */
151     hWriter2 = CreateFile(szmspath, GENERIC_WRITE,
152                      FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, NULL);
153     ok( hWriter2 == INVALID_HANDLE_VALUE, "writer\n");
154
155     /* now open another one */
156     hSlot2 = CreateMailslot( szmspath, 0, 0, NULL );
157     ok( hSlot2 == INVALID_HANDLE_VALUE , "opened two mailslots\n");
158
159     /* close the client again */
160     ok( CloseHandle( hWriter ), "closing the client\n");
161
162     /*
163      * now try reopen it with slightly different permissions ...
164      * shared writing
165      */
166     hWriter = CreateFile(szmspath, GENERIC_WRITE,
167               FILE_SHARE_READ|FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, NULL);
168     ok( hWriter != INVALID_HANDLE_VALUE, "sharing writer\n");
169
170     /*
171      * now try open another as a writer ...
172      * but don't share with the first ... fail
173      */
174     hWriter2 = CreateFile(szmspath, GENERIC_WRITE,
175                      FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, NULL);
176     ok( hWriter2 == INVALID_HANDLE_VALUE, "greedy writer succeeded\n");
177
178     /* now try open another as a writer ... and share with the first */
179     hWriter2 = CreateFile(szmspath, GENERIC_WRITE,
180               FILE_SHARE_READ|FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, NULL);
181     ok( hWriter2 != INVALID_HANDLE_VALUE, "2nd sharing writer\n");
182
183     /* check the mailslot info */
184     dwMax = dwNext = dwMsgCount = dwTimeout = 0;
185     ok( GetMailslotInfo( hSlot, &dwMax, &dwNext, &dwMsgCount, &dwTimeout ),
186         "getmailslotinfo failed\n");
187     ok( dwNext == MAILSLOT_NO_MESSAGE, "dwNext incorrect\n");
188     ok( dwMax == 0, "dwMax incorrect\n");
189     ok( dwMsgCount == 0, "dwMsgCount incorrect\n");
190     ok( dwTimeout == 0, "dwTimeout incorrect\n");
191
192     /* check there's still no data */
193     ok( !ReadFile( hSlot, buffer, sizeof buffer, &count, NULL), "slot read\n");
194
195     /* write two messages */
196     buffer[0] = 'a';
197     ok( WriteFile( hWriter, buffer, 1, &count, NULL), "1st write failed\n");
198
199     /* check the mailslot info */
200     dwNext = dwMsgCount = 0;
201     ok( GetMailslotInfo( hSlot, NULL, &dwNext, &dwMsgCount, NULL ),
202         "getmailslotinfo failed\n");
203     ok( dwNext == 1, "dwNext incorrect\n");
204     ok( dwMsgCount == 1, "dwMsgCount incorrect\n");
205
206     buffer[0] = 'b';
207     buffer[1] = 'c';
208     ok( WriteFile( hWriter2, buffer, 2, &count, NULL), "2nd write failed\n");
209
210     /* check the mailslot info */
211     dwNext = dwMsgCount = 0;
212     ok( GetMailslotInfo( hSlot, NULL, &dwNext, &dwMsgCount, NULL ),
213         "getmailslotinfo failed\n");
214     ok( dwNext == 1, "dwNext incorrect\n");
215     todo_wine {
216     ok( dwMsgCount == 2, "dwMsgCount incorrect\n");
217     }
218
219     /* write a 3rd message with zero size */
220     ok( WriteFile( hWriter2, buffer, 0, &count, NULL), "3rd write failed\n");
221
222     /* check the mailslot info */
223     dwNext = dwMsgCount = 0;
224     ok( GetMailslotInfo( hSlot, NULL, &dwNext, &dwMsgCount, NULL ),
225         "getmailslotinfo failed\n");
226     ok( dwNext == 1, "dwNext incorrect\n");
227     todo_wine {
228     ok( dwMsgCount == 3, "dwMsgCount incorrect\n");
229     }
230
231     buffer[0]=buffer[1]=0;
232
233     /*
234      * then check that they come out with the correct order and size,
235      * then the slot is empty
236      */
237     ok( ReadFile( hSlot, buffer, sizeof buffer, &count, NULL),
238         "1st slot read failed\n");
239     ok( count == 1, "failed to get 1st message\n");
240     ok( buffer[0] == 'a', "1st message wrong\n");
241
242     /* check the mailslot info */
243     dwNext = dwMsgCount = 0;
244     ok( GetMailslotInfo( hSlot, NULL, &dwNext, &dwMsgCount, NULL ),
245         "getmailslotinfo failed\n");
246     ok( dwNext == 2, "dwNext incorrect\n");
247     todo_wine {
248     ok( dwMsgCount == 2, "dwMsgCount incorrect\n");
249     }
250
251     /* read the second message */
252     ok( ReadFile( hSlot, buffer, sizeof buffer, &count, NULL),
253         "2nd slot read failed\n");
254     ok( count == 2, "failed to get 2nd message\n");
255     ok( ( buffer[0] == 'b' ) && ( buffer[1] == 'c' ), "2nd message wrong\n");
256
257     /* check the mailslot info */
258     dwNext = dwMsgCount = 0;
259     ok( GetMailslotInfo( hSlot, NULL, &dwNext, &dwMsgCount, NULL ),
260         "getmailslotinfo failed\n");
261     todo_wine {
262     ok( dwNext == 0, "dwNext incorrect\n");
263     ok( dwMsgCount == 1, "dwMsgCount incorrect\n");
264     }
265
266     /* read the 3rd (zero length) message */
267     todo_wine {
268     ok( ReadFile( hSlot, buffer, sizeof buffer, &count, NULL),
269         "3rd slot read failed\n");
270     }
271     ok( count == 0, "failed to get 3rd message\n");
272
273     /*
274      * now there should be no more messages
275      * check the mailslot info
276      */
277     dwNext = dwMsgCount = 0;
278     ok( GetMailslotInfo( hSlot, NULL, &dwNext, &dwMsgCount, NULL ),
279         "getmailslotinfo failed\n");
280     ok( dwNext == MAILSLOT_NO_MESSAGE, "dwNext incorrect\n");
281     ok( dwMsgCount == 0, "dwMsgCount incorrect\n");
282
283     /* check that reads fail */
284     ok( !ReadFile( hSlot, buffer, sizeof buffer, &count, NULL),
285         "3rd slot read succeeded\n");
286
287     /* finally close the mailslot and its client */
288     ok( CloseHandle( hWriter2 ), "closing 2nd client\n");
289     ok( CloseHandle( hWriter ), "closing the client\n");
290     ok( CloseHandle( hSlot ), "closing the mailslot\n");
291
292     return 0;
293 }
294
295 START_TEST(mailslot)
296 {
297     mailslot_test();
298 }