[bcl] Make mono cas opt-in features instead of opt-out for mobile profiles
[mono.git] / mcs / class / corlib / Test / System.Security / CodeAccessPermissionCas.cs
1 //
2 // CodeAccessPermissionCas.cs - 
3 //      CAS unit tests for System.Security.CodeAccessPermission
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 using NUnit.Framework;
31
32 using System;
33 using System.Collections;
34 using System.Reflection;
35 using System.Security;
36 using System.Security.Permissions;
37
38 namespace MonoCasTests.System.Security {
39
40         [TestFixture]
41         [Category ("CAS")]
42         public class CodeAccessPermissionCas {
43
44                 private const SecurityPermissionFlag Both = SecurityPermissionFlag.RemotingConfiguration | SecurityPermissionFlag.UnmanagedCode;
45
46                 private bool result;
47
48                 [SetUp]
49                 public void SetUp ()
50                 {
51                         if (!SecurityManager.SecurityEnabled)
52                                 Assert.Ignore ("SecurityManager.SecurityEnabled is OFF");
53                         result = false;
54                 }
55
56                 [SecurityPermission (SecurityAction.Demand, Assertion = true)]
57                 private void DemandAssertion ()
58                 {
59                         result = true;
60                 }
61
62                 [Test]
63                 [SecurityPermission (SecurityAction.Deny, Assertion = true)]
64                 [ExpectedException (typeof (SecurityException))]
65                 public void Declarative_DenyAssertion_Assert ()
66                 {
67                         DemandAssertion ();
68                 }
69
70                 [SecurityPermission (SecurityAction.Demand, UnmanagedCode = true)]
71                 private void DemandUnmanagedCode ()
72                 {
73                         result = true;
74                 }
75
76                 [SecurityPermission (SecurityAction.Demand, RemotingConfiguration = true)]
77                 private void DemandRemotingConfiguration ()
78                 {
79                         result = true;
80                 }
81
82                 [SecurityPermission (SecurityAction.Demand, UnmanagedCode = true, RemotingConfiguration = true)]
83                 private void DemandBoth ()
84                 {
85                         result = true;
86                 }
87
88                 private void AssertSecurity (SecurityPermissionFlag flag)
89                 {
90                         bool unmanaged = ((flag & SecurityPermissionFlag.UnmanagedCode) != 0);
91                         bool remoting = ((flag & SecurityPermissionFlag.RemotingConfiguration) != 0);
92
93                         if (unmanaged && remoting)
94                                 DemandBoth ();
95                         else if (unmanaged)
96                                 DemandUnmanagedCode ();
97                         else if (remoting)
98                                 DemandRemotingConfiguration ();
99                         else 
100                                 Assert.Fail ("Invalid demand");
101                 }
102
103                 [SecurityPermission (SecurityAction.Assert, UnmanagedCode = true)]
104                 private void AssertUnmanagedCode (SecurityPermissionFlag flag)
105                 {
106                         AssertSecurity (flag);
107                 }
108
109                 [SecurityPermission (SecurityAction.Assert, RemotingConfiguration = true)]
110                 private void AssertRemotingConfiguration (SecurityPermissionFlag flag)
111                 {
112                         AssertSecurity (flag);
113                 }
114
115                 [SecurityPermission (SecurityAction.Assert, UnmanagedCode = true, RemotingConfiguration = true)]
116                 private void AssertBoth (SecurityPermissionFlag flag)
117                 {
118                         AssertSecurity (flag);
119                 }
120
121                 [SecurityPermission (SecurityAction.Assert, Unrestricted = true)]
122                 private void AssertUnrestricted (SecurityPermissionFlag flag)
123                 {
124                         AssertSecurity (flag);
125                 }
126
127                 [Test]
128                 [SecurityPermission (SecurityAction.Deny, Unrestricted = true)]
129                 public void Declarative_DenyUnrestricted_AssertUnmanaged_DemandUnmanaged ()
130                 {
131                         AssertUnmanagedCode (SecurityPermissionFlag.UnmanagedCode);
132                         Assert.IsTrue (result);
133                 }
134
135                 [Test]
136                 [SecurityPermission (SecurityAction.Deny, Unrestricted = true)]
137                 [ExpectedException (typeof (SecurityException))]
138                 public void Declarative_DenyUnrestricted_AssertUnmanaged_DemandRemoting ()
139                 {
140                         AssertUnmanagedCode (SecurityPermissionFlag.RemotingConfiguration);
141                 }
142
143                 [Test]
144                 [SecurityPermission (SecurityAction.Deny, Unrestricted = true)]
145                 [ExpectedException (typeof (SecurityException))]
146                 public void Declarative_DenyUnrestricted_AssertUnmanaged_DemandBoth ()
147                 {
148                         AssertUnmanagedCode (Both);
149                 }
150
151                 [Test]
152                 [SecurityPermission (SecurityAction.Deny, Unrestricted = true)]
153                 [ExpectedException (typeof (SecurityException))]
154                 public void Declarative_DenyUnrestricted_AssertRemoting_DemandUnmanaged ()
155                 {
156                         AssertRemotingConfiguration (SecurityPermissionFlag.UnmanagedCode);
157                 }
158
159                 [Test]
160                 [SecurityPermission (SecurityAction.Deny, Unrestricted = true)]
161                 public void Declarative_DenyUnrestricted_AssertRemoting_DemandRemoting ()
162                 {
163                         AssertRemotingConfiguration (SecurityPermissionFlag.RemotingConfiguration);
164                         Assert.IsTrue (result);
165                 }
166
167                 [Test]
168                 [SecurityPermission (SecurityAction.Deny, Unrestricted = true)]
169                 [ExpectedException (typeof (SecurityException))]
170                 public void Declarative_DenyUnrestricted_AssertRemoting_DemandBoth ()
171                 {
172                         AssertRemotingConfiguration (Both);
173                 }
174
175                 [Test]
176                 [SecurityPermission (SecurityAction.Deny, Unrestricted = true)]
177                 public void Declarative_DenyUnrestricted_AssertBoth_DemandUnmanaged ()
178                 {
179                         AssertBoth (SecurityPermissionFlag.UnmanagedCode);
180                         Assert.IsTrue (result);
181                 }
182
183                 [Test]
184                 [SecurityPermission (SecurityAction.Deny, Unrestricted = true)]
185                 public void Declarative_DenyUnrestricted_AssertBoth_DemandRemoting ()
186                 {
187                         AssertBoth (SecurityPermissionFlag.RemotingConfiguration);
188                         Assert.IsTrue (result);
189                 }
190
191                 [Test]
192                 [SecurityPermission (SecurityAction.Deny, Unrestricted = true)]
193                 public void Declarative_DenyUnrestricted_AssertBoth_DemandBoth ()
194                 {
195                         AssertBoth (Both);
196                         Assert.IsTrue (result);
197                 }
198
199
200                 [SecurityPermission (SecurityAction.Assert, UnmanagedCode = true)]
201                 private void Assert_UnmanagedCode_RemotingConfiguration (SecurityPermissionFlag flag)
202                 {
203                         AssertRemotingConfiguration (flag);
204                 }
205
206                 [SecurityPermission (SecurityAction.Assert, RemotingConfiguration = true)]
207                 private void Assert_RemotingConfiguration_UnmanagedCode (SecurityPermissionFlag flag)
208                 {
209                         AssertUnmanagedCode (flag);
210                 }
211
212                 [Test]
213                 [SecurityPermission (SecurityAction.Deny, Unrestricted = true)]
214                 [ExpectedException (typeof (SecurityException))]
215                 public void Declarative_DenyUnrestricted_AssertUnmanagedRemoting_DemandBoth ()
216                 {
217                         // no single stack frame can assert the whole SecurityPermission
218                         // which is different from a PermissionSet
219                         Assert_UnmanagedCode_RemotingConfiguration (Both);
220                 }
221
222                 [Test]
223                 [SecurityPermission (SecurityAction.Deny, Unrestricted = true)]
224                 public void Declarative_DenyUnrestricted_AssertUnmanagedRemoting_DemandUnmanaged ()
225                 {
226                         Assert_UnmanagedCode_RemotingConfiguration (SecurityPermissionFlag.UnmanagedCode);
227                         Assert.IsTrue (result);
228                 }
229
230                 [Test]
231                 [SecurityPermission (SecurityAction.Deny, Unrestricted = true)]
232                 public void Declarative_DenyUnrestricted_AssertUnmanagedRemoting_DemandRemoting ()
233                 {
234                         Assert_UnmanagedCode_RemotingConfiguration (SecurityPermissionFlag.RemotingConfiguration);
235                         Assert.IsTrue (result);
236                 }
237
238                 [Test]
239                 [SecurityPermission (SecurityAction.Deny, Unrestricted = true)]
240                 [ExpectedException (typeof (SecurityException))]
241                 public void Declarative_DenyUnrestricted_AssertRemotingUnmanaged_DemandBoth ()
242                 {
243                         // no single stack frame can assert the whole SecurityPermission
244                         // which is different from a PermissionSet
245                         Assert_RemotingConfiguration_UnmanagedCode (Both);
246                 }
247
248                 [Test]
249                 [SecurityPermission (SecurityAction.Deny, Unrestricted = true)]
250                 public void Declarative_DenyUnrestricted_AssertRemotingUnmanaged_DemandUnmanaged ()
251                 {
252                         Assert_RemotingConfiguration_UnmanagedCode (SecurityPermissionFlag.UnmanagedCode);
253                         Assert.IsTrue (result);
254                 }
255
256                 [Test]
257                 [SecurityPermission (SecurityAction.Deny, Unrestricted = true)]
258                 public void Declarative_DenyUnrestricted_AssertRemotingUnmanaged_DemandRemoting ()
259                 {
260                         Assert_RemotingConfiguration_UnmanagedCode (SecurityPermissionFlag.RemotingConfiguration);
261                         Assert.IsTrue (result);
262                 }
263
264
265                 [SecurityPermission (SecurityAction.Demand, UnmanagedCode = true)]
266                 [ReflectionPermission (SecurityAction.Demand, ReflectionEmit = true)]
267                 private void Demand_Reflection_Unmanaged ()
268                 {
269                         result = true;
270                 }
271
272                 [SecurityPermission (SecurityAction.Assert, UnmanagedCode = true)]
273                 private void Assert_Unmanaged (bool call)
274                 {
275                         if (call)
276                                 Demand_Reflection_Unmanaged ();
277                         else
278                                 Assert_Reflection (true);
279                 }
280
281                 [ReflectionPermission (SecurityAction.Assert, ReflectionEmit = true)]
282                 private void Assert_Reflection (bool call)
283                 {
284                         if (call)
285                                 Demand_Reflection_Unmanaged ();
286                         else
287                                 Assert_Unmanaged (true);
288                 }
289
290                 [Test]
291                 [SecurityPermission (SecurityAction.Deny, Unrestricted = true)]
292                 [ReflectionPermission (SecurityAction.Deny, Unrestricted = true)]
293                 public void Declarative_DenyUnrestricted_AssertReflectionUnmanaged_DemandUnmanagedReflection ()
294                 {
295                         Assert_Reflection (false);
296                         Assert.IsTrue (result);
297                 }
298
299                 [Test]
300                 [SecurityPermission (SecurityAction.Deny, Unrestricted = true)]
301                 [ReflectionPermission (SecurityAction.Deny, Unrestricted = true)]
302                 [ExpectedException (typeof (SecurityException))]
303                 public void Declarative_DenyUnrestricted_AssertReflection_DemandUnmanagedReflection ()
304                 {
305                         Assert_Reflection (true);
306                 }
307
308                 [Test]
309                 [SecurityPermission (SecurityAction.Deny, Unrestricted = true)]
310                 [ReflectionPermission (SecurityAction.Deny, Unrestricted = true)]
311                 public void Declarative_DenyUnrestricted_AssertUnmanagedReflection_DemandUnmanagedReflection ()
312                 {
313                         Assert_Unmanaged (false);
314                         Assert.IsTrue (result);
315                 }
316
317                 [Test]
318                 [SecurityPermission (SecurityAction.Deny, Unrestricted = true)]
319                 [ReflectionPermission (SecurityAction.Deny, Unrestricted = true)]
320                 [ExpectedException (typeof (SecurityException))]
321                 public void Declarative_DenyUnrestricted_AssertUnmanaged_DemandUnmanagedReflection ()
322                 {
323                         Assert_Unmanaged (true);
324                 }
325
326                 [Test]
327                 [PermissionSet (SecurityAction.PermitOnly, Unrestricted = true)]
328                 public void Declarative_PermitOnly_Unrestricted ()
329                 {
330                         // permitonly unrestricted is (technically) a no-op
331                         DemandBoth ();
332                         Assert.IsTrue (result);
333                 }
334         }
335 }