Some extra tests for GetPrinterDriverDirectory.
[wine] / dlls / winspool / tests / info.c
1 /*
2  * Copyright (C) 2003, 2004 Stefan Leichter
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
17  */
18
19 #include <stdarg.h>
20
21 #include "wine/test.h"
22 #include "windef.h"
23 #include "winbase.h"
24 #include "winerror.h"
25 #include "wingdi.h"
26 #include "winspool.h"
27
28
29 static char env_x86[] = "Windows NT x86";
30 static char env_win9x_case[] = "windowS 4.0";
31
32
33 static void test_default_printer(void)
34 {
35 #define DEFAULT_PRINTER_SIZE 1000
36     BOOL    retval;
37     DWORD   exact = DEFAULT_PRINTER_SIZE;
38     DWORD   size;
39     FARPROC func = NULL;
40     HMODULE lib = NULL;
41     char    buffer[DEFAULT_PRINTER_SIZE];
42
43     lib = GetModuleHandleA("winspool.drv");
44     if (!lib) {
45         ok( 0, "GetModuleHandleA(\"winspool.drv\") failed\n");
46         return;
47     }
48
49     func = GetProcAddress( lib, "GetDefaultPrinterA");
50     if (!func)
51         /* only supported on NT like OSes starting with win2k */
52         return;
53
54     SetLastError(ERROR_SUCCESS);
55     retval = func( buffer, &exact);
56     if (!retval || !exact || !strlen(buffer) ||
57         (ERROR_SUCCESS != GetLastError())) {
58         if ((ERROR_FILE_NOT_FOUND == GetLastError()) ||
59             (ERROR_INVALID_NAME == GetLastError()))
60             trace("this test requires a default printer to be set\n");
61         else {
62                 ok( 0, "function call GetDefaultPrinterA failed unexpected!\n"
63                 "function returned %s\n"
64                 "last error 0x%08lx\n"
65                 "returned buffer size 0x%08lx\n"
66                 "returned buffer content %s\n",
67                 retval ? "true" : "false", GetLastError(), exact, buffer);
68         }
69         return;
70     }
71     SetLastError(ERROR_SUCCESS);
72     retval = func( NULL, NULL); 
73     ok( !retval, "function result wrong! False expected\n");
74     ok( ERROR_INVALID_PARAMETER == GetLastError(),
75         "Last error wrong! ERROR_INVALID_PARAMETER expected, got 0x%08lx\n",
76         GetLastError());
77
78     SetLastError(ERROR_SUCCESS);
79     retval = func( buffer, NULL); 
80     ok( !retval, "function result wrong! False expected\n");
81     ok( ERROR_INVALID_PARAMETER == GetLastError(),
82         "Last error wrong! ERROR_INVALID_PARAMETER expected, got 0x%08lx\n",
83         GetLastError());
84
85     SetLastError(ERROR_SUCCESS);
86     size = 0;
87     retval = func( NULL, &size); 
88     ok( !retval, "function result wrong! False expected\n");
89     ok( ERROR_INSUFFICIENT_BUFFER == GetLastError(),
90         "Last error wrong! ERROR_INSUFFICIENT_BUFFER expected, got 0x%08lx\n",
91         GetLastError());
92     ok( size == exact, "Parameter size wrong! %ld expected got %ld\n",
93         exact, size);
94
95     SetLastError(ERROR_SUCCESS);
96     size = DEFAULT_PRINTER_SIZE;
97     retval = func( NULL, &size); 
98     ok( !retval, "function result wrong! False expected\n");
99     ok( ERROR_INSUFFICIENT_BUFFER == GetLastError(),
100         "Last error wrong! ERROR_INSUFFICIENT_BUFFER expected, got 0x%08lx\n",
101         GetLastError());
102     ok( size == exact, "Parameter size wrong! %ld expected got %ld\n",
103         exact, size);
104
105     size = 0;
106     retval = func( buffer, &size); 
107     ok( !retval, "function result wrong! False expected\n");
108     ok( ERROR_INSUFFICIENT_BUFFER == GetLastError(),
109         "Last error wrong! ERROR_INSUFFICIENT_BUFFER expected, got 0x%08lx\n",
110         GetLastError());
111     ok( size == exact, "Parameter size wrong! %ld expected got %ld\n",
112         exact, size);
113
114     size = exact;
115     retval = func( buffer, &size); 
116     ok( retval, "function result wrong! True expected\n");
117     ok( size == exact, "Parameter size wrong! %ld expected got %ld\n",
118         exact, size);
119 }
120
121 static void test_printer_directory(void)
122 {   LPBYTE buffer = NULL;
123     DWORD  cbBuf = 0, pcbNeeded = 0;
124     BOOL   res;
125
126     SetLastError(0x00dead00);
127     res = GetPrinterDriverDirectoryA( NULL, NULL, 1, NULL, 0, &cbBuf);
128     trace("GetPrinterDriverDirectoryA: first call returned 0x%04x, "
129           "buffer size 0x%08lx\n", res, cbBuf);
130
131     if((res == 0) && (GetLastError() == RPC_S_SERVER_UNAVAILABLE))
132     {
133         trace("The Service 'Spooler' is required for this test\n");
134         return;
135     }
136     ok((res == 0) && (GetLastError() == ERROR_INSUFFICIENT_BUFFER),
137         "returned %d with lasterror=%ld (expected '0' with " \
138         "ERROR_INSUFFICIENT_BUFFER)\n", res, GetLastError());
139
140     if (!cbBuf) {
141         trace("no valid buffer size returned, skipping tests\n");
142         return;
143     }
144
145     buffer = HeapAlloc( GetProcessHeap(), 0, cbBuf*2);
146     if (buffer == NULL)  return ;
147
148     res = GetPrinterDriverDirectoryA(NULL, NULL, 1, buffer, cbBuf, &pcbNeeded);
149     ok( res, "expected result != 0, got %d\n", res);
150     ok( cbBuf == pcbNeeded, "pcbNeeded set to %ld instead of %ld\n",
151                             pcbNeeded, cbBuf);
152
153     res = GetPrinterDriverDirectoryA(NULL, NULL, 1, buffer, cbBuf*2, &pcbNeeded);
154     ok( res, "expected result != 0, got %d\n", res);
155     ok( cbBuf == pcbNeeded, "pcbNeeded set to %ld instead of %ld\n",
156                             pcbNeeded, cbBuf);
157  
158     SetLastError(0x00dead00);
159     res = GetPrinterDriverDirectoryA( NULL, NULL, 1, buffer, cbBuf-1, &pcbNeeded);
160     ok( !res , "expected result == 0, got %d\n", res);
161     ok( cbBuf == pcbNeeded, "pcbNeeded set to %ld instead of %ld\n",
162                             pcbNeeded, cbBuf);
163     todo_wine {
164     ok( ERROR_INSUFFICIENT_BUFFER == GetLastError(),
165         "last error set to %ld instead of ERROR_INSUFFICIENT_BUFFER\n",
166         GetLastError());
167     }
168  
169     SetLastError(0x00dead00);
170     res = GetPrinterDriverDirectoryA( NULL, NULL, 1, NULL, cbBuf, &pcbNeeded);
171     ok( (!res && ERROR_INVALID_USER_BUFFER == GetLastError()) || 
172         ( res && ERROR_INVALID_PARAMETER == GetLastError()) ,
173          "expected either result == 0 and "
174          "last error == ERROR_INVALID_USER_BUFFER "
175          "or result != 0 and last error == ERROR_INVALID_PARAMETER "
176          "got result %d and last error == %ld\n", res, GetLastError());
177
178     SetLastError(0x00dead00);
179     res = GetPrinterDriverDirectoryA( NULL, NULL, 1, buffer, cbBuf, NULL);
180     ok( (!res && RPC_X_NULL_REF_POINTER == GetLastError()) || res,
181          "expected either result == 0 and "
182          "last error == RPC_X_NULL_REF_POINTER or result != 0 "
183          "got result %d and last error == %ld\n", res, GetLastError());
184
185     SetLastError(0x00dead00);
186     res = GetPrinterDriverDirectoryA( NULL, NULL, 1, NULL, cbBuf, NULL);
187     ok( (!res && RPC_X_NULL_REF_POINTER == GetLastError()) || 
188         ( res && ERROR_INVALID_PARAMETER == GetLastError()) ,
189          "expected either result == 0 and "
190          "last error == RPC_X_NULL_REF_POINTER "
191          "or result != 0 and last error == ERROR_INVALID_PARAMETER "
192          "got result %d and last error == %ld\n", res, GetLastError());
193
194     /* with a valid buffer, but level is to large */
195     buffer[0] = '\0';
196     SetLastError(0x00dead00);
197     res = GetPrinterDriverDirectoryA(NULL, NULL, 2, buffer, cbBuf, &pcbNeeded);
198
199     /* Level not checked in win9x and wine:*/
200     if((res != FALSE) && buffer[0])
201     {
202         trace("invalid Level '2' not checked (valid Level is '1') => '%s'\n", 
203                 buffer);
204     }
205     else
206     {
207         ok( !res && (GetLastError() == ERROR_INVALID_LEVEL),
208         "returned %d with lasterror=%ld (expected '0' with " \
209         "ERROR_INVALID_LEVEL)\n", res, GetLastError());
210     }
211
212     /* printing environments are case insensitive */
213     /* "Windows 4.0" is valid for win9x and NT */
214     buffer[0] = '\0';
215     SetLastError(0x00dead00);
216     res = GetPrinterDriverDirectoryA(NULL, env_win9x_case, 1, 
217                                         buffer, cbBuf*2, &pcbNeeded);
218
219     if(!res && (GetLastError() == ERROR_INSUFFICIENT_BUFFER)) {
220         cbBuf = pcbNeeded;
221         buffer = HeapReAlloc(GetProcessHeap(), 0, buffer, cbBuf*2);
222         if (buffer == NULL)  return ;
223
224         SetLastError(0x00dead00);
225         res = GetPrinterDriverDirectoryA(NULL, env_win9x_case, 1, 
226                                         buffer, cbBuf*2, &pcbNeeded);
227     }
228     
229     todo_wine{
230     ok(res && buffer[0], "returned %d with " \
231         "lasterror=%ld and len=%d (expected '0' with 'len > 0')\n", 
232         res, GetLastError(), lstrlenA((char *)buffer));
233     }
234
235     buffer[0] = '\0';
236     SetLastError(0x00dead00);
237     res = GetPrinterDriverDirectoryA(NULL, env_x86, 1, 
238                                         buffer, cbBuf*2, &pcbNeeded);
239
240     if(!res && (GetLastError() == ERROR_INSUFFICIENT_BUFFER)) {
241         cbBuf = pcbNeeded;
242         buffer = HeapReAlloc(GetProcessHeap(), 0, buffer, cbBuf*2);
243         if (buffer == NULL)  return ;
244
245         buffer[0] = '\0';
246         SetLastError(0x00dead00);
247         res = GetPrinterDriverDirectoryA(NULL, env_x86, 1, 
248                                         buffer, cbBuf*2, &pcbNeeded);
249     }
250
251     /* "Windows NT x86" is invalid for win9x */
252     ok( (res && buffer[0]) ||
253         (!res && (GetLastError() == ERROR_INVALID_ENVIRONMENT)), 
254         "returned %d with lasterror=%ld and len=%d (expected '!= 0' with " \
255         "'len > 0' or '0' with ERROR_INVALID_ENVIRONMENT)\n",
256         res, GetLastError(), lstrlenA((char *)buffer));
257
258     HeapFree( GetProcessHeap(), 0, buffer);
259 }
260
261 START_TEST(info)
262 {
263     test_default_printer();
264     test_printer_directory();
265 }