Merge pull request #3528 from BrzVlad/fix-sgen-check-before-collections
[mono.git] / mcs / class / corlib / Test / System.IO / FileStreamTest.cs
index dca54d652affc4eb71ca1c2c335bc4b7ffdfe327..b07955903f41a1946e709d8568c1fa42c8c02529 100644 (file)
@@ -25,10 +25,6 @@ namespace MonoTests.System.IO
        {
                string TempFolder = Path.Combine (Path.GetTempPath (), "MonoTests.System.IO.Tests");
                static readonly char DSC = Path.DirectorySeparatorChar;
-               static bool MacOSX = false;
-
-               [DllImport ("libc")]
-               static extern int uname (IntPtr buf);
 
                [TearDown]
                public void TearDown ()
@@ -44,13 +40,6 @@ namespace MonoTests.System.IO
                                Directory.Delete (TempFolder, true);
 
                        Directory.CreateDirectory (TempFolder);
-#if !MOBILE                    
-                       // from XplatUI.cs
-                       IntPtr buf = Marshal.AllocHGlobal (8192);
-                       if (uname (buf) == 0)
-                               MacOSX = Marshal.PtrToStringAnsi (buf) == "Darwin";
-                       Marshal.FreeHGlobal (buf);
-#endif
                }
 
                public void TestCtr ()
@@ -1532,15 +1521,23 @@ namespace MonoTests.System.IO
                static IAsyncResult DoBeginWrite(Stream stream, ManualResetEvent mre, byte[] RandomBuffer)
                {
                        return stream.BeginWrite (RandomBuffer, 0, RandomBuffer.Length, ar => {
-                               stream.EndWrite (ar);
+                               IAsyncResult begin_write_recursive_ares;
+
+                               try {
+                                       stream.EndWrite (ar);
+
+                                       // we don't supply an ManualResetEvent so this will throw an NRE on the second run
+                                       // which nunit-console will ignore (but other test runners don't like that)
+                                       if (mre == null)
+                                               return;
 
-                               // we don't supply an ManualResetEvent so this will throw an NRE on the second run
-                               // which nunit-console will ignore (but other test runners don't like that)
-                               if (mre == null)
-                                       return;
+                                       begin_write_recursive_ares = DoBeginWrite (stream, null, RandomBuffer);
+                                       begin_write_recursive_ares.AsyncWaitHandle.WaitOne ();
 
-                               DoBeginWrite (stream, null, RandomBuffer).AsyncWaitHandle.WaitOne ();
-                               mre.Set ();
+                                       mre.Set ();
+                               } catch (ObjectDisposedException e) {
+                                       Console.WriteLine ("stream was disposed: {0}", e);
+                               }
                        }, null);
                }
 
@@ -1549,12 +1546,19 @@ namespace MonoTests.System.IO
                {
                        string path = TempFolder + Path.DirectorySeparatorChar + "temp";
                        DeleteFile (path);
-       
-                       using (FileStream stream = new FileStream (path, FileMode.OpenOrCreate, FileAccess.Write)) {
-                               var mre = new ManualResetEvent (false); 
-                               var RandomBuffer = new byte[1024];                      
-                               DoBeginWrite (stream, mre, RandomBuffer);
-                               Assert.IsTrue (mre.WaitOne (5000), "#1");
+
+                       IAsyncResult begin_write_ares = null;
+
+                       try {
+                               using (FileStream stream = new FileStream (path, FileMode.OpenOrCreate, FileAccess.Write)) {
+                                       var mre = new ManualResetEvent (false);
+                                       var RandomBuffer = new byte[1024];
+                                       begin_write_ares = DoBeginWrite (stream, mre, RandomBuffer);
+                                       Assert.IsTrue (mre.WaitOne (5000), "#1");
+                               }
+                       } finally {
+                               if (begin_write_ares != null)
+                                       begin_write_ares.AsyncWaitHandle.WaitOne ();
                        }
                }
 
@@ -1645,6 +1649,28 @@ namespace MonoTests.System.IO
                        
                }
 
+               [Test]
+               public void OpenCharDeviceRepeatedly ()
+               {
+                       // https://bugzilla.xamarin.com/show_bug.cgi?id=38408
+                       try {
+                               using (var f = new FileStream ("/dev/zero", FileMode.Open))
+                               {
+                               }
+                       } catch (FileNotFoundException) {
+                               // Only run this test on platforms where /dev/zero exists
+                               Assert.Ignore();
+                       } catch (DirectoryNotFoundException) {
+                               // Only run this test on platforms where /dev exists
+                               Assert.Ignore();
+                       }
+
+                       // this shouldn't throw
+                       using (var g = new FileStream ("/dev/zero", FileMode.Open))
+                       {
+                       }
+               }
+
 #if !MOBILE
                [Test]
                public void WriteWithExposedHandle ()
@@ -1691,5 +1717,30 @@ namespace MonoTests.System.IO
                        }
                }
 #endif
+
+               [Test] // Covers #11699
+               public void ReadWriteFileLength ()
+               {
+                       int bufferSize = 128;
+                       int readLength = 1;
+                       int writeLength = bufferSize + 1;
+
+                       string path = TempFolder + DSC + "readwritefilelength.tmp";
+
+                       try {
+                               File.WriteAllBytes (path, new byte [readLength + 1]);
+
+                               using (var file = new FileStream (path, FileMode.Open, FileAccess.ReadWrite, FileShare.Read,
+                                                                                                bufferSize, FileOptions.SequentialScan))
+                               {
+                                       file.Read (new byte [readLength], 0, readLength);
+                                       file.Write (new byte [writeLength], 0, writeLength);
+
+                                       Assert.AreEqual (readLength + writeLength, file.Length);
+                               }
+                       } finally {
+                               DeleteFile (path);
+                       }
+               }
        }
 }