1 // FileStreamTests.cs - NUnit2 Test Cases for System.IO.FileStream class
4 // Ville Palo (vi64pa@koti.soon.fi)
5 // Gert Driesen (gert.driesen@ardatis.com)
6 // Gonzalo Paniagua Javier (gonzalo@ximian.com)
9 // (c) 2003 Ximian, Inc. (http://www.ximian.com)
10 // Copyright 2011 Xamarin Inc (http://www.xamarin.com).
13 using NUnit.Framework;
16 using System.Runtime.InteropServices;
18 using System.Threading;
19 using Microsoft.Win32.SafeHandles;
21 namespace MonoTests.System.IO
24 public class FileStreamTest
26 string TempFolder = Path.Combine (Path.GetTempPath (), "MonoTests.System.IO.Tests");
27 static readonly char DSC = Path.DirectorySeparatorChar;
28 static bool MacOSX = false;
31 static extern int uname (IntPtr buf);
34 public void TearDown ()
36 if (Directory.Exists (TempFolder))
37 Directory.Delete (TempFolder, true);
43 if (Directory.Exists (TempFolder))
44 Directory.Delete (TempFolder, true);
46 Directory.CreateDirectory (TempFolder);
49 IntPtr buf = Marshal.AllocHGlobal (8192);
51 MacOSX = Marshal.PtrToStringAnsi (buf) == "Darwin";
52 Marshal.FreeHGlobal (buf);
56 public void TestCtr ()
58 string path = TempFolder + DSC + "testfilestream.tmp.1";
60 FileStream stream = null;
62 stream = new FileStream (path, FileMode.Create);
72 [ExpectedException (typeof (ArgumentException))]
73 public void CtorArgumentException1 ()
76 stream = new FileStream ("", FileMode.Create);
81 [ExpectedException (typeof (ArgumentNullException))]
82 public void CtorArgumentNullException ()
84 FileStream stream = new FileStream (null, FileMode.Create);
89 public void CtorFileNotFoundException_Mode_Open ()
91 const string file_name = "thisfileshouldnotexist.test";
93 string path = TempFolder + DSC + file_name;
95 FileStream stream = null;
97 stream = new FileStream (TempFolder + DSC + file_name, FileMode.Open);
99 } catch (FileNotFoundException ex) {
100 Assert.AreEqual (typeof (FileNotFoundException), ex.GetType (), "#A2");
101 Assert.AreEqual (path, ex.FileName, "#A3");
102 Assert.IsNull (ex.InnerException, "#A4");
103 Assert.IsNotNull (ex.Message, "#A5");
104 Assert.IsTrue (ex.Message.IndexOf (path) != -1, "#A6");
106 if (stream != null) {
114 string orignalCurrentDir = Directory.GetCurrentDirectory ();
115 Directory.SetCurrentDirectory (TempFolder);
117 // If TempFolder is a symlink, Mono will follow it and ex.FileName below will contain
118 // the real directory name, not that of the TempFolder symlink and the test will fail
119 // (happens e.g. on Android M)
120 string realTempDir = Directory.GetCurrentDirectory ();
121 path = realTempDir + DSC + file_name;
124 stream = new FileStream (file_name, FileMode.Open);
126 } catch (FileNotFoundException ex) {
127 Assert.AreEqual (typeof (FileNotFoundException), ex.GetType (), "#B2");
128 Assert.AreEqual (path, ex.FileName, "#B3");
129 Assert.IsNull (ex.InnerException, "#B4");
130 Assert.IsNotNull (ex.Message, "#B5");
131 Assert.IsTrue (ex.Message.IndexOf (path) != -1, "#B6");
133 // restore original current directory
134 Directory.SetCurrentDirectory (orignalCurrentDir);
142 public void CtorFileNotFoundException_Mode_Truncate ()
144 const string file_name = "thisfileshouldNOTexist.test";
146 string path = TempFolder + DSC + file_name;
148 FileStream stream = null;
150 stream = new FileStream (path, FileMode.Truncate);
152 } catch (FileNotFoundException ex) {
153 Assert.AreEqual (typeof (FileNotFoundException), ex.GetType (), "#A2");
154 Assert.AreEqual (path, ex.FileName, "#A3");
155 Assert.IsNull (ex.InnerException, "#A4");
156 Assert.IsNotNull (ex.Message, "#A5");
157 Assert.IsTrue (ex.Message.IndexOf (path) != -1, "#A6");
159 if (stream != null) {
167 string orignalCurrentDir = Directory.GetCurrentDirectory ();
168 Directory.SetCurrentDirectory (TempFolder);
170 // If TempFolder is a symlink, Mono will follow it and ex.FileName below will contain
171 // the real directory name, not that of the TempFolder symlink and the test will fail
172 // (happens e.g. on Android M)
173 string realTempDir = Directory.GetCurrentDirectory ();
174 path = realTempDir + DSC + file_name;
177 stream = new FileStream (file_name, FileMode.Truncate);
179 } catch (FileNotFoundException ex) {
180 Assert.AreEqual (typeof (FileNotFoundException), ex.GetType (), "#B2");
181 Assert.AreEqual (path, ex.FileName, "#B3");
182 Assert.IsNull (ex.InnerException, "#B4");
183 Assert.IsNotNull (ex.Message, "#B5");
184 Assert.IsTrue (ex.Message.IndexOf (path) != -1, "#B6");
186 // restore original current directory
187 Directory.SetCurrentDirectory (orignalCurrentDir);
195 public void CtorIOException1 ()
197 const string file_name = "thisfileshouldexists.test";
199 string path = TempFolder + DSC + file_name;
200 FileStream stream = null;
203 stream = new FileStream (path, FileMode.CreateNew);
206 stream = new FileStream (path, FileMode.CreateNew);
208 } catch (IOException ex) {
209 Assert.AreEqual (typeof (IOException), ex.GetType (), "#A2");
210 Assert.IsNull (ex.InnerException, "#A3");
211 Assert.IsNotNull (ex.Message, "#A4");
212 Assert.IsTrue (ex.Message.IndexOf (path) != -1, "#A5");
214 if (stream != null) {
222 string orignalCurrentDir = Directory.GetCurrentDirectory ();
223 Directory.SetCurrentDirectory (TempFolder);
225 // If TempFolder is a symlink, Mono will follow it and ex.FileName below will contain
226 // the real directory name, not that of the TempFolder symlink and the test will fail
227 // (happens e.g. on Android M)
228 string realTempDir = Directory.GetCurrentDirectory ();
229 path = realTempDir + DSC + file_name;
232 stream = new FileStream (file_name, FileMode.CreateNew);
235 stream = new FileStream (file_name, FileMode.CreateNew);
237 } catch (IOException ex) {
238 Assert.AreEqual (typeof (IOException), ex.GetType (), "#B2");
239 Assert.IsNull (ex.InnerException, "#B3");
240 Assert.IsNotNull (ex.Message, "#B4");
241 Assert.IsTrue (ex.Message.IndexOf (path) != -1, "#B5");
243 // restore original current directory
244 Directory.SetCurrentDirectory (orignalCurrentDir);
252 [ExpectedException (typeof (ArgumentOutOfRangeException))]
253 public void CtorArgumentOutOfRangeException1 ()
255 FileStream stream = null;
256 string path = TempFolder + Path.DirectorySeparatorChar + "temp";
259 stream = new FileStream (path, FileMode.Append | FileMode.CreateNew);
268 [ExpectedException (typeof (ArgumentOutOfRangeException))]
269 public void CtorArgumentOutOfRangeException2 ()
271 FileStream stream = null;
272 string path = TempFolder + Path.DirectorySeparatorChar + "temp";
275 stream = new FileStream ("test.test.test", FileMode.Append | FileMode.Open);
283 private void CtorDirectoryNotFoundException (FileMode mode)
285 string path = TempFolder + DSC + "thisDirectoryShouldNotExists";
286 if (Directory.Exists (path))
287 Directory.Delete (path, true);
289 FileStream stream = null;
291 stream = new FileStream (path + DSC + "eitherthisfile.test", mode);
297 if (Directory.Exists (path))
298 Directory.Delete (path, true);
303 [ExpectedException (typeof (DirectoryNotFoundException))]
304 public void CtorDirectoryNotFoundException_CreateNew ()
306 CtorDirectoryNotFoundException (FileMode.CreateNew);
310 [ExpectedException (typeof (DirectoryNotFoundException))]
311 public void CtorDirectoryNotFoundException_Create ()
313 CtorDirectoryNotFoundException (FileMode.Create);
317 [ExpectedException (typeof (DirectoryNotFoundException))]
318 public void CtorDirectoryNotFoundException_Open ()
320 CtorDirectoryNotFoundException (FileMode.Open);
324 [ExpectedException (typeof (DirectoryNotFoundException))]
325 public void CtorDirectoryNotFoundException_OpenOrCreate ()
327 CtorDirectoryNotFoundException (FileMode.OpenOrCreate);
331 [ExpectedException (typeof (DirectoryNotFoundException))]
332 public void CtorDirectoryNotFoundException_Truncate ()
334 CtorDirectoryNotFoundException (FileMode.Truncate);
338 [ExpectedException (typeof (DirectoryNotFoundException))]
339 public void CtorDirectoryNotFoundException_Append ()
341 CtorDirectoryNotFoundException (FileMode.Append);
345 public void CtorDirectoryNotFound_RelativePath ()
347 string orignalCurrentDir = Directory.GetCurrentDirectory ();
348 Directory.SetCurrentDirectory (TempFolder);
350 // If TempFolder is a symlink, Mono will follow it and ex.FileName below will contain
351 // the real directory name, not that of the TempFolder symlink and the test will fail
352 // (happens e.g. on Android M)
353 string realTempDir = Directory.GetCurrentDirectory ();
355 string relativePath = "DirectoryDoesNotExist" + Path.DirectorySeparatorChar + "file.txt";
356 string fullPath = Path.Combine (realTempDir, relativePath);
358 new FileStream (relativePath, FileMode.Open);
360 } catch (DirectoryNotFoundException ex) {
361 Assert.AreEqual (typeof (DirectoryNotFoundException), ex.GetType (), "#A2");
362 Assert.IsNull (ex.InnerException, "#A3");
363 Assert.IsNotNull (ex.Message, "#A4");
364 Assert.IsTrue (ex.Message.IndexOf (fullPath) != -1, "#A5");
366 // restore original current directory
367 Directory.SetCurrentDirectory (orignalCurrentDir);
372 // FileShare.Inheritable is ignored, but file does not exist
373 [ExpectedException (typeof (FileNotFoundException))]
374 public void CtorArgumentOutOfRangeException3 ()
376 string path = TempFolder + DSC + "CtorArgumentOutOfRangeException1";
379 FileStream stream = null;
381 stream = new FileStream (path, FileMode.Open, FileAccess.Read, FileShare.Inheritable);
390 [ExpectedException (typeof (ArgumentOutOfRangeException))]
391 public void CtorArgumentOutOfRangeException4 ()
393 string path = TempFolder + DSC + "CtorArgumentOutOfRangeException4";
396 FileStream stream = null;
398 stream = new FileStream (path, FileMode.OpenOrCreate, FileAccess.Read, FileShare.ReadWrite, -1);
407 [ExpectedException (typeof (ArgumentOutOfRangeException))]
408 public void CtorBufferSizeZero ()
410 // Buffer size can't be zero
412 string path = Path.Combine (TempFolder, "CtorBufferSizeZero");
415 FileStream stream = null;
417 stream = new FileStream (path, FileMode.CreateNew, FileAccess.Write, FileShare.ReadWrite, 0);
426 [ExpectedException (typeof (ArgumentException))]
427 public void CtorArgumentException2 ()
429 // FileMode.CreateNew && FileAccess.Read
431 string path = TempFolder + Path.DirectorySeparatorChar + "temp";
432 FileStream stream = null;
437 stream = new FileStream (".test.test.test.2", FileMode.CreateNew, FileAccess.Read, FileShare.None | FileShare.Write);
448 // FileShare.Inheritable is ignored, but file does not exist
449 [ExpectedException (typeof (FileNotFoundException))]
450 public void CtorArgumentOutOfRangeException5 ()
452 string path = TempFolder + Path.DirectorySeparatorChar + "temp";
455 FileStream stream = null;
457 stream = new FileStream (path, FileMode.Open, FileAccess.Read, FileShare.Inheritable | FileShare.ReadWrite);
467 [ExpectedException (typeof (ArgumentException))]
468 public void CtorArgumentException3 ()
470 string path = TempFolder + Path.DirectorySeparatorChar + "temp";
471 FileStream stream = null;
476 stream = new FileStream (".test.test.test.2", FileMode.Truncate, FileAccess.Read);
486 public void ModeAndAccessCombinations ()
488 string path = TempFolder + Path.DirectorySeparatorChar + "temp";
490 FileStream stream = null;
494 // Append access can be requested only in write-only mode
495 stream = new FileStream (path, FileMode.Append, FileAccess.Read);
497 } catch (ArgumentException ex) {
498 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#A2");
506 // Append / ReadWrite
508 // Append access can be requested only in write-only mode
509 stream = new FileStream (path, FileMode.Append, FileAccess.ReadWrite);
511 } catch (ArgumentException ex) {
512 // make sure it is exact this exception, and not a derived type
513 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#B2");
523 stream = new FileStream (path, FileMode.Append, FileAccess.Write);
533 stream = new FileStream (path, FileMode.Create, FileAccess.Read);
535 } catch (ArgumentException ex) {
536 // make sure it is exact this exception, and not a derived type
537 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#C2");
545 // Create / ReadWrite
547 stream = new FileStream (path, FileMode.Create, FileAccess.ReadWrite);
557 stream = new FileStream (path, FileMode.Create, FileAccess.Write);
567 stream = new FileStream (path, FileMode.CreateNew, FileAccess.Read);
569 } catch (ArgumentException ex) {
570 // make sure it is exact this exception, and not a derived type
571 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#D2");
579 // CreateNew / ReadWrite
581 stream = new FileStream (path, FileMode.CreateNew, FileAccess.ReadWrite);
591 stream = new FileStream (path, FileMode.CreateNew, FileAccess.Write);
601 stream = new FileStream (path, FileMode.Open, FileAccess.Read);
603 } catch (FileNotFoundException ex) {
604 // make sure it is exact this exception, and not a derived type
605 Assert.AreEqual (typeof (FileNotFoundException), ex.GetType (), "#E2");
606 Assert.AreEqual (path, ex.FileName, "#E3");
607 Assert.IsNull (ex.InnerException, "#E4");
608 Assert.IsNotNull (ex.Message, "#E5");
609 Assert.IsTrue (ex.Message.IndexOf (path) != -1, "#E6");
619 stream = new FileStream (path, FileMode.Open, FileAccess.ReadWrite);
621 } catch (FileNotFoundException ex) {
622 // make sure it is exact this exception, and not a derived type
623 Assert.AreEqual (typeof (FileNotFoundException), ex.GetType (), "#F2");
624 Assert.AreEqual (path, ex.FileName, "#F3");
625 Assert.IsNull (ex.InnerException, "#F4");
626 Assert.IsNotNull (ex.Message, "#F5");
627 Assert.IsTrue (ex.Message.IndexOf (path) != -1, "#F6");
637 stream = new FileStream (path, FileMode.Open, FileAccess.Write);
639 } catch (FileNotFoundException ex) {
640 // make sure it is exact this exception, and not a derived type
641 Assert.AreEqual (typeof (FileNotFoundException), ex.GetType (), "#G2");
642 Assert.AreEqual (path, ex.FileName, "#G3");
643 Assert.IsNull (ex.InnerException, "#G4");
644 Assert.IsNotNull (ex.Message, "#G5");
645 Assert.IsTrue (ex.Message.IndexOf (path) != -1, "#G6");
653 // OpenOrCreate / Read
655 stream = new FileStream (path, FileMode.OpenOrCreate, FileAccess.Read);
663 // OpenOrCreate / ReadWrite
665 stream = new FileStream (path, FileMode.OpenOrCreate, FileAccess.ReadWrite);
673 // OpenOrCreate / Write
675 stream = new FileStream (path, FileMode.OpenOrCreate, FileAccess.Write);
685 stream = new FileStream (path, FileMode.Truncate, FileAccess.Read);
687 } catch (ArgumentException ex) {
688 // make sure it is exact this exception, and not a derived type
689 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#H2");
697 // Truncate / ReadWrite
699 stream = new FileStream (path, FileMode.Truncate, FileAccess.ReadWrite);
701 } catch (FileNotFoundException ex) {
702 // make sure it is exact this exception, and not a derived type
703 Assert.AreEqual (typeof (FileNotFoundException), ex.GetType (), "#I2");
704 Assert.AreEqual (path, ex.FileName, "#I3");
705 Assert.IsNull (ex.InnerException, "#I4");
706 Assert.IsNotNull (ex.Message, "#I5");
707 Assert.IsTrue (ex.Message.IndexOf (path) != -1, "#I6");
717 stream = new FileStream (path, FileMode.Truncate, FileAccess.Write);
719 } catch (FileNotFoundException ex) {
720 // make sure it is exact this exception, and not a derived type
721 Assert.AreEqual (typeof (FileNotFoundException), ex.GetType (), "#J2");
722 Assert.AreEqual (path, ex.FileName, "#J3");
723 Assert.IsNull (ex.InnerException, "#J4");
724 Assert.IsNotNull (ex.Message, "#J5");
725 Assert.IsTrue (ex.Message.IndexOf (path) != -1, "#J6");
734 [Test, ExpectedException (typeof (IOException))]
735 public void CtorIOException2 ()
737 FileStream stream = null;
739 stream = new FileStream (new IntPtr (Int32.MaxValue), FileAccess.Read);
746 [Test, ExpectedException (typeof (IOException))]
747 public void CtorIOException ()
749 string path = TempFolder + DSC + "CTorIOException.Test";
750 FileStream stream = null;
751 FileStream stream2 = null;
755 stream = new FileStream (path, FileMode.CreateNew);
757 // used by an another process
758 stream2 = new FileStream (path, FileMode.OpenOrCreate);
769 public void CtorAccess1Read2Read ()
771 FileStream fs = null;
772 FileStream fs2 = null;
773 string fn = Path.Combine (TempFolder, "temp");
775 if (!File.Exists (fn)) {
776 TextWriter tw = File.CreateText (fn);
780 fs = new FileStream (fn, FileMode.Open, FileAccess.Read);
781 fs2 = new FileStream (fn, FileMode.Open, FileAccess.Read);
787 if (File.Exists (fn))
793 [ExpectedException (typeof (IOException))]
794 public void CtorAccess1Read2Write ()
796 string fn = Path.Combine (TempFolder, "temp");
797 FileStream fs1 = null;
798 FileStream fs2 = null;
800 if (!File.Exists (fn)) {
801 using (TextWriter tw = File.CreateText (fn)) {
805 fs1 = new FileStream (fn, FileMode.Open, FileAccess.Read);
806 fs2 = new FileStream (fn, FileMode.Create, FileAccess.Write);
812 if (File.Exists (fn))
818 [ExpectedException (typeof (IOException))]
819 public void CtorAccess1Write2Write ()
821 string fn = Path.Combine (TempFolder, "temp");
822 FileStream fs1 = null;
823 FileStream fs2 = null;
825 if (File.Exists (fn))
827 fs1 = new FileStream (fn, FileMode.Create, FileAccess.Write);
828 fs2 = new FileStream (fn, FileMode.Create, FileAccess.Write);
834 if (File.Exists (fn))
840 [ExpectedException (typeof (UnauthorizedAccessException))]
841 public void CtorReadDirectoryAsFile ()
843 FileStream stream = null;
845 stream = new FileStream (TempFolder, FileMode.Open, FileAccess.Read);
853 public void FileShare_Delete ()
855 string fn = Path.Combine (TempFolder, "temp");
857 using (Stream s = new FileStream (fn, FileMode.OpenOrCreate, FileAccess.Write, FileShare.Delete)) {
858 s.Write (new byte [1] { 0x5 }, 0, 1);
862 using (Stream s = new FileStream (fn, FileMode.OpenOrCreate, FileAccess.Write, FileShare.ReadWrite | FileShare.Delete)) {
863 s.Write (new byte [1] { 0x5 }, 0, 1);
867 using (Stream s = new FileStream (fn, FileMode.OpenOrCreate, FileAccess.Write, FileShare.Write | FileShare.Delete)) {
868 s.Write (new byte [1] { 0x5 }, 0, 1);
872 using (Stream s = new FileStream (fn, FileMode.OpenOrCreate, FileAccess.Write, FileShare.Read | FileShare.Delete)) {
873 s.Write (new byte [1] { 0x5 }, 0, 1);
877 using (Stream s = new FileStream (fn, FileMode.OpenOrCreate, FileAccess.Write, FileShare.Inheritable | FileShare.Delete)) {
878 s.Write (new byte [1] { 0x5 }, 0, 1);
886 string path = TempFolder + DSC + "FileStreamTest.Write";
890 FileStream stream = new FileStream (path, FileMode.CreateNew, FileAccess.ReadWrite, FileShare.ReadWrite, 8);
892 byte[] outbytes = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16 };
893 byte[] bytes = new byte[15];
895 // Check that the data is flushed when we overflow the buffer
896 // with a large amount of data
897 stream.Write (outbytes, 0, 5);
898 stream.Write (outbytes, 5, 10);
899 stream.Seek (0, SeekOrigin.Begin);
901 stream.Read (bytes, 0, 15);
902 for (int i = 0; i < 15; ++i)
903 Assert.AreEqual (i + 1, bytes[i], "#1");
905 // Check that the data is flushed when we overflow the buffer
906 // with a small amount of data
907 stream.Write (outbytes, 0, 7);
908 stream.Write (outbytes, 7, 7);
909 stream.Write (outbytes, 14, 1);
911 stream.Read (bytes, 0, 15);
912 stream.Seek (15, SeekOrigin.Begin);
913 for (int i = 0; i < 15; ++i)
914 Assert.AreEqual (i + 1, bytes[i], "#2");
919 public void Length ()
921 // Test that the Length property takes into account the data
923 string path = TempFolder + DSC + "FileStreamTest.Length";
927 FileStream stream = new FileStream (path, FileMode.CreateNew);
929 byte[] outbytes = new byte[] { 1, 2, 3, 4 };
931 stream.Write (outbytes, 0, 4);
932 Assert.AreEqual (4, stream.Length);
939 string path = TempFolder + DSC + "FileStreamTest.Flush";
940 FileStream stream = null;
941 FileStream stream2 = null;
946 stream = new FileStream (path, FileMode.CreateNew, FileAccess.ReadWrite, FileShare.ReadWrite);
947 stream2 = new FileStream (path, FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite);
949 stream.Write (new byte[] { 1, 2, 3, 4, 5 }, 0, 5);
951 byte[] bytes = new byte[5];
952 stream2.Read (bytes, 0, 5);
953 Assert.AreEqual (0, bytes[0], "#A1");
954 Assert.AreEqual (0, bytes[1], "#A2");
955 Assert.AreEqual (0, bytes[2], "#A3");
956 Assert.AreEqual (0, bytes[3], "#A4");
959 stream2.Read (bytes, 0, 5);
960 Assert.AreEqual (1, bytes[0], "#B1");
961 Assert.AreEqual (2, bytes[1], "#B2");
962 Assert.AreEqual (3, bytes[2], "#B3");
963 Assert.AreEqual (4, bytes[3], "#B4");
974 public void TestDefaultProperties ()
976 string path = TempFolder + Path.DirectorySeparatorChar + "testfilestream.tmp.2";
979 FileStream stream = new FileStream (path, FileMode.Create);
981 Assert.IsTrue (stream.CanRead, "#A1");
982 Assert.IsTrue (stream.CanSeek, "#A2");
983 Assert.IsTrue (stream.CanWrite, "#A3");
984 Assert.IsFalse (stream.IsAsync, "#A4");
985 Assert.IsTrue (stream.Name.EndsWith (path), "#A5");
986 Assert.AreEqual (0, stream.Position, "#A6");
987 Assert.AreEqual ("System.IO.FileStream", stream.ToString (), "#A7");
991 stream = new FileStream (path, FileMode.OpenOrCreate, FileAccess.Read);
992 Assert.IsTrue (stream.CanRead, "#B1");
993 Assert.IsTrue (stream.CanSeek, "#B2");
994 Assert.IsFalse (stream.CanWrite, "#B3");
995 Assert.IsFalse (stream.IsAsync, "#B4");
996 Assert.IsTrue (stream.Name.EndsWith (path), "#B5");
997 Assert.AreEqual (0, stream.Position, "#B6");
998 Assert.AreEqual ("System.IO.FileStream", stream.ToString (), "#B7");
1001 stream = new FileStream (path, FileMode.Truncate, FileAccess.Write, FileShare.ReadWrite);
1002 Assert.IsFalse (stream.CanRead, "#C1");
1003 Assert.IsTrue (stream.CanSeek, "#C2");
1004 Assert.IsTrue (stream.CanWrite, "#C3");
1005 Assert.IsFalse (stream.IsAsync, "#C4");
1006 Assert.IsTrue (stream.Name.EndsWith ("testfilestream.tmp.2"), "#C5");
1007 Assert.AreEqual (0, stream.Position, "#C6");
1008 Assert.AreEqual ("System.IO.FileStream", stream.ToString (), "#C7");
1013 [Category ("NotWorking")]
1014 // Bug: 71371 -> duplicate and WONTFIX.
1015 public void TestLock_FailsOnMono ()
1017 string path = TempFolder + Path.DirectorySeparatorChar + "TestLock";
1020 FileStream stream = new FileStream (path, FileMode.CreateNew, FileAccess.ReadWrite);
1022 stream.Write (new Byte[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }, 0, 10);
1025 stream = new FileStream (path, FileMode.Open, FileAccess.ReadWrite);
1029 FileStream stream2 = new FileStream (path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
1031 byte[] bytes = new byte[5];
1033 stream2.Read (bytes, 0, 5);
1035 } catch (Exception e) {
1036 Assert.AreEqual (typeof (IOException), e.GetType (), "#2");
1045 public void TestLock ()
1047 string path = TempFolder + Path.DirectorySeparatorChar + "TestLock";
1050 FileStream stream = new FileStream (path, FileMode.CreateNew, FileAccess.ReadWrite);
1052 stream.Write (new Byte[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }, 0, 10);
1055 stream = new FileStream (path, FileMode.Open, FileAccess.ReadWrite);
1059 FileStream stream2 = new FileStream (path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
1061 byte[] bytes = new byte[5];
1063 stream2.Read (bytes, 0, 5);
1064 Assert.Fail ("#A1");
1065 } catch (Exception) {
1066 // Bug #71371: on MS.NET you get an IOException detailing a lock
1067 // Assert.AreEqual (typeof (IOException), e.GetType (), "#A2");
1070 stream2.Seek (5, SeekOrigin.Begin);
1071 stream2.Read (bytes, 0, 5);
1073 Assert.AreEqual (5, bytes[0], "#B1");
1074 Assert.AreEqual (6, bytes[1], "#B2");
1075 Assert.AreEqual (7, bytes[2], "#B3");
1076 Assert.AreEqual (8, bytes[3], "#B4");
1077 Assert.AreEqual (9, bytes[4], "#B5");
1079 stream.Unlock (0, 5);
1080 stream2.Seek (0, SeekOrigin.Begin);
1081 stream2.Read (bytes, 0, 5);
1083 Assert.AreEqual (0, bytes[0], "#C1");
1084 Assert.AreEqual (1, bytes[1], "#C2");
1085 Assert.AreEqual (2, bytes[2], "#C3");
1086 Assert.AreEqual (3, bytes[3], "#C4");
1087 Assert.AreEqual (4, bytes[4], "#C5");
1098 string path = TempFolder + DSC + "FST.Seek.Test";
1101 FileStream stream = new FileStream (path, FileMode.CreateNew, FileAccess.ReadWrite, FileShare.ReadWrite);
1102 FileStream stream2 = new FileStream (path, FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite);
1104 stream.Write (new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 10 }, 0, 9);
1105 Assert.AreEqual (5, stream2.Seek (5, SeekOrigin.Begin), "#1");
1106 Assert.AreEqual (-1, stream2.ReadByte (), "#2");
1108 Assert.AreEqual (2, stream2.Seek (-3, SeekOrigin.Current), "#3");
1109 Assert.AreEqual (-1, stream2.ReadByte (), "#4");
1111 Assert.AreEqual (12, stream.Seek (3, SeekOrigin.Current), "#5");
1112 Assert.AreEqual (-1, stream.ReadByte (), "#6");
1114 Assert.AreEqual (5, stream.Seek (-7, SeekOrigin.Current), "#7");
1115 Assert.AreEqual (6, stream.ReadByte (), "#8");
1117 Assert.AreEqual (5, stream2.Seek (5, SeekOrigin.Begin), "#9");
1118 Assert.AreEqual (6, stream2.ReadByte (), "#10");
1126 public void TestSeek ()
1128 string path = TempFolder + Path.DirectorySeparatorChar + "TestSeek";
1131 FileStream stream = new FileStream (path, FileMode.CreateNew, FileAccess.ReadWrite, FileShare.ReadWrite);
1132 stream.Write (new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }, 0, 10);
1134 stream.Seek (5, SeekOrigin.End);
1135 Assert.AreEqual (-1, stream.ReadByte (), "#1");
1137 stream.Seek (-5, SeekOrigin.End);
1138 Assert.AreEqual (6, stream.ReadByte (), "#2");
1141 stream.Seek (-11, SeekOrigin.End);
1143 } catch (Exception e) {
1144 Assert.AreEqual (typeof (IOException), e.GetType (), "#4");
1147 stream.Seek (19, SeekOrigin.Begin);
1148 Assert.AreEqual (-1, stream.ReadByte (), "#5");
1150 stream.Seek (1, SeekOrigin.Begin);
1151 Assert.AreEqual (2, stream.ReadByte (), "#6");
1153 stream.Seek (3, SeekOrigin.Current);
1154 Assert.AreEqual (6, stream.ReadByte (), "#7");
1156 stream.Seek (-2, SeekOrigin.Current);
1157 Assert.AreEqual (5, stream.ReadByte (), "#8");
1161 // Test that seeks work correctly when seeking inside the buffer
1162 stream.Seek (0, SeekOrigin.Begin);
1163 stream.WriteByte (0);
1164 stream.WriteByte (1);
1165 stream.Seek (0, SeekOrigin.Begin);
1166 byte[] buf = new byte[1];
1168 stream.Write (buf, 0, 1);
1169 stream.Write (buf, 0, 1);
1171 stream.Seek (0, SeekOrigin.Begin);
1172 Assert.AreEqual (2, stream.ReadByte (), "#9");
1173 Assert.AreEqual (2, stream.ReadByte (), "#10");
1180 public void TestClose ()
1182 string path = TempFolder + Path.DirectorySeparatorChar + "TestClose";
1185 FileStream stream = new FileStream (path, FileMode.CreateNew, FileAccess.ReadWrite);
1187 stream.Write (new byte[] { 1, 2, 3, 4 }, 0, 4);
1193 Assert.Fail ("#A1");
1194 } catch (Exception e) {
1195 Assert.AreEqual (typeof (ObjectDisposedException), e.GetType (), "#A2");
1199 stream.WriteByte (64);
1200 Assert.Fail ("#B1");
1201 } catch (Exception e) {
1202 Assert.AreEqual (typeof (ObjectDisposedException), e.GetType (), "#B2");
1207 Assert.Fail ("#C1");
1208 } catch (Exception e) {
1209 Assert.AreEqual (typeof (ObjectDisposedException), e.GetType (), "#C2");
1213 long l = stream.Length;
1214 Assert.Fail ("#D1");
1215 } catch (Exception e) {
1216 Assert.AreEqual (typeof (ObjectDisposedException), e.GetType (), "#D2");
1220 long l = stream.Position;
1221 Assert.Fail ("#E1");
1222 } catch (Exception e) {
1223 Assert.AreEqual (typeof (ObjectDisposedException), e.GetType (), "#E2");
1226 Assert.IsFalse (stream.CanRead, "#F1");
1227 Assert.IsFalse (stream.CanSeek, "#F2");
1228 Assert.IsFalse (stream.CanWrite, "#F3");
1229 Assert.IsTrue (stream.Name.EndsWith (path), "#F4");
1236 /// Checks whether the <see cref="FileStream" /> throws a <see cref="NotSupportedException" />
1237 /// when the stream is opened with access mode <see cref="FileAccess.Read" /> and the
1238 /// <see cref="FileStream.Write(byte[], int, int)" /> method is called.
1241 [ExpectedException (typeof (NotSupportedException))]
1242 public void TestWriteVerifyAccessMode ()
1244 string path = TempFolder + Path.DirectorySeparatorChar + "temp";
1247 FileStream stream = null;
1251 buffer = Encoding.ASCII.GetBytes ("test");
1252 stream = new FileStream (path, FileMode.OpenOrCreate, FileAccess.Read);
1253 stream.Write (buffer, 0, buffer.Length);
1262 /// Checks whether the <see cref="FileStream" /> throws a <see cref="NotSupportedException" />
1263 /// when the stream is opened with access mode <see cref="FileAccess.Read" /> and the
1264 /// <see cref="FileStream.WriteByte(byte)" /> method is called.
1267 [ExpectedException (typeof (NotSupportedException))]
1268 public void TestWriteByteVerifyAccessMode ()
1270 string path = TempFolder + Path.DirectorySeparatorChar + "temp";
1273 FileStream stream = null;
1276 stream = new FileStream (path, FileMode.OpenOrCreate, FileAccess.Read);
1277 stream.WriteByte (Byte.MinValue);
1286 /// Checks whether the <see cref="FileStream" /> throws a <see cref="NotSupportedException" />
1287 /// when the stream is opened with access mode <see cref="FileAccess.Write" /> and the
1288 /// <see cref="FileStream.Read(byte[], int, int)" /> method is called.
1291 [ExpectedException (typeof (NotSupportedException))]
1292 public void TestReadVerifyAccessMode ()
1294 string path = TempFolder + Path.DirectorySeparatorChar + "temp";
1297 FileStream stream = null;
1298 byte[] buffer = new byte[100];
1301 stream = new FileStream (path, FileMode.OpenOrCreate, FileAccess.Write);
1302 stream.Read (buffer, 0, buffer.Length);
1310 /// Checks whether the <see cref="FileStream" /> throws a <see cref="NotSupportedException" />
1311 /// when the stream is opened with access mode <see cref="FileAccess.Write" /> and the
1312 /// <see cref="FileStream.ReadByte()" /> method is called.
1315 [ExpectedException (typeof (NotSupportedException))]
1316 public void TestReadByteVerifyAccessMode ()
1318 string path = TempFolder + Path.DirectorySeparatorChar + "temp";
1321 FileStream stream = null;
1324 stream = new FileStream (path, FileMode.OpenOrCreate, FileAccess.Write);
1325 int readByte = stream.ReadByte ();
1333 // Check that the stream is flushed even when it doesn't own the
1336 public void TestFlushNotOwningHandle ()
1338 string path = Path.Combine (TempFolder, "TestFlushNotOwningHandle");
1341 FileStream s = new FileStream (path, FileMode.Create);
1342 using (FileStream s2 = new FileStream (s.Handle, FileAccess.Write, false)) {
1343 byte[] buf = new byte[2];
1345 s2.Write (buf, 0, 1);
1349 Assert.AreEqual ((int) '1', s.ReadByte ());
1353 private void DeleteFile (string path)
1355 if (File.Exists (path))
1360 [ExpectedException (typeof (ArgumentOutOfRangeException))]
1361 public void Read_OffsetNegative ()
1363 string path = TempFolder + Path.DirectorySeparatorChar + "temp";
1366 using (FileStream stream = new FileStream (path, FileMode.OpenOrCreate, FileAccess.Read)) {
1367 stream.Read (new byte[0], -1, 1);
1372 [ExpectedException (typeof (ArgumentException))]
1373 public void Read_OffsetOverflow ()
1375 string path = TempFolder + Path.DirectorySeparatorChar + "temp";
1378 using (FileStream stream = new FileStream (path, FileMode.OpenOrCreate, FileAccess.Read)) {
1379 stream.Read (new byte[0], Int32.MaxValue, 1);
1384 [ExpectedException (typeof (ArgumentOutOfRangeException))]
1385 public void Read_CountNegative ()
1387 string path = TempFolder + Path.DirectorySeparatorChar + "temp";
1390 using (FileStream stream = new FileStream (path, FileMode.OpenOrCreate, FileAccess.Read)) {
1391 stream.Read (new byte[0], 1, -1);
1396 [ExpectedException (typeof (ArgumentException))]
1397 public void Read_CountOverflow ()
1399 string path = TempFolder + Path.DirectorySeparatorChar + "temp";
1402 using (FileStream stream = new FileStream (path, FileMode.OpenOrCreate, FileAccess.Read)) {
1403 stream.Read (new byte[0], 1, Int32.MaxValue);
1408 [ExpectedException (typeof (ArgumentOutOfRangeException))]
1409 public void Write_OffsetNegative ()
1411 string path = TempFolder + Path.DirectorySeparatorChar + "temp";
1414 using (FileStream stream = new FileStream (path, FileMode.OpenOrCreate, FileAccess.Write)) {
1415 stream.Write (new byte[0], -1, 1);
1420 [ExpectedException (typeof (ArgumentException))]
1421 public void Write_OffsetOverflow ()
1423 string path = TempFolder + Path.DirectorySeparatorChar + "temp";
1426 using (FileStream stream = new FileStream (path, FileMode.OpenOrCreate, FileAccess.Write)) {
1427 stream.Write (new byte[0], Int32.MaxValue, 1);
1432 [ExpectedException (typeof (ArgumentOutOfRangeException))]
1433 public void Write_CountNegative ()
1435 string path = TempFolder + Path.DirectorySeparatorChar + "temp";
1438 using (FileStream stream = new FileStream (path, FileMode.OpenOrCreate, FileAccess.Write)) {
1439 stream.Write (new byte[0], 1, -1);
1444 [ExpectedException (typeof (ArgumentException))]
1445 public void Write_CountOverflow ()
1447 string path = TempFolder + Path.DirectorySeparatorChar + "temp";
1450 using (FileStream stream = new FileStream (path, FileMode.OpenOrCreate, FileAccess.Write)) {
1451 stream.Write (new byte[0], 1, Int32.MaxValue);
1456 [ExpectedException (typeof (ArgumentException))]
1457 public void Seek_InvalidSeekOrigin ()
1459 string path = TempFolder + Path.DirectorySeparatorChar + "temp";
1462 using (FileStream stream = new FileStream (path, FileMode.OpenOrCreate, FileAccess.Read)) {
1463 stream.Seek (0, (SeekOrigin) (-1));
1468 [ExpectedException (typeof (ArgumentException))]
1469 public void Constructor_InvalidFileHandle ()
1471 new FileStream ((IntPtr) (-1L), FileAccess.Read);
1475 public void PositionAfterSetLength ()
1477 string path = TempFolder + Path.DirectorySeparatorChar + "temp";
1480 using (FileStream stream = new FileStream (path, FileMode.OpenOrCreate, FileAccess.Write)) {
1481 stream.SetLength (32);
1482 stream.Position = 32;
1483 stream.SetLength (16);
1484 Assert.AreEqual (16, stream.Position);
1489 [ExpectedException (typeof (ObjectDisposedException))]
1490 public void SetLength_Disposed ()
1492 string path = TempFolder + Path.DirectorySeparatorChar + "temp";
1494 FileStream stream = new FileStream (path, FileMode.OpenOrCreate, FileAccess.Write);
1496 stream.SetLength (16);
1500 [ExpectedException (typeof (ObjectDisposedException))]
1501 public void Position_Disposed ()
1503 string path = TempFolder + Path.DirectorySeparatorChar + "temp";
1505 FileStream stream = new FileStream (path, FileMode.OpenOrCreate, FileAccess.Read);
1507 stream.Position = 0;
1511 [ExpectedException (typeof (ObjectDisposedException))]
1512 public void BeginRead_Disposed ()
1514 string path = TempFolder + Path.DirectorySeparatorChar + "temp";
1516 FileStream stream = new FileStream (path, FileMode.OpenOrCreate, FileAccess.Read);
1518 stream.EndRead (stream.BeginRead (new byte[8], 0, 8, null, null));
1522 [ExpectedException (typeof (ObjectDisposedException))]
1523 public void BeginWrite_Disposed ()
1525 string path = TempFolder + Path.DirectorySeparatorChar + "temp";
1527 FileStream stream = new FileStream (path, FileMode.OpenOrCreate, FileAccess.Write);
1529 stream.EndWrite (stream.BeginWrite (new byte[8], 0, 8, null, null));
1532 static IAsyncResult DoBeginWrite(Stream stream, ManualResetEvent mre, byte[] RandomBuffer)
1534 return stream.BeginWrite (RandomBuffer, 0, RandomBuffer.Length, ar => {
1535 IAsyncResult begin_write_recursive_ares;
1538 stream.EndWrite (ar);
1540 // we don't supply an ManualResetEvent so this will throw an NRE on the second run
1541 // which nunit-console will ignore (but other test runners don't like that)
1545 begin_write_recursive_ares = DoBeginWrite (stream, null, RandomBuffer);
1546 begin_write_recursive_ares.AsyncWaitHandle.WaitOne ();
1549 } catch (ObjectDisposedException e) {
1550 Console.WriteLine ("stream was disposed: {0}", e);
1556 public void BeginWrite_Recursive ()
1558 string path = TempFolder + Path.DirectorySeparatorChar + "temp";
1561 IAsyncResult begin_write_ares = null;
1564 using (FileStream stream = new FileStream (path, FileMode.OpenOrCreate, FileAccess.Write)) {
1565 var mre = new ManualResetEvent (false);
1566 var RandomBuffer = new byte[1024];
1567 begin_write_ares = DoBeginWrite (stream, mre, RandomBuffer);
1568 Assert.IsTrue (mre.WaitOne (5000), "#1");
1571 if (begin_write_ares != null)
1572 begin_write_ares.AsyncWaitHandle.WaitOne ();
1577 [ExpectedException (typeof (ObjectDisposedException))]
1578 public void Lock_Disposed ()
1580 string path = TempFolder + Path.DirectorySeparatorChar + "temp";
1582 FileStream stream = new FileStream (path, FileMode.OpenOrCreate, FileAccess.Write);
1588 [ExpectedException (typeof (ObjectDisposedException))]
1589 public void Unlock_Disposed ()
1591 string path = TempFolder + Path.DirectorySeparatorChar + "temp";
1593 FileStream stream = new FileStream (path, FileMode.OpenOrCreate, FileAccess.Write);
1595 stream.Unlock (0, 1);
1599 public void ReadBytePastEndOfStream ()
1601 string path = TempFolder + Path.DirectorySeparatorChar + "temp";
1603 using (FileStream stream = new FileStream (path, FileMode.OpenOrCreate, FileAccess.Read)) {
1604 stream.Seek (0, SeekOrigin.End);
1605 Assert.AreEqual (-1, stream.ReadByte ());
1611 [ExpectedException (typeof (NotSupportedException))]
1612 public void SetLengthWithClosedBaseStream ()
1614 string fn = Path.Combine (TempFolder, "temp");
1616 FileStream fs = new FileStream (fn, FileMode.Create);
1617 BufferedStream bs = new BufferedStream (fs);
1620 bs.SetLength (1000);
1627 public void LengthAfterWrite ()
1629 string path = TempFolder + DSC + "oneofthefilescreated.txt";
1630 FileStream fs = null;
1633 fs = new FileStream (path, FileMode.CreateNew);
1634 fs.WriteByte (Convert.ToByte ('A'));
1635 byte [] buffer = Encoding.ASCII.GetBytes (" is a first character.");
1636 fs.Write (buffer, 0, buffer.Length);
1637 fs.Seek (0, SeekOrigin.Begin);
1638 char a = Convert.ToChar (fs.ReadByte ());
1639 Assert.AreEqual ('A', a, "#A1");
1640 Assert.AreEqual (23, fs.Length, "#A2");
1641 int nread = fs.Read (buffer, 0, 5);
1642 Assert.AreEqual (5, nread, "#A3");
1651 public void DeleteOnClose ()
1653 string path = TempFolder + DSC + "created.txt";
1655 FileStream fs = new FileStream (path, FileMode.CreateNew, FileAccess.Write, FileShare.None, 1024,
1656 FileOptions.DeleteOnClose);
1657 Assert.AreEqual (true, File.Exists (path), "DOC#1");
1659 Assert.AreEqual (false, File.Exists (path), "DOC#2");
1664 public void OpenCharDeviceRepeatedly ()
1666 // https://bugzilla.xamarin.com/show_bug.cgi?id=38408
1668 using (var f = new FileStream ("/dev/zero", FileMode.Open))
1671 } catch (FileNotFoundException) {
1672 // Only run this test on platforms where /dev/zero exists
1676 // this shouldn't throw
1677 using (var g = new FileStream ("/dev/zero", FileMode.Open))
1684 public void WriteWithExposedHandle ()
1686 string path = TempFolder + DSC + "exposed-handle.txt";
1687 FileStream fs1 = null;
1688 FileStream fs2 = null;
1692 fs1 = new FileStream (path, FileMode.Create);
1693 fs2 = new FileStream (fs1.SafeFileHandle, FileAccess.ReadWrite);
1694 fs1.WriteByte (Convert.ToByte ('H'));
1695 fs1.WriteByte (Convert.ToByte ('E'));
1696 fs1.WriteByte (Convert.ToByte ('L'));
1697 fs2.WriteByte (Convert.ToByte ('L'));
1698 fs2.WriteByte (Convert.ToByte ('O'));
1699 long fs1Pos = fs1.Position;
1705 var check = Encoding.ASCII.GetString (File.ReadAllBytes (path));
1706 Assert.AreEqual ("HELLO", check, "EXPOSED#1");
1707 Assert.AreEqual (5, fs1Pos, "EXPOSED#2");
1718 public void Ctor_InvalidSafeHandle ()
1720 var sf = new SafeFileHandle (IntPtr.Zero, true);
1722 new FileStream (sf, FileAccess.ReadWrite);
1724 } catch (ArgumentException) {
1729 [Test] // Covers #11699
1730 public void ReadWriteFileLength ()
1732 int bufferSize = 128;
1734 int writeLength = bufferSize + 1;
1736 string path = TempFolder + DSC + "readwritefilelength.tmp";
1739 File.WriteAllBytes (path, new byte [readLength + 1]);
1741 using (var file = new FileStream (path, FileMode.Open, FileAccess.ReadWrite, FileShare.Read,
1742 bufferSize, FileOptions.SequentialScan))
1744 file.Read (new byte [readLength], 0, readLength);
1745 file.Write (new byte [writeLength], 0, writeLength);
1747 Assert.AreEqual (readLength + writeLength, file.Length);