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.ChangeExtension ();
70 pt.ChangeExtension_Extension_InvalidPathChars ();
71 pt.GetDirectoryName ();
74 pt.GetFileNameWithoutExtension ();
81 [EnvironmentPermission (SecurityAction.PermitOnly, Unrestricted = true)]
82 public void PartialTrust_PermitOnlyEnvironment ()
84 // ... some methods (or tests) require to read environment variables...
89 [EnvironmentPermission (SecurityAction.PermitOnly, Unrestricted = true)]
90 [FileIOPermission (SecurityAction.PermitOnly, Unrestricted = true)]
91 public void PartialTrust_PermitOnlyEnvironmentFileIO ()
93 // ... some methods (or tests) require to read environment variables
94 // and file i/o permissions ...
96 pt.GetTempFileName ();
100 [FileIOPermission (SecurityAction.PermitOnly, Unrestricted = true)]
101 public void PartialTrust_PermitOnlyFileIO ()
103 // ... while others do need only FileIOPermission
105 pt.CanonicalizeDots (); // only calls Path.GetFullPath
108 // test Demand by denying the required permissions
111 [FileIOPermission (SecurityAction.Deny, Unrestricted = true)]
112 [ExpectedException (typeof (SecurityException))]
113 public void GetFullPath1 ()
115 Assert.IsNotNull (Path.GetFullPath (cd));
119 [FileIOPermission (SecurityAction.PermitOnly, Unrestricted = true)]
120 public void GetFullPath2 ()
122 Assert.IsNotNull (Path.GetFullPath (cd));
126 [EnvironmentPermission (SecurityAction.PermitOnly, Unrestricted = true)]
127 public void GetTempFileName1 ()
129 Assert.IsNotNull (Path.GetTempFileName ());
130 // i.e. no FileIOPermission is required to get a temporary filename
134 [EnvironmentPermission (SecurityAction.Deny, Write = "USERNAME")]
135 [ExpectedException (typeof (SecurityException))]
136 public void GetTempFileName2 ()
138 // yep, Write USERNAME don't make sense - unless the callee
139 // (indirectly) requires Unrestricted access for EnvironmentPermission
140 Assert.IsNotNull (Path.GetTempFileName ());
144 [EnvironmentPermission (SecurityAction.PermitOnly, Unrestricted = true)]
145 public void GetTempPath1 ()
147 Assert.IsNotNull (Path.GetTempPath ());
148 // i.e. no FileIOPermission is required to get the temporary directory
152 [EnvironmentPermission (SecurityAction.Deny, Write = "USERNAME")]
153 [ExpectedException (typeof (SecurityException))]
154 public void GetTempPath2 ()
156 // yep, Write USERNAME don't make sense - unless the callee
157 // requires Unrestricted access for EnvironmentPermission
158 Assert.IsNotNull (Path.GetTempPath ());
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
165 [PermissionSet (SecurityAction.Deny, Unrestricted = true)]
166 public void NoFileIOPermission ()
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");