[bcl] Remove more NET_2_0 checks from class libs
[mono.git] / mcs / class / corlib / Test / System.Security.Policy / ApplicationSecurityManagerTest.cs
1 //
2 // ApplicationSecurityManagerTest.cs - 
3 //      NUnit Test Cases for 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 using System;
33 using System.Collections;
34 using System.Security;
35 using System.Security.Policy;
36
37 namespace MonoTests.System.Security.Policy {
38
39         [TestFixture]
40         public class ApplicationSecurityManagerTest {
41
42                 private string defaultTrustManagerTypeName;
43
44                 [TestFixtureSetUp]
45                 public void FixtureSetUp ()
46                 {
47                         defaultTrustManagerTypeName = ApplicationSecurityManager.ApplicationTrustManager.GetType ().AssemblyQualifiedName;
48                 }
49
50                 [Test]
51                 public void ApplicationTrustManager ()
52                 {
53                         Assert.IsNotNull (ApplicationSecurityManager.ApplicationTrustManager);
54                 }
55
56                 [Test]
57                 public void UserApplicationTrusts ()
58                 {
59                         Assert.AreEqual (0, ApplicationSecurityManager.UserApplicationTrusts.Count);
60                 }
61
62                 // FIXME: creating an ActivationContext here seems not easy
63
64                 [Test]
65 //              [ExpectedException (typeof (ArgumentNullException))]
66                 [ExpectedException (typeof (NullReferenceException))]
67                 public void DetermineApplicationTrust_Null_Null ()
68                 {
69                         ApplicationSecurityManager.DetermineApplicationTrust (null, null);
70                 }
71
72                 [Test]
73 //              [ExpectedException (typeof (ArgumentNullException))]
74                 [ExpectedException (typeof (NullReferenceException))]
75                 public void DetermineApplicationTrust_Null_TrustManagerContext ()
76                 {
77                         ApplicationSecurityManager.DetermineApplicationTrust (null, new TrustManagerContext ());
78                 }
79
80                 // testing the default application security manager here
81
82                 // FIXME: creating an ActivationContext here seems not easy
83
84                 [Test]
85                 [ExpectedException (typeof (ArgumentNullException))]
86                 public void DefaultTrustManager_DetermineApplicationTrust_Null_Null ()
87                 {
88                         ApplicationSecurityManager.ApplicationTrustManager.DetermineApplicationTrust (null, null);
89                 }
90
91                 [Test]
92                 [ExpectedException (typeof (ArgumentNullException))]
93                 public void DefaultTrustManager_DetermineApplicationTrust_Null_TrustManagerContext ()
94                 {
95                         ApplicationSecurityManager.ApplicationTrustManager.DetermineApplicationTrust (null, new TrustManagerContext ());
96                 }
97
98                 [Test]
99                 [ExpectedException (typeof (ArgumentNullException))]
100                 public void DefaultTrustManager_FromXml_Null ()
101                 {
102                         ApplicationSecurityManager.ApplicationTrustManager.FromXml (null);
103                 }
104
105                 [Test]
106                 [ExpectedException (typeof (ArgumentException))]
107                 public void DefaultTrustManager_FromXml_BadTag ()
108                 {
109                         SecurityElement se = new SecurityElement (String.Empty);
110                         ApplicationSecurityManager.ApplicationTrustManager.FromXml (se);
111                 }
112
113                 private void CheckXml (SecurityElement se)
114                 {
115                         Assert.AreEqual (defaultTrustManagerTypeName, se.Attribute ("class"), "class");
116                         Assert.AreEqual ("1", se.Attribute ("version"), "version");
117                         Assert.AreEqual (2, se.Attributes.Count, "Count");
118                         Assert.IsNull (se.Children, "Children");
119                 }
120
121                 [Test]
122                 public void DefaultTrustManager_FromXml_NoAttributes ()
123                 {
124                         SecurityElement se = new SecurityElement ("IApplicationTrustManager");
125                         ApplicationSecurityManager.ApplicationTrustManager.FromXml (se);
126                         // accepted
127                         CheckXml (ApplicationSecurityManager.ApplicationTrustManager.ToXml ());
128                 }
129
130                 [Test]
131                 public void DefaultTrustManager_FromXml_BadClass ()
132                 {
133                         SecurityElement se = new SecurityElement ("IApplicationTrustManager");
134                         se.AddAttribute ("class", "System.DoesntExist");
135                         se.AddAttribute ("version", "1");
136                         ApplicationSecurityManager.ApplicationTrustManager.FromXml (se);
137                         // accepted
138                         CheckXml (ApplicationSecurityManager.ApplicationTrustManager.ToXml ());
139                 }
140
141                 [Test]
142                 public void DefaultTrustManager_FromXml_BadVersion ()
143                 {
144                         SecurityElement se = new SecurityElement ("IApplicationTrustManager");
145                         se.AddAttribute ("class", defaultTrustManagerTypeName);
146                         se.AddAttribute ("version", "42");
147                         ApplicationSecurityManager.ApplicationTrustManager.FromXml (se);
148                         // accepted
149                         CheckXml (ApplicationSecurityManager.ApplicationTrustManager.ToXml ());
150                 }
151
152                 [Test]
153                 public void DefaultTrustManager_FromXml ()
154                 {
155                         SecurityElement se = new SecurityElement ("IApplicationTrustManager");
156                         se.AddAttribute ("class", defaultTrustManagerTypeName);
157                         se.AddAttribute ("version", "1");
158                         ApplicationSecurityManager.ApplicationTrustManager.FromXml (se);
159                         // accepted
160                         CheckXml (ApplicationSecurityManager.ApplicationTrustManager.ToXml ());
161                 }
162
163                 [Test]
164                 public void DefaultTrustManager_ToXml ()
165                 {
166                         CheckXml (ApplicationSecurityManager.ApplicationTrustManager.ToXml ());
167                 }
168         }
169 }
170