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]
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)]
PartialTrust_DenyUnrestricted_Success()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]
NoRestriction()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 			Assert.IsNotNull (e.Data, "Data");
71 			Assert.IsNull (e.HelpLink, "HelpLink");
72 			Assert.IsNull (e.Source, "Source");
73 			Assert.IsNull (e.StackTrace, "StackTrace");
74 			Assert.IsNull (e.TargetSite, "TargetSite");
75 		}
76 
77 		[Test]
Throw_NoRestriction()78 		public void Throw_NoRestriction ()
79 		{
80 			try {
81 				throw new Exception ("message", new Exception ("inner message"));
82 			}
83 			catch (Exception e) {
84 				Assert.AreEqual ("message", e.Message, "Message");
85 				Assert.IsNotNull (e.InnerException, "InnerException");
86 				Assert.IsNotNull (e.ToString (), "ToString");
87 				Assert.IsNotNull (e.Data, "Data");
88 				Assert.IsNull (e.HelpLink, "HelpLink");
89 				Assert.IsNotNull (e.Source, "Source");
90 				Assert.IsNotNull (e.StackTrace, "StackTrace");
91 				Assert.IsNotNull (e.TargetSite, "TargetSite");
92 			}
93 		}
94 
95 		[Test]
96 		[PermissionSet (SecurityAction.Deny, Unrestricted = true)]
FullRestriction()97 		public void FullRestriction ()
98 		{
99 			Exception e = new Exception ("message", new Exception ("inner message"));
100 
101 			Assert.AreEqual ("message", e.Message, "Message");
102 			Assert.IsNotNull (e.InnerException, "InnerException");
103 			Assert.IsNotNull (e.ToString (), "ToString");
104 			Assert.IsNull (e.HelpLink, "HelpLink");
105 			Assert.IsNull (e.Source, "Source");
106 			Assert.IsNull (e.StackTrace, "StackTrace");
107 			Assert.IsNotNull (e.Data, "Data");
108 			// throws under 1.x
109 			Assert.IsNull (e.TargetSite, "TargetSite");
110 		}
111 
112 		[Test]
113 		[PermissionSet (SecurityAction.Deny, Unrestricted = true)]
Throw_FullRestriction_Pass()114 		public void Throw_FullRestriction_Pass ()
115 		{
116 			try {
117 				throw new Exception ("message", new Exception ("inner message"));
118 			}
119 			catch (Exception e) {
120 				Assert.AreEqual ("message", e.Message, "Message");
121 				Assert.IsNotNull (e.InnerException, "InnerException");
122 				Assert.IsNull (e.HelpLink, "HelpLink");
123 				Assert.IsNotNull (e.Source, "Source");
124 				Assert.IsNotNull (e.Data, "Data");
125 				// throws under 1.x
126 				Assert.IsNotNull (e.TargetSite, "TargetSite");
127 			}
128 		}
129 
130 
131 		[Test]
132 		[FileIOPermission (SecurityAction.Deny, PathDiscovery = "/")]
133 		[ExpectedException (typeof (Exception))]
Throw_FullRestriction_Fail_StackTrace()134 		public void Throw_FullRestriction_Fail_StackTrace ()
135 		{
136 			try {
137 				throw new Exception ("message");
138 			}
139 			catch (Exception e) {
140 				// throws only under 2.x
141 				string s = e.StackTrace;
142 			}
143 		}
144 
145 		[Test]
146 		[FileIOPermission (SecurityAction.Deny, PathDiscovery = "/")]
147 		[ExpectedException (typeof (Exception))]
Throw_FullRestriction_Fail_ToString()148 		public void Throw_FullRestriction_Fail_ToString ()
149 		{
150 			try {
151 				throw new Exception ("message");
152 			}
153 			catch (Exception e) {
154 				// throws only under 2.x
155 				string s = e.ToString ();
156 			}
157 		}
158 
159 	}
160 }
161