[LOCALSPL]
[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-2017 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 BYTE TempBuffer[50];
19 BYTE ZeroBuffer[50] = { 0 };
20 DWORD cbNeeded;
21 DWORD cbTemp;
22 DWORD dwReturned;
23 PVOID pMem;
24 DWORD i;
25 DWORD dwValidLevels[] = { 0, 1, 2, 4, 5 };
26
27 // Verify that EnumPrintersW returns success and zeroes all input variables even though no flag has been specified.
28 memset(TempBuffer, 0xDE, sizeof(TempBuffer));
29 cbNeeded = 0xDEADBEEF;
30 dwReturned = 0xDEADBEEF;
31 SetLastError(0xDEADBEEF);
32 ok(EnumPrintersW(0, NULL, 1, TempBuffer, sizeof(TempBuffer), &cbNeeded, &dwReturned), "EnumPrintersW returns FALSE\n");
33 ok(GetLastError() == ERROR_SUCCESS, "EnumPrintersW returns error %lu!\n", GetLastError());
34 ok(memcmp(TempBuffer, ZeroBuffer, sizeof(TempBuffer)) == 0, "TempBuffer has not been zeroed!\n");
35 ok(cbNeeded == 0, "cbNeeded is %lu!\n", cbNeeded);
36 ok(dwReturned == 0, "dwReturned is %lu!\n", dwReturned);
37
38 // Level 5 is the highest supported under Windows Server 2003. Higher levels need to fail and leave the variables untouched!
39 cbNeeded = 0xDEADBEEF;
40 dwReturned = 0xDEADBEEF;
41 SetLastError(0xDEADBEEF);
42 ok(!EnumPrintersW(0, NULL, 6, NULL, 0, &cbNeeded, &dwReturned), "EnumPrintersW returns TRUE!\n");
43 ok(GetLastError() == ERROR_INVALID_LEVEL, "EnumPrintersW returns error %lu!\n", GetLastError());
44 ok(cbNeeded == 0xDEADBEEF, "cbNeeded is %lu!\n", cbNeeded);
45 ok(dwReturned == 0xDEADBEEF, "dwReturned is %lu!\n", dwReturned);
46
47 // Same goes for level 3.
48 cbNeeded = 0xDEADBEEF;
49 dwReturned = 0xDEADBEEF;
50 SetLastError(0xDEADBEEF);
51 ok(!EnumPrintersW(0, NULL, 3, NULL, 0, &cbNeeded, &dwReturned), "EnumPrintersW returns TRUE!\n");
52 ok(GetLastError() == ERROR_INVALID_LEVEL, "EnumPrintersW returns error %lu!\n", GetLastError());
53 ok(cbNeeded == 0xDEADBEEF, "cbNeeded is %lu!\n", cbNeeded);
54 ok(dwReturned == 0xDEADBEEF, "dwReturned is %lu!\n", dwReturned);
55
56 // Try for all valid levels. Level 0 is valid here and returns the PRINTER_INFO_STRESS structure (documented in MS-RPRN).
57 for (i = 0; i < sizeof(dwValidLevels) / sizeof(DWORD); i++)
58 {
59 // Try with no valid arguments at all.
60 SetLastError(0xDEADBEEF);
61 ok(!EnumPrintersW(0, NULL, dwValidLevels[i], NULL, 0, NULL, NULL), "EnumPrintersW returns TRUE for Level %lu!\n", dwValidLevels[i]);
62 ok(GetLastError() == RPC_X_NULL_REF_POINTER, "EnumPrintersW returns error %lu for Level %lu!\n", GetLastError(), dwValidLevels[i]);
63
64 // It has to succeed if we supply the required pointers and query no information.
65 SetLastError(0xDEADBEEF);
66 ok(EnumPrintersW(0, NULL, dwValidLevels[i], NULL, 0, &cbNeeded, &dwReturned), "EnumPrintersW returns FALSE for Level %lu!\n", dwValidLevels[i]);
67 ok(GetLastError() == ERROR_SUCCESS, "EnumPrintersW returns error %lu for Level %lu!\n", GetLastError(), dwValidLevels[i]);
68 ok(cbNeeded == 0, "cbNeeded is %lu for Level %lu!\n", cbNeeded, dwValidLevels[i]);
69 ok(dwReturned == 0, "dwReturned is %lu for Level %lu!\n", dwReturned, dwValidLevels[i]);
70
71 // This constant is from Windows 9x/ME times and mustn't work anymore.
72 SetLastError(0xDEADBEEF);
73 ok(EnumPrintersW(PRINTER_ENUM_DEFAULT, NULL, dwValidLevels[i], NULL, 0, &cbNeeded, &dwReturned), "EnumPrintersW returns FALSE for Level %lu!\n", dwValidLevels[i]);
74 ok(GetLastError() == ERROR_SUCCESS, "EnumPrintersW returns error %lu for Level %lu!\n", GetLastError(), dwValidLevels[i]);
75 ok(cbNeeded == 0, "cbNeeded is %lu for Level %lu!\n", cbNeeded, dwValidLevels[i]);
76 ok(dwReturned == 0, "dwReturned is %lu for Level %lu!\n", dwReturned, dwValidLevels[i]);
77
78 // Now things get interesting. Let's query the buffer size for information about the local printers.
79 SetLastError(0xDEADBEEF);
80 ok(!EnumPrintersW(PRINTER_ENUM_LOCAL, NULL, dwValidLevels[i], NULL, 0, &cbNeeded, &dwReturned), "EnumPrintersW returns TRUE for Level %lu!\n", dwValidLevels[i]);
81 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER, "EnumPrintersW returns error %lu for Level %lu!\n", GetLastError(), dwValidLevels[i]);
82 ok(cbNeeded > 0, "cbNeeded is 0 for Level %lu!\n", dwValidLevels[i]);
83 ok(dwReturned == 0, "dwReturned is %lu for Level %lu!\n", dwReturned, dwValidLevels[i]);
84
85 // Same error has to occur with no buffer, but a size < 4 (AlignRpcPtr comes into play here).
86 SetLastError(0xDEADBEEF);
87 ok(!EnumPrintersW(PRINTER_ENUM_LOCAL, NULL, dwValidLevels[i], NULL, 1, &cbNeeded, &dwReturned), "EnumPrintersW returns TRUE for Level %lu!\n", dwValidLevels[i]);
88 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER, "EnumPrintersW returns error %lu for Level %lu!\n", GetLastError(), dwValidLevels[i]);
89 ok(cbNeeded > 0, "cbNeeded is 0 for Level %lu!\n", dwValidLevels[i]);
90 ok(dwReturned == 0, "dwReturned is %lu for Level %lu!\n", dwReturned, dwValidLevels[i]);
91
92 // Now provide the demanded size, but no buffer.
93 SetLastError(0xDEADBEEF);
94 ok(!EnumPrintersW(PRINTER_ENUM_LOCAL, NULL, dwValidLevels[i], NULL, cbNeeded, &cbTemp, &dwReturned), "EnumPrintersW returns TRUE for Level %lu!\n", dwValidLevels[i]);
95 ok(GetLastError() == ERROR_INVALID_USER_BUFFER, "EnumPrintersW returns error %lu for Level %lu!\n", GetLastError(), dwValidLevels[i]);
96 ok(cbTemp == 0, "cbTemp is %lu for Level %lu!\n", cbTemp, dwValidLevels[i]);
97 ok(dwReturned == 0, "dwReturned is %lu for Level %lu!\n", dwReturned, dwValidLevels[i]);
98
99 // Finally use the function as intended and aim for success!
100 pMem = HeapAlloc(GetProcessHeap(), 0, cbNeeded);
101 SetLastError(0xDEADBEEF);
102 ok(EnumPrintersW(PRINTER_ENUM_LOCAL, NULL, dwValidLevels[i], pMem, cbNeeded, &cbTemp, &dwReturned), "EnumPrintersW returns FALSE for Level %lu!\n", dwValidLevels[i]);
103 ok(GetLastError() == ERROR_SUCCESS, "EnumPrintersW returns error %lu for Level %lu!\n", GetLastError(), dwValidLevels[i]);
104 HeapFree(GetProcessHeap(), 0, pMem);
105 }
106 }