2005-03-14 Sebastien Pouliot <sebastien@ximian.com>
[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
49                 [Test]
50                 public void NoRestriction ()
51                 {
52                         Exception e = new Exception ("message", new Exception ("inner message"));
53
54                         Assert.AreEqual ("message", e.Message, "Message");
55                         Assert.IsNotNull (e.InnerException, "InnerException");
56                         Assert.IsNotNull (e.ToString (), "ToString");
57 #if NET_2_0
58                         Assert.IsNotNull (e.Data, "Data");
59 #endif
60                         Assert.IsNull (e.HelpLink, "HelpLink");
61                         Assert.IsNull (e.Source, "Source");
62                         Assert.IsNull (e.StackTrace, "StackTrace");
63                         Assert.IsNull (e.TargetSite, "TargetSite");
64                 }
65
66                 [Test]
67                 public void Throw_NoRestriction ()
68                 {
69                         try {
70                                 throw new Exception ("message", new Exception ("inner message"));
71                         }
72                         catch (Exception e) {
73                                 Assert.AreEqual ("message", e.Message, "Message");
74                                 Assert.IsNotNull (e.InnerException, "InnerException");
75                                 Assert.IsNotNull (e.ToString (), "ToString");
76 #if NET_2_0
77                                 Assert.IsNotNull (e.Data, "Data");
78 #endif
79                                 Assert.IsNull (e.HelpLink, "HelpLink");
80                                 Assert.IsNotNull (e.Source, "Source");
81                                 Assert.IsNotNull (e.StackTrace, "StackTrace");
82                                 Assert.IsNotNull (e.TargetSite, "TargetSite");
83                         }
84                 }
85
86                 [Test]
87                 [PermissionSet (SecurityAction.Deny, Unrestricted = true)]
88                 public void FullRestriction ()
89                 {
90                         Exception e = new Exception ("message", new Exception ("inner message"));
91
92                         Assert.AreEqual ("message", e.Message, "Message");
93                         Assert.IsNotNull (e.InnerException, "InnerException");
94                         Assert.IsNotNull (e.ToString (), "ToString");
95                         Assert.IsNull (e.HelpLink, "HelpLink");
96                         Assert.IsNull (e.Source, "Source");
97                         Assert.IsNull (e.StackTrace, "StackTrace");
98 #if NET_2_0
99                         Assert.IsNotNull (e.Data, "Data");
100                         // throws under 1.x
101                         Assert.IsNull (e.TargetSite, "TargetSite");
102 #endif
103                 }
104
105 #if !NET_2_0
106                 [Test]
107                 [PermissionSet (SecurityAction.Deny, Unrestricted = true)]
108                 [ExpectedException (typeof (SecurityException))]
109                 public void FullRestriction_TargetSite ()
110                 {
111                         Exception e = new Exception ("message", new Exception ("inner message"));
112                         Assert.IsNull (e.TargetSite, "TargetSite");
113                 }
114 #endif
115
116                 [Test]
117                 [PermissionSet (SecurityAction.Deny, Unrestricted = true)]
118                 public void Throw_FullRestriction_Pass ()
119                 {
120                         try {
121                                 throw new Exception ("message", new Exception ("inner message"));
122                         }
123                         catch (Exception e) {
124                                 Assert.AreEqual ("message", e.Message, "Message");
125                                 Assert.IsNotNull (e.InnerException, "InnerException");
126                                 Assert.IsNull (e.HelpLink, "HelpLink");
127                                 Assert.IsNotNull (e.Source, "Source");
128 #if NET_2_0
129                                 Assert.IsNotNull (e.Data, "Data");
130                                 // throws under 1.x
131                                 Assert.IsNotNull (e.TargetSite, "TargetSite");
132 #endif
133                         }
134                 }
135
136                 [Test]
137 #if WINDOWS
138                 [FileIOPermission (SecurityAction.Deny, PathDiscovery = "C:\\")]
139 #else
140                 [FileIOPermission (SecurityAction.Deny, PathDiscovery = "/")]
141 #endif
142 #if NET_2_0
143                 [ExpectedException (typeof (Exception))]
144 #endif
145                 public void Throw_FullRestriction_Fail_StackTrace ()
146                 {
147                         try {
148                                 throw new Exception ("message");
149                         }
150                         catch (Exception e) {
151                                 // throws only under 2.x
152                                 string s = e.StackTrace;
153                         }
154                 }
155
156                 [Test]
157 #if WINDOWS
158                 [FileIOPermission (SecurityAction.Deny, PathDiscovery = "C:\\")]
159 #else
160                 [FileIOPermission (SecurityAction.Deny, PathDiscovery = "/")]
161 #endif
162 #if NET_2_0
163                 [ExpectedException (typeof (Exception))]
164 #endif
165                 public void Throw_FullRestriction_Fail_ToString ()
166                 {
167                         try {
168                                 throw new Exception ("message");
169                         }
170                         catch (Exception e) {
171                                 // throws only under 2.x
172                                 string s = e.ToString ();
173                         }
174                 }
175         }
176 }