mailslot.c 12.6 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
/*
 *  Mailslot regression test
 *
 *  Copyright 2003 Mike McCormack
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * 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 22 23 24 25 26 27 28 29
 */

#include <stdarg.h>
#include <stdlib.h>
#include <stdio.h>

#include <windef.h>
#include <winbase.h>

#include "wine/test.h"

30
static const char szmspath[] = "\\\\.\\mailslot\\wine_mailslot_test";
31

32
static int mailslot_test(void)
33 34 35 36
{
    HANDLE hSlot, hSlot2, hWriter, hWriter2;
    unsigned char buffer[16];
    DWORD count, dwMax, dwNext, dwMsgCount, dwTimeout;
37
    BOOL ret;
38 39 40 41

    /* sanity check on GetMailslotInfo */
    dwMax = dwNext = dwMsgCount = dwTimeout = 0;
    ok( !GetMailslotInfo( INVALID_HANDLE_VALUE, &dwMax, &dwNext,
42
            &dwMsgCount, &dwTimeout ), "getmailslotinfo succeeded\n");
43 44 45 46

    /* open a mailslot that doesn't exist */
    hWriter = CreateFile(szmspath, GENERIC_READ|GENERIC_WRITE,
                             FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, NULL);
47
    ok( hWriter == INVALID_HANDLE_VALUE, "nonexistent mailslot\n");
48 49 50 51

    /* open a mailslot without the right name */
    hSlot = CreateMailslot( "blah", 0, 0, NULL );
    ok( hSlot == INVALID_HANDLE_VALUE,
52
            "Created mailslot with invalid name\n");
53 54
    ok( GetLastError() == ERROR_INVALID_NAME,
            "error should be ERROR_INVALID_NAME\n");
55 56 57

    /* open a mailslot with a null name */
    hSlot = CreateMailslot( NULL, 0, 0, NULL );
58 59
    ok( hSlot == INVALID_HANDLE_VALUE, "Created mailslot with invalid name\n");
    ok( GetLastError() == ERROR_PATH_NOT_FOUND, "error should be ERROR_PATH_NOT_FOUND\n");
60 61 62

    /* valid open, but with wacky parameters ... then check them */
    hSlot = CreateMailslot( szmspath, -1, -1, NULL );
63
    ok( hSlot != INVALID_HANDLE_VALUE , "mailslot with valid name failed\n");
64 65
    dwMax = dwNext = dwMsgCount = dwTimeout = 0;
    ok( GetMailslotInfo( hSlot, &dwMax, &dwNext, &dwMsgCount, &dwTimeout ),
66
           "getmailslotinfo failed\n");
67
    ok( dwMax == ~0U, "dwMax incorrect\n");
68 69
    ok( dwNext == MAILSLOT_NO_MESSAGE, "dwNext incorrect\n");
    ok( dwMsgCount == 0, "dwMsgCount incorrect\n");
70
    ok( dwTimeout == ~0U, "dwTimeout incorrect\n");
71
    ok( GetMailslotInfo( hSlot, NULL, NULL, NULL, NULL ),
72 73
            "getmailslotinfo failed\n");
    ok( CloseHandle(hSlot), "failed to close mailslot\n");
74 75 76

    /* now open it for real */
    hSlot = CreateMailslot( szmspath, 0, 0, NULL );
77
    ok( hSlot != INVALID_HANDLE_VALUE , "valid mailslot failed\n");
78 79 80 81

    /* try and read/write to it */
    count = 0;
    memset(buffer, 0, sizeof buffer);
82
    ret = ReadFile( hSlot, buffer, sizeof buffer, &count, NULL);
83
    ok( !ret, "slot read\n");
84 85
    if (!ret) ok( GetLastError() == ERROR_SEM_TIMEOUT, "wrong error %u\n", GetLastError() );
    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 91
    /* now try and open the client, but with the wrong sharing mode */
    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 98 99

    /* now open the client with the correct sharing mode */
    hWriter = CreateFile(szmspath, GENERIC_READ|GENERIC_WRITE,
                             FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, NULL);
100
    ok( hWriter != INVALID_HANDLE_VALUE, "existing mailslot err %u\n", GetLastError());
101 102 103 104 105

    /*
     * opening a client should make no difference to
     * 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 109
    if (!ret) ok( GetLastError() == ERROR_SEM_TIMEOUT, "wrong error %u\n", GetLastError() );
    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 116 117 118

    /*
     * we can't read from this client, 
     * but we should be able to write to it
     */
    ok( !ReadFile( hWriter, buffer, sizeof buffer/2, &count, NULL),
119
            "can read client\n");
120 121
    ok( GetLastError() == ERROR_INVALID_PARAMETER || GetLastError() == ERROR_ACCESS_DENIED,
            "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 127
    ok( GetLastError() == ERROR_INVALID_PARAMETER || GetLastError() == ERROR_ACCESS_DENIED,
            "wrong error %u\n", GetLastError() );
128 129 130 131 132 133

    /*
     * seeing as there's something in the slot,
     * we should be able to read it once
     */
    ok( ReadFile( hSlot, buffer, sizeof buffer, &count, NULL),
134 135
            "slot read\n");
    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 141
    if (!ret) ok( GetLastError() == ERROR_SEM_TIMEOUT, "wrong error %u\n", GetLastError() );
    else ok( count == 0, "wrong count %u\n", count );
142 143 144 145

    /* now try open another writer... should fail */
    hWriter2 = CreateFile(szmspath, GENERIC_READ|GENERIC_WRITE,
                     FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, NULL);
146 147
    /* succeeds on vista, don't test */
    if (hWriter2 != INVALID_HANDLE_VALUE) CloseHandle( hWriter2 );
148 149 150 151

    /* now try open another as a reader ... also fails */
    hWriter2 = CreateFile(szmspath, GENERIC_READ,
                     FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, NULL);
152 153
    /* succeeds on vista, don't test */
    if (hWriter2 != INVALID_HANDLE_VALUE) CloseHandle( hWriter2 );
154 155 156 157

    /* now try open another as a writer ... still fails */
    hWriter2 = CreateFile(szmspath, GENERIC_WRITE,
                     FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, NULL);
158 159
    /* succeeds on vista, don't test */
    if (hWriter2 != INVALID_HANDLE_VALUE) CloseHandle( hWriter2 );
160 161 162

    /* now open another one */
    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 170 171 172 173

    /*
     * now try reopen it with slightly different permissions ...
     * shared writing
     */
    hWriter = CreateFile(szmspath, GENERIC_WRITE,
              FILE_SHARE_READ|FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, NULL);
174
    ok( hWriter != INVALID_HANDLE_VALUE, "sharing writer\n");
175 176 177 178 179 180 181

    /*
     * now try open another as a writer ...
     * but don't share with the first ... fail
     */
    hWriter2 = CreateFile(szmspath, GENERIC_WRITE,
                     FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, NULL);
182 183
    /* succeeds on vista, don't test */
    if (hWriter2 != INVALID_HANDLE_VALUE) CloseHandle( hWriter2 );
184 185 186 187

    /* now try open another as a writer ... and share with the first */
    hWriter2 = CreateFile(szmspath, GENERIC_WRITE,
              FILE_SHARE_READ|FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, NULL);
188
    ok( hWriter2 != INVALID_HANDLE_VALUE, "2nd sharing writer\n");
189 190 191 192

    /* check the mailslot info */
    dwMax = dwNext = dwMsgCount = dwTimeout = 0;
    ok( GetMailslotInfo( hSlot, &dwMax, &dwNext, &dwMsgCount, &dwTimeout ),
193 194 195 196 197
        "getmailslotinfo failed\n");
    ok( dwNext == MAILSLOT_NO_MESSAGE, "dwNext incorrect\n");
    ok( dwMax == 0, "dwMax incorrect\n");
    ok( dwMsgCount == 0, "dwMsgCount incorrect\n");
    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 203
    if (!ret) ok( GetLastError() == ERROR_SEM_TIMEOUT, "wrong error %u\n", GetLastError() );
    else ok( count == 0, "wrong count %u\n", count );
204 205 206

    /* write two messages */
    buffer[0] = 'a';
207
    ok( WriteFile( hWriter, buffer, 1, &count, NULL), "1st write failed\n");
208 209 210 211

    /* check the mailslot info */
    dwNext = dwMsgCount = 0;
    ok( GetMailslotInfo( hSlot, NULL, &dwNext, &dwMsgCount, NULL ),
212 213 214
        "getmailslotinfo failed\n");
    ok( dwNext == 1, "dwNext incorrect\n");
    ok( dwMsgCount == 1, "dwMsgCount incorrect\n");
215 216 217

    buffer[0] = 'b';
    buffer[1] = 'c';
218
    ok( WriteFile( hWriter2, buffer, 2, &count, NULL), "2nd write failed\n");
219 220 221 222

    /* check the mailslot info */
    dwNext = dwMsgCount = 0;
    ok( GetMailslotInfo( hSlot, NULL, &dwNext, &dwMsgCount, NULL ),
223 224
        "getmailslotinfo failed\n");
    ok( dwNext == 1, "dwNext incorrect\n");
225
    todo_wine {
226
    ok( dwMsgCount == 2, "dwMsgCount incorrect\n");
Mike McCormack's avatar
Mike McCormack committed
227
    }
228 229

    /* write a 3rd message with zero size */
230
    ok( WriteFile( hWriter2, buffer, 0, &count, NULL), "3rd write failed\n");
231 232 233 234

    /* check the mailslot info */
    dwNext = dwMsgCount = 0;
    ok( GetMailslotInfo( hSlot, NULL, &dwNext, &dwMsgCount, NULL ),
235 236
        "getmailslotinfo failed\n");
    ok( dwNext == 1, "dwNext incorrect\n");
237
    todo_wine
238
        ok( dwMsgCount == 3, "dwMsgCount incorrect %u\n", dwMsgCount);
239 240 241 242 243 244 245 246

    buffer[0]=buffer[1]=0;

    /*
     * then check that they come out with the correct order and size,
     * then the slot is empty
     */
    ok( ReadFile( hSlot, buffer, sizeof buffer, &count, NULL),
247 248 249
        "1st slot read failed\n");
    ok( count == 1, "failed to get 1st message\n");
    ok( buffer[0] == 'a', "1st message wrong\n");
250 251 252 253

    /* check the mailslot info */
    dwNext = dwMsgCount = 0;
    ok( GetMailslotInfo( hSlot, NULL, &dwNext, &dwMsgCount, NULL ),
254 255
        "getmailslotinfo failed\n");
    ok( dwNext == 2, "dwNext incorrect\n");
256
    todo_wine {
257
        ok( dwMsgCount == 2, "dwMsgCount incorrect %u\n", dwMsgCount);
Mike McCormack's avatar
Mike McCormack committed
258
    }
259 260 261

    /* read the second message */
    ok( ReadFile( hSlot, buffer, sizeof buffer, &count, NULL),
262 263 264
        "2nd slot read failed\n");
    ok( count == 2, "failed to get 2nd message\n");
    ok( ( buffer[0] == 'b' ) && ( buffer[1] == 'c' ), "2nd message wrong\n");
265 266 267 268

    /* check the mailslot info */
    dwNext = dwMsgCount = 0;
    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);
Mike McCormack's avatar
Mike McCormack committed
273
    }
