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;
20 namespace MonoTests.System.IO
23 public class FileStreamTest
25 string TempFolder = Path.Combine (Path.GetTempPath (), "MonoTests.System.IO.Tests");
26 static readonly char DSC = Path.DirectorySeparatorChar;
27 static bool MacOSX = false;
30 static extern int uname (IntPtr buf);
33 public void TearDown ()
35 if (Directory.Exists (TempFolder))
36 Directory.Delete (TempFolder, true);
42 if (Directory.Exists (TempFolder))
43 Directory.Delete (TempFolder, true);
45 Directory.CreateDirectory (TempFolder);
48 IntPtr buf = Marshal.AllocHGlobal (8192);
50 MacOSX = Marshal.PtrToStringAnsi (buf) == "Darwin";
51 Marshal.FreeHGlobal (buf);
55 public void TestCtr ()
57 string path = TempFolder + DSC + "testfilestream.tmp.1";
59 FileStream stream = null;
61 stream = new FileStream (path, FileMode.Create);
71 [ExpectedException (typeof (ArgumentException))]
72 public void CtorArgumentException1 ()
75 stream = new FileStream ("", FileMode.Create);
80 [ExpectedException (typeof (ArgumentNullException))]
81 public void CtorArgumentNullException ()
83 FileStream stream = new FileStream (null, FileMode.Create);
88 public void CtorFileNotFoundException_Mode_Open ()
91 string path = TempFolder + DSC + "thisfileshouldnotexists.test";
93 FileStream stream = null;
95 stream = new FileStream (TempFolder + DSC + "thisfileshouldnotexists.test", FileMode.Open);
97 } catch (FileNotFoundException ex) {
98 Assert.AreEqual (typeof (FileNotFoundException), ex.GetType (), "#A2");
99 Assert.AreEqual (path, ex.FileName, "#A3");
100 Assert.IsNull (ex.InnerException, "#A4");
101 Assert.IsNotNull (ex.Message, "#A5");
102 Assert.IsTrue (ex.Message.IndexOf (path) != -1, "#A6");
104 if (stream != null) {
112 string orignalCurrentDir = Directory.GetCurrentDirectory ();
113 Directory.SetCurrentDirectory (TempFolder);
115 stream = new FileStream ("thisfileshouldnotexists.test", FileMode.Open);
117 } catch (FileNotFoundException ex) {
118 Assert.AreEqual (typeof (FileNotFoundException), ex.GetType (), "#B2");
119 // under OSX 'var' is a symlink to 'private/var'
121 path = "/private" + path;
122 Assert.AreEqual (path, ex.FileName, "#B3");
123 Assert.IsNull (ex.InnerException, "#B4");
124 Assert.IsNotNull (ex.Message, "#B5");
125 Assert.IsTrue (ex.Message.IndexOf (path) != -1, "#B6");
127 // restore original current directory
128 Directory.SetCurrentDirectory (orignalCurrentDir);
136 public void CtorFileNotFoundException_Mode_Truncate ()
139 string path = TempFolder + DSC + "thisfileshouldNOTexists.test";
141 FileStream stream = null;
143 stream = new FileStream (path, FileMode.Truncate);
145 } catch (FileNotFoundException ex) {
146 Assert.AreEqual (typeof (FileNotFoundException), ex.GetType (), "#A2");
147 Assert.AreEqual (path, ex.FileName, "#A3");
148 Assert.IsNull (ex.InnerException, "#A4");
149 Assert.IsNotNull (ex.Message, "#A5");
150 Assert.IsTrue (ex.Message.IndexOf (path) != -1, "#A6");
152 if (stream != null) {
160 string orignalCurrentDir = Directory.GetCurrentDirectory ();
161 Directory.SetCurrentDirectory (TempFolder);
163 stream = new FileStream ("thisfileshouldNOTexists.test", FileMode.Truncate);
165 } catch (FileNotFoundException ex) {
166 Assert.AreEqual (typeof (FileNotFoundException), ex.GetType (), "#B2");
167 // under OSX 'var' is a symlink to 'private/var'
169 path = "/private" + path;
170 Assert.AreEqual (path, ex.FileName, "#B3");
171 Assert.IsNull (ex.InnerException, "#B4");
172 Assert.IsNotNull (ex.Message, "#B5");
173 Assert.IsTrue (ex.Message.IndexOf (path) != -1, "#B6");
175 // restore original current directory
176 Directory.SetCurrentDirectory (orignalCurrentDir);
184 public void CtorIOException1 ()
187 string path = TempFolder + DSC + "thisfileshouldexists.test";
188 FileStream stream = null;
191 stream = new FileStream (path, FileMode.CreateNew);
194 stream = new FileStream (path, FileMode.CreateNew);
196 } catch (IOException ex) {
197 Assert.AreEqual (typeof (IOException), ex.GetType (), "#A2");
198 Assert.IsNull (ex.InnerException, "#A3");
199 Assert.IsNotNull (ex.Message, "#A4");
200 Assert.IsTrue (ex.Message.IndexOf (path) != -1, "#A5");
202 if (stream != null) {
210 string orignalCurrentDir = Directory.GetCurrentDirectory ();
211 Directory.SetCurrentDirectory (TempFolder);
213 stream = new FileStream ("thisfileshouldexists.test", FileMode.CreateNew);
216 stream = new FileStream ("thisfileshouldexists.test", FileMode.CreateNew);
218 } catch (IOException ex) {
219 Assert.AreEqual (typeof (IOException), ex.GetType (), "#B2");
220 Assert.IsNull (ex.InnerException, "#B3");
221 Assert.IsNotNull (ex.Message, "#B4");
222 Assert.IsTrue (ex.Message.IndexOf (path) != -1, "#B5");
224 // restore original current directory
225 Directory.SetCurrentDirectory (orignalCurrentDir);
233 [ExpectedException (typeof (ArgumentOutOfRangeException))]
234 public void CtorArgumentOutOfRangeException1 ()
236 FileStream stream = null;
237 string path = TempFolder + Path.DirectorySeparatorChar + "temp";
240 stream = new FileStream (path, FileMode.Append | FileMode.CreateNew);
249 [ExpectedException (typeof (ArgumentOutOfRangeException))]
250 public void CtorArgumentOutOfRangeException2 ()
252 FileStream stream = null;
253 string path = TempFolder + Path.DirectorySeparatorChar + "temp";
256 stream = new FileStream ("test.test.test", FileMode.Append | FileMode.Open);
264 private void CtorDirectoryNotFoundException (FileMode mode)
266 string path = TempFolder + DSC + "thisDirectoryShouldNotExists";
267 if (Directory.Exists (path))
268 Directory.Delete (path, true);
270 FileStream stream = null;
272 stream = new FileStream (path + DSC + "eitherthisfile.test", mode);
278 if (Directory.Exists (path))
279 Directory.Delete (path, true);
284 [ExpectedException (typeof (DirectoryNotFoundException))]
285 public void CtorDirectoryNotFoundException_CreateNew ()
287 CtorDirectoryNotFoundException (FileMode.CreateNew);
291 [ExpectedException (typeof (DirectoryNotFoundException))]
292 public void CtorDirectoryNotFoundException_Create ()
294 CtorDirectoryNotFoundException (FileMode.Create);
298 [ExpectedException (typeof (DirectoryNotFoundException))]
299 public void CtorDirectoryNotFoundException_Open ()
301 CtorDirectoryNotFoundException (FileMode.Open);
305 [ExpectedException (typeof (DirectoryNotFoundException))]
306 public void CtorDirectoryNotFoundException_OpenOrCreate ()
308 CtorDirectoryNotFoundException (FileMode.OpenOrCreate);
312 [ExpectedException (typeof (DirectoryNotFoundException))]
313 public void CtorDirectoryNotFoundException_Truncate ()
315 CtorDirectoryNotFoundException (FileMode.Truncate);
319 [ExpectedException (typeof (DirectoryNotFoundException))]
320 public void CtorDirectoryNotFoundException_Append ()
322 CtorDirectoryNotFoundException (FileMode.Append);
326 public void CtorDirectoryNotFound_RelativePath ()
328 string orignalCurrentDir = Directory.GetCurrentDirectory ();
329 Directory.SetCurrentDirectory (TempFolder);
330 string relativePath = "DirectoryDoesNotExist" + Path.DirectorySeparatorChar + "file.txt";
331 string fullPath = Path.Combine (TempFolder, relativePath);
333 new FileStream (relativePath, FileMode.Open);
335 } catch (DirectoryNotFoundException ex) {
336 Assert.AreEqual (typeof (DirectoryNotFoundException), ex.GetType (), "#A2");
337 Assert.IsNull (ex.InnerException, "#A3");
338 Assert.IsNotNull (ex.Message, "#A4");
339 Assert.IsTrue (ex.Message.IndexOf (fullPath) != -1, "#A5");
341 // restore original current directory
342 Directory.SetCurrentDirectory (orignalCurrentDir);
347 // FileShare.Inheritable is ignored, but file does not exist
348 [ExpectedException (typeof (FileNotFoundException))]
349 public void CtorArgumentOutOfRangeException3 ()
351 string path = TempFolder + DSC + "CtorArgumentOutOfRangeException1";
354 FileStream stream = null;
356 stream = new FileStream (path, FileMode.Open, FileAccess.Read, FileShare.Inheritable);
365 [ExpectedException (typeof (ArgumentOutOfRangeException))]
366 public void CtorArgumentOutOfRangeException4 ()
368 string path = TempFolder + DSC + "CtorArgumentOutOfRangeException4";
371 FileStream stream = null;
373 stream = new FileStream (path, FileMode.OpenOrCreate, FileAccess.Read, FileShare.ReadWrite, -1);
382 [ExpectedException (typeof (ArgumentOutOfRangeException))]
383 public void CtorBufferSizeZero ()
385 // Buffer size can't be zero
387 string path = Path.Combine (TempFolder, "CtorBufferSizeZero");
390 FileStream stream = null;
392 stream = new FileStream (path, FileMode.CreateNew, FileAccess.Write, FileShare.ReadWrite, 0);
401 [ExpectedException (typeof (ArgumentException))]
402 public void CtorArgumentException2 ()
404 // FileMode.CreateNew && FileAccess.Read
406 string path = TempFolder + Path.DirectorySeparatorChar + "temp";
407 FileStream stream = null;
412 stream = new FileStream (".test.test.test.2", FileMode.CreateNew, FileAccess.Read, FileShare.None | FileShare.Write);
423 // FileShare.Inheritable is ignored, but file does not exist
424 [ExpectedException (typeof (FileNotFoundException))]
425 public void CtorArgumentOutOfRangeException5 ()
427 string path = TempFolder + Path.DirectorySeparatorChar + "temp";
430 FileStream stream = null;
432 stream = new FileStream (path, FileMode.Open, FileAccess.Read, FileShare.Inheritable | FileShare.ReadWrite);
442 [ExpectedException (typeof (ArgumentException))]
443 public void CtorArgumentException3 ()
445 string path = TempFolder + Path.DirectorySeparatorChar + "temp";
446 FileStream stream = null;
451 stream = new FileStream (".test.test.test.2", FileMode.Truncate, FileAccess.Read);
461 public void ModeAndAccessCombinations ()
463 string path = TempFolder + Path.DirectorySeparatorChar + "temp";
465 FileStream stream = null;
469 // Append access can be requested only in write-only mode
470 stream = new FileStream (path, FileMode.Append, FileAccess.Read);
472 } catch (ArgumentException ex) {
473 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#A2");
481 // Append / ReadWrite
483 // Append access can be requested only in write-only mode
484 stream = new FileStream (path, FileMode.Append, FileAccess.ReadWrite);
486 } catch (ArgumentException ex) {
487 // make sure it is exact this exception, and not a derived type
488 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#B2");
498 stream = new FileStream (path, FileMode.Append, FileAccess.Write);
508 stream = new FileStream (path, FileMode.Create, FileAccess.Read);
510 } catch (ArgumentException ex) {
511 // make sure it is exact this exception, and not a derived type
512 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#C2");
520 // Create / ReadWrite
522 stream = new FileStream (path, FileMode.Create, FileAccess.ReadWrite);
532 stream = new FileStream (path, FileMode.Create, FileAccess.Write);
542 stream = new FileStream (path, FileMode.CreateNew, FileAccess.Read);
544 } catch (ArgumentException ex) {
545 // make sure it is exact this exception, and not a derived type
546 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#D2");
554 // CreateNew / ReadWrite
556 stream = new FileStream (path, FileMode.CreateNew, FileAccess.ReadWrite);
566 stream = new FileStream (path, FileMode.CreateNew, FileAccess.Write);
576 stream = new FileStream (path, FileMode.Open, FileAccess.Read);
578 } catch (FileNotFoundException ex) {
579 // make sure it is exact this exception, and not a derived type
580 Assert.AreEqual (typeof (FileNotFoundException), ex.GetType (), "#E2");
581 Assert.AreEqual (path, ex.FileName, "#E3");
582 Assert.IsNull (ex.InnerException, "#E4");
583 Assert.IsNotNull (ex.Message, "#E5");
584 Assert.IsTrue (ex.Message.IndexOf (path) != -1, "#E6");
594 stream = new FileStream (path, FileMode.Open, FileAccess.ReadWrite);
596 } catch (FileNotFoundException ex) {
597 // make sure it is exact this exception, and not a derived type
598 Assert.AreEqual (typeof (FileNotFoundException), ex.GetType (), "#F2");
599 Assert.AreEqual (path, ex.FileName, "#F3");
600 Assert.IsNull (ex.InnerException, "#F4");
601 Assert.IsNotNull (ex.Message, "#F5");
602 Assert.IsTrue (ex.Message.IndexOf (path) != -1, "#F6");
612 stream = new FileStream (path, FileMode.Open, FileAccess.Write);
614 } catch (FileNotFoundException ex) {
615 // make sure it is exact this exception, and not a derived type
616 Assert.AreEqual (typeof (FileNotFoundException), ex.GetType (), "#G2");
617 Assert.AreEqual (path, ex.FileName, "#G3");
618 Assert.IsNull (ex.InnerException, "#G4");
619 Assert.IsNotNull (ex.Message, "#G5");
620 Assert.IsTrue (ex.Message.IndexOf (path) != -1, "#G6");
628 // OpenOrCreate / Read
630 stream = new FileStream (path, FileMode.OpenOrCreate, FileAccess.Read);
638 // OpenOrCreate / ReadWrite
640 stream = new FileStream (path, FileMode.OpenOrCreate, FileAccess.ReadWrite);
648 // OpenOrCreate / Write
650 stream = new FileStream (path, FileMode.OpenOrCreate, FileAccess.Write);
660 stream = new FileStream (path, FileMode.Truncate, FileAccess.Read);
662 } catch (ArgumentException ex) {
663 // make sure it is exact this exception, and not a derived type
664 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#H2");
672 // Truncate / ReadWrite
674 stream = new FileStream (path, FileMode.Truncate, FileAccess.ReadWrite);
676 } catch (FileNotFoundException ex) {
677 // make sure it is exact this exception, and not a derived type
678 Assert.AreEqual (typeof (FileNotFoundException), ex.GetType (), "#I2");
679 Assert.AreEqual (path, ex.FileName, "#I3");
680 Assert.IsNull (ex.InnerException, "#I4");
681 Assert.IsNotNull (ex.Message, "#I5");
682 Assert.IsTrue (ex.Message.IndexOf (path) != -1, "#I6");
692 stream = new FileStream (path, FileMode.Truncate, FileAccess.Write);
694 } catch (FileNotFoundException ex) {
695 // make sure it is exact this exception, and not a derived type
696 Assert.AreEqual (typeof (FileNotFoundException), ex.GetType (), "#J2");
697 Assert.AreEqual (path, ex.FileName, "#J3");
698 Assert.IsNull (ex.InnerException, "#J4");
699 Assert.IsNotNull (ex.Message, "#J5");
700 Assert.IsTrue (ex.Message.IndexOf (path) != -1, "#J6");
709 [Test, ExpectedException (typeof (IOException))]
710 public void CtorIOException2 ()
712 FileStream stream = null;
714 stream = new FileStream (new IntPtr (Int32.MaxValue), FileAccess.Read);
721 [Test, ExpectedException (typeof (IOException))]
722 public void CtorIOException ()
724 string path = TempFolder + DSC + "CTorIOException.Test";
725 FileStream stream = null;
726 FileStream stream2 = null;
730 stream = new FileStream (path, FileMode.CreateNew);
732 // used by an another process
733 stream2 = new FileStream (path, FileMode.OpenOrCreate);
744 public void CtorAccess1Read2Read ()
746 FileStream fs = null;
747 FileStream fs2 = null;
748 string fn = Path.Combine (TempFolder, "temp");
750 if (!File.Exists (fn)) {
751 TextWriter tw = File.CreateText (fn);
755 fs = new FileStream (fn, FileMode.Open, FileAccess.Read);
756 fs2 = new FileStream (fn, FileMode.Open, FileAccess.Read);
762 if (File.Exists (fn))
768 [ExpectedException (typeof (IOException))]
769 public void CtorAccess1Read2Write ()
771 string fn = Path.Combine (TempFolder, "temp");
772 FileStream fs1 = null;
773 FileStream fs2 = null;
775 if (!File.Exists (fn)) {
776 using (TextWriter tw = File.CreateText (fn)) {
780 fs1 = new FileStream (fn, FileMode.Open, FileAccess.Read);
781 fs2 = new FileStream (fn, FileMode.Create, FileAccess.Write);
787 if (File.Exists (fn))
793 [ExpectedException (typeof (IOException))]
794 public void CtorAccess1Write2Write ()
796 string fn = Path.Combine (TempFolder, "temp");
797 FileStream fs1 = null;
798 FileStream fs2 = null;
800 if (File.Exists (fn))
802 fs1 = new FileStream (fn, FileMode.Create, FileAccess.Write);
803 fs2 = new FileStream (fn, FileMode.Create, FileAccess.Write);
809 if (File.Exists (fn))
815 [ExpectedException (typeof (UnauthorizedAccessException))]
816 public void CtorReadDirectoryAsFile ()
818 FileStream stream = null;
820 stream = new FileStream (TempFolder, FileMode.Open, FileAccess.Read);
828 public void FileShare_Delete ()
830 string fn = Path.Combine (TempFolder, "temp");
832 using (Stream s = new FileStream (fn, FileMode.OpenOrCreate, FileAccess.Write, FileShare.Delete)) {
833 s.Write (new byte [1] { 0x5 }, 0, 1);
837 using (Stream s = new FileStream (fn, FileMode.OpenOrCreate, FileAccess.Write, FileShare.ReadWrite | FileShare.Delete)) {
838 s.Write (new byte [1] { 0x5 }, 0, 1);
842 using (Stream s = new FileStream (fn, FileMode.OpenOrCreate, FileAccess.Write, FileShare.Write | FileShare.Delete)) {
843 s.Write (new byte [1] { 0x5 }, 0, 1);
847 using (Stream s = new FileStream (fn, FileMode.OpenOrCreate, FileAccess.Write, FileShare.Read | FileShare.Delete)) {
848 s.Write (new byte [1] { 0x5 }, 0, 1);
852 using (Stream s = new FileStream (fn, FileMode.OpenOrCreate, FileAccess.Write, FileShare.Inheritable | FileShare.Delete)) {
853 s.Write (new byte [1] { 0x5 }, 0, 1);
861 string path = TempFolder + DSC + "FileStreamTest.Write";
865 FileStream stream = new FileStream (path, FileMode.CreateNew, FileAccess.ReadWrite, FileShare.ReadWrite, 8);
867 byte[] outbytes = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16 };
868 byte[] bytes = new byte[15];
870 // Check that the data is flushed when we overflow the buffer
871 // with a large amount of data
872 stream.Write (outbytes, 0, 5);
873 stream.Write (outbytes, 5, 10);
874 stream.Seek (0, SeekOrigin.Begin);
876 stream.Read (bytes, 0, 15);
877 for (int i = 0; i < 15; ++i)
878 Assert.AreEqual (i + 1, bytes[i], "#1");
880 // Check that the data is flushed when we overflow the buffer
881 // with a small amount of data
882 stream.Write (outbytes, 0, 7);
883 stream.Write (outbytes, 7, 7);
884 stream.Write (outbytes, 14, 1);
886 stream.Read (bytes, 0, 15);
887 stream.Seek (15, SeekOrigin.Begin);
888 for (int i = 0; i < 15; ++i)
889 Assert.AreEqual (i + 1, bytes[i], "#2");
894 public void Length ()
896 // Test that the Length property takes into account the data
898 string path = TempFolder + DSC + "FileStreamTest.Length";
902 FileStream stream = new FileStream (path, FileMode.CreateNew);
904 byte[] outbytes = new byte[] { 1, 2, 3, 4 };
906 stream.Write (outbytes, 0, 4);
907 Assert.AreEqual (4, stream.Length);
914 string path = TempFolder + DSC + "FileStreamTest.Flush";
915 FileStream stream = null;
916 FileStream stream2 = null;
921 stream = new FileStream (path, FileMode.CreateNew, FileAccess.ReadWrite, FileShare.ReadWrite);
922 stream2 = new FileStream (path, FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite);
924 stream.Write (new byte[] { 1, 2, 3, 4, 5 }, 0, 5);
926 byte[] bytes = new byte[5];
927 stream2.Read (bytes, 0, 5);
928 Assert.AreEqual (0, bytes[0], "#A1");
929 Assert.AreEqual (0, bytes[1], "#A2");
930 Assert.AreEqual (0, bytes[2], "#A3");
931 Assert.AreEqual (0, bytes[3], "#A4");
934 stream2.Read (bytes, 0, 5);
935 Assert.AreEqual (1, bytes[0], "#B1");
936 Assert.AreEqual (2, bytes[1], "#B2");
937 Assert.AreEqual (3, bytes[2], "#B3");
938 Assert.AreEqual (4, bytes[3], "#B4");
949 public void TestDefaultProperties ()
951 string path = TempFolder + Path.DirectorySeparatorChar + "testfilestream.tmp.2";
954 FileStream stream = new FileStream (path, FileMode.Create);
956 Assert.IsTrue (stream.CanRead, "#A1");
957 Assert.IsTrue (stream.CanSeek, "#A2");
958 Assert.IsTrue (stream.CanWrite, "#A3");
959 Assert.IsFalse (stream.IsAsync, "#A4");
960 Assert.IsTrue (stream.Name.EndsWith (path), "#A5");
961 Assert.AreEqual (0, stream.Position, "#A6");
962 Assert.AreEqual ("System.IO.FileStream", stream.ToString (), "#A7");
966 stream = new FileStream (path, FileMode.OpenOrCreate, FileAccess.Read);
967 Assert.IsTrue (stream.CanRead, "#B1");
968 Assert.IsTrue (stream.CanSeek, "#B2");
969 Assert.IsFalse (stream.CanWrite, "#B3");
970 Assert.IsFalse (stream.IsAsync, "#B4");
971 Assert.IsTrue (stream.Name.EndsWith (path), "#B5");
972 Assert.AreEqual (0, stream.Position, "#B6");
973 Assert.AreEqual ("System.IO.FileStream", stream.ToString (), "#B7");
976 stream = new FileStream (path, FileMode.Truncate, FileAccess.Write, FileShare.ReadWrite);
977 Assert.IsFalse (stream.CanRead, "#C1");
978 Assert.IsTrue (stream.CanSeek, "#C2");
979 Assert.IsTrue (stream.CanWrite, "#C3");
980 Assert.IsFalse (stream.IsAsync, "#C4");
981 Assert.IsTrue (stream.Name.EndsWith ("testfilestream.tmp.2"), "#C5");
982 Assert.AreEqual (0, stream.Position, "#C6");
983 Assert.AreEqual ("System.IO.FileStream", stream.ToString (), "#C7");
988 [Category ("NotWorking")]
989 // Bug: 71371 -> duplicate and WONTFIX.
990 public void TestLock_FailsOnMono ()
992 string path = TempFolder + Path.DirectorySeparatorChar + "TestLock";
995 FileStream stream = new FileStream (path, FileMode.CreateNew, FileAccess.ReadWrite);
997 stream.Write (new Byte[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }, 0, 10);
1000 stream = new FileStream (path, FileMode.Open, FileAccess.ReadWrite);
1004 FileStream stream2 = new FileStream (path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
1006 byte[] bytes = new byte[5];
1008 stream2.Read (bytes, 0, 5);
1010 } catch (Exception e) {
1011 Assert.AreEqual (typeof (IOException), e.GetType (), "#2");
1020 public void TestLock ()
1022 string path = TempFolder + Path.DirectorySeparatorChar + "TestLock";
1025 FileStream stream = new FileStream (path, FileMode.CreateNew, FileAccess.ReadWrite);
1027 stream.Write (new Byte[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }, 0, 10);
1030 stream = new FileStream (path, FileMode.Open, FileAccess.ReadWrite);
1034 FileStream stream2 = new FileStream (path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
1036 byte[] bytes = new byte[5];
1038 stream2.Read (bytes, 0, 5);
1039 Assert.Fail ("#A1");
1040 } catch (Exception) {
1041 // Bug #71371: on MS.NET you get an IOException detailing a lock
1042 // Assert.AreEqual (typeof (IOException), e.GetType (), "#A2");
1045 stream2.Seek (5, SeekOrigin.Begin);
1046 stream2.Read (bytes, 0, 5);
1048 Assert.AreEqual (5, bytes[0], "#B1");
1049 Assert.AreEqual (6, bytes[1], "#B2");
1050 Assert.AreEqual (7, bytes[2], "#B3");
1051 Assert.AreEqual (8, bytes[3], "#B4");
1052 Assert.AreEqual (9, bytes[4], "#B5");
1054 stream.Unlock (0, 5);
1055 stream2.Seek (0, SeekOrigin.Begin);
1056 stream2.Read (bytes, 0, 5);
1058 Assert.AreEqual (0, bytes[0], "#C1");
1059 Assert.AreEqual (1, bytes[1], "#C2");
1060 Assert.AreEqual (2, bytes[2], "#C3");
1061 Assert.AreEqual (3, bytes[3], "#C4");
1062 Assert.AreEqual (4, bytes[4], "#C5");
1073 string path = TempFolder + DSC + "FST.Seek.Test";
1076 FileStream stream = new FileStream (path, FileMode.CreateNew, FileAccess.ReadWrite, FileShare.ReadWrite);
1077 FileStream stream2 = new FileStream (path, FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite);
1079 stream.Write (new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 10 }, 0, 9);
1080 Assert.AreEqual (5, stream2.Seek (5, SeekOrigin.Begin), "#1");
1081 Assert.AreEqual (-1, stream2.ReadByte (), "#2");
1083 Assert.AreEqual (2, stream2.Seek (-3, SeekOrigin.Current), "#3");
1084 Assert.AreEqual (-1, stream2.ReadByte (), "#4");
1086 Assert.AreEqual (12, stream.Seek (3, SeekOrigin.Current), "#5");
1087 Assert.AreEqual (-1, stream.ReadByte (), "#6");
1089 Assert.AreEqual (5, stream.Seek (-7, SeekOrigin.Current), "#7");
1090 Assert.AreEqual (6, stream.ReadByte (), "#8");
1092 Assert.AreEqual (5, stream2.Seek (5, SeekOrigin.Begin), "#9");
1093 Assert.AreEqual (6, stream2.ReadByte (), "#10");
1101 public void TestSeek ()
1103 string path = TempFolder + Path.DirectorySeparatorChar + "TestSeek";
1106 FileStream stream = new FileStream (path, FileMode.CreateNew, FileAccess.ReadWrite, FileShare.ReadWrite);
1107 stream.Write (new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }, 0, 10);
1109 stream.Seek (5, SeekOrigin.End);
1110 Assert.AreEqual (-1, stream.ReadByte (), "#1");
1112 stream.Seek (-5, SeekOrigin.End);
1113 Assert.AreEqual (6, stream.ReadByte (), "#2");
1116 stream.Seek (-11, SeekOrigin.End);
1118 } catch (Exception e) {
1119 Assert.AreEqual (typeof (IOException), e.GetType (), "#4");
1122 stream.Seek (19, SeekOrigin.Begin);
1123 Assert.AreEqual (-1, stream.ReadByte (), "#5");
1125 stream.Seek (1, SeekOrigin.Begin);
1126 Assert.AreEqual (2, stream.ReadByte (), "#6");
1128 stream.Seek (3, SeekOrigin.Current);
1129 Assert.AreEqual (6, stream.ReadByte (), "#7");
1131 stream.Seek (-2, SeekOrigin.Current);
1132 Assert.AreEqual (5, stream.ReadByte (), "#8");
1136 // Test that seeks work correctly when seeking inside the buffer
1137 stream.Seek (0, SeekOrigin.Begin);
1138 stream.WriteByte (0);
1139 stream.WriteByte (1);
1140 stream.Seek (0, SeekOrigin.Begin);
1141 byte[] buf = new byte[1];
1143 stream.Write (buf, 0, 1);
1144 stream.Write (buf, 0, 1);
1146 stream.Seek (0, SeekOrigin.Begin);
1147 Assert.AreEqual (2, stream.ReadByte (), "#9");
1148 Assert.AreEqual (2, stream.ReadByte (), "#10");
1155 public void TestClose ()
1157 string path = TempFolder + Path.DirectorySeparatorChar + "TestClose";
1160 FileStream stream = new FileStream (path, FileMode.CreateNew, FileAccess.ReadWrite);
1162 stream.Write (new byte[] { 1, 2, 3, 4 }, 0, 4);
1168 Assert.Fail ("#A1");
1169 } catch (Exception e) {
1170 Assert.AreEqual (typeof (ObjectDisposedException), e.GetType (), "#A2");
1174 stream.WriteByte (64);
1175 Assert.Fail ("#B1");
1176 } catch (Exception e) {
1177 Assert.AreEqual (typeof (ObjectDisposedException), e.GetType (), "#B2");
1182 Assert.Fail ("#C1");
1183 } catch (Exception e) {
1184 Assert.AreEqual (typeof (ObjectDisposedException), e.GetType (), "#C2");
1188 long l = stream.Length;
1189 Assert.Fail ("#D1");
1190 } catch (Exception e) {
1191 Assert.AreEqual (typeof (ObjectDisposedException), e.GetType (), "#D2");
1195 long l = stream.Position;
1196 Assert.Fail ("#E1");
1197 } catch (Exception e) {
1198 Assert.AreEqual (typeof (ObjectDisposedException), e.GetType (), "#E2");
1201 Assert.IsFalse (stream.CanRead, "#F1");
1202 Assert.IsFalse (stream.CanSeek, "#F2");
1203 Assert.IsFalse (stream.CanWrite, "#F3");
1204 Assert.IsTrue (stream.Name.EndsWith (path), "#F4");
1211 /// Checks whether the <see cref="FileStream" /> throws a <see cref="NotSupportedException" />
1212 /// when the stream is opened with access mode <see cref="FileAccess.Read" /> and the
1213 /// <see cref="FileStream.Write(byte[], int, int)" /> method is called.
1216 [ExpectedException (typeof (NotSupportedException))]
1217 public void TestWriteVerifyAccessMode ()
1219 string path = TempFolder + Path.DirectorySeparatorChar + "temp";
1222 FileStream stream = null;
1226 buffer = Encoding.ASCII.GetBytes ("test");
1227 stream = new FileStream (path, FileMode.OpenOrCreate, FileAccess.Read);
1228 stream.Write (buffer, 0, buffer.Length);
1237 /// Checks whether the <see cref="FileStream" /> throws a <see cref="NotSupportedException" />
1238 /// when the stream is opened with access mode <see cref="FileAccess.Read" /> and the
1239 /// <see cref="FileStream.WriteByte(byte)" /> method is called.
1242 [ExpectedException (typeof (NotSupportedException))]
1243 public void TestWriteByteVerifyAccessMode ()
1245 string path = TempFolder + Path.DirectorySeparatorChar + "temp";
1248 FileStream stream = null;
1251 stream = new FileStream (path, FileMode.OpenOrCreate, FileAccess.Read);
1252 stream.WriteByte (Byte.MinValue);
1261 /// Checks whether the <see cref="FileStream" /> throws a <see cref="NotSupportedException" />
1262 /// when the stream is opened with access mode <see cref="FileAccess.Write" /> and the
1263 /// <see cref="FileStream.Read(byte[], int, int)" /> method is called.
1266 [ExpectedException (typeof (NotSupportedException))]
1267 public void TestReadVerifyAccessMode ()
1269 string path = TempFolder + Path.DirectorySeparatorChar + "temp";
1272 FileStream stream = null;
1273 byte[] buffer = new byte[100];
1276 stream = new FileStream (path, FileMode.OpenOrCreate, FileAccess.Write);
1277 stream.Read (buffer, 0, buffer.Length);
1285 /// Checks whether the <see cref="FileStream" /> throws a <see cref="NotSupportedException" />
1286 /// when the stream is opened with access mode <see cref="FileAccess.Write" /> and the
1287 /// <see cref="FileStream.ReadByte()" /> method is called.
1290 [ExpectedException (typeof (NotSupportedException))]
1291 public void TestReadByteVerifyAccessMode ()
1293 string path = TempFolder + Path.DirectorySeparatorChar + "temp";
1296 FileStream stream = null;
1299 stream = new FileStream (path, FileMode.OpenOrCreate, FileAccess.Write);
1300 int readByte = stream.ReadByte ();
1308 // Check that the stream is flushed even when it doesn't own the
1311 public void TestFlushNotOwningHandle ()
1313 string path = Path.Combine (TempFolder, "TestFlushNotOwningHandle");
1316 FileStream s = new FileStream (path, FileMode.Create);
1317 using (FileStream s2 = new FileStream (s.Handle, FileAccess.Write, false)) {
1318 byte[] buf = new byte[2];
1320 s2.Write (buf, 0, 1);
1324 Assert.AreEqual ((int) '1', s.ReadByte ());
1328 private void DeleteFile (string path)
1330 if (File.Exists (path))
1335 [ExpectedException (typeof (ArgumentOutOfRangeException))]
1336 public void Read_OffsetNegative ()
1338 string path = TempFolder + Path.DirectorySeparatorChar + "temp";
1341 using (FileStream stream = new FileStream (path, FileMode.OpenOrCreate, FileAccess.Read)) {
1342 stream.Read (new byte[0], -1, 1);
1347 [ExpectedException (typeof (ArgumentException))]
1348 public void Read_OffsetOverflow ()
1350 string path = TempFolder + Path.DirectorySeparatorChar + "temp";
1353 using (FileStream stream = new FileStream (path, FileMode.OpenOrCreate, FileAccess.Read)) {
1354 stream.Read (new byte[0], Int32.MaxValue, 1);
1359 [ExpectedException (typeof (ArgumentOutOfRangeException))]
1360 public void Read_CountNegative ()
1362 string path = TempFolder + Path.DirectorySeparatorChar + "temp";
1365 using (FileStream stream = new FileStream (path, FileMode.OpenOrCreate, FileAccess.Read)) {
1366 stream.Read (new byte[0], 1, -1);
1371 [ExpectedException (typeof (ArgumentException))]
1372 public void Read_CountOverflow ()
1374 string path = TempFolder + Path.DirectorySeparatorChar + "temp";
1377 using (FileStream stream = new FileStream (path, FileMode.OpenOrCreate, FileAccess.Read)) {
1378 stream.Read (new byte[0], 1, Int32.MaxValue);
1383 [ExpectedException (typeof (ArgumentOutOfRangeException))]
1384 public void Write_OffsetNegative ()
1386 string path = TempFolder + Path.DirectorySeparatorChar + "temp";
1389 using (FileStream stream = new FileStream (path, FileMode.OpenOrCreate, FileAccess.Write)) {
1390 stream.Write (new byte[0], -1, 1);
1395 [ExpectedException (typeof (ArgumentException))]
1396 public void Write_OffsetOverflow ()
1398 string path = TempFolder + Path.DirectorySeparatorChar + "temp";
1401 using (FileStream stream = new FileStream (path, FileMode.OpenOrCreate, FileAccess.Write)) {
1402 stream.Write (new byte[0], Int32.MaxValue, 1);
1407 [ExpectedException (typeof (ArgumentOutOfRangeException))]
1408 public void Write_CountNegative ()
1410 string path = TempFolder + Path.DirectorySeparatorChar + "temp";
1413 using (FileStream stream = new FileStream (path, FileMode.OpenOrCreate, FileAccess.Write)) {
1414 stream.Write (new byte[0], 1, -1);
1419 [ExpectedException (typeof (ArgumentException))]
1420 public void Write_CountOverflow ()
1422 string path = TempFolder + Path.DirectorySeparatorChar + "temp";
1425 using (FileStream stream = new FileStream (path, FileMode.OpenOrCreate, FileAccess.Write)) {
1426 stream.Write (new byte[0], 1, Int32.MaxValue);
1431 [ExpectedException (typeof (ArgumentException))]
1432 public void Seek_InvalidSeekOrigin ()
1434 string path = TempFolder + Path.DirectorySeparatorChar + "temp";
1437 using (FileStream stream = new FileStream (path, FileMode.OpenOrCreate, FileAccess.Read)) {
1438 stream.Seek (0, (SeekOrigin) (-1));
1443 [ExpectedException (typeof (ArgumentException))]
1444 public void Constructor_InvalidFileHandle ()
1446 new FileStream ((IntPtr) (-1L), FileAccess.Read);
1450 public void PositionAfterSetLength ()
1452 string path = TempFolder + Path.DirectorySeparatorChar + "temp";
1455 using (FileStream stream = new FileStream (path, FileMode.OpenOrCreate, FileAccess.Write)) {
1456 stream.SetLength (32);
1457 stream.Position = 32;
1458 stream.SetLength (16);
1459 Assert.AreEqual (16, stream.Position);
1464 [ExpectedException (typeof (ObjectDisposedException))]
1465 public void SetLength_Disposed ()
1467 string path = TempFolder + Path.DirectorySeparatorChar + "temp";
1469 FileStream stream = new FileStream (path, FileMode.OpenOrCreate, FileAccess.Write);
1471 stream.SetLength (16);
1475 [ExpectedException (typeof (ObjectDisposedException))]
1476 public void Position_Disposed ()
1478 string path = TempFolder + Path.DirectorySeparatorChar + "temp";
1480 FileStream stream = new FileStream (path, FileMode.OpenOrCreate, FileAccess.Read);
1482 stream.Position = 0;
1486 [ExpectedException (typeof (ObjectDisposedException))]
1487 public void BeginRead_Disposed ()
1489 string path = TempFolder + Path.DirectorySeparatorChar + "temp";
1491 FileStream stream = new FileStream (path, FileMode.OpenOrCreate, FileAccess.Read);
1493 stream.EndRead (stream.BeginRead (new byte[8], 0, 8, null, null));
1497 [ExpectedException (typeof (ObjectDisposedException))]
1498 public void BeginWrite_Disposed ()
1500 string path = TempFolder + Path.DirectorySeparatorChar + "temp";
1502 FileStream stream = new FileStream (path, FileMode.OpenOrCreate, FileAccess.Write);
1504 stream.EndWrite (stream.BeginWrite (new byte[8], 0, 8, null, null));
1507 static IAsyncResult DoBeginWrite(Stream stream, ManualResetEvent mre, byte[] RandomBuffer)
1509 return stream.BeginWrite (RandomBuffer, 0, RandomBuffer.Length, ar => {
1510 stream.EndWrite (ar);
1512 // we don't supply an ManualResetEvent so this will throw an NRE on the second run
1513 // which nunit-console will ignore (but other test runners don't like that)
1517 DoBeginWrite (stream, null, RandomBuffer).AsyncWaitHandle.WaitOne ();
1523 public void BeginWrite_Recursive ()
1525 string path = TempFolder + Path.DirectorySeparatorChar + "temp";
1528 using (FileStream stream = new FileStream (path, FileMode.OpenOrCreate, FileAccess.Write)) {
1529 var mre = new ManualResetEvent (false);
1530 var RandomBuffer = new byte[1024];
1531 DoBeginWrite (stream, mre, RandomBuffer);
1532 Assert.IsTrue (mre.WaitOne (5000), "#1");
1537 [ExpectedException (typeof (ObjectDisposedException))]
1538 public void Lock_Disposed ()
1540 string path = TempFolder + Path.DirectorySeparatorChar + "temp";
1542 FileStream stream = new FileStream (path, FileMode.OpenOrCreate, FileAccess.Write);
1548 [ExpectedException (typeof (ObjectDisposedException))]
1549 public void Unlock_Disposed ()
1551 string path = TempFolder + Path.DirectorySeparatorChar + "temp";
1553 FileStream stream = new FileStream (path, FileMode.OpenOrCreate, FileAccess.Write);
1555 stream.Unlock (0, 1);
1559 public void ReadBytePastEndOfStream ()
1561 string path = TempFolder + Path.DirectorySeparatorChar + "temp";
1563 using (FileStream stream = new FileStream (path, FileMode.OpenOrCreate, FileAccess.Read)) {
1564 stream.Seek (0, SeekOrigin.End);
1565 Assert.AreEqual (-1, stream.ReadByte ());
1571 [ExpectedException (typeof (NotSupportedException))]
1572 public void SetLengthWithClosedBaseStream ()
1574 string fn = Path.Combine (TempFolder, "temp");
1576 FileStream fs = new FileStream (fn, FileMode.Create);
1577 BufferedStream bs = new BufferedStream (fs);
1580 bs.SetLength (1000);
1587 public void LengthAfterWrite ()
1589 string path = TempFolder + DSC + "oneofthefilescreated.txt";
1590 FileStream fs = null;
1593 fs = new FileStream (path, FileMode.CreateNew);
1594 fs.WriteByte (Convert.ToByte ('A'));
1595 byte [] buffer = Encoding.ASCII.GetBytes (" is a first character.");
1596 fs.Write (buffer, 0, buffer.Length);
1597 fs.Seek (0, SeekOrigin.Begin);
1598 char a = Convert.ToChar (fs.ReadByte ());
1599 Assert.AreEqual ('A', a, "#A1");
1600 Assert.AreEqual (23, fs.Length, "#A2");
1601 int nread = fs.Read (buffer, 0, 5);
1602 Assert.AreEqual (5, nread, "#A3");
1611 public void DeleteOnClose ()
1613 string path = TempFolder + DSC + "created.txt";
1615 FileStream fs = new FileStream (path, FileMode.CreateNew, FileAccess.Write, FileShare.None, 1024,
1616 FileOptions.DeleteOnClose);
1617 Assert.AreEqual (true, File.Exists (path), "DOC#1");
1619 Assert.AreEqual (false, File.Exists (path), "DOC#2");
1625 public void WriteWithExposedHandle ()
1627 string path = TempFolder + DSC + "exposed-handle.txt";
1628 FileStream fs1 = null;
1629 FileStream fs2 = null;
1633 fs1 = new FileStream (path, FileMode.Create);
1634 fs2 = new FileStream (fs1.SafeFileHandle, FileAccess.ReadWrite);
1635 fs1.WriteByte (Convert.ToByte ('H'));
1636 fs1.WriteByte (Convert.ToByte ('E'));
1637 fs1.WriteByte (Convert.ToByte ('L'));
1638 fs2.WriteByte (Convert.ToByte ('L'));
1639 fs2.WriteByte (Convert.ToByte ('O'));
1640 long fs1Pos = fs1.Position;
1646 var check = Encoding.ASCII.GetString (File.ReadAllBytes (path));
1647 Assert.AreEqual ("HELLO", check, "EXPOSED#1");
1648 Assert.AreEqual (5, fs1Pos, "EXPOSED#2");