[bcl] Remove more NET_2_0 checks from class libs
[mono.git] / mcs / class / corlib / Test / System.Security.Policy / ApplicationSecurityManagerCas.cs
1 //
2 // ApplicationSecurityManagerCas.cs -
3 //      CAS unit tests for System.Security.Policy.ApplicationSecurityManager
4 //
5 // Author:
6 //      Sebastien Pouliot  <sebastien@ximian.com>
7 //
8 // Copyright (C) 2005 Novell, Inc (http://www.novell.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
31 using NUnit.Framework;
32
33 using System;
34 using System.Collections;
35 using System.Reflection;
36 using System.Security;
37 using System.Security.Permissions;
38 using System.Security.Policy;
39
40 namespace MonoCasTests.System.Security.Policy {
41
42         [TestFixture]
43         [Category ("CAS")]
44         public class ApplicationSecurityManagerCas {
45
46                 private string defaultTrustManagerTypeName;
47
48                 [TestFixtureSetUp]
49                 public void FixtureSetUp ()
50                 {
51                         defaultTrustManagerTypeName = ApplicationSecurityManager.ApplicationTrustManager.GetType ().AssemblyQualifiedName;
52                 }
53
54                 [SetUp]
55                 public void SetUp ()
56                 {
57                         if (!SecurityManager.SecurityEnabled)
58                                 Assert.Ignore ("SecurityManager.SecurityEnabled is OFF");
59                 }
60
61                 [Test]
62                 [SecurityPermission (SecurityAction.Deny, ControlPolicy = true)]
63                 [ExpectedException (typeof (SecurityException))]
64                 public void ApplicationTrustManager_DenyControlPolicy ()
65                 {
66                         Assert.IsNotNull (ApplicationSecurityManager.ApplicationTrustManager);
67                 }
68
69                 [Test]
70                 [SecurityPermission (SecurityAction.PermitOnly, ControlPolicy = true)]
71                 public void ApplicationTrustManager_PermitOnlyControlPolicy ()
72                 {
73                         Assert.IsNotNull (ApplicationSecurityManager.ApplicationTrustManager);
74                 }
75
76                 [Test]
77                 [SecurityPermission (SecurityAction.Deny, ControlPolicy = true)]
78                 [ExpectedException (typeof (SecurityException))]
79                 public void UserApplicationTrusts_DenyControlPolicy ()
80                 {
81                         // no security requirements documented
82                         Assert.AreEqual (0, ApplicationSecurityManager.UserApplicationTrusts.Count);
83                 }
84
85                 [Test]
86                 [SecurityPermission (SecurityAction.PermitOnly, ControlPolicy = true)]
87                 public void UserApplicationTrusts_PermitOnlyControlPolicy ()
88                 {
89                         // no security requirements documented
90                         Assert.IsNotNull (ApplicationSecurityManager.UserApplicationTrusts);
91                 }
92
93                 [Test]
94                 [PermissionSet (SecurityAction.Deny, Unrestricted = true)]
95                 [ExpectedException (typeof (NullReferenceException))]
96                 public void DetermineApplicationTrust_DenyUnrestricted ()
97                 {
98                         // documented as requiring ControlPolicy and ControlEvidence
99                         // possibly a linkdemand as only ControlPolicy seems check by the default
100                         // IApplicationTrustManager
101                         ApplicationSecurityManager.DetermineApplicationTrust (null, null);
102                 }
103
104                 // default trust manager
105
106                 [Test]
107                 [SecurityPermission (SecurityAction.Deny, ControlPolicy = true)]
108                 [ExpectedException (typeof (SecurityException))]
109                 public void DefaultTrustManager_DetermineApplicationTrust_DenyControlPolicy ()
110                 {
111                         ApplicationSecurityManager.ApplicationTrustManager.DetermineApplicationTrust (null, null);
112                 }
113
114                 [Test]
115                 [SecurityPermission (SecurityAction.PermitOnly, ControlPolicy = true)]
116                 [ExpectedException (typeof (ArgumentNullException))]
117                 public void DefaultTrustManager_DetermineApplicationTrust_PermitOnlyControlPolicy ()
118                 {
119                         ApplicationSecurityManager.ApplicationTrustManager.DetermineApplicationTrust (null, null);
120                 }
121
122                 private void CheckXml (SecurityElement se)
123                 {
124                         Assert.AreEqual (defaultTrustManagerTypeName, se.Attribute ("class"), "class");
125                         Assert.AreEqual ("1", se.Attribute ("version"), "version");
126                         Assert.AreEqual (2, se.Attributes.Count, "Count");
127                         Assert.IsNull (se.Children, "Children");
128                 }
129
130                 [PermissionSet (SecurityAction.Deny, Unrestricted = true)]
131                 private void CheckFromXml (IApplicationTrustManager atm)
132                 {
133                         SecurityElement se = new SecurityElement ("IApplicationTrustManager");
134                         se.AddAttribute ("class", defaultTrustManagerTypeName);
135                         se.AddAttribute ("version", "1");
136                         atm.FromXml (se);
137                         // accepted
138                         CheckXml (atm.ToXml ());
139                 }
140
141                 [Test]
142                 public void DefaultTrustManager_FromXml ()
143                 {
144                         CheckFromXml (ApplicationSecurityManager.ApplicationTrustManager);
145                 }
146
147                 [PermissionSet (SecurityAction.Deny, Unrestricted = true)]
148                 public void CheckToXml (IApplicationTrustManager atm)
149                 {
150                         CheckXml (atm.ToXml ());
151                 }
152
153                 [Test]
154                 public void DefaultTrustManager_ToXml ()
155                 {
156                         CheckToXml (ApplicationSecurityManager.ApplicationTrustManager);
157                 }
158         }
159 }
160