localspl/tests: Replace some '#if 0's with 'if (0)'s.
[wine/gsoc_dplay.git] / dlls / localspl / tests / localmon.c
blob682d6f216dccb243fc3403d9d8d5b1399ff82e01
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)
87 /* NT4 crash on this test */
88 res = pAddPort(NULL, 0, NULL);
91 /* Testing-Results (localmon.dll from NT4.0):
92 - The Servername is ignored
93 - Case of MonitorName is ignored
96 SetLastError(0xdeadbeef);
97 res = pAddPort(NULL, 0, emptyW);
98 ok(!res, "returned %d with 0x%x (expected '0')\n", res, GetLastError());
100 SetLastError(0xdeadbeef);
101 res = pAddPort(NULL, 0, does_not_existW);
102 ok(!res, "returned %d with 0x%x (expected '0')\n", res, GetLastError());
106 /* ########################### */
108 static void test_ConfigurePort(void)
110 DWORD res;
112 /* moved to localui.dll since w2k */
113 if (!pConfigurePort) return;
115 if (0)
117 /* NT4 crash on this test */
118 res = pConfigurePort(NULL, 0, NULL);
121 /* Testing-Results (localmon.dll from NT4.0):
122 - Case of Portname is ignored
123 - "COM1:" and "COM01:" are the same (Compared by value)
124 - Portname without ":" => Dialog "Nothing to configure" comes up; Success
125 - "LPT1:", "LPT0:" and "LPT:" are the same (Numbers in "LPT:" are ignored)
126 - Empty Servername (LPT1:) => Dialog comes up (Servername is ignored)
127 - "FILE:" => Dialog "Nothing to configure" comes up; Success
128 - Empty Portname => => Dialog "Nothing to configure" comes up; Success
129 - Port "does_not_exist" => Dialog "Nothing to configure" comes up; Success
131 if (winetest_interactive > 0) {
133 SetLastError(0xdeadbeef);
134 res = pConfigurePort(NULL, 0, portname_com1W);
135 trace("returned %d with %d\n", res, GetLastError());
137 SetLastError(0xdeadbeef);
138 res = pConfigurePort(NULL, 0, portname_lpt1W);
139 trace("returned %d with %d\n", res, GetLastError());
141 SetLastError(0xdeadbeef);
142 res = pConfigurePort(NULL, 0, portname_fileW);
143 trace("returned %d with %d\n", res, GetLastError());
147 /* ########################### */
149 static void test_DeletePort(void)
151 DWORD res;
153 /* moved to localui.dll since w2k */
154 if (!pDeletePort) return;
156 if (0)
158 /* NT4 crash on this test */
159 res = pDeletePort(NULL, 0, NULL);
162 /* Testing-Results (localmon.dll from NT4.0):
163 - Case of Portname is ignored (returned '1' on Success)
164 - "COM1:" and "COM01:" are different (Compared as string)
165 - server_does_not_exist (LPT1:) => Port deleted, Success (Servername is ignored)
166 - Empty Portname => => FALSE (LastError not changed)
167 - Port "does_not_exist" => FALSE (LastError not changed)
170 SetLastError(0xdeadbeef);
171 res = pDeletePort(NULL, 0, emptyW);
172 ok(!res, "returned %d with 0x%x (expected '0')\n", res, GetLastError());
174 SetLastError(0xdeadbeef);
175 res = pDeletePort(NULL, 0, does_not_existW);
176 ok(!res, "returned %d with 0x%x (expected '0')\n", res, GetLastError());
180 /* ########################### */
182 static void test_EnumPorts(void)
184 DWORD res;
185 DWORD level;
186 LPBYTE buffer;
187 DWORD cbBuf;
188 DWORD pcbNeeded;
189 DWORD pcReturned;
191 if (!pEnumPorts) return;
193 /* valid levels are 1 and 2 */
194 for(level = 0; level < 4; level++) {
196 cbBuf = 0xdeadbeef;
197 pcReturned = 0xdeadbeef;
198 SetLastError(0xdeadbeef);
199 res = pEnumPorts(NULL, level, NULL, 0, &cbBuf, &pcReturned);
201 /* use only a short test, when we test with an invalid level */
202 if(!level || (level > 2)) {
203 /* NT4 fails with ERROR_INVALID_LEVEL (as expected)
204 XP succeeds with ERROR_SUCCESS () */
205 ok( (cbBuf == 0) && (pcReturned == 0),
206 "(%d) returned %d with %d and %d, %d (expected 0, 0)\n",
207 level, res, GetLastError(), cbBuf, pcReturned);
208 continue;
211 ok( !res && (GetLastError() == ERROR_INSUFFICIENT_BUFFER),
212 "(%d) returned %d with %d and %d, %d (expected '0' with " \
213 "ERROR_INSUFFICIENT_BUFFER)\n",
214 level, res, GetLastError(), cbBuf, pcReturned);
216 buffer = HeapAlloc(GetProcessHeap(), 0, cbBuf * 2);
217 if (buffer == NULL) continue;
219 pcbNeeded = 0xdeadbeef;
220 pcReturned = 0xdeadbeef;
221 SetLastError(0xdeadbeef);
222 res = pEnumPorts(NULL, level, buffer, cbBuf, &pcbNeeded, &pcReturned);
223 ok( res, "(%d) returned %d with %d and %d, %d (expected '!= 0')\n",
224 level, res, GetLastError(), cbBuf, pcReturned);
225 /* We can compare the returned Data with the Registry / "win.ini",[Ports] here */
227 pcbNeeded = 0xdeadbeef;
228 pcReturned = 0xdeadbeef;
229 SetLastError(0xdeadbeef);
230 res = pEnumPorts(NULL, level, buffer, cbBuf+1, &pcbNeeded, &pcReturned);
231 ok( res, "(%d) returned %d with %d and %d, %d (expected '!= 0')\n",
232 level, res, GetLastError(), cbBuf, pcReturned);
234 pcbNeeded = 0xdeadbeef;
235 pcReturned = 0xdeadbeef;
236 SetLastError(0xdeadbeef);
237 res = pEnumPorts(NULL, level, buffer, cbBuf-1, &pcbNeeded, &pcReturned);
238 ok( !res && (GetLastError() == ERROR_INSUFFICIENT_BUFFER),
239 "(%d) returned %d with %d and %d, %d (expected '0' with " \
240 "ERROR_INSUFFICIENT_BUFFER)\n",
241 level, res, GetLastError(), cbBuf, pcReturned);
243 if (0)
245 /* The following tests crash this app with native localmon/localspl */
246 res = pEnumPorts(NULL, level, NULL, cbBuf, &pcbNeeded, &pcReturned);
247 res = pEnumPorts(NULL, level, buffer, cbBuf, NULL, &pcReturned);
248 res = pEnumPorts(NULL, level, buffer, cbBuf, &pcbNeeded, NULL);
251 /* The Servername is ignored */
252 pcbNeeded = 0xdeadbeef;
253 pcReturned = 0xdeadbeef;
254 SetLastError(0xdeadbeef);
255 res = pEnumPorts(emptyW, level, buffer, cbBuf+1, &pcbNeeded, &pcReturned);
256 ok( res, "(%d) returned %d with %d and %d, %d (expected '!= 0')\n",
257 level, res, GetLastError(), cbBuf, pcReturned);
259 pcbNeeded = 0xdeadbeef;
260 pcReturned = 0xdeadbeef;
261 SetLastError(0xdeadbeef);
262 res = pEnumPorts(invalid_serverW, level, buffer, cbBuf+1, &pcbNeeded, &pcReturned);
263 ok( res, "(%d) returned %d with %d and %d, %d (expected '!= 0')\n",
264 level, res, GetLastError(), cbBuf, pcReturned);
266 HeapFree(GetProcessHeap(), 0, buffer);
270 /* ########################### */
273 static void test_InitializePrintMonitor(void)
275 LPMONITOREX res;
277 SetLastError(0xdeadbeef);
278 res = pInitializePrintMonitor(NULL);
279 /* The Parameter was unchecked before w2k */
280 ok( res || (GetLastError() == ERROR_INVALID_PARAMETER),
281 "returned %p with %d\n (expected '!= NULL' or: NULL with " \
282 "ERROR_INVALID_PARAMETER)\n", res, GetLastError());
284 SetLastError(0xdeadbeef);
285 res = pInitializePrintMonitor(emptyW);
286 ok( res || (GetLastError() == ERROR_INVALID_PARAMETER),
287 "returned %p with %d\n (expected '!= NULL' or: NULL with " \
288 "ERROR_INVALID_PARAMETER)\n", res, GetLastError());
291 /* Every call with a non-empty string returns the same Pointer */
292 SetLastError(0xdeadbeef);
293 res = pInitializePrintMonitor(Monitors_LocalPortW);
294 ok( res == pm,
295 "returned %p with %d (expected %p)\n", res, GetLastError(), pm);
298 /* ########################### */
300 static void test_XcvClosePort(void)
302 DWORD res;
303 HANDLE hXcv;
305 if ((pXcvOpenPort == NULL) || (pXcvClosePort == NULL)) return;
307 if (0)
309 /* crash with native localspl.dll (w2k+xp) */
310 res = pXcvClosePort(NULL);
311 res = pXcvClosePort(INVALID_HANDLE_VALUE);
315 SetLastError(0xdeadbeef);
316 hXcv = (HANDLE) 0xdeadbeef;
317 res = pXcvOpenPort(emptyW, SERVER_ACCESS_ADMINISTER, &hXcv);
318 ok(res, "returned %d with 0x%x and %p (expected '!= 0')\n", res, GetLastError(), hXcv);
320 if (res) {
321 SetLastError(0xdeadbeef);
322 res = pXcvClosePort(hXcv);
323 ok( res, "returned %d with 0x%x(expected '!= 0')\n", res, GetLastError());
325 if (0)
327 /* test for "Double Free": crash with native localspl.dll (w2k+xp) */
328 res = pXcvClosePort(hXcv);
333 /* ########################### */
335 static void test_XcvOpenPort(void)
337 DWORD res;
338 HANDLE hXcv;
340 if ((pXcvOpenPort == NULL) || (pXcvClosePort == NULL)) return;
342 if (0)
344 /* crash with native localspl.dll (w2k+xp) */
345 res = pXcvOpenPort(NULL, SERVER_ACCESS_ADMINISTER, &hXcv);
346 res = pXcvOpenPort(emptyW, SERVER_ACCESS_ADMINISTER, NULL);
350 /* The returned handle is the result from a previous "spoolss.dll,DllAllocSplMem" */
351 SetLastError(0xdeadbeef);
352 hXcv = (HANDLE) 0xdeadbeef;
353 res = pXcvOpenPort(emptyW, SERVER_ACCESS_ADMINISTER, &hXcv);
354 ok(res, "returned %d with 0x%x and %p (expected '!= 0')\n", res, GetLastError(), hXcv);
355 if (res) pXcvClosePort(hXcv);
358 /* The ACCESS_MASK is not checked in XcvOpenPort */
359 SetLastError(0xdeadbeef);
360 hXcv = (HANDLE) 0xdeadbeef;
361 res = pXcvOpenPort(emptyW, 0, &hXcv);
362 ok(res, "returned %d with 0x%x and %p (expected '!= 0')\n", res, GetLastError(), hXcv);
363 if (res) pXcvClosePort(hXcv);
366 /* A copy of pszObject is saved in the Memory-Block */
367 SetLastError(0xdeadbeef);
368 hXcv = (HANDLE) 0xdeadbeef;
369 res = pXcvOpenPort(portname_lpt1W, SERVER_ALL_ACCESS, &hXcv);
370 ok(res, "returned %d with 0x%x and %p (expected '!= 0')\n", res, GetLastError(), hXcv);
371 if (res) pXcvClosePort(hXcv);
373 SetLastError(0xdeadbeef);
374 hXcv = (HANDLE) 0xdeadbeef;
375 res = pXcvOpenPort(portname_fileW, SERVER_ALL_ACCESS, &hXcv);
376 ok(res, "returned %d with 0x%x and %p (expected '!= 0')\n", res, GetLastError(), hXcv);
377 if (res) pXcvClosePort(hXcv);
381 /* ########################### */
383 #define GET_MONITOR_FUNC(name) \
384 if(numentries > 0) { \
385 numentries--; \
386 p##name = (void *) pm->Monitor.pfn##name ; \
390 START_TEST(localmon)
392 /* This DLL does not exists on Win9x */
393 hdll = LoadLibraryA("localspl.dll");
394 if (!hdll) return;
396 pInitializePrintMonitor = (void *) GetProcAddress(hdll, "InitializePrintMonitor");
397 if (!pInitializePrintMonitor) {
398 /* The Monitor for "Local Ports" was in a seperate dll before w2k */
399 hlocalmon = LoadLibraryA("localmon.dll");
400 if (hlocalmon) {
401 pInitializePrintMonitor = (void *) GetProcAddress(hlocalmon, "InitializePrintMonitor");
404 if (!pInitializePrintMonitor) return;
406 /* Native localmon.dll / localspl.dll need a vaild Port-Entry in:
407 a) since xp: HKLM\Software\Microsoft\Windows NT\CurrentVersion\Ports
408 b) up to w2k: Section "Ports" in win.ini
409 or InitializePrintMonitor fails. */
410 pm = pInitializePrintMonitor(Monitors_LocalPortW);
411 if (pm) {
412 DWORD numentries;
413 numentries = (pm->dwMonitorSize ) / sizeof(VOID *);
414 /* NT4: 14, since w2k: 17 */
415 ok( numentries == 14 || numentries == 17,
416 "dwMonitorSize (%d) => %d Functions\n", pm->dwMonitorSize, numentries);
418 GET_MONITOR_FUNC(EnumPorts);
419 GET_MONITOR_FUNC(OpenPort);
420 GET_MONITOR_FUNC(OpenPortEx);
421 GET_MONITOR_FUNC(StartDocPort);
422 GET_MONITOR_FUNC(WritePort);
423 GET_MONITOR_FUNC(ReadPort);
424 GET_MONITOR_FUNC(EndDocPort);
425 GET_MONITOR_FUNC(ClosePort);
426 GET_MONITOR_FUNC(AddPort);
427 GET_MONITOR_FUNC(AddPortEx);
428 GET_MONITOR_FUNC(ConfigurePort);
429 GET_MONITOR_FUNC(DeletePort);
430 GET_MONITOR_FUNC(GetPrinterDataFromPort);
431 GET_MONITOR_FUNC(SetPortTimeOuts);
432 GET_MONITOR_FUNC(XcvOpenPort);
433 GET_MONITOR_FUNC(XcvDataPort);
434 GET_MONITOR_FUNC(XcvClosePort);
436 test_InitializePrintMonitor();
438 test_AddPort();
439 test_ConfigurePort();
440 test_DeletePort();
441 test_EnumPorts();
442 test_XcvClosePort();
443 test_XcvOpenPort();