* FileStream.cs: To match MSFT, ignore FileShare.Inheritable on 2.0
authorGert Driesen <drieseng@users.sourceforge.net>
Mon, 27 Feb 2006 17:45:07 +0000 (17:45 -0000)
committerGert Driesen <drieseng@users.sourceforge.net>
Mon, 27 Feb 2006 17:45:07 +0000 (17:45 -0000)
profile. This fixes bug #77644. Improved usefulness of some existing
exception messages.
* FileStreamTest.cs: Fixed tests for invalid share value to pass on
MS.NET 2.0, and all Mono profiles. Added test that verifies the
correct behavior of all possible FileMode and FileAccess combinations.
Fixed compiler warning.

svn path=/trunk/mcs/; revision=57337

mcs/class/corlib/System.IO/ChangeLog
mcs/class/corlib/System.IO/FileStream.cs
mcs/class/corlib/Test/System.IO/ChangeLog
mcs/class/corlib/Test/System.IO/FileStreamTest.cs

index 99daa98583d5eaaf7ea592929758c3eeeb4b5b77..0ba43550c3128f0dfcf468d62ab6f2a4ee7cc19e 100644 (file)
@@ -1,3 +1,9 @@
+2006-02-26  Gert Driesen  <drieseng@users.sourceforge.net>
+
+       * FileStream.cs: To match MSFT, ignore FileShare.Inheritable on 2.0
+       profile. This fixes bug #77644. Improved usefulness of some existing
+       exception messages.
+
 2006-02-22  Joerg Rosenkranz <joergr@voelcker.com>
 
        * MonoIO.cs, MonoIOError.cs: Verbose exception for error 39 
index 2181a3d7f48c8ecb4d67ee914c9d1d23cac45611..738dc0e859ded687be7f4bc28088828752dd81cd 100644 (file)
@@ -142,17 +142,22 @@ namespace System.IO
                                throw new ArgumentException ("Name is empty");
                        }
 
+#if NET_2_0
+                       // ignore the Inheritable flag
+                       share &= ~FileShare.Inheritable;
+#endif
+
                        if (bufferSize <= 0)
                                throw new ArgumentOutOfRangeException ("Positive number required.");
 
                        if (mode < FileMode.CreateNew || mode > FileMode.Append)
-                               throw new ArgumentOutOfRangeException ("mode");
+                               throw new ArgumentOutOfRangeException ("mode", "Enum value was out of legal range.");
 
                        if (access < FileAccess.Read || access > FileAccess.ReadWrite)
-                               throw new ArgumentOutOfRangeException ("access");
+                               throw new ArgumentOutOfRangeException ("access", "Enum value was out of legal range.");
 
                        if (share < FileShare.None || share > FileShare.ReadWrite)
-                               throw new ArgumentOutOfRangeException ("share");
+                               throw new ArgumentOutOfRangeException ("share", "Enum value was out of legal range.");
 
                        if (name.IndexOfAny (Path.InvalidPathChars) != -1) {
                                throw new ArgumentException ("Name has invalid chars");
@@ -169,13 +174,16 @@ namespace System.IO
                         * docs)
                         */
                        if (mode==FileMode.Append &&
-                           (access&FileAccess.Read)==FileAccess.Read) {
-                               throw new ArgumentException("Append streams can not be read");
+                               (access&FileAccess.Read)==FileAccess.Read) {
+                               throw new ArgumentException("Append access can be requested only in write-only mode.");
                        }
 
                        if ((access & FileAccess.Write) == 0 &&
-                           (mode != FileMode.Open && mode != FileMode.OpenOrCreate))
-                               throw new ArgumentException ("access and mode not compatible");
+                               (mode != FileMode.Open && mode != FileMode.OpenOrCreate)) {
+                               string msg = Locale.GetText ("Combining FileMode: {0} with " +
+                                       "FileAccess: {1} is invalid.");
+                               throw new ArgumentException (string.Format (msg, access, mode));
+                       }
 
                        string dname = Path.GetDirectoryName (name);
                        if (dname.Length > 0) {
index d98a09a05894ad076e00f432ad9ddb28ae4b0e81..4f2d8e3bf82fa4ac06fa7af37a940bbe17adb47c 100644 (file)
@@ -1,3 +1,10 @@
+2006-02-26  Gert Driesen  <drieseng@users.sourceforge.net>
+
+       * FileStreamTest.cs: Fixed tests for invalid share value to pass on
+       MS.NET 2.0, and all Mono profiles. Added test that verifies the 
+       correct behavior of all possible FileMode and FileAccess combinations.
+       Fixed compiler warning.
+
 2006-02-25  Gert Driesen  <drieseng@users.sourceforge.net>
 
        * FileStreamTest.cs: Spaces to tabs, (re)numbered tests.
index ea4624ed74782da6582269bfd69d2885600d3431..0851359f6940996be8cb5cd0f6ce932b5e8b7042 100644 (file)
@@ -219,7 +219,14 @@ namespace MonoTests.System.IO
                }
 
                [Test]
