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