Copied remotely
[mono.git] / mcs / class / corlib / Test / System.Security.Permissions / HostProtectionAttributeTest.cs
1 //
2 // HostProtectionAttributeTest.cs - NUnit Test Cases for HostProtectionAttribute
3 //
4 // Author:
5 //      Sebastien Pouliot  <sebastien@ximian.com>
6 //
7 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
8 //
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:
16 // 
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
19 // 
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.
27 //
28
29 #if NET_2_0
30
31 using NUnit.Framework;
32 using System;
33 using System.Security;
34 using System.Security.Permissions;
35
36 namespace MonoTests.System.Security.Permissions {
37
38         [TestFixture]
39         public class HostProtectionAttributeTest {
40
41                 private void DefaultTests (HostProtectionAttribute hpa)
42                 {
43                         Assert.AreEqual (SecurityAction.LinkDemand, hpa.Action, "Action");
44                         Assert.AreEqual (HostProtectionResource.None, hpa.Resources, "Resources");
45                         Assert.IsFalse (hpa.ExternalProcessMgmt, "ExternalProcessMgmt");
46                         Assert.IsFalse (hpa.ExternalThreading, "ExternalThreading");
47                         Assert.IsFalse (hpa.MayLeakOnAbort, "MayLeakOnAbort");
48                         Assert.IsFalse (hpa.SecurityInfrastructure, "SecurityInfrastructure");
49                         Assert.IsFalse (hpa.SelfAffectingProcessMgmt, "SelfAffectingProcessMgmt");
50                         Assert.IsFalse (hpa.SelfAffectingThreading, "SelfAffectingThreading");
51                         Assert.IsFalse (hpa.SharedState, "SharedState");
52                         Assert.IsFalse (hpa.Synchronization, "Synchronization");
53                         Assert.IsFalse (hpa.UI, "UI");
54                         Assert.IsFalse (hpa.Unrestricted, "Unrestricted");
55                         IPermission p = hpa.CreatePermission ();
56                         Assert.AreEqual ("System.Security.Permissions.HostProtectionPermission", p.GetType ().ToString (), "CreatePermission");
57                         Assert.IsTrue ((p is IUnrestrictedPermission), "IUnrestrictedPermission");
58                 }
59
60                 [Test]
61                 public void HostProtectionAttribute_Empty ()
62                 {
63                         // note: normally security attributes don't have an empty constructor
64                         HostProtectionAttribute hpa = new HostProtectionAttribute ();
65                         DefaultTests (hpa);
66                 }
67
68                 [Test]
69                 [ExpectedException (typeof (ArgumentException))]
70                 public void HostProtectionAttribute_Assert ()
71                 {
72                         new HostProtectionAttribute (SecurityAction.Assert);
73                 }
74
75                 [Test]
76                 [ExpectedException (typeof (ArgumentException))]
77                 public void HostProtectionAttribute_Demand ()
78                 {
79                         new HostProtectionAttribute (SecurityAction.Demand);
80                 }
81
82                 [Test]
83                 [ExpectedException (typeof (ArgumentException))]
84                 public void HostProtectionAttribute_DemandChoice ()
85                 {
86                         new HostProtectionAttribute (SecurityAction.DemandChoice);
87                 }
88
89                 [Test]
90                 [ExpectedException (typeof (ArgumentException))]
91                 public void HostProtectionAttribute_Deny ()
92                 {
93                         new HostProtectionAttribute (SecurityAction.Deny);
94                 }
95
96                 [Test]
97                 [ExpectedException (typeof (ArgumentException))]
98                 public void HostProtectionAttribute_InheritanceDemand ()
99                 {
100                         new HostProtectionAttribute (SecurityAction.InheritanceDemand);
101                 }
102
103                 [Test]
104                 [ExpectedException (typeof (ArgumentException))]
105                 public void HostProtectionAttribute_InheritanceDemandChoice ()
106                 {
107                         new HostProtectionAttribute (SecurityAction.InheritanceDemandChoice);
108                 }
109
110                 [Test]
111                 public void HostProtectionAttribute_LinkDemand ()
112                 {
113                         HostProtectionAttribute hpa = new HostProtectionAttribute (SecurityAction.LinkDemand);
114                         DefaultTests (hpa);
115                 }
116
117                 [Test]
118                 [ExpectedException (typeof (ArgumentException))]
119                 public void HostProtectionAttribute_LinkDemandChoice ()
120                 {
121                         new HostProtectionAttribute (SecurityAction.LinkDemandChoice);
122                 }
123
124                 [Test]
125                 [ExpectedException (typeof (ArgumentException))]
126                 public void HostProtectionAttribute_PermitOnly ()
127                 {
128                         new HostProtectionAttribute (SecurityAction.PermitOnly);
129                 }
130
131                 [Test]
132                 [ExpectedException (typeof (ArgumentException))]
133                 public void HostProtectionAttribute_RequestMinimum ()
134                 {
135                         new HostProtectionAttribute (SecurityAction.RequestMinimum);
136                 }
137
138                 [Test]
139                 [ExpectedException (typeof (ArgumentException))]
140                 public void HostProtectionAttribute_RequestOptional ()
141                 {
142                         new HostProtectionAttribute (SecurityAction.RequestOptional);
143                 }
144
145                 [Test]
146                 [ExpectedException (typeof (ArgumentException))]
147                 public void HostProtectionAttribute_RequestRefuse ()
148                 {
149                         new HostProtectionAttribute (SecurityAction.RequestRefuse);
150                 }
151
152                 [Test]
153                 public void Properties () 
154                 {
155                         HostProtectionAttribute hpa = new HostProtectionAttribute (SecurityAction.LinkDemand);
156                         HostProtectionResource expected = HostProtectionResource.None;
157                         Assert.AreEqual (expected, hpa.Resources, "None");
158                         Assert.IsFalse (hpa.Unrestricted, "Unrestricted-1");
159
160                         hpa.ExternalProcessMgmt = true;
161                         expected |= HostProtectionResource.ExternalProcessMgmt;
162                         Assert.AreEqual (expected, hpa.Resources, "+ExternalProcessMgmt");
163                         Assert.IsFalse (hpa.Unrestricted, "Unrestricted-2");
164
165                         hpa.ExternalThreading = true;
166                         expected |= HostProtectionResource.ExternalThreading;
167                         Assert.AreEqual (expected, hpa.Resources, "+ExternalThreading");
168                         Assert.IsFalse (hpa.Unrestricted, "Unrestricted-3");
169
170                         hpa.MayLeakOnAbort = true;
171                         expected |= HostProtectionResource.MayLeakOnAbort;
172                         Assert.AreEqual (expected, hpa.Resources, "+MayLeakOnAbort");
173                         Assert.IsFalse (hpa.Unrestricted, "Unrestricted-4");
174
175                         hpa.SecurityInfrastructure = true;
176                         expected |= HostProtectionResource.SecurityInfrastructure;
177                         Assert.AreEqual (expected, hpa.Resources, "+SecurityInfrastructure");
178                         Assert.IsFalse (hpa.Unrestricted, "Unrestricted-5");
179
180                         hpa.SelfAffectingProcessMgmt = true;
181                         expected |= HostProtectionResource.SelfAffectingProcessMgmt;
182                         Assert.AreEqual (expected, hpa.Resources, "+SelfAffectingProcessMgmt");
183                         Assert.IsFalse (hpa.Unrestricted, "Unrestricted-6");
184
185                         hpa.SelfAffectingThreading = true;
186                         expected |= HostProtectionResource.SelfAffectingThreading;
187                         Assert.AreEqual (expected, hpa.Resources, "+SelfAffectingThreading");
188                         Assert.IsFalse (hpa.Unrestricted, "Unrestricted-7");
189
190                         hpa.SharedState = true;
191                         expected |= HostProtectionResource.SharedState;
192                         Assert.AreEqual (expected, hpa.Resources, "+SharedState");
193                         Assert.IsFalse (hpa.Unrestricted, "Unrestricted-8");
194
195                         hpa.Synchronization = true;
196                         expected |= HostProtectionResource.Synchronization;
197                         Assert.AreEqual (expected, hpa.Resources, "+Synchronization");
198                         Assert.IsFalse (hpa.Unrestricted, "Unrestricted-9");
199
200                         hpa.UI = true;
201                         expected |= HostProtectionResource.UI;
202                         Assert.AreEqual (expected, hpa.Resources, "+UI");
203
204                         Assert.IsFalse (hpa.Unrestricted, "Unrestricted");
205                 }
206
207                 [Test]
208                 public void Attributes ()
209                 {
210                         Type t = typeof (HostProtectionAttribute);
211                         Assert.IsTrue (t.IsSerializable, "IsSerializable");
212
213                         object [] attrs = t.GetCustomAttributes (typeof (AttributeUsageAttribute), false);
214                         Assert.AreEqual (1, attrs.Length, "AttributeUsage");
215                         AttributeUsageAttribute aua = (AttributeUsageAttribute)attrs [0];
216                         Assert.IsTrue (aua.AllowMultiple, "AllowMultiple");
217                         Assert.IsFalse (aua.Inherited, "Inherited");
218                         AttributeTargets at = (AttributeTargets.Assembly | AttributeTargets.Class | AttributeTargets.Struct | AttributeTargets.Constructor | AttributeTargets.Method | AttributeTargets.Delegate);
219                         Assert.AreEqual (at, aua.ValidOn, "ValidOn");
220                 }
221         }
222 }
223
224 #endif