Merge pull request #1513 from akoeplinger/remove-net20-ifdefs
[mono.git] / mcs / class / corlib / Test / System.Diagnostics / StackFrameCas.cs
1 //
2 // StackFrameCas.cs - CAS unit tests for System.Diagnostics.StackFrame
3 //
4 // Author:
5 //      Sebastien Pouliot  <sebastien@ximian.com>
6 //
7 // Copyright (C) 2005 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 using NUnit.Framework;
30
31 using System;
32 using System.Diagnostics;
33 using System.Security;
34 using System.Security.Permissions;
35 using System.Threading;
36
37 namespace MonoCasTests.System.Diagnostics {
38
39         [TestFixture]
40         [Category ("CAS")]
41         public class StackFrameCas {
42
43                 [SetUp]
44                 public void SetUp ()
45                 {
46                         if (!SecurityManager.SecurityEnabled)
47                                 Assert.Ignore ("SecurityManager.SecurityEnabled is OFF");
48                 }
49
50                 // avoid replication of tests on all constructors (this is no 
51                 // problem because the stack is already set correctly). The 
52                 // goal is to call every property and methods to see if they
53                 // have any* security requirements (*except for LinkDemand and
54                 // InheritanceDemand).
55                 private void Check (StackFrame sf, bool checkFile)
56                 {
57                         int cn = sf.GetFileColumnNumber ();
58                         int ln = sf.GetFileLineNumber ();
59                         int il = sf.GetILOffset ();
60                         int no = sf.GetNativeOffset ();
61
62                         Assert.IsNotNull (sf.GetMethod (), "GetMethod");
63
64                         if (checkFile) {
65                                 string fn = sf.GetFileName ();
66                         }
67                 }
68
69                 [Test]
70                 [PermissionSet (SecurityAction.Deny, Unrestricted = true)]
71                 public void StackFrame_DefaultConstructor ()
72                 {
73                         StackFrame sf = new StackFrame ();
74                         Check (sf, true);
75                 }
76                 
77 #if !RUN_ONDOTNET || NET_4_0 // Disabled because .net 2 fails to load dll with "Failure decoding embedded permission set object" due to "/" path
78                 [Test]
79                 [FileIOPermission (SecurityAction.Deny, PathDiscovery = "/")]
80                 [ExpectedException (typeof (SecurityException))]
81                 public void StackFrame_TrueConstructor_Fail ()
82                 {
83                         StackFrame sf = null;
84                         try {
85                                 // ask for file informations
86                                 sf = new StackFrame (true);
87                                 Check (sf, false);
88                         }
89                         catch {
90                                 Assert.Fail ("Didn't ask for file information");
91                         }
92                         // now look at the file informations...
93                         // note: only fails under 2.0
94                         Check (sf, true);
95                 }
96
97                 [Test]
98                 [FileIOPermission (SecurityAction.PermitOnly, PathDiscovery = "/")]
99                 public void StackFrame_TrueConstructor_Pass ()
100                 {
101                         // ask file info
102                         StackFrame sf = new StackFrame (true);
103                         Check (sf, true);
104                 }
105                 
106                 [Test]
107                 [FileIOPermission (SecurityAction.Deny, PathDiscovery = "/")]
108                 [ExpectedException (typeof (SecurityException))]
109                 public void StackFrame_IntTrueConstructor_Fail ()
110                 {
111                         StackFrame sf = null;
112                         try {
113                                 // ask for file informations
114                                 sf = new StackFrame (0, true);
115                                 Check (sf, false);
116                         }
117                         catch {
118                                 Assert.Fail ("Didn't ask for file information");
119                         }
120                         // now look at the file informations...
121                         // note: only fails under 2.0
122                         Check (sf, true);
123                 }
124
125                 [Test]
126                 [FileIOPermission (SecurityAction.PermitOnly, PathDiscovery = "/")]
127                 public void StackFrame_IntTrueConstructor_Pass ()
128                 {
129                         // ask file info
130                         StackFrame sf = new StackFrame (0, true);
131                         Check (sf, true);
132                 }
133                 
134                 [Test]
135                 [FileIOPermission (SecurityAction.Deny, PathDiscovery = "/")]
136                 [ExpectedException (typeof (SecurityException))]
137                 public void StackFrame_StringIntConstructor_Fail ()
138                 {
139                         StackFrame sf = null;
140                         try {
141                                 // ask for file informations
142                                 sf = new StackFrame ("mono.cs", 1);
143                                 Check (sf, false);
144                         }
145                         catch {
146                                 Assert.Fail ("Didn't ask for file information");
147                         }
148                         // now look at the file informations...
149                         // note: only fails under 2.0
150                         Check (sf, true);
151                 }
152
153                 [Test]
154                 [FileIOPermission (SecurityAction.PermitOnly, PathDiscovery = "/")]
155                 public void StackFrame_StringIntConstructor_Pass ()
156                 {
157                         // supply file info
158                         StackFrame sf = new StackFrame ("mono.cs", 1);
159                         Check (sf, true);
160                 }
161
162                 [Test]
163                 [FileIOPermission (SecurityAction.Deny, PathDiscovery = "/")]
164                 [ExpectedException (typeof (SecurityException))]
165                 public void StackFrame_StringIntIntConstructor_Fail ()
166                 {
167                         StackFrame sf = null;
168                         try {
169                                 // supply file info
170                                 sf = new StackFrame ("mono.cs", 1, 1);
171                                 Check (sf, false);
172                         }
173                         catch {
174                                 Assert.Fail ("Didn't ask for file information");
175                         }
176                         // now look at the file informations...
177                         // note: only fails under 2.0
178                         Check (sf, true);
179                 }
180
181                 [Test]
182                 [FileIOPermission (SecurityAction.PermitOnly, PathDiscovery = "/")]
183                 public void StackFrame_StringIntIntConstructor_Pass ()
184                 {
185                         // supply file info
186                         StackFrame sf = new StackFrame ("mono.cs", 1, 1);
187                         Check (sf, true);
188                 }               
189 #endif          
190
191                 [Test]
192                 [PermissionSet (SecurityAction.Deny, Unrestricted = true)]
193                 public void StackFrame_FalseConstructor ()
194                 {
195                         // DO NOT ask for file informations
196                         StackFrame sf = new StackFrame (false);
197                         Check (sf, true);
198                 }
199
200                 [Test]
201                 [PermissionSet (SecurityAction.Deny, Unrestricted = true)]
202                 public void StackFrame_IntConstructor ()
203                 {
204                         StackFrame sf = new StackFrame (1);
205                         Check (sf, true);
206                 }
207
208                 [Test]
209                 [PermissionSet (SecurityAction.Deny, Unrestricted = true)]
210                 public void StackFrame_IntFalseConstructor ()
211                 {
212                         // DO NOT ask for file informations
213                         StackFrame sf = new StackFrame (1, false);
214                         Check (sf, true);
215                 }
216         }
217 }