ObjectSecurityTest.cs 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. // ObjectSecurityTest.cs - NUnit Test Cases for ObjectSecurity
  2. //
  3. // Authors:
  4. // James Bellinger <[email protected]>
  5. //
  6. // Copyright (C) 2012 James Bellinger
  7. using System;
  8. using System.Collections.Generic;
  9. using System.Security.AccessControl;
  10. using System.Security.Principal;
  11. using NUnit.Framework;
  12. namespace MonoTests.System.Security.AccessControl
  13. {
  14. [TestFixture]
  15. public class ObjectSecurityTest
  16. {
  17. [Test]
  18. public void Defaults ()
  19. {
  20. TestSecurity security = new TestSecurity ();
  21. Assert.IsTrue (security.AreAccessRulesCanonical);
  22. Assert.IsTrue (security.AreAuditRulesCanonical);
  23. Assert.IsFalse (security.AreAccessRulesProtected);
  24. Assert.IsFalse (security.AreAuditRulesProtected);
  25. Assert.IsNull (security.GetGroup (typeof (SecurityIdentifier)));
  26. Assert.IsNull (security.GetOwner (typeof (SecurityIdentifier)));
  27. }
  28. [Test]
  29. public void DefaultsForSddlAndBinary ()
  30. {
  31. TestSecurity security = new TestSecurity ();
  32. Assert.AreEqual ("D:", security.GetSecurityDescriptorSddlForm (AccessControlSections.All));
  33. Assert.AreEqual (28, security.GetSecurityDescriptorBinaryForm ().Length);
  34. }
  35. [Test]
  36. public void SetSddlForm ()
  37. {
  38. TestSecurity security = new TestSecurity ();
  39. SecurityIdentifier groupSid = new SecurityIdentifier ("WD");
  40. SecurityIdentifier userSid = new SecurityIdentifier ("SY");
  41. security.SetGroup (groupSid);
  42. security.SetOwner (userSid);
  43. Assert.AreEqual ("G:WD", security.GetSecurityDescriptorSddlForm (AccessControlSections.Group));
  44. Assert.AreEqual ("O:SY", security.GetSecurityDescriptorSddlForm (AccessControlSections.Owner));
  45. security.SetSecurityDescriptorSddlForm ("O:BG", AccessControlSections.Owner);
  46. Assert.AreEqual ("O:BG", security.GetSecurityDescriptorSddlForm (AccessControlSections.Owner));
  47. Assert.AreEqual (new SecurityIdentifier ("BG"), security.GetOwner (typeof (SecurityIdentifier)));
  48. }
  49. [Test]
  50. public void SetSddlFormAllowsFlags ()
  51. {
  52. TestSecurity security = new TestSecurity ();
  53. security.SetSecurityDescriptorSddlForm ("G:BA", AccessControlSections.Group | AccessControlSections.Owner);
  54. Assert.AreEqual ("", security.GetSecurityDescriptorSddlForm (AccessControlSections.Owner));
  55. Assert.AreEqual ("G:BA", security.GetSecurityDescriptorSddlForm (AccessControlSections.Group));
  56. }
  57. [Test, ExpectedException (typeof (ArgumentNullException))]
  58. public void SetGroupThrowsOnNull ()
  59. {
  60. TestSecurity security = new TestSecurity ();
  61. security.SetGroup (null);
  62. }
  63. [Test, ExpectedException (typeof (ArgumentNullException))]
  64. public void SetOwnerThrowsOnNull ()
  65. {
  66. TestSecurity security = new TestSecurity ();
  67. security.SetOwner (null);
  68. }
  69. [Test, ExpectedException (typeof (ArgumentNullException))]
  70. public void PurgeThrowsOnNull ()
  71. {
  72. TestSecurity security = new TestSecurity ();
  73. security.PurgeAccessRules (null);
  74. }
  75. [Test]
  76. public void AllTypesAcceptedOnGetGroupOwnerUntilTheyAreSet ()
  77. {
  78. TestSecurity security = new TestSecurity ();
  79. Assert.IsNull (security.GetGroup (typeof (void)));
  80. Assert.IsNull (security.GetOwner (typeof (int)));
  81. SecurityIdentifier everyoneSid = new SecurityIdentifier ("WD");
  82. security.SetOwner (everyoneSid);
  83. bool throwsOnInt = false;
  84. try { security.GetOwner (typeof (int)); } catch (ArgumentException) { throwsOnInt = true; }
  85. Assert.IsTrue (throwsOnInt);
  86. bool throwsOnSuperclass = false;
  87. try { security.GetOwner (typeof (IdentityReference)); } catch (ArgumentException) { throwsOnSuperclass = true; }
  88. Assert.IsTrue (throwsOnSuperclass);
  89. Assert.IsNull (security.GetGroup (typeof (void)));
  90. Assert.IsInstanceOfType (typeof (SecurityIdentifier), security.GetOwner (typeof (SecurityIdentifier)));
  91. }
  92. [Test]
  93. public void ModifyAccessRuleAllowsDerivedTypeAndCallsModifyAccessButNothingChanges ()
  94. {
  95. bool modifiedRet, modifiedOut;
  96. SecurityIdentifier everyoneSid = new SecurityIdentifier ("WD");
  97. TestSecurity security = new TestSecurity ();
  98. DerivedAccessRule rule = new DerivedAccessRule (everyoneSid, TestRights.One, AccessControlType.Allow);
  99. modifiedRet = security.ModifyAccessRule (AccessControlModification.Add, rule, out modifiedOut);
  100. Assert.AreEqual (modifiedRet, modifiedOut);
  101. Assert.IsTrue (modifiedRet);
  102. Assert.IsTrue (security.modify_access_called);
  103. Assert.AreEqual ("D:", security.GetSecurityDescriptorSddlForm (AccessControlSections.All));
  104. // (1) There is no external abstract/virtual 'get collection',
  105. // (2) The overrides in this test call this base class, which does not change it, and
  106. // (3) There are methods based on the collection value such as GetSecurityDescriptorSddlForm.
  107. // Conclusion: Collection is internal and manipulated by derived classes.
  108. }
  109. [Test, ExpectedException (typeof (ArgumentException))]
  110. public void ModifyAccessRuleThrowsOnWrongType ()
  111. {
  112. bool modified;
  113. SecurityIdentifier everyoneSid = new SecurityIdentifier ("WD");
  114. TestSecurity security = new TestSecurity ();
  115. FileSystemAccessRule rule = new FileSystemAccessRule
  116. (everyoneSid, FileSystemRights.FullControl, AccessControlType.Allow);
  117. security.ModifyAccessRule (AccessControlModification.Add, rule, out modified);
  118. }
  119. [Test]
  120. public void Reset ()
  121. {
  122. bool modifiedRet, modifiedOut;
  123. SecurityIdentifier everyoneSid = new SecurityIdentifier ("WD");
  124. TestSecurity security = new TestSecurity ();
  125. TestAccessRule rule = new TestAccessRule
  126. (everyoneSid, TestRights.One, AccessControlType.Allow);
  127. modifiedRet = security.ModifyAccessRule (AccessControlModification.Reset, rule, out modifiedOut);
  128. }
  129. [Test]
  130. public void Protection ()
  131. {
  132. TestSecurity security = new TestSecurity ();
  133. security.SetAccessRuleProtection (true, true);
  134. Assert.IsTrue (security.AreAccessRulesProtected);
  135. Assert.IsFalse (security.AreAuditRulesProtected);
  136. security.SetAuditRuleProtection (true, false);
  137. Assert.IsTrue (security.AreAccessRulesProtected);
  138. Assert.IsTrue (security.AreAuditRulesProtected);
  139. security.SetAccessRuleProtection (false, false);
  140. Assert.IsFalse (security.AreAccessRulesProtected);
  141. Assert.IsTrue (security.AreAuditRulesProtected);
  142. security.SetAuditRuleProtection (false, true);
  143. Assert.IsFalse (security.AreAccessRulesProtected);
  144. Assert.IsFalse (security.AreAuditRulesProtected);
  145. }
  146. enum TestRights
  147. {
  148. One = 1
  149. }
  150. class DerivedAccessRule : TestAccessRule
  151. {
  152. public DerivedAccessRule (IdentityReference identity, TestRights rights, AccessControlType type)
  153. : base (identity, rights, type)
  154. {
  155. }
  156. }
  157. class TestAccessRule : AccessRule
  158. {
  159. public TestAccessRule (IdentityReference identity, TestRights rights, AccessControlType type)
  160. : this (identity, rights, false, InheritanceFlags.None, PropagationFlags.None, type)
  161. {
  162. }
  163. public TestAccessRule (IdentityReference identity,
  164. TestRights rights, bool isInherited,
  165. InheritanceFlags inheritanceFlags,
  166. PropagationFlags propagationFlags,
  167. AccessControlType type)
  168. : base (identity, (int)rights, isInherited, inheritanceFlags, propagationFlags, type)
  169. {
  170. }
  171. }
  172. class TestAuditRule : AuditRule
  173. {
  174. public TestAuditRule (IdentityReference identity,
  175. TestRights rights, bool isInherited,
  176. InheritanceFlags inheritanceFlags,
  177. PropagationFlags propagationFlags,
  178. AuditFlags flags)
  179. : base (identity, (int)rights, isInherited, inheritanceFlags, propagationFlags, flags)
  180. {
  181. }
  182. }
  183. class TestSecurity : ObjectSecurity
  184. {
  185. internal bool modify_access_called;
  186. public TestSecurity () : base (false, false)
  187. {
  188. }
  189. public override AccessRule AccessRuleFactory (IdentityReference identityReference,
  190. int accessMask, bool isInherited,
  191. InheritanceFlags inheritanceFlags,
  192. PropagationFlags propagationFlags,
  193. AccessControlType type)
  194. {
  195. return new TestAccessRule (identityReference, (TestRights)accessMask, isInherited,
  196. inheritanceFlags, propagationFlags, type);
  197. }
  198. public override AuditRule AuditRuleFactory (IdentityReference identityReference,
  199. int accessMask, bool isInherited,
  200. InheritanceFlags inheritanceFlags,
  201. PropagationFlags propagationFlags,
  202. AuditFlags flags)
  203. {
  204. return new TestAuditRule (identityReference, (TestRights)accessMask, isInherited,
  205. inheritanceFlags, propagationFlags, flags);
  206. }
  207. protected override bool ModifyAccess (AccessControlModification modification,
  208. AccessRule rule, out bool modified)
  209. {
  210. modify_access_called = true;
  211. modified = true; return modified;
  212. }
  213. protected override bool ModifyAudit (AccessControlModification modification,
  214. AuditRule rule, out bool modified)
  215. {
  216. modified = false; return modified;
  217. }
  218. public override Type AccessRightType {
  219. get { return typeof (TestRights); }
  220. }
  221. public override Type AccessRuleType {
  222. get { return typeof (TestAccessRule); }
  223. }
  224. public override Type AuditRuleType {
  225. get { return typeof (TestAuditRule); }
  226. }
  227. }
  228. }
  229. }