merging the Mainsoft branch to the trunk
[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.Runtime.Hosting;
35 using System.Security;
36 using System.Security.Policy;
37
38 namespace MonoTests.System.Security {
39
40         [TestFixture]
41         public class HostSecurityManagerTest {
42
43                 [Test]
44                 public void Defaults ()
45                 {
46                         HostSecurityManager hsm = new HostSecurityManager ();
47                         Assert.IsNull (hsm.DomainPolicy, "DomainPolicy");
48                         Assert.AreEqual (HostSecurityManagerOptions.AllFlags, hsm.Flags, "Flags");
49                 }
50
51                 [Test]
52                 [ExpectedException (typeof (ArgumentNullException))]
53                 public void DetermineApplicationTrust_Null_Evidence_TrustManagerContext ()
54                 {
55                         HostSecurityManager hsm = new HostSecurityManager ();
56                         hsm.DetermineApplicationTrust (null, new Evidence (), new TrustManagerContext ());
57                 }
58
59                 [Test]
60                 [Category ("NotWorking")]
61                 [ExpectedException (typeof (ArgumentException))]
62                 public void DetermineApplicationTrust_Evidence_Null_TrustManagerContext ()
63                 {
64                         HostSecurityManager hsm = new HostSecurityManager ();
65                         Evidence app = new Evidence ();
66                         //app.AddHost (new ActivationArgument ());
67                         hsm.DetermineApplicationTrust (app, null, new TrustManagerContext ());
68                 }
69
70                 [Test]
71                 [Category ("NotWorking")]
72                 [ExpectedException (typeof (ArgumentException))]
73                 public void DetermineApplicationTrust_Evidence_Evidence_Null ()
74                 {
75                         HostSecurityManager hsm = new HostSecurityManager ();
76                         hsm.DetermineApplicationTrust (new Evidence (), new Evidence (), null);
77                 }
78
79                 [Test]
80                 public void ProvideAppDomainEvidence ()
81                 {
82                         HostSecurityManager hsm = new HostSecurityManager ();
83                         Assert.IsNull (hsm.ProvideAppDomainEvidence (null), "null");
84
85                         Evidence e = new Evidence ();
86                         Evidence result = hsm.ProvideAppDomainEvidence (e);
87                         Assert.IsNotNull (result, "empty");
88                         Assert.AreEqual (0, result.Count, "Count-0");
89
90                         e.AddHost (new Zone (SecurityZone.Untrusted));
91                         result = hsm.ProvideAppDomainEvidence (e);
92                         Assert.AreEqual (1, result.Count, "Count-1");
93                 }
94
95                 [Test]
96                 public void ProvideAssemblyEvidence ()
97                 {
98                         HostSecurityManager hsm = new HostSecurityManager ();
99                         Assembly a = Assembly.GetExecutingAssembly ();
100
101                         Evidence result = hsm.ProvideAssemblyEvidence (a, null);
102                         Assert.IsNull (result, "null");
103
104                         Evidence e = new Evidence ();
105                         result = hsm.ProvideAssemblyEvidence (a, e);
106                         Assert.AreEqual (0, result.Count, "Count-empty");
107
108                         e.AddHost (new Zone (SecurityZone.Untrusted));
109                         result = hsm.ProvideAssemblyEvidence (a, e);
110                         Assert.AreEqual (1, result.Count, "Count-1");
111                 }
112
113                 [Test]
114                 public void ProvideAssemblyEvidence_NullAssembly ()
115                 {
116                         HostSecurityManager hsm = new HostSecurityManager ();
117
118                         Evidence result = hsm.ProvideAssemblyEvidence (null, null);
119                         Assert.IsNull (result, "null");
120
121                         Evidence e = new Evidence ();
122                         result = hsm.ProvideAssemblyEvidence (null, e);
123                         Assert.AreEqual (0, result.Count, "Count-empty");
124
125                         e.AddHost (new Zone (SecurityZone.Untrusted));
126                         result = hsm.ProvideAssemblyEvidence (null, e);
127                         Assert.AreEqual (1, result.Count, "Count-1");
128                 }
129
130                 [Test]
131                 [ExpectedException (typeof (NullReferenceException))]
132                 public void ResolvePolicy_Null ()
133                 {
134                         HostSecurityManager hsm = new HostSecurityManager ();
135                         PermissionSet ps = hsm.ResolvePolicy (null);
136                 }
137
138                 [Test]
139                 public void ResolvePolicy_Empty ()
140                 {
141                         HostSecurityManager hsm = new HostSecurityManager ();
142                         PermissionSet ps = hsm.ResolvePolicy (new Evidence ());
143                         Assert.AreEqual (0, ps.Count, "Count");
144                         Assert.IsFalse (ps.IsUnrestricted (), "IsUnrestricted");
145                 }
146
147                 [Test]
148                 public void ResolvePolicy_CurrentAssemblyEvidence ()
149                 {
150                         HostSecurityManager hsm = new HostSecurityManager ();
151                         Assembly a = Assembly.GetExecutingAssembly ();
152                         PermissionSet ps = hsm.ResolvePolicy (a.Evidence);
153
154                         PermissionSet expected = SecurityManager.ResolvePolicy (a.Evidence);
155                         Assert.AreEqual (expected.ToString (), ps.ToString (), "PermissionSet");
156                 }
157         }
158 }
159
160 #endif