localspl/tests: Add tests for XcvClosePort.
[wine/multimedia.git] / dlls / localspl / tests / localmon.c
blob00978e7f0ee844e2cd6cf30a34bd31d5ee23e5d9
1 /*
2 * Unit test suite for localspl API functions: local print monitor
4 * Copyright 2006 Detlef Riekenberg
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.
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.
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
22 #include <stdarg.h>
24 #include "windef.h"
25 #include "winbase.h"
26 #include "winerror.h"
27 #include "wingdi.h"
28 #include "winreg.h"
30 #include "winspool.h"
31 #include "ddk/winsplp.h"
33 #include "wine/test.h"
36 /* ##### */
38 static HMODULE hdll;
39 static HMODULE hlocalmon;
40 static LPMONITOREX (WINAPI *pInitializePrintMonitor)(LPWSTR);
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)(LPCWSTR, ACCESS_MASK, PHANDLE phXcv);
58 static DWORD (WINAPI *pXcvDataPort)(HANDLE, LPCWSTR, PBYTE, DWORD, PBYTE, DWORD, PDWORD);
59 static BOOL (WINAPI *pXcvClosePort)(HANDLE);
61 static WCHAR does_not_existW[] = {'d','o','e','s','_','n','o','t','_','e','x','i','s','t',0};
62 static WCHAR emptyW[] = {0};
63 static WCHAR invalid_serverW[] = {'\\','\\','i','n','v','a','l','i','d','_','s','e','r','v','e','r',0};
64 static WCHAR Monitors_LocalPortW[] = { \
65 'S','y','s','t','e','m','\\',
66 'C','u','r','r','e','n','t','C','o','n','t','r','o','l','S','e','t','\\',
67 'C','o','n','t','r','o','l','\\',
68 'P','r','i','n','t','\\',
69 'M','o','n','i','t','o','r','s','\\',
70 'L','o','c','a','l',' ','P','o','r','t',0};
72 static WCHAR portname_com1W[] = {'C','O','M','1',':',0};
73 static WCHAR portname_fileW[] = {'F','I','L','E',':',0};
74 static WCHAR portname_lpt1W[] = {'L','P','T','1',':',0};
76 /* ########################### */
78 static void test_AddPort(void)
80 DWORD res;
82 /* moved to localui.dll since w2k */
83 if (!pAddPort) return;
85 #if 0
86 /* NT4 crash on this test */
87 res = pAddPort(NULL, 0, NULL);
88 #endif
90 /* Testing-Results (localmon.dll from NT4.0):
91 - The Servername is ignored
92 - Case of MonitorName is ignored
95 SetLastError(0xdeadbeef);
96 res = pAddPort(NULL, 0, emptyW);
97 ok(!res, "returned %d with 0x%x (expected '0')\n", res, GetLastError());
99 SetLastError(0xdeadbeef);
100 res = pAddPort(NULL, 0, does_not_existW);
101 ok(!res, "returned %d with 0x%x (expected '0')\n", res, GetLastError());
105 /* ########################### */
107 static void test_ConfigurePort(void)
109 DWORD res;
111 /* moved to localui.dll since w2k */
112 if (!pConfigurePort) return;
114 #if 0
115 /* NT4 crash on this test */
116 res = pConfigurePort(NULL, 0, NULL);
117 #endif
119 /* Testing-Results (localmon.dll from NT4.0):
120 - Case of Portname is ignored
121 - "COM1:" and "COM01:" are the same (Compared by value)
122 - Portname without ":" => Dialog "Nothing to configure" comes up; Success
123 - "LPT1:", "LPT0:" and "LPT:" are the same (Numbers in "LPT:" are ignored)
124 - Empty Servername (LPT1:) => Dialog comes up (Servername is ignored)
125 - "FILE:" => Dialog "Nothing to configure" comes up; Success
126 - Empty Portname => => Dialog "Nothing to configure" comes up; Success
127 - Port "does_not_exist" => Dialog "Nothing to configure" comes up; Success
129 if (winetest_interactive > 0) {
131 SetLastError(0xdeadbeef);
132 res = pConfigurePort(NULL, 0, portname_com1W);
133 trace("returned %d with %d\n", res, GetLastError());
135 SetLastError(0xdeadbeef);
136 res = pConfigurePort(NULL, 0, portname_lpt1W);
137 trace("returned %d with %d\n", res, GetLastError());
139 SetLastError(0xdeadbeef);
140 res = pConfigurePort(NULL, 0, portname_fileW);
141 trace("returned %d with %d\n", res, GetLastError());
145 /* ########################### */
147 static void test_DeletePort(void)
149 DWORD res;
151 /* moved to localui.dll since w2k */
152 if (!pDeletePort) return;
154 #if 0
155 /* NT4 crash on this test */
156 res = pDeletePort(NULL, 0, NULL);
157 #endif
159 /* Testing-Results (localmon.dll from NT4.0):
160 - Case of Portname is ignored (returned '1' on Success)
161 - "COM1:" and "COM01:" are different (Compared as string)
162 - server_does_not_exist (LPT1:) => Port deleted, Success (Servername is ignored)
163 - Empty Portname => => FALSE (LastError not changed)
164 - Port "does_not_exist" => FALSE (LastError not changed)
167 SetLastError(0xdeadbeef);
168 res = pDeletePort(NULL, 0, emptyW);
169 ok(!res, "returned %d with 0x%x (expected '0')\n", res, GetLastError());
171 SetLastError(0xdeadbeef);
172 res = pDeletePort(NULL, 0, does_not_existW);
173 ok(!res, "returned %d with 0x%x (expected '0')\n", res, GetLastError());
177 /* ########################### */
179 static void test_EnumPorts(void)
181 DWORD res;
182 DWORD level;
183 LPBYTE buffer;
184 DWORD cbBuf;
185 DWORD pcbNeeded;
186 DWORD pcReturned;
188 if (!pEnumPorts) return;
190 /* valid levels are 1 and 2 */
191 for(level = 0; level < 4; level++) {
193 cbBuf = 0xdeadbeef;
194 pcReturned = 0xdeadbeef;
195 SetLastError(0xdeadbeef);
196 res = pEnumPorts(NULL, level, NULL, 0, &cbBuf, &pcReturned);
198 /* use only a short test, when we test with an invalid level */
199 if(!level || (level > 2)) {
200 /* NT4 fails with ERROR_INVALID_LEVEL (as expected)
201 XP succeeds with ERROR_SUCCESS () */
202 ok( (cbBuf == 0) && (pcReturned == 0),
203 "(%d) returned %d with %d and %d, %d (expected 0, 0)\n",
204 level, res, GetLastError(), cbBuf, pcReturned);
205 continue;
208 ok( !res && (GetLastError() == ERROR_INSUFFICIENT_BUFFER),
209 "(%d) returned %d with %d and %d, %d (expected '0' with " \
210 "ERROR_INSUFFICIENT_BUFFER)\n",
211 level, res, GetLastError(), cbBuf, pcReturned);
213 buffer = HeapAlloc(GetProcessHeap(), 0, cbBuf * 2);
214 if (buffer == NULL) continue;
216 pcbNeeded = 0xdeadbeef;
217 pcReturned = 0xdeadbeef;
218 SetLastError(0xdeadbeef);
219 res = pEnumPorts(NULL, level, buffer, cbBuf, &pcbNeeded, &pcReturned);
220 ok( res, "(%d) returned %d with %d and %d, %d (expected '!= 0')\n",
221 level, res, GetLastError(), cbBuf, pcReturned);
222 /* We can compare the returned Data with the Registry / "win.ini",[Ports] here */
224 pcbNeeded = 0xdeadbeef;
225 pcReturned = 0xdeadbeef;
226 SetLastError(0xdeadbeef);
227 res = pEnumPorts(NULL, level, buffer, cbBuf+1, &pcbNeeded, &pcReturned);
228 ok( res, "(%d) returned %d with %d and %d, %d (expected '!= 0')\n",
229 level, res, GetLastError(), cbBuf, pcReturned);
231 pcbNeeded = 0xdeadbeef;
232 pcReturned = 0xdeadbeef;
233 SetLastError(0xdeadbeef);
234 res = pEnumPorts(NULL, level, buffer, cbBuf-1, &pcbNeeded, &pcReturned);
235 ok( !res && (GetLastError() == ERROR_INSUFFICIENT_BUFFER),
236 "(%d) returned %d with %d and %d, %d (expected '0' with " \
237 "ERROR_INSUFFICIENT_BUFFER)\n",
238 level, res, GetLastError(), cbBuf, pcReturned);
240 #if 0
241 /* The following tests crash this app with native localmon/localspl */
242 res = pEnumPorts(NULL, level, NULL, cbBuf, &pcbNeeded, &pcReturned);
243 res = pEnumPorts(NULL, level, buffer, cbBuf, NULL, &pcReturned);
244 res = pEnumPorts(NULL, level, buffer, cbBuf, &pcbNeeded, NULL);
245 #endif
247 /* The Servername is ignored */
248 pcbNeeded = 0xdeadbeef;
249 pcReturned = 0xdeadbeef;
250 SetLastError(0xdeadbeef);
251 res = pEnumPorts(emptyW, level, buffer, cbBuf+1, &pcbNeeded, &pcReturned);
252 ok( res, "(%d) returned %d with %d and %d, %d (expected '!= 0')\n",
253 level, res, GetLastError(), cbBuf, pcReturned);
255 pcbNeeded = 0xdeadbeef;
256 pcReturned = 0xdeadbeef;
257 SetLastError(0xdeadbeef);
258 res = pEnumPorts(invalid_serverW, level, buffer, cbBuf+1, &pcbNeeded, &pcReturned);
259 ok( res, "(%d) returned %d with %d and %d, %d (expected '!= 0')\n",
260 level, res, GetLastError(), cbBuf, pcReturned);
262 HeapFree(GetProcessHeap(), 0, buffer);
266 /* ########################### */
269 static void test_InitializePrintMonitor(void)
271 LPMONITOREX res;
273 SetLastError(0xdeadbeef);
274 res = pInitializePrintMonitor(NULL);
275 /* The Parameter was unchecked before w2k */
276 ok( res || (GetLastError() == ERROR_INVALID_PARAMETER),
277 "returned %p with %d\n (expected '!= NULL' or: NULL with " \
278 "ERROR_INVALID_PARAMETER)\n", res, GetLastError());
280 SetLastError(0xdeadbeef);
281 res = pInitializePrintMonitor(emptyW);
282 ok( res || (GetLastError() == ERROR_INVALID_PARAMETER),
283 "returned %p with %d\n (expected '!= NULL' or: NULL with " \
284 "ERROR_INVALID_PARAMETER)\n", res, GetLastError());
287 /* Every call with a non-empty string returns the same Pointer */
288 SetLastError(0xdeadbeef);
289 res = pInitializePrintMonitor(Monitors_LocalPortW);
290 ok( res == pm,
291 "returned %p with %d (expected %p)\n", res, GetLastError(), pm);
294 /* ########################### */
296 static void test_XcvClosePort(void)
298 DWORD res;
299 HANDLE hXcv;
301 if ((pXcvOpenPort == NULL) || (pXcvClosePort == NULL)) return;
303 #if 0
304 /* crash with native localspl.dll (w2k+xp) */
305 res = pXcvClosePort(NULL);
306 res = pXcvClosePort(INVALID_HANDLE_VALUE);
307 #endif
310 SetLastError(0xdeadbeef);
311 hXcv = (HANDLE) 0xdeadbeef;
312 res = pXcvOpenPort(emptyW, SERVER_ACCESS_ADMINISTER, &hXcv);
313 ok(res, "returned %d with 0x%x and %p (expected '!= 0')\n", res, GetLastError(), hXcv);
315 if (res) {
316 SetLastError(0xdeadbeef);
317 res = pXcvClosePort(hXcv);
318 ok( res, "returned %d with 0x%x(expected '!= 0')\n", res, GetLastError());
320 #if 0
321 /* test for "Double Free": crash with native localspl.dll (w2k+xp) */
322 res = pXcvClosePort(hXcv);
323 #endif
327 /* ########################### */
329 static void test_XcvOpenPort(void)
331 DWORD res;
332 HANDLE hXcv;
334 if ((pXcvOpenPort == NULL) || (pXcvClosePort == NULL)) return;
336 #if 0
337 /* crash with native localspl.dll (w2k+xp) */
338 res = pXcvOpenPort(NULL, SERVER_ACCESS_ADMINISTER, &hXcv);
339 res = pXcvOpenPort(emptyW, SERVER_ACCESS_ADMINISTER, NULL);
340 #endif
343 /* The returned handle is the result from a previous "spoolss.dll,DllAllocSplMem" */
344 SetLastError(0xdeadbeef);
345 hXcv = (HANDLE) 0xdeadbeef;
346 res = pXcvOpenPort(emptyW, SERVER_ACCESS_ADMINISTER, &hXcv);
347 ok(res, "returned %d with 0x%x and %p (expected '!= 0')\n", res, GetLastError(), hXcv);
348 if (res) pXcvClosePort(hXcv);
351 /* The ACCESS_MASK is not checked in XcvOpenPort */
352 SetLastError(0xdeadbeef);
353 hXcv = (HANDLE) 0xdeadbeef;
354 res = pXcvOpenPort(emptyW, 0, &hXcv);
355 ok(res, "returned %d with 0x%x and %p (expected '!= 0')\n", res, GetLastError(), hXcv);
356 if (res) pXcvClosePort(hXcv);
359 /* A copy of pszObject is saved in the Memory-Block */
360 SetLastError(0xdeadbeef);
361 hXcv = (HANDLE) 0xdeadbeef;
362 res = pXcvOpenPort(portname_lpt1W, SERVER_ALL_ACCESS, &hXcv);
363 ok(res, "returned %d with 0x%x and %p (expected '!= 0')\n", res, GetLastError(), hXcv);
364 if (res) pXcvClosePort(hXcv);
366 SetLastError(0xdeadbeef);
367 hXcv = (HANDLE) 0xdeadbeef;
368 res = pXcvOpenPort(portname_fileW, SERVER_ALL_ACCESS, &hXcv);
369 ok(res, "returned %d with 0x%x and %p (expected '!= 0')\n", res, GetLastError(), hXcv);
370 if (res) pXcvClosePort(hXcv);
374 /* ########################### */
376 #define GET_MONITOR_FUNC(name) \
377 if(numentries > 0) { \
378 numentries--; \
379 p##name = (void *) pm->Monitor.pfn##name ; \
383 START_TEST(localmon)
385 /* This DLL does not exists on Win9x */
386 hdll = LoadLibraryA("localspl.dll");
387 if (!hdll) return;
389 pInitializePrintMonitor = (void *) GetProcAddress(hdll, "InitializePrintMonitor");
390 if (!pInitializePrintMonitor) {
391 /* The Monitor for "Local Ports" was in a seperate dll before w2k */
392 hlocalmon = LoadLibraryA("localmon.dll");
393 if (hlocalmon) {
394 pInitializePrintMonitor = (void *) GetProcAddress(hlocalmon, "InitializePrintMonitor");
397 if (!pInitializePrintMonitor) return;
399 /* Native localmon.dll / localspl.dll need a vaild Port-Entry in:
400 a) since xp: HKLM\Software\Microsoft\Windows NT\CurrentVersion\Ports
401 b) up to w2k: Section "Ports" in win.ini
402 or InitializePrintMonitor fails. */
403 pm = pInitializePrintMonitor(Monitors_LocalPortW);
404 if (pm) {
405 DWORD numentries;
406 numentries = (pm->dwMonitorSize ) / sizeof(VOID *);
407 /* NT4: 14, since w2k: 17 */
408 ok( numentries == 14 || numentries == 17,
409 "dwMonitorSize (%d) => %d Functions\n", pm->dwMonitorSize, numentries);
411 GET_MONITOR_FUNC(EnumPorts);
412 GET_MONITOR_FUNC(OpenPort);
413 GET_MONITOR_FUNC(OpenPortEx);
414 GET_MONITOR_FUNC(StartDocPort);
415 GET_MONITOR_FUNC(WritePort);
416 GET_MONITOR_FUNC(ReadPort);
417 GET_MONITOR_FUNC(EndDocPort);
418 GET_MONITOR_FUNC(ClosePort);
419 GET_MONITOR_FUNC(AddPort);
420 GET_MONITOR_FUNC(AddPortEx);
421 GET_MONITOR_FUNC(ConfigurePort);
422 GET_MONITOR_FUNC(DeletePort);
423 GET_MONITOR_FUNC(GetPrinterDataFromPort);
424 GET_MONITOR_FUNC(SetPortTimeOuts);
425 GET_MONITOR_FUNC(XcvOpenPort);
426 GET_MONITOR_FUNC(XcvDataPort);
427 GET_MONITOR_FUNC(XcvClosePort);
429 test_InitializePrintMonitor();
431 test_AddPort();
432 test_ConfigurePort();
433 test_DeletePort();
434 test_EnumPorts();
435 test_XcvClosePort();
436 test_XcvOpenPort();