Merge pull request #943 from ermshiperete/bug-novell-325669
[mono.git] / mcs / class / corlib / Test / System.IO / PathCas.cs
1 //
2 // PathCas.cs -CAS unit tests for System.IO.Path
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.IO;
33 using System.Reflection;
34 using System.Security;
35 using System.Security.Permissions;
36
37 namespace MonoCasTests.System.IO {
38
39         [TestFixture]
40         [Category ("CAS")]
41         public class PathCas {
42
43                 private MonoTests.System.IO.PathTest pt;
44                 private string cd;
45
46                 [TestFixtureSetUp]
47                 public void FixtureSetUp ()
48                 {
49                         // this occurs with a "clean" stack (full trust)
50                         pt  = new MonoTests.System.IO.PathTest ();
51                         cd = Environment.CurrentDirectory;
52                 }
53
54                 [SetUp]
55                 public void SetUp ()
56                 {
57                         if (!SecurityManager.SecurityEnabled)
58                                 Assert.Ignore ("SecurityManager.SecurityEnabled is OFF");
59                         pt.SetUp ();
60                 }
61
62                 // Partial Trust Tests - i.e. call "normal" unit with reduced privileges
63
64                 [Test]
65                 [PermissionSet (SecurityAction.Deny, Unrestricted = true)]
66                 public void PartialTrust_DenyUnrestricted_Success ()
67                 {
68                         // some calls do not require any permissions...
69                         pt.ChangeExtension ();
70                         pt.ChangeExtension_Extension_InvalidPathChars ();
71                         pt.GetDirectoryName ();
72                         pt.GetExtension ();
73                         pt.GetFileName ();
74                         pt.GetFileNameWithoutExtension ();
75                         pt.GetPathRoot2 ();
76                         pt.HasExtension ();
77                         pt.IsPathRooted ();
78                 }
79
80                 [Test]
81                 [EnvironmentPermission (SecurityAction.PermitOnly, Unrestricted = true)]
82                 public void PartialTrust_PermitOnlyEnvironment ()
83                 {
84                         // ... some methods (or tests) require to read environment variables...
85                         pt.GetTempPath ();
86                 }
87
88                 [Test]
89                 [EnvironmentPermission (SecurityAction.PermitOnly, Unrestricted = true)]
90                 [FileIOPermission (SecurityAction.PermitOnly, Unrestricted = true)]
91                 public void PartialTrust_PermitOnlyEnvironmentFileIO ()
92                 {
93                         // ... some methods (or tests) require to read environment variables
94                         // and file i/o permissions ...
95                         pt.Combine ();
96                         pt.GetTempFileName ();
97                 }
98
99                 [Test]
100                 [FileIOPermission (SecurityAction.PermitOnly, Unrestricted = true)]
101                 public void PartialTrust_PermitOnlyFileIO ()
102                 {
103                         // ... while others do need only FileIOPermission
104                         pt.GetFullPath2 ();
105                         pt.CanonicalizeDots (); // only calls Path.GetFullPath
106                 }
107
108                 // test Demand by denying the required permissions
109
110                 [Test]
111                 [FileIOPermission (SecurityAction.Deny, Unrestricted = true)]
112                 [ExpectedException (typeof (SecurityException))]
113                 public void GetFullPath1 ()
114                 {
115                         Assert.IsNotNull (Path.GetFullPath (cd));
116                 }
117
118                 [Test]
119                 [FileIOPermission (SecurityAction.PermitOnly, Unrestricted = true)]
120                 public void GetFullPath2 ()
121                 {
122                         Assert.IsNotNull (Path.GetFullPath (cd));
123                 }
124
125                 [Test]
126                 [EnvironmentPermission (SecurityAction.PermitOnly, Unrestricted = true)]
127                 public void GetTempFileName1 ()
128                 {
129                         Assert.IsNotNull (Path.GetTempFileName ());
130                         // i.e. no FileIOPermission is required to get a temporary filename
131                 }
132
133                 [Test]
134                 [EnvironmentPermission (SecurityAction.Deny, Write = "USERNAME")]
135                 [ExpectedException (typeof (SecurityException))]
136                 public void GetTempFileName2 ()
137                 {
138                         // yep, Write USERNAME don't make sense - unless the callee
139                         // (indirectly) requires Unrestricted access for EnvironmentPermission 
140                         Assert.IsNotNull (Path.GetTempFileName ());
141                 }
142
143                 [Test]
144                 [EnvironmentPermission (SecurityAction.PermitOnly, Unrestricted = true)]
145                 public void GetTempPath1 ()
146                 {
147                         Assert.IsNotNull (Path.GetTempPath ());
148                         // i.e. no FileIOPermission is required to get the temporary directory
149                 }
150
151                 [Test]
152                 [EnvironmentPermission (SecurityAction.Deny, Write = "USERNAME")]
153                 [ExpectedException (typeof (SecurityException))]
154                 public void GetTempPath2 ()
155                 {
156                         // yep, Write USERNAME don't make sense - unless the callee
157                         // requires Unrestricted access for EnvironmentPermission 
158                         Assert.IsNotNull (Path.GetTempPath ());
159                 }
160
161                 // many calls work only on strings (i.e. they dont access the file system)
162                 // so no Demand for FileIOPermission (or any other) are required
163
164                 [Test]
165                 [PermissionSet (SecurityAction.Deny, Unrestricted = true)]
166                 public void NoFileIOPermission ()
167                 {
168                         Assert.IsNotNull (Path.ChangeExtension ("test.doc", "txt"), "ChangeExtension");
169                         string combine = Path.Combine ("dir", "test.txt");
170                         Assert.IsNotNull (combine, "Combine");
171                         Assert.IsNotNull (Path.GetDirectoryName (combine), "GetDirectoryName");
172                         Assert.IsNotNull (Path.GetExtension ("test.txt"), "GetExtension");
173                         Assert.IsNotNull (Path.GetFileName ("test.txt"), "GetFileName");
174                         Assert.IsNotNull (Path.GetFileNameWithoutExtension ("test.txt"), "GetFileNameWithoutExtension");
175                         Assert.IsNotNull (Path.GetPathRoot (cd), "GetPathRoot");
176                         Assert.IsTrue (Path.HasExtension ("test.txt"), "HasExtension");
177                         Assert.IsFalse (Path.IsPathRooted (combine), "IsPathRooted");
178                 }
179         }
180 }