From 1f78cb8f4fb2264d1c8fcc5a6f87ea7054da23a1 Mon Sep 17 00:00:00 2001 From: Hans Leidekker Date: Wed, 13 Dec 2017 14:19:15 +0100 Subject: [PATCH] wsdapi/tests: Register an exception with the firewall to avoid a dialog. Signed-off-by: Hans Leidekker Signed-off-by: Alexandre Julliard --- dlls/wsdapi/tests/Makefile.in | 2 +- dlls/wsdapi/tests/discovery.c | 140 ++++++++++++++++++++++++++++++++++ 2 files changed, 141 insertions(+), 1 deletion(-) diff --git a/dlls/wsdapi/tests/Makefile.in b/dlls/wsdapi/tests/Makefile.in index cd4b41865c7..c1e559e1abb 100644 --- a/dlls/wsdapi/tests/Makefile.in +++ b/dlls/wsdapi/tests/Makefile.in @@ -1,5 +1,5 @@ TESTDLL = wsdapi.dll -IMPORTS = wsdapi ole32 user32 ws2_32 +IMPORTS = wsdapi ole32 oleaut32 user32 ws2_32 advapi32 C_SRCS = \ address.c \ diff --git a/dlls/wsdapi/tests/discovery.c b/dlls/wsdapi/tests/discovery.c index db5947d6057..f373510a760 100644 --- a/dlls/wsdapi/tests/discovery.c +++ b/dlls/wsdapi/tests/discovery.c @@ -27,6 +27,7 @@ #include "initguid.h" #include "objbase.h" #include "wsdapi.h" +#include typedef struct IWSDiscoveryPublisherNotifyImpl { IWSDiscoveryPublisherNotify IWSDiscoveryPublisherNotify_iface; @@ -276,8 +277,146 @@ static void Publish_tests(void) IWSDiscoveryPublisherNotify_Release(sink2); } +enum firewall_op +{ + APP_ADD, + APP_REMOVE +}; + +static BOOL is_process_elevated(void) +{ + HANDLE token; + if (OpenProcessToken( GetCurrentProcess(), TOKEN_QUERY, &token )) + { + TOKEN_ELEVATION_TYPE type; + DWORD size; + BOOL ret; + + ret = GetTokenInformation( token, TokenElevationType, &type, sizeof(type), &size ); + CloseHandle( token ); + return (ret && type == TokenElevationTypeFull); + } + return FALSE; +} + +static BOOL is_firewall_enabled(void) +{ + HRESULT hr, init; + INetFwMgr *mgr = NULL; + INetFwPolicy *policy = NULL; + INetFwProfile *profile = NULL; + VARIANT_BOOL enabled = VARIANT_FALSE; + + init = CoInitializeEx( 0, COINIT_APARTMENTTHREADED ); + + hr = CoCreateInstance( &CLSID_NetFwMgr, NULL, CLSCTX_INPROC_SERVER, &IID_INetFwMgr, + (void **)&mgr ); + ok( hr == S_OK, "got %08x\n", hr ); + if (hr != S_OK) goto done; + + hr = INetFwMgr_get_LocalPolicy( mgr, &policy ); + ok( hr == S_OK, "got %08x\n", hr ); + if (hr != S_OK) goto done; + + hr = INetFwPolicy_get_CurrentProfile( policy, &profile ); + if (hr != S_OK) goto done; + + hr = INetFwProfile_get_FirewallEnabled( profile, &enabled ); + ok( hr == S_OK, "got %08x\n", hr ); + +done: + if (policy) INetFwPolicy_Release( policy ); + if (profile) INetFwProfile_Release( profile ); + if (mgr) INetFwMgr_Release( mgr ); + if (SUCCEEDED( init )) CoUninitialize(); + return (enabled == VARIANT_TRUE); +} + +static HRESULT set_firewall( enum firewall_op op ) +{ + static const WCHAR testW[] = {'w','s','d','a','p','i','_','t','e','s','t',0}; + HRESULT hr, init; + INetFwMgr *mgr = NULL; + INetFwPolicy *policy = NULL; + INetFwProfile *profile = NULL; + INetFwAuthorizedApplication *app = NULL; + INetFwAuthorizedApplications *apps = NULL; + BSTR name, image = SysAllocStringLen( NULL, MAX_PATH ); + + if (!GetModuleFileNameW( NULL, image, MAX_PATH )) + { + SysFreeString( image ); + return E_FAIL; + } + init = CoInitializeEx( 0, COINIT_APARTMENTTHREADED ); + + hr = CoCreateInstance( &CLSID_NetFwMgr, NULL, CLSCTX_INPROC_SERVER, &IID_INetFwMgr, + (void **)&mgr ); + ok( hr == S_OK, "got %08x\n", hr ); + if (hr != S_OK) goto done; + + hr = INetFwMgr_get_LocalPolicy( mgr, &policy ); + ok( hr == S_OK, "got %08x\n", hr ); + if (hr != S_OK) goto done; + + hr = INetFwPolicy_get_CurrentProfile( policy, &profile ); + if (hr != S_OK) goto done; + + hr = INetFwProfile_get_AuthorizedApplications( profile, &apps ); + ok( hr == S_OK, "got %08x\n", hr ); + if (hr != S_OK) goto done; + + hr = CoCreateInstance( &CLSID_NetFwAuthorizedApplication, NULL, CLSCTX_INPROC_SERVER, + &IID_INetFwAuthorizedApplication, (void **)&app ); + ok( hr == S_OK, "got %08x\n", hr ); + if (hr != S_OK) goto done; + + hr = INetFwAuthorizedApplication_put_ProcessImageFileName( app, image ); + if (hr != S_OK) goto done; + + name = SysAllocString( testW ); + hr = INetFwAuthorizedApplication_put_Name( app, name ); + SysFreeString( name ); + ok( hr == S_OK, "got %08x\n", hr ); + if (hr != S_OK) goto done; + + if (op == APP_ADD) + hr = INetFwAuthorizedApplications_Add( apps, app ); + else if (op == APP_REMOVE) + hr = INetFwAuthorizedApplications_Remove( apps, image ); + else + hr = E_INVALIDARG; + +done: + if (app) INetFwAuthorizedApplication_Release( app ); + if (apps) INetFwAuthorizedApplications_Release( apps ); + if (policy) INetFwPolicy_Release( policy ); + if (profile) INetFwProfile_Release( profile ); + if (mgr) INetFwMgr_Release( mgr ); + if (SUCCEEDED( init )) CoUninitialize(); + SysFreeString( image ); + return hr; +} + START_TEST(discovery) { + BOOL firewall_enabled = is_firewall_enabled(); + HRESULT hr; + + if (firewall_enabled) + { + if (!is_process_elevated()) + { + skip("no privileges, skipping tests to avoid firewall dialog\n"); + return; + } + if ((hr = set_firewall(APP_ADD)) != S_OK) + { + skip("can't authorize app in firewall %08x\n", hr); + return; + } + } + CoInitialize(NULL); CreateDiscoveryPublisher_tests(); @@ -285,4 +424,5 @@ START_TEST(discovery) Publish_tests(); CoUninitialize(); + if (firewall_enabled) set_firewall(APP_REMOVE); }