2 // HostSecurityManagerTest.cs - NUnit Test Cases for HostSecurityManager
5 // Sebastien Pouliot <sebastien@ximian.com>
7 // Copyright (C) 2005 Novell, Inc (http://www.novell.com)
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:
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
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.
31 using NUnit.Framework;
33 using System.Reflection;
34 using System.Security;
35 using System.Security.Policy;
37 namespace MonoTests.System.Security {
40 public class HostSecurityManagerTest {
43 public void Defaults ()
45 HostSecurityManager hsm = new HostSecurityManager ();
46 Assert.IsNull (hsm.DomainPolicy, "DomainPolicy");
47 Assert.AreEqual (HostSecurityManagerOptions.AllFlags, hsm.Flags, "Flags");
51 [ExpectedException (typeof (ArgumentNullException))]
52 public void DetermineApplicationTrust_Null_Evidence_TrustManagerContext ()
54 HostSecurityManager hsm = new HostSecurityManager ();
55 hsm.DetermineApplicationTrust (null, new Evidence (), new TrustManagerContext ());
59 [ExpectedException (typeof (ArgumentException))]
60 public void DetermineApplicationTrust_Evidence_Null_TrustManagerContext ()
62 HostSecurityManager hsm = new HostSecurityManager ();
63 hsm.DetermineApplicationTrust (new Evidence (), null, new TrustManagerContext ());
67 [ExpectedException (typeof (ArgumentException))]
68 public void DetermineApplicationTrust_Evidence_Evidence_Null ()
70 HostSecurityManager hsm = new HostSecurityManager ();
71 hsm.DetermineApplicationTrust (new Evidence (), new Evidence (), null);
75 public void ProvideAppDomainEvidence ()
77 HostSecurityManager hsm = new HostSecurityManager ();
78 Assert.IsNull (hsm.ProvideAppDomainEvidence (null), "null");
80 Evidence e = new Evidence ();
81 Evidence result = hsm.ProvideAppDomainEvidence (e);
82 Assert.IsNotNull (result, "empty");
83 Assert.AreEqual (0, result.Count, "Count-0");
85 e.AddHost (new Zone (SecurityZone.Untrusted));
86 result = hsm.ProvideAppDomainEvidence (e);
87 Assert.AreEqual (1, result.Count, "Count-1");
91 public void ProvideAssemblyEvidence ()
93 HostSecurityManager hsm = new HostSecurityManager ();
94 Assembly a = Assembly.GetExecutingAssembly ();
96 Evidence result = hsm.ProvideAssemblyEvidence (a, null);
97 Assert.IsNull (result, "null");
99 Evidence e = new Evidence ();
100 result = hsm.ProvideAssemblyEvidence (a, e);
101 Assert.AreEqual (0, result.Count, "Count-empty");
103 e.AddHost (new Zone (SecurityZone.Untrusted));
104 result = hsm.ProvideAssemblyEvidence (a, e);
105 Assert.AreEqual (1, result.Count, "Count-1");
109 public void ProvideAssemblyEvidence_NullAssembly ()
111 HostSecurityManager hsm = new HostSecurityManager ();
113 Evidence result = hsm.ProvideAssemblyEvidence (null, null);
114 Assert.IsNull (result, "null");
116 Evidence e = new Evidence ();
117 result = hsm.ProvideAssemblyEvidence (null, e);
118 Assert.AreEqual (0, result.Count, "Count-empty");
120 e.AddHost (new Zone (SecurityZone.Untrusted));
121 result = hsm.ProvideAssemblyEvidence (null, e);
122 Assert.AreEqual (1, result.Count, "Count-1");
126 [ExpectedException (typeof (NullReferenceException))]
127 public void ResolvePolicy_Null ()
129 HostSecurityManager hsm = new HostSecurityManager ();
130 PermissionSet ps = hsm.ResolvePolicy (null);
134 public void ResolvePolicy_Empty ()
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");
143 public void ResolvePolicy_CurrentAssemblyEvidence ()
145 HostSecurityManager hsm = new HostSecurityManager ();
146 Assembly a = Assembly.GetExecutingAssembly ();
147 PermissionSet ps = hsm.ResolvePolicy (a.Evidence);
149 PermissionSet expected = SecurityManager.ResolvePolicy (a.Evidence);
150 Assert.AreEqual (expected.ToString (), ps.ToString (), "PermissionSet");