winedbg: Don't dereference possibly NULL thread pointer.
[wine/zf.git] / dlls / dpnet / tests / server.c
blob74a0e5adfcfe3d7fbd56eacde239eeb83f98c1ec
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[] = L"winegamesserver";
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[] = L"wine";
110 WCHAR name2[] = L"wine2";
111 WCHAR data[] = L"XXXX";
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 HRESULT hr, init;
239 INetFwMgr *mgr = NULL;
240 INetFwPolicy *policy = NULL;
241 INetFwProfile *profile = NULL;
242 INetFwAuthorizedApplication *app = NULL;
243 INetFwAuthorizedApplications *apps = NULL;
244 BSTR name, image = SysAllocStringLen( NULL, MAX_PATH );
245 WCHAR path[MAX_PATH];
247 if (!GetModuleFileNameW( NULL, image, MAX_PATH ))
249 SysFreeString( image );
250 return E_FAIL;
253 if(!GetSystemDirectoryW(path, MAX_PATH))
255 SysFreeString( image );
256 return E_FAIL;
258 lstrcatW(path, L"\\dpnsvr.exe");
260 init = CoInitializeEx( 0, COINIT_APARTMENTTHREADED );
262 hr = CoCreateInstance( &CLSID_NetFwMgr, NULL, CLSCTX_INPROC_SERVER, &IID_INetFwMgr,
263 (void **)&mgr );
264 ok( hr == S_OK, "got %08x\n", hr );
265 if (hr != S_OK) goto done;
267 hr = INetFwMgr_get_LocalPolicy( mgr, &policy );
268 ok( hr == S_OK, "got %08x\n", hr );
269 if (hr != S_OK) goto done;
271 hr = INetFwPolicy_get_CurrentProfile( policy, &profile );
272 if (hr != S_OK) goto done;
274 hr = INetFwProfile_get_AuthorizedApplications( profile, &apps );
275 ok( hr == S_OK, "got %08x\n", hr );
276 if (hr != S_OK) goto done;
278 hr = CoCreateInstance( &CLSID_NetFwAuthorizedApplication, NULL, CLSCTX_INPROC_SERVER,
279 &IID_INetFwAuthorizedApplication, (void **)&app );
280 ok( hr == S_OK, "got %08x\n", hr );
281 if (hr != S_OK) goto done;
283 hr = INetFwAuthorizedApplication_put_ProcessImageFileName( app, image );
284 if (hr != S_OK) goto done;
286 name = SysAllocString( L"dpnet_client" );
287 hr = INetFwAuthorizedApplication_put_Name( app, name );
288 SysFreeString( name );
289 ok( hr == S_OK, "got %08x\n", hr );
290 if (hr != S_OK) goto done;
292 if (op == APP_ADD)
293 hr = INetFwAuthorizedApplications_Add( apps, app );
294 else if (op == APP_REMOVE)
295 hr = INetFwAuthorizedApplications_Remove( apps, image );
296 else
297 hr = E_INVALIDARG;
298 if (hr != S_OK) goto done;
300 INetFwAuthorizedApplication_Release( app );
301 hr = CoCreateInstance( &CLSID_NetFwAuthorizedApplication, NULL, CLSCTX_INPROC_SERVER,
302 &IID_INetFwAuthorizedApplication, (void **)&app );
303 ok( hr == S_OK, "got %08x\n", hr );
304 if (hr != S_OK) goto done;
306 SysFreeString( image );
307 image = SysAllocString( path );
308 hr = INetFwAuthorizedApplication_put_ProcessImageFileName( app, image );
309 if (hr != S_OK) goto done;
311 name = SysAllocString( L"dpnet_server" );
312 hr = INetFwAuthorizedApplication_put_Name( app, name );
313 SysFreeString( name );
314 ok( hr == S_OK, "got %08x\n", hr );
315 if (hr != S_OK) goto done;
317 if (op == APP_ADD)
318 hr = INetFwAuthorizedApplications_Add( apps, app );
319 else if (op == APP_REMOVE)
320 hr = INetFwAuthorizedApplications_Remove( apps, image );
322 done:
323 if (app) INetFwAuthorizedApplication_Release( app );
324 if (apps) INetFwAuthorizedApplications_Release( apps );
325 if (policy) INetFwPolicy_Release( policy );
326 if (profile) INetFwProfile_Release( profile );
327 if (mgr) INetFwMgr_Release( mgr );
328 if (SUCCEEDED( init )) CoUninitialize();
329 SysFreeString( image );
330 return hr;
333 /* taken from programs/winetest/main.c */
334 BOOL is_stub_dll(const char *filename)
336 DWORD size, ver;
337 BOOL isstub = FALSE;
338 char *p, *data;
340 size = GetFileVersionInfoSizeA(filename, &ver);
341 if (!size) return FALSE;
343 data = HeapAlloc(GetProcessHeap(), 0, size);
344 if (!data) return FALSE;
346 if (GetFileVersionInfoA(filename, ver, size, data))
348 char buf[256];
350 sprintf(buf, "\\StringFileInfo\\%04x%04x\\OriginalFilename", MAKELANGID(LANG_ENGLISH, SUBLANG_ENGLISH_US), 1200);
351 if (VerQueryValueA(data, buf, (void**)&p, &size))
352 isstub = !lstrcmpiA("wcodstub.dll", p);
354 HeapFree(GetProcessHeap(), 0, data);
356 return isstub;
359 START_TEST(server)
361 HRESULT hr;
362 BOOL firewall_enabled;
363 char path[MAX_PATH];
365 if(!GetSystemDirectoryA(path, MAX_PATH))
367 skip("Failed to get systems directory\n");
368 return;
370 strcat(path, "\\dpnet.dll");
372 if (!winetest_interactive && is_stub_dll(path))
374 win_skip("dpnet is a stub dll, skipping tests\n");
375 return;
378 if ((firewall_enabled = is_firewall_enabled()) && !is_process_elevated())
380 skip("no privileges, skipping tests to avoid firewall dialog\n");
381 return;
384 if (firewall_enabled)
386 HRESULT hr = set_firewall(APP_ADD);
387 if (hr != S_OK)
389 skip("can't authorize app in firewall %08x\n", hr);
390 return;
394 hr = CoInitialize(0);
395 ok( hr == S_OK, "failed to init com\n");
396 if (hr != S_OK)
397 goto done;
399 create_server();
400 test_server_info();
402 CoUninitialize();
404 done:
405 if (firewall_enabled) set_firewall(APP_REMOVE);