Mono BCL test fixes so XM can run them (#4210)
[mono.git] / mcs / class / corlib / Test / System.Security / HostSecurityManagerTest.cs
1 //
2 // HostSecurityManagerTest.cs - NUnit Test Cases for HostSecurityManager
3 //
4 // Author:
5 //      Sebastien Pouliot  <sebastien@ximian.com>
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
30 // MOBILE profile lacks some (of the few) CAS features required to execute those tests
31 #if !MOBILE && !MONOMAC
32
33 using NUnit.Framework;
34 using System;
35 using System.Reflection;
36 using System.Security;
37 using System.Security.Policy;
38
39 namespace MonoTests.System.Security {
40
41         [TestFixture]
42         public class HostSecurityManagerTest {
43
44                 [Test]
45                 public void Defaults ()
46                 {
47                         HostSecurityManager hsm = new HostSecurityManager ();
48                         Assert.IsNull (hsm.DomainPolicy, "DomainPolicy");
49                         Assert.AreEqual (HostSecurityManagerOptions.AllFlags, hsm.Flags, "Flags");
50                 }
51
52                 [Test]
53                 [ExpectedException (typeof (ArgumentNullException))]
54                 public void DetermineApplicationTrust_Null_Evidence_TrustManagerContext ()
55                 {
56                         HostSecurityManager hsm = new HostSecurityManager ();
57                         hsm.DetermineApplicationTrust (null, new Evidence (), new TrustManagerContext ());
58                 }
59
60                 [Test]
61                 [ExpectedException (typeof (ArgumentException))]
62                 public void DetermineApplicationTrust_Evidence_Null_TrustManagerContext ()
63                 {
64                         HostSecurityManager hsm = new HostSecurityManager ();
65                         hsm.DetermineApplicationTrust (new Evidence (), null, new TrustManagerContext ());
66                 }
67
68                 [Test]
69                 [ExpectedException (typeof (ArgumentException))]
70                 public void DetermineApplicationTrust_Evidence_Evidence_Null ()
71                 {
72                         HostSecurityManager hsm = new HostSecurityManager ();
73                         hsm.DetermineApplicationTrust (new Evidence (), new Evidence (), null);
74                 }
75
76                 [Test]
77                 public void ProvideAppDomainEvidence ()
78                 {
79                         HostSecurityManager hsm = new HostSecurityManager ();
80                         Assert.IsNull (hsm.ProvideAppDomainEvidence (null), "null");
81
82                         Evidence e = new Evidence ();
83                         Evidence result = hsm.ProvideAppDomainEvidence (e);
84                         Assert.IsNotNull (result, "empty");
85                         Assert.AreEqual (0, result.Count, "Count-0");
86
87                         e.AddHost (new Zone (SecurityZone.Untrusted));
88                         result = hsm.ProvideAppDomainEvidence (e);
89                         Assert.AreEqual (1, result.Count, "Count-1");
90                 }
91
92                 [Test]
93                 public void ProvideAssemblyEvidence ()
94                 {
95                         HostSecurityManager hsm = new HostSecurityManager ();
96                         Assembly a = Assembly.GetExecutingAssembly ();
97
98                         Evidence result = hsm.ProvideAssemblyEvidence (a, null);
99                         Assert.IsNull (result, "null");
100
101                         Evidence e = new Evidence ();
102                         result = hsm.ProvideAssemblyEvidence (a, e);
103                         Assert.AreEqual (0, result.Count, "Count-empty");
104
105                         e.AddHost (new Zone (SecurityZone.Untrusted));
106                         result = hsm.ProvideAssemblyEvidence (a, e);
107                         Assert.AreEqual (1, result.Count, "Count-1");
108                 }
109
110                 [Test]
111                 public void ProvideAssemblyEvidence_NullAssembly ()
112                 {
113                         HostSecurityManager hsm = new HostSecurityManager ();
114
115                         Evidence result = hsm.ProvideAssemblyEvidence (null, null);
116                         Assert.IsNull (result, "null");
117
118                         Evidence e = new Evidence ();
119                         result = hsm.ProvideAssemblyEvidence (null, e);
120                         Assert.AreEqual (0, result.Count, "Count-empty");
121
122                         e.AddHost (new Zone (SecurityZone.Untrusted));
123                         result = hsm.ProvideAssemblyEvidence (null, e);
124                         Assert.AreEqual (1, result.Count, "Count-1");
125                 }
126
127                 [Test]
128                 [ExpectedException (typeof (NullReferenceException))]
129                 public void ResolvePolicy_Null ()
130                 {
131                         HostSecurityManager hsm = new HostSecurityManager ();
132                         PermissionSet ps = hsm.ResolvePolicy (null);
133                 }
134
135                 [Test]
136                 public void ResolvePolicy_Empty ()
137                 {
138                         HostSecurityManager hsm = new HostSecurityManager ();
139                         PermissionSet ps = hsm.ResolvePolicy (new Evidence ());
140                         Assert.AreEqual (0, ps.Count, "Count");
141                         Assert.IsFalse (ps.IsUnrestricted (), "IsUnrestricted");
142                 }
143
144                 [Test]
145                 public void ResolvePolicy_CurrentAssemblyEvidence ()
146                 {
147                         HostSecurityManager hsm = new HostSecurityManager ();
148                         Assembly a = Assembly.GetExecutingAssembly ();
149                         PermissionSet ps = hsm.ResolvePolicy (a.Evidence);
150
151                         PermissionSet expected = SecurityManager.ResolvePolicy (a.Evidence);
152                         Assert.AreEqual (expected.ToString (), ps.ToString (), "PermissionSet");
153                 }
154         }
155 }
156
157 #endif