widl: Add support for function parameter flags to SLTG typelib generator.
[wine.git] / dlls / dpnet / tests / server.c
blob94a9da0d47f65f7eb0ee9445797aa39785fceb5a
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%08lx\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%08lx\n", hr);
65 hr = IDirectPlay8Server_Initialize(server, NULL, NULL, 0);
66 ok(hr == DPNERR_INVALIDPARAM, "got 0x%08lx\n", hr);
68 hr = IDirectPlay8Server_Initialize(server, NULL, DirectPlayMessageHandler, 0);
69 ok(hr == S_OK, "got 0x%08lx\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%08lx\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%08lx\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%08lx\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 %lx\n", hr);
125 info.pwszName = name;
126 hr = IDirectPlay8Server_SetServerInfo(server, &info, NULL, NULL, DPNSETSERVERINFO_SYNC);
127 ok(hr == DPNERR_UNINITIALIZED, "got %lx\n", hr);
129 hr = IDirectPlay8Server_Initialize(server, NULL, DirectPlayMessageHandler, 0);
130 ok(hr == S_OK, "got 0x%08lx\n", hr);
132 hr = IDirectPlay8Server_SetServerInfo(server, NULL, NULL, NULL, DPNSETSERVERINFO_SYNC);
133 ok(hr == E_POINTER, "got %lx\n", hr);
135 info.pwszName = NULL;
136 hr = IDirectPlay8Server_SetServerInfo(server, &info, NULL, NULL, DPNSETSERVERINFO_SYNC);
137 ok(hr == S_OK, "got %lx\n", hr);
139 info.pwszName = name;
140 hr = IDirectPlay8Server_SetServerInfo(server, &info, NULL, NULL, DPNSETSERVERINFO_SYNC);
141 ok(hr == S_OK, "got %lx\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 %lx\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 %lx\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 %lx\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 %lx\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 %lx\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 %lx\n", hr);
183 IDirectPlay8Server_Release(server);
187 static void test_enum_service_providers(void)
189 DPN_SERVICE_PROVIDER_INFO *serv_prov_info = NULL;
190 IDirectPlay8Server *server = NULL;
191 DWORD items, size;
192 DWORD i;
193 HRESULT hr;
195 hr = CoCreateInstance( &CLSID_DirectPlay8Server, NULL, CLSCTX_ALL, &IID_IDirectPlay8Server, (LPVOID*)&server);
196 ok(hr == S_OK, "Failed to create IDirectPlay8Server object\n");
197 if (FAILED(hr))
198 return;
200 size = 0;
201 items = 0;
202 hr = IDirectPlay8Server_EnumServiceProviders(server, NULL, NULL, serv_prov_info, &size, &items, 0);
203 ok(hr == DPNERR_UNINITIALIZED, "got %lx\n", hr);
205 hr = IDirectPlay8Server_Initialize(server, NULL, DirectPlayMessageHandler, 0);
206 ok(hr == S_OK, "got 0x%08lx\n", hr);
207 if (FAILED(hr))
209 IDirectPlay8Server_Release(server);
210 return;
213 size = 0;
214 items = 0;
216 hr = IDirectPlay8Server_EnumServiceProviders(server, NULL, NULL, NULL, &size, NULL, 0);
217 ok(hr == E_POINTER, "IDirectPlay8Server_EnumServiceProviders failed with %lx\n", hr);
219 hr = IDirectPlay8Server_EnumServiceProviders(server, NULL, NULL, NULL, NULL, &items, 0);
220 ok(hr == E_POINTER, "IDirectPlay8Server_EnumServiceProviders failed with %lx\n", hr);
222 hr = IDirectPlay8Server_EnumServiceProviders(server, NULL, NULL, NULL, &size, &items, 0);
223 ok(hr == DPNERR_BUFFERTOOSMALL, "IDirectPlay8Server_EnumServiceProviders failed with %lx\n", hr);
224 ok(size != 0, "size is unexpectedly 0\n");
226 serv_prov_info = HeapAlloc(GetProcessHeap(), 0, size);
228 hr = IDirectPlay8Server_EnumServiceProviders(server, NULL, NULL, serv_prov_info, &size, &items, 0);
229 ok(hr == S_OK, "IDirectPlay8Server_EnumServiceProviders failed with %lx\n", hr);
230 ok(items != 0, "Found unexpectedly no service providers\n");
232 trace("number of items found: %ld\n", items);
234 for (i=0;i<items;i++)
236 trace("Found Service Provider: %s\n", wine_dbgstr_w(serv_prov_info[i].pwszName));
237 trace("Found guid: %s\n", wine_dbgstr_guid(&serv_prov_info[i].guid));
240 ok(HeapFree(GetProcessHeap(), 0, serv_prov_info), "Failed freeing server provider info\n");
242 size = 0;
243 items = 0;
245 hr = IDirectPlay8Server_EnumServiceProviders(server, &CLSID_DP8SP_TCPIP, NULL, NULL, &size, &items, 0);
246 ok(hr == DPNERR_BUFFERTOOSMALL, "IDirectPlay8Server_EnumServiceProviders failed with %lx\n", hr);
247 ok(size != 0, "size is unexpectedly 0\n");
249 serv_prov_info = HeapAlloc(GetProcessHeap(), 0, size);
251 hr = IDirectPlay8Server_EnumServiceProviders(server, &CLSID_DP8SP_TCPIP, NULL, serv_prov_info, &size, &items, 0);
252 ok(hr == S_OK, "IDirectPlay8Server_EnumServiceProviders failed with %lx\n", hr);
253 ok(items != 0, "Found unexpectedly no adapter\n");
256 for (i=0;i<items;i++)
258 trace("Found adapter: %s\n", wine_dbgstr_w(serv_prov_info[i].pwszName));
259 trace("Found adapter guid: %s\n", wine_dbgstr_guid(&serv_prov_info[i].guid));
262 /* Invalid GUID */
263 items = 88;
264 hr = IDirectPlay8Server_EnumServiceProviders(server, &appguid, NULL, serv_prov_info, &size, &items, 0);
265 ok(hr == DPNERR_DOESNOTEXIST, "IDirectPlay8Peer_EnumServiceProviders failed with %lx\n", hr);
266 ok(items == 88, "Found adapter %ld\n", items);
268 HeapFree(GetProcessHeap(), 0, serv_prov_info);
269 IDirectPlay8Server_Release(server);
272 BOOL is_process_elevated(void)
274 HANDLE token;
275 if (OpenProcessToken( GetCurrentProcess(), TOKEN_QUERY, &token ))
277 TOKEN_ELEVATION_TYPE type;
278 DWORD size;
279 BOOL ret;
281 ret = GetTokenInformation( token, TokenElevationType, &type, sizeof(type), &size );
282 CloseHandle( token );
283 return (ret && type == TokenElevationTypeFull);
285 return FALSE;
288 BOOL is_firewall_enabled(void)
290 HRESULT hr, init;
291 INetFwMgr *mgr = NULL;
292 INetFwPolicy *policy = NULL;
293 INetFwProfile *profile = NULL;
294 VARIANT_BOOL enabled = VARIANT_FALSE;
296 init = CoInitializeEx( 0, COINIT_APARTMENTTHREADED );
298 hr = CoCreateInstance( &CLSID_NetFwMgr, NULL, CLSCTX_INPROC_SERVER, &IID_INetFwMgr,
299 (void **)&mgr );
300 ok( hr == S_OK, "got %08lx\n", hr );
301 if (hr != S_OK) goto done;
303 hr = INetFwMgr_get_LocalPolicy( mgr, &policy );
304 ok( hr == S_OK, "got %08lx\n", hr );
305 if (hr != S_OK) goto done;
307 hr = INetFwPolicy_get_CurrentProfile( policy, &profile );
308 if (hr != S_OK) goto done;
310 hr = INetFwProfile_get_FirewallEnabled( profile, &enabled );
311 ok( hr == S_OK, "got %08lx\n", hr );
313 done:
314 if (policy) INetFwPolicy_Release( policy );
315 if (profile) INetFwProfile_Release( profile );
316 if (mgr) INetFwMgr_Release( mgr );
317 if (SUCCEEDED( init )) CoUninitialize();
318 return (enabled == VARIANT_TRUE);
321 HRESULT set_firewall( enum firewall_op op )
323 HRESULT hr, init;
324 INetFwMgr *mgr = NULL;
325 INetFwPolicy *policy = NULL;
326 INetFwProfile *profile = NULL;
327 INetFwAuthorizedApplication *app = NULL;
328 INetFwAuthorizedApplications *apps = NULL;
329 BSTR name, image = SysAllocStringLen( NULL, MAX_PATH );
330 WCHAR path[MAX_PATH];
332 if (!GetModuleFileNameW( NULL, image, MAX_PATH ))
334 SysFreeString( image );
335 return E_FAIL;
338 if(!GetSystemDirectoryW(path, MAX_PATH))
340 SysFreeString( image );
341 return E_FAIL;
343 lstrcatW(path, L"\\dpnsvr.exe");
345 init = CoInitializeEx( 0, COINIT_APARTMENTTHREADED );
347 hr = CoCreateInstance( &CLSID_NetFwMgr, NULL, CLSCTX_INPROC_SERVER, &IID_INetFwMgr,
348 (void **)&mgr );
349 ok( hr == S_OK, "got %08lx\n", hr );
350 if (hr != S_OK) goto done;
352 hr = INetFwMgr_get_LocalPolicy( mgr, &policy );
353 ok( hr == S_OK, "got %08lx\n", hr );
354 if (hr != S_OK) goto done;
356 hr = INetFwPolicy_get_CurrentProfile( policy, &profile );
357 if (hr != S_OK) goto done;
359 hr = INetFwProfile_get_AuthorizedApplications( profile, &apps );
360 ok( hr == S_OK, "got %08lx\n", hr );
361 if (hr != S_OK) goto done;
363 hr = CoCreateInstance( &CLSID_NetFwAuthorizedApplication, NULL, CLSCTX_INPROC_SERVER,
364 &IID_INetFwAuthorizedApplication, (void **)&app );
365 ok( hr == S_OK, "got %08lx\n", hr );
366 if (hr != S_OK) goto done;
368 hr = INetFwAuthorizedApplication_put_ProcessImageFileName( app, image );
369 if (hr != S_OK) goto done;
371 name = SysAllocString( L"dpnet_client" );
372 hr = INetFwAuthorizedApplication_put_Name( app, name );
373 SysFreeString( name );
374 ok( hr == S_OK, "got %08lx\n", hr );
375 if (hr != S_OK) goto done;
377 if (op == APP_ADD)
378 hr = INetFwAuthorizedApplications_Add( apps, app );
379 else if (op == APP_REMOVE)
380 hr = INetFwAuthorizedApplications_Remove( apps, image );
381 else
382 hr = E_INVALIDARG;
383 if (hr != S_OK) goto done;
385 INetFwAuthorizedApplication_Release( app );
386 hr = CoCreateInstance( &CLSID_NetFwAuthorizedApplication, NULL, CLSCTX_INPROC_SERVER,
387 &IID_INetFwAuthorizedApplication, (void **)&app );
388 ok( hr == S_OK, "got %08lx\n", hr );
389 if (hr != S_OK) goto done;
391 SysFreeString( image );
392 image = SysAllocString( path );
393 hr = INetFwAuthorizedApplication_put_ProcessImageFileName( app, image );
394 if (hr != S_OK) goto done;
396 name = SysAllocString( L"dpnet_server" );
397 hr = INetFwAuthorizedApplication_put_Name( app, name );
398 SysFreeString( name );
399 ok( hr == S_OK, "got %08lx\n", hr );
400 if (hr != S_OK) goto done;
402 if (op == APP_ADD)
403 hr = INetFwAuthorizedApplications_Add( apps, app );
404 else if (op == APP_REMOVE)
405 hr = INetFwAuthorizedApplications_Remove( apps, image );
407 done:
408 if (app) INetFwAuthorizedApplication_Release( app );
409 if (apps) INetFwAuthorizedApplications_Release( apps );
410 if (policy) INetFwPolicy_Release( policy );
411 if (profile) INetFwProfile_Release( profile );
412 if (mgr) INetFwMgr_Release( mgr );
413 if (SUCCEEDED( init )) CoUninitialize();
414 SysFreeString( image );
415 return hr;
418 /* taken from programs/winetest/main.c */
419 BOOL is_stub_dll(const char *filename)
421 UINT size;
422 DWORD ver;
423 BOOL isstub = FALSE;
424 char *p, *data;
426 size = GetFileVersionInfoSizeA(filename, &ver);
427 if (!size) return FALSE;
429 data = HeapAlloc(GetProcessHeap(), 0, size);
430 if (!data) return FALSE;
432 if (GetFileVersionInfoA(filename, ver, size, data))
434 char buf[256];
436 sprintf(buf, "\\StringFileInfo\\%04x%04x\\OriginalFilename", MAKELANGID(LANG_ENGLISH, SUBLANG_ENGLISH_US), 1200);
437 if (VerQueryValueA(data, buf, (void**)&p, &size))
438 isstub = !lstrcmpiA("wcodstub.dll", p);
440 HeapFree(GetProcessHeap(), 0, data);
442 return isstub;
445 START_TEST(server)
447 HRESULT hr;
448 BOOL firewall_enabled;
449 char path[MAX_PATH];
451 if(!GetSystemDirectoryA(path, MAX_PATH))
453 skip("Failed to get systems directory\n");
454 return;
456 strcat(path, "\\dpnet.dll");
458 if (!winetest_interactive && is_stub_dll(path))
460 win_skip("dpnet is a stub dll, skipping tests\n");
461 return;
464 if ((firewall_enabled = is_firewall_enabled()) && !is_process_elevated())
466 skip("no privileges, skipping tests to avoid firewall dialog\n");
467 return;
470 if (firewall_enabled)
472 HRESULT hr = set_firewall(APP_ADD);
473 if (hr != S_OK)
475 skip("can't authorize app in firewall %08lx\n", hr);
476 return;
480 hr = CoInitialize(0);
481 ok( hr == S_OK, "failed to init com\n");
482 if (hr != S_OK)
483 goto done;
485 create_server();
486 test_server_info();
487 test_enum_service_providers();
489 CoUninitialize();
491 done:
492 if (firewall_enabled) set_firewall(APP_REMOVE);