[SPOOLSS]
[reactos.git] / rostests / apitests / winspool / EnumPrinters.c
1 /*
2 * PROJECT: ReactOS Print Spooler DLL API Tests
3 * LICENSE: GNU GPLv2 or any later version as published by the Free Software Foundation
4 * PURPOSE: Tests for EnumPrintersA/EnumPrintersW
5 * COPYRIGHT: Copyright 2015 Colin Finck <colin@reactos.org>
6 */
7
8 #include <apitest.h>
9
10 #define WIN32_NO_STATUS
11 #include <windef.h>
12 #include <winbase.h>
13 #include <wingdi.h>
14 #include <winspool.h>
15
16 START_TEST(EnumPrinters)
17 {
18 DWORD cbNeeded;
19 DWORD cbTemp;
20 DWORD dwReturned;
21 PVOID pMem;
22 DWORD i;
23 DWORD dwValidLevels[] = { 0, 1, 2, 4, 5 };
24
25 // Level 5 is the highest supported under Windows Server 2003. Higher levels need to fail and leave the variables untouched!
26 cbNeeded = 0xDEADBEEF;
27 dwReturned = 0xDEADBEEF;
28 SetLastError(0xDEADBEEF);
29 ok(!EnumPrintersW(0, NULL, 6, NULL, 0, &cbNeeded, &dwReturned), "EnumPrintersW returns TRUE!\n");
30 ok(GetLastError() == ERROR_INVALID_LEVEL, "EnumPrintersW returns error %lu!\n", GetLastError());
31 ok(cbNeeded == 0xDEADBEEF, "cbNeeded is %lu!\n", cbNeeded);
32 ok(dwReturned == 0xDEADBEEF, "dwReturned is %lu!\n", dwReturned);
33
34 // Same goes for level 3.
35 cbNeeded = 0xDEADBEEF;
36 dwReturned = 0xDEADBEEF;
37 SetLastError(0xDEADBEEF);
38 ok(!EnumPrintersW(0, NULL, 3, NULL, 0, &cbNeeded, &dwReturned), "EnumPrintersW returns TRUE!\n");
39 ok(GetLastError() == ERROR_INVALID_LEVEL, "EnumPrintersW returns error %lu!\n", GetLastError());
40 ok(cbNeeded == 0xDEADBEEF, "cbNeeded is %lu!\n", cbNeeded);
41 ok(dwReturned == 0xDEADBEEF, "dwReturned is %lu!\n", dwReturned);
42
43 // Try for all valid levels. Level 0 is valid here and returns the PRINTER_INFO_STRESS structure (documented in MS-RPRN).
44 for (i = 0; i < sizeof(dwValidLevels) / sizeof(DWORD); i++)
45 {
46 // Try with no valid arguments at all.
47 SetLastError(0xDEADBEEF);
48 ok(!EnumPrintersW(0, NULL, dwValidLevels[i], NULL, 0, NULL, NULL), "EnumPrintersW returns TRUE for Level %lu!\n", dwValidLevels[i]);
49 ok(GetLastError() == RPC_X_NULL_REF_POINTER, "EnumPrintersW returns error %lu for Level %lu!\n", GetLastError(), dwValidLevels[i]);
50
51 // It has to succeed if we supply the required pointers and query no information.
52 SetLastError(0xDEADBEEF);
53 ok(EnumPrintersW(0, NULL, dwValidLevels[i], NULL, 0, &cbNeeded, &dwReturned), "EnumPrintersW returns FALSE for Level %lu!\n", dwValidLevels[i]);
54 ok(GetLastError() == ERROR_SUCCESS, "EnumPrintersW returns error %lu for Level %lu!\n", GetLastError(), dwValidLevels[i]);
55 ok(cbNeeded == 0, "cbNeeded is %lu for Level %lu!\n", cbNeeded, dwValidLevels[i]);
56 ok(dwReturned == 0, "dwReturned is %lu for Level %lu!\n", dwReturned, dwValidLevels[i]);
57
58 // This constant is from Windows 9x/ME times and mustn't work anymore.
59 SetLastError(0xDEADBEEF);
60 ok(EnumPrintersW(PRINTER_ENUM_DEFAULT, NULL, dwValidLevels[i], NULL, 0, &cbNeeded, &dwReturned), "EnumPrintersW returns FALSE for Level %lu!\n", dwValidLevels[i]);
61 ok(GetLastError() == ERROR_SUCCESS, "EnumPrintersW returns error %lu for Level %lu!\n", GetLastError(), dwValidLevels[i]);
62 ok(cbNeeded == 0, "cbNeeded is %lu for Level %lu!\n", cbNeeded, dwValidLevels[i]);
63 ok(dwReturned == 0, "dwReturned is %lu for Level %lu!\n", dwReturned, dwValidLevels[i]);
64
65 // Now things get interesting. Let's query the buffer size for information about the local printers.
66 SetLastError(0xDEADBEEF);
67 ok(!EnumPrintersW(PRINTER_ENUM_LOCAL, NULL, dwValidLevels[i], NULL, 0, &cbNeeded, &dwReturned), "EnumPrintersW returns TRUE for Level %lu!\n", dwValidLevels[i]);
68 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER, "EnumPrintersW returns error %lu for Level %lu!\n", GetLastError(), dwValidLevels[i]);
69 ok(cbNeeded > 0, "cbNeeded is 0 for Level %lu!\n", dwValidLevels[i]);
70 ok(dwReturned == 0, "dwReturned is %lu for Level %lu!\n", dwReturned, dwValidLevels[i]);
71
72 // Same error has to occur with a size to small.
73 SetLastError(0xDEADBEEF);
74 ok(!EnumPrintersW(PRINTER_ENUM_LOCAL, NULL, dwValidLevels[i], NULL, 1, &cbNeeded, &dwReturned), "EnumPrintersW returns TRUE for Level %lu!\n", dwValidLevels[i]);
75 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER, "EnumPrintersW returns error %lu for Level %lu!\n", GetLastError(), dwValidLevels[i]);
76 ok(cbNeeded > 0, "cbNeeded is 0 for Level %lu!\n", dwValidLevels[i]);
77 ok(dwReturned == 0, "dwReturned is %lu for Level %lu!\n", dwReturned, dwValidLevels[i]);
78
79 // Now provide the demanded size, but no buffer.
80 SetLastError(0xDEADBEEF);
81 ok(!EnumPrintersW(PRINTER_ENUM_LOCAL, NULL, dwValidLevels[i], NULL, cbNeeded, &cbTemp, &dwReturned), "EnumPrintersW returns TRUE for Level %lu!\n", dwValidLevels[i]);
82 ok(GetLastError() == ERROR_INVALID_USER_BUFFER, "EnumPrintersW returns error %lu for Level %lu!\n", GetLastError(), dwValidLevels[i]);
83 ok(cbTemp == 0, "cbTemp is %lu for Level %lu!\n", cbTemp, dwValidLevels[i]);
84 ok(dwReturned == 0, "dwReturned is %lu for Level %lu!\n", dwReturned, dwValidLevels[i]);
85
86 // Finally use the function as intended and aim for success!
87 pMem = HeapAlloc(GetProcessHeap(), 0, cbNeeded);
88 SetLastError(0xDEADBEEF);
89 ok(EnumPrintersW(PRINTER_ENUM_LOCAL, NULL, dwValidLevels[i], pMem, cbNeeded, &cbTemp, &dwReturned), "EnumPrintersW returns FALSE for Level %lu!\n", dwValidLevels[i]);
90 ok(GetLastError() == ERROR_SUCCESS, "EnumPrintersW returns error %lu for Level %lu!\n", GetLastError(), dwValidLevels[i]);
91 HeapFree(GetProcessHeap(), 0, pMem);
92 }
93 }