Added check for illegal pipe names.
[wine] / dlls / kernel / tests / pipe.c
1 /*
2  * Unit tests for named pipe functions in Wine
3  *
4  * Copyright (c) 2002 Dan Kegel
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 <stdlib.h>
22 #include <stdio.h>
23 #include <time.h>
24
25 #ifndef STANDALONE
26 #include "wine/test.h"
27 #else
28 #include <assert.h>
29 #define START_TEST(name) main(int argc, char **argv)
30 #define ok(condition, msg) assert(condition)
31 #endif
32
33 #include <windef.h>
34 #include <winbase.h>
35 #include <winerror.h>
36 #include <wtypes.h>
37
38 #define PIPENAME "\\\\.\\PiPe\\tests_" __FILE__
39
40 void test_CreateNamedPipeA(void)
41 {
42     HANDLE hnp;
43     HANDLE hFile;
44     const char obuf[] = "Bit Bucket";
45     char ibuf[32];
46     DWORD written;
47     DWORD gelesen;
48
49     /* Bad parameter checks */
50     hnp = CreateNamedPipeA("not a named pipe", 
51         PIPE_ACCESS_DUPLEX, PIPE_TYPE_BYTE|PIPE_WAIT, 
52         /* nMaxInstances */ 1,
53         /* nOutBufSize */ 1024,
54         /* nInBufSize */ 1024,
55         /* nDefaultWait */ NMPWAIT_USE_DEFAULT_WAIT,
56         /* lpSecurityAttrib */ NULL);
57
58     if (hnp == INVALID_HANDLE_VALUE && GetLastError() == ERROR_CALL_NOT_IMPLEMENTED) {
59         /* Is this the right way to notify user of skipped tests? */
60         ok(hnp == INVALID_HANDLE_VALUE && GetLastError() == ERROR_CALL_NOT_IMPLEMENTED, 
61             "CreateNamedPipe not supported on this platform, skipping tests.");
62         return;
63     }
64     ok(hnp == INVALID_HANDLE_VALUE && GetLastError() == ERROR_INVALID_NAME,
65         "CreateNamedPipe should fail if name doesn't start with \\\\.\\pipe");
66
67     hnp = CreateNamedPipeA(NULL, 
68         PIPE_ACCESS_DUPLEX, PIPE_TYPE_BYTE|PIPE_WAIT, 
69         1, 1024, 1024, NMPWAIT_USE_DEFAULT_WAIT, NULL);
70     ok(hnp == INVALID_HANDLE_VALUE && GetLastError() == ERROR_PATH_NOT_FOUND,
71         "CreateNamedPipe should fail if name is NULL");
72
73     /* Functional checks */
74
75     hnp = CreateNamedPipeA(PIPENAME,
76         PIPE_ACCESS_DUPLEX, PIPE_TYPE_BYTE|PIPE_WAIT, 
77         /* nMaxInstances */ 1,
78         /* nOutBufSize */ 1024,
79         /* nInBufSize */ 1024,
80         /* nDefaultWait */ NMPWAIT_USE_DEFAULT_WAIT,
81         /* lpSecurityAttrib */ NULL);
82     ok(hnp != INVALID_HANDLE_VALUE, "CreateNamedPipe failed");
83
84     hFile = CreateFileA(PIPENAME, GENERIC_READ|GENERIC_WRITE, 0, 
85             NULL, OPEN_EXISTING, 0, 0);
86     todo_wine
87     {
88         ok(hFile != INVALID_HANDLE_VALUE, "CreateFile failed");
89     }
90
91     /* don't try to do i/o if one side couldn't be opened, as it hangs */
92     if (hFile != INVALID_HANDLE_VALUE) {
93         /* Make sure we can read and write a few bytes in both directions*/
94         memset(ibuf, 0, sizeof(ibuf));
95         ok(WriteFile(hnp, obuf, sizeof(obuf), &written, NULL), "WriteFile");
96         ok(written == sizeof(obuf), "write file len");
97         ok(ReadFile(hFile, ibuf, sizeof(obuf), &gelesen, NULL), "ReadFile");
98         ok(gelesen == sizeof(obuf), "read file len");
99         ok(memcmp(obuf, ibuf, written) == 0, "content check");
100
101         memset(ibuf, 0, sizeof(ibuf));
102         ok(WriteFile(hFile, obuf, sizeof(obuf), &written, NULL), "WriteFile");
103         ok(written == sizeof(obuf), "write file len");
104         ok(ReadFile(hnp, ibuf, sizeof(obuf), &gelesen, NULL), "ReadFile");
105         ok(gelesen == sizeof(obuf), "read file len");
106         ok(memcmp(obuf, ibuf, written) == 0, "content check");
107
108         CloseHandle(hFile);
109     }
110
111     CloseHandle(hnp);
112 }
113
114 START_TEST(pipe)
115 {
116     test_CreateNamedPipeA();
117 }