EventWaitHandleSecurityTest.cs 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. // EventWaitHandleSecurityTest.cs - NUnit Test Cases for EventWaitHandleSecurity
  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 System.Threading;
  12. using NUnit.Framework;
  13. namespace MonoTests.System.Security.AccessControl
  14. {
  15. [TestFixture]
  16. public class EventWaitHandleSecurityTest
  17. {
  18. // TODO: Mono System.Threading.EventWaitHandle does not throw exceptions on failure!
  19. [Test, ExpectedExceptionAttribute (typeof (UnauthorizedAccessException))]
  20. public void PermissionsActuallyWork ()
  21. {
  22. if (PlatformID.Win32NT != Environment.OSVersion.Platform) {
  23. Assert.Ignore (); return;
  24. }
  25. bool createdNew; EventWaitHandleSecurity security;
  26. string name = @"Local\MonoTestWaitHandle";
  27. using (EventWaitHandle handle = new EventWaitHandle (false, EventResetMode.ManualReset,
  28. name, out createdNew)) {
  29. Assert.IsFalse (handle.SafeWaitHandle.IsInvalid);
  30. Assert.IsTrue (createdNew);
  31. // Make sure our later error will be due to permissions and not some sharing bug.
  32. bool createdAnotherNew;
  33. using (EventWaitHandle anotherHandle = new EventWaitHandle (false, EventResetMode.ManualReset,
  34. name, out createdAnotherNew)) {
  35. Assert.IsFalse (anotherHandle.SafeWaitHandle.IsInvalid);
  36. Assert.IsFalse (createdAnotherNew);
  37. }
  38. // Let's make a deny all.
  39. security = handle.GetAccessControl ();
  40. foreach (EventWaitHandleAccessRule rule in security.GetAccessRules
  41. (true, false, typeof (SecurityIdentifier))) {
  42. security.RemoveAccessRuleSpecific (rule);
  43. }
  44. Assert.AreEqual (0, security.GetAccessRules (true, false, typeof (SecurityIdentifier)).Count);
  45. handle.SetAccessControl (security);
  46. security = handle.GetAccessControl ();
  47. Assert.AreEqual (0, security.GetAccessRules (true, false, typeof (SecurityIdentifier)).Count);
  48. // MS.NET will throw on the first line below.
  49. // For Mono testing the latter verifies the rest until the EventWaitHandle bug is fixed.
  50. // Also, NUnit 2.4 appears to lacks Assert.Pass ().
  51. EventWaitHandle badHandle = new EventWaitHandle(false, EventResetMode.ManualReset, name);
  52. if (badHandle.SafeWaitHandle.IsInvalid)
  53. throw new UnauthorizedAccessException ();
  54. }
  55. }
  56. }
  57. }