2 // SecurityManagerTest.cs - NUnit Test Cases for SecurityManager
5 // Sebastien Pouliot <sebastien@ximian.com>
7 // (C) 2004 Motus Technologies Inc. (http://www.motus.com)
8 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
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:
18 // The above copyright notice and this permission notice shall be
19 // included in all copies or substantial portions of the Software.
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.
30 using NUnit
.Framework
;
32 using System
.Collections
;
33 using System
.Reflection
;
34 using System
.Security
;
35 using System
.Security
.Permissions
;
36 using System
.Security
.Policy
;
38 namespace MonoTests
.System
.Security
{
41 public class SecurityManagerTest
{
43 static Evidence CurrentEvidence
;
46 public void FixtureSetUp ()
48 CurrentEvidence
= Assembly
.GetExecutingAssembly ().Evidence
;
52 public void TearDown ()
54 SecurityManager
.CheckExecutionRights
= true;
58 public void IsGranted_Null ()
60 // null is always granted
61 Assert
.IsTrue (SecurityManager
.IsGranted (null));
65 [ExpectedException (typeof (ArgumentNullException
))]
66 public void LoadPolicyLevelFromFile_Null ()
68 SecurityManager
.LoadPolicyLevelFromFile (null, PolicyLevelType
.AppDomain
);
72 [ExpectedException (typeof (ArgumentNullException
))]
73 public void LoadPolicyLevelFromString_Null ()
75 SecurityManager
.LoadPolicyLevelFromString (null, PolicyLevelType
.AppDomain
);
79 public void PolicyHierarchy ()
81 IEnumerator e
= SecurityManager
.PolicyHierarchy ();
82 Assert
.IsNotNull (e
, "PolicyHierarchy");
85 private void ResolveEvidenceHost (SecurityZone zone
, bool unrestricted
, bool empty
)
87 string prefix
= zone
.ToString () + "-";
88 Evidence e
= new Evidence ();
89 e
.AddHost (new Zone (zone
));
90 PermissionSet ps
= SecurityManager
.ResolvePolicy (e
);
91 Assert
.IsTrue (ps
.Count
> 0, prefix
+ "Count");
92 Assert
.AreEqual (empty
, ps
.IsEmpty (), prefix
+ "IsEmpty");
93 Assert
.AreEqual (unrestricted
, ps
.IsUnrestricted (), prefix
+ "IsUnrestricted");
94 Assert
.IsNotNull (ps
.GetPermission (typeof (ZoneIdentityPermission
)), prefix
+ "GetPermission(ZoneIdentityPermission)");
98 public void ResolvePolicy_Evidence_Host_Zone ()
100 ResolveEvidenceHost (SecurityZone
.Internet
, false, false);
101 ResolveEvidenceHost (SecurityZone
.Intranet
, false, false);
102 ResolveEvidenceHost (SecurityZone
.MyComputer
, true, false);
103 ResolveEvidenceHost (SecurityZone
.Trusted
, false, false);
104 ResolveEvidenceHost (SecurityZone
.Untrusted
, false, false);
105 ResolveEvidenceHost (SecurityZone
.NoZone
, false, true);
108 private void ResolveEvidenceAssembly (SecurityZone zone
)
110 string prefix
= zone
.ToString () + "-";
111 Evidence e
= new Evidence ();
112 e
.AddAssembly (new Zone (zone
));
113 PermissionSet ps
= SecurityManager
.ResolvePolicy (e
);
114 Assert
.AreEqual (0, ps
.Count
, prefix
+ "Count");
115 Assert
.IsTrue (ps
.IsEmpty (), prefix
+ "IsEmpty");
116 Assert
.IsFalse (ps
.IsUnrestricted (), prefix
+ "IsUnrestricted");
120 public void ResolvePolicy_Evidence_Assembly_Zone ()
122 ResolveEvidenceAssembly (SecurityZone
.Internet
);
123 ResolveEvidenceAssembly (SecurityZone
.Intranet
);
124 ResolveEvidenceAssembly (SecurityZone
.MyComputer
);
125 ResolveEvidenceAssembly (SecurityZone
.Trusted
);
126 ResolveEvidenceAssembly (SecurityZone
.Untrusted
);
127 ResolveEvidenceAssembly (SecurityZone
.NoZone
);
131 public void ResolvePolicy_Evidence_Null ()
134 PermissionSet ps
= SecurityManager
.ResolvePolicy (e
);
135 // no exception thrown
136 Assert
.IsNotNull (ps
);
137 Assert
.IsFalse (ps
.IsUnrestricted (), "IsUnrestricted");
141 public void ResolvePolicy_Evidence_CurrentAssembly ()
143 PermissionSet granted
= SecurityManager
.ResolvePolicy (CurrentEvidence
);
144 Assert
.IsNotNull (granted
);
145 Assert
.IsTrue (granted
.IsUnrestricted (), "IsUnrestricted");
150 public void ResolvePolicy_Evidences_Null ()
153 PermissionSet ps
= SecurityManager
.ResolvePolicy (e
);
154 // no exception thrown
155 Assert
.IsNotNull (ps
);
156 Assert
.IsFalse (ps
.IsUnrestricted (), "IsUnrestricted");
160 [ExpectedException (typeof (PolicyException
))]
161 public void ResolvePolicy_Evidence_AllNull ()
163 PermissionSet denied
= null;
164 SecurityManager
.ResolvePolicy (null, null, null, null, out denied
);
165 // null is missing the Execution right
169 public void ResolvePolicy_Evidence_AllNull_NoExecution ()
171 PermissionSet denied
= null;
172 SecurityManager
.CheckExecutionRights
= false;
173 PermissionSet granted
= SecurityManager
.ResolvePolicy (null, null, null, null, out denied
);
174 Assert
.IsNull (denied
, "Denied");
175 Assert
.AreEqual (0, granted
.Count
, "Granted.Count");
176 Assert
.IsFalse (granted
.IsUnrestricted (), "!Granted.IsUnrestricted");
180 public void ResolvePolicy_Evidence_NullRequests_CurrentAssembly ()
182 PermissionSet denied
= null;
183 PermissionSet granted
= SecurityManager
.ResolvePolicy (CurrentEvidence
, null, null, null, out denied
);
184 Assert
.IsNull (denied
, "Denied");
185 Assert
.IsTrue (granted
.IsUnrestricted (), "Granted.IsUnrestricted");
190 [ExpectedException (typeof (PolicyException
))]
192 public void ResolvePolicy_Evidence_DenyUnrestricted_CurrentAssembly ()
194 PermissionSet deny
= new PermissionSet (PermissionState
.Unrestricted
);
195 PermissionSet denied
= null;
196 PermissionSet granted
= SecurityManager
.ResolvePolicy (CurrentEvidence
, null, null, deny
, out denied
);
197 // doing this we denied the Execution right
199 Assert
.AreEqual (0, denied
.Count
, "Denied");
200 Assert
.IsTrue (granted
.IsUnrestricted (), "Granted.IsUnrestricted");
205 [Ignore ("MS bug - throws a NullReferenceException")]
206 public void ResolvePolicy_Evidence_DenyUnrestricted_NoExecution ()
208 PermissionSet deny
= new PermissionSet (PermissionState
.Unrestricted
);
209 PermissionSet denied
= null;
210 SecurityManager
.CheckExecutionRights
= false;
211 PermissionSet granted
= SecurityManager
.ResolvePolicy (CurrentEvidence
, null, null, deny
, out denied
);
215 [ExpectedException (typeof (ArgumentNullException
))]
216 public void ResolvePolicyGroups_Null ()
218 IEnumerator e
= SecurityManager
.ResolvePolicyGroups (null);
222 [ExpectedException (typeof (NullReferenceException
))]
223 public void SavePolicyLevel_Null ()
225 SecurityManager
.SavePolicyLevel (null);
229 [ExpectedException (typeof (PolicyException
))]
230 public void SavePolicyLevel_AppDomain ()
232 PolicyLevel adl
= PolicyLevel
.CreateAppDomainLevel ();
233 SecurityManager
.SavePolicyLevel (adl
);