+#if NET_2_0
+               // FileShare.Inheritable is ignored, but file does not exist
+               [ExpectedException (typeof (FileNotFoundException))]
+#else
+               // share: Enum value was out of legal range.
+               // (FileShare.Inheritable is not valid)
                [ExpectedException (typeof (ArgumentOutOfRangeException))]
+#endif
                public void CtorArgumentOutOfRangeException3 ()
                {
                        string path = TempFolder + DSC + "CtorArgumentOutOfRangeException1";
@@ -227,7 +234,7 @@ namespace MonoTests.System.IO
 
                        FileStream stream = null;
                        try {
-                               stream = new FileStream (path, FileMode.CreateNew, FileAccess.Read, FileShare.None | FileShare.Inheritable);
+                               stream = new FileStream (path, FileMode.Open, FileAccess.Read, FileShare.Inheritable);
                        } finally {
                                if (stream != null)
                                        stream.Close ();
@@ -294,7 +301,14 @@ namespace MonoTests.System.IO
 
 
                [Test]
+#if NET_2_0
+               // FileShare.Inheritable is ignored, but file does not exist
+               [ExpectedException (typeof (FileNotFoundException))]
+#else
+               // share: Enum value was out of legal range.
+               // (FileShare.Inheritable is not valid)
                [ExpectedException (typeof (ArgumentOutOfRangeException))]
+#endif
                public void CtorArgumentOutOfRangeException5 ()
                {
                        string path = TempFolder + Path.DirectorySeparatorChar + "temp";
@@ -302,7 +316,7 @@ namespace MonoTests.System.IO
 
                        FileStream stream = null;
                        try {
-                               stream = new FileStream (path, FileMode.CreateNew, FileAccess.Read, FileShare.Inheritable | FileShare.ReadWrite);
+                               stream = new FileStream (path, FileMode.Open, FileAccess.Read, FileShare.Inheritable | FileShare.ReadWrite);
                        } finally {
                                if (stream != null)
                                        stream.Close ();
@@ -330,6 +344,235 @@ namespace MonoTests.System.IO
                        }
                }
 
+               [Test]
+               public void ModeAndAccessCombinations ()
+               {
+                       string path = TempFolder + Path.DirectorySeparatorChar + "temp";
+                       DeleteFile (path);
+                       FileStream stream = null;
+
+                       // Append / Read
+                       try {
+                               // Append access can be requested only in write-only mode
+                               stream = new FileStream (path, FileMode.Append, FileAccess.Read);
+                               Assert.Fail ("#A1");
+                       } catch (ArgumentException ex) {
+                               Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#A2");
+                       } finally {
+                               if (stream != null)
+                                       stream.Close ();
+
+                               DeleteFile (path);
+                       }
+
+                       // Append / ReadWrite
+                       try {
+                               // Append access can be requested only in write-only mode
+                               stream = new FileStream (path, FileMode.Append, FileAccess.ReadWrite);
+                               Assert.Fail ("#B1");
+                       } catch (ArgumentException ex) {
+                               // make sure it is exact this exception, and not a derived type
+                               Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#B2");
+                       } finally {
+                               if (stream != null)
+                                       stream.Close ();
+
+                               DeleteFile (path);
+                       }
+
+                       // Append / Write
+                       try {
+                               stream = new FileStream (path, FileMode.Append, FileAccess.Write);
+                       } finally {
+                               if (stream != null)
+                                       stream.Close ();
+
+                               DeleteFile (path);
+                       }
+
+                       // Create / Read
+                       try {
+                               stream = new FileStream (path, FileMode.Create, FileAccess.Read);
+                               Assert.Fail ("#C1");
+                       } catch (ArgumentException ex) {
+                               // make sure it is exact this exception, and not a derived type
+                               Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#C2");
+                       } finally {
+                               if (stream != null)
+                                       stream.Close ();
+
+                               DeleteFile (path);
+                       }
+
+                       // Create / ReadWrite
+                       try {
+                               stream = new FileStream (path, FileMode.Create, FileAccess.ReadWrite);
+                       } finally {
+                               if (stream != null)
+                                       stream.Close ();
+
+                               DeleteFile (path);
+                       }
+
+                       // Create / Write
+                       try {
+                               stream = new FileStream (path, FileMode.Create, FileAccess.Write);
+                       } finally {
+                               if (stream != null)
+                                       stream.Close ();
+
+                               DeleteFile (path);
+                       }
+
+                       // CreateNew / Read
+                       try {
+                               stream = new FileStream (path, FileMode.CreateNew, FileAccess.Read);
+                               Assert.Fail ("#D1");
+                       } catch (ArgumentException ex) {
+                               // make sure it is exact this exception, and not a derived type
+                               Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#D2");
+                       } finally {
+                               if (stream != null)
+                                       stream.Close ();
+
+                               DeleteFile (path);
+                       }
+
+                       // CreateNew / ReadWrite
+                       try {
+                               stream = new FileStream (path, FileMode.CreateNew, FileAccess.ReadWrite);
+                       } finally {
+                               if (stream != null)
+                                       stream.Close ();
+
+                               DeleteFile (path);
+                       }
+
+                       // CreateNew / Write
+                       try {
+                               stream = new FileStream (path, FileMode.CreateNew, FileAccess.Write);
+                       } finally {
+                               if (stream != null)
+                                       stream.Close ();
+
+                               DeleteFile (path);
+                       }
+
+                       // Open / Read
+                       try {
+                               stream = new FileStream (path, FileMode.Open, FileAccess.Read);
+                               Assert.Fail ("#E1");
+                       } catch (FileNotFoundException ex) {
+                               // make sure it is exact this exception, and not a derived type
+                               Assert.AreEqual (typeof (FileNotFoundException), ex.GetType (), "#E2");
+                       } finally {
+                               if (stream != null)
+                                       stream.Close ();
+
+                               DeleteFile (path);
+                       }
+
+                       // Open / ReadWrite
+                       try {
+                               stream = new FileStream (path, FileMode.Open, FileAccess.ReadWrite);
+                               Assert.Fail ("#F1");
+                       } catch (FileNotFoundException ex) {
+                               // make sure it is exact this exception, and not a derived type
+                               Assert.AreEqual (typeof (FileNotFoundException), ex.GetType (), "#F2");
+                       } finally {
+                               if (stream != null)
+                                       stream.Close ();
+
+                               DeleteFile (path);
+                       }
+
+                       // Open / Write
+                       try {
+                               stream = new FileStream (path, FileMode.Open, FileAccess.Write);
+                               Assert.Fail ("#G1");
+                       } catch (FileNotFoundException ex) {
+                               // make sure it is exact this exception, and not a derived type
+                               Assert.AreEqual (typeof (FileNotFoundException), ex.GetType (), "#G2");
+                       } finally {
+                               if (stream != null)
+                                       stream.Close ();
+
+                               DeleteFile (path);
+                       }
+
+                       // OpenOrCreate / Read
+                       try {
+                               stream = new FileStream (path, FileMode.OpenOrCreate, FileAccess.Read);
+                       } finally {
+                               if (stream != null)
+                                       stream.Close ();
+
+                               DeleteFile (path);
+                       }
+
+                       // OpenOrCreate / ReadWrite
+                       try {
+                               stream = new FileStream (path, FileMode.OpenOrCreate, FileAccess.ReadWrite);
+                       } finally {
+                               if (stream != null)
+                                       stream.Close ();
+
+                               DeleteFile (path);
+                       }
+
+                       // OpenOrCreate / Write
+                       try {
+                               stream = new FileStream (path, FileMode.OpenOrCreate, FileAccess.Write);
+                       } finally {
+                               if (stream != null)
+                                       stream.Close ();
+
+                               DeleteFile (path);
+                       }
+
+                       // Truncate / Read
+                       try {
+                               stream = new FileStream (path, FileMode.Truncate, FileAccess.Read);
+                               Assert.Fail ("#H1");
+                       } catch (ArgumentException ex) {
+                               // make sure it is exact this exception, and not a derived type
+                               Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#H2");
+                       } finally {
+                               if (stream != null)
+                                       stream.Close ();
+
+                               DeleteFile (path);
+                       }
+
+                       // Truncate / ReadWrite
+                       try {
+                               stream = new FileStream (path, FileMode.Truncate, FileAccess.ReadWrite);
+                               Assert.Fail ("#I1");
+                       } catch (FileNotFoundException ex) {
+                               // make sure it is exact this exception, and not a derived type
+                               Assert.AreEqual (typeof (FileNotFoundException), ex.GetType (), "#I2");
+                       } finally {
+                               if (stream != null)
+                                       stream.Close ();
+
+                               DeleteFile (path);
+                       }
+
+                       // Truncate / Write
+                       try {
+                               stream = new FileStream (path, FileMode.Truncate, FileAccess.Write);
+                               Assert.Fail ("#J1");
+                       } catch (FileNotFoundException ex) {
+                               // make sure it is exact this exception, and not a derived type
+                               Assert.AreEqual (typeof (FileNotFoundException), ex.GetType (), "#J2");
+                       } finally {
+                               if (stream != null)
+                                       stream.Close ();
+
+                               DeleteFile (path);
+                       }
+               }
+
                [Test, ExpectedException (typeof (IOException))]
                public void CtorIOException2 ()
                {
@@ -624,7 +867,7 @@ namespace MonoTests.System.IO
                        try {
                                stream2.Read (bytes, 0, 5);
                                Assert.Fail ("#A1");
-                       } catch (Exception e) {
+                       } catch (Exception) {
                                // Bug #71371: on MS.NET you get an IOException detailing a lock
                                // Assert.AreEqual (typeof (IOException), e.GetType (), "#A2");
                        }