CodeGroupTest.cs 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. //
  2. // MonoTests.System.Security.Policy.CodeGroupTest
  3. //
  4. // Author(s):
  5. // Nick Drochak ([email protected])
  6. //
  7. // (C) 2001 Nick Drochak, All rights reserved.
  8. using NUnit.Framework;
  9. using System;
  10. using System.Security;
  11. using System.Security.Policy;
  12. using System.Security.Permissions;
  13. namespace MonoTests.System.Security.Policy
  14. {
  15. public class CodeGroupTest : TestCase
  16. {
  17. protected override void SetUp()
  18. {
  19. }
  20. public class MyCodeGroup : CodeGroup
  21. {
  22. public MyCodeGroup(IMembershipCondition membershipCondition,
  23. PolicyStatement policy) :base(membershipCondition, policy)
  24. {
  25. }
  26. public override CodeGroup Copy()
  27. {
  28. return this;
  29. }
  30. public override string MergeLogic
  31. {
  32. get
  33. {
  34. return "";
  35. }
  36. }
  37. public override PolicyStatement Resolve( Evidence evidence)
  38. {
  39. return (PolicyStatement)null;
  40. }
  41. public override CodeGroup ResolveMatchingCodeGroups(Evidence evidence)
  42. {
  43. return this;
  44. }
  45. }
  46. public void TestConstructorExceptions()
  47. {
  48. MyCodeGroup cg;
  49. try
  50. {
  51. cg = new MyCodeGroup(null, null);
  52. Fail("Constructor should throw exception on null paramters");
  53. }
  54. catch(Exception e)
  55. {
  56. Assert("Should have caught an ArgumentNull Exception", e is ArgumentNullException);
  57. }
  58. }
  59. public void TestConstructor()
  60. {
  61. MyCodeGroup cg = null;
  62. try
  63. {
  64. cg = new MyCodeGroup(new AllMembershipCondition(), new PolicyStatement(new PermissionSet(PermissionState.None)));
  65. Assert("PolicyStatement property not set correctly by constructor.", cg.PolicyStatement != null);
  66. Assert("MembershipCondition property not set correctly by constructor.", cg.MembershipCondition != null);
  67. }
  68. catch(Exception e)
  69. {
  70. Fail("Constructor failed. Exception caught was: " + e.ToString());
  71. }
  72. }
  73. public void TestDescriptionProperty()
  74. {
  75. MyCodeGroup cg = null;
  76. const string description = "Test Description";
  77. cg = new MyCodeGroup(new AllMembershipCondition(), new PolicyStatement(new PermissionSet(PermissionState.None)));
  78. cg.Description = description;
  79. Assert("Description not the expected value", cg.Description == description);
  80. }
  81. public void TestNameProperty()
  82. {
  83. MyCodeGroup cg = null;
  84. const string name = "Test Name";
  85. cg = new MyCodeGroup(new AllMembershipCondition(), new PolicyStatement(new PermissionSet(PermissionState.None)));
  86. cg.Name = name;
  87. Assert("Description not the expected value", cg.Name == name);
  88. }
  89. public void TestChildren()
  90. {
  91. MyCodeGroup cg = null;
  92. cg = new MyCodeGroup(new AllMembershipCondition(), new PolicyStatement(new PermissionSet(PermissionState.None)));
  93. cg.AddChild(new MyCodeGroup(new AllMembershipCondition(), new PolicyStatement(new PermissionSet(PermissionState.Unrestricted))));
  94. Assert("Unexpected number of children", cg.Children.Count == 1);
  95. }
  96. public void TestAttributeStringProperty()
  97. {
  98. MyCodeGroup cg = null;
  99. PolicyStatementAttribute psa = PolicyStatementAttribute.LevelFinal;
  100. PolicyStatement ps = new PolicyStatement(new PermissionSet(PermissionState.None));
  101. ps.Attributes = psa;
  102. cg = new MyCodeGroup(new AllMembershipCondition(), ps);
  103. AssertEquals("AttributeString", psa.ToString(), cg.AttributeString);
  104. }
  105. public void TestPermissionSetNameProperty()
  106. {
  107. MyCodeGroup cg = null;
  108. const string ps_Name = "TestName";
  109. PolicyStatement ps = new PolicyStatement(new NamedPermissionSet(ps_Name));
  110. cg = new MyCodeGroup(new AllMembershipCondition(), ps);
  111. AssertEquals("AttributeString", ps_Name, cg.PermissionSetName);
  112. }
  113. public void TestEquals()
  114. {
  115. MyCodeGroup cg = null;
  116. MyCodeGroup cg2 = null;
  117. const string ps_Name = "TestName";
  118. PolicyStatement ps = new PolicyStatement(new NamedPermissionSet(ps_Name));
  119. cg = new MyCodeGroup(new AllMembershipCondition(), ps);
  120. cg.Name = "SomeName";
  121. cg.Description = "Some Description";
  122. bool isEquals;
  123. isEquals = cg.Equals(cg);
  124. isEquals = cg.Equals("Not Equal to this");
  125. Assert("CodeGroup should not be equal to a non-CodeGroup type", !isEquals);
  126. cg2 = new MyCodeGroup(new AllMembershipCondition(), ps);
  127. cg2.Name = "SomeOtherName";
  128. cg2.Description = "Some Other Description";
  129. isEquals = cg.Equals(cg2);
  130. Assert("CodeGroup should not be equal when Name or Description is different", !isEquals);
  131. cg2 = new MyCodeGroup(new ApplicationDirectoryMembershipCondition(), ps);
  132. cg2.Name = cg.Name;
  133. cg2.Description = cg.Description;
  134. isEquals = cg.Equals(cg2);
  135. Assert("CodeGroup should not be equal when Membership Condition is different", !isEquals);
  136. }
  137. public void TestEqualsWithChildren()
  138. {
  139. MyCodeGroup cg = null;
  140. MyCodeGroup cg2 = null;
  141. MyCodeGroup cgChild = null;
  142. const string ps_Name = "TestName";
  143. bool isEquals;
  144. PolicyStatement ps = new PolicyStatement(new NamedPermissionSet(ps_Name));
  145. cg = new MyCodeGroup(new AllMembershipCondition(), ps);
  146. cg.Name = "SomeName";
  147. cg.Description = "Some Description";
  148. cgChild = new MyCodeGroup(new ApplicationDirectoryMembershipCondition(), ps);
  149. cgChild.Name = "ChildName";
  150. cgChild.Description = "Child Descripiton";
  151. cg.AddChild(cgChild);
  152. cg2 = new MyCodeGroup(cg.MembershipCondition, cg.PolicyStatement);
  153. cg2.Name = cg.Name;
  154. cg2.Description = cg.Description;
  155. isEquals = cg.Equals(cg2);
  156. Assert("Should be equal when Children are ignored", isEquals);
  157. isEquals = cg.Equals(cg2, true);
  158. Assert("Should not be equal when Child count is different", !isEquals);
  159. cg2.AddChild(cgChild);
  160. isEquals = cg2.Equals(cg, true);
  161. Assert("Should be equal when children are equal", isEquals);
  162. }
  163. public void TestRemoveChild()
  164. {
  165. MyCodeGroup cg = null;
  166. MyCodeGroup cgChild = null;
  167. MyCodeGroup cgChild2 = null;
  168. const string ps_Name = "TestName";
  169. PolicyStatement ps = new PolicyStatement(new NamedPermissionSet(ps_Name));
  170. cg = new MyCodeGroup(new AllMembershipCondition(), ps);
  171. cg.Name = "SomeName";
  172. cg.Description = "Some Description";
  173. cgChild = new MyCodeGroup(new ApplicationDirectoryMembershipCondition(), ps);
  174. cgChild.Name = "ChildName";
  175. cgChild.Description = "Child Descripiton";
  176. cg.AddChild(cgChild);
  177. cgChild2 = new MyCodeGroup(new ApplicationDirectoryMembershipCondition(), ps);
  178. cgChild2.Name = "ChildName2";
  179. cgChild2.Description = "Child Descripiton 2";
  180. cg.AddChild(cgChild2);
  181. AssertEquals("Should be two children before the call to Remove()", 2, cg.Children.Count);
  182. cg.RemoveChild(cgChild);
  183. AssertEquals("Remaing child does not have correct name", "ChildName2", ((CodeGroup)cg.Children[0]).Name);
  184. try
  185. {
  186. cg.RemoveChild(cgChild);
  187. Fail("Should have throw error on trying to remove non-existant child");
  188. }
  189. catch{}
  190. }
  191. } // public class CodeGroupTest
  192. } // namespace MonoTests.System.Security.Policy