From: Hans Leidekker Subject: [2/2] rpcrt4/tests: Also register a firewall exception for the rpc test. Message-Id: <1481199887-7715-2-git-send-email-hans@codeweavers.com> Date: Thu, 8 Dec 2016 13:24:47 +0100 Signed-off-by: Hans Leidekker --- dlls/rpcrt4/tests/rpc.c | 147 ++++++++++++++++++++++++++++++++++++++++++++- dlls/rpcrt4/tests/server.c | 1 - 2 files changed, 146 insertions(+), 2 deletions(-) diff --git a/dlls/rpcrt4/tests/rpc.c b/dlls/rpcrt4/tests/rpc.c index 2edae0b..36b3de9 100644 --- a/dlls/rpcrt4/tests/rpc.c +++ b/dlls/rpcrt4/tests/rpc.c @@ -22,6 +22,7 @@ #include #include +#define COBJMACROS #include #define WIN32_NO_STATUS #include "wine/test.h" @@ -30,7 +31,10 @@ #include #include #include +#include #include +#include +#include #include "rpc.h" #include "rpcdce.h" @@ -1061,15 +1065,142 @@ static void test_endpoint_mapper(RPC_CSTR protseq, RPC_CSTR address) ok(status == RPC_S_OK, "%s: RpcBindingVectorFree failed with error %u\n", protseq, status); } +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); +} + +enum firewall_op +{ + APP_ADD, + APP_REMOVE +}; + +static HRESULT set_firewall( enum firewall_op op ) +{ + static const WCHAR testW[] = {'r','p','c','r','t','4','_','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; + + 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( rpc ) { static unsigned char ncacn_np[] = "ncacn_np"; static unsigned char ncalrpc[] = "ncalrpc"; static unsigned char np_address[] = "."; + BOOL firewall_enabled = is_firewall_enabled(); + + if (firewall_enabled && !is_process_elevated()) + { + skip("no privileges, skipping tests to avoid firewall dialog\n"); + return; + } UuidConversionAndComparison(); TestDceErrorInqText(); - test_rpc_ncacn_ip_tcp(); test_towers(); test_I_RpcMapWin32Status(); test_RpcStringBindingParseA(); @@ -1080,7 +1211,21 @@ START_TEST( rpc ) test_RpcBindingFree(); test_RpcServerInqDefaultPrincName(); test_RpcServerRegisterAuthInfo(); + + if (firewall_enabled) + { + HRESULT hr = set_firewall(APP_ADD); + if (hr != S_OK) + { + skip("can't authorize app in firewall %08x\n", hr); + return; + } + } + + test_rpc_ncacn_ip_tcp(); test_RpcServerUseProtseq(); test_endpoint_mapper(ncacn_np, np_address); test_endpoint_mapper(ncalrpc, NULL); + + if (firewall_enabled) set_firewall(APP_REMOVE); } diff --git a/dlls/rpcrt4/tests/server.c b/dlls/rpcrt4/tests/server.c index c196c79..290e7de 100644 --- a/dlls/rpcrt4/tests/server.c +++ b/dlls/rpcrt4/tests/server.c @@ -24,7 +24,6 @@ #include #include #include -#include #include #include "wine/test.h" #include "server.h" -- 2.1.4