winspool: Renamed the directory to winspool.drv.
[wine] / dlls / winspool.drv / tests / info.c
1 /*
2  * Copyright (C) 2003, 2004 Stefan Leichter
3  * Copyright (C) 2005, 2006 Detlef Riekenberg
4  * Copyright (C) 2006 Dmitry Timoshkov
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 #include <stdarg.h>
22
23 #include "wine/test.h"
24 #include "windef.h"
25 #include "winbase.h"
26 #include "winerror.h"
27 #include "wingdi.h"
28 #include "winuser.h"
29 #include "winreg.h"
30 #include "winspool.h"
31
32 #define MAGIC_DEAD  0x00dead00
33 #define DEFAULT_PRINTER_SIZE 1000
34
35 static char env_x86[] = "Windows NT x86";
36 static char env_win9x_case[] = "windowS 4.0";
37 static char winetest_monitor[] = "winetest";
38
39 static HANDLE  hwinspool;
40 static FARPROC pGetDefaultPrinterA;
41 static FARPROC pSetDefaultPrinterA;
42
43 struct monitor_entry {
44     LPSTR  env;
45     LPSTR  dllname;
46 };
47
48 /* report common behavior only once */
49 static DWORD report_deactivated_spooler = 1;
50 #define RETURN_ON_DEACTIVATED_SPOOLER(res) \
51     if((res == 0) && (GetLastError() == RPC_S_SERVER_UNAVAILABLE)) \
52     { \
53         if(report_deactivated_spooler > 0) { \
54             report_deactivated_spooler--; \
55             trace("The Service 'Spooler' is required for many test\n"); \
56         } \
57         return; \
58     }
59
60
61 static LPSTR find_default_printer(VOID)
62 {
63     static  LPSTR   default_printer = NULL;
64     static  char    buffer[DEFAULT_PRINTER_SIZE];
65     DWORD   needed;
66     DWORD   res;
67     LPSTR   ptr;
68
69     if ((default_printer == NULL) && (pGetDefaultPrinterA))
70     {
71         /* w2k and above */
72         needed = sizeof(buffer);
73         res = pGetDefaultPrinterA(buffer, &needed);
74         if(res)  default_printer = buffer;
75         trace("default_printer: '%s'\n", default_printer);
76     }
77     if (default_printer == NULL)
78     {
79         HKEY hwindows;
80         DWORD   type;
81         /* NT 3.x and above */
82         if (RegOpenKeyEx(HKEY_CURRENT_USER, 
83                         "Software\\Microsoft\\Windows NT\\CurrentVersion\\Windows",
84                         0, KEY_QUERY_VALUE, &hwindows) == NO_ERROR) {
85
86             needed = sizeof(buffer);
87             if (RegQueryValueEx(hwindows, "device", NULL, 
88                                 &type, (LPBYTE)buffer, &needed) == NO_ERROR) {
89
90                 ptr = strchr(buffer, ',');
91                 if (ptr) {
92                     ptr[0] = '\0';
93                     default_printer = buffer;
94                 }
95             }
96             RegCloseKey(hwindows);
97         }
98         trace("default_printer: '%s'\n", default_printer);
99     }
100     if (default_printer == NULL)
101     {
102         /* win9x */
103         needed = sizeof(buffer);
104         res = GetProfileStringA("windows", "device", "*", buffer, needed);
105         if(res) {
106             ptr = strchr(buffer, ',');
107             if (ptr) {
108                 ptr[0] = '\0';
109                 default_printer = buffer;
110             }
111         }
112         trace("default_printer: '%s'\n", default_printer);
113     }
114     return default_printer;
115 }
116
117
118 static struct monitor_entry * find_installed_monitor(void)
119 {
120     MONITOR_INFO_2A mi2a; 
121     static struct  monitor_entry * entry = NULL;
122     DWORD   res;
123     DWORD   num_tests;
124     DWORD   i = 0;
125
126     static struct monitor_entry  monitor_table[] = {
127         {env_win9x_case, "localspl.dll"},
128         {env_x86,        "localspl.dll"},
129         {env_win9x_case, "localmon.dll"},
130         {env_x86,        "localmon.dll"},
131         {env_win9x_case, "tcpmon.dll"},
132         {env_x86,        "tcpmon.dll"},
133         {env_win9x_case, "usbmon.dll"},
134         {env_x86,        "usbmon.dll"},
135         {env_win9x_case, "mspp32.dll"},
136         {env_x86,        "win32spl.dll"},
137         {env_x86,        "redmonnt.dll"},
138         {env_x86,        "redmon35.dll"},
139         {env_win9x_case, "redmon95.dll"},
140         {env_x86,        "pdfcmnnt.dll"},
141         {env_win9x_case, "pdfcmn95.dll"},
142     };
143
144     if (entry) return entry;
145
146     num_tests = (sizeof(monitor_table)/sizeof(struct monitor_entry));
147
148     SetLastError(MAGIC_DEAD);
149     res = DeleteMonitorA(NULL, NULL, NULL);
150     if (!res && (GetLastError() == ERROR_CALL_NOT_IMPLEMENTED)) {
151         trace("DeleteMonitorA() not implemented yet\n");
152         return NULL;
153     }
154
155     SetLastError(MAGIC_DEAD);
156     res = AddMonitorA(NULL, 0, NULL);
157     if (!res && (GetLastError() == ERROR_CALL_NOT_IMPLEMENTED)) {
158         trace("AddMonitorA() not implemented yet\n");
159         return NULL;
160     }
161
162     /* cleanup */
163     DeleteMonitorA(NULL, env_x86, winetest_monitor);
164     DeleteMonitorA(NULL, env_win9x_case, winetest_monitor);
165
166     /* find a usable monitor from the table */    
167     mi2a.pName = winetest_monitor;
168     while ((entry == NULL) && (i < num_tests)) {
169         entry = &monitor_table[i];
170         i++;
171         mi2a.pEnvironment = entry->env;
172         mi2a.pDLLName = entry->dllname;
173
174         if (AddMonitorA(NULL, 2, (LPBYTE) &mi2a)) {
175             /* we got one */
176             trace("using '%s', '%s'\n", entry->env, entry->dllname);
177             DeleteMonitorA(NULL, entry->env, winetest_monitor);
178         }
179         else
180         {
181             entry = NULL;
182         }
183     }
184     return entry;
185 }
186
187 /* ########################### */
188
189
190 static void test_AddMonitor(void)
191 {
192     MONITOR_INFO_2A mi2a; 
193     struct  monitor_entry * entry = NULL;
194     DWORD   res;
195
196     entry = find_installed_monitor();
197
198     SetLastError(MAGIC_DEAD);
199     res = AddMonitorA(NULL, 1, NULL);
200     ok(!res && (GetLastError() == ERROR_INVALID_LEVEL), 
201         "returned %ld with %ld (expected '0' with ERROR_INVALID_LEVEL)\n",
202         res, GetLastError());
203
204     SetLastError(MAGIC_DEAD);
205     res = AddMonitorA(NULL, 3, NULL);
206     ok(!res && (GetLastError() == ERROR_INVALID_LEVEL), 
207         "returned %ld with %ld (expected '0' with ERROR_INVALID_LEVEL)\n",
208         res, GetLastError());
209
210 #if 0
211     /* This test crash with win9x on vmware (works with win9x on qemu 0.8.1) */
212     SetLastError(MAGIC_DEAD);
213     res = AddMonitorA(NULL, 2, NULL);
214     /* NT: unchanged,  9x: ERROR_PRIVILEGE_NOT_HELD */
215     ok(!res &&
216         ((GetLastError() == MAGIC_DEAD) ||
217          (GetLastError() == ERROR_PRIVILEGE_NOT_HELD)), 
218         "returned %ld with %ld (expected '0' with: MAGIC_DEAD or " \
219         "ERROR_PRIVILEGE_NOT_HELD)\n", res, GetLastError());
220 #endif
221
222     ZeroMemory(&mi2a, sizeof(MONITOR_INFO_2A));
223     SetLastError(MAGIC_DEAD);
224     res = AddMonitorA(NULL, 2, (LPBYTE) &mi2a);
225     RETURN_ON_DEACTIVATED_SPOOLER(res)
226
227     if (!res && (GetLastError() == ERROR_ACCESS_DENIED)) {
228         trace("skip tests (ACCESS_DENIED)\n");
229         return;
230     }
231
232     /* NT: ERROR_INVALID_PARAMETER,  9x: ERROR_INVALID_ENVIRONMENT */
233     ok(!res && ((GetLastError() == ERROR_INVALID_PARAMETER) ||
234                 (GetLastError() == ERROR_INVALID_ENVIRONMENT)), 
235         "returned %ld with %ld (expected '0' with: ERROR_INVALID_PARAMETER or " \
236         "ERROR_INVALID_ENVIRONMENT)\n", res, GetLastError());
237
238     if (!entry) {
239         trace("No usable Monitor found: Skip tests\n");
240         return;
241     }
242
243 #if 0
244     /* The Test is deactivated, because when mi2a.pName is NULL, the subkey
245        HKLM\System\CurrentControlSet\Control\Print\Monitors\C:\WINDOWS\SYSTEM
246        or HKLM\System\CurrentControlSet\Control\Print\Monitors\ì
247        is created on win9x and we do not want to hit this bug here. */
248
249     mi2a.pEnvironment = entry->env;
250     SetLastError(MAGIC_DEAD);
251     res = AddMonitorA(NULL, 2, (LPBYTE) &mi2a);
252     /* NT: ERROR_INVALID_PARAMETER,  9x: ERROR_PRIVILEGE_NOT_HELD */
253 #endif
254
255     mi2a.pEnvironment = entry->env;
256     mi2a.pName = "";
257     SetLastError(MAGIC_DEAD);
258     res = AddMonitorA(NULL, 2, (LPBYTE) &mi2a);
259     /* NT: ERROR_INVALID_PARAMETER,  9x: ERROR_PRIVILEGE_NOT_HELD */
260     ok( !res &&
261         ((GetLastError() == ERROR_INVALID_PARAMETER) ||
262          (GetLastError() == ERROR_PRIVILEGE_NOT_HELD)), 
263         "returned %ld with %ld (expected '0' with: ERROR_INVALID_PARAMETER or " \
264         "ERROR_PRIVILEGE_NOT_HELD)\n",
265         res, GetLastError());
266
267     mi2a.pName = winetest_monitor;
268     SetLastError(MAGIC_DEAD);
269     res = AddMonitorA(NULL, 2, (LPBYTE) &mi2a);
270     /* NT: ERROR_INVALID_PARAMETER,  9x: ERROR_PRIVILEGE_NOT_HELD */
271     ok( !res &&
272         ((GetLastError() == ERROR_INVALID_PARAMETER) ||
273          (GetLastError() == ERROR_PRIVILEGE_NOT_HELD)), 
274         "returned %ld with %ld (expected '0' with: ERROR_INVALID_PARAMETER or " \
275         "ERROR_PRIVILEGE_NOT_HELD)\n",
276         res, GetLastError());
277
278     mi2a.pDLLName = "";
279     SetLastError(MAGIC_DEAD);
280     res = AddMonitorA(NULL, 2, (LPBYTE) &mi2a);
281     ok( !res && (GetLastError() == ERROR_INVALID_PARAMETER),
282         "returned %ld with %ld (expected '0' with ERROR_INVALID_PARAMETER)\n",
283         res, GetLastError());
284
285
286     mi2a.pDLLName = "does_not_exists.dll";
287     SetLastError(MAGIC_DEAD);
288     res = AddMonitorA(NULL, 2, (LPBYTE) &mi2a);
289     /* NT: ERROR_MOD_NOT_FOUND,  9x: ERROR_INVALID_PARAMETER */
290     ok( !res &&
291         ((GetLastError() == ERROR_MOD_NOT_FOUND) ||
292         (GetLastError() == ERROR_INVALID_PARAMETER)),
293         "returned %ld with %ld (expected '0' with: ERROR_MOD_NOT_FOUND or " \
294         "ERROR_INVALID_PARAMETER)\n", res, GetLastError());
295
296     mi2a.pDLLName = "version.dll";
297     SetLastError(MAGIC_DEAD);
298     res = AddMonitorA(NULL, 2, (LPBYTE) &mi2a);
299     /* NT: ERROR_PROC_NOT_FOUND,  9x: ERROR_INVALID_PARAMETER */
300     todo_wine {
301     ok( !res &&
302         ((GetLastError() == ERROR_PROC_NOT_FOUND) ||
303         (GetLastError() == ERROR_INVALID_PARAMETER)),
304         "returned %ld with %ld (expected '0' with: ERROR_PROC_NOT_FOUND or " \
305         "ERROR_INVALID_PARAMETER)\n", res, GetLastError());
306     if (res) DeleteMonitorA(NULL, entry->env, winetest_monitor); 
307     }
308
309    /* Test AddMonitor with real options */
310     mi2a.pDLLName = entry->dllname;
311     SetLastError(MAGIC_DEAD);
312     res = AddMonitorA(NULL, 2, (LPBYTE) &mi2a);
313     ok(res, "returned %ld with %ld (expected '!= 0')\n", res, GetLastError());
314     
315     /* add a monitor twice */
316     SetLastError(MAGIC_DEAD);
317     res = AddMonitorA(NULL, 2, (LPBYTE) &mi2a);
318     /* NT: ERROR_PRINT_MONITOR_ALREADY_INSTALLED (3006), 9x: ERROR_ALREADY_EXISTS (183) */
319     ok( !res &&
320         ((GetLastError() == ERROR_PRINT_MONITOR_ALREADY_INSTALLED) ||
321         (GetLastError() == ERROR_ALREADY_EXISTS)), 
322         "returned %ld with %ld (expected '0' with: " \
323         "ERROR_PRINT_MONITOR_ALREADY_INSTALLED or ERROR_ALREADY_EXISTS)\n",
324         res, GetLastError());
325
326     DeleteMonitorA(NULL, entry->env, winetest_monitor); 
327     SetLastError(MAGIC_DEAD);
328     res = AddMonitorA("", 2, (LPBYTE) &mi2a);
329     ok(res, "returned %ld with %ld (expected '!= 0')\n", res, GetLastError());
330
331     /* cleanup */
332     DeleteMonitorA(NULL, entry->env, winetest_monitor);
333
334 }
335
336 /* ########################### */
337
338 static void test_DeleteMonitor(void)
339 {
340     MONITOR_INFO_2A mi2a; 
341     struct  monitor_entry * entry = NULL;
342     DWORD   res;
343
344     entry = find_installed_monitor();
345
346     if (!entry) {
347         trace("No usable Monitor found: Skip tests\n");
348         return;
349     }
350
351     mi2a.pName = winetest_monitor;
352     mi2a.pEnvironment = entry->env;
353     mi2a.pDLLName = entry->dllname;
354
355     /* Testing DeleteMonitor with real options */
356     AddMonitorA(NULL, 2, (LPBYTE) &mi2a);
357
358     SetLastError(MAGIC_DEAD);
359     res = DeleteMonitorA(NULL, entry->env, winetest_monitor);
360     ok(res, "returned %ld with %ld (expected '!= 0')\n", res, GetLastError());
361
362     /* Delete the Monitor twice */
363     SetLastError(MAGIC_DEAD);
364     res = DeleteMonitorA(NULL, entry->env, winetest_monitor);
365     /* NT: ERROR_UNKNOWN_PRINT_MONITOR (3000), 9x: ERROR_INVALID_PARAMETER (87) */
366     ok( !res &&
367         ((GetLastError() == ERROR_UNKNOWN_PRINT_MONITOR) ||
368         (GetLastError() == ERROR_INVALID_PARAMETER)), 
369         "returned %ld with %ld (expected '0' with: ERROR_UNKNOWN_PRINT_MONITOR" \
370         " or ERROR_INVALID_PARAMETER)\n", res, GetLastError());
371
372     /* the environment */
373     AddMonitorA(NULL, 2, (LPBYTE) &mi2a);
374     SetLastError(MAGIC_DEAD);
375     res = DeleteMonitorA(NULL, NULL, winetest_monitor);
376     ok(res, "returned %ld with %ld (expected '!=0')\n", res, GetLastError());
377
378     AddMonitorA(NULL, 2, (LPBYTE) &mi2a);
379     SetLastError(MAGIC_DEAD);
380     res = DeleteMonitorA(NULL, "", winetest_monitor);
381     ok(res, "returned %ld with %ld (expected '!=0')\n", res, GetLastError());
382
383     AddMonitorA(NULL, 2, (LPBYTE) &mi2a);
384     SetLastError(MAGIC_DEAD);
385     res = DeleteMonitorA(NULL, "bad_env", winetest_monitor);
386     ok(res, "returned %ld with %ld (expected '!=0')\n", res, GetLastError());
387
388     /* the monitor-name */
389     AddMonitorA(NULL, 2, (LPBYTE) &mi2a);
390     SetLastError(MAGIC_DEAD);
391     res = DeleteMonitorA(NULL, entry->env, NULL);
392     /* NT: ERROR_INVALID_PARAMETER (87),  9x: ERROR_INVALID_NAME (123)*/
393     ok( !res &&
394         ((GetLastError() == ERROR_INVALID_PARAMETER) ||
395         (GetLastError() == ERROR_INVALID_NAME)),
396         "returned %ld with %ld (expected '0' with: ERROR_INVALID_PARAMETER or " \
397         "ERROR_INVALID_NAME)\n", res, GetLastError());
398
399     AddMonitorA(NULL, 2, (LPBYTE) &mi2a);
400     SetLastError(MAGIC_DEAD);
401     res = DeleteMonitorA(NULL, entry->env, "");
402     /* NT: ERROR_INVALID_PARAMETER (87),  9x: ERROR_INVALID_NAME (123)*/
403     ok( !res && 
404         ((GetLastError() == ERROR_INVALID_PARAMETER) ||
405         (GetLastError() == ERROR_INVALID_NAME)),
406         "returned %ld with %ld (expected '0' with: ERROR_INVALID_PARAMETER or " \
407         "ERROR_INVALID_NAME)\n", res, GetLastError());
408
409     AddMonitorA(NULL, 2, (LPBYTE) &mi2a);
410     SetLastError(MAGIC_DEAD);
411     res = DeleteMonitorA("", entry->env, winetest_monitor);
412     ok(res, "returned %ld with %ld (expected '!=0')\n", res, GetLastError());
413
414     /* cleanup */
415     DeleteMonitorA(NULL, entry->env, winetest_monitor);
416 }
417
418
419 /* ########################### */
420
421 static void test_EnumMonitors(void)
422 {
423     DWORD   res;
424     LPBYTE  buffer;
425     DWORD   cbBuf;
426     DWORD   pcbNeeded;
427     DWORD   pcReturned;
428     DWORD   level;
429
430     /* valid levels are 1 and 2 */
431     for(level = 0; level < 4; level++) {
432         cbBuf = MAGIC_DEAD;
433         pcReturned = MAGIC_DEAD;
434         SetLastError(MAGIC_DEAD);
435         res = EnumMonitorsA(NULL, level, NULL, 0, &cbBuf, &pcReturned);
436
437         RETURN_ON_DEACTIVATED_SPOOLER(res)
438
439         /* not implemented yet in wine */
440         if (!res && (GetLastError() == ERROR_CALL_NOT_IMPLEMENTED)) continue;
441
442
443         /* use only a short test, when we test with an invalid level */
444         if(!level || (level > 2)) {
445             ok( (!res && (GetLastError() == ERROR_INVALID_LEVEL)) ||
446                 (res && (pcReturned == 0)),
447                 "(%ld) returned %ld with %ld and 0x%08lx (expected '0' with " \
448                 "ERROR_INVALID_LEVEL or '!=0' and 0x0)\n",
449                 level, res, GetLastError(), pcReturned);
450             continue;
451         }        
452
453         /* Level 2 is not supported on win9x */
454         if (!res && (GetLastError() == ERROR_INVALID_LEVEL)) {
455             trace("Level %ld not supported, skipping tests\n", level);
456             continue;
457         }
458
459         ok((!res) && (GetLastError() == ERROR_INSUFFICIENT_BUFFER),
460             "(%ld) returned %ld with %ld (expected '0' with " \
461             "ERROR_INSUFFICIENT_BUFFER)\n", level, res, GetLastError());
462
463         if (!cbBuf) {
464             trace("no valid buffer size returned, skipping tests\n");
465             continue;
466         }
467
468         buffer = HeapAlloc(GetProcessHeap(), 0, cbBuf *2);
469         if (buffer == NULL) continue;
470
471         SetLastError(MAGIC_DEAD);
472         pcbNeeded = MAGIC_DEAD;
473         res = EnumMonitorsA(NULL, level, buffer, cbBuf, &pcbNeeded, &pcReturned);
474         ok(res, "(%ld) returned %ld with %ld (expected '!=0')\n",
475                 level, res, GetLastError());
476         ok(pcbNeeded == cbBuf, "(%ld) returned %ld (expected %ld)\n",
477                 level, pcbNeeded, cbBuf);
478         /* We can validate the returned Data with the Registry here */
479
480
481         SetLastError(MAGIC_DEAD);
482         pcReturned = MAGIC_DEAD;
483         pcbNeeded = MAGIC_DEAD;
484         res = EnumMonitorsA(NULL, level, buffer, cbBuf+1, &pcbNeeded, &pcReturned);
485         ok(res, "(%ld) returned %ld with %ld (expected '!=0')\n", level,
486                 res, GetLastError());
487         ok(pcbNeeded == cbBuf, "(%ld) returned %ld (expected %ld)\n", level,
488                 pcbNeeded, cbBuf);
489
490         SetLastError(MAGIC_DEAD);
491         pcbNeeded = MAGIC_DEAD;
492         res = EnumMonitorsA(NULL, level, buffer, cbBuf-1, &pcbNeeded, &pcReturned);
493         ok( !res && (GetLastError() == ERROR_INSUFFICIENT_BUFFER),
494             "(%ld) returned %ld with %ld (expected '0' with " \
495             "ERROR_INSUFFICIENT_BUFFER)\n", level, res, GetLastError());
496
497         ok(pcbNeeded == cbBuf, "(%ld) returned %ld (expected %ld)\n", level,
498                 pcbNeeded, cbBuf);
499
500 /*
501       Do not add the next test:
502       w2k+:  RPC_X_NULL_REF_POINTER 
503       NT3.5: ERROR_INVALID_USER_BUFFER
504       win9x: crash in winspool.drv
505
506       res = EnumMonitorsA(NULL, level, NULL, cbBuf, &pcbNeeded, &pcReturned);
507 */
508
509         SetLastError(MAGIC_DEAD);
510         pcbNeeded = MAGIC_DEAD;
511         pcReturned = MAGIC_DEAD;
512         res = EnumMonitorsA(NULL, level, buffer, cbBuf, NULL, &pcReturned);
513         ok( res || (!res && (GetLastError() == RPC_X_NULL_REF_POINTER)) ,
514             "(%ld) returned %ld with %ld (expected '!=0' or '0' with "\
515             "RPC_X_NULL_REF_POINTER)\n", level, res, GetLastError());
516
517         pcbNeeded = MAGIC_DEAD;
518         pcReturned = MAGIC_DEAD;
519         SetLastError(MAGIC_DEAD);
520         res = EnumMonitorsA(NULL, level, buffer, cbBuf, &pcbNeeded, NULL);
521         ok( res || (!res && (GetLastError() == RPC_X_NULL_REF_POINTER)) ,
522             "(%ld) returned %ld with %ld (expected '!=0' or '0' with "\
523             "RPC_X_NULL_REF_POINTER)\n", level, res, GetLastError());
524
525         HeapFree(GetProcessHeap(), 0, buffer);
526     } /* for(level ... */
527 }
528
529
530 static void test_GetDefaultPrinter(void)
531 {
532     BOOL    retval;
533     DWORD   exact = DEFAULT_PRINTER_SIZE;
534     DWORD   size;
535     char    buffer[DEFAULT_PRINTER_SIZE];
536
537     if (!pGetDefaultPrinterA)  return;
538         /* only supported on NT like OSes starting with win2k */
539
540     SetLastError(ERROR_SUCCESS);
541     retval = pGetDefaultPrinterA(buffer, &exact);
542     if (!retval || !exact || !strlen(buffer) ||
543         (ERROR_SUCCESS != GetLastError())) {
544         if ((ERROR_FILE_NOT_FOUND == GetLastError()) ||
545             (ERROR_INVALID_NAME == GetLastError()))
546             trace("this test requires a default printer to be set\n");
547         else {
548                 ok( 0, "function call GetDefaultPrinterA failed unexpected!\n"
549                 "function returned %s\n"
550                 "last error 0x%08lx\n"
551                 "returned buffer size 0x%08lx\n"
552                 "returned buffer content %s\n",
553                 retval ? "true" : "false", GetLastError(), exact, buffer);
554         }
555         return;
556     }
557     SetLastError(ERROR_SUCCESS);
558     retval = pGetDefaultPrinterA(NULL, NULL); 
559     ok( !retval, "function result wrong! False expected\n");
560     ok( ERROR_INVALID_PARAMETER == GetLastError(),
561         "Last error wrong! ERROR_INVALID_PARAMETER expected, got 0x%08lx\n",
562         GetLastError());
563
564     SetLastError(ERROR_SUCCESS);
565     retval = pGetDefaultPrinterA(buffer, NULL); 
566     ok( !retval, "function result wrong! False expected\n");
567     ok( ERROR_INVALID_PARAMETER == GetLastError(),
568         "Last error wrong! ERROR_INVALID_PARAMETER expected, got 0x%08lx\n",
569         GetLastError());
570
571     SetLastError(ERROR_SUCCESS);
572     size = 0;
573     retval = pGetDefaultPrinterA(NULL, &size); 
574     ok( !retval, "function result wrong! False expected\n");
575     ok( ERROR_INSUFFICIENT_BUFFER == GetLastError(),
576         "Last error wrong! ERROR_INSUFFICIENT_BUFFER expected, got 0x%08lx\n",
577         GetLastError());
578     ok( size == exact, "Parameter size wrong! %ld expected got %ld\n",
579         exact, size);
580
581     SetLastError(ERROR_SUCCESS);
582     size = DEFAULT_PRINTER_SIZE;
583     retval = pGetDefaultPrinterA(NULL, &size); 
584     ok( !retval, "function result wrong! False expected\n");
585     ok( ERROR_INSUFFICIENT_BUFFER == GetLastError(),
586         "Last error wrong! ERROR_INSUFFICIENT_BUFFER expected, got 0x%08lx\n",
587         GetLastError());
588     ok( size == exact, "Parameter size wrong! %ld expected got %ld\n",
589         exact, size);
590
591     size = 0;
592     retval = pGetDefaultPrinterA(buffer, &size); 
593     ok( !retval, "function result wrong! False expected\n");
594     ok( ERROR_INSUFFICIENT_BUFFER == GetLastError(),
595         "Last error wrong! ERROR_INSUFFICIENT_BUFFER expected, got 0x%08lx\n",
596         GetLastError());
597     ok( size == exact, "Parameter size wrong! %ld expected got %ld\n",
598         exact, size);
599
600     size = exact;
601     retval = pGetDefaultPrinterA(buffer, &size); 
602     ok( retval, "function result wrong! True expected\n");
603     ok( size == exact, "Parameter size wrong! %ld expected got %ld\n",
604         exact, size);
605 }
606
607 static void test_GetPrinterDriverDirectory(void)
608 {
609     LPBYTE buffer = NULL;
610     DWORD  cbBuf = 0, pcbNeeded = 0;
611     BOOL   res;
612
613     SetLastError(MAGIC_DEAD);
614     res = GetPrinterDriverDirectoryA( NULL, NULL, 1, NULL, 0, &cbBuf);
615     trace("first call returned 0x%04x, with %ld: buffer size 0x%08lx\n",
616     res, GetLastError(), cbBuf);
617
618     RETURN_ON_DEACTIVATED_SPOOLER(res)
619     ok((res == 0) && (GetLastError() == ERROR_INSUFFICIENT_BUFFER),
620         "returned %d with lasterror=%ld (expected '0' with " \
621         "ERROR_INSUFFICIENT_BUFFER)\n", res, GetLastError());
622
623     if (!cbBuf) {
624         trace("no valid buffer size returned, skipping tests\n");
625         return;
626     }
627
628     buffer = HeapAlloc( GetProcessHeap(), 0, cbBuf*2);
629     if (buffer == NULL)  return ;
630
631     res = GetPrinterDriverDirectoryA(NULL, NULL, 1, buffer, cbBuf, &pcbNeeded);
632     ok( res, "expected result != 0, got %d\n", res);
633     ok( cbBuf == pcbNeeded, "pcbNeeded set to %ld instead of %ld\n",
634                             pcbNeeded, cbBuf);
635
636     res = GetPrinterDriverDirectoryA(NULL, NULL, 1, buffer, cbBuf*2, &pcbNeeded);
637     ok( res, "expected result != 0, got %d\n", res);
638     ok( cbBuf == pcbNeeded, "pcbNeeded set to %ld instead of %ld\n",
639                             pcbNeeded, cbBuf);
640  
641     SetLastError(MAGIC_DEAD);
642     res = GetPrinterDriverDirectoryA( NULL, NULL, 1, buffer, cbBuf-1, &pcbNeeded);
643     ok( !res , "expected result == 0, got %d\n", res);
644     ok( cbBuf == pcbNeeded, "pcbNeeded set to %ld instead of %ld\n",
645                             pcbNeeded, cbBuf);
646     
647     ok( ERROR_INSUFFICIENT_BUFFER == GetLastError(),
648         "last error set to %ld instead of ERROR_INSUFFICIENT_BUFFER\n",
649         GetLastError());
650
651 /*
652     Do not add the next test:
653     XPsp2: crash in this app, when the spooler is not running 
654     NT3.5: ERROR_INVALID_USER_BUFFER
655     win9x: ERROR_INVALID_PARAMETER
656
657     pcbNeeded = MAGIC_DEAD;
658     SetLastError(MAGIC_DEAD);
659     res = GetPrinterDriverDirectoryA( NULL, NULL, 1, NULL, cbBuf, &pcbNeeded);
660 */
661
662     SetLastError(MAGIC_DEAD);
663     res = GetPrinterDriverDirectoryA( NULL, NULL, 1, buffer, cbBuf, NULL);
664     ok( (!res && RPC_X_NULL_REF_POINTER == GetLastError()) || res,
665          "expected either result == 0 and "
666          "last error == RPC_X_NULL_REF_POINTER or result != 0 "
667          "got result %d and last error == %ld\n", res, GetLastError());
668
669     SetLastError(MAGIC_DEAD);
670     res = GetPrinterDriverDirectoryA( NULL, NULL, 1, NULL, cbBuf, NULL);
671     ok(res || (GetLastError() == RPC_X_NULL_REF_POINTER),
672         "returned %d with %ld (expected '!=0' or '0' with " \
673         "RPC_X_NULL_REF_POINTER)\n", res, GetLastError());
674  
675  
676     /* with a valid buffer, but level is too large */
677     buffer[0] = '\0';
678     SetLastError(MAGIC_DEAD);
679     res = GetPrinterDriverDirectoryA(NULL, NULL, 2, buffer, cbBuf, &pcbNeeded);
680
681     /* Level not checked in win9x and wine:*/
682     if((res != FALSE) && buffer[0])
683     {
684         trace("Level '2' not checked '%s'\n", buffer);
685     }
686     else
687     {
688         ok( !res && (GetLastError() == ERROR_INVALID_LEVEL),
689         "returned %d with lasterror=%ld (expected '0' with " \
690         "ERROR_INVALID_LEVEL)\n", res, GetLastError());
691     }
692
693     /* printing environments are case insensitive */
694     /* "Windows 4.0" is valid for win9x and NT */
695     buffer[0] = '\0';
696     SetLastError(MAGIC_DEAD);
697     res = GetPrinterDriverDirectoryA(NULL, env_win9x_case, 1, 
698                                         buffer, cbBuf*2, &pcbNeeded);
699
700     if(!res && (GetLastError() == ERROR_INSUFFICIENT_BUFFER)) {
701         cbBuf = pcbNeeded;
702         buffer = HeapReAlloc(GetProcessHeap(), 0, buffer, cbBuf*2);
703         if (buffer == NULL)  return ;
704
705         SetLastError(MAGIC_DEAD);
706         res = GetPrinterDriverDirectoryA(NULL, env_win9x_case, 1, 
707                                         buffer, cbBuf*2, &pcbNeeded);
708     }
709
710     ok(res && buffer[0], "returned %d with " \
711         "lasterror=%ld and len=%d (expected '1' with 'len > 0')\n", 
712         res, GetLastError(), lstrlenA((char *)buffer));
713
714     buffer[0] = '\0';
715     SetLastError(MAGIC_DEAD);
716     res = GetPrinterDriverDirectoryA(NULL, env_x86, 1, 
717                                         buffer, cbBuf*2, &pcbNeeded);
718
719     if(!res && (GetLastError() == ERROR_INSUFFICIENT_BUFFER)) {
720         cbBuf = pcbNeeded;
721         buffer = HeapReAlloc(GetProcessHeap(), 0, buffer, cbBuf*2);
722         if (buffer == NULL)  return ;
723
724         buffer[0] = '\0';
725         SetLastError(MAGIC_DEAD);
726         res = GetPrinterDriverDirectoryA(NULL, env_x86, 1, 
727                                         buffer, cbBuf*2, &pcbNeeded);
728     }
729
730     /* "Windows NT x86" is invalid for win9x */
731     ok( (res && buffer[0]) ||
732         (!res && (GetLastError() == ERROR_INVALID_ENVIRONMENT)), 
733         "returned %d with lasterror=%ld and len=%d (expected '!= 0' with " \
734         "'len > 0' or '0' with ERROR_INVALID_ENVIRONMENT)\n",
735         res, GetLastError(), lstrlenA((char *)buffer));
736
737     /* A Setup-Programm (PDFCreator_0.8.0) use empty strings */
738     SetLastError(MAGIC_DEAD);
739     res = GetPrinterDriverDirectoryA("", "", 1, buffer, cbBuf*2, &pcbNeeded);
740     ok(res, "returned %d with %ld (expected '!=0')\n", res, GetLastError() );
741
742     SetLastError(MAGIC_DEAD);
743     res = GetPrinterDriverDirectoryA(NULL, "", 1, buffer, cbBuf*2, &pcbNeeded);
744     ok(res, "returned %d with %ld (expected '!=0')\n", res, GetLastError() );
745
746     SetLastError(MAGIC_DEAD);
747     res = GetPrinterDriverDirectoryA("", NULL, 1, buffer, cbBuf*2, &pcbNeeded);
748     ok(res, "returned %d with %ld (expected '!=0')\n", res, GetLastError() );
749
750     HeapFree( GetProcessHeap(), 0, buffer);
751 }
752
753 static void test_OpenPrinter(void)
754 {
755     PRINTER_DEFAULTSA defaults;
756     HANDLE  hprinter;
757     LPSTR   default_printer;
758     DWORD   res;
759     DWORD   size;
760     CHAR    buffer[DEFAULT_PRINTER_SIZE];
761     LPSTR   ptr;
762
763     SetLastError(MAGIC_DEAD);
764     res = OpenPrinter(NULL, NULL, NULL);    
765     /* The deactivated Spooler is catched here on NT3.51 */
766     RETURN_ON_DEACTIVATED_SPOOLER(res)
767     ok(!res && (GetLastError() == ERROR_INVALID_PARAMETER),
768         "returned %ld with %ld (expected '0' with ERROR_INVALID_PARAMETER)\n",
769         res, GetLastError());
770
771
772     /* Get Handle for the local Printserver (NT only)*/
773     hprinter = (HANDLE) MAGIC_DEAD;
774     SetLastError(MAGIC_DEAD);
775     res = OpenPrinter(NULL, &hprinter, NULL);
776     /* The deactivated Spooler is catched here on XPsp2 */
777     RETURN_ON_DEACTIVATED_SPOOLER(res)
778     ok(res || (!res && GetLastError() == ERROR_INVALID_PARAMETER),
779         "returned %ld with %ld (expected '!=0' or '0' with ERROR_INVALID_PARAMETER)\n",
780         res, GetLastError());
781     if(res) {
782         ClosePrinter(hprinter);
783
784         defaults.pDatatype=NULL;
785         defaults.pDevMode=NULL;
786
787         defaults.DesiredAccess=0;
788         hprinter = (HANDLE) MAGIC_DEAD;
789         SetLastError(MAGIC_DEAD);
790         res = OpenPrinter(NULL, &hprinter, &defaults);
791         ok(res, "returned %ld with %ld (expected '!=0')\n", res, GetLastError());
792         if (res) ClosePrinter(hprinter);
793
794         defaults.DesiredAccess=-1;
795         hprinter = (HANDLE) MAGIC_DEAD;
796         SetLastError(MAGIC_DEAD);
797         res = OpenPrinter(NULL, &hprinter, &defaults);
798         todo_wine {
799         ok(!res && GetLastError() == ERROR_ACCESS_DENIED,
800             "returned %ld with %ld (expected '0' with ERROR_ACCESS_DENIED)\n", 
801             res, GetLastError());
802         }
803         if (res) ClosePrinter(hprinter);
804
805     }
806
807     size = sizeof(buffer) - 3 ;
808     ptr = buffer;
809     ptr[0] = '\\';
810     ptr++;
811     ptr[0] = '\\';
812     ptr++;
813     if (GetComputerNameA(ptr, &size)) {
814
815         hprinter = (HANDLE) MAGIC_DEAD;
816         SetLastError(MAGIC_DEAD);
817         res = OpenPrinter(buffer, &hprinter, NULL);
818         todo_wine {
819         ok(res || (!res && GetLastError() == ERROR_INVALID_PARAMETER),
820             "returned %ld with %ld (expected '!=0' or '0' with ERROR_INVALID_PARAMETER)\n",
821             res, GetLastError());
822         }
823         if(res) ClosePrinter(hprinter);
824     }
825
826     /* Invalid Printername */
827     hprinter = (HANDLE) MAGIC_DEAD;
828     SetLastError(MAGIC_DEAD);
829     res = OpenPrinter("illegal,name", &hprinter, NULL);
830     ok(!res && ((GetLastError() == ERROR_INVALID_PRINTER_NAME) || 
831                 (GetLastError() == ERROR_INVALID_PARAMETER) ),
832        "returned %ld with %ld (expected '0' with: ERROR_INVALID_PARAMETER or" \
833        "ERROR_INVALID_PRINTER_NAME)\n", res, GetLastError());
834     if(res) ClosePrinter(hprinter);
835
836     hprinter = (HANDLE) MAGIC_DEAD;
837     SetLastError(MAGIC_DEAD);
838     res = OpenPrinter("", &hprinter, NULL);
839     /* NT: ERROR_INVALID_PRINTER_NAME,  9x: ERROR_INVALID_PARAMETER */
840     ok( !res &&
841         ((GetLastError() == ERROR_INVALID_PRINTER_NAME) || 
842         (GetLastError() == ERROR_INVALID_PARAMETER) ),
843         "returned %ld with %ld (expected '0' with: ERROR_INVALID_PRINTER_NAME" \
844         " or ERROR_INVALID_PARAMETER)\n", res, GetLastError());
845     if(res) ClosePrinter(hprinter);
846
847
848     /* Get Handle for the default Printer */
849     if ((default_printer = find_default_printer()))
850     {
851         hprinter = (HANDLE) MAGIC_DEAD;
852         SetLastError(MAGIC_DEAD);
853         res = OpenPrinter(default_printer, &hprinter, NULL);
854         if((!res) && (GetLastError() == RPC_S_SERVER_UNAVAILABLE))
855         {
856             trace("The Service 'Spooler' is required for '%s'\n", default_printer);
857             return;
858         }
859         ok(res, "returned %ld with %ld (expected '!=0')\n", res, GetLastError());
860         if(res) ClosePrinter(hprinter);
861
862         SetLastError(MAGIC_DEAD);
863         res = OpenPrinter(default_printer, NULL, NULL);
864         /* NT: FALSE with ERROR_INVALID_PARAMETER, 9x: TRUE */
865         ok(res || (GetLastError() == ERROR_INVALID_PARAMETER),
866             "returned %ld with %ld (expected '!=0' or '0' with " \
867             "ERROR_INVALID_PARAMETER)\n", res, GetLastError());
868
869         defaults.pDatatype=NULL;
870         defaults.pDevMode=NULL;
871         defaults.DesiredAccess=0;
872
873         hprinter = (HANDLE) MAGIC_DEAD;
874         SetLastError(MAGIC_DEAD);
875         res = OpenPrinter(default_printer, &hprinter, &defaults);
876         ok(res || GetLastError() == ERROR_ACCESS_DENIED,
877             "returned %ld with %ld (expected '!=0' or '0' with " \
878             "ERROR_ACCESS_DENIED)\n", res, GetLastError());
879         if(res) ClosePrinter(hprinter);
880
881         defaults.pDatatype="";
882
883         hprinter = (HANDLE) MAGIC_DEAD;
884         SetLastError(MAGIC_DEAD);
885         res = OpenPrinter(default_printer, &hprinter, &defaults);
886         /* stop here, when a remote Printserver has no RPC-Service running */
887         RETURN_ON_DEACTIVATED_SPOOLER(res)
888         ok(res || ((GetLastError() == ERROR_INVALID_DATATYPE) ||
889                    (GetLastError() == ERROR_ACCESS_DENIED)),
890             "returned %ld with %ld (expected '!=0' or '0' with: " \
891             "ERROR_INVALID_DATATYPE or ERROR_ACCESS_DENIED)\n",
892             res, GetLastError());
893         if(res) ClosePrinter(hprinter);
894
895
896         defaults.pDatatype=NULL;
897         defaults.DesiredAccess=PRINTER_ACCESS_USE;
898
899         hprinter = (HANDLE) MAGIC_DEAD;
900         SetLastError(MAGIC_DEAD);
901         res = OpenPrinter(default_printer, &hprinter, &defaults);
902         ok(res || GetLastError() == ERROR_ACCESS_DENIED,
903             "returned %ld with %ld (expected '!=0' or '0' with " \
904             "ERROR_ACCESS_DENIED)\n", res, GetLastError());
905         if(res) ClosePrinter(hprinter);
906
907
908         defaults.DesiredAccess=PRINTER_ALL_ACCESS;
909         hprinter = (HANDLE) MAGIC_DEAD;
910         SetLastError(MAGIC_DEAD);
911         res = OpenPrinter(default_printer, &hprinter, &defaults);
912         ok(res || GetLastError() == ERROR_ACCESS_DENIED,
913             "returned %ld with %ld (expected '!=0' or '0' with " \
914             "ERROR_ACCESS_DENIED)\n", res, GetLastError());
915         if(res) ClosePrinter(hprinter);
916     }
917
918 }
919
920
921 static void test_SetDefaultPrinter(void)
922 {
923     DWORD   res;
924     LPSTR   default_printer;
925     DWORD   size = DEFAULT_PRINTER_SIZE;
926     CHAR    buffer[DEFAULT_PRINTER_SIZE];
927     CHAR    org_value[DEFAULT_PRINTER_SIZE];
928
929
930     if (!pSetDefaultPrinterA)  return;
931         /* only supported on win2k and above */
932
933     default_printer = find_default_printer();
934
935     /* backup the original value */
936     org_value[0] = '\0';
937     SetLastError(MAGIC_DEAD);
938     res = GetProfileStringA("windows", "device", NULL, org_value, size);
939
940     /* first part: with the default Printer */
941     SetLastError(MAGIC_DEAD);
942     res = pSetDefaultPrinterA("no_printer_with_this_name");
943
944     RETURN_ON_DEACTIVATED_SPOOLER(res)
945     /* spooler is running or we have no spooler here*/
946
947     /* Not implemented in wine */
948     if (!res && (GetLastError() == ERROR_CALL_NOT_IMPLEMENTED)) {
949         trace("SetDefaultPrinterA() not implemented yet.\n");
950         return;
951     }
952
953     ok(!res && (GetLastError() == ERROR_INVALID_PRINTER_NAME),
954         "returned %ld with %ld (expected '0' with " \
955         "ERROR_INVALID_PRINTER_NAME)\n", res, GetLastError());
956
957     WriteProfileStringA("windows", "device", org_value);
958     SetLastError(MAGIC_DEAD);
959     res = pSetDefaultPrinterA("");
960     ok(res || (!res && (GetLastError() == ERROR_INVALID_PRINTER_NAME)),
961         "returned %ld with %ld (expected '!=0' or '0' with " \
962         "ERROR_INVALID_PRINTER_NAME)\n", res, GetLastError());
963
964     WriteProfileStringA("windows", "device", org_value);
965     SetLastError(MAGIC_DEAD);
966     res = pSetDefaultPrinterA(NULL);
967     ok(res || (!res && (GetLastError() == ERROR_INVALID_PRINTER_NAME)),
968         "returned %ld with %ld (expected '!=0' or '0' with " \
969         "ERROR_INVALID_PRINTER_NAME)\n", res, GetLastError());
970
971     WriteProfileStringA("windows", "device", org_value);
972     SetLastError(MAGIC_DEAD);
973     res = pSetDefaultPrinterA(default_printer);
974     ok(res || (!res && (GetLastError() == ERROR_INVALID_PRINTER_NAME)),
975         "returned %ld with %ld (expected '!=0' or '0' with " \
976         "ERROR_INVALID_PRINTER_NAME)\n", res, GetLastError());
977
978
979     /* second part: always without a default Printer */
980     WriteProfileStringA("windows", "device", NULL);    
981     SetLastError(MAGIC_DEAD);
982     res = pSetDefaultPrinterA("no_printer_with_this_name");
983
984     ok(!res && (GetLastError() == ERROR_INVALID_PRINTER_NAME),
985         "returned %ld with %ld (expected '0' with " \
986         "ERROR_INVALID_PRINTER_NAME)\n", res, GetLastError());
987
988     WriteProfileStringA("windows", "device", NULL);    
989     SetLastError(MAGIC_DEAD);
990     res = pSetDefaultPrinterA("");
991     /* we get ERROR_INVALID_PRINTER_NAME when no printer is installed */
992     ok(res || (!res && (GetLastError() == ERROR_INVALID_PRINTER_NAME)),
993          "returned %ld with %ld (expected '!=0' or '0' with " \
994          "ERROR_INVALID_PRINTER_NAME)\n", res, GetLastError());
995
996     WriteProfileStringA("windows", "device", NULL);    
997     SetLastError(MAGIC_DEAD);
998     res = pSetDefaultPrinterA(NULL);
999     /* we get ERROR_INVALID_PRINTER_NAME when no printer is installed */
1000     ok(res || (!res && (GetLastError() == ERROR_INVALID_PRINTER_NAME)),
1001         "returned %ld with %ld (expected '!=0' or '0' with " \
1002         "ERROR_INVALID_PRINTER_NAME)\n", res, GetLastError());
1003
1004     WriteProfileStringA("windows", "device", NULL);    
1005     SetLastError(MAGIC_DEAD);
1006     res = pSetDefaultPrinterA(default_printer);
1007     ok(res || (!res && (GetLastError() == ERROR_INVALID_PRINTER_NAME)),
1008         "returned %ld with %ld (expected '!=0' or '0' with " \
1009         "ERROR_INVALID_PRINTER_NAME)\n", res, GetLastError());
1010
1011     /* restore the original value */
1012     res = pSetDefaultPrinterA(default_printer);          /* the nice way */
1013     WriteProfileStringA("windows", "device", org_value); /* the old way */
1014
1015     buffer[0] = '\0';
1016     SetLastError(MAGIC_DEAD);
1017     res = GetProfileStringA("windows", "device", NULL, buffer, size);
1018     ok(!lstrcmpA(org_value, buffer), "'%s' (expected '%s')\n", buffer, org_value);
1019
1020 }
1021
1022 static void test_GetPrinterDriver(void)
1023 {
1024     LPSTR default_printer;
1025     HANDLE hprn;
1026     BOOL ret;
1027     BYTE *buf;
1028     INT level;
1029     DWORD needed, filled;
1030
1031     default_printer = find_default_printer();
1032     if (!default_printer)
1033     {
1034         trace("There is no default printer installed, skiping the test\n");
1035         return;
1036     }
1037
1038     hprn = 0;
1039     ret = OpenPrinter(default_printer, &hprn, NULL);
1040     if (!ret)
1041     {
1042         trace("There is no printers installed, skiping the test\n");
1043         return;
1044     }
1045     ok(hprn != 0, "wrong hprn %p\n", hprn);
1046
1047     for (level = -1; level <= 7; level++)
1048     {
1049         SetLastError(0xdeadbeef);
1050         needed = (DWORD)-1;
1051         ret = GetPrinterDriver(hprn, NULL, level, NULL, 0, &needed);
1052         ok(!ret, "level %d: GetPrinterDriver should fail\n", level);
1053         if (level >= 1 && level <= 6)
1054         {
1055             /* Not all levels are supported on all Windows-Versions */
1056             if(GetLastError() == ERROR_INVALID_LEVEL) continue;
1057             ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER, "wrong error %ld\n", GetLastError());
1058             ok(needed > 0,"not expected needed buffer size %ld\n", needed);
1059         }
1060         else
1061         {
1062             /* ERROR_OUTOFMEMORY found on win9x */
1063             ok( ((GetLastError() == ERROR_INVALID_LEVEL) ||
1064                  (GetLastError() == ERROR_OUTOFMEMORY)),
1065                 "%d: returned %d with %ld (expected '0' with: " \
1066                 "ERROR_INVALID_LEVEL or ERROR_OUTOFMEMORY)\n",
1067                 level, ret, GetLastError());
1068             /* needed is modified in win9x. The modified Value depends on the
1069                default Printer. testing for "needed == (DWORD)-1" will fail */
1070             continue;
1071         }
1072
1073         buf = HeapAlloc(GetProcessHeap(), 0, needed);
1074
1075         SetLastError(0xdeadbeef);
1076         filled = -1;
1077         ret = GetPrinterDriver(hprn, NULL, level, buf, needed, &filled);
1078         ok(ret, "level %d: GetPrinterDriver error %ld\n", level, GetLastError());
1079         ok(needed == filled, "needed %ld != filled %ld\n", needed, filled);
1080
1081         if (level == 2)
1082         {
1083             DRIVER_INFO_2 *di_2 = (DRIVER_INFO_2 *)buf;
1084             DWORD calculated = sizeof(*di_2);
1085
1086             /* MSDN is wrong: The Drivers on the win9x-CD's have cVersion=0x0400
1087                NT351: 1, NT4.0+w2k(Kernelmode): 2, w2k and above(Usermode): 3  */
1088             ok((di_2->cVersion >= 0 && di_2->cVersion <= 3) ||
1089                 (di_2->cVersion == 0x0400), "di_2->cVersion = %ld\n", di_2->cVersion);
1090             ok(di_2->pName != NULL, "not expected NULL ptr\n");
1091             ok(di_2->pEnvironment != NULL, "not expected NULL ptr\n");
1092             ok(di_2->pDriverPath != NULL, "not expected NULL ptr\n");
1093             ok(di_2->pDataFile != NULL, "not expected NULL ptr\n");
1094             ok(di_2->pConfigFile != NULL, "not expected NULL ptr\n");
1095
1096             trace("cVersion %ld\n", di_2->cVersion);
1097             trace("pName %s\n", di_2->pName);
1098             calculated += strlen(di_2->pName) + 1;
1099             trace("pEnvironment %s\n", di_2->pEnvironment);
1100             calculated += strlen(di_2->pEnvironment) + 1;
1101             trace("pDriverPath %s\n", di_2->pDriverPath);
1102             calculated += strlen(di_2->pDriverPath) + 1;
1103             trace("pDataFile %s\n", di_2->pDataFile);
1104             calculated += strlen(di_2->pDataFile) + 1;
1105             trace("pConfigFile %s\n", di_2->pConfigFile);
1106             calculated += strlen(di_2->pConfigFile) + 1;
1107
1108             /* XP allocates memory for both ANSI and unicode names */
1109             ok(filled >= calculated,"calculated %ld != filled %ld\n", calculated, filled);
1110         }
1111
1112         HeapFree(GetProcessHeap(), 0, buf);
1113     }
1114
1115     SetLastError(0xdeadbeef);
1116     ret = ClosePrinter(hprn);
1117     ok(ret, "ClosePrinter error %ld\n", GetLastError());
1118 }
1119
1120 static void test_DEVMODE(const DEVMODE *dm, LONG dmSize, LPCSTR exp_prn_name)
1121 {
1122     /* On NT3.51, some fields in DEVMODE are empty/zero
1123       (dmDeviceName, dmSpecVersion, dmDriverVersion and dmDriverExtra)
1124        We skip the Tests on this Platform */
1125     if (dm->dmSpecVersion || dm->dmDriverVersion || dm->dmDriverExtra) {
1126     /* The 0-terminated Printername can be larger (MAX_PATH) than CCHDEVICENAME */
1127         ok(!strncmp(exp_prn_name, (LPCSTR)dm->dmDeviceName, CCHDEVICENAME -1),
1128             "expected '%s', got '%s'\n", exp_prn_name, dm->dmDeviceName);
1129         ok(dm->dmSize + dm->dmDriverExtra == dmSize,
1130             "%u != %ld\n", dm->dmSize + dm->dmDriverExtra, dmSize);
1131     }
1132     trace("dmFields %08lx\n", dm->dmFields);
1133 }
1134
1135 static void test_DocumentProperties(void)
1136 {
1137     LPSTR default_printer;
1138     HANDLE hprn;
1139     LONG dm_size, ret;
1140     DEVMODE *dm;
1141
1142     default_printer = find_default_printer();
1143     if (!default_printer)
1144     {
1145         trace("There is no default printer installed, skiping the test\n");
1146         return;
1147     }
1148
1149     hprn = 0;
1150     ret = OpenPrinter(default_printer, &hprn, NULL);
1151     if (!ret)
1152     {
1153         trace("There is no printers installed, skiping the test\n");
1154         return;
1155     }
1156     ok(hprn != 0, "wrong hprn %p\n", hprn);
1157
1158     dm_size = DocumentProperties(0, hprn, NULL, NULL, NULL, 0);
1159     trace("DEVMODE required size %ld\n", dm_size);
1160     ok(dm_size >= sizeof(DEVMODE), "unexpected DocumentProperties ret value %ld\n", dm_size);
1161
1162     dm = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, dm_size);
1163
1164     ret = DocumentProperties(0, hprn, NULL, dm, dm, DM_OUT_BUFFER);
1165     ok(ret == IDOK, "DocumentProperties ret value %ld != expected IDOK\n", ret);
1166
1167     test_DEVMODE(dm, dm_size, default_printer);
1168
1169     HeapFree(GetProcessHeap(), 0, dm);
1170
1171     SetLastError(0xdeadbeef);
1172     ret = ClosePrinter(hprn);
1173     ok(ret, "ClosePrinter error %ld\n", GetLastError());
1174 }
1175
1176 START_TEST(info)
1177 {
1178     hwinspool = GetModuleHandleA("winspool.drv");
1179     pGetDefaultPrinterA = (void *) GetProcAddress(hwinspool, "GetDefaultPrinterA");
1180     pSetDefaultPrinterA = (void *) GetProcAddress(hwinspool, "SetDefaultPrinterA");
1181
1182     find_default_printer();
1183
1184     test_AddMonitor();
1185     test_DeleteMonitor();
1186     test_DocumentProperties();
1187     test_EnumMonitors(); 
1188     test_GetDefaultPrinter();
1189     test_GetPrinterDriverDirectory();
1190     test_OpenPrinter();
1191     test_GetPrinterDriver();
1192     test_SetDefaultPrinter();
1193 }