server: Implement RegisterHotKey/UnregisterHotKey.
[wine] / dlls / user32 / tests / winstation.c
1 /*
2  * Unit tests for window stations and desktops
3  *
4  * Copyright 2002 Alexandre Julliard
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 #include "wine/test.h"
22 #include "winbase.h"
23 #include "wingdi.h"
24 #include "winuser.h"
25 #include "winnls.h"
26
27 #define DESKTOP_ALL_ACCESS 0x01ff
28
29 static void print_object( HANDLE obj )
30 {
31     char buffer[100];
32     DWORD size;
33
34     strcpy( buffer, "foobar" );
35     if (!GetUserObjectInformationA( obj, UOI_NAME, buffer, sizeof(buffer), &size ))
36         trace( "could not get info for %p\n", obj );
37     else
38         trace( "obj %p name '%s'\n", obj, buffer );
39     strcpy( buffer, "foobar" );
40     if (!GetUserObjectInformationA( obj, UOI_TYPE, buffer, sizeof(buffer), &size ))
41         trace( "could not get type for %p\n", obj );
42     else
43         trace( "obj %p type '%s'\n", obj, buffer );
44 }
45
46 static void register_class(void)
47 {
48     WNDCLASSA cls;
49
50     cls.style = CS_DBLCLKS;
51     cls.lpfnWndProc = DefWindowProcA;
52     cls.cbClsExtra = 0;
53     cls.cbWndExtra = 0;
54     cls.hInstance = GetModuleHandleA(0);
55     cls.hIcon = 0;
56     cls.hCursor = LoadCursorA(0, IDC_ARROW);
57     cls.hbrBackground = GetStockObject(WHITE_BRUSH);
58     cls.lpszMenuName = NULL;
59     cls.lpszClassName = "WinStationClass";
60     RegisterClassA(&cls);
61 }
62
63 static HDESK initial_desktop;
64
65 static DWORD CALLBACK thread( LPVOID arg )
66 {
67     HDESK d1, d2;
68     HWND hwnd = CreateWindowExA(0,"WinStationClass","test",WS_POPUP,0,0,100,100,GetDesktopWindow(),0,0,0);
69     ok( hwnd != 0, "CreateWindow failed\n" );
70     d1 = GetThreadDesktop(GetCurrentThreadId());
71     trace( "thread %p desktop: %p\n", arg, d1 );
72     ok( d1 == initial_desktop, "thread %p doesn't use initial desktop\n", arg );
73
74     SetLastError( 0xdeadbeef );
75     ok( !CloseHandle( d1 ), "CloseHandle succeeded\n" );
76     ok( GetLastError() == ERROR_INVALID_HANDLE, "bad last error %d\n", GetLastError() );
77     SetLastError( 0xdeadbeef );
78     ok( !CloseDesktop( d1 ), "CloseDesktop succeeded\n" );
79     ok( GetLastError() == ERROR_BUSY || broken(GetLastError() == 0xdeadbeef), /* wow64 */
80         "bad last error %d\n", GetLastError() );
81     print_object( d1 );
82     d2 = CreateDesktop( "foobar2", NULL, NULL, 0, DESKTOP_ALL_ACCESS, NULL );
83     trace( "created desktop %p\n", d2 );
84     ok( d2 != 0, "CreateDesktop failed\n" );
85
86     SetLastError( 0xdeadbeef );
87     ok( !SetThreadDesktop( d2 ), "set thread desktop succeeded with existing window\n" );
88     ok( GetLastError() == ERROR_BUSY || broken(GetLastError() == 0xdeadbeef), /* wow64 */
89         "bad last error %d\n", GetLastError() );
90
91     DestroyWindow( hwnd );
92     ok( SetThreadDesktop( d2 ), "set thread desktop failed\n" );
93     d1 = GetThreadDesktop(GetCurrentThreadId());
94     ok( d1 == d2, "GetThreadDesktop did not return set desktop %p/%p\n", d1, d2 );
95     print_object( d2 );
96     if (arg < (LPVOID)5)
97     {
98         HANDLE hthread = CreateThread( NULL, 0, thread, (char *)arg + 1, 0, NULL );
99         Sleep(1000);
100         WaitForSingleObject( hthread, INFINITE );
101         CloseHandle( hthread );
102     }
103     return 0;
104 }
105
106 static void test_handles(void)
107 {
108     HWINSTA w1, w2, w3;
109     HDESK d1, d2, d3;
110     HANDLE hthread;
111     DWORD id, flags;
112     ATOM atom;
113     char buffer[20];
114
115     /* win stations */
116
117     w1 = GetProcessWindowStation();
118     ok( GetProcessWindowStation() == w1, "GetProcessWindowStation returned different handles\n" );
119     ok( !CloseWindowStation(w1), "closing process win station succeeded\n" );
120     SetLastError( 0xdeadbeef );
121     ok( !CloseHandle(w1), "closing process win station handle succeeded\n" );
122     ok( GetLastError() == ERROR_INVALID_HANDLE, "bad last error %d\n", GetLastError() );
123     print_object( w1 );
124
125     flags = 0;
126     ok( GetHandleInformation( w1, &flags ), "GetHandleInformation failed\n" );
127     ok( !(flags & HANDLE_FLAG_PROTECT_FROM_CLOSE) ||
128         broken(flags & HANDLE_FLAG_PROTECT_FROM_CLOSE), /* set on nt4 */
129         "handle %p PROTECT_FROM_CLOSE set\n", w1 );
130
131     ok( DuplicateHandle( GetCurrentProcess(), w1, GetCurrentProcess(), (PHANDLE)&w2, 0,
132                          TRUE, DUPLICATE_SAME_ACCESS ), "DuplicateHandle failed\n" );
133     ok( CloseWindowStation(w2), "closing dup win station failed\n" );
134
135     ok( DuplicateHandle( GetCurrentProcess(), w1, GetCurrentProcess(), (PHANDLE)&w2, 0,
136                          TRUE, DUPLICATE_SAME_ACCESS ), "DuplicateHandle failed\n" );
137     ok( CloseHandle(w2), "closing dup win station handle failed\n" );
138
139     w2 = CreateWindowStation("WinSta0", 0, WINSTA_ALL_ACCESS, NULL );
140     ok( w2 != 0, "CreateWindowStation failed\n" );
141     ok( w2 != w1, "CreateWindowStation returned default handle\n" );
142     SetLastError( 0xdeadbeef );
143     ok( !CloseDesktop( (HDESK)w2 ), "CloseDesktop succeeded on win station\n" );
144     ok( GetLastError() == ERROR_INVALID_HANDLE || broken(GetLastError() == 0xdeadbeef), /* wow64 */
145         "bad last error %d\n", GetLastError() );
146     ok( CloseWindowStation( w2 ), "CloseWindowStation failed\n" );
147
148     w2 = CreateWindowStation("WinSta0", 0, WINSTA_ALL_ACCESS, NULL );
149     ok( CloseHandle( w2 ), "CloseHandle failed\n" );
150
151     w2 = OpenWindowStation("winsta0", TRUE, WINSTA_ALL_ACCESS );
152     ok( w2 != 0, "OpenWindowStation failed\n" );
153     ok( w2 != w1, "OpenWindowStation returned default handle\n" );
154     ok( CloseWindowStation( w2 ), "CloseWindowStation failed\n" );
155
156     w2 = OpenWindowStation("dummy name", TRUE, WINSTA_ALL_ACCESS );
157     ok( !w2, "open dummy win station succeeded\n" );
158
159     CreateMutexA( NULL, 0, "foobar" );
160     w2 = CreateWindowStation("foobar", 0, WINSTA_ALL_ACCESS, NULL );
161     ok( w2 != 0, "create foobar station failed\n" );
162
163     w3 = OpenWindowStation("foobar", TRUE, WINSTA_ALL_ACCESS );
164     ok( w3 != 0, "open foobar station failed\n" );
165     ok( w3 != w2, "open foobar station returned same handle\n" );
166     ok( CloseWindowStation( w2 ), "CloseWindowStation failed\n" );
167     ok( CloseWindowStation( w3 ), "CloseWindowStation failed\n" );
168
169     w3 = OpenWindowStation("foobar", TRUE, WINSTA_ALL_ACCESS );
170     ok( !w3, "open foobar station succeeded\n" );
171
172     w2 = CreateWindowStation("foobar1", 0, WINSTA_ALL_ACCESS, NULL );
173     ok( w2 != 0, "create foobar station failed\n" );
174     w3 = CreateWindowStation("foobar2", 0, WINSTA_ALL_ACCESS, NULL );
175     ok( w3 != 0, "create foobar station failed\n" );
176     ok( GetHandleInformation( w2, &flags ), "GetHandleInformation failed\n" );
177     ok( GetHandleInformation( w3, &flags ), "GetHandleInformation failed\n" );
178
179     SetProcessWindowStation( w2 );
180     register_class();
181     atom = GlobalAddAtomA("foo");
182     ok( GlobalGetAtomNameA( atom, buffer, sizeof(buffer) ) == 3, "GlobalGetAtomName failed\n" );
183     ok( !lstrcmpiA( buffer, "foo" ), "bad atom value %s\n", buffer );
184
185     ok( !CloseWindowStation( w2 ), "CloseWindowStation succeeded\n" );
186     ok( GetHandleInformation( w2, &flags ), "GetHandleInformation failed\n" );
187
188     SetProcessWindowStation( w3 );
189     ok( GetHandleInformation( w2, &flags ), "GetHandleInformation failed\n" );
190     ok( CloseWindowStation( w2 ), "CloseWindowStation failed\n" );
191     ok( GlobalGetAtomNameA( atom, buffer, sizeof(buffer) ) == 3, "GlobalGetAtomName failed\n" );
192     ok( !lstrcmpiA( buffer, "foo" ), "bad atom value %s\n", buffer );
193
194     /* desktops */
195     d1 = GetThreadDesktop(GetCurrentThreadId());
196     initial_desktop = d1;
197     ok( GetThreadDesktop(GetCurrentThreadId()) == d1,
198         "GetThreadDesktop returned different handles\n" );
199
200     flags = 0;
201     ok( GetHandleInformation( d1, &flags ), "GetHandleInformation failed\n" );
202     ok( !(flags & HANDLE_FLAG_PROTECT_FROM_CLOSE), "handle %p PROTECT_FROM_CLOSE set\n", d1 );
203
204     SetLastError( 0xdeadbeef );
205     ok( !CloseDesktop(d1), "closing thread desktop succeeded\n" );
206     ok( GetLastError() == ERROR_BUSY || broken(GetLastError() == 0xdeadbeef), /* wow64 */
207         "bad last error %d\n", GetLastError() );
208
209     SetLastError( 0xdeadbeef );
210     if (CloseHandle( d1 ))  /* succeeds on nt4 */
211     {
212         win_skip( "NT4 desktop handle management is completely different\n" );
213         return;
214     }
215     ok( GetLastError() == ERROR_INVALID_HANDLE, "bad last error %d\n", GetLastError() );
216
217     ok( DuplicateHandle( GetCurrentProcess(), d1, GetCurrentProcess(), (PHANDLE)&d2, 0,
218                          TRUE, DUPLICATE_SAME_ACCESS ), "DuplicateHandle failed\n" );
219     ok( CloseDesktop(d2), "closing dup desktop failed\n" );
220
221     ok( DuplicateHandle( GetCurrentProcess(), d1, GetCurrentProcess(), (PHANDLE)&d2, 0,
222                          TRUE, DUPLICATE_SAME_ACCESS ), "DuplicateHandle failed\n" );
223     ok( CloseHandle(d2), "closing dup desktop handle failed\n" );
224
225     d2 = OpenDesktop( "dummy name", 0, TRUE, DESKTOP_ALL_ACCESS );
226     ok( !d2, "open dummy desktop succeeded\n" );
227
228     d2 = CreateDesktop( "foobar", NULL, NULL, 0, DESKTOP_ALL_ACCESS, NULL );
229     ok( d2 != 0, "create foobar desktop failed\n" );
230     SetLastError( 0xdeadbeef );
231     ok( !CloseWindowStation( (HWINSTA)d2 ), "CloseWindowStation succeeded on desktop\n" );
232     ok( GetLastError() == ERROR_INVALID_HANDLE || broken(GetLastError() == 0xdeadbeef), /* wow64 */
233         "bad last error %d\n", GetLastError() );
234
235     SetLastError( 0xdeadbeef );
236     d3 = CreateDesktop( "foobar", NULL, NULL, 0, DESKTOP_ALL_ACCESS, NULL );
237     ok( d3 != 0, "create foobar desktop again failed\n" );
238     ok( GetLastError() == 0xdeadbeef, "bad last error %d\n", GetLastError() );
239     ok( CloseDesktop( d3 ), "CloseDesktop failed\n" );
240
241     d3 = OpenDesktop( "foobar", 0, TRUE, DESKTOP_ALL_ACCESS );
242     ok( d3 != 0, "open foobar desktop failed\n" );
243     ok( d3 != d2, "open foobar desktop returned same handle\n" );
244     ok( CloseDesktop( d2 ), "CloseDesktop failed\n" );
245     ok( CloseDesktop( d3 ), "CloseDesktop failed\n" );
246
247     d3 = OpenDesktop( "foobar", 0, TRUE, DESKTOP_ALL_ACCESS );
248     ok( !d3, "open foobar desktop succeeded\n" );
249
250     ok( !CloseHandle(d1), "closing thread desktop handle succeeded\n" );
251     d2 = GetThreadDesktop(GetCurrentThreadId());
252     ok( d1 == d2, "got different handles after close\n" );
253
254     trace( "thread 1 desktop: %p\n", d1 );
255     print_object( d1 );
256     hthread = CreateThread( NULL, 0, thread, (LPVOID)2, 0, &id );
257     Sleep(1000);
258     trace( "get other thread desktop: %p\n", GetThreadDesktop(id) );
259     WaitForSingleObject( hthread, INFINITE );
260     CloseHandle( hthread );
261 }
262
263 /* Enumeration tests */
264
265 static BOOL CALLBACK window_station_callbackA(LPSTR winsta, LPARAM lp)
266 {
267     trace("window_station_callbackA called with argument %s\n", winsta);
268     return lp;
269 }
270
271 static BOOL CALLBACK open_window_station_callbackA(LPSTR winsta, LPARAM lp)
272 {
273     HWINSTA hwinsta;
274
275     trace("open_window_station_callbackA called with argument %s\n", winsta);
276     hwinsta = OpenWindowStationA(winsta, FALSE, WINSTA_ENUMERATE);
277     ok(hwinsta != NULL, "Could not open desktop %s!\n", winsta);
278     if (hwinsta)
279         CloseWindowStation(hwinsta);
280     return lp;
281 }
282
283 static void test_enumstations(void)
284 {
285     BOOL ret;
286
287     if (0) /* Crashes instead */
288     {
289         SetLastError(0xbabefeed);
290         ret = EnumWindowStationsA(NULL, 0);
291         ok(!ret, "EnumWindowStationsA returned successfully!\n");
292         ok(GetLastError() == ERROR_INVALID_PARAMETER, "LastError is set to %08x\n", GetLastError());
293     }
294
295     SetLastError(0xdeadbeef);
296     ret = EnumWindowStationsA(open_window_station_callbackA, 0x12345);
297     ok(ret == 0x12345, "EnumWindowStationsA returned %x\n", ret);
298     ok(GetLastError() == 0xdeadbeef, "LastError is set to %08x\n", GetLastError());
299
300     SetLastError(0xdeadbeef);
301     ret = EnumWindowStationsA(window_station_callbackA, 0);
302     ok(!ret, "EnumWindowStationsA returned %x\n", ret);
303     ok(GetLastError() == 0xdeadbeef, "LastError is set to %08x\n", GetLastError());
304 }
305
306 static BOOL CALLBACK desktop_callbackA(LPSTR desktop, LPARAM lp)
307 {
308     trace("desktop_callbackA called with argument %s\n", desktop);
309     return lp;
310 }
311
312 static BOOL CALLBACK open_desktop_callbackA(LPSTR desktop, LPARAM lp)
313 {
314     HDESK hdesk;
315     static int once;
316
317     trace("open_desktop_callbackA called with argument %s\n", desktop);
318     /* Only try to open one desktop */
319     if (once++)
320         return lp;
321
322     hdesk = OpenDesktopA(desktop, 0, FALSE, DESKTOP_ENUMERATE);
323     ok(hdesk != NULL, "Could not open desktop %s!\n", desktop);
324     if (hdesk)
325         CloseDesktop(hdesk);
326     return lp;
327 }
328
329 static void test_enumdesktops(void)
330 {
331     BOOL ret;
332
333     if (0)  /* Crashes instead */
334     {
335         SetLastError(0xbabefeed);
336         ret = EnumDesktopsA(GetProcessWindowStation(), NULL, 0);
337         ok(!ret, "EnumDesktopsA returned successfully!\n");
338         ok(GetLastError() == ERROR_INVALID_PARAMETER, "LastError is set to %08x\n", GetLastError());
339     }
340
341     SetLastError(0xdeadbeef);
342     ret = EnumDesktopsA(NULL, desktop_callbackA, 0x12345);
343     ok(ret == 0x12345, "EnumDesktopsA returned %x\n", ret);
344     ok(GetLastError() == 0xdeadbeef, "LastError is set to %08x\n", GetLastError());
345
346     SetLastError(0xdeadbeef);
347     ret = EnumDesktopsA(GetProcessWindowStation(), open_desktop_callbackA, 0x12345);
348     ok(ret == 0x12345, "EnumDesktopsA returned %x\n", ret);
349     ok(GetLastError() == 0xdeadbeef, "LastError is set to %08x\n", GetLastError());
350
351     SetLastError(0xdeadbeef);
352     ret = EnumDesktopsA(INVALID_HANDLE_VALUE, desktop_callbackA, 0x12345);
353     ok(!ret, "EnumDesktopsA returned %x\n", ret);
354     ok(GetLastError() == ERROR_INVALID_HANDLE, "LastError is set to %08x\n", GetLastError());
355
356     SetLastError(0xdeadbeef);
357     ret = EnumDesktopsA(GetProcessWindowStation(), desktop_callbackA, 0);
358     ok(!ret, "EnumDesktopsA returned %x\n", ret);
359     ok(GetLastError() == 0xdeadbeef, "LastError is set to %08x\n", GetLastError());
360 }
361
362 START_TEST(winstation)
363 {
364     /* Check whether this platform supports WindowStation calls */
365
366     SetLastError( 0xdeadbeef );
367     GetProcessWindowStation();
368     if (GetLastError() == ERROR_CALL_NOT_IMPLEMENTED)
369     {
370         skip("WindowStation calls not supported on this platform\n");
371         return;
372     }
373
374     test_enumstations();
375     test_enumdesktops();
376     test_handles();
377 }