HostSecurityManagerTest.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. //
  2. // HostSecurityManagerTest.cs - NUnit Test Cases for HostSecurityManager
  3. //
  4. // Author:
  5. // Sebastien Pouliot <[email protected]>
  6. //
  7. // Copyright (C) 2005 Novell, Inc (http://www.novell.com)
  8. //
  9. // Permission is hereby granted, free of charge, to any person obtaining
  10. // a copy of this software and associated documentation files (the
  11. // "Software"), to deal in the Software without restriction, including
  12. // without limitation the rights to use, copy, modify, merge, publish,
  13. // distribute, sublicense, and/or sell copies of the Software, and to
  14. // permit persons to whom the Software is furnished to do so, subject to
  15. // the following conditions:
  16. //
  17. // The above copyright notice and this permission notice shall be
  18. // included in all copies or substantial portions of the Software.
  19. //
  20. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  21. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  22. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  23. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  24. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  25. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  26. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  27. //
  28. #if NET_2_0
  29. using NUnit.Framework;
  30. using System;
  31. using System.Reflection;
  32. using System.Security;
  33. using System.Security.Policy;
  34. namespace MonoTests.System.Security {
  35. [TestFixture]
  36. public class HostSecurityManagerTest {
  37. [Test]
  38. public void Defaults ()
  39. {
  40. HostSecurityManager hsm = new HostSecurityManager ();
  41. Assert.IsNull (hsm.DomainPolicy, "DomainPolicy");
  42. Assert.AreEqual (HostSecurityManagerOptions.AllFlags, hsm.Flags, "Flags");
  43. }
  44. [Test]
  45. [ExpectedException (typeof (ArgumentNullException))]
  46. public void DetermineApplicationTrust_Null_Evidence_TrustManagerContext ()
  47. {
  48. HostSecurityManager hsm = new HostSecurityManager ();
  49. hsm.DetermineApplicationTrust (null, new Evidence (), new TrustManagerContext ());
  50. }
  51. [Test]
  52. [ExpectedException (typeof (ArgumentException))]
  53. public void DetermineApplicationTrust_Evidence_Null_TrustManagerContext ()
  54. {
  55. HostSecurityManager hsm = new HostSecurityManager ();
  56. hsm.DetermineApplicationTrust (new Evidence (), null, new TrustManagerContext ());
  57. }
  58. [Test]
  59. [ExpectedException (typeof (ArgumentException))]
  60. public void DetermineApplicationTrust_Evidence_Evidence_Null ()
  61. {
  62. HostSecurityManager hsm = new HostSecurityManager ();
  63. hsm.DetermineApplicationTrust (new Evidence (), new Evidence (), null);
  64. }
  65. [Test]
  66. public void ProvideAppDomainEvidence ()
  67. {
  68. HostSecurityManager hsm = new HostSecurityManager ();
  69. Assert.IsNull (hsm.ProvideAppDomainEvidence (null), "null");
  70. Evidence e = new Evidence ();
  71. Evidence result = hsm.ProvideAppDomainEvidence (e);
  72. Assert.IsNotNull (result, "empty");
  73. Assert.AreEqual (0, result.Count, "Count-0");
  74. e.AddHost (new Zone (SecurityZone.Untrusted));
  75. result = hsm.ProvideAppDomainEvidence (e);
  76. Assert.AreEqual (1, result.Count, "Count-1");
  77. }
  78. [Test]
  79. public void ProvideAssemblyEvidence ()
  80. {
  81. HostSecurityManager hsm = new HostSecurityManager ();
  82. Assembly a = Assembly.GetExecutingAssembly ();
  83. Evidence result = hsm.ProvideAssemblyEvidence (a, null);
  84. Assert.IsNull (result, "null");
  85. Evidence e = new Evidence ();
  86. result = hsm.ProvideAssemblyEvidence (a, e);
  87. Assert.AreEqual (0, result.Count, "Count-empty");
  88. e.AddHost (new Zone (SecurityZone.Untrusted));
  89. result = hsm.ProvideAssemblyEvidence (a, e);
  90. Assert.AreEqual (1, result.Count, "Count-1");
  91. }
  92. [Test]
  93. public void ProvideAssemblyEvidence_NullAssembly ()
  94. {
  95. HostSecurityManager hsm = new HostSecurityManager ();
  96. Evidence result = hsm.ProvideAssemblyEvidence (null, null);
  97. Assert.IsNull (result, "null");
  98. Evidence e = new Evidence ();
  99. result = hsm.ProvideAssemblyEvidence (null, e);
  100. Assert.AreEqual (0, result.Count, "Count-empty");
  101. e.AddHost (new Zone (SecurityZone.Untrusted));
  102. result = hsm.ProvideAssemblyEvidence (null, e);
  103. Assert.AreEqual (1, result.Count, "Count-1");
  104. }
  105. [Test]
  106. [ExpectedException (typeof (NullReferenceException))]
  107. public void ResolvePolicy_Null ()
  108. {
  109. HostSecurityManager hsm = new HostSecurityManager ();
  110. PermissionSet ps = hsm.ResolvePolicy (null);
  111. }
  112. [Test]
  113. public void ResolvePolicy_Empty ()
  114. {
  115. HostSecurityManager hsm = new HostSecurityManager ();
  116. PermissionSet ps = hsm.ResolvePolicy (new Evidence ());
  117. Assert.AreEqual (0, ps.Count, "Count");
  118. Assert.IsFalse (ps.IsUnrestricted (), "IsUnrestricted");
  119. }
  120. [Test]
  121. public void ResolvePolicy_CurrentAssemblyEvidence ()
  122. {
  123. HostSecurityManager hsm = new HostSecurityManager ();
  124. Assembly a = Assembly.GetExecutingAssembly ();
  125. PermissionSet ps = hsm.ResolvePolicy (a.Evidence);
  126. PermissionSet expected = SecurityManager.ResolvePolicy (a.Evidence);
  127. Assert.AreEqual (expected.ToString (), ps.ToString (), "PermissionSet");
  128. }
  129. }
  130. }
  131. #endif