2 // HostSecurityManagerTest.cs - NUnit Test Cases for HostSecurityManager
5 // Sebastien Pouliot <sebastien@ximian.com>
7 // Copyright (C) 2005 Novell, Inc (http://www.novell.com)
8 // Copyright 2011 Xamarin Inc (http://www.xamarin.com).
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:
18 // The above copyright notice and this permission notice shall be
19 // included in all copies or substantial portions of the Software.
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.
30 // MOBILE profile lacks some (of the few) CAS features required to execute those tests
31 #if !MOBILE && !MONOMAC
33 using NUnit.Framework;
35 using System.Reflection;
36 using System.Security;
37 using System.Security.Policy;
39 namespace MonoTests.System.Security {
42 public class HostSecurityManagerTest {
45 public void Defaults ()
47 HostSecurityManager hsm = new HostSecurityManager ();
48 Assert.IsNull (hsm.DomainPolicy, "DomainPolicy");
49 Assert.AreEqual (HostSecurityManagerOptions.AllFlags, hsm.Flags, "Flags");
53 [ExpectedException (typeof (ArgumentNullException))]
54 public void DetermineApplicationTrust_Null_Evidence_TrustManagerContext ()
56 HostSecurityManager hsm = new HostSecurityManager ();
57 hsm.DetermineApplicationTrust (null, new Evidence (), new TrustManagerContext ());
61 [ExpectedException (typeof (ArgumentException))]
62 public void DetermineApplicationTrust_Evidence_Null_TrustManagerContext ()
64 HostSecurityManager hsm = new HostSecurityManager ();
65 hsm.DetermineApplicationTrust (new Evidence (), null, new TrustManagerContext ());
69 [ExpectedException (typeof (ArgumentException))]
70 public void DetermineApplicationTrust_Evidence_Evidence_Null ()
72 HostSecurityManager hsm = new HostSecurityManager ();
73 hsm.DetermineApplicationTrust (new Evidence (), new Evidence (), null);
77 public void ProvideAppDomainEvidence ()
79 HostSecurityManager hsm = new HostSecurityManager ();
80 Assert.IsNull (hsm.ProvideAppDomainEvidence (null), "null");
82 Evidence e = new Evidence ();
83 Evidence result = hsm.ProvideAppDomainEvidence (e);
84 Assert.IsNotNull (result, "empty");
85 Assert.AreEqual (0, result.Count, "Count-0");
87 e.AddHost (new Zone (SecurityZone.Untrusted));
88 result = hsm.ProvideAppDomainEvidence (e);
89 Assert.AreEqual (1, result.Count, "Count-1");
93 public void ProvideAssemblyEvidence ()
95 HostSecurityManager hsm = new HostSecurityManager ();
96 Assembly a = Assembly.GetExecutingAssembly ();
98 Evidence result = hsm.ProvideAssemblyEvidence (a, null);
99 Assert.IsNull (result, "null");
101 Evidence e = new Evidence ();
102 result = hsm.ProvideAssemblyEvidence (a, e);
103 Assert.AreEqual (0, result.Count, "Count-empty");
105 e.AddHost (new Zone (SecurityZone.Untrusted));
106 result = hsm.ProvideAssemblyEvidence (a, e);
107 Assert.AreEqual (1, result.Count, "Count-1");
111 public void ProvideAssemblyEvidence_NullAssembly ()
113 HostSecurityManager hsm = new HostSecurityManager ();
115 Evidence result = hsm.ProvideAssemblyEvidence (null, null);
116 Assert.IsNull (result, "null");
118 Evidence e = new Evidence ();
119 result = hsm.ProvideAssemblyEvidence (null, e);
120 Assert.AreEqual (0, result.Count, "Count-empty");
122 e.AddHost (new Zone (SecurityZone.Untrusted));
123 result = hsm.ProvideAssemblyEvidence (null, e);
124 Assert.AreEqual (1, result.Count, "Count-1");
128 [ExpectedException (typeof (NullReferenceException))]
129 public void ResolvePolicy_Null ()
131 HostSecurityManager hsm = new HostSecurityManager ();
132 PermissionSet ps = hsm.ResolvePolicy (null);
136 public void ResolvePolicy_Empty ()
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");
145 public void ResolvePolicy_CurrentAssemblyEvidence ()
147 HostSecurityManager hsm = new HostSecurityManager ();
148 Assembly a = Assembly.GetExecutingAssembly ();
149 PermissionSet ps = hsm.ResolvePolicy (a.Evidence);
151 PermissionSet expected = SecurityManager.ResolvePolicy (a.Evidence);
152 Assert.AreEqual (expected.ToString (), ps.ToString (), "PermissionSet");