2 // PathCas.cs -CAS unit tests for System.IO.Path
5 // Sebastien Pouliot <sebastien@ximian.com>
7 // Copyright (C) 2005 Novell, Inc (http://www.novell.com)
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:
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
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.
29 using NUnit.Framework;
33 using System.Reflection;
34 using System.Security;
35 using System.Security.Permissions;
37 namespace MonoCasTests.System.IO {
41 public class PathCas {
43 private MonoTests.System.IO.PathTest pt;
47 public void FixtureSetUp ()
49 // this occurs with a "clean" stack (full trust)
50 pt = new MonoTests.System.IO.PathTest ();
51 cd = Environment.CurrentDirectory;
57 if (!SecurityManager.SecurityEnabled)
58 Assert.Ignore ("SecurityManager.SecurityEnabled is OFF");
62 // Partial Trust Tests - i.e. call "normal" unit with reduced privileges
65 [PermissionSet (SecurityAction.Deny, Unrestricted = true)]
66 public void PartialTrust_DenyUnrestricted_Success ()
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 ();
78 pt.TestDirectoryNameBugs ();
82 [EnvironmentPermission (SecurityAction.PermitOnly, Unrestricted = true)]
83 public void PartialTrust_PermitOnlyEnvironment ()
85 // ... some methods (or tests) require to read environment variables...
86 pt.TestGetTempPath ();
90 [EnvironmentPermission (SecurityAction.PermitOnly, Unrestricted = true)]
91 [FileIOPermission (SecurityAction.PermitOnly, Unrestricted = true)]
92 public void PartialTrust_PermitOnlyEnvironmentFileIO ()
94 // ... some methods (or tests) require to read environment variables
95 // and file i/o permissions ...
97 pt.TestGetTempFileName ();
101 [FileIOPermission (SecurityAction.PermitOnly, Unrestricted = true)]
102 public void PartialTrust_PermitOnlyFileIO ()
104 // ... while others do need only FileIOPermission
105 pt.TestGetFullPath2 ();
106 pt.TestCanonicalizeDots (); // only calls Path.GetFullPath
107 pt.TestGetFullPathUnix (); // calls Environment.CurrentDirectory
110 // test Demand by denying the required permissions
113 [FileIOPermission (SecurityAction.Deny, Unrestricted = true)]
114 [ExpectedException (typeof (SecurityException))]
115 public void GetFullPath1 ()
117 Assert.IsNotNull (Path.GetFullPath (cd));
121 [FileIOPermission (SecurityAction.PermitOnly, Unrestricted = true)]
122 public void GetFullPath2 ()
124 Assert.IsNotNull (Path.GetFullPath (cd));
128 [EnvironmentPermission (SecurityAction.PermitOnly, Unrestricted = true)]
129 public void GetTempFileName1 ()
131 Assert.IsNotNull (Path.GetTempFileName ());
132 // i.e. no FileIOPermission is required to get a temporary filename
136 [EnvironmentPermission (SecurityAction.Deny, Write = "USERNAME")]
137 [ExpectedException (typeof (SecurityException))]
138 public void GetTempFileName2 ()
140 // yep, Write USERNAME don't make sense - unless the callee
141 // (indirectly) requires Unrestricted access for EnvironmentPermission
142 Assert.IsNotNull (Path.GetTempFileName ());
146 [EnvironmentPermission (SecurityAction.PermitOnly, Unrestricted = true)]
147 public void GetTempPath1 ()
149 Assert.IsNotNull (Path.GetTempPath ());
150 // i.e. no FileIOPermission is required to get the temporary directory
154 [EnvironmentPermission (SecurityAction.Deny, Write = "USERNAME")]
155 [ExpectedException (typeof (SecurityException))]
156 public void GetTempPath2 ()
158 // yep, Write USERNAME don't make sense - unless the callee
159 // requires Unrestricted access for EnvironmentPermission
160 Assert.IsNotNull (Path.GetTempPath ());
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
167 [PermissionSet (SecurityAction.Deny, Unrestricted = true)]
168 public void NoFileIOPermission ()
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");