localspl/tests: Check for Xcv-API only once.
[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)(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 HMODULE  hlocalui;
62 static PMONITORUI (WINAPI *pInitializePrintMonitorUI)(VOID);
63 static PMONITORUI pui;
64 static BOOL  (WINAPI *pAddPortUI)(PCWSTR, HWND, PCWSTR, PWSTR *);
65 static BOOL  (WINAPI *pConfigurePortUI)(PCWSTR, HWND, PCWSTR);
66 static BOOL  (WINAPI *pDeletePortUI)(PCWSTR, HWND, PCWSTR);
67
68
69 static const WCHAR cmd_AddPortW[] = {'A','d','d','P','o','r','t',0};
70 static const WCHAR cmd_ConfigureLPTPortCommandOKW[] = {'C','o','n','f','i','g','u','r','e',
71                                     'L','P','T','P','o','r','t',
72                                     'C','o','m','m','a','n','d','O','K',0};
73 static WCHAR cmd_DeletePortW[] = {'D','e','l','e','t','e','P','o','r','t',0};
74 static WCHAR cmd_GetTransmissionRetryTimeoutW[] = {'G','e','t',
75                                     'T','r','a','n','s','m','i','s','s','i','o','n',
76                                     'R','e','t','r','y','T','i','m','e','o','u','t',0};
77
78 static WCHAR cmd_MonitorUIW[] = {'M','o','n','i','t','o','r','U','I',0};
79 static WCHAR cmd_MonitorUI_lcaseW[] = {'m','o','n','i','t','o','r','u','i',0};
80 static WCHAR cmd_PortIsValidW[] = {'P','o','r','t','I','s','V','a','l','i','d',0};
81 static WCHAR does_not_existW[] = {'d','o','e','s','_','n','o','t','_','e','x','i','s','t',0};
82 static CHAR  emptyA[] = "";
83 static WCHAR emptyW[] = {0};
84 static WCHAR Monitors_LocalPortW[] = {
85                                 'S','y','s','t','e','m','\\',
86                                 'C','u','r','r','e','n','t','C','o','n','t','r','o','l','S','e','t','\\',
87                                 'C','o','n','t','r','o','l','\\',
88                                 'P','r','i','n','t','\\',
89                                 'M','o','n','i','t','o','r','s','\\',
90                                 'L','o','c','a','l',' ','P','o','r','t',0};
91
92 static CHAR  num_0A[] = "0";
93 static WCHAR num_0W[] = {'0',0};
94 static CHAR  num_1A[] = "1";
95 static WCHAR num_1W[] = {'1',0};
96 static CHAR  num_999999A[] = "999999";
97 static WCHAR num_999999W[] = {'9','9','9','9','9','9',0};
98 static CHAR  num_1000000A[] = "1000000";
99 static WCHAR num_1000000W[] = {'1','0','0','0','0','0','0',0};
100
101 static WCHAR portname_com1W[] = {'C','O','M','1',':',0};
102 static WCHAR portname_com2W[] = {'C','O','M','2',':',0};
103 static WCHAR portname_fileW[] = {'F','I','L','E',':',0};
104 static WCHAR portname_lpt1W[] = {'L','P','T','1',':',0};
105 static WCHAR portname_lpt2W[] = {'L','P','T','2',':',0};
106 static WCHAR server_does_not_existW[] = {'\\','\\','d','o','e','s','_','n','o','t','_','e','x','i','s','t',0};
107
108 static CHAR  TransmissionRetryTimeoutA[] = {'T','r','a','n','s','m','i','s','s','i','o','n',
109                                     'R','e','t','r','y','T','i','m','e','o','u','t',0};
110
111 static CHAR  WinNT_CV_WindowsA[] = {'S','o','f','t','w','a','r','e','\\',
112                                     'M','i','c','r','o','s','o','f','t','\\',
113                                     'W','i','n','d','o','w','s',' ','N','T','\\',
114                                     'C','u','r','r','e','n','t','V','e','r','s','i','o','n','\\',
115                                     'W','i','n','d','o','w','s',0};
116 static WCHAR wineW[] = {'W','i','n','e',0};
117
118 static WCHAR tempdirW[MAX_PATH];
119 static WCHAR tempfileW[MAX_PATH];
120
121 /* ########################### */
122
123 static void test_AddPort(void)
124 {
125     DWORD   res;
126
127     /* moved to localui.dll since w2k */
128     if (!pAddPort) return;
129
130     if (0)
131     {
132     /* NT4 crash on this test */
133     res = pAddPort(NULL, 0, NULL);
134     }
135
136     /*  Testing-Results (localmon.dll from NT4.0):
137         - The Servername is ignored
138         - Case of MonitorName is ignored
139     */
140
141     SetLastError(0xdeadbeef);
142     res = pAddPort(NULL, 0, emptyW);
143     ok(!res, "returned %d with %u (expected '0')\n", res, GetLastError());
144
145     SetLastError(0xdeadbeef);
146     res = pAddPort(NULL, 0, does_not_existW);
147     ok(!res, "returned %d with %u (expected '0')\n", res, GetLastError());
148
149 }
150
151 /* ########################### */
152                                        
153 static void test_ConfigurePort(void)
154 {
155     DWORD   res;
156
157     /* moved to localui.dll since w2k */
158     if (!pConfigurePort) return;
159
160     if (0)
161     {
162     /* NT4 crash on this test */
163     res = pConfigurePort(NULL, 0, NULL);
164     }
165
166     /*  Testing-Results (localmon.dll from NT4.0):
167         - Case of Portname is ignored
168         - "COM1:" and "COM01:" are the same (Compared by value)
169         - Portname without ":" => Dialog "Nothing to configure" comes up; Success
170         - "LPT1:", "LPT0:" and "LPT:" are the same (Numbers in "LPT:" are ignored)
171         - Empty Servername (LPT1:) => Dialog comes up (Servername is ignored)
172         - "FILE:" => Dialog "Nothing to configure" comes up; Success
173         - Empty Portname =>  => Dialog "Nothing to configure" comes up; Success
174         - Port "does_not_exist" => Dialog "Nothing to configure" comes up; Success
175     */
176     if (winetest_interactive > 0) {
177
178         SetLastError(0xdeadbeef);
179         res = pConfigurePort(NULL, 0, portname_com1W);
180         trace("returned %d with %u\n", res, GetLastError());
181
182         SetLastError(0xdeadbeef);
183         res = pConfigurePort(NULL, 0, portname_lpt1W);
184         trace("returned %d with %u\n", res, GetLastError());
185
186         SetLastError(0xdeadbeef);
187         res = pConfigurePort(NULL, 0, portname_fileW);
188         trace("returned %d with %u\n", res, GetLastError());
189     }
190 }
191
192 /* ########################### */
193
194 static void test_DeletePort(void)
195 {
196     DWORD   res;
197
198     /* moved to localui.dll since w2k */
199     if (!pDeletePort) return;
200
201     if (0)
202     {
203     /* NT4 crash on this test */
204     res = pDeletePort(NULL, 0, NULL);
205     }
206
207     /*  Testing-Results (localmon.dll from NT4.0):
208         - Case of Portname is ignored (returned '1' on Success)
209         - "COM1:" and "COM01:" are different (Compared as string)
210         - server_does_not_exist (LPT1:) => Port deleted, Success (Servername is ignored)
211         - Empty Portname =>  => FALSE (LastError not changed)
212         - Port "does_not_exist" => FALSE (LastError not changed)
213     */
214
215     SetLastError(0xdeadbeef);
216     res = pDeletePort(NULL, 0, emptyW);
217     ok(!res, "returned %d with %u (expected '0')\n", res, GetLastError());
218
219     SetLastError(0xdeadbeef);
220     res = pDeletePort(NULL, 0, does_not_existW);
221     ok(!res, "returned %d with %u (expected '0')\n", res, GetLastError());
222
223 }
224
225 /* ########################### */
226
227 static void test_EnumPorts(void)
228 {
229     DWORD   res;
230     DWORD   level;
231     LPBYTE  buffer;
232     DWORD   cbBuf;
233     DWORD   pcbNeeded;
234     DWORD   pcReturned;
235
236     if (!pEnumPorts) return;
237
238     /* valid levels are 1 and 2 */
239     for(level = 0; level < 4; level++) {
240
241         cbBuf = 0xdeadbeef;
242         pcReturned = 0xdeadbeef;
243         SetLastError(0xdeadbeef);
244         res = pEnumPorts(NULL, level, NULL, 0, &cbBuf, &pcReturned);
245
246         /* use only a short test, when we test with an invalid level */
247         if(!level || (level > 2)) {
248             /* NT4 fails with ERROR_INVALID_LEVEL (as expected)
249                XP succeeds with ERROR_SUCCESS () */
250             ok( (cbBuf == 0) && (pcReturned == 0),
251                 "(%d) returned %d with %u and %d, %d (expected 0, 0)\n",
252                 level, res, GetLastError(), cbBuf, pcReturned);
253             continue;
254         }        
255
256         ok( !res && (GetLastError() == ERROR_INSUFFICIENT_BUFFER),
257             "(%d) returned %d with %u and %d, %d (expected '0' with "
258             "ERROR_INSUFFICIENT_BUFFER)\n",
259             level, res, GetLastError(), cbBuf, pcReturned);
260
261         buffer = HeapAlloc(GetProcessHeap(), 0, cbBuf * 2);
262         if (buffer == NULL) continue;
263
264         pcbNeeded = 0xdeadbeef;
265         pcReturned = 0xdeadbeef;
266         SetLastError(0xdeadbeef);
267         res = pEnumPorts(NULL, level, buffer, cbBuf, &pcbNeeded, &pcReturned);
268         ok( res, "(%d) returned %d with %u and %d, %d (expected '!= 0')\n",
269             level, res, GetLastError(), pcbNeeded, pcReturned);
270         /* We can compare the returned Data with the Registry / "win.ini",[Ports] here */
271
272         pcbNeeded = 0xdeadbeef;
273         pcReturned = 0xdeadbeef;
274         SetLastError(0xdeadbeef);
275         res = pEnumPorts(NULL, level, buffer, cbBuf+1, &pcbNeeded, &pcReturned);
276         ok( res, "(%d) returned %d with %u and %d, %d (expected '!= 0')\n",
277             level, res, GetLastError(), pcbNeeded, pcReturned);
278
279         pcbNeeded = 0xdeadbeef;
280         pcReturned = 0xdeadbeef;
281         SetLastError(0xdeadbeef);
282         res = pEnumPorts(NULL, level, buffer, cbBuf-1, &pcbNeeded, &pcReturned);
283         ok( !res && (GetLastError() == ERROR_INSUFFICIENT_BUFFER),
284             "(%d) returned %d with %u and %d, %d (expected '0' with "
285             "ERROR_INSUFFICIENT_BUFFER)\n",
286             level, res, GetLastError(), pcbNeeded, pcReturned);
287
288         if (0)
289         {
290         /* The following tests crash this app with native localmon/localspl */
291         res = pEnumPorts(NULL, level, NULL, cbBuf, &pcbNeeded, &pcReturned);
292         res = pEnumPorts(NULL, level, buffer, cbBuf, NULL, &pcReturned);
293         res = pEnumPorts(NULL, level, buffer, cbBuf, &pcbNeeded, NULL);
294         }
295
296         /* The Servername is ignored */
297         pcbNeeded = 0xdeadbeef;
298         pcReturned = 0xdeadbeef;
299         SetLastError(0xdeadbeef);
300         res = pEnumPorts(emptyW, level, buffer, cbBuf+1, &pcbNeeded, &pcReturned);
301         ok( res, "(%d) returned %d with %u and %d, %d (expected '!= 0')\n",
302             level, res, GetLastError(), pcbNeeded, pcReturned);
303
304         pcbNeeded = 0xdeadbeef;
305         pcReturned = 0xdeadbeef;
306         SetLastError(0xdeadbeef);
307         res = pEnumPorts(server_does_not_existW, level, buffer, cbBuf+1, &pcbNeeded, &pcReturned);
308         ok( res, "(%d) returned %d with %u and %d, %d (expected '!= 0')\n",
309             level, res, GetLastError(), pcbNeeded, pcReturned);
310
311         HeapFree(GetProcessHeap(), 0, buffer);
312     }
313 }
314
315 /* ########################### */
316
317
318 static void test_InitializePrintMonitor(void)
319 {
320     LPMONITOREX res;
321
322     SetLastError(0xdeadbeef);
323     res = pInitializePrintMonitor(NULL);
324     /* The Parameter was unchecked before w2k */
325     ok( res || (GetLastError() == ERROR_INVALID_PARAMETER),
326         "returned %p with %u\n (expected '!= NULL' or: NULL with "
327         "ERROR_INVALID_PARAMETER)\n", res, GetLastError());
328
329     SetLastError(0xdeadbeef);
330     res = pInitializePrintMonitor(emptyW);
331     ok( res || (GetLastError() == ERROR_INVALID_PARAMETER),
332         "returned %p with %u\n (expected '!= NULL' or: NULL with "
333         "ERROR_INVALID_PARAMETER)\n", res, GetLastError());
334
335
336     /* Every call with a non-empty string returns the same Pointer */
337     SetLastError(0xdeadbeef);
338     res = pInitializePrintMonitor(Monitors_LocalPortW);
339     ok( res == pm,
340         "returned %p with %u (expected %p)\n", res, GetLastError(), pm);
341 }
342
343 /* ########################### */
344
345 static void test_XcvClosePort(void)
346 {
347     DWORD   res;
348     HANDLE hXcv;
349
350
351     if (0)
352     {
353     /* crash with native localspl.dll (w2k+xp) */
354     res = pXcvClosePort(NULL);
355     res = pXcvClosePort(INVALID_HANDLE_VALUE);
356     }
357
358
359     SetLastError(0xdeadbeef);
360     hXcv = (HANDLE) 0xdeadbeef;
361     res = pXcvOpenPort(emptyW, SERVER_ACCESS_ADMINISTER, &hXcv);
362     ok(res, "returned %d with %u and %p (expected '!= 0')\n", res, GetLastError(), hXcv);
363
364     if (res) {
365         SetLastError(0xdeadbeef);
366         res = pXcvClosePort(hXcv);
367         ok( res, "returned %d with %u (expected '!= 0')\n", res, GetLastError());
368
369         if (0)
370         {
371         /* test for "Double Free": crash with native localspl.dll (w2k+xp) */
372         res = pXcvClosePort(hXcv);
373         }
374     }
375 }
376
377 /* ########################### */
378
379 static void test_XcvDataPort_AddPort(void)
380 {
381     DWORD   res;
382     HANDLE  hXcv;
383
384
385     hXcv = (HANDLE) 0xdeadbeef;
386     SetLastError(0xdeadbeef);
387     res = pXcvOpenPort(emptyW, SERVER_ALL_ACCESS, &hXcv);
388     ok(res, "hXcv: %d with %u and %p (expected '!= 0')\n", res, GetLastError(), hXcv);
389     if (!res) return;
390
391     /*
392      * The following tests crash with native localspl.dll on w2k and xp,
393      * but it works, when the native dll (w2k and xp) is used in wine.
394      * also tested (same crash): replacing emptyW with portname_lpt1W
395      * and replacing "NULL, 0, NULL" with "buffer, MAX_PATH, &needed"
396      *
397      * We need to use a different API (AddPortEx) instead
398      */
399     if (0)
400     {
401     /* create a Port for a normal, writeable file */
402     SetLastError(0xdeadbeef);
403     res = pXcvDataPort(hXcv, cmd_AddPortW, (PBYTE) tempfileW, (lstrlenW(tempfileW) + 1) * sizeof(WCHAR), NULL, 0, NULL);
404
405     /* add our testport again */
406     SetLastError(0xdeadbeef);
407     res = pXcvDataPort(hXcv, cmd_AddPortW, (PBYTE) tempfileW, (lstrlenW(tempfileW) + 1) * sizeof(WCHAR), NULL, 0, NULL);
408
409     /* create a well-known Port  */
410     SetLastError(0xdeadbeef);
411     res = pXcvDataPort(hXcv, cmd_AddPortW, (PBYTE) portname_lpt1W, (lstrlenW(portname_lpt1W) + 1) * sizeof(WCHAR), NULL, 0, NULL);
412
413     SetLastError(0xdeadbeef);
414     res = pXcvDataPort(hXcv, cmd_AddPortW, (PBYTE) portname_lpt1W, (lstrlenW(portname_lpt1W) + 1) * sizeof(WCHAR), NULL, 0, NULL);
415     /* native localspl.dll on wine: ERROR_ALREADY_EXISTS */
416
417     /* ERROR_ALREADY_EXISTS is also returned from native localspl.dll on wine,
418        when "RPT1:" was already installed for redmonnt.dll:
419        res = pXcvDataPort(hXcv, cmd_AddPortW, (PBYTE) portname_rpt1W, ...
420     */
421
422     /* cleanup */
423     SetLastError(0xdeadbeef);
424     res = pXcvDataPort(hXcv, cmd_DeletePortW, (PBYTE) tempfileW, (lstrlenW(tempfileW) + 1) * sizeof(WCHAR), NULL, 0, NULL);
425     }
426
427     pXcvClosePort(hXcv);
428 }
429
430 /* ########################### */
431
432 static void test_XcvDataPort_ConfigureLPTPortCommandOK(void)
433 {
434     CHAR    org_value[16];
435     CHAR    buffer[16];
436     HKEY    hroot = NULL;
437     HANDLE  hXcv;
438     DWORD   res;
439     DWORD   needed;
440
441
442     hXcv = (HANDLE) 0xdeadbeef;
443     SetLastError(0xdeadbeef);
444     res = pXcvOpenPort(emptyW, SERVER_ACCESS_ADMINISTER, &hXcv);
445     ok(res, "hXcv: %d with %u and %p (expected '!= 0')\n", res, GetLastError(), hXcv);
446     if (!res) return;
447
448     /* Read the original value from the registry */
449     res = RegOpenKeyExA(HKEY_LOCAL_MACHINE, WinNT_CV_WindowsA, 0, KEY_ALL_ACCESS, &hroot);
450     if (res == ERROR_ACCESS_DENIED) {
451         pXcvClosePort(hXcv);
452         skip("ACCESS_DENIED\n");
453         return;
454     }
455
456     if (res != ERROR_SUCCESS) {
457         /* unable to open the registry: skip the test */
458         pXcvClosePort(hXcv);
459         skip("got %d\n", res);
460         return;
461     }
462     org_value[0] = '\0';
463     needed = sizeof(org_value)-1 ;
464     res = RegQueryValueExA(hroot, TransmissionRetryTimeoutA, NULL, NULL, (PBYTE) org_value, &needed);
465     ok( (res == ERROR_SUCCESS) || (res == ERROR_FILE_NOT_FOUND),
466         "returned %u and %u for \"%s\" (expected ERROR_SUCCESS or "
467         "ERROR_FILE_NOT_FOUND)\n", res, needed, org_value);
468
469     RegDeleteValueA(hroot, TransmissionRetryTimeoutA);
470
471     /* set to "0" */
472     needed = (DWORD) 0xdeadbeef;
473     SetLastError(0xdeadbeef);
474     res = pXcvDataPort(hXcv, cmd_ConfigureLPTPortCommandOKW, (PBYTE) num_0W, sizeof(num_0W), NULL, 0, &needed);
475     if (res == ERROR_INVALID_PARAMETER) {
476         pXcvClosePort(hXcv);
477         skip("'ConfigureLPTPortCommandOK' not supported\n");
478         return;
479     }
480     ok( res == ERROR_SUCCESS, "returned %d with %u (expected ERROR_SUCCESS)\n", res, GetLastError());
481     needed = sizeof(buffer)-1 ;
482     res = RegQueryValueExA(hroot, TransmissionRetryTimeoutA, NULL, NULL, (PBYTE) buffer, &needed);
483     ok( (res == ERROR_SUCCESS) && (lstrcmpA(buffer, num_0A) == 0),
484         "returned %d and '%s' (expected ERROR_SUCCESS and '%s')\n",
485         res, buffer, num_0A);
486
487
488     /* set to "1" */
489     needed = (DWORD) 0xdeadbeef;
490     SetLastError(0xdeadbeef);
491     res = pXcvDataPort(hXcv, cmd_ConfigureLPTPortCommandOKW, (PBYTE) num_1W, sizeof(num_1W), NULL, 0, &needed);
492     ok( res == ERROR_SUCCESS, "returned %d with %u (expected ERROR_SUCCESS)\n", res, GetLastError());
493     needed = sizeof(buffer)-1 ;
494     res = RegQueryValueExA(hroot, TransmissionRetryTimeoutA, NULL, NULL, (PBYTE) buffer, &needed);
495     ok( (res == ERROR_SUCCESS) && (lstrcmpA(buffer, num_1A) == 0),
496         "returned %d and '%s' (expected ERROR_SUCCESS and '%s')\n",
497         res, buffer, num_1A);
498
499     /* set to "999999" */
500     needed = (DWORD) 0xdeadbeef;
501     SetLastError(0xdeadbeef);
502     res = pXcvDataPort(hXcv, cmd_ConfigureLPTPortCommandOKW, (PBYTE) num_999999W, sizeof(num_999999W), NULL, 0, &needed);
503     ok( res == ERROR_SUCCESS, "returned %d with %u (expected ERROR_SUCCESS)\n", res, GetLastError());
504     needed = sizeof(buffer)-1 ;
505     res = RegQueryValueExA(hroot, TransmissionRetryTimeoutA, NULL, NULL, (PBYTE) buffer, &needed);
506     ok( (res == ERROR_SUCCESS) && (lstrcmpA(buffer, num_999999A) == 0),
507         "returned %d and '%s' (expected ERROR_SUCCESS and '%s')\n",
508         res, buffer, num_999999A);
509
510     /* set to "1000000" */
511     needed = (DWORD) 0xdeadbeef;
512     SetLastError(0xdeadbeef);
513     res = pXcvDataPort(hXcv, cmd_ConfigureLPTPortCommandOKW, (PBYTE) num_1000000W, sizeof(num_1000000W), NULL, 0, &needed);
514     ok( res == ERROR_SUCCESS, "returned %d with %u (expected ERROR_SUCCESS)\n", res, GetLastError());
515     needed = sizeof(buffer)-1 ;
516     res = RegQueryValueExA(hroot, TransmissionRetryTimeoutA, NULL, NULL, (PBYTE) buffer, &needed);
517     ok( (res == ERROR_SUCCESS) && (lstrcmpA(buffer, num_1000000A) == 0),
518         "returned %d and '%s' (expected ERROR_SUCCESS and '%s')\n",
519         res, buffer, num_1000000A);
520
521     /*  using cmd_ConfigureLPTPortCommandOKW with does_not_existW:
522         the string "does_not_exist" is written to the registry */
523
524
525     /* restore the original value */
526     RegDeleteValueA(hroot, TransmissionRetryTimeoutA);
527     if (org_value[0]) {
528         res = RegSetValueExA(hroot, TransmissionRetryTimeoutA, 0, REG_SZ, (PBYTE)org_value, lstrlenA(org_value)+1);
529         ok(res == ERROR_SUCCESS, "unable to restore original value (got %u): %s\n", res, org_value);
530     }
531
532     RegCloseKey(hroot);
533     pXcvClosePort(hXcv);
534
535 }
536
537 /* ########################### */
538
539 static void test_XcvDataPort_DeletePort(void)
540 {
541     DWORD   res;
542     HANDLE  hXcv;
543     DWORD   needed;
544
545
546     hXcv = (HANDLE) 0xdeadbeef;
547     SetLastError(0xdeadbeef);
548     res = pXcvOpenPort(emptyW, SERVER_ALL_ACCESS, &hXcv);
549     ok(res, "hXcv: %d with %u and %p (expected '!= 0')\n", res, GetLastError(), hXcv);
550     if (!res) return;
551
552     /* cleanup: just to make sure */
553     needed = (DWORD) 0xdeadbeef;
554     SetLastError(0xdeadbeef);
555     res = pXcvDataPort(hXcv, cmd_DeletePortW, (PBYTE) tempfileW, (lstrlenW(tempfileW) + 1) * sizeof(WCHAR), NULL, 0, &needed);
556     ok( !res  || (res == ERROR_FILE_NOT_FOUND),
557         "returned %d with %u (expected ERROR_SUCCESS or ERROR_FILE_NOT_FOUND)\n",
558         res, GetLastError());
559
560
561     /* ToDo: cmd_AddPortW for tempfileW, then cmd_DeletePortW for the existing Port */
562
563
564     /* try to delete a non-existing Port */
565     needed = (DWORD) 0xdeadbeef;
566     SetLastError(0xdeadbeef);
567     res = pXcvDataPort(hXcv, cmd_DeletePortW, (PBYTE) tempfileW, (lstrlenW(tempfileW) + 1) * sizeof(WCHAR), NULL, 0, &needed);
568     ok( res == ERROR_FILE_NOT_FOUND,
569         "returned %d with %u (expected ERROR_FILE_NOT_FOUND)\n", res, GetLastError());
570
571     /* emptyW as Portname: ERROR_FILE_NOT_FOUND is returned */
572     /* NULL as Portname: Native localspl.dll crashed */
573
574     pXcvClosePort(hXcv);
575 }
576
577 /* ########################### */
578
579 static void test_XcvDataPort_GetTransmissionRetryTimeout(void)
580 {
581     CHAR    org_value[16];
582     HKEY    hroot = NULL;
583     HANDLE  hXcv;
584     DWORD   buffer[2];
585     DWORD   res;
586     DWORD   needed;
587     DWORD   len;
588
589
590     hXcv = (HANDLE) 0xdeadbeef;
591     SetLastError(0xdeadbeef);
592     res = pXcvOpenPort(emptyW, SERVER_ACCESS_ADMINISTER, &hXcv);
593     ok(res, "hXcv: %d with %u and %p (expected '!= 0')\n", res, GetLastError(), hXcv);
594     if (!res) return;
595
596     /* ask for needed size */
597     needed = (DWORD) 0xdeadbeef;
598     SetLastError(0xdeadbeef);
599     res = pXcvDataPort(hXcv, cmd_GetTransmissionRetryTimeoutW, NULL, 0, NULL, 0, &needed);
600     if (res == ERROR_INVALID_PARAMETER) {
601         pXcvClosePort(hXcv);
602         skip("'GetTransmissionRetryTimeout' not supported\n");
603         return;
604     }
605     len = sizeof(DWORD);
606     ok( (res == ERROR_INSUFFICIENT_BUFFER) && (needed == len),
607         "returned %d with %u and %u (expected ERROR_INSUFFICIENT_BUFFER "
608         "and '%u')\n", res, GetLastError(), needed, len);
609     len = needed;
610
611     /* Read the original value from the registry */
612     res = RegOpenKeyExA(HKEY_LOCAL_MACHINE, WinNT_CV_WindowsA, 0, KEY_ALL_ACCESS, &hroot);
613     if (res == ERROR_ACCESS_DENIED) {
614         pXcvClosePort(hXcv);
615         skip("ACCESS_DENIED\n");
616         return;
617     }
618
619     if (res != ERROR_SUCCESS) {
620         /* unable to open the registry: skip the test */
621         pXcvClosePort(hXcv);
622         skip("got %d\n", res);
623         return;
624     }
625
626     org_value[0] = '\0';
627     needed = sizeof(org_value)-1 ;
628     res = RegQueryValueExA(hroot, TransmissionRetryTimeoutA, NULL, NULL, (PBYTE) org_value, &needed);
629     ok( (res == ERROR_SUCCESS) || (res == ERROR_FILE_NOT_FOUND),
630         "returned %u and %u for \"%s\" (expected ERROR_SUCCESS or "
631         "ERROR_FILE_NOT_FOUND)\n", res, needed, org_value);
632
633     /* Get default value (documented as 90 in the w2k reskit, but that is wrong) */
634     RegDeleteValueA(hroot, TransmissionRetryTimeoutA);
635     needed = (DWORD) 0xdeadbeef;
636     buffer[0] = 0xdeadbeef;
637     SetLastError(0xdeadbeef);
638     res = pXcvDataPort(hXcv, cmd_GetTransmissionRetryTimeoutW, NULL, 0, (PBYTE) buffer, len, &needed);
639     ok( (res == ERROR_SUCCESS) && (buffer[0] == 45),
640         "returned %d with %u and %u for %d\n (expected ERROR_SUCCESS "
641         "for '45')\n", res, GetLastError(), needed, buffer[0]);
642
643     /* the default timeout is returned, when the value is empty */
644     res = RegSetValueExA(hroot, TransmissionRetryTimeoutA, 0, REG_SZ, (PBYTE)emptyA, 1);
645     needed = (DWORD) 0xdeadbeef;
646     buffer[0] = 0xdeadbeef;
647     SetLastError(0xdeadbeef);
648     res = pXcvDataPort(hXcv, cmd_GetTransmissionRetryTimeoutW, NULL, 0, (PBYTE) buffer, len, &needed);
649     ok( (res == ERROR_SUCCESS) && (buffer[0] == 45),
650         "returned %d with %u and %u for %d\n (expected ERROR_SUCCESS "
651         "for '45')\n", res, GetLastError(), needed, buffer[0]);
652
653     /* the dialog is limited (1 - 999999), but that is done somewhere else */
654     res = RegSetValueExA(hroot, TransmissionRetryTimeoutA, 0, REG_SZ, (PBYTE)num_0A, lstrlenA(num_0A)+1);
655     needed = (DWORD) 0xdeadbeef;
656     buffer[0] = 0xdeadbeef;
657     SetLastError(0xdeadbeef);
658     res = pXcvDataPort(hXcv, cmd_GetTransmissionRetryTimeoutW, NULL, 0, (PBYTE) buffer, len, &needed);
659     ok( (res == ERROR_SUCCESS) && (buffer[0] == 0),
660         "returned %d with %u and %u for %d\n (expected ERROR_SUCCESS "
661         "for '0')\n", res, GetLastError(), needed, buffer[0]);
662
663
664     res = RegSetValueExA(hroot, TransmissionRetryTimeoutA, 0, REG_SZ, (PBYTE)num_1A, lstrlenA(num_1A)+1);
665     needed = (DWORD) 0xdeadbeef;
666     buffer[0] = 0xdeadbeef;
667     SetLastError(0xdeadbeef);
668     res = pXcvDataPort(hXcv, cmd_GetTransmissionRetryTimeoutW, NULL, 0, (PBYTE) buffer, len, &needed);
669     ok( (res == ERROR_SUCCESS) && (buffer[0] == 1),
670         "returned %d with %u and %u for %d\n (expected 'ERROR_SUCCESS' "
671         "for '1')\n", res, GetLastError(), needed, buffer[0]);
672
673     res = RegSetValueExA(hroot, TransmissionRetryTimeoutA, 0, REG_SZ, (PBYTE)num_999999A, lstrlenA(num_999999A)+1);
674     needed = (DWORD) 0xdeadbeef;
675     buffer[0] = 0xdeadbeef;
676     SetLastError(0xdeadbeef);
677     res = pXcvDataPort(hXcv, cmd_GetTransmissionRetryTimeoutW, NULL, 0, (PBYTE) buffer, len, &needed);
678     ok( (res == ERROR_SUCCESS) && (buffer[0] == 999999),
679         "returned %d with %u and %u for %d\n (expected ERROR_SUCCESS "
680         "for '999999')\n", res, GetLastError(), needed, buffer[0]);
681
682
683     res = RegSetValueExA(hroot, TransmissionRetryTimeoutA, 0, REG_SZ, (PBYTE)num_1000000A, lstrlenA(num_1000000A)+1);
684     needed = (DWORD) 0xdeadbeef;
685     buffer[0] = 0xdeadbeef;
686     SetLastError(0xdeadbeef);
687     res = pXcvDataPort(hXcv, cmd_GetTransmissionRetryTimeoutW, NULL, 0, (PBYTE) buffer, len, &needed);
688     ok( (res == ERROR_SUCCESS) && (buffer[0] == 1000000),
689         "returned %d with %u and %u for %d\n (expected ERROR_SUCCESS "
690         "for '1000000')\n", res, GetLastError(), needed, buffer[0]);
691
692     /* restore the original value */
693     RegDeleteValueA(hroot, TransmissionRetryTimeoutA);
694     if (org_value[0]) {
695         res = RegSetValueExA(hroot, TransmissionRetryTimeoutA, 0, REG_SZ, (PBYTE)org_value, lstrlenA(org_value)+1);
696         ok(res == ERROR_SUCCESS, "unable to restore original value (got %u): %s\n", res, org_value);
697     }
698
699     RegCloseKey(hroot);
700     pXcvClosePort(hXcv);
701 }
702
703 /* ########################### */
704
705 static void test_XcvDataPort_MonitorUI(void)
706 {
707     DWORD   res;
708     HANDLE  hXcv;
709     BYTE    buffer[MAX_PATH + 2];
710     DWORD   needed;
711     DWORD   len;
712
713
714     hXcv = (HANDLE) 0xdeadbeef;
715     SetLastError(0xdeadbeef);
716     res = pXcvOpenPort(emptyW, SERVER_ACCESS_ADMINISTER, &hXcv);
717     ok(res, "returned %d with %u and %p (expected '!= 0')\n", res, GetLastError(), hXcv);
718     if (!res) return;
719
720     /* ask for needed size */
721     needed = (DWORD) 0xdeadbeef;
722     SetLastError(0xdeadbeef);
723     res = pXcvDataPort(hXcv, cmd_MonitorUIW, NULL, 0, NULL, 0, &needed);
724     if (res == ERROR_INVALID_PARAMETER) {
725         pXcvClosePort(hXcv);
726         skip("'MonitorUI' nor supported\n");
727         return;
728     }
729     ok( (res == ERROR_INSUFFICIENT_BUFFER) && (needed <= MAX_PATH),
730         "returned %d with %u and 0x%x (expected 'ERROR_INSUFFICIENT_BUFFER' "
731         " and '<= MAX_PATH')\n", res, GetLastError(), needed);
732
733     if (needed > MAX_PATH) {
734         pXcvClosePort(hXcv);
735         skip("buffer overflow (%u)\n", needed);
736         return;
737     }
738     len = needed;
739
740     /* the command is required */
741     needed = (DWORD) 0xdeadbeef;
742     SetLastError(0xdeadbeef);
743     res = pXcvDataPort(hXcv, emptyW, NULL, 0, NULL, 0, &needed);
744     ok( res == ERROR_INVALID_PARAMETER, "returned %d with %u and 0x%x "
745         "(expected 'ERROR_INVALID_PARAMETER')\n", res, GetLastError(), needed);
746
747     if (0) {
748     /* crash with native localspl.dll (w2k+xp) */
749     res = pXcvDataPort(hXcv, NULL, NULL, 0, buffer, MAX_PATH, &needed);
750     res = pXcvDataPort(hXcv, cmd_MonitorUIW, NULL, 0, NULL, len, &needed);
751     res = pXcvDataPort(hXcv, cmd_MonitorUIW, NULL, 0, buffer, len, NULL);
752     }
753
754
755     /* hXcv is ignored for the command "MonitorUI" */
756     needed = (DWORD) 0xdeadbeef;
757     SetLastError(0xdeadbeef);
758     res = pXcvDataPort(NULL, cmd_MonitorUIW, NULL, 0, buffer, len, &needed);
759     ok( res == ERROR_SUCCESS, "returned %d with %u and 0x%x "
760         "(expected 'ERROR_SUCCESS')\n", res, GetLastError(), needed);
761
762
763     /* pszDataName is case-sensitive */
764     memset(buffer, 0, len);
765     needed = (DWORD) 0xdeadbeef;
766     SetLastError(0xdeadbeef);
767     res = pXcvDataPort(hXcv, cmd_MonitorUI_lcaseW, NULL, 0, buffer, len, &needed);
768     ok( res == ERROR_INVALID_PARAMETER, "returned %d with %u and 0x%x "
769         "(expected 'ERROR_INVALID_PARAMETER')\n", res, GetLastError(), needed);
770
771     /* off by one: larger  */
772     needed = (DWORD) 0xdeadbeef;
773     SetLastError(0xdeadbeef);
774     res = pXcvDataPort(hXcv, cmd_MonitorUIW, NULL, 0, buffer, len+1, &needed);
775     ok( res == ERROR_SUCCESS, "returned %d with %u and 0x%x "
776         "(expected 'ERROR_SUCCESS')\n", res, GetLastError(), needed);
777
778
779     /* off by one: smaller */
780     /* the buffer is not modified for NT4, w2k, XP */
781     needed = (DWORD) 0xdeadbeef;
782     SetLastError(0xdeadbeef);
783     res = pXcvDataPort(hXcv, cmd_MonitorUIW, NULL, 0, buffer, len-1, &needed);
784     ok( res == ERROR_INSUFFICIENT_BUFFER, "returned %d with %u and 0x%x "
785         "(expected 'ERROR_INSUFFICIENT_BUFFER')\n", res, GetLastError(), needed);
786
787     /* Normal use. The DLL-Name without a Path is returned */
788     memset(buffer, 0, len);
789     needed = (DWORD) 0xdeadbeef;
790     SetLastError(0xdeadbeef);
791     res = pXcvDataPort(hXcv, cmd_MonitorUIW, NULL, 0, buffer, len, &needed);
792     ok( res == ERROR_SUCCESS, "returned %d with %u and 0x%x "
793         "(expected 'ERROR_SUCCESS')\n", res, GetLastError(), needed);
794
795
796     pXcvClosePort(hXcv);
797
798
799     /* small check without access-rights: */
800     hXcv = (HANDLE) 0xdeadbeef;
801     SetLastError(0xdeadbeef);
802     res = pXcvOpenPort(emptyW, 0, &hXcv);
803     ok(res, "returned %d with %u and %p (expected '!= 0')\n", res, GetLastError(), hXcv);
804     if (!res) return;
805
806     /* The ACCESS_MASK is ignored for "MonitorUI" */
807     memset(buffer, 0, len);
808     needed = (DWORD) 0xdeadbeef;
809     SetLastError(0xdeadbeef);
810     res = pXcvDataPort(hXcv, cmd_MonitorUIW, NULL, 0, buffer, sizeof(buffer), &needed);
811     ok( res == ERROR_SUCCESS, "returned %d with %u and 0x%x "
812         "(expected 'ERROR_SUCCESS')\n", res, GetLastError(), needed);
813
814     pXcvClosePort(hXcv);
815
816 }
817
818 /* ########################### */
819
820 static void test_XcvDataPort_PortIsValid(void)
821 {
822     DWORD   res;
823     HANDLE  hXcv;
824     DWORD   needed;
825
826
827     hXcv = (HANDLE) 0xdeadbeef;
828     SetLastError(0xdeadbeef);
829     res = pXcvOpenPort(emptyW, SERVER_ACCESS_ADMINISTER, &hXcv);
830     ok(res, "hXcv: %d with %u and %p (expected '!= 0')\n", res, GetLastError(), hXcv);
831     if (!res) return;
832
833     /* normal use: "LPT1:" */
834     needed = (DWORD) 0xdeadbeef;
835     SetLastError(0xdeadbeef);
836     res = pXcvDataPort(hXcv, cmd_PortIsValidW, (PBYTE) portname_lpt1W, sizeof(portname_lpt1W), NULL, 0, &needed);
837     if (res == ERROR_INVALID_PARAMETER) {
838         pXcvClosePort(hXcv);
839         skip("'PostIsValid' not supported\n");
840         return;
841     }
842     ok( res == ERROR_SUCCESS, "returned %d with %u (expected ERROR_SUCCESS)\n", res, GetLastError());
843
844
845     if (0) {
846     /* crash with native localspl.dll (w2k+xp) */
847     res = pXcvDataPort(hXcv, cmd_PortIsValidW, NULL, 0, NULL, 0, &needed);
848     }
849
850
851     /* hXcv is ignored for the command "PortIsValid" */
852     needed = (DWORD) 0xdeadbeef;
853     SetLastError(0xdeadbeef);
854     res = pXcvDataPort(NULL, cmd_PortIsValidW, (PBYTE) portname_lpt1W, sizeof(portname_lpt1W), NULL, 0, NULL);
855     ok( res == ERROR_SUCCESS, "returned %d with %u (expected ERROR_SUCCESS)\n", res, GetLastError());
856
857     /* needed is ignored */
858     needed = (DWORD) 0xdeadbeef;
859     SetLastError(0xdeadbeef);
860     res = pXcvDataPort(hXcv, cmd_PortIsValidW, (PBYTE) portname_lpt1W, sizeof(portname_lpt1W), NULL, 0, NULL);
861     ok( res == ERROR_SUCCESS, "returned %d with %u (expected ERROR_SUCCESS)\n", res, GetLastError());
862
863
864     /* cbInputData is ignored */
865     needed = (DWORD) 0xdeadbeef;
866     SetLastError(0xdeadbeef);
867     res = pXcvDataPort(hXcv, cmd_PortIsValidW, (PBYTE) portname_lpt1W, 0, NULL, 0, &needed);
868     ok( res == ERROR_SUCCESS,
869         "returned %d with %u and 0x%x (expected ERROR_SUCCESS)\n",
870         res, GetLastError(), needed);
871
872     needed = (DWORD) 0xdeadbeef;
873     SetLastError(0xdeadbeef);
874     res = pXcvDataPort(hXcv, cmd_PortIsValidW, (PBYTE) portname_lpt1W, 1, NULL, 0, &needed);
875     ok( res == ERROR_SUCCESS,
876         "returned %d with %u and 0x%x (expected ERROR_SUCCESS)\n",
877         res, GetLastError(), needed);
878
879     needed = (DWORD) 0xdeadbeef;
880     SetLastError(0xdeadbeef);
881     res = pXcvDataPort(hXcv, cmd_PortIsValidW, (PBYTE) portname_lpt1W, sizeof(portname_lpt1W) -1, NULL, 0, &needed);
882     ok( res == ERROR_SUCCESS,
883         "returned %d with %u and 0x%x (expected ERROR_SUCCESS)\n",
884         res, GetLastError(), needed);
885
886     needed = (DWORD) 0xdeadbeef;
887     SetLastError(0xdeadbeef);
888     res = pXcvDataPort(hXcv, cmd_PortIsValidW, (PBYTE) portname_lpt1W, sizeof(portname_lpt1W) -2, NULL, 0, &needed);
889     ok( res == ERROR_SUCCESS,
890         "returned %d with %u and 0x%x (expected ERROR_SUCCESS)\n",
891         res, GetLastError(), needed);
892
893
894     /* an empty name is not allowed */
895     needed = (DWORD) 0xdeadbeef;
896     SetLastError(0xdeadbeef);
897     res = pXcvDataPort(hXcv, cmd_PortIsValidW, (PBYTE) emptyW, sizeof(emptyW), NULL, 0, &needed);
898     ok( res == ERROR_PATH_NOT_FOUND,
899         "returned %d with %u and 0x%x (expected ERROR_PATH_NOT_FOUND)\n",
900         res, GetLastError(), needed);
901
902
903     /* a directory is not allowed */
904     needed = (DWORD) 0xdeadbeef;
905     SetLastError(0xdeadbeef);
906     res = pXcvDataPort(hXcv, cmd_PortIsValidW, (PBYTE) tempdirW, (lstrlenW(tempdirW) + 1) * sizeof(WCHAR), NULL, 0, &needed);
907     /* XP(admin): ERROR_INVALID_NAME, XP(user): ERROR_PATH_NOT_FOUND, w2k ERROR_ACCESS_DENIED */
908     ok( (res == ERROR_INVALID_NAME) || (res == ERROR_PATH_NOT_FOUND) ||
909         (res == ERROR_ACCESS_DENIED), "returned %d with %u and 0x%x "
910         "(expected ERROR_INVALID_NAME, ERROR_PATH_NOT_FOUND or ERROR_ACCESS_DENIED)\n",
911         res, GetLastError(), needed);
912
913
914     /* test more valid well known Ports: */
915     needed = (DWORD) 0xdeadbeef;
916     SetLastError(0xdeadbeef);
917     res = pXcvDataPort(hXcv, cmd_PortIsValidW, (PBYTE) portname_lpt2W, sizeof(portname_lpt2W), NULL, 0, &needed);
918     ok( res == ERROR_SUCCESS,
919         "returned %d with %u and 0x%x (expected ERROR_SUCCESS)\n",
920         res, GetLastError(), needed);
921
922
923     needed = (DWORD) 0xdeadbeef;
924     SetLastError(0xdeadbeef);
925     res = pXcvDataPort(hXcv, cmd_PortIsValidW, (PBYTE) portname_com1W, sizeof(portname_com1W), NULL, 0, &needed);
926     ok( res == ERROR_SUCCESS,
927         "returned %d with %u and 0x%x (expected ERROR_SUCCESS)\n",
928         res, GetLastError(), needed);
929
930
931     needed = (DWORD) 0xdeadbeef;
932     SetLastError(0xdeadbeef);
933     res = pXcvDataPort(hXcv, cmd_PortIsValidW, (PBYTE) portname_com2W, sizeof(portname_com2W), NULL, 0, &needed);
934     ok( res == ERROR_SUCCESS,
935         "returned %d with %u and 0x%x (expected ERROR_SUCCESS)\n",
936         res, GetLastError(), needed);
937
938
939     needed = (DWORD) 0xdeadbeef;
940     SetLastError(0xdeadbeef);
941     res = pXcvDataPort(hXcv, cmd_PortIsValidW, (PBYTE) portname_fileW, sizeof(portname_fileW), NULL, 0, &needed);
942     ok( res == ERROR_SUCCESS,
943         "returned %d with %u and 0x%x (expected ERROR_SUCCESS)\n",
944         res, GetLastError(), needed);
945
946
947     /* a normal, writable file is allowed */
948     needed = (DWORD) 0xdeadbeef;
949     SetLastError(0xdeadbeef);
950     res = pXcvDataPort(hXcv, cmd_PortIsValidW, (PBYTE) tempfileW, (lstrlenW(tempfileW) + 1) * sizeof(WCHAR), NULL, 0, &needed);
951     ok( res == ERROR_SUCCESS,
952         "returned %d with %u and 0x%x (expected ERROR_SUCCESS)\n",
953         res, GetLastError(), needed);
954
955     pXcvClosePort(hXcv);
956
957
958     /* small check without access-rights: */
959     hXcv = (HANDLE) 0xdeadbeef;
960     SetLastError(0xdeadbeef);
961     res = pXcvOpenPort(emptyW, 0, &hXcv);
962     ok(res, "returned %d with %u and %p (expected '!= 0')\n", res, GetLastError(), hXcv);
963     if (!res) return;
964
965     /* The ACCESS_MASK from XcvOpenPort is ignored in "PortIsValid" */
966     needed = (DWORD) 0xdeadbeef;
967     SetLastError(0xdeadbeef);
968     res = pXcvDataPort(hXcv, cmd_PortIsValidW, (PBYTE) portname_lpt1W, sizeof(portname_lpt1W), NULL, 0, &needed);
969     ok( res == ERROR_SUCCESS, "returned %d with %u (expected ERROR_SUCCESS)\n", res, GetLastError());
970
971     pXcvClosePort(hXcv);
972
973 }
974
975 /* ########################### */
976
977 static void test_XcvOpenPort(void)
978 {
979     DWORD   res;
980     HANDLE  hXcv;
981
982
983     if (0)
984     {
985     /* crash with native localspl.dll (w2k+xp) */
986     res = pXcvOpenPort(NULL, SERVER_ACCESS_ADMINISTER, &hXcv);
987     res = pXcvOpenPort(emptyW, SERVER_ACCESS_ADMINISTER, NULL);
988     }
989
990
991     /* The returned handle is the result from a previous "spoolss.dll,DllAllocSplMem" */
992     SetLastError(0xdeadbeef);
993     hXcv = (HANDLE) 0xdeadbeef;
994     res = pXcvOpenPort(emptyW, SERVER_ACCESS_ADMINISTER, &hXcv);
995     ok(res, "returned %d with %u and %p (expected '!= 0')\n", res, GetLastError(), hXcv);
996     if (res) pXcvClosePort(hXcv);
997
998
999     /* The ACCESS_MASK is not checked in XcvOpenPort */
1000     SetLastError(0xdeadbeef);
1001     hXcv = (HANDLE) 0xdeadbeef;
1002     res = pXcvOpenPort(emptyW, 0, &hXcv);
1003     ok(res, "returned %d with %u and %p (expected '!= 0')\n", res, GetLastError(), hXcv);
1004     if (res) pXcvClosePort(hXcv);
1005
1006
1007     /* A copy of pszObject is saved in the Memory-Block */
1008     SetLastError(0xdeadbeef);
1009     hXcv = (HANDLE) 0xdeadbeef;
1010     res = pXcvOpenPort(portname_lpt1W, SERVER_ALL_ACCESS, &hXcv);
1011     ok(res, "returned %d with %u and %p (expected '!= 0')\n", res, GetLastError(), hXcv);
1012     if (res) pXcvClosePort(hXcv);
1013
1014     SetLastError(0xdeadbeef);
1015     hXcv = (HANDLE) 0xdeadbeef;
1016     res = pXcvOpenPort(portname_fileW, SERVER_ALL_ACCESS, &hXcv);
1017     ok(res, "returned %d with %u and %p (expected '!= 0')\n", res, GetLastError(), hXcv);
1018     if (res) pXcvClosePort(hXcv);
1019
1020 }
1021
1022 /* ########################### */
1023
1024 #define GET_MONITOR_FUNC(name) \
1025             if(numentries > 0) { \
1026                 numentries--; \
1027                 p##name = (void *) pm->Monitor.pfn##name ;  \
1028             }
1029
1030
1031 START_TEST(localmon)
1032 {
1033     DWORD   numentries;
1034     DWORD   res;
1035
1036     /* This DLL does not exist on Win9x */
1037     hdll = LoadLibraryA("localspl.dll");
1038     if (!hdll) return;
1039
1040     tempdirW[0] = '\0';
1041     tempfileW[0] = '\0';
1042     res = GetTempPathW(MAX_PATH, tempdirW);
1043     ok(res != 0, "with %u\n", GetLastError());
1044     res = GetTempFileNameW(tempdirW, wineW, 0, tempfileW);
1045     ok(res != 0, "with %u\n", GetLastError());
1046
1047     pInitializePrintMonitor = (void *) GetProcAddress(hdll, "InitializePrintMonitor");
1048     pInitializePrintMonitorUI = (void *) GetProcAddress(hdll, "InitializePrintMonitorUI");
1049
1050     if (!pInitializePrintMonitor) {
1051         /* The Monitor for "Local Ports" was in a seperate dll before w2k */
1052         hlocalmon = LoadLibraryA("localmon.dll");
1053         if (hlocalmon) {
1054             pInitializePrintMonitor = (void *) GetProcAddress(hlocalmon, "InitializePrintMonitor");
1055         }
1056     }
1057     if (!pInitializePrintMonitor) return;
1058
1059     /* Native localmon.dll / localspl.dll need a vaild Port-Entry in:
1060        a) since xp: HKLM\Software\Microsoft\Windows NT\CurrentVersion\Ports 
1061        b) up to w2k: Section "Ports" in win.ini
1062        or InitializePrintMonitor fails. */
1063     pm = pInitializePrintMonitor(Monitors_LocalPortW);
1064     if (pm) {
1065         numentries = (pm->dwMonitorSize ) / sizeof(VOID *);
1066         /* NT4: 14, since w2k: 17 */
1067         ok( numentries == 14 || numentries == 17, 
1068             "dwMonitorSize (%d) => %d Functions\n", pm->dwMonitorSize, numentries);
1069
1070         GET_MONITOR_FUNC(EnumPorts);
1071         GET_MONITOR_FUNC(OpenPort);
1072         GET_MONITOR_FUNC(OpenPortEx);
1073         GET_MONITOR_FUNC(StartDocPort);
1074         GET_MONITOR_FUNC(WritePort);
1075         GET_MONITOR_FUNC(ReadPort);
1076         GET_MONITOR_FUNC(EndDocPort);
1077         GET_MONITOR_FUNC(ClosePort);
1078         GET_MONITOR_FUNC(AddPort);
1079         GET_MONITOR_FUNC(AddPortEx);
1080         GET_MONITOR_FUNC(ConfigurePort);
1081         GET_MONITOR_FUNC(DeletePort);
1082         GET_MONITOR_FUNC(GetPrinterDataFromPort);
1083         GET_MONITOR_FUNC(SetPortTimeOuts);
1084         GET_MONITOR_FUNC(XcvOpenPort);
1085         GET_MONITOR_FUNC(XcvDataPort);
1086         GET_MONITOR_FUNC(XcvClosePort);
1087     }
1088
1089     if ((!pInitializePrintMonitorUI) && (pXcvOpenPort) && (pXcvDataPort) && (pXcvClosePort)) {
1090         /* The user interface for "Local Ports" is in a separate dll since w2k */
1091         BYTE    buffer[MAX_PATH];
1092         DWORD   res;
1093         DWORD   len;
1094         HANDLE  hXcv;
1095
1096         res = pXcvOpenPort(emptyW, SERVER_ACCESS_ADMINISTER, &hXcv);
1097         if (res) {
1098             res = pXcvDataPort(hXcv, cmd_MonitorUIW, NULL, 0, buffer, MAX_PATH, &len);
1099             if (res == ERROR_SUCCESS) hlocalui = LoadLibraryW( (LPWSTR) buffer);
1100             if (hlocalui) pInitializePrintMonitorUI = (void *) GetProcAddress(hlocalui, "InitializePrintMonitorUI");
1101             pXcvClosePort(hXcv);
1102         }
1103     }
1104
1105     if (pInitializePrintMonitorUI) {
1106         pui = pInitializePrintMonitorUI();
1107         if (pui) {
1108             numentries = (pui->dwMonitorUISize - sizeof(DWORD)) / sizeof(VOID *);
1109             ok( numentries == 3,
1110                 "dwMonitorUISize (%d) => %d Functions\n", pui->dwMonitorUISize, numentries);
1111
1112             if (numentries > 2) {
1113                 pAddPortUI = pui->pfnAddPortUI;
1114                 pConfigurePortUI = pui->pfnConfigurePortUI;
1115                 pDeletePortUI = pui->pfnDeletePortUI;
1116             }
1117         }
1118     }
1119
1120     test_InitializePrintMonitor();
1121
1122     test_AddPort();
1123     test_ConfigurePort();
1124     test_DeletePort();
1125     test_EnumPorts();
1126     if ((pXcvOpenPort == NULL) || (pXcvDataPort == NULL) || (pXcvClosePort == NULL)) {
1127         skip("Xcv not supported\n");
1128     }
1129     else
1130     {
1131         test_XcvClosePort();
1132         test_XcvDataPort_AddPort();
1133         test_XcvDataPort_ConfigureLPTPortCommandOK();
1134         test_XcvDataPort_DeletePort();
1135         test_XcvDataPort_GetTransmissionRetryTimeout();
1136         test_XcvDataPort_MonitorUI();
1137         test_XcvDataPort_PortIsValid();
1138         test_XcvOpenPort();
1139     }
1140
1141     /* Cleanup our temporary file */
1142     DeleteFileW(tempfileW);
1143 }