* roottypes.cs: Rename from tree.cs.
[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_Deny ()
85                 {
86                         new HostProtectionAttribute (SecurityAction.Deny);
87                 }
88
89                 [Test]
90                 [ExpectedException (typeof (ArgumentException))]
91                 public void HostProtectionAttribute_InheritanceDemand ()
92                 {
93                         new HostProtectionAttribute (SecurityAction.InheritanceDemand);
94                 }
95
96                 [Test]
97                 public void HostProtectionAttribute_LinkDemand ()
98                 {
99                         HostProtectionAttribute hpa = new HostProtectionAttribute (SecurityAction.LinkDemand);
100                         DefaultTests (hpa);
101                 }
102
103                 [Test]
104                 [ExpectedException (typeof (ArgumentException))]
105                 public void HostProtectionAttribute_PermitOnly ()
106                 {
107                         new HostProtectionAttribute (SecurityAction.PermitOnly);
108                 }
109
110                 [Test]
111                 [ExpectedException (typeof (ArgumentException))]
112                 public void HostProtectionAttribute_RequestMinimum ()
113                 {
114                         new HostProtectionAttribute (SecurityAction.RequestMinimum);
115                 }
116
117                 [Test]
118                 [ExpectedException (typeof (ArgumentException))]
119                 public void HostProtectionAttribute_RequestOptional ()
120                 {
121                         new HostProtectionAttribute (SecurityAction.RequestOptional);
122                 }
123
124                 [Test]
125                 [ExpectedException (typeof (ArgumentException))]
126                 public void HostProtectionAttribute_RequestRefuse ()
127                 {
128                         new HostProtectionAttribute (SecurityAction.RequestRefuse);
129                 }
130
131                 private HostProtectionAttribute Empty () 
132                 {
133                         HostProtectionAttribute a = new HostProtectionAttribute ();
134                         a.Synchronization = false;
135                         a.SharedState = false;
136                         a.ExternalProcessMgmt = false;
137                         a.SelfAffectingProcessMgmt = false;
138                         a.ExternalThreading = false;
139                         a.SelfAffectingThreading = false;
140                         a.SecurityInfrastructure = false;
141                         a.UI = false;
142                         a.MayLeakOnAbort = false;
143                         Assert.AreEqual (HostProtectionResource.None, a.Resources, "Resources=None");
144                         return a;
145                 }
146
147                 [Test]
148                 public void Synchronization () 
149                 {
150                         HostProtectionAttribute a = Empty ();
151                         a.Synchronization = true;
152                         Assert.AreEqual (HostProtectionResource.Synchronization, a.Resources, "Resources=Synchronization");
153                         a.Synchronization = false;
154                         Assert.AreEqual (HostProtectionResource.None, a.Resources, "Resources=None");
155                 }
156
157                 [Test]
158                 public void SharedState () 
159                 {
160                         HostProtectionAttribute a = Empty ();
161                         a.SharedState = true;
162                         Assert.AreEqual (HostProtectionResource.SharedState, a.Resources, "Resources=SharedState");
163                         a.SharedState = false;
164                         Assert.AreEqual (HostProtectionResource.None, a.Resources, "Resources=None");
165                 }
166
167                 [Test]
168                 public void ExternalProcessMgmt () 
169                 {
170                         HostProtectionAttribute a = Empty ();
171                         a.ExternalProcessMgmt = true;
172                         Assert.AreEqual (HostProtectionResource.ExternalProcessMgmt, a.Resources, "Resources=ExternalProcessMgmt");
173                         a.ExternalProcessMgmt = false;
174                         Assert.AreEqual (HostProtectionResource.None, a.Resources, "Resources=None");
175                 }
176
177                 [Test]
178                 public void SelfAffectingProcessMgmt () 
179                 {
180                         HostProtectionAttribute a = Empty ();
181                         a.SelfAffectingProcessMgmt = true;
182                         Assert.AreEqual (HostProtectionResource.SelfAffectingProcessMgmt, a.Resources, "Resources=SelfAffectingProcessMgmt");
183                         a.SelfAffectingProcessMgmt = false;
184                         Assert.AreEqual (HostProtectionResource.None, a.Resources, "Resources=None");
185                 }
186
187                 [Test]
188                 public void ExternalThreading () 
189                 {
190                         HostProtectionAttribute a = Empty ();
191                         a.ExternalThreading = true;
192                         Assert.AreEqual (HostProtectionResource.ExternalThreading, a.Resources, "Resources=ExternalThreading");
193                         a.ExternalThreading = false;
194                         Assert.AreEqual (HostProtectionResource.None, a.Resources, "Resources=None");
195                 }
196
197                 [Test]
198                 public void SelfAffectingThreading () 
199                 {
200                         HostProtectionAttribute a = Empty ();
201                         a.SelfAffectingThreading = true;
202                         Assert.AreEqual (HostProtectionResource.SelfAffectingThreading, a.Resources, "Resources=SelfAffectingThreading");
203                         a.SelfAffectingThreading = false;
204                         Assert.AreEqual (HostProtectionResource.None, a.Resources, "Resources=None");
205                 }
206
207                 [Test]
208                 public void SecurityInfrastructure () 
209                 {
210                         HostProtectionAttribute a = Empty ();
211                         a.SecurityInfrastructure = true;
212                         Assert.AreEqual (HostProtectionResource.SecurityInfrastructure, a.Resources, "Resources=SecurityInfrastructure");
213                         a.SecurityInfrastructure = false;
214                         Assert.AreEqual (HostProtectionResource.None, a.Resources, "Resources=None");
215                 }
216
217                 [Test]
218                 public void UI () 
219                 {
220                         HostProtectionAttribute a = Empty ();
221                         a.UI = true;
222                         Assert.AreEqual (HostProtectionResource.UI, a.Resources, "Resources=UI");
223                         a.UI = false;
224                         Assert.AreEqual (HostProtectionResource.None, a.Resources, "Resources=None");
225                 }
226
227                 [Test]
228                 public void MayLeakOnAbort () 
229                 {
230                         HostProtectionAttribute a = Empty ();
231                         a.MayLeakOnAbort = true;
232                         Assert.AreEqual (HostProtectionResource.MayLeakOnAbort, a.Resources, "Resources=MayLeakOnAbort");
233                         a.MayLeakOnAbort = false;
234                         Assert.AreEqual (HostProtectionResource.None, a.Resources, "Resources=None");
235                 }
236
237                 [Test]
238                 public void Properties () 
239                 {
240                         HostProtectionAttribute hpa = new HostProtectionAttribute (SecurityAction.LinkDemand);
241                         HostProtectionResource expected = HostProtectionResource.None;
242                         Assert.AreEqual (expected, hpa.Resources, "None");
243                         Assert.IsFalse (hpa.Unrestricted, "Unrestricted-1");
244
245                         hpa.ExternalProcessMgmt = true;
246                         expected |= HostProtectionResource.ExternalProcessMgmt;
247                         Assert.AreEqual (expected, hpa.Resources, "+ExternalProcessMgmt");
248                         Assert.IsFalse (hpa.Unrestricted, "Unrestricted-2");
249
250                         hpa.ExternalThreading = true;
251                         expected |= HostProtectionResource.ExternalThreading;
252                         Assert.AreEqual (expected, hpa.Resources, "+ExternalThreading");
253                         Assert.IsFalse (hpa.Unrestricted, "Unrestricted-3");
254
255                         hpa.MayLeakOnAbort = true;
256                         expected |= HostProtectionResource.MayLeakOnAbort;
257                         Assert.AreEqual (expected, hpa.Resources, "+MayLeakOnAbort");
258                         Assert.IsFalse (hpa.Unrestricted, "Unrestricted-4");
259
260                         hpa.SecurityInfrastructure = true;
261                         expected |= HostProtectionResource.SecurityInfrastructure;
262                         Assert.AreEqual (expected, hpa.Resources, "+SecurityInfrastructure");
263                         Assert.IsFalse (hpa.Unrestricted, "Unrestricted-5");
264
265                         hpa.SelfAffectingProcessMgmt = true;
266                         expected |= HostProtectionResource.SelfAffectingProcessMgmt;
267                         Assert.AreEqual (expected, hpa.Resources, "+SelfAffectingProcessMgmt");
268                         Assert.IsFalse (hpa.Unrestricted, "Unrestricted-6");
269
270                         hpa.SelfAffectingThreading = true;
271                         expected |= HostProtectionResource.SelfAffectingThreading;
272                         Assert.AreEqual (expected, hpa.Resources, "+SelfAffectingThreading");
273                         Assert.IsFalse (hpa.Unrestricted, "Unrestricted-7");
274
275                         hpa.SharedState = true;
276                         expected |= HostProtectionResource.SharedState;
277                         Assert.AreEqual (expected, hpa.Resources, "+SharedState");
278                         Assert.IsFalse (hpa.Unrestricted, "Unrestricted-8");
279
280                         hpa.Synchronization = true;
281                         expected |= HostProtectionResource.Synchronization;
282                         Assert.AreEqual (expected, hpa.Resources, "+Synchronization");
283                         Assert.IsFalse (hpa.Unrestricted, "Unrestricted-9");
284
285                         hpa.UI = true;
286                         expected |= HostProtectionResource.UI;
287                         Assert.AreEqual (expected, hpa.Resources, "+UI");
288
289                         Assert.IsFalse (hpa.Unrestricted, "Unrestricted");
290
291                         hpa.ExternalProcessMgmt = false;
292                         expected &= ~HostProtectionResource.ExternalProcessMgmt;
293                         Assert.AreEqual (expected, hpa.Resources, "-ExternalProcessMgmt");
294                         Assert.IsFalse (hpa.Unrestricted, "Unrestricted-10");
295
296                         hpa.ExternalThreading = false;
297                         expected &= ~HostProtectionResource.ExternalThreading;
298                         Assert.AreEqual (expected, hpa.Resources, "+ExternalThreading");
299                         Assert.IsFalse (hpa.Unrestricted, "Unrestricted-11");
300
301                         hpa.MayLeakOnAbort = false;
302                         expected &= ~HostProtectionResource.MayLeakOnAbort;
303                         Assert.AreEqual (expected, hpa.Resources, "+MayLeakOnAbort");
304                         Assert.IsFalse (hpa.Unrestricted, "Unrestricted-12");
305
306                         hpa.SecurityInfrastructure = false;
307                         expected &= ~HostProtectionResource.SecurityInfrastructure;
308                         Assert.AreEqual (expected, hpa.Resources, "+SecurityInfrastructure");
309                         Assert.IsFalse (hpa.Unrestricted, "Unrestricted-13");
310
311                         hpa.SelfAffectingProcessMgmt = false;
312                         expected &= ~HostProtectionResource.SelfAffectingProcessMgmt;
313                         Assert.AreEqual (expected, hpa.Resources, "+SelfAffectingProcessMgmt");
314                         Assert.IsFalse (hpa.Unrestricted, "Unrestricted-14");
315
316                         hpa.SelfAffectingThreading = false;
317                         expected &= ~HostProtectionResource.SelfAffectingThreading;
318                         Assert.AreEqual (expected, hpa.Resources, "+SelfAffectingThreading");
319                         Assert.IsFalse (hpa.Unrestricted, "Unrestricted-15");
320
321                         hpa.SharedState = false;
322                         expected &= ~HostProtectionResource.SharedState;
323                         Assert.AreEqual (expected, hpa.Resources, "+SharedState");
324                         Assert.IsFalse (hpa.Unrestricted, "Unrestricted-16");
325
326                         hpa.Synchronization = false;
327                         expected &= ~HostProtectionResource.Synchronization;
328                         Assert.AreEqual (expected, hpa.Resources, "+Synchronization");
329                         Assert.IsFalse (hpa.Unrestricted, "Unrestricted-17");
330
331                         hpa.UI = false;
332                         expected &= ~HostProtectionResource.UI;
333                         Assert.AreEqual (expected, hpa.Resources, "+UI");
334                         Assert.IsFalse (hpa.Unrestricted, "Unrestricted-18");
335                 }
336
337                 [Test]
338                 public void Attributes ()
339                 {
340                         Type t = typeof (HostProtectionAttribute);
341                         Assert.IsTrue (t.IsSerializable, "IsSerializable");
342
343                         object [] attrs = t.GetCustomAttributes (typeof (AttributeUsageAttribute), false);
344                         Assert.AreEqual (1, attrs.Length, "AttributeUsage");
345                         AttributeUsageAttribute aua = (AttributeUsageAttribute)attrs [0];
346                         Assert.IsTrue (aua.AllowMultiple, "AllowMultiple");
347                         Assert.IsFalse (aua.Inherited, "Inherited");
348                         AttributeTargets at = (AttributeTargets.Assembly | AttributeTargets.Class | AttributeTargets.Struct | AttributeTargets.Constructor | AttributeTargets.Method | AttributeTargets.Delegate);
349                         Assert.AreEqual (at, aua.ValidOn, "ValidOn");
350                 }
351         }
352 }
353
354 #endif