SecurityManagerTest.cs 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  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. [ExpectedException (typeof (ArgumentNullException))]
  58. public void LoadPolicyLevelFromFile_Null ()
  59. {
  60. SecurityManager.LoadPolicyLevelFromFile (null, PolicyLevelType.AppDomain);
  61. }
  62. [Test]
  63. [ExpectedException (typeof (ArgumentNullException))]
  64. public void LoadPolicyLevelFromString_Null ()
  65. {
  66. SecurityManager.LoadPolicyLevelFromString (null, PolicyLevelType.AppDomain);
  67. }
  68. [Test]
  69. public void PolicyHierarchy ()
  70. {
  71. IEnumerator e = SecurityManager.PolicyHierarchy ();
  72. Assert.IsNotNull (e, "PolicyHierarchy");
  73. }
  74. private void ResolveEvidenceHost (SecurityZone zone, bool unrestricted, bool empty)
  75. {
  76. string prefix = zone.ToString () + "-";
  77. Evidence e = new Evidence ();
  78. e.AddHost (new Zone (zone));
  79. PermissionSet ps = SecurityManager.ResolvePolicy (e);
  80. // as 2.0 use Unrestricted for Identity permissions they have no need to be
  81. // kept in resolved permission set
  82. #if NET_2_0
  83. Assert.IsTrue ((unrestricted || (ps.Count > 0)), prefix + "Count");
  84. #else
  85. Assert.IsTrue ((ps.Count > 0), prefix + "Count");
  86. #endif
  87. Assert.AreEqual (empty, ps.IsEmpty (), prefix + "IsEmpty");
  88. Assert.AreEqual (unrestricted, ps.IsUnrestricted (), prefix + "IsUnrestricted");
  89. #if NET_2_0
  90. if (unrestricted)
  91. Assert.IsNull (ps.GetPermission (typeof (ZoneIdentityPermission)), prefix + "GetPermission(ZoneIdentityPermission)");
  92. else
  93. Assert.IsNotNull (ps.GetPermission (typeof (ZoneIdentityPermission)), prefix + "GetPermission(ZoneIdentityPermission)");
  94. #else
  95. Assert.IsNotNull (ps.GetPermission (typeof (ZoneIdentityPermission)), prefix + "GetPermission(ZoneIdentityPermission)");
  96. #endif
  97. }
  98. #if NET_2_0
  99. [Category ("NotWorking")]
  100. #endif
  101. [Test]
  102. public void ResolvePolicy_Evidence_Host_Zone ()
  103. {
  104. ResolveEvidenceHost (SecurityZone.Internet, false, false);
  105. ResolveEvidenceHost (SecurityZone.Intranet, false, false);
  106. ResolveEvidenceHost (SecurityZone.MyComputer, true, false);
  107. ResolveEvidenceHost (SecurityZone.Trusted, false, false);
  108. ResolveEvidenceHost (SecurityZone.Untrusted, false, false);
  109. ResolveEvidenceHost (SecurityZone.NoZone, false, true);
  110. }
  111. private void ResolveEvidenceAssembly (SecurityZone zone)
  112. {
  113. string prefix = zone.ToString () + "-";
  114. Evidence e = new Evidence ();
  115. e.AddAssembly (new Zone (zone));
  116. PermissionSet ps = SecurityManager.ResolvePolicy (e);
  117. Assert.AreEqual (0, ps.Count, prefix + "Count");
  118. Assert.IsTrue (ps.IsEmpty (), prefix + "IsEmpty");
  119. Assert.IsFalse (ps.IsUnrestricted (), prefix + "IsUnrestricted");
  120. }
  121. [Test]
  122. public void ResolvePolicy_Evidence_Assembly_Zone ()
  123. {
  124. ResolveEvidenceAssembly (SecurityZone.Internet);
  125. ResolveEvidenceAssembly (SecurityZone.Intranet);
  126. ResolveEvidenceAssembly (SecurityZone.MyComputer);
  127. ResolveEvidenceAssembly (SecurityZone.Trusted);
  128. ResolveEvidenceAssembly (SecurityZone.Untrusted);
  129. ResolveEvidenceAssembly (SecurityZone.NoZone);
  130. }
  131. [Test]
  132. public void ResolvePolicy_Evidence_Null ()
  133. {
  134. Evidence e = null;
  135. PermissionSet ps = SecurityManager.ResolvePolicy (e);
  136. // no exception thrown
  137. Assert.IsNotNull (ps);
  138. Assert.IsFalse (ps.IsUnrestricted (), "IsUnrestricted");
  139. }
  140. [Test]
  141. public void ResolvePolicy_Evidence_CurrentAssembly ()
  142. {
  143. PermissionSet granted = SecurityManager.ResolvePolicy (CurrentEvidence);
  144. Assert.IsNotNull (granted);
  145. Assert.IsTrue (granted.IsUnrestricted (), "IsUnrestricted");
  146. }
  147. #if NET_2_0
  148. [Test]
  149. public void ResolvePolicy_Evidences_Null ()
  150. {
  151. Evidence[] e = null;
  152. PermissionSet ps = SecurityManager.ResolvePolicy (e);
  153. // no exception thrown
  154. Assert.IsNotNull (ps);
  155. Assert.IsFalse (ps.IsUnrestricted (), "IsUnrestricted");
  156. }
  157. #endif
  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. #if NET_2_0
  178. [ExpectedException (typeof (PolicyException))]
  179. [Category ("NotWorking")]
  180. #endif
  181. public void ResolvePolicy_Evidence_DenyUnrestricted_CurrentAssembly ()
  182. {
  183. PermissionSet deny = new PermissionSet (PermissionState.Unrestricted);
  184. PermissionSet denied = null;
  185. PermissionSet granted = SecurityManager.ResolvePolicy (CurrentEvidence, null, null, deny, out denied);
  186. // doing this we denied the Execution right
  187. #if !NET_2_0
  188. Assert.AreEqual (0, denied.Count, "Denied");
  189. Assert.IsTrue (granted.IsUnrestricted (), "Granted.IsUnrestricted");
  190. #endif
  191. }
  192. [Test]
  193. [Category ("NotDotNet")] // MS bug - throws a NullReferenceException
  194. public void ResolvePolicy_Evidence_DenyUnrestricted_NoExecution ()
  195. {
  196. PermissionSet deny = new PermissionSet (PermissionState.Unrestricted);
  197. PermissionSet denied = null;
  198. SecurityManager.CheckExecutionRights = false;
  199. PermissionSet granted = SecurityManager.ResolvePolicy (CurrentEvidence, null, null, deny, out denied);
  200. }
  201. [Test]
  202. [ExpectedException (typeof (ArgumentNullException))]
  203. public void ResolvePolicyGroups_Null ()
  204. {
  205. IEnumerator e = SecurityManager.ResolvePolicyGroups (null);
  206. }
  207. [Test]
  208. [ExpectedException (typeof (NullReferenceException))]
  209. public void SavePolicyLevel_Null ()
  210. {
  211. SecurityManager.SavePolicyLevel (null);
  212. }
  213. [Test]
  214. [ExpectedException (typeof (PolicyException))]
  215. public void SavePolicyLevel_AppDomain ()
  216. {
  217. PolicyLevel adl = PolicyLevel.CreateAppDomainLevel ();
  218. SecurityManager.SavePolicyLevel (adl);
  219. }
  220. #if NET_2_0
  221. [Test]
  222. public void GetZoneAndOrigin ()
  223. {
  224. ArrayList zone = null;
  225. ArrayList origin = null;
  226. SecurityManager.GetZoneAndOrigin (out zone, out origin);
  227. Assert.IsNotNull (zone, "Zone");
  228. Assert.AreEqual (0, zone.Count, "Zone.Count");
  229. Assert.IsNotNull (origin, "Origin");
  230. Assert.AreEqual (0, origin.Count, "Origin.Count");
  231. }
  232. [Test]
  233. public void ResolvePolicy_Evidence_ArrayNull ()
  234. {
  235. Evidence[] e = null;
  236. PermissionSet ps = SecurityManager.ResolvePolicy (e);
  237. Assert.IsNotNull (ps, "PermissionSet");
  238. Assert.IsFalse (ps.IsUnrestricted (), "IsUnrestricted");
  239. Assert.AreEqual (0, ps.Count, "Count");
  240. }
  241. [Test]
  242. public void ResolvePolicy_Evidence_ArrayEmpty ()
  243. {
  244. Evidence[] e = new Evidence [0];
  245. PermissionSet ps = SecurityManager.ResolvePolicy (e);
  246. Assert.IsNotNull (ps, "PermissionSet");
  247. Assert.IsFalse (ps.IsUnrestricted (), "IsUnrestricted");
  248. Assert.AreEqual (0, ps.Count, "Count");
  249. }
  250. [Test]
  251. public void ResolvePolicy_Evidence_Array ()
  252. {
  253. Evidence[] e = new Evidence[] { new Evidence () };
  254. PermissionSet ps = SecurityManager.ResolvePolicy (e);
  255. Assert.IsNotNull (ps, "PermissionSet");
  256. Assert.IsFalse (ps.IsUnrestricted (), "IsUnrestricted");
  257. Assert.AreEqual (0, ps.Count, "Count");
  258. }
  259. [Test]
  260. [ExpectedException (typeof (NullReferenceException))]
  261. [Category ("NotWorking")]
  262. public void ResolveSystemPolicy_Null ()
  263. {
  264. SecurityManager.ResolveSystemPolicy (null);
  265. }
  266. #endif
  267. }
  268. }