Unit testing security model ClickOnce

允我心安 提交于 2020-01-03 16:57:37

问题


I am fiddling around trying to install an application via ClickOnce - with certain minimum permissions. I'd like to unit test to assert that my application does not use any additional functionality disallowed by the wanted security policy.

Can I in my unit test specify that I want to use the specified manifest to regulate permissions, make calls to my library and then assert that no security exceptions are thrown?

If so, how?

Thanks!


回答1:


If you want to unit test (test in isolation) you have to

  • test permissionLogic and you have to
  • test that your contrologic (i.e. MVVM) uses the permission logic.

Test SecurityManager for permissionLogic

you can extract the permissionLogic to a class of its own with methods

public class SecurityManager
{
 bool IsAllowedToPrint(User user);
 bool IsAllowedToAdminister(User user);
}

then you write unit tests

 User user = CreateAdimistrator();
 Assert.AreEqual(true, securityManager.IsAllowedToAdminister(user));

Contrologic (i.e. MVVM) uses permission logic

create a mock-SecurityManager that always allow/disallow functionality. and write unit tests for the controller if it reacts as expected.

var allowEverythingMock = CreateSecurityManagerMockThatAllowsEverything();
var mvvm = CreateMvvm(allowEverythingMock );
Assert.IsNotNull(mvvm.GetAdminGui());

I am not shure if there is an easy way to create an integration-test where Click-Once-App actually uses the real SecurityManager and the result gets verified.

Update after getting more infos on what the goal is

write unit tests for the controller if it reacts as expected.

var controller = CreateCreate(Permission.Low);

try
{
   // File io is not allowed with low permissions
   controller.SaveTextAsFile("HellowWorld", @"c:\temp\UnittestResult.txt");
   Assert.Fail("The Controller should have forbidden this");
} catch(PermissionException pex) {
   // everything is ok. This specific exception was expected.
}


来源:https://stackoverflow.com/questions/4585174/unit-testing-security-model-clickonce

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!