* roottypes.cs: Rename from tree.cs.
[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.TestChangeExtension ();
70                         pt.ChangeExtension_BadExtension ();
71                         pt.TestDirectoryName ();
72                         pt.TestGetExtension ();
73                         pt.TestGetFileName ();
74                         pt.TestGetFileNameWithoutExtension ();
75                         pt.TestGetPathRoot2 ();
76                         pt.TestHasExtension ();
77                         pt.TestRooted ();
78                         pt.TestDirectoryNameBugs ();
79                 }
80
81                 [Test]
82                 [EnvironmentPermission (SecurityAction.PermitOnly, Unrestricted = true)]
83                 public void PartialTrust_PermitOnlyEnvironment ()
84                 {
85                         // ... some methods (or tests) require to read environment variables...
86                         pt.TestGetTempPath ();
87                 }
88
89                 [Test]
90                 [EnvironmentPermission (SecurityAction.PermitOnly, Unrestricted = true)]
91                 [FileIOPermission (SecurityAction.PermitOnly, Unrestricted = true)]
92                 public void PartialTrust_PermitOnlyEnvironmentFileIO ()
93                 {
94                         // ... some methods (or tests) require to read environment variables
95                         // and file i/o permissions ...
96                         pt.TestCombine ();
97                         pt.TestGetTempFileName ();
98                 }
99
100                 [Test]
101                 [FileIOPermission (SecurityAction.PermitOnly, Unrestricted = true)]
102                 public void PartialTrust_PermitOnlyFileIO ()
103                 {
104                         // ... while others do need only FileIOPermission
105                         pt.TestGetFullPath2 ();
106                         pt.TestCanonicalizeDots ();     // only calls Path.GetFullPath
107                         pt.TestGetFullPathUnix ();      // calls Environment.CurrentDirectory
108                 }
109
110                 // test Demand by denying the required permissions
111
112                 [Test]
113                 [FileIOPermission (SecurityAction.Deny, Unrestricted = true)]
114                 [ExpectedException (typeof (SecurityException))]
115                 public void GetFullPath1 ()
116                 {
117                         Assert.IsNotNull (Path.GetFullPath (cd));
118                 }
119
120                 [Test]
121                 [FileIOPermission (SecurityAction.PermitOnly, Unrestricted = true)]
122                 public void GetFullPath2 ()
123                 {
124                         Assert.IsNotNull (Path.GetFullPath (cd));
125                 }
126
127                 [Test]
128                 [EnvironmentPermission (SecurityAction.PermitOnly, Unrestricted = true)]
129                 public void GetTempFileName1 ()
130                 {
131                         Assert.IsNotNull (Path.GetTempFileName ());
132                         // i.e. no FileIOPermission is required to get a temporary filename
133                 }
134
135                 [Test]
136                 [EnvironmentPermission (SecurityAction.Deny, Write = "USERNAME")]
137                 [ExpectedException (typeof (SecurityException))]
138                 public void GetTempFileName2 ()
139                 {
140                         // yep, Write USERNAME don't make sense - unless the callee
141                         // (indirectly) requires Unrestricted access for EnvironmentPermission 
142                         Assert.IsNotNull (Path.GetTempFileName ());
143                 }
144
145                 [Test]
146                 [EnvironmentPermission (SecurityAction.PermitOnly, Unrestricted = true)]
147                 public void GetTempPath1 ()
148                 {
149                         Assert.IsNotNull (Path.GetTempPath ());
150                         // i.e. no FileIOPermission is required to get the temporary directory
151                 }
152
153                 [Test]
154                 [EnvironmentPermission (SecurityAction.Deny, Write = "USERNAME")]
155                 [ExpectedException (typeof (SecurityException))]
156                 public void GetTempPath2 ()
157                 {
158                         // yep, Write USERNAME don't make sense - unless the callee
159                         // requires Unrestricted access for EnvironmentPermission 
160                         Assert.IsNotNull (Path.GetTempPath ());
161                 }
162
163                 // many calls work only on strings (i.e. they dont access the file system)
164                 // so no Demand for FileIOPermission (or any other) are required
165
166                 [Test]
167                 [PermissionSet (SecurityAction.Deny, Unrestricted = true)]
168                 public void NoFileIOPermission ()
169                 {
170                         Assert.IsNotNull (Path.ChangeExtension ("test.doc", "txt"), "ChangeExtension");
171                         string combine = Path.Combine ("dir", "test.txt");
172                         Assert.IsNotNull (combine, "Combine");
173                         Assert.IsNotNull (Path.GetDirectoryName (combine), "GetDirectoryName");
174                         Assert.IsNotNull (Path.GetExtension ("test.txt"), "GetExtension");
175                         Assert.IsNotNull (Path.GetFileName ("test.txt"), "GetFileName");
176                         Assert.IsNotNull (Path.GetFileNameWithoutExtension ("test.txt"), "GetFileNameWithoutExtension");
177                         Assert.IsNotNull (Path.GetPathRoot (cd), "GetPathRoot");
178                         Assert.IsTrue (Path.HasExtension ("test.txt"), "HasExtension");
179                         Assert.IsFalse (Path.IsPathRooted (combine), "IsPathRooted");
180                 }
181         }
182 }