274 275

    /* read the 3rd (zero length) message */
Mike McCormack's avatar
Mike McCormack committed
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 284 285 286 287

    /*
     * now there should be no more messages
     * check the mailslot info
     */
    dwNext = dwMsgCount = 0;
    ok( GetMailslotInfo( hSlot, NULL, &dwNext, &dwMsgCount, NULL ),
288 289 290
        "getmailslotinfo failed\n");
    ok( dwNext == MAILSLOT_NO_MESSAGE, "dwNext incorrect\n");
    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 296
    if (!ret) ok( GetLastError() == ERROR_SEM_TIMEOUT, "wrong error %u\n", GetLastError() );
    else ok( count == 0, "wrong count %u\n", count );
297 298

    /* finally close the mailslot and its client */
299 300 301
    ok( CloseHandle( hWriter2 ), "closing 2nd client\n");
    ok( CloseHandle( hWriter ), "closing the client\n");
    ok( CloseHandle( hSlot ), "closing the mailslot\n");
302

303 304 305 306 307 308 309
    /* test timeouts */
    hSlot = CreateMailslot( szmspath, 0, 1000, NULL );
    ok( hSlot != INVALID_HANDLE_VALUE , "valid mailslot failed\n");
    count = 0;
    memset(buffer, 0, sizeof buffer);
    dwTimeout = GetTickCount();
    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 314
    ok( CloseHandle( hSlot ), "closing the mailslot\n");

315 316 317 318 319 320 321
    return 0;
}

START_TEST(mailslot)
{
    mailslot_test();
}