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