SecurityManagerTest.cs 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. //
  2. // SecurityManagerTest.cs - NUnit Test Cases for SecurityManager
  3. //
  4. // Author:
  5. // Sebastien Pouliot <[email protected]>
  6. //
  7. // (C) 2004 Motus Technologies Inc. (http://www.motus.com)
  8. // Copyright (C) 2004 Novell, Inc (http://www.novell.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. using NUnit.Framework;
  30. using System;
  31. using System.Collections;
  32. using System.Reflection;
  33. using System.Security;
  34. using System.Security.Permissions;
  35. using System.Security.Policy;
  36. namespace MonoTests.System.Security {
  37. [TestFixture]
  38. public class SecurityManagerTest {
  39. static Evidence CurrentEvidence;
  40. [TestFixtureSetUp]
  41. public void FixtureSetUp ()
  42. {
  43. CurrentEvidence = Assembly.GetExecutingAssembly ().Evidence;
  44. }
  45. [TearDown]
  46. public void TearDown ()
  47. {
  48. SecurityManager.CheckExecutionRights = true;
  49. }
  50. [Test]
  51. public void IsGranted_Null ()
  52. {
  53. // null is always granted
  54. Assert.IsTrue (SecurityManager.IsGranted (null));
  55. }
  56. [Test]
  57. #if MOBILE
  58. [ExpectedException (typeof (NotSupportedException))]
  59. #else
  60. [ExpectedException (typeof (ArgumentNullException))]
  61. #endif
  62. public void LoadPolicyLevelFromFile_Null ()
  63. {
  64. SecurityManager.LoadPolicyLevelFromFile (null, PolicyLevelType.AppDomain);
  65. }
  66. [Test]
  67. #if MOBILE
  68. [ExpectedException (typeof (NotSupportedException))]
  69. #else
  70. [ExpectedException (typeof (ArgumentNullException))]
  71. #endif
  72. public void LoadPolicyLevelFromString_Null ()
  73. {
  74. SecurityManager.LoadPolicyLevelFromString (null, PolicyLevelType.AppDomain);
  75. }
  76. [Test]
  77. #if MOBILE
  78. [ExpectedException (typeof (NotSupportedException))]
  79. #endif
  80. public void PolicyHierarchy ()
  81. {
  82. IEnumerator e = SecurityManager.PolicyHierarchy ();
  83. Assert.IsNotNull (e, "PolicyHierarchy");
  84. }
  85. #if !MOBILE
  86. private void ResolveEvidenceHost (SecurityZone zone, bool unrestricted, bool empty)
  87. {
  88. string prefix = zone.ToString () + "-";
  89. Evidence e = new Evidence ();
  90. e.AddHost (new Zone (zone));
  91. PermissionSet ps = SecurityManager.ResolvePolicy (e);
  92. // as 2.0 use Unrestricted for Identity permissions they have no need to be
  93. // kept in resolved permission set
  94. Assert.IsTrue ((unrestricted || (ps.Count > 0)), prefix + "Count");
  95. Assert.AreEqual (empty, ps.IsEmpty (), prefix + "IsEmpty");
  96. Assert.AreEqual (unrestricted, ps.IsUnrestricted (), prefix + "IsUnrestricted");
  97. if (unrestricted)
  98. Assert.IsNull (ps.GetPermission (typeof (ZoneIdentityPermission)), prefix + "GetPermission(ZoneIdentityPermission)");
  99. else
  100. Assert.IsNotNull (ps.GetPermission (typeof (ZoneIdentityPermission)), prefix + "GetPermission(ZoneIdentityPermission)");
  101. }
  102. [Category ("NotWorking")]
  103. [Test]
  104. public void ResolvePolicy_Evidence_Host_Zone ()
  105. {
  106. ResolveEvidenceHost (SecurityZone.Internet, false, false);
  107. ResolveEvidenceHost (SecurityZone.Intranet, false, false);
  108. ResolveEvidenceHost (SecurityZone.MyComputer, true, false);
  109. ResolveEvidenceHost (SecurityZone.Trusted, false, false);
  110. ResolveEvidenceHost (SecurityZone.Untrusted, false, false);
  111. ResolveEvidenceHost (SecurityZone.NoZone, false, true);
  112. }
  113. private void ResolveEvidenceAssembly (SecurityZone zone)
  114. {
  115. string prefix = zone.ToString () + "-";
  116. Evidence e = new Evidence ();
  117. e.AddAssembly (new Zone (zone));
  118. PermissionSet ps = SecurityManager.ResolvePolicy (e);
  119. Assert.AreEqual (0, ps.Count, prefix + "Count");
  120. Assert.IsTrue (ps.IsEmpty (), prefix + "IsEmpty");
  121. Assert.IsFalse (ps.IsUnrestricted (), prefix + "IsUnrestricted");
  122. }
  123. [Test]
  124. public void ResolvePolicy_Evidence_Assembly_Zone ()
  125. {
  126. ResolveEvidenceAssembly (SecurityZone.Internet);
  127. ResolveEvidenceAssembly (SecurityZone.Intranet);
  128. ResolveEvidenceAssembly (SecurityZone.MyComputer);
  129. ResolveEvidenceAssembly (SecurityZone.Trusted);
  130. ResolveEvidenceAssembly (SecurityZone.Untrusted);
  131. ResolveEvidenceAssembly (SecurityZone.NoZone);
  132. }
  133. [Test]
  134. public void ResolvePolicy_Evidence_Null ()
  135. {
  136. Evidence e = null;
  137. PermissionSet ps = SecurityManager.ResolvePolicy (e);
  138. // no exception thrown
  139. Assert.IsNotNull (ps);
  140. Assert.IsFalse (ps.IsUnrestricted (), "IsUnrestricted");
  141. }
  142. [Test]
  143. public void ResolvePolicy_Evidence_CurrentAssembly ()
  144. {
  145. PermissionSet granted = SecurityManager.ResolvePolicy (CurrentEvidence);
  146. Assert.IsNotNull (granted);
  147. Assert.IsTrue (granted.IsUnrestricted (), "IsUnrestricted");
  148. }
  149. [Test]
  150. public void ResolvePolicy_Evidences_Null ()
  151. {
  152. Evidence[] e = null;
  153. PermissionSet ps = SecurityManager.ResolvePolicy (e);
  154. // no exception thrown
  155. Assert.IsNotNull (ps);
  156. Assert.IsFalse (ps.IsUnrestricted (), "IsUnrestricted");
  157. }
  158. [Test]
  159. public void ResolvePolicy_Evidence_AllNull_NoExecution ()
  160. {
  161. PermissionSet denied = null;
  162. SecurityManager.CheckExecutionRights = false;
  163. PermissionSet granted = SecurityManager.ResolvePolicy (null, null, null, null, out denied);
  164. Assert.IsNull (denied, "Denied");
  165. Assert.AreEqual (0, granted.Count, "Granted.Count");
  166. Assert.IsFalse (granted.IsUnrestricted (), "!Granted.IsUnrestricted");
  167. }
  168. [Test]
  169. public void ResolvePolicy_Evidence_NullRequests_CurrentAssembly ()
  170. {
  171. PermissionSet denied = null;
  172. PermissionSet granted = SecurityManager.ResolvePolicy (CurrentEvidence, null, null, null, out denied);
  173. Assert.IsNull (denied, "Denied");
  174. Assert.IsTrue (granted.IsUnrestricted (), "Granted.IsUnrestricted");
  175. }
  176. [Test]
  177. [ExpectedException (typeof (PolicyException))]
  178. [Category ("NotWorking")]
  179. public void ResolvePolicy_Evidence_DenyUnrestricted_CurrentAssembly ()
  180. {
  181. PermissionSet deny = new PermissionSet (PermissionState.Unrestricted);
  182. PermissionSet denied = null;
  183. PermissionSet granted = SecurityManager.ResolvePolicy (CurrentEvidence, null, null, deny, out denied);
  184. }
  185. [Test]
  186. [Category ("NotDotNet")] // MS bug - throws a NullReferenceException
  187. public void ResolvePolicy_Evidence_DenyUnrestricted_NoExecution ()
  188. {
  189. PermissionSet deny = new PermissionSet (PermissionState.Unrestricted);
  190. PermissionSet denied = null;
  191. SecurityManager.CheckExecutionRights = false;
  192. PermissionSet granted = SecurityManager.ResolvePolicy (CurrentEvidence, null, null, deny, out denied);
  193. }
  194. [Test]
  195. [ExpectedException (typeof (ArgumentNullException))]
  196. public void ResolvePolicyGroups_Null ()
  197. {
  198. IEnumerator e = SecurityManager.ResolvePolicyGroups (null);
  199. }
  200. [Test]
  201. [ExpectedException (typeof (NullReferenceException))]
  202. public void SavePolicyLevel_Null ()
  203. {
  204. SecurityManager.SavePolicyLevel (null);
  205. }
  206. [Test]
  207. [ExpectedException (typeof (PolicyException))]
  208. public void SavePolicyLevel_AppDomain ()
  209. {
  210. PolicyLevel adl = PolicyLevel.CreateAppDomainLevel ();
  211. SecurityManager.SavePolicyLevel (adl);
  212. }
  213. [Test]
  214. public void GetZoneAndOrigin ()
  215. {
  216. ArrayList zone = null;
  217. ArrayList origin = null;
  218. SecurityManager.GetZoneAndOrigin (out zone, out origin);
  219. Assert.IsNotNull (zone, "Zone");
  220. Assert.AreEqual (0, zone.Count, "Zone.Count");
  221. Assert.IsNotNull (origin, "Origin");
  222. Assert.AreEqual (0, origin.Count, "Origin.Count");
  223. }
  224. [Test]
  225. public void ResolvePolicy_Evidence_ArrayNull ()
  226. {
  227. Evidence[] e = null;
  228. PermissionSet ps = SecurityManager.ResolvePolicy (e);
  229. Assert.IsNotNull (ps, "PermissionSet");
  230. Assert.IsFalse (ps.IsUnrestricted (), "IsUnrestricted");
  231. Assert.AreEqual (0, ps.Count, "Count");
  232. }
  233. [Test]
  234. public void ResolvePolicy_Evidence_ArrayEmpty ()
  235. {
  236. Evidence[] e = new Evidence [0];
  237. PermissionSet ps = SecurityManager.ResolvePolicy (e);
  238. Assert.IsNotNull (ps, "PermissionSet");
  239. Assert.IsFalse (ps.IsUnrestricted (), "IsUnrestricted");
  240. Assert.AreEqual (0, ps.Count, "Count");
  241. }
  242. [Test]
  243. public void ResolvePolicy_Evidence_Array ()
  244. {
  245. Evidence[] e = new Evidence[] { new Evidence () };
  246. PermissionSet ps = SecurityManager.ResolvePolicy (e);
  247. Assert.IsNotNull (ps, "PermissionSet");
  248. Assert.IsFalse (ps.IsUnrestricted (), "IsUnrestricted");
  249. Assert.AreEqual (0, ps.Count, "Count");
  250. }
  251. [Test]
  252. [ExpectedException (typeof (NullReferenceException))]
  253. [Category ("NotWorking")]
  254. public void ResolveSystemPolicy_Null ()
  255. {
  256. SecurityManager.ResolveSystemPolicy (null);
  257. }
  258. #endif
  259. }
  260. }