ObjectSecurity_TTest.cs 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. // ObjectSecurity_TTest.cs - NUnit Test Cases for ObjectSecurity<T>
  2. //
  3. // Authors:
  4. // James Bellinger ([email protected])
  5. #if NET_4_0
  6. using System;
  7. using System.Security.AccessControl;
  8. using System.Security.Principal;
  9. using NUnit.Framework;
  10. namespace MonoTests.System.Security.AccessControl
  11. {
  12. [TestFixture]
  13. public class ObjectSecurity_TTest
  14. {
  15. enum WillWorkRights
  16. {
  17. Value = 1
  18. }
  19. class WillWorkSecurity : ObjectSecurity<WillWorkRights>
  20. {
  21. public WillWorkSecurity ()
  22. : base (false, ResourceType.Unknown)
  23. {
  24. }
  25. }
  26. struct WillFailRights
  27. {
  28. }
  29. class WillFailSecurity : ObjectSecurity<WillFailRights>
  30. {
  31. public WillFailSecurity ()
  32. : base (false, ResourceType.Unknown)
  33. {
  34. }
  35. }
  36. [Test]
  37. public void TypesAreCorrect ()
  38. {
  39. WillWorkSecurity security = new WillWorkSecurity ();
  40. Assert.AreEqual (security.AccessRightType, typeof (WillWorkRights));
  41. Assert.AreEqual (security.AccessRuleType, typeof (AccessRule<WillWorkRights>));
  42. Assert.AreEqual (security.AuditRuleType, typeof (AuditRule<WillWorkRights>));
  43. }
  44. [Test]
  45. public void WillWorkOKUsingAccessFactory ()
  46. {
  47. WillWorkSecurity security = new WillWorkSecurity ();
  48. SecurityIdentifier id = new SecurityIdentifier (WellKnownSidType.WorldSid, null);
  49. AccessRule<WillWorkRights> rule = (AccessRule<WillWorkRights>)
  50. security.AccessRuleFactory (id, 1, false,
  51. InheritanceFlags.None, PropagationFlags.None,
  52. AccessControlType.Allow);
  53. Assert.AreEqual (rule.AccessControlType, AccessControlType.Allow);
  54. Assert.AreEqual (rule.IdentityReference, id);
  55. Assert.AreEqual (rule.InheritanceFlags, InheritanceFlags.None);
  56. Assert.AreEqual (rule.PropagationFlags, PropagationFlags.None);
  57. Assert.AreEqual (rule.Rights, WillWorkRights.Value);
  58. }
  59. [Test]
  60. public void WillWorkOKUsingConstructor()
  61. {
  62. SecurityIdentifier id = new SecurityIdentifier (WellKnownSidType.WorldSid, null);
  63. AccessRule<WillWorkRights> rule = new AccessRule<WillWorkRights> (id, WillWorkRights.Value,
  64. AccessControlType.Allow);
  65. Assert.AreEqual (rule.AccessControlType, AccessControlType.Allow);
  66. Assert.AreEqual (rule.IdentityReference, id);
  67. Assert.AreEqual (rule.Rights, WillWorkRights.Value);
  68. }
  69. [Test, ExpectedException (typeof (InvalidCastException))]
  70. public void WillFailFailsUsingFactoryOnGetter()
  71. {
  72. WillFailSecurity security = new WillFailSecurity ();
  73. SecurityIdentifier id = new SecurityIdentifier (WellKnownSidType.WorldSid, null);
  74. AccessRule<WillFailRights> rule = (AccessRule<WillFailRights>)
  75. security.AccessRuleFactory (id, 1, false,
  76. InheritanceFlags.None, PropagationFlags.None,
  77. AccessControlType.Allow);
  78. WillFailRights rights = rule.Rights;
  79. }
  80. [Test, ExpectedException (typeof (InvalidCastException))]
  81. public void WillFailFailsUsingConstructor()
  82. {
  83. SecurityIdentifier id = new SecurityIdentifier (WellKnownSidType.WorldSid, null);
  84. AccessRule<WillFailRights> rule = new AccessRule<WillFailRights> (id, new WillFailRights(),
  85. AccessControlType.Allow);
  86. }
  87. }
  88. }
  89. #endif