riched20: Fix one more memory leak.
[wine] / dlls / localspl / tests / localmon.c
1 /* 
2  * Unit test suite for localspl API functions: local print monitor
3  *
4  * Copyright 2006 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
24 #include "windef.h"
25 #include "winbase.h"
26 #include "winerror.h"
27 #include "wingdi.h"
28 #include "winreg.h"
29
30 #include "winspool.h"
31 #include "ddk/winsplp.h"
32
33 #include "wine/test.h"
34
35
36 /* ##### */
37
38 static HMODULE  hdll;
39 static HMODULE  hlocalmon;
40 static LPMONITOREX (WINAPI *pInitializePrintMonitor)(LPWSTR);
41
42 static LPMONITOREX pm;
43 static BOOL  (WINAPI *pEnumPorts)(LPWSTR, DWORD, LPBYTE, DWORD, LPDWORD, LPDWORD);
44 static BOOL  (WINAPI *pOpenPort)(LPWSTR, PHANDLE);
45 static BOOL  (WINAPI *pOpenPortEx)(LPWSTR, LPWSTR, PHANDLE, struct _MONITOR *);
46 static BOOL  (WINAPI *pStartDocPort)(HANDLE, LPWSTR, DWORD, DWORD, LPBYTE);
47 static BOOL  (WINAPI *pWritePort)(HANDLE hPort, LPBYTE, DWORD, LPDWORD);
48 static BOOL  (WINAPI *pReadPort)(HANDLE hPort, LPBYTE, DWORD, LPDWORD);
49 static BOOL  (WINAPI *pEndDocPort)(HANDLE);
50 static BOOL  (WINAPI *pClosePort)(HANDLE);
51 static BOOL  (WINAPI *pAddPort)(LPWSTR, HWND, LPWSTR);
52 static BOOL  (WINAPI *pAddPortEx)(LPWSTR, DWORD, LPBYTE, LPWSTR);
53 static BOOL  (WINAPI *pConfigurePort)(LPWSTR, HWND, LPWSTR);
54 static BOOL  (WINAPI *pDeletePort)(LPWSTR, HWND, LPWSTR);
55 static BOOL  (WINAPI *pGetPrinterDataFromPort)(HANDLE, DWORD, LPWSTR, LPWSTR, DWORD, LPWSTR, DWORD, LPDWORD);
56 static BOOL  (WINAPI *pSetPortTimeOuts)(HANDLE, LPCOMMTIMEOUTS, DWORD);
57 static BOOL  (WINAPI *pXcvOpenPort)(HANDLE, LPCWSTR, ACCESS_MASK, PHANDLE phXcv);
58 static DWORD (WINAPI *pXcvDataPort)(HANDLE, LPCWSTR, PBYTE, DWORD, PBYTE, DWORD, PDWORD);
59 static BOOL  (WINAPI *pXcvClosePort)(HANDLE);
60
61 static WCHAR emptyW[] = {0};
62 static WCHAR invalid_serverW[] = {'\\','\\','i','n','v','a','l','i','d','_','s','e','r','v','e','r',0};
63 static WCHAR Monitors_LocalPortW[] = { \
64                                 'S','y','s','t','e','m','\\',
65                                 'C','u','r','r','e','n','t','C','o','n','t','r','o','l','S','e','t','\\',
66                                 'C','o','n','t','r','o','l','\\',
67                                 'P','r','i','n','t','\\',
68                                 'M','o','n','i','t','o','r','s','\\',
69                                 'L','o','c','a','l',' ','P','o','r','t',0};
70                                        
71 /* ##### */
72
73 static void test_EnumPorts(void)
74 {
75     DWORD   res;
76     DWORD   level;
77     LPBYTE  buffer;
78     DWORD   cbBuf;
79     DWORD   pcbNeeded;
80     DWORD   pcReturned;
81
82     if (!pEnumPorts) return;
83
84     /* valid levels are 1 and 2 */
85     for(level = 0; level < 4; level++) {
86
87         cbBuf = 0xdeadbeef;
88         pcReturned = 0xdeadbeef;
89         SetLastError(0xdeadbeef);
90         res = pEnumPorts(NULL, level, NULL, 0, &cbBuf, &pcReturned);
91
92         /* use only a short test, when we test with an invalid level */
93         if(!level || (level > 2)) {
94             /* NT4 fails with ERROR_INVALID_LEVEL (as expected)
95                XP succeeds with ERROR_SUCCESS () */
96             ok( (cbBuf == 0) && (pcReturned == 0),
97                 "(%d) returned %d with %d and %d, %d (expected 0, 0)\n",
98                 level, res, GetLastError(), cbBuf, pcReturned);
99             continue;
100         }        
101
102         ok( !res && (GetLastError() == ERROR_INSUFFICIENT_BUFFER),
103             "(%d) returned %d with %d and %d, %d (expected '0' with " \
104             "ERROR_INSUFFICIENT_BUFFER)\n",
105             level, res, GetLastError(), cbBuf, pcReturned);
106
107         buffer = HeapAlloc(GetProcessHeap(), 0, cbBuf * 2);
108         if (buffer == NULL) continue;
109
110         pcbNeeded = 0xdeadbeef;
111         pcReturned = 0xdeadbeef;
112         SetLastError(0xdeadbeef);
113         res = pEnumPorts(NULL, level, buffer, cbBuf, &pcbNeeded, &pcReturned);
114         ok( res, "(%d) returned %d with %d and %d, %d (expected '!= 0')\n",
115             level, res, GetLastError(), cbBuf, pcReturned);
116         /* We can compare the returned Data with the Registry / "win.ini",[Ports] here */
117
118         pcbNeeded = 0xdeadbeef;
119         pcReturned = 0xdeadbeef;
120         SetLastError(0xdeadbeef);
121         res = pEnumPorts(NULL, level, buffer, cbBuf+1, &pcbNeeded, &pcReturned);
122         ok( res, "(%d) returned %d with %d and %d, %d (expected '!= 0')\n",
123             level, res, GetLastError(), cbBuf, pcReturned);
124
125         pcbNeeded = 0xdeadbeef;
126         pcReturned = 0xdeadbeef;
127         SetLastError(0xdeadbeef);
128         res = pEnumPorts(NULL, level, buffer, cbBuf-1, &pcbNeeded, &pcReturned);
129         ok( !res && (GetLastError() == ERROR_INSUFFICIENT_BUFFER),
130             "(%d) returned %d with %d and %d, %d (expected '0' with " \
131             "ERROR_INSUFFICIENT_BUFFER)\n",
132             level, res, GetLastError(), cbBuf, pcReturned);
133
134 #if 0
135         /* The following tests crash this app with native localmon/localspl */
136         res = pEnumPorts(NULL, level, NULL, cbBuf, &pcbNeeded, &pcReturned);
137         res = pEnumPorts(NULL, level, buffer, cbBuf, NULL, &pcReturned);
138         res = pEnumPorts(NULL, level, buffer, cbBuf, &pcbNeeded, NULL);
139 #endif
140
141         /* The Servername is ignored */
142         pcbNeeded = 0xdeadbeef;
143         pcReturned = 0xdeadbeef;
144         SetLastError(0xdeadbeef);
145         res = pEnumPorts(emptyW, level, buffer, cbBuf+1, &pcbNeeded, &pcReturned);
146         ok( res, "(%d) returned %d with %d and %d, %d (expected '!= 0')\n",
147             level, res, GetLastError(), cbBuf, pcReturned);
148
149         pcbNeeded = 0xdeadbeef;
150         pcReturned = 0xdeadbeef;
151         SetLastError(0xdeadbeef);
152         res = pEnumPorts(invalid_serverW, level, buffer, cbBuf+1, &pcbNeeded, &pcReturned);
153         ok( res, "(%d) returned %d with %d and %d, %d (expected '!= 0')\n",
154             level, res, GetLastError(), cbBuf, pcReturned);
155
156         HeapFree(GetProcessHeap(), 0, buffer);
157     }
158 }
159
160 /* ########################### */
161
162
163 static void test_InitializePrintMonitor(void)
164 {
165     LPMONITOREX res;
166
167     SetLastError(0xdeadbeef);
168     res = pInitializePrintMonitor(NULL);
169     /* The Parameter was unchecked before w2k */
170     ok( res || (GetLastError() == ERROR_INVALID_PARAMETER),
171         "returned %p with %d\n (expected '!= NULL' or: NULL with " \
172         "ERROR_INVALID_PARAMETER)\n", res, GetLastError());
173
174     SetLastError(0xdeadbeef);
175     res = pInitializePrintMonitor(emptyW);
176     ok( res || (GetLastError() == ERROR_INVALID_PARAMETER),
177         "returned %p with %d\n (expected '!= NULL' or: NULL with " \
178         "ERROR_INVALID_PARAMETER)\n", res, GetLastError());
179
180
181     /* Every call with a non-empty string returns the same Pointer */
182     SetLastError(0xdeadbeef);
183     res = pInitializePrintMonitor(Monitors_LocalPortW);
184     ok( res == pm,
185         "returned %p with %d (expected %p)\n", res, GetLastError(), pm);
186 }
187
188 /* ########################### */
189
190 #define GET_MONITOR_FUNC(name) \
191             if(numentries > 0) { \
192                 numentries--; \
193                 p##name = (void *) pm->Monitor.pfn##name ;  \
194             }
195
196
197 START_TEST(localmon)
198 {
199     /* This DLL does not exists on Win9x */
200     hdll = LoadLibraryA("localspl.dll");
201     if (!hdll) return;
202
203     pInitializePrintMonitor = (void *) GetProcAddress(hdll, "InitializePrintMonitor");
204     if (!pInitializePrintMonitor) {
205         /* The Monitor for "Local Ports" was in a seperate dll before w2k */
206         hlocalmon = LoadLibraryA("localmon.dll");
207         if (hlocalmon) {
208             pInitializePrintMonitor = (void *) GetProcAddress(hlocalmon, "InitializePrintMonitor");
209         }
210     }
211     if (!pInitializePrintMonitor) return;
212
213     /* Native localmon.dll / localspl.dll need a vaild Port-Entry in:
214        a) since xp: HKLM\Software\Microsoft\Windows NT\CurrentVersion\Ports 
215        b) upto w2k: Section "Ports" in win.ini
216        or InitializePrintMonitor fails. */
217     pm = pInitializePrintMonitor(Monitors_LocalPortW);
218     if (pm) {
219         DWORD   numentries;
220         numentries = (pm->dwMonitorSize ) / sizeof(VOID *);
221         /* NT4: 14, since w2k: 17 */
222         ok( numentries == 14 || numentries == 17, 
223             "dwMonitorSize (%d) => %d Functions\n", pm->dwMonitorSize, numentries);
224
225         GET_MONITOR_FUNC(EnumPorts);
226         GET_MONITOR_FUNC(OpenPort);
227         GET_MONITOR_FUNC(OpenPortEx);
228         GET_MONITOR_FUNC(StartDocPort);
229         GET_MONITOR_FUNC(WritePort);
230         GET_MONITOR_FUNC(ReadPort);
231         GET_MONITOR_FUNC(EndDocPort);
232         GET_MONITOR_FUNC(ClosePort);
233         GET_MONITOR_FUNC(AddPort);
234         GET_MONITOR_FUNC(AddPortEx);
235         GET_MONITOR_FUNC(ConfigurePort);
236         GET_MONITOR_FUNC(DeletePort);
237         GET_MONITOR_FUNC(GetPrinterDataFromPort);
238         GET_MONITOR_FUNC(SetPortTimeOuts);
239         GET_MONITOR_FUNC(XcvOpenPort);
240         GET_MONITOR_FUNC(XcvDataPort);
241         GET_MONITOR_FUNC(XcvClosePort);
242     }
243     test_InitializePrintMonitor();
244     test_EnumPorts();
245 }