New test.
[mono.git] / mcs / class / corlib / Test / System.IO / DirectoryCas.cs
index f7e46f98a6946761a2c12f27bc1849d1b2c5e7e5..de42f26365e02e279974e510efba59c7a7fb76e4 100644 (file)
@@ -40,21 +40,95 @@ namespace MonoCasTests.System.IO {
        [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");\r
+               }
+
                [SetUp]
                public void SetUp ()
                {
                        if (!SecurityManager.SecurityEnabled)
                                Assert.Ignore ("SecurityManager.SecurityEnabled is OFF");
+                       dt.SetUp ();
+               }
+
+               [TearDown]\r
+               public void TearDown () 
+               {
+                       dt.TearDown ();
+               }\r
+
+               [TestFixtureTearDown]
+               public void FixtureTearDown ()
+               {
+                       if (Directory.Exists (dir))\r
+                               Directory.Delete (dir, true);\r
                }
 
                private bool RunningOnWindows {
-                       get { return ((int) Environment.OSVersion.Platform == 128); }
+                       get {
+                               // check for non-Unix platforms - see FAQ for more details
+                               // http://www.mono-project.com/FAQ:_Technical#How_to_detect_the_execution_platform_.3F
+                               int platform = (int) Environment.OSVersion.Platform;
+                               return ((platform != 4) && (platform != 128));
+                       }
+               }
+
+               // Partial Trust Tests - i.e. call "normal" unit with reduced privileges
+
+               [Test]
+               [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]
-               [SecurityPermission (SecurityAction.Deny, UnmanagedCode = true)]
                [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\\");