Merge pull request #1057 from lextm/master
[mono.git] / mcs / class / corlib / Test / System.IO / FileNotFoundExceptionCas.cs
1 //
2 // FileNotFoundExceptionCas.cs - CAS unit tests for 
3 //      System.IO.FileNotFoundException
4 //
5 // Author:
6 //      Sebastien Pouliot  <sebastien@ximian.com>
7 //
8 // Copyright (C) 2005 Novell, Inc (http://www.novell.com)
9 //
10 // Permission is hereby granted, free of charge, to any person obtaining
11 // a copy of this software and associated documentation files (the
12 // "Software"), to deal in the Software without restriction, including
13 // without limitation the rights to use, copy, modify, merge, publish,
14 // distribute, sublicense, and/or sell copies of the Software, and to
15 // permit persons to whom the Software is furnished to do so, subject to
16 // the following conditions:
17 // 
18 // The above copyright notice and this permission notice shall be
19 // included in all copies or substantial portions of the Software.
20 // 
21 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
22 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
23 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
24 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
25 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
26 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
27 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28 //
29
30 using NUnit.Framework;
31
32 using System;
33 using System.IO;
34 using System.Security;
35 using System.Security.Permissions;
36
37 namespace MonoCasTests.System.IO {
38
39         [TestFixture]
40         [Category ("CAS")]
41         public class FileNotFoundExceptionCas {
42
43                 [SetUp]
44                 public void SetUp ()
45                 {
46                         if (!SecurityManager.SecurityEnabled)
47                                 Assert.Ignore ("SecurityManager.SecurityEnabled is OFF");
48                 }
49
50
51                 [Test]
52                 public void NoRestriction ()
53                 {
54                         FileNotFoundException fle = new FileNotFoundException ("message", "filename",
55                                 new FileNotFoundException ("inner message", "inner filename"));
56
57                         Assert.AreEqual ("message", fle.Message, "Message");
58                         Assert.AreEqual ("filename", fle.FileName, "FileName");
59                         Assert.IsNull (fle.FusionLog, "FusionLog");
60                         Assert.IsNotNull (fle.ToString (), "ToString");
61                 }
62
63                 [Test]
64                 [PermissionSet (SecurityAction.Deny, Unrestricted = true)]
65                 [ExpectedException (typeof (SecurityException))]
66                 public void FullRestriction ()
67                 {
68                         FileNotFoundException fle = new FileNotFoundException ("message", "filename",
69                                 new FileNotFoundException ("inner message", "inner filename"));
70
71                         Assert.AreEqual ("message", fle.Message, "Message");
72                         Assert.AreEqual ("filename", fle.FileName, "FileName");
73                         Assert.IsNull (fle.FusionLog, "FusionLog");
74                         // ToString doesn't work in this case and strangely throws a FileNotFoundException
75                         Assert.IsNotNull (fle.ToString (), "ToString");
76                 }
77
78                 [Test]
79                 [SecurityPermission (SecurityAction.PermitOnly, ControlEvidence = true, ControlPolicy = true)]
80                 public void GetFusionLog_Pass ()
81                 {
82                         FileNotFoundException fle = new FileNotFoundException ("message", "filename");
83                         Assert.AreEqual ("message", fle.Message, "Message");
84                         Assert.AreEqual ("filename", fle.FileName, "FileName");
85                         Assert.IsNull (fle.FusionLog, "FusionLog");
86                         // note: ToString doesn't work in this case
87                 }
88
89                 [Test]
90                 [SecurityPermission (SecurityAction.Deny, ControlEvidence = true)]
91                 [ExpectedException (typeof (SecurityException))]
92                 public void GetFusionLog_Fail_ControlEvidence ()
93                 {
94                         FileNotFoundException fle = new FileNotFoundException ();
95                         Assert.IsNull (fle.FusionLog, "FusionLog");
96                 }
97
98                 [Test]
99                 [SecurityPermission (SecurityAction.Deny, ControlPolicy = true)]
100                 [ExpectedException (typeof (SecurityException))]
101                 public void GetFusionLog_Fail_ControlPolicy ()
102                 {
103                         FileNotFoundException fle = new FileNotFoundException ();
104                         Assert.IsNull (fle.FusionLog, "FusionLog");
105                         // we don't have to throw the exception to have FusionLog
106                         // informations restricted (even if there could be no
107                         // data in this state).
108                 }
109
110
111                 [Test]
112                 public void Throw_NoRestriction ()
113                 {
114                         try {
115                                 throw new FileNotFoundException ("message", "filename",
116                                         new FileNotFoundException ("inner message", "inner filename"));
117                         }
118                         catch (FileNotFoundException fle) {
119                                 Assert.AreEqual ("message", fle.Message, "Message");
120                                 Assert.AreEqual ("filename", fle.FileName, "FileName");
121                                 Assert.IsNull (fle.FusionLog, "FusionLog");
122                                 Assert.IsNotNull (fle.ToString (), "ToString");
123                         }
124                 }
125
126                 [Test]
127                 [PermissionSet (SecurityAction.Deny, Unrestricted = true)]
128                 [ExpectedException (typeof (SecurityException))]
129                 public void Throw_FullRestriction ()
130                 {
131                         try {
132                                 throw new FileNotFoundException ("message", "filename",
133                                         new FileNotFoundException ("inner message", "inner filename"));
134                         }
135                         catch (FileNotFoundException fle) {
136                                 Assert.AreEqual ("message", fle.Message, "Message");
137                                 Assert.AreEqual ("filename", fle.FileName, "FileName");
138                                 // both FusionLog and ToString doesn't work in this case
139                                 // but strangely we get a FileNotFoundException
140                                 Assert.IsNull (fle.FusionLog, "FusionLog");
141                                 Assert.IsNotNull (fle.ToString (), "ToString");
142                         }
143                 }
144
145                 [Test]
146                 [SecurityPermission (SecurityAction.Deny, ControlEvidence = true)]
147                 [ExpectedException (typeof (SecurityException))]
148                 public void Throw_GetFusionLog_Fail_ControlEvidence ()
149                 {
150                         try {
151                                 throw new FileNotFoundException ("message", "filename",
152                                         new FileNotFoundException ("inner message", "inner filename"));
153                         }
154                         catch (FileNotFoundException fle) {
155                                 Assert.IsNull (fle.FusionLog, "FusionLog");
156                         }
157                 }
158
159                 [Test]
160                 [SecurityPermission (SecurityAction.Deny, ControlPolicy = true)]
161                 [ExpectedException (typeof (SecurityException))]
162                 public void Throw_GetFusionLog_Fail_ControlPolicy ()
163                 {
164                         try {
165                                 throw new FileNotFoundException ("message", "filename",
166                                         new FileNotFoundException ("inner message", "inner filename"));
167                         }
168                         catch (FileNotFoundException fle) {
169                                 Assert.IsNull (fle.FusionLog, "FusionLog");
170                         }
171                 }
172         }
173 }