Fixes for -Wmissing-declaration and -Wwrite-string warnings.
[wine] / dlls / user / 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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  */
20
21 #include "wine/test.h"
22 #include "winbase.h"
23 #include "wingdi.h"
24 #include "winuser.h"
25
26 #define DESKTOP_ALL_ACCESS 0x01ff
27
28 static void print_object( HANDLE obj )
29 {
30     char buffer[100];
31     DWORD size;
32
33     strcpy( buffer, "foobar" );
34     if (!GetUserObjectInformationA( obj, UOI_NAME, buffer, sizeof(buffer), &size ))
35         trace( "could not get info for %p\n", obj );
36     else
37         trace( "obj %p name '%s'\n", obj, buffer );
38     strcpy( buffer, "foobar" );
39     if (!GetUserObjectInformationA( obj, UOI_TYPE, buffer, sizeof(buffer), &size ))
40         trace( "could not get type for %p\n", obj );
41     else
42         trace( "obj %p type '%s'\n", obj, buffer );
43 }
44
45 static HDESK initial_desktop;
46
47 static DWORD CALLBACK thread( LPVOID arg )
48 {
49     HDESK d1, d2;
50     HWND hwnd = CreateWindowExA(0,"BUTTON","test",WS_POPUP,0,0,100,100,GetDesktopWindow(),0,0,0);
51     ok( hwnd != 0, "CreateWindow failed\n" );
52     d1 = GetThreadDesktop(GetCurrentThreadId());
53     trace( "thread %p desktop: %p\n", arg, d1 );
54     ok( d1 == initial_desktop, "thread %p doesn't use initial desktop\n", arg );
55
56     SetLastError( 0xdeadbeef );
57     ok( !CloseHandle( d1 ), "CloseHandle succeeded\n" );
58     ok( GetLastError() == ERROR_INVALID_HANDLE, "bad last error %ld\n", GetLastError() );
59     SetLastError( 0xdeadbeef );
60     ok( !CloseDesktop( d1 ), "CloseDesktop succeeded\n" );
61     ok( GetLastError() == ERROR_BUSY, "bad last error %ld\n", GetLastError() );
62     print_object( d1 );
63     d2 = CreateDesktop( "foobar2", NULL, NULL, 0, DESKTOP_ALL_ACCESS, NULL );
64     trace( "created desktop %p\n", d2 );
65     ok( d2 != 0, "CreateDesktop failed\n" );
66
67     todo_wine
68     {
69         SetLastError( 0xdeadbeef );
70         ok( !SetThreadDesktop( d2 ), "set thread desktop succeeded with existing window\n" );
71         ok( GetLastError() == ERROR_BUSY, "bad last error %ld\n", GetLastError() );
72     }
73
74     DestroyWindow( hwnd );
75     ok( SetThreadDesktop( d2 ), "set thread desktop failed\n" );
76     d1 = GetThreadDesktop(GetCurrentThreadId());
77     ok( d1 == d2, "GetThreadDesktop did not return set desktop %p/%p\n", d1, d2 );
78     print_object( d2 );
79     if (arg < (LPVOID)5)
80     {
81         HANDLE hthread = CreateThread( NULL, 0, thread, (char *)arg + 1, 0, NULL );
82         Sleep(1000);
83         WaitForSingleObject( hthread, INFINITE );
84         CloseHandle( hthread );
85     }
86     return 0;
87 }
88
89 static void test_handles(void)
90 {
91     HWINSTA w1, w2, w3;
92     HDESK d1, d2, d3;
93     HANDLE hthread;
94     DWORD id, flags;
95
96     /* win stations */
97
98     w1 = GetProcessWindowStation();
99     ok( GetProcessWindowStation() == w1, "GetProcessWindowStation returned different handles\n" );
100     ok( !CloseWindowStation(w1), "closing process win station succeeded\n" );
101     SetLastError( 0xdeadbeef );
102     ok( !CloseHandle(w1), "closing process win station handle succeeded\n" );
103     ok( GetLastError() == ERROR_INVALID_HANDLE, "bad last error %ld\n", GetLastError() );
104     print_object( w1 );
105
106     flags = 0;
107     ok( GetHandleInformation( w1, &flags ), "GetHandleInformation failed\n" );
108     ok( !(flags & HANDLE_FLAG_PROTECT_FROM_CLOSE), "handle %p PROTECT_FROM_CLOSE set\n", w1 );
109
110     ok( DuplicateHandle( GetCurrentProcess(), w1, GetCurrentProcess(), (PHANDLE)&w2, 0,
111                          TRUE, DUPLICATE_SAME_ACCESS ), "DuplicateHandle failed\n" );
112     ok( CloseWindowStation(w2), "closing dup win station failed\n" );
113
114     ok( DuplicateHandle( GetCurrentProcess(), w1, GetCurrentProcess(), (PHANDLE)&w2, 0,
115                          TRUE, DUPLICATE_SAME_ACCESS ), "DuplicateHandle failed\n" );
116     ok( CloseHandle(w2), "closing dup win station handle failed\n" );
117
118     w2 = CreateWindowStation("WinSta0", 0, WINSTA_ALL_ACCESS, NULL );
119     ok( w2 != 0, "CreateWindowStation failed\n" );
120     ok( w2 != w1, "CreateWindowStation returned default handle\n" );
121     SetLastError( 0xdeadbeef );
122     ok( !CloseDesktop( (HDESK)w2 ), "CloseDesktop succeeded on win station\n" );
123     ok( GetLastError() == ERROR_INVALID_HANDLE, "bad last error %ld\n", GetLastError() );
124     ok( CloseWindowStation( w2 ), "CloseWindowStation failed\n" );
125
126     w2 = CreateWindowStation("WinSta0", 0, WINSTA_ALL_ACCESS, NULL );
127     ok( CloseHandle( w2 ), "CloseHandle failed\n" );
128
129     w2 = OpenWindowStation("winsta0", TRUE, WINSTA_ALL_ACCESS );
130     ok( w2 != 0, "OpenWindowStation failed\n" );
131     ok( w2 != w1, "OpenWindowStation returned default handle\n" );
132     ok( CloseWindowStation( w2 ), "CloseWindowStation failed\n" );
133
134     w2 = OpenWindowStation("dummy name", TRUE, WINSTA_ALL_ACCESS );
135     ok( !w2, "open dummy win station succeeded\n" );
136
137     CreateMutexA( NULL, 0, "foobar" );
138     w2 = CreateWindowStation("foobar", 0, WINSTA_ALL_ACCESS, NULL );
139     ok( w2 != 0, "create foobar station failed\n" );
140
141     w3 = OpenWindowStation("foobar", TRUE, WINSTA_ALL_ACCESS );
142     ok( w3 != 0, "open foobar station failed\n" );
143     ok( w3 != w2, "open foobar station returned same handle\n" );
144     ok( CloseWindowStation( w2 ), "CloseWindowStation failed\n" );
145     ok( CloseWindowStation( w3 ), "CloseWindowStation failed\n" );
146
147     w3 = OpenWindowStation("foobar", TRUE, WINSTA_ALL_ACCESS );
148     ok( !w3, "open foobar station succeeded\n" );
149
150     /* desktops */
151     d1 = GetThreadDesktop(GetCurrentThreadId());
152     initial_desktop = d1;
153     ok( GetThreadDesktop(GetCurrentThreadId()) == d1,
154         "GetThreadDesktop returned different handles\n" );
155
156     flags = 0;
157     ok( GetHandleInformation( d1, &flags ), "GetHandleInformation failed\n" );
158     ok( !(flags & HANDLE_FLAG_PROTECT_FROM_CLOSE), "handle %p PROTECT_FROM_CLOSE set\n", d1 );
159
160     SetLastError( 0xdeadbeef );
161     ok( !CloseDesktop(d1), "closing thread desktop succeeded\n" );
162     ok( GetLastError() == ERROR_BUSY, "bad last error %ld\n", GetLastError() );
163
164     SetLastError( 0xdeadbeef );
165     ok( !CloseHandle(d1), "closing thread desktop handle failed\n" );
166     ok( GetLastError() == ERROR_INVALID_HANDLE, "bad last error %ld\n", GetLastError() );
167
168     ok( DuplicateHandle( GetCurrentProcess(), d1, GetCurrentProcess(), (PHANDLE)&d2, 0,
169                          TRUE, DUPLICATE_SAME_ACCESS ), "DuplicateHandle failed\n" );
170     ok( CloseDesktop(d2), "closing dup desktop failed\n" );
171
172     ok( DuplicateHandle( GetCurrentProcess(), d1, GetCurrentProcess(), (PHANDLE)&d2, 0,
173                          TRUE, DUPLICATE_SAME_ACCESS ), "DuplicateHandle failed\n" );
174     ok( CloseHandle(d2), "closing dup desktop handle failed\n" );
175
176     d2 = OpenDesktop( "dummy name", 0, TRUE, DESKTOP_ALL_ACCESS );
177     ok( !d2, "open dummy desktop succeeded\n" );
178
179     d2 = CreateDesktop( "foobar", NULL, NULL, 0, DESKTOP_ALL_ACCESS, NULL );
180     ok( d2 != 0, "create foobar desktop failed\n" );
181     SetLastError( 0xdeadbeef );
182     ok( !CloseWindowStation( (HWINSTA)d2 ), "CloseWindowStation succeeded on desktop\n" );
183     ok( GetLastError() == ERROR_INVALID_HANDLE, "bad last error %ld\n", GetLastError() );
184
185     d3 = OpenDesktop( "foobar", 0, TRUE, DESKTOP_ALL_ACCESS );
186     ok( d3 != 0, "open foobar desktop failed\n" );
187     ok( d3 != d2, "open foobar desktop returned same handle\n" );
188     ok( CloseDesktop( d2 ), "CloseDesktop failed\n" );
189     ok( CloseDesktop( d3 ), "CloseDesktop failed\n" );
190
191     d3 = OpenDesktop( "foobar", 0, TRUE, DESKTOP_ALL_ACCESS );
192     ok( !d3, "open foobar desktop succeeded\n" );
193
194     ok( !CloseHandle(d1), "closing thread desktop handle succeeded\n" );
195     d2 = GetThreadDesktop(GetCurrentThreadId());
196     ok( d1 == d2, "got different handles after close\n" );
197
198     trace( "thread 1 desktop: %p\n", d1 );
199     print_object( d1 );
200     hthread = CreateThread( NULL, 0, thread, (LPVOID)2, 0, &id );
201     Sleep(1000);
202     trace( "get other thread desktop: %p\n", GetThreadDesktop(id) );
203     WaitForSingleObject( hthread, INFINITE );
204     CloseHandle( hthread );
205 }
206
207 START_TEST(winstation)
208 {
209     test_handles();
210 }