From: <gc...@us...> - 2003-02-13 17:53:22
|
Update of /cvsroot/dotnetmock/dotnetmock/DotNetMock.Examples/Security In directory sc8-pr-cvs1:/tmp/cvs-serv27506/DotNetMock.Examples/Security Added Files: SensitiveClass.cs SensitiveClassTests.cs Log Message: Added Security example to the Examples project --- NEW FILE: SensitiveClass.cs --- using System; using System.Threading; using System.Security; using System.Security.Permissions; using System.Security.Principal; namespace DotNetMock.Examples.Security { /// <summary> /// This is a "Sensitive" class that will demonstrate how to use the MockIPrincipal /// class for 3 type of security checks: Manually, Imperatively, and Declaratively /// </summary> public class SensitiveClass { public SensitiveClass() { } public bool CanRunManualCEOCheck() { bool allowed = false; IPrincipal currentPrincipal = Thread.CurrentPrincipal; if ( currentPrincipal.Identity.IsAuthenticated ) { if ( currentPrincipal.IsInRole( "CEO" ) ) { // Inside of this statment, we would run some code that requires the CEO role. // For this example all we are going to do is verify the fact that the caller has // the CEO role allowed = true; } } return allowed; } public bool CanRunImperativeCEOCheck() { bool allowed = false; PrincipalPermission CEOPermission = new PrincipalPermission(null, "CEO"); try { CEOPermission.Demand(); allowed = true; } catch (SecurityException ex) { } return allowed; } [PrincipalPermission (SecurityAction.Demand, Role="CEO")] public bool CanRunDeclarativeCEOCheck() { return true; } } } --- NEW FILE: SensitiveClassTests.cs --- using System; using System.Security; using System.Threading; using NUnit.Framework; using DotNetMock.Security.Principal; namespace DotNetMock.Examples.Security { [TestFixture] public class SensitiveClassTests { private SensitiveClass sensitiveClass = null; private MockIIdentity mockIdentity = null; private MockIPrincipal mockPrincipal = null; [SetUp] public void Init() { sensitiveClass = new SensitiveClass(); mockIdentity = new MockIIdentity(); mockPrincipal = new MockIPrincipal(); mockPrincipal.SetExpectedIdentity( mockIdentity ); Thread.CurrentPrincipal = mockPrincipal; } [TearDown] public void Destroy() { sensitiveClass = null; mockIdentity = null; mockPrincipal = null; } [Test] public void ManualCEOCheck() { mockIdentity.SetExpectedAuthenticationType( "NTLM" ); mockIdentity.SetExpectedIsAuthenticated( true ); mockIdentity.SetExpectedName( "Mr. CEO" ); mockPrincipal.SetExpectedIsInRoleCount(1); mockPrincipal.AddExpectedRole( "CEO" ); Assertion.Assert( "Cannot run Manual CEO Check!", sensitiveClass.CanRunManualCEOCheck() ); verifyMocks(); } [Test] public void CannotRunManualCEOCheck() { mockIdentity.SetExpectedAuthenticationType( "NTLM" ); mockIdentity.SetExpectedIsAuthenticated( true ); mockIdentity.SetExpectedName( "Mr. Employee" ); mockPrincipal.SetExpectedIsInRoleCount(1); mockPrincipal.AddExpectedRole( "Employee" ); Assertion.Assert( "Can run Manual CEO Check!", !sensitiveClass.CanRunManualCEOCheck() ); verifyMocks(); } [Test] public void NotAuthenticatedForCEOCheck() { mockIdentity.SetExpectedAuthenticationType( "NTLM" ); mockIdentity.SetExpectedIsAuthenticated( false ); mockIdentity.SetExpectedName( "Mr. Employee" ); mockPrincipal.SetExpectedIsInRoleCount(0); Assertion.Assert( "Authenticated!", !sensitiveClass.CanRunManualCEOCheck() ); verifyMocks(); } [Test] public void ImperativeCEOCheck() { mockIdentity.SetExpectedAuthenticationType( "NTLM" ); mockIdentity.SetExpectedIsAuthenticated( true ); mockIdentity.SetExpectedName( "Mr. CEO" ); mockPrincipal.SetExpectedIsInRoleCount(1); mockPrincipal.AddExpectedRole( "CEO" ); Assertion.Assert( "Cannot run Imperative CEO Check!", sensitiveClass.CanRunImperativeCEOCheck() ); verifyMocks(); } [Test] public void CannotRunImperativeCEOCheck() { mockIdentity.SetExpectedAuthenticationType( "NTLM" ); mockIdentity.SetExpectedIsAuthenticated( true ); mockIdentity.SetExpectedName( "Mr. Employee" ); mockPrincipal.SetExpectedIsInRoleCount(1); mockPrincipal.AddExpectedRole( "Employee" ); Assertion.Assert( "Can run Imperative CEO Check!", !sensitiveClass.CanRunImperativeCEOCheck() ); verifyMocks(); } [Test] public void NotAuthenticatedForImperativeCEOCheck() { mockIdentity.SetExpectedAuthenticationType( "NTLM" ); mockIdentity.SetExpectedIsAuthenticated( false ); mockIdentity.SetExpectedName( "Mr. Employee" ); mockPrincipal.SetExpectedIsInRoleCount(0); Assertion.Assert( "Authenticated!", !sensitiveClass.CanRunImperativeCEOCheck() ); verifyMocks(); } [Test] public void DeclarativeCEOCheck() { mockIdentity.SetExpectedAuthenticationType( "NTLM" ); mockIdentity.SetExpectedIsAuthenticated( true ); mockIdentity.SetExpectedName( "Mr. CEO" ); mockPrincipal.SetExpectedIsInRoleCount(1); mockPrincipal.AddExpectedRole( "CEO" ); Assertion.Assert( "Cannot run Declarative CEO Check!", sensitiveClass.CanRunDeclarativeCEOCheck() ); verifyMocks(); } [Test] [ExpectedException(typeof(SecurityException))] public void CannotRunDeclarativeCEOCheck() { mockIdentity.SetExpectedAuthenticationType( "NTLM" ); mockIdentity.SetExpectedIsAuthenticated( true ); mockIdentity.SetExpectedName( "Mr. Employee" ); mockPrincipal.SetExpectedIsInRoleCount(1); mockPrincipal.AddExpectedRole( "Employee" ); try { sensitiveClass.CanRunDeclarativeCEOCheck(); } finally { verifyMocks(); } } [Test] [ExpectedException(typeof(SecurityException))] public void NotAuthenticatedForDeclarativeCEOCheck() { mockIdentity.SetExpectedAuthenticationType( "NTLM" ); mockIdentity.SetExpectedIsAuthenticated( false ); mockIdentity.SetExpectedName( "Mr. Employee" ); mockPrincipal.SetExpectedIsInRoleCount(0); try { sensitiveClass.CanRunDeclarativeCEOCheck(); } finally { verifyMocks(); } } private void verifyMocks() { mockIdentity.Verify(); mockPrincipal.Verify(); } } } |