HostSecurityManagerTest.cs 5.1 KB

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