ACLTest.cs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. /*
  2. * Copyright (c) Contributors, http://opensimulator.org/
  3. * See CONTRIBUTORS.TXT for a full list of copyright holders.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. * * Redistributions of source code must retain the above copyright
  8. * notice, this list of conditions and the following disclaimer.
  9. * * Redistributions in binary form must reproduce the above copyright
  10. * notice, this list of conditions and the following disclaimer in the
  11. * documentation and/or other materials provided with the distribution.
  12. * * Neither the name of the OpenSimulator Project nor the
  13. * names of its contributors may be used to endorse or promote products
  14. * derived from this software without specific prior written permission.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY
  17. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  18. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  19. * DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY
  20. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  21. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  22. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  23. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  24. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  25. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. using System;
  28. using NUnit.Framework;
  29. using System.Collections.Generic;
  30. namespace OpenSim.Framework.Tests
  31. {
  32. [TestFixture]
  33. public class ACLTest
  34. {
  35. #region Tests
  36. /// <summary>
  37. /// ACL Test class
  38. /// </summary>
  39. [Test]
  40. public void ACLTest01()
  41. {
  42. ACL acl = new ACL();
  43. Role Guests = new Role("Guests");
  44. acl.AddRole(Guests);
  45. Role[] parents = new Role[1];
  46. parents[0] = Guests;
  47. Role JoeGuest = new Role("JoeGuest", parents);
  48. acl.AddRole(JoeGuest);
  49. Resource CanBuild = new Resource("CanBuild");
  50. acl.AddResource(CanBuild);
  51. acl.GrantPermission("Guests", "CanBuild");
  52. Permission perm = acl.HasPermission("JoeGuest", "CanBuild");
  53. Assert.That(perm == Permission.Allow, "JoeGuest should have permission to build");
  54. perm = Permission.None;
  55. try
  56. {
  57. perm = acl.HasPermission("unknownGuest", "CanBuild");
  58. }
  59. catch (KeyNotFoundException)
  60. {
  61. }
  62. catch (Exception)
  63. {
  64. Assert.That(false,"Exception thrown should have been KeyNotFoundException");
  65. }
  66. Assert.That(perm == Permission.None,"Permission None should be set because exception should have been thrown");
  67. }
  68. [Test]
  69. public void KnownButPermissionDenyAndPermissionNoneUserTest()
  70. {
  71. ACL acl = new ACL();
  72. Role Guests = new Role("Guests");
  73. acl.AddRole(Guests);
  74. Role Administrators = new Role("Administrators");
  75. acl.AddRole(Administrators);
  76. Role[] Guestparents = new Role[1];
  77. Role[] Adminparents = new Role[1];
  78. Guestparents[0] = Guests;
  79. Adminparents[0] = Administrators;
  80. Role JoeGuest = new Role("JoeGuest", Guestparents);
  81. acl.AddRole(JoeGuest);
  82. Resource CanBuild = new Resource("CanBuild");
  83. acl.AddResource(CanBuild);
  84. Resource CanScript = new Resource("CanScript");
  85. acl.AddResource(CanScript);
  86. Resource CanRestart = new Resource("CanRestart");
  87. acl.AddResource(CanRestart);
  88. acl.GrantPermission("Guests", "CanBuild");
  89. acl.DenyPermission("Guests", "CanRestart");
  90. acl.GrantPermission("Administrators", "CanScript");
  91. acl.GrantPermission("Administrators", "CanRestart");
  92. Permission setPermission = acl.HasPermission("JoeGuest", "CanRestart");
  93. Assert.That(setPermission == Permission.Deny, "Guests Should not be able to restart");
  94. Assert.That(acl.HasPermission("JoeGuest", "CanScript") == Permission.None,
  95. "No Explicit Permissions set so should be Permission.None");
  96. }
  97. #endregion
  98. }
  99. }