1 // 2 // MonoTests.System.Security.Policy.FirstMatchCodeGroupTest 3 // 4 // Author: 5 // Sebastien Pouliot (spouliot@motus.com) 6 // 7 // (C) 2004 Motus Technologies Inc. (http://www.motus.com) 8 // 9 10 using NUnit.Framework; 11 using System; 12 using System.Collections; 13 using System.Security; 14 using System.Security.Policy; 15 using System.Security.Permissions; 16 17 namespace MonoTests.System.Security.Policy { 18 19 [TestFixture] 20 public class FirstMatchCodeGroupTest { 21 22 [Test] 23 [ExpectedException (typeof (ArgumentNullException))] Constructor_MembershipConditionNullPolicyStatement()24 public void Constructor_MembershipConditionNullPolicyStatement () 25 { 26 FirstMatchCodeGroup cg = new FirstMatchCodeGroup (null, new PolicyStatement (new PermissionSet (PermissionState.None))); 27 } 28 29 [Test] Constructor_MembershipConditionPolicyStatementNull()30 public void Constructor_MembershipConditionPolicyStatementNull () 31 { 32 // legal 33 FirstMatchCodeGroup cg = new FirstMatchCodeGroup (new AllMembershipCondition (), null); 34 Assert.IsNull (cg.PolicyStatement, "PolicyStatement"); 35 } 36 37 [Test] Constructor()38 public void Constructor () 39 { 40 FirstMatchCodeGroup cg = new FirstMatchCodeGroup (new AllMembershipCondition (), new PolicyStatement (new PermissionSet (PermissionState.None))); 41 Assert.IsNotNull (cg.PolicyStatement, "PolicyStatement"); 42 Assert.IsNotNull (cg.MembershipCondition, "MembershipCondition"); 43 } 44 45 [Test] MergeLogic()46 public void MergeLogic () 47 { 48 FirstMatchCodeGroup cg = new FirstMatchCodeGroup (new AllMembershipCondition (), new PolicyStatement (new PermissionSet (PermissionState.None))); 49 Assert.AreEqual ("First Match", cg.MergeLogic, "MergeLogic"); 50 } 51 52 [Test] Copy()53 public void Copy () 54 { 55 FirstMatchCodeGroup cg = new FirstMatchCodeGroup (new AllMembershipCondition (), new PolicyStatement (new PermissionSet (PermissionState.None))); 56 FirstMatchCodeGroup cg2 = (FirstMatchCodeGroup) cg.Copy (); 57 Assert.AreEqual (cg.AttributeString, cg2.AttributeString, "AttributeString"); 58 Assert.AreEqual (cg.Children.Count, cg2.Children.Count, "Children"); 59 Assert.AreEqual (cg.Description, cg2.Description, "Description"); 60 Assert.AreEqual (cg.MergeLogic, cg2.MergeLogic, "MergeLogic"); 61 Assert.AreEqual (cg.Name, cg2.Name, "Name"); 62 Assert.AreEqual (cg.PermissionSetName, cg2.PermissionSetName, "PermissionSetName"); 63 Assert.AreEqual (cg.ToXml ().ToString (), cg2.ToXml ().ToString (), "ToXml"); 64 } 65 66 [Test] CopyWithChildren()67 public void CopyWithChildren () 68 { 69 FirstMatchCodeGroup cgChild = new FirstMatchCodeGroup (new AllMembershipCondition (), new PolicyStatement (new PermissionSet (PermissionState.Unrestricted))); 70 FirstMatchCodeGroup cg = new FirstMatchCodeGroup (new AllMembershipCondition (), new PolicyStatement (new PermissionSet (PermissionState.None))); 71 cg.AddChild (cgChild); 72 FirstMatchCodeGroup cg2 = (FirstMatchCodeGroup) cg.Copy (); 73 Assert.AreEqual (cg.Children.Count, cg2.Children.Count, "Children"); 74 Assert.AreEqual (cg.ToXml ().ToString (), cg2.ToXml ().ToString (), "ToXml"); 75 } 76 77 [Test] 78 [ExpectedException (typeof (ArgumentNullException))] Resolve_Null()79 public void Resolve_Null () 80 { 81 FirstMatchCodeGroup cg = new FirstMatchCodeGroup (new AllMembershipCondition (), new PolicyStatement (new PermissionSet (PermissionState.None))); 82 cg.Resolve (null); 83 } 84 85 [Test] 86 [ExpectedException (typeof (ArgumentNullException))] ResolveMatchingCodeGroups_Null()87 public void ResolveMatchingCodeGroups_Null () 88 { 89 FirstMatchCodeGroup cg = new FirstMatchCodeGroup (new AllMembershipCondition (), new PolicyStatement (new PermissionSet (PermissionState.None))); 90 cg.ResolveMatchingCodeGroups (null); 91 } 92 93 [Test] ToFromXmlRoundtrip()94 public void ToFromXmlRoundtrip () 95 { 96 const string ps_Name = "TestName"; 97 PolicyStatement ps = new PolicyStatement (new NamedPermissionSet (ps_Name)); 98 FirstMatchCodeGroup cg = new FirstMatchCodeGroup (new AllMembershipCondition (), ps); 99 cg.Name = "SomeName"; 100 cg.Description = "Some Description"; 101 Assert.IsTrue (cg.Equals (cg), "Equals (itself)"); 102 SecurityElement se = cg.ToXml (); 103 104 FirstMatchCodeGroup cg2 = new FirstMatchCodeGroup (new AllMembershipCondition(), ps); 105 cg2.Name = "SomeOtherName"; 106 cg2.Description = "Some Other Description"; 107 Assert.IsTrue (!cg.Equals (cg2), "Equals (another)"); 108 109 cg2.FromXml (se); 110 Assert.IsTrue (cg.Equals (cg2), "Equals (FromXml)"); 111 } 112 } 113 } 114