Normalize line endings.
[mono.git] / mcs / class / corlib / Test / System / ExceptionCas.cs
1 //
2 // ExceptionCas.cs - CAS unit tests for System.Exception
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.Security;
33 using System.Security.Permissions;
34
35 namespace MonoCasTests.System {
36
37         [TestFixture]
38         [Category ("CAS")]
39         public class ExceptionCas {
40
41                 [SetUp]
42                 public void SetUp ()
43                 {
44                         if (!SecurityManager.SecurityEnabled)
45                                 Assert.Ignore ("SecurityManager.SecurityEnabled is OFF");
46                 }
47
48                 // Partial Trust Tests - i.e. call "normal" unit with reduced privileges
49
50                 [Test]
51                 [PermissionSet (SecurityAction.Deny, Unrestricted = true)]
52                 public void PartialTrust_DenyUnrestricted_Success ()
53                 {
54                         MonoTests.System.ExceptionTest et = new MonoTests.System.ExceptionTest ();
55                         // call most (all but arguments checking) unit tests from ExceptionTest
56                         et.TestThrowOnBlockBoundaries ();
57                         et.Source_InnerException ();
58                 }
59
60                 // CAS tests
61
62                 [Test]
63                 public void NoRestriction ()
64                 {
65                         Exception e = new Exception ("message", new Exception ("inner message"));
66
67                         Assert.AreEqual ("message", e.Message, "Message");
68                         Assert.IsNotNull (e.InnerException, "InnerException");
69                         Assert.IsNotNull (e.ToString (), "ToString");
70 #if NET_2_0
71                         Assert.IsNotNull (e.Data, "Data");
72 #endif
73                         Assert.IsNull (e.HelpLink, "HelpLink");
74                         Assert.IsNull (e.Source, "Source");
75                         Assert.IsNull (e.StackTrace, "StackTrace");
76                         Assert.IsNull (e.TargetSite, "TargetSite");
77                 }
78
79                 [Test]
80                 public void Throw_NoRestriction ()
81                 {
82                         try {
83                                 throw new Exception ("message", new Exception ("inner message"));
84                         }
85                         catch (Exception e) {
86                                 Assert.AreEqual ("message", e.Message, "Message");
87                                 Assert.IsNotNull (e.InnerException, "InnerException");
88                                 Assert.IsNotNull (e.ToString (), "ToString");
89 #if NET_2_0
90                                 Assert.IsNotNull (e.Data, "Data");
91 #endif
92                                 Assert.IsNull (e.HelpLink, "HelpLink");
93                                 Assert.IsNotNull (e.Source, "Source");
94                                 Assert.IsNotNull (e.StackTrace, "StackTrace");
95                                 Assert.IsNotNull (e.TargetSite, "TargetSite");
96                         }
97                 }
98
99                 [Test]
100                 [PermissionSet (SecurityAction.Deny, Unrestricted = true)]
101                 public void FullRestriction ()
102                 {
103                         Exception e = new Exception ("message", new Exception ("inner message"));
104
105                         Assert.AreEqual ("message", e.Message, "Message");
106                         Assert.IsNotNull (e.InnerException, "InnerException");
107                         Assert.IsNotNull (e.ToString (), "ToString");
108                         Assert.IsNull (e.HelpLink, "HelpLink");
109                         Assert.IsNull (e.Source, "Source");
110                         Assert.IsNull (e.StackTrace, "StackTrace");
111 #if NET_2_0
112                         Assert.IsNotNull (e.Data, "Data");
113                         // throws under 1.x
114                         Assert.IsNull (e.TargetSite, "TargetSite");
115 #endif
116                 }
117
118 #if !NET_2_0
119                 // note: TypeInformation is obsolete in 2.0
120                 [Test]
121                 [ReflectionPermission (SecurityAction.Deny, TypeInformation = true)]
122                 [ExpectedException (typeof (SecurityException))]
123                 public void ReflectionTypeInformationRestriction_TargetSite ()
124                 {
125                         Exception e = new Exception ("message", new Exception ("inner message"));
126                         Assert.IsNull (e.TargetSite, "TargetSite");
127                 }
128 #endif
129
130                 [Test]
131                 [PermissionSet (SecurityAction.Deny, Unrestricted = true)]
132                 public void Throw_FullRestriction_Pass ()
133                 {
134                         try {
135                                 throw new Exception ("message", new Exception ("inner message"));
136                         }
137                         catch (Exception e) {
138                                 Assert.AreEqual ("message", e.Message, "Message");
139                                 Assert.IsNotNull (e.InnerException, "InnerException");
140                                 Assert.IsNull (e.HelpLink, "HelpLink");
141                                 Assert.IsNotNull (e.Source, "Source");
142 #if NET_2_0
143                                 Assert.IsNotNull (e.Data, "Data");
144                                 // throws under 1.x
145                                 Assert.IsNotNull (e.TargetSite, "TargetSite");
146 #endif
147                         }
148                 }
149
150                 [Test]
151 #if WINDOWS
152                 [FileIOPermission (SecurityAction.Deny, PathDiscovery = "C:\\")]
153 #else
154                 [FileIOPermission (SecurityAction.Deny, PathDiscovery = "/")]
155 #endif
156 #if NET_2_0
157                 [ExpectedException (typeof (Exception))]
158 #endif
159                 public void Throw_FullRestriction_Fail_StackTrace ()
160                 {
161                         try {
162                                 throw new Exception ("message");
163                         }
164                         catch (Exception e) {
165                                 // throws only under 2.x
166                                 string s = e.StackTrace;
167                         }
168                 }
169
170                 [Test]
171 #if WINDOWS
172                 [FileIOPermission (SecurityAction.Deny, PathDiscovery = "C:\\")]
173 #else
174                 [FileIOPermission (SecurityAction.Deny, PathDiscovery = "/")]
175 #endif
176 #if NET_2_0
177                 [ExpectedException (typeof (Exception))]
178 #endif
179                 public void Throw_FullRestriction_Fail_ToString ()
180                 {
181                         try {
182                                 throw new Exception ("message");
183                         }
184                         catch (Exception e) {
185                                 // throws only under 2.x
186                                 string s = e.ToString ();
187                         }
188                 }
189         }
190 }