e0bc01ed850e9afe83e8a3975bde153af97e63f9
[reactos.git] / rostests / winetests / ntprint / ntprint.c
1 /*
2 * Unit test suite for the Spooler Setup API (Printing)
3 *
4 * Copyright 2007 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 #include <stdio.h>
24
25 #include "windef.h"
26 #include "winbase.h"
27 #include "winerror.h"
28 #include "wingdi.h"
29 #include "wine/test.h"
30
31
32 /* ##### */
33
34 static HMODULE hdll;
35 static HANDLE (WINAPI *pPSetupCreateMonitorInfo)(LPVOID, LPVOID, LPVOID);
36 static VOID (WINAPI *pPSetupDestroyMonitorInfo)(HANDLE);
37 static BOOL (WINAPI *pPSetupEnumMonitor)(HANDLE, DWORD, LPWSTR, LPDWORD);
38
39 /* ########################### */
40
41 static LPCSTR load_functions(void)
42 {
43 LPCSTR ptr;
44
45 ptr = "ntprint.dll";
46 hdll = LoadLibraryA(ptr);
47 if (!hdll) return ptr;
48
49 ptr = "PSetupCreateMonitorInfo";
50 pPSetupCreateMonitorInfo = (VOID *) GetProcAddress(hdll, ptr);
51 if (!pPSetupCreateMonitorInfo) return ptr;
52
53 ptr = "PSetupDestroyMonitorInfo";
54 pPSetupDestroyMonitorInfo = (VOID *) GetProcAddress(hdll, ptr);
55 if (!pPSetupDestroyMonitorInfo) return ptr;
56
57 ptr = "PSetupEnumMonitor";
58 pPSetupEnumMonitor = (VOID *) GetProcAddress(hdll, ptr);
59 if (!pPSetupEnumMonitor) return ptr;
60
61 return NULL;
62 }
63
64 /* ########################### */
65
66 static void test_PSetupCreateMonitorInfo(VOID)
67 {
68 HANDLE mi;
69 BYTE buffer[1024] ;
70
71 SetLastError(0xdeadbeef);
72 mi = pPSetupCreateMonitorInfo(NULL, NULL, NULL);
73 if (!mi && (GetLastError() == RPC_S_SERVER_UNAVAILABLE)) {
74 win_skip("The service 'Spooler' is required for many tests\n");
75 return;
76 }
77 ok( mi != NULL, "got %p with %u (expected '!= NULL')\n", mi, GetLastError());
78 if (mi) pPSetupDestroyMonitorInfo(mi);
79
80
81 memset(buffer, 0, sizeof(buffer));
82 SetLastError(0xdeadbeef);
83 mi = pPSetupCreateMonitorInfo(buffer, NULL, NULL);
84 ok( mi != NULL, "got %p with %u (expected '!= NULL')\n", mi, GetLastError());
85 if (mi) pPSetupDestroyMonitorInfo(mi);
86
87 }
88
89 /* ########################### */
90
91 static void test_PSetupDestroyMonitorInfo(VOID)
92 {
93 HANDLE mi;
94
95
96 SetLastError(0xdeadbeef);
97 pPSetupDestroyMonitorInfo(NULL);
98 /* lasterror is returned */
99 trace("returned with %u\n", GetLastError());
100
101 SetLastError(0xdeadbeef);
102 mi = pPSetupCreateMonitorInfo(NULL, NULL, NULL);
103 if (!mi && (GetLastError() == RPC_S_SERVER_UNAVAILABLE)) {
104 win_skip("The service 'Spooler' is required for many tests\n");
105 return;
106 }
107 ok( mi != NULL, "got %p with %u (expected '!= NULL')\n", mi, GetLastError());
108
109 if (!mi) return;
110
111 SetLastError(0xdeadbeef);
112 pPSetupDestroyMonitorInfo(mi);
113 /* lasterror is returned */
114 trace("returned with %u\n", GetLastError());
115
116 /* Trying to destroy the handle twice crashes with native ntprint.dll */
117 if (0) {
118 SetLastError(0xdeadbeef);
119 pPSetupDestroyMonitorInfo(mi);
120 trace(" with %u\n", GetLastError());
121 }
122
123 }
124
125 /* ########################### */
126
127 static void test_PSetupEnumMonitor(VOID)
128 {
129 HANDLE mi;
130 WCHAR buffer[MAX_PATH+2];
131 DWORD minsize = 0;
132 DWORD size;
133 DWORD res;
134 DWORD index=0;
135
136 SetLastError(0xdeadbeef);
137 mi = pPSetupCreateMonitorInfo(NULL, NULL, NULL);
138 if (!mi) {
139 skip("PSetupCreateMonitorInfo\n");
140 return;
141 }
142
143 minsize = 0;
144 SetLastError(0xdeadbeef);
145 res = pPSetupEnumMonitor(mi, 0, NULL, &minsize);
146 ok( !res && (GetLastError() == ERROR_INSUFFICIENT_BUFFER) && (minsize > 0),
147 "got %u with %u and %u (expected '0' with ERROR_INSUFFICIENT_BUFFER "
148 "and '> 0')\n", res, GetLastError(), minsize);
149
150
151 size = sizeof(buffer) / sizeof(buffer[0]);
152 if ((minsize + 1) > size) {
153 skip("overflow: %u\n", minsize);
154 pPSetupDestroyMonitorInfo(mi);
155 return;
156 }
157
158 if (0) {
159 /* XP: ERROR_INVALID_PARAMETER, w2k: Crash */
160 SetLastError(0xdeadbeef);
161 size = sizeof(buffer) / sizeof(buffer[0]);
162 res = pPSetupEnumMonitor(NULL, 0, buffer, &size);
163 ok( !res && (GetLastError() == ERROR_INVALID_PARAMETER),
164 "got %u with %u (expected '0' with ERROR_INVALID_PARAMETER)\n",
165 res, GetLastError());
166 }
167
168 if (0) {
169 /* XP: Crash, w2k: Success (how can that work?) */
170 SetLastError(0xdeadbeef);
171 size = sizeof(buffer) / sizeof(buffer[0]);
172 res = pPSetupEnumMonitor(mi, 0, NULL, &size);
173 trace("got %u with %u and %u\n", res, GetLastError(), size);
174 }
175
176 if (0) {
177 /* XP: ERROR_INVALID_PARAMETER, w2k: Crash */
178 SetLastError(0xdeadbeef);
179 res = pPSetupEnumMonitor(mi, 0, buffer, NULL);
180 ok( !res && (GetLastError() == ERROR_INVALID_PARAMETER),
181 "got %u with %u (expected '0' with ERROR_INVALID_PARAMETER)\n",
182 res, GetLastError());
183 }
184
185 SetLastError(0xdeadbeef);
186 size = minsize - 1;
187 res = pPSetupEnumMonitor(mi, 0, buffer, &size);
188 ok( !res && (GetLastError() == ERROR_INSUFFICIENT_BUFFER),
189 "got %u with %u and %u (expected '0' with ERROR_INSUFFICIENT_BUFFER)\n",
190 res, GetLastError(), size);
191
192
193 SetLastError(0xdeadbeef);
194 size = minsize;
195 res = pPSetupEnumMonitor(mi, 0, buffer, &size);
196 ok( res, "got %u with %u and %u (expected '!= 0')\n",
197 res, GetLastError(), size);
198
199 SetLastError(0xdeadbeef);
200 size = minsize + 1;
201 res = pPSetupEnumMonitor(mi, 0, buffer, &size);
202 ok( res, "got %u with %u and %u (expected '!= 0')\n",
203 res, GetLastError(), size);
204
205 /* try max. 20 monitors */
206 while (res && (index < 20)) {
207 SetLastError(0xdeadbeef);
208 buffer[0] = '\0';
209 size = sizeof(buffer) / sizeof(buffer[0]);
210 res = pPSetupEnumMonitor(mi, index, buffer, &size);
211 ok( res || (GetLastError() == ERROR_NO_MORE_ITEMS),
212 "(%u) got %u with %u and %u (expected '!=0' or: '0' with "
213 "ERROR_NO_MORE_ITEMS)\n", index, res, GetLastError(), size);
214
215 if (res) index++;
216 }
217 pPSetupDestroyMonitorInfo(mi);
218
219 }
220
221 /* ########################### */
222
223 START_TEST(ntprint)
224 {
225 load_functions();
226
227 test_PSetupCreateMonitorInfo();
228 test_PSetupDestroyMonitorInfo();
229 test_PSetupEnumMonitor();
230
231 }