* roottypes.cs: Rename from tree.cs.
[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 //
9 // Permission is hereby granted, free of charge, to any person obtaining
10 // a copy of this software and associated documentation files (the
11 // "Software"), to deal in the Software without restriction, including
12 // without limitation the rights to use, copy, modify, merge, publish,
13 // distribute, sublicense, and/or sell copies of the Software, and to
14 // permit persons to whom the Software is furnished to do so, subject to
15 // the following conditions:
16 // 
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
19 // 
20 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
24 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27 //
28
29 #if NET_2_0
30
31 using NUnit.Framework;
32 using System;
33 using System.Reflection;
34 using System.Security;
35 using System.Security.Policy;
36
37 namespace MonoTests.System.Security {
38
39         [TestFixture]
40         public class HostSecurityManagerTest {
41
42                 [Test]
43                 public void Defaults ()
44                 {
45                         HostSecurityManager hsm = new HostSecurityManager ();
46                         Assert.IsNull (hsm.DomainPolicy, "DomainPolicy");
47                         Assert.AreEqual (HostSecurityManagerOptions.AllFlags, hsm.Flags, "Flags");
48                 }
49
50                 [Test]
51                 [ExpectedException (typeof (ArgumentNullException))]
52                 public void DetermineApplicationTrust_Null_Evidence_TrustManagerContext ()
53                 {
54                         HostSecurityManager hsm = new HostSecurityManager ();
55                         hsm.DetermineApplicationTrust (null, new Evidence (), new TrustManagerContext ());
56                 }
57
58                 [Test]
59                 [ExpectedException (typeof (ArgumentException))]
60                 public void DetermineApplicationTrust_Evidence_Null_TrustManagerContext ()
61                 {
62                         HostSecurityManager hsm = new HostSecurityManager ();
63                         hsm.DetermineApplicationTrust (new Evidence (), null, new TrustManagerContext ());
64                 }
65
66                 [Test]
67                 [ExpectedException (typeof (ArgumentException))]
68                 public void DetermineApplicationTrust_Evidence_Evidence_Null ()
69                 {
70                         HostSecurityManager hsm = new HostSecurityManager ();
71                         hsm.DetermineApplicationTrust (new Evidence (), new Evidence (), null);
72                 }
73
74                 [Test]
75                 public void ProvideAppDomainEvidence ()
76                 {
77                         HostSecurityManager hsm = new HostSecurityManager ();
78                         Assert.IsNull (hsm.ProvideAppDomainEvidence (null), "null");
79
80                         Evidence e = new Evidence ();
81                         Evidence result = hsm.ProvideAppDomainEvidence (e);
82                         Assert.IsNotNull (result, "empty");
83                         Assert.AreEqual (0, result.Count, "Count-0");
84
85                         e.AddHost (new Zone (SecurityZone.Untrusted));
86                         result = hsm.ProvideAppDomainEvidence (e);
87                         Assert.AreEqual (1, result.Count, "Count-1");
88                 }
89
90                 [Test]
91                 public void ProvideAssemblyEvidence ()
92                 {
93                         HostSecurityManager hsm = new HostSecurityManager ();
94                         Assembly a = Assembly.GetExecutingAssembly ();
95
96                         Evidence result = hsm.ProvideAssemblyEvidence (a, null);
97                         Assert.IsNull (result, "null");
98
99                         Evidence e = new Evidence ();
100                         result = hsm.ProvideAssemblyEvidence (a, e);
101                         Assert.AreEqual (0, result.Count, "Count-empty");
102
103                         e.AddHost (new Zone (SecurityZone.Untrusted));
104                         result = hsm.ProvideAssemblyEvidence (a, e);
105                         Assert.AreEqual (1, result.Count, "Count-1");
106                 }
107
108                 [Test]
109                 public void ProvideAssemblyEvidence_NullAssembly ()
110                 {
111                         HostSecurityManager hsm = new HostSecurityManager ();
112
113                         Evidence result = hsm.ProvideAssemblyEvidence (null, null);
114                         Assert.IsNull (result, "null");
115
116                         Evidence e = new Evidence ();
117                         result = hsm.ProvideAssemblyEvidence (null, e);
118                         Assert.AreEqual (0, result.Count, "Count-empty");
119
120                         e.AddHost (new Zone (SecurityZone.Untrusted));
121                         result = hsm.ProvideAssemblyEvidence (null, e);
122                         Assert.AreEqual (1, result.Count, "Count-1");
123                 }
124
125                 [Test]
126                 [ExpectedException (typeof (NullReferenceException))]
127                 public void ResolvePolicy_Null ()
128                 {
129                         HostSecurityManager hsm = new HostSecurityManager ();
130                         PermissionSet ps = hsm.ResolvePolicy (null);
131                 }
132
133                 [Test]
134                 public void ResolvePolicy_Empty ()
135                 {
136                         HostSecurityManager hsm = new HostSecurityManager ();
137                         PermissionSet ps = hsm.ResolvePolicy (new Evidence ());
138                         Assert.AreEqual (0, ps.Count, "Count");
139                         Assert.IsFalse (ps.IsUnrestricted (), "IsUnrestricted");
140                 }
141
142                 [Test]
143                 public void ResolvePolicy_CurrentAssemblyEvidence ()
144                 {
145                         HostSecurityManager hsm = new HostSecurityManager ();
146                         Assembly a = Assembly.GetExecutingAssembly ();
147                         PermissionSet ps = hsm.ResolvePolicy (a.Evidence);
148
149                         PermissionSet expected = SecurityManager.ResolvePolicy (a.Evidence);
150                         Assert.AreEqual (expected.ToString (), ps.ToString (), "PermissionSet");
151                 }
152         }
153 }
154
155 #endif