2 // ExceptionCas.cs - CAS unit tests for System.Exception
5 // Sebastien Pouliot <sebastien@ximian.com>
7 // Copyright (C) 2005 Novell, Inc (http://www.novell.com)
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:
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
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.
29 using NUnit.Framework;
32 using System.Security;
33 using System.Security.Permissions;
35 namespace MonoCasTests.System {
39 public class ExceptionCas {
44 if (!SecurityManager.SecurityEnabled)
45 Assert.Ignore ("SecurityManager.SecurityEnabled is OFF");
48 // Partial Trust Tests - i.e. call "normal" unit with reduced privileges
51 [PermissionSet (SecurityAction.Deny, Unrestricted = true)]
52 public void PartialTrust_DenyUnrestricted_Success ()
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 ();
63 public void NoRestriction ()
65 Exception e = new Exception ("message", new Exception ("inner message"));
67 Assert.AreEqual ("message", e.Message, "Message");
68 Assert.IsNotNull (e.InnerException, "InnerException");
69 Assert.IsNotNull (e.ToString (), "ToString");
71 Assert.IsNotNull (e.Data, "Data");
73 Assert.IsNull (e.HelpLink, "HelpLink");
74 Assert.IsNull (e.Source, "Source");
75 Assert.IsNull (e.StackTrace, "StackTrace");
76 Assert.IsNull (e.TargetSite, "TargetSite");
80 public void Throw_NoRestriction ()
83 throw new Exception ("message", new Exception ("inner message"));
86 Assert.AreEqual ("message", e.Message, "Message");
87 Assert.IsNotNull (e.InnerException, "InnerException");
88 Assert.IsNotNull (e.ToString (), "ToString");
90 Assert.IsNotNull (e.Data, "Data");
92 Assert.IsNull (e.HelpLink, "HelpLink");
93 Assert.IsNotNull (e.Source, "Source");
94 Assert.IsNotNull (e.StackTrace, "StackTrace");
95 Assert.IsNotNull (e.TargetSite, "TargetSite");
100 [PermissionSet (SecurityAction.Deny, Unrestricted = true)]
101 public void FullRestriction ()
103 Exception e = new Exception ("message", new Exception ("inner message"));
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");
112 Assert.IsNotNull (e.Data, "Data");
114 Assert.IsNull (e.TargetSite, "TargetSite");
119 // note: TypeInformation is obsolete in 2.0
121 [ReflectionPermission (SecurityAction.Deny, TypeInformation = true)]
122 [ExpectedException (typeof (SecurityException))]
123 public void ReflectionTypeInformationRestriction_TargetSite ()
125 Exception e = new Exception ("message", new Exception ("inner message"));
126 Assert.IsNull (e.TargetSite, "TargetSite");
131 [PermissionSet (SecurityAction.Deny, Unrestricted = true)]
132 public void Throw_FullRestriction_Pass ()
135 throw new Exception ("message", new Exception ("inner message"));
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");
143 Assert.IsNotNull (e.Data, "Data");
145 Assert.IsNotNull (e.TargetSite, "TargetSite");
152 [FileIOPermission (SecurityAction.Deny, PathDiscovery = "C:\\")]
154 [FileIOPermission (SecurityAction.Deny, PathDiscovery = "/")]
157 [ExpectedException (typeof (Exception))]
159 public void Throw_FullRestriction_Fail_StackTrace ()
162 throw new Exception ("message");
164 catch (Exception e) {
165 // throws only under 2.x
166 string s = e.StackTrace;
172 [FileIOPermission (SecurityAction.Deny, PathDiscovery = "C:\\")]
174 [FileIOPermission (SecurityAction.Deny, PathDiscovery = "/")]
177 [ExpectedException (typeof (Exception))]
179 public void Throw_FullRestriction_Fail_ToString ()
182 throw new Exception ("message");
184 catch (Exception e) {
185 // throws only under 2.x
186 string s = e.ToString ();