Author: spouliot
Date: 2005-03-24 09:18:23 -0500 (Thu, 24 Mar 2005)
New Revision: 42213
Added:
trunk/mcs/class/corlib/Test/System.IO/FileStreamCas.cs
trunk/mcs/class/corlib/Test/System.IO/PathCas.cs
Modified:
trunk/mcs/class/corlib/Test/System.IO/ChangeLog
trunk/mcs/class/corlib/Test/System.IO/DirectoryCas.cs
trunk/mcs/class/corlib/Test/System.IO/DirectoryTest.cs
trunk/mcs/class/corlib/Test/System.IO/PathTest.cs
Log:
2005-03-24 Sebastien Pouliot <[EMAIL PROTECTED]>
* DirectoryCas.cs: Added partial trust unit tests.
* DirectoryTest.cs: Added missing [Test] attribute to
SetCreationTimeException1.
* FileStreamCas.cs: New. CAS unit tests for FileStream.
* PathCas.cs: New. CAS unit tests for Path.
* PathTest.cs: Splitted TestGetPathRoot test in two so the second part
could be re-used in partial trust tests.
Modified: trunk/mcs/class/corlib/Test/System.IO/ChangeLog
===================================================================
--- trunk/mcs/class/corlib/Test/System.IO/ChangeLog 2005-03-24 14:12:01 UTC
(rev 42212)
+++ trunk/mcs/class/corlib/Test/System.IO/ChangeLog 2005-03-24 14:18:23 UTC
(rev 42213)
@@ -1,3 +1,13 @@
+2005-03-24 Sebastien Pouliot <[EMAIL PROTECTED]>
+
+ * DirectoryCas.cs: Added partial trust unit tests.
+ * DirectoryTest.cs: Added missing [Test] attribute to
+ SetCreationTimeException1.
+ * FileStreamCas.cs: New. CAS unit tests for FileStream.
+ * PathCas.cs: New. CAS unit tests for Path.
+ * PathTest.cs: Splitted TestGetPathRoot test in two so the second part
+ could be re-used in partial trust tests.
+
2005-03-15 Sebastien Pouliot <[EMAIL PROTECTED]>
* FileStreamTest.cs: Added tests for all FileMode when a directory
Modified: trunk/mcs/class/corlib/Test/System.IO/DirectoryCas.cs
===================================================================
--- trunk/mcs/class/corlib/Test/System.IO/DirectoryCas.cs 2005-03-24
14:12:01 UTC (rev 42212)
+++ trunk/mcs/class/corlib/Test/System.IO/DirectoryCas.cs 2005-03-24
14:18:23 UTC (rev 42213)
@@ -40,22 +40,91 @@
[Category ("CAS")]
public class DirectoryCas {
+ private MonoTests.System.IO.DirectoryTest dt;
+ private string dir;
+
+ [TestFixtureSetUp]
+ public void FixtureSetUp ()
+ {
+ // this occurs with a "clean" stack (full trust)
+ dt = new MonoTests.System.IO.DirectoryTest ();
+ dir = Path.Combine (Path.GetTempPath (),
"MonoCasTests.System.IO");
+ }
+
[SetUp]
public void SetUp ()
{
if (!SecurityManager.SecurityEnabled)
Assert.Ignore ("SecurityManager.SecurityEnabled
is OFF");
+ dt.SetUp ();
}
+ [TearDown]
+ public void TearDown ()
+ {
+ dt.TearDown ();
+ }
+
+ [TestFixtureTearDown]
+ public void FixtureTearDown ()
+ {
+ if (Directory.Exists (dir))
+ Directory.Delete (dir, true);
+ }
+
private bool RunningOnWindows {
get { return ((int) Environment.OSVersion.Platform ==
128); }
}
+ // Partial Trust Tests - i.e. call "normal" unit with reduced
privileges
+
[Test]
- [SecurityPermission (SecurityAction.Deny, UnmanagedCode = true)]
+ [FileIOPermission (SecurityAction.PermitOnly, Unrestricted =
true)]
+ public void PartialTrust_PermitOnly_FileIOPermission ()
+ {
+ // test under limited permissions (only
FileIOPermission)
+ dt.CreateDirectory ();
+ dt.Delete ();
+ dt.Exists ();
+ dt.Move ();
+ dt.LastAccessTime ();
+ dt.LastWriteTime ();
+ dt.GetDirectories ();
+ dt.GetFiles ();
+ dt.GetNoFiles ();
+ }
+
+ // test Demand by denying the required permissions
+
+ [Test]
+ [FileIOPermission (SecurityAction.Deny, Unrestricted = true)]
[ExpectedException (typeof (SecurityException))]
+ public void CreateDirectory ()
+ {
+ // FIXME: Change Deny to imperative when supported
+ Directory.CreateDirectory (dir);
+ }
+
+ [Test]
+ [ExpectedException (typeof (SecurityException))]
public void SetCurrentDirectory_DoesntExist ()
{
+ string cd = null;
+ try {
+ cd = Directory.GetCurrentDirectory ();
+ // this will change the current directory (to /
or C:\)
+ // and cause tests failures elsewhere...
+ SetCurrentDirectory_DoesntExist_Restricted ();
+ }
+ finally {
+ // ... unless we return to the original
directory
+ Directory.SetCurrentDirectory (cd);
+ }
+ }
+
+ [SecurityPermission (SecurityAction.Deny, UnmanagedCode = true)]
+ private void SetCurrentDirectory_DoesntExist_Restricted ()
+ {
if (RunningOnWindows) {
Directory.SetCurrentDirectory
("C:\\D0ES-N0T-EX1ST\\");
} else {
Modified: trunk/mcs/class/corlib/Test/System.IO/DirectoryTest.cs
===================================================================
--- trunk/mcs/class/corlib/Test/System.IO/DirectoryTest.cs 2005-03-24
14:12:01 UTC (rev 42212)
+++ trunk/mcs/class/corlib/Test/System.IO/DirectoryTest.cs 2005-03-24
14:18:23 UTC (rev 42213)
@@ -969,8 +969,9 @@
// DeleteDirectory (path);
// }
// }
-
- [ExpectedException(typeof(ArgumentNullException))]
+
+ [Test]
+ [ExpectedException(typeof(ArgumentNullException))]
public void SetCreationTimeException1 ()
{
DateTime time = new DateTime (2003, 4, 6, 6, 4, 2);
Added: trunk/mcs/class/corlib/Test/System.IO/FileStreamCas.cs
===================================================================
--- trunk/mcs/class/corlib/Test/System.IO/FileStreamCas.cs 2005-03-24
14:12:01 UTC (rev 42212)
+++ trunk/mcs/class/corlib/Test/System.IO/FileStreamCas.cs 2005-03-24
14:18:23 UTC (rev 42213)
@@ -0,0 +1,165 @@
+//
+// FileStreamCas.cs -CAS unit tests for System.IO.FileStream
+//
+// Author:
+// Sebastien Pouliot <[EMAIL PROTECTED]>
+//
+// Copyright (C) 2005 Novell, Inc (http://www.novell.com)
+//
+// Permission is hereby granted, free of charge, to any person obtaining
+// a copy of this software and associated documentation files (the
+// "Software"), to deal in the Software without restriction, including
+// without limitation the rights to use, copy, modify, merge, publish,
+// distribute, sublicense, and/or sell copies of the Software, and to
+// permit persons to whom the Software is furnished to do so, subject to
+// the following conditions:
+//
+// The above copyright notice and this permission notice shall be
+// included in all copies or substantial portions of the Software.
+//
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
+// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
+// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
+// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+//
+
+using NUnit.Framework;
+
+using System;
+using System.IO;
+using System.Reflection;
+using System.Security;
+using System.Security.Permissions;
+
+namespace MonoCasTests.System.IO {
+
+ [TestFixture]
+ [Category ("CAS")]
+ public class FileStreamCas {
+
+ private MonoTests.System.IO.FileStreamTest fst;
+
+ [TestFixtureSetUp]
+ public void FixtureSetUp ()
+ {
+ // this occurs with a "clean" stack (full trust)
+ fst = new MonoTests.System.IO.FileStreamTest ();
+ }
+
+ [SetUp]
+ public void SetUp ()
+ {
+ if (!SecurityManager.SecurityEnabled)
+ Assert.Ignore ("SecurityManager.SecurityEnabled
is OFF");
+ }
+
+ // Partial Trust Tests - i.e. call "normal" unit with reduced
privileges
+
+ [Test]
+ [ExpectedException (typeof (SecurityException))]
+ public void PartialTrust_DenyUnrestricted_Failure ()
+ {
+ try {
+ // SetUp/TearDown requires FileIOPermission
+ fst.SetUp ();
+ // so does the call but that's the test ;-)
+ CallRestricted (fst);
+ }
+ finally {
+ fst.TearDown ();
+ }
+ }
+
+ [PermissionSet (SecurityAction.Deny, Unrestricted = true)]
+ private void CallRestricted (MonoTests.System.IO.FileStreamTest
fst)
+ {
+ fst.TestDefaultProperties ();
+ }
+
+ [Test]
+ [FileIOPermission (SecurityAction.PermitOnly, Unrestricted =
true)]
+ public void PartialTrust_PermitOnly_FileIOPermission_Success ()
+ {
+ fst.SetUp ();
+ fst.TestCtr ();
+ fst.CtorAccess1Read2Read ();
+ fst.Write ();
+ fst.Length ();
+ fst.Flush ();
+ fst.TestDefaultProperties ();
+ fst.TestLock ();
+ fst.Seek ();
+ fst.TestSeek ();
+ fst.TestClose ();
+ fst.PositionAfterSetLength ();
+ fst.ReadBytePastEndOfStream ();
+ fst.TearDown ();
+ }
+
+ [Test]
+ [FileIOPermission (SecurityAction.PermitOnly, Unrestricted =
true)]
+ [SecurityPermission (SecurityAction.PermitOnly, UnmanagedCode =
true)]
+ public void
PartialTrust_PermitOnly_FileIOPermissionUnmanagedCode_Success ()
+ {
+ fst.SetUp ();
+ fst.TestFlushNotOwningHandle ();
+ fst.TearDown ();
+ }
+
+ // test Demand by denying the required permissions
+
+ [Test]
+ [SecurityPermission (SecurityAction.Deny, UnmanagedCode = true)]
+ [ExpectedException (typeof (SecurityException))]
+ public void Ctor_IntPtrFileAccess ()
+ {
+ new FileStream (IntPtr.Zero, FileAccess.Read);
+ }
+
+ [Test]
+ [SecurityPermission (SecurityAction.Deny, UnmanagedCode = true)]
+ [ExpectedException (typeof (SecurityException))]
+ public void Ctor_IntPtrFileAccessBool ()
+ {
+ new FileStream (IntPtr.Zero, FileAccess.Read, false);
+ }
+
+ [Test]
+ [SecurityPermission (SecurityAction.Deny, UnmanagedCode = true)]
+ [ExpectedException (typeof (SecurityException))]
+ public void Ctor_IntPtrFileAccessBoolInt ()
+ {
+ new FileStream (IntPtr.Zero, FileAccess.Read, false, 0);
+ }
+
+ [Test]
+ [SecurityPermission (SecurityAction.Deny, UnmanagedCode = true)]
+ [ExpectedException (typeof (SecurityException))]
+ public void Ctor_IntPtrFileAccessBoolIntBool ()
+ {
+ new FileStream (IntPtr.Zero, FileAccess.Read, false, 0,
false);
+ }
+
+ // we use reflection to call FileStream as the Handle property
is protected
+ // by a LinkDemand (which will be converted into full demand,
i.e. a stack
+ // walk) when reflection is used (i.e. it gets testable).
+
+ [Test]
+ [SecurityPermission (SecurityAction.Deny, UnmanagedCode = true)]
+ [ExpectedException (typeof (SecurityException))]
+ public void Handle ()
+ {
+ FileStream fs = File.OpenWrite (Path.GetTempFileName
());
+ try {
+ MethodInfo mi = typeof (FileStream).GetProperty
("Handle").GetGetMethod ();
+ mi.Invoke (fs, null);
+ }
+ finally {
+ fs.Close ();
+ }
+ }
+ }
+}
Added: trunk/mcs/class/corlib/Test/System.IO/PathCas.cs
===================================================================
--- trunk/mcs/class/corlib/Test/System.IO/PathCas.cs 2005-03-24 14:12:01 UTC
(rev 42212)
+++ trunk/mcs/class/corlib/Test/System.IO/PathCas.cs 2005-03-24 14:18:23 UTC
(rev 42213)
@@ -0,0 +1,182 @@
+//
+// PathCas.cs -CAS unit tests for System.IO.Path
+//
+// Author:
+// Sebastien Pouliot <[EMAIL PROTECTED]>
+//
+// Copyright (C) 2005 Novell, Inc (http://www.novell.com)
+//
+// Permission is hereby granted, free of charge, to any person obtaining
+// a copy of this software and associated documentation files (the
+// "Software"), to deal in the Software without restriction, including
+// without limitation the rights to use, copy, modify, merge, publish,
+// distribute, sublicense, and/or sell copies of the Software, and to
+// permit persons to whom the Software is furnished to do so, subject to
+// the following conditions:
+//
+// The above copyright notice and this permission notice shall be
+// included in all copies or substantial portions of the Software.
+//
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
+// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
+// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
+// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+//
+
+using NUnit.Framework;
+
+using System;
+using System.IO;
+using System.Reflection;
+using System.Security;
+using System.Security.Permissions;
+
+namespace MonoCasTests.System.IO {
+
+ [TestFixture]
+ [Category ("CAS")]
+ public class PathCas {
+
+ private MonoTests.System.IO.PathTest pt;
+ private string cd;
+
+ [TestFixtureSetUp]
+ public void FixtureSetUp ()
+ {
+ // this occurs with a "clean" stack (full trust)
+ pt = new MonoTests.System.IO.PathTest ();
+ cd = Environment.CurrentDirectory;
+ }
+
+ [SetUp]
+ public void SetUp ()
+ {
+ if (!SecurityManager.SecurityEnabled)
+ Assert.Ignore ("SecurityManager.SecurityEnabled
is OFF");
+ pt.SetUp ();
+ }
+
+ // Partial Trust Tests - i.e. call "normal" unit with reduced
privileges
+
+ [Test]
+ [PermissionSet (SecurityAction.Deny, Unrestricted = true)]
+ public void PartialTrust_DenyUnrestricted_Success ()
+ {
+ // some calls do not require any permissions...
+ pt.TestChangeExtension ();
+ pt.ChangeExtension_BadExtension ();
+ pt.TestDirectoryName ();
+ pt.TestGetExtension ();
+ pt.TestGetFileName ();
+ pt.TestGetFileNameWithoutExtension ();
+ pt.TestGetPathRoot2 ();
+ pt.TestHasExtension ();
+ pt.TestRooted ();
+ pt.TestDirectoryNameBugs ();
+ }
+
+ [Test]
+ [EnvironmentPermission (SecurityAction.PermitOnly, Unrestricted
= true)]
+ public void PartialTrust_PermitOnlyEnvironment ()
+ {
+ // ... some methods (or tests) require to read
environment variables...
+ pt.TestGetTempPath ();
+ }
+
+ [Test]
+ [EnvironmentPermission (SecurityAction.PermitOnly, Unrestricted
= true)]
+ [FileIOPermission (SecurityAction.PermitOnly, Unrestricted =
true)]
+ public void PartialTrust_PermitOnlyEnvironmentFileIO ()
+ {
+ // ... some methods (or tests) require to read
environment variables
+ // and file i/o permissions ...
+ pt.TestCombine ();
+ pt.TestGetTempFileName ();
+ }
+
+ [Test]
+ [FileIOPermission (SecurityAction.PermitOnly, Unrestricted =
true)]
+ public void PartialTrust_PermitOnlyFileIO ()
+ {
+ // ... while others do need only FileIOPermission
+ pt.TestGetFullPath2 ();
+ pt.TestCanonicalizeDots (); // only calls
Path.GetFullPath
+ pt.TestGetFullPathUnix (); // calls
Environment.CurrentDirectory
+ }
+
+ // test Demand by denying the required permissions
+
+ [Test]
+ [FileIOPermission (SecurityAction.Deny, Unrestricted = true)]
+ [ExpectedException (typeof (SecurityException))]
+ public void GetFullPath1 ()
+ {
+ Assert.IsNotNull (Path.GetFullPath (cd));
+ }
+
+ [Test]
+ [FileIOPermission (SecurityAction.PermitOnly, Unrestricted =
true)]
+ public void GetFullPath2 ()
+ {
+ Assert.IsNotNull (Path.GetFullPath (cd));
+ }
+
+ [Test]
+ [EnvironmentPermission (SecurityAction.PermitOnly, Unrestricted
= true)]
+ public void GetTempFileName1 ()
+ {
+ Assert.IsNotNull (Path.GetTempFileName ());
+ // i.e. no FileIOPermission is required to get a
temporary filename
+ }
+
+ [Test]
+ [EnvironmentPermission (SecurityAction.Deny, Write =
"USERNAME")]
+ [ExpectedException (typeof (SecurityException))]
+ public void GetTempFileName2 ()
+ {
+ // yep, Write USERNAME don't make sense - unless the
callee
+ // (indirectly) requires Unrestricted access for
EnvironmentPermission
+ Assert.IsNotNull (Path.GetTempFileName ());
+ }
+
+ [Test]
+ [EnvironmentPermission (SecurityAction.PermitOnly, Unrestricted
= true)]
+ public void GetTempPath1 ()
+ {
+ Assert.IsNotNull (Path.GetTempPath ());
+ // i.e. no FileIOPermission is required to get the
temporary directory
+ }
+
+ [Test]
+ [EnvironmentPermission (SecurityAction.Deny, Write =
"USERNAME")]
+ [ExpectedException (typeof (SecurityException))]
+ public void GetTempPath2 ()
+ {
+ // yep, Write USERNAME don't make sense - unless the
callee
+ // requires Unrestricted access for
EnvironmentPermission
+ Assert.IsNotNull (Path.GetTempPath ());
+ }
+
+ // many calls work only on strings (i.e. they dont access the
file system)
+ // so no Demand for FileIOPermission (or any other) are required
+
+ [Test]
+ [PermissionSet (SecurityAction.Deny, Unrestricted = true)]
+ public void NoFileIOPermission ()
+ {
+ Assert.IsNotNull (Path.ChangeExtension ("test.doc",
"txt"), "ChangeExtension");
+ string combine = Path.Combine ("dir", "test.txt");
+ Assert.IsNotNull (combine, "Combine");
+ Assert.IsNotNull (Path.GetDirectoryName (combine),
"GetDirectoryName");
+ Assert.IsNotNull (Path.GetExtension ("test.txt"),
"GetExtension");
+ Assert.IsNotNull (Path.GetFileName ("test.txt"),
"GetFileName");
+ Assert.IsNotNull (Path.GetFileNameWithoutExtension
("test.txt"), "GetFileNameWithoutExtension");
+ Assert.IsNotNull (Path.GetPathRoot (cd), "GetPathRoot");
+ Assert.IsTrue (Path.HasExtension ("test.txt"),
"HasExtension");
+ Assert.IsFalse (Path.IsPathRooted (combine),
"IsPathRooted");
+ }
+ }
+}
Modified: trunk/mcs/class/corlib/Test/System.IO/PathTest.cs
===================================================================
--- trunk/mcs/class/corlib/Test/System.IO/PathTest.cs 2005-03-24 14:12:01 UTC
(rev 42212)
+++ trunk/mcs/class/corlib/Test/System.IO/PathTest.cs 2005-03-24 14:18:23 UTC
(rev 42213)
@@ -479,8 +479,15 @@
string pathRoot = Path.GetPathRoot (current);
AssertEquals ("GetPathRoot #01", expected, pathRoot);
+ }
- pathRoot = Path.GetPathRoot ("hola");
+ [Test]
+ public void TestGetPathRoot2 ()
+ {
+ // note: this method doesn't call
Directory.GetCurrentDirectory so it can be
+ // reused for partial trust unit tests in PathCas.cs
+
+ string pathRoot = Path.GetPathRoot ("hola");
AssertEquals ("GetPathRoot #02", String.Empty,
pathRoot);
pathRoot = Path.GetPathRoot (null);
_______________________________________________
Mono-patches maillist - [email protected]
http://lists.ximian.com/mailman/listinfo/mono-patches