localspl/tests: Add test for ConfigurePortW.
[wine] / dlls / localspl / tests / localmon.c
1 /* 
2  * Unit test suite for localspl API functions: local print monitor
3  *
4  * Copyright 2006 Detlef Riekenberg
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
22 #include <stdarg.h>
23
24 #include "windef.h"
25 #include "winbase.h"
26 #include "winerror.h"
27 #include "wingdi.h"
28 #include "winreg.h"
29
30 #include "winspool.h"
31 #include "ddk/winsplp.h"
32
33 #include "wine/test.h"
34
35
36 /* ##### */
37
38 static HMODULE  hdll;
39 static HMODULE  hlocalmon;
40 static LPMONITOREX (WINAPI *pInitializePrintMonitor)(LPWSTR);
41
42 static LPMONITOREX pm;
43 static BOOL  (WINAPI *pEnumPorts)(LPWSTR, DWORD, LPBYTE, DWORD, LPDWORD, LPDWORD);
44 static BOOL  (WINAPI *pOpenPort)(LPWSTR, PHANDLE);
45 static BOOL  (WINAPI *pOpenPortEx)(LPWSTR, LPWSTR, PHANDLE, struct _MONITOR *);
46 static BOOL  (WINAPI *pStartDocPort)(HANDLE, LPWSTR, DWORD, DWORD, LPBYTE);
47 static BOOL  (WINAPI *pWritePort)(HANDLE hPort, LPBYTE, DWORD, LPDWORD);
48 static BOOL  (WINAPI *pReadPort)(HANDLE hPort, LPBYTE, DWORD, LPDWORD);
49 static BOOL  (WINAPI *pEndDocPort)(HANDLE);
50 static BOOL  (WINAPI *pClosePort)(HANDLE);
51 static BOOL  (WINAPI *pAddPort)(LPWSTR, HWND, LPWSTR);
52 static BOOL  (WINAPI *pAddPortEx)(LPWSTR, DWORD, LPBYTE, LPWSTR);
53 static BOOL  (WINAPI *pConfigurePort)(LPWSTR, HWND, LPWSTR);
54 static BOOL  (WINAPI *pDeletePort)(LPWSTR, HWND, LPWSTR);
55 static BOOL  (WINAPI *pGetPrinterDataFromPort)(HANDLE, DWORD, LPWSTR, LPWSTR, DWORD, LPWSTR, DWORD, LPDWORD);
56 static BOOL  (WINAPI *pSetPortTimeOuts)(HANDLE, LPCOMMTIMEOUTS, DWORD);
57 static BOOL  (WINAPI *pXcvOpenPort)(HANDLE, LPCWSTR, ACCESS_MASK, PHANDLE phXcv);
58 static DWORD (WINAPI *pXcvDataPort)(HANDLE, LPCWSTR, PBYTE, DWORD, PBYTE, DWORD, PDWORD);
59 static BOOL  (WINAPI *pXcvClosePort)(HANDLE);
60
61 static WCHAR emptyW[] = {0};
62 static WCHAR invalid_serverW[] = {'\\','\\','i','n','v','a','l','i','d','_','s','e','r','v','e','r',0};
63 static WCHAR Monitors_LocalPortW[] = { \
64                                 'S','y','s','t','e','m','\\',
65                                 'C','u','r','r','e','n','t','C','o','n','t','r','o','l','S','e','t','\\',
66                                 'C','o','n','t','r','o','l','\\',
67                                 'P','r','i','n','t','\\',
68                                 'M','o','n','i','t','o','r','s','\\',
69                                 'L','o','c','a','l',' ','P','o','r','t',0};
70
71 static WCHAR portname_com1W[] = {'C','O','M','1',':',0};
72 static WCHAR portname_fileW[] = {'F','I','L','E',':',0};
73 static WCHAR portname_lpt1W[] = {'L','P','T','1',':',0};
74
75 /* ########################### */
76                                        
77 static void test_ConfigurePort(void)
78 {
79     DWORD   res;
80
81     if (!pConfigurePort) return;
82
83 #if 0
84     /* NT4 crash on this test */
85     res = pConfigurePort(NULL, 0, NULL);
86 #endif
87
88     /*  Testing-Results (localmon.dll from NT4.0):
89         - Case of Portname is ignored
90         - "COM1:" and "COM01:" are the same (Compared by value)
91         - Portname without ":" => Dialog "Nothing to configure" comes up; Success
92         - "LPT1:", "LPT0:" and "LPT:" are the same (Numbers in "LPT:" are ignored)
93         - Empty Servername (LPT1:) => Dialog comes up (Servername is ignored)
94         - "FILE:" => Dialog "Nothing to configure" comes up; Success
95         - Empty Portname =>  => Dialog "Nothing to configure" comes up; Success
96         - Port "does_not_exist" => Dialog "Nothing to configure" comes up; Success
97     */
98     if (winetest_interactive > 0) {
99
100         SetLastError(0xdeadbeef);
101         res = pConfigurePort(NULL, 0, portname_com1W);
102         trace("returned %d with %d\n", res, GetLastError());
103
104         SetLastError(0xdeadbeef);
105         res = pConfigurePort(NULL, 0, portname_lpt1W);
106         trace("returned %d with %d\n", res, GetLastError());
107
108         SetLastError(0xdeadbeef);
109         res = pConfigurePort(NULL, 0, portname_fileW);
110         trace("returned %d with %d\n", res, GetLastError());
111     }
112 }
113
114 /* ########################### */
115
116 static void test_EnumPorts(void)
117 {
118     DWORD   res;
119     DWORD   level;
120     LPBYTE  buffer;
121     DWORD   cbBuf;
122     DWORD   pcbNeeded;
123     DWORD   pcReturned;
124
125     if (!pEnumPorts) return;
126
127     /* valid levels are 1 and 2 */
128     for(level = 0; level < 4; level++) {
129
130         cbBuf = 0xdeadbeef;
131         pcReturned = 0xdeadbeef;
132         SetLastError(0xdeadbeef);
133         res = pEnumPorts(NULL, level, NULL, 0, &cbBuf, &pcReturned);
134
135         /* use only a short test, when we test with an invalid level */
136         if(!level || (level > 2)) {
137             /* NT4 fails with ERROR_INVALID_LEVEL (as expected)
138                XP succeeds with ERROR_SUCCESS () */
139             ok( (cbBuf == 0) && (pcReturned == 0),
140                 "(%d) returned %d with %d and %d, %d (expected 0, 0)\n",
141                 level, res, GetLastError(), cbBuf, pcReturned);
142             continue;
143         }        
144
145         ok( !res && (GetLastError() == ERROR_INSUFFICIENT_BUFFER),
146             "(%d) returned %d with %d and %d, %d (expected '0' with " \
147             "ERROR_INSUFFICIENT_BUFFER)\n",
148             level, res, GetLastError(), cbBuf, pcReturned);
149
150         buffer = HeapAlloc(GetProcessHeap(), 0, cbBuf * 2);
151         if (buffer == NULL) continue;
152
153         pcbNeeded = 0xdeadbeef;
154         pcReturned = 0xdeadbeef;
155         SetLastError(0xdeadbeef);
156         res = pEnumPorts(NULL, level, buffer, cbBuf, &pcbNeeded, &pcReturned);
157         ok( res, "(%d) returned %d with %d and %d, %d (expected '!= 0')\n",
158             level, res, GetLastError(), cbBuf, pcReturned);
159         /* We can compare the returned Data with the Registry / "win.ini",[Ports] here */
160
161         pcbNeeded = 0xdeadbeef;
162         pcReturned = 0xdeadbeef;
163         SetLastError(0xdeadbeef);
164         res = pEnumPorts(NULL, level, buffer, cbBuf+1, &pcbNeeded, &pcReturned);
165         ok( res, "(%d) returned %d with %d and %d, %d (expected '!= 0')\n",
166             level, res, GetLastError(), cbBuf, pcReturned);
167
168         pcbNeeded = 0xdeadbeef;
169         pcReturned = 0xdeadbeef;
170         SetLastError(0xdeadbeef);
171         res = pEnumPorts(NULL, level, buffer, cbBuf-1, &pcbNeeded, &pcReturned);
172         ok( !res && (GetLastError() == ERROR_INSUFFICIENT_BUFFER),
173             "(%d) returned %d with %d and %d, %d (expected '0' with " \
174             "ERROR_INSUFFICIENT_BUFFER)\n",
175             level, res, GetLastError(), cbBuf, pcReturned);
176
177 #if 0
178         /* The following tests crash this app with native localmon/localspl */
179         res = pEnumPorts(NULL, level, NULL, cbBuf, &pcbNeeded, &pcReturned);
180         res = pEnumPorts(NULL, level, buffer, cbBuf, NULL, &pcReturned);
181         res = pEnumPorts(NULL, level, buffer, cbBuf, &pcbNeeded, NULL);
182 #endif
183
184         /* The Servername is ignored */
185         pcbNeeded = 0xdeadbeef;
186         pcReturned = 0xdeadbeef;
187         SetLastError(0xdeadbeef);
188         res = pEnumPorts(emptyW, level, buffer, cbBuf+1, &pcbNeeded, &pcReturned);
189         ok( res, "(%d) returned %d with %d and %d, %d (expected '!= 0')\n",
190             level, res, GetLastError(), cbBuf, pcReturned);
191
192         pcbNeeded = 0xdeadbeef;
193         pcReturned = 0xdeadbeef;
194         SetLastError(0xdeadbeef);
195         res = pEnumPorts(invalid_serverW, level, buffer, cbBuf+1, &pcbNeeded, &pcReturned);
196         ok( res, "(%d) returned %d with %d and %d, %d (expected '!= 0')\n",
197             level, res, GetLastError(), cbBuf, pcReturned);
198
199         HeapFree(GetProcessHeap(), 0, buffer);
200     }
201 }
202
203 /* ########################### */
204
205
206 static void test_InitializePrintMonitor(void)
207 {
208     LPMONITOREX res;
209
210     SetLastError(0xdeadbeef);
211     res = pInitializePrintMonitor(NULL);
212     /* The Parameter was unchecked before w2k */
213     ok( res || (GetLastError() == ERROR_INVALID_PARAMETER),
214         "returned %p with %d\n (expected '!= NULL' or: NULL with " \
215         "ERROR_INVALID_PARAMETER)\n", res, GetLastError());
216
217     SetLastError(0xdeadbeef);
218     res = pInitializePrintMonitor(emptyW);
219     ok( res || (GetLastError() == ERROR_INVALID_PARAMETER),
220         "returned %p with %d\n (expected '!= NULL' or: NULL with " \
221         "ERROR_INVALID_PARAMETER)\n", res, GetLastError());
222
223
224     /* Every call with a non-empty string returns the same Pointer */
225     SetLastError(0xdeadbeef);
226     res = pInitializePrintMonitor(Monitors_LocalPortW);
227     ok( res == pm,
228         "returned %p with %d (expected %p)\n", res, GetLastError(), pm);
229 }
230
231 /* ########################### */
232
233 #define GET_MONITOR_FUNC(name) \
234             if(numentries > 0) { \
235                 numentries--; \
236                 p##name = (void *) pm->Monitor.pfn##name ;  \
237             }
238
239
240 START_TEST(localmon)
241 {
242     /* This DLL does not exists on Win9x */
243     hdll = LoadLibraryA("localspl.dll");
244     if (!hdll) return;
245
246     pInitializePrintMonitor = (void *) GetProcAddress(hdll, "InitializePrintMonitor");
247     if (!pInitializePrintMonitor) {
248         /* The Monitor for "Local Ports" was in a seperate dll before w2k */
249         hlocalmon = LoadLibraryA("localmon.dll");
250         if (hlocalmon) {
251             pInitializePrintMonitor = (void *) GetProcAddress(hlocalmon, "InitializePrintMonitor");
252         }
253     }
254     if (!pInitializePrintMonitor) return;
255
256     /* Native localmon.dll / localspl.dll need a vaild Port-Entry in:
257        a) since xp: HKLM\Software\Microsoft\Windows NT\CurrentVersion\Ports 
258        b) up to w2k: Section "Ports" in win.ini
259        or InitializePrintMonitor fails. */
260     pm = pInitializePrintMonitor(Monitors_LocalPortW);
261     if (pm) {
262         DWORD   numentries;
263         numentries = (pm->dwMonitorSize ) / sizeof(VOID *);
264         /* NT4: 14, since w2k: 17 */
265         ok( numentries == 14 || numentries == 17, 
266             "dwMonitorSize (%d) => %d Functions\n", pm->dwMonitorSize, numentries);
267
268         GET_MONITOR_FUNC(EnumPorts);
269         GET_MONITOR_FUNC(OpenPort);
270         GET_MONITOR_FUNC(OpenPortEx);
271         GET_MONITOR_FUNC(StartDocPort);
272         GET_MONITOR_FUNC(WritePort);
273         GET_MONITOR_FUNC(ReadPort);
274         GET_MONITOR_FUNC(EndDocPort);
275         GET_MONITOR_FUNC(ClosePort);
276         GET_MONITOR_FUNC(AddPort);
277         GET_MONITOR_FUNC(AddPortEx);
278         GET_MONITOR_FUNC(ConfigurePort);
279         GET_MONITOR_FUNC(DeletePort);
280         GET_MONITOR_FUNC(GetPrinterDataFromPort);
281         GET_MONITOR_FUNC(SetPortTimeOuts);
282         GET_MONITOR_FUNC(XcvOpenPort);
283         GET_MONITOR_FUNC(XcvDataPort);
284         GET_MONITOR_FUNC(XcvClosePort);
285     }
286     test_InitializePrintMonitor();
287
288     test_ConfigurePort();
289     test_EnumPorts();
290 }