ntdll: Add a wrapper to call the unhandled exception filter.
[wine.git] / dlls / dpnet / tests / server.c
blob698082d3825c1cde1c2774a91ff02f6819ac69fe
1 /*
2 * Copyright 2014 Alistair Leslie-Hughes
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19 #define WIN32_LEAN_AND_MEAN
20 #include <stdio.h>
22 #include <dplay8.h>
23 #define COBJMACROS
24 #include <netfw.h>
25 #include "wine/test.h"
27 #include "dpnet_test.h"
29 /* {CD0C3D4B-E15E-4CF2-9EA8-6E1D6548C5A5} */
30 static const GUID appguid = { 0xcd0c3d4b, 0xe15e, 0x4cf2, { 0x9e, 0xa8, 0x6e, 0x1d, 0x65, 0x48, 0xc5, 0xa5 } };
31 static WCHAR sessionname[] = {'w','i','n','e','g','a','m','e','s','s','e','r','v','e','r',0};
33 static BOOL nCreatePlayer;
34 static BOOL nDestroyPlayer;
36 static HRESULT WINAPI DirectPlayMessageHandler(PVOID pvUserContext, DWORD dwMessageId, PVOID pMsgBuffer)
38 trace("msgid: 0x%08x\n", dwMessageId);
40 switch(dwMessageId)
42 case DPN_MSGID_CREATE_PLAYER:
43 nCreatePlayer = TRUE;
44 break;
45 case DPN_MSGID_DESTROY_PLAYER:
46 nDestroyPlayer = TRUE;
47 break;
50 return S_OK;
53 static void create_server(void)
55 HRESULT hr;
56 IDirectPlay8Server *server = NULL;
58 hr = CoCreateInstance( &CLSID_DirectPlay8Server, NULL, CLSCTX_ALL, &IID_IDirectPlay8Server, (LPVOID*)&server);
59 ok(hr == S_OK, "Failed to create IDirectPlay8Server object\n");
60 if( SUCCEEDED(hr) )
62 hr = IDirectPlay8Server_Close(server, 0);
63 todo_wine ok(hr == DPNERR_UNINITIALIZED, "got 0x%08x\n", hr);
65 hr = IDirectPlay8Server_Initialize(server, NULL, NULL, 0);
66 ok(hr == DPNERR_INVALIDPARAM, "got 0x%08x\n", hr);
68 hr = IDirectPlay8Server_Initialize(server, NULL, DirectPlayMessageHandler, 0);
69 ok(hr == S_OK, "got 0x%08x\n", hr);
70 if(hr == S_OK)
72 IDirectPlay8Address *localaddr = NULL;
73 DPN_APPLICATION_DESC appdesc;
75 hr = CoCreateInstance( &CLSID_DirectPlay8Address, NULL, CLSCTX_ALL, &IID_IDirectPlay8Address, (LPVOID*)&localaddr);
76 ok(hr == S_OK, "Failed to create IDirectPlay8Address object\n");
78 hr = IDirectPlay8Address_SetSP(localaddr, &CLSID_DP8SP_TCPIP);
79 ok(hr == S_OK, "got 0x%08x\n", hr);
81 memset( &appdesc, 0, sizeof(DPN_APPLICATION_DESC) );
82 appdesc.dwSize = sizeof( DPN_APPLICATION_DESC );
83 appdesc.dwFlags = DPNSESSION_CLIENT_SERVER;
84 appdesc.guidApplication = appguid;
85 appdesc.pwszSessionName = sessionname;
87 hr = IDirectPlay8Server_Host(server, &appdesc, &localaddr, 1, NULL, NULL, NULL, 0);
88 todo_wine ok(hr == S_OK, "got 0x%08x\n", hr);
90 todo_wine ok(nCreatePlayer, "No DPN_MSGID_CREATE_PLAYER Message\n");
91 ok(!nDestroyPlayer, "Received DPN_MSGID_DESTROY_PLAYER Message\n");
93 hr = IDirectPlay8Server_Close(server, 0);
94 todo_wine ok(hr == S_OK, "got 0x%08x\n", hr);
96 todo_wine ok(nDestroyPlayer, "No DPN_MSGID_DESTROY_PLAYER Message\n");
98 IDirectPlay8Address_Release(localaddr);
101 IDirectPlay8Server_Release(server);
105 static void test_server_info(void)
107 HRESULT hr;
108 DPN_PLAYER_INFO info;
109 WCHAR name[] = {'w','i','n','e',0};
110 WCHAR name2[] = {'w','i','n','e','2',0};
111 WCHAR data[] = {'X','X','X','X',0};
112 IDirectPlay8Server *server = NULL;
114 hr = CoCreateInstance( &CLSID_DirectPlay8Server, NULL, CLSCTX_ALL, &IID_IDirectPlay8Server, (LPVOID*)&server);
115 ok(hr == S_OK, "Failed to create IDirectPlay8Server object\n");
116 if( SUCCEEDED(hr) )
118 ZeroMemory( &info, sizeof(DPN_PLAYER_INFO) );
119 info.dwSize = sizeof(DPN_PLAYER_INFO);
120 info.dwInfoFlags = DPNINFO_NAME;
122 hr = IDirectPlay8Server_SetServerInfo(server, NULL, NULL, NULL, DPNSETSERVERINFO_SYNC);
123 ok(hr == E_POINTER, "got %x\n", hr);
125 info.pwszName = name;
126 hr = IDirectPlay8Server_SetServerInfo(server, &info, NULL, NULL, DPNSETSERVERINFO_SYNC);
127 ok(hr == DPNERR_UNINITIALIZED, "got %x\n", hr);
129 hr = IDirectPlay8Server_Initialize(server, NULL, DirectPlayMessageHandler, 0);
130 ok(hr == S_OK, "got 0x%08x\n", hr);
132 hr = IDirectPlay8Server_SetServerInfo(server, NULL, NULL, NULL, DPNSETSERVERINFO_SYNC);
133 ok(hr == E_POINTER, "got %x\n", hr);
135 info.pwszName = NULL;
136 hr = IDirectPlay8Server_SetServerInfo(server, &info, NULL, NULL, DPNSETSERVERINFO_SYNC);
137 ok(hr == S_OK, "got %x\n", hr);
139 info.pwszName = name;
140 hr = IDirectPlay8Server_SetServerInfo(server, &info, NULL, NULL, DPNSETSERVERINFO_SYNC);
141 ok(hr == S_OK, "got %x\n", hr);
143 info.dwInfoFlags = DPNINFO_NAME;
144 info.pwszName = name2;
145 hr = IDirectPlay8Server_SetServerInfo(server, &info, NULL, NULL, DPNSETSERVERINFO_SYNC);
146 ok(hr == S_OK, "got %x\n", hr);
148 info.dwInfoFlags = DPNINFO_DATA;
149 info.pwszName = NULL;
150 info.pvData = NULL;
151 info.dwDataSize = sizeof(data);
152 hr = IDirectPlay8Server_SetServerInfo(server, &info, NULL, NULL, DPNSETSERVERINFO_SYNC);
153 ok(hr == E_POINTER, "got %x\n", hr);
155 info.dwInfoFlags = DPNINFO_DATA;
156 info.pwszName = NULL;
157 info.pvData = data;
158 info.dwDataSize = 0;
159 hr = IDirectPlay8Server_SetServerInfo(server, &info, NULL, NULL, DPNSETSERVERINFO_SYNC);
160 ok(hr == S_OK, "got %x\n", hr);
162 info.dwInfoFlags = DPNINFO_DATA;
163 info.pwszName = NULL;
164 info.pvData = data;
165 info.dwDataSize = sizeof(data);
166 hr = IDirectPlay8Server_SetServerInfo(server, &info, NULL, NULL, DPNSETSERVERINFO_SYNC);
167 ok(hr == S_OK, "got %x\n", hr);
169 info.dwInfoFlags = DPNINFO_DATA | DPNINFO_NAME;
170 info.pwszName = name;
171 info.pvData = data;
172 info.dwDataSize = sizeof(data);
173 hr = IDirectPlay8Server_SetServerInfo(server, &info, NULL, NULL, DPNSETSERVERINFO_SYNC);
174 ok(hr == S_OK, "got %x\n", hr);
176 info.dwInfoFlags = DPNINFO_DATA | DPNINFO_NAME;
177 info.pwszName = name;
178 info.pvData = NULL;
179 info.dwDataSize = 0;
180 hr = IDirectPlay8Server_SetServerInfo(server, &info, NULL, NULL, DPNSETSERVERINFO_SYNC);
181 ok(hr == S_OK, "got %x\n", hr);
183 IDirectPlay8Server_Release(server);
187 BOOL is_process_elevated(void)
189 HANDLE token;
190 if (OpenProcessToken( GetCurrentProcess(), TOKEN_QUERY, &token ))
192 TOKEN_ELEVATION_TYPE type;
193 DWORD size;
194 BOOL ret;
196 ret = GetTokenInformation( token, TokenElevationType, &type, sizeof(type), &size );
197 CloseHandle( token );
198 return (ret && type == TokenElevationTypeFull);
200 return FALSE;
203 BOOL is_firewall_enabled(void)
205 HRESULT hr, init;
206 INetFwMgr *mgr = NULL;
207 INetFwPolicy *policy = NULL;
208 INetFwProfile *profile = NULL;
209 VARIANT_BOOL enabled = VARIANT_FALSE;
211 init = CoInitializeEx( 0, COINIT_APARTMENTTHREADED );
213 hr = CoCreateInstance( &CLSID_NetFwMgr, NULL, CLSCTX_INPROC_SERVER, &IID_INetFwMgr,
214 (void **)&mgr );
215 ok( hr == S_OK, "got %08x\n", hr );
216 if (hr != S_OK) goto done;
218 hr = INetFwMgr_get_LocalPolicy( mgr, &policy );
219 ok( hr == S_OK, "got %08x\n", hr );
220 if (hr != S_OK) goto done;
222 hr = INetFwPolicy_get_CurrentProfile( policy, &profile );
223 if (hr != S_OK) goto done;
225 hr = INetFwProfile_get_FirewallEnabled( profile, &enabled );
226 ok( hr == S_OK, "got %08x\n", hr );
228 done:
229 if (policy) INetFwPolicy_Release( policy );
230 if (profile) INetFwProfile_Release( profile );
231 if (mgr) INetFwMgr_Release( mgr );
232 if (SUCCEEDED( init )) CoUninitialize();
233 return (enabled == VARIANT_TRUE);
236 HRESULT set_firewall( enum firewall_op op )
238 static const WCHAR dpnsvrW[] =
239 {'d','p','n','s','v','r','.','e','x','e',0};
240 static const WCHAR separator[] = {'\\',0};
241 static const WCHAR clientW[] =
242 {'d','p','n','e','t','_','c','l','i','e','n','t',0};
243 static const WCHAR serverW[] =
244 {'d','p','n','e','t','_','s','e','r','v','e','r',0};
245 HRESULT hr, init;
246 INetFwMgr *mgr = NULL;
247 INetFwPolicy *policy = NULL;
248 INetFwProfile *profile = NULL;
249 INetFwAuthorizedApplication *app = NULL;
250 INetFwAuthorizedApplications *apps = NULL;
251 BSTR name, image = SysAllocStringLen( NULL, MAX_PATH );
252 WCHAR path[MAX_PATH];
254 if (!GetModuleFileNameW( NULL, image, MAX_PATH ))
256 SysFreeString( image );
257 return E_FAIL;
260 if(!GetSystemDirectoryW(path, MAX_PATH))
262 SysFreeString( image );
263 return E_FAIL;
265 lstrcatW(path, separator);
266 lstrcatW(path, dpnsvrW);
268 init = CoInitializeEx( 0, COINIT_APARTMENTTHREADED );
270 hr = CoCreateInstance( &CLSID_NetFwMgr, NULL, CLSCTX_INPROC_SERVER, &IID_INetFwMgr,
271 (void **)&mgr );
272 ok( hr == S_OK, "got %08x\n", hr );
273 if (hr != S_OK) goto done;
275 hr = INetFwMgr_get_LocalPolicy( mgr, &policy );
276 ok( hr == S_OK, "got %08x\n", hr );
277 if (hr != S_OK) goto done;
279 hr = INetFwPolicy_get_CurrentProfile( policy, &profile );
280 if (hr != S_OK) goto done;
282 hr = INetFwProfile_get_AuthorizedApplications( profile, &apps );
283 ok( hr == S_OK, "got %08x\n", hr );
284 if (hr != S_OK) goto done;
286 hr = CoCreateInstance( &CLSID_NetFwAuthorizedApplication, NULL, CLSCTX_INPROC_SERVER,
287 &IID_INetFwAuthorizedApplication, (void **)&app );
288 ok( hr == S_OK, "got %08x\n", hr );
289 if (hr != S_OK) goto done;
291 hr = INetFwAuthorizedApplication_put_ProcessImageFileName( app, image );
292 if (hr != S_OK) goto done;
294 name = SysAllocString( clientW );
295 hr = INetFwAuthorizedApplication_put_Name( app, name );
296 SysFreeString( name );
297 ok( hr == S_OK, "got %08x\n", hr );
298 if (hr != S_OK) goto done;
300 if (op == APP_ADD)
301 hr = INetFwAuthorizedApplications_Add( apps, app );
302 else if (op == APP_REMOVE)
303 hr = INetFwAuthorizedApplications_Remove( apps, image );
304 else
305 hr = E_INVALIDARG;
306 if (hr != S_OK) goto done;
308 INetFwAuthorizedApplication_Release( app );
309 hr = CoCreateInstance( &CLSID_NetFwAuthorizedApplication, NULL, CLSCTX_INPROC_SERVER,
310 &IID_INetFwAuthorizedApplication, (void **)&app );
311 ok( hr == S_OK, "got %08x\n", hr );
312 if (hr != S_OK) goto done;
314 SysFreeString( image );
315 image = SysAllocString( path );
316 hr = INetFwAuthorizedApplication_put_ProcessImageFileName( app, image );
317 if (hr != S_OK) goto done;
319 name = SysAllocString( serverW );
320 hr = INetFwAuthorizedApplication_put_Name( app, name );
321 SysFreeString( name );
322 ok( hr == S_OK, "got %08x\n", hr );
323 if (hr != S_OK) goto done;
325 if (op == APP_ADD)
326 hr = INetFwAuthorizedApplications_Add( apps, app );
327 else if (op == APP_REMOVE)
328 hr = INetFwAuthorizedApplications_Remove( apps, image );
329 else
330 hr = E_INVALIDARG;
332 done:
333 if (app) INetFwAuthorizedApplication_Release( app );
334 if (apps) INetFwAuthorizedApplications_Release( apps );
335 if (policy) INetFwPolicy_Release( policy );
336 if (profile) INetFwProfile_Release( profile );
337 if (mgr) INetFwMgr_Release( mgr );
338 if (SUCCEEDED( init )) CoUninitialize();
339 SysFreeString( image );
340 return hr;
343 /* taken from programs/winetest/main.c */
344 BOOL is_stub_dll(const char *filename)
346 DWORD size, ver;
347 BOOL isstub = FALSE;
348 char *p, *data;
350 size = GetFileVersionInfoSizeA(filename, &ver);
351 if (!size) return FALSE;
353 data = HeapAlloc(GetProcessHeap(), 0, size);
354 if (!data) return FALSE;
356 if (GetFileVersionInfoA(filename, ver, size, data))
358 char buf[256];
360 sprintf(buf, "\\StringFileInfo\\%04x%04x\\OriginalFilename", MAKELANGID(LANG_ENGLISH, SUBLANG_ENGLISH_US), 1200);
361 if (VerQueryValueA(data, buf, (void**)&p, &size))
362 isstub = !lstrcmpiA("wcodstub.dll", p);
364 HeapFree(GetProcessHeap(), 0, data);
366 return isstub;
369 START_TEST(server)
371 HRESULT hr;
372 BOOL firewall_enabled;
373 char path[MAX_PATH];
375 if(!GetSystemDirectoryA(path, MAX_PATH))
377 skip("Failed to get systems directory\n");
378 return;
380 strcat(path, "\\dpnet.dll");
382 if (!winetest_interactive && is_stub_dll(path))
384 win_skip("dpnet is a stub dll, skipping tests\n");
385 return;
388 if ((firewall_enabled = is_firewall_enabled()) && !is_process_elevated())
390 skip("no privileges, skipping tests to avoid firewall dialog\n");
391 return;
394 if (firewall_enabled)
396 HRESULT hr = set_firewall(APP_ADD);
397 if (hr != S_OK)
399 skip("can't authorize app in firewall %08x\n", hr);
400 return;
404 hr = CoInitialize(0);
405 ok( hr == S_OK, "failed to init com\n");
406 if (hr != S_OK)
407 goto done;
409 create_server();
410 test_server_info();
412 CoUninitialize();
414 done:
415 if (firewall_enabled) set_firewall(APP_REMOVE);