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 #if !TARGET_JVM // No support IntPtr file handles under TARGET_JVM
710 [Test, ExpectedException (typeof (IOException))]
711 public void CtorIOException2 ()
713 FileStream stream = null;
715 stream = new FileStream (new IntPtr (Int32.MaxValue), FileAccess.Read);
723 [Category("TargetJvmNotSupported")] // File sharing not supported for TARGET_JVM
724 [Test, ExpectedException (typeof (IOException))]
725 public void CtorIOException ()
727 string path = TempFolder + DSC + "CTorIOException.Test";
728 FileStream stream = null;
729 FileStream stream2 = null;
733 stream = new FileStream (path, FileMode.CreateNew);
735 // used by an another process
736 stream2 = new FileStream (path, FileMode.OpenOrCreate);
747 public void CtorAccess1Read2Read ()
749 FileStream fs = null;
750 FileStream fs2 = null;
751 string fn = Path.Combine (TempFolder, "temp");
753 if (!File.Exists (fn)) {
754 TextWriter tw = File.CreateText (fn);
758 fs = new FileStream (fn, FileMode.Open, FileAccess.Read);
759 fs2 = new FileStream (fn, FileMode.Open, FileAccess.Read);
765 if (File.Exists (fn))
771 [Category("TargetJvmNotSupported")] // File sharing not supported for TARGET_JVM
772 [ExpectedException (typeof (IOException))]
773 public void CtorAccess1Read2Write ()
775 string fn = Path.Combine (TempFolder, "temp");
776 FileStream fs1 = null;
777 FileStream fs2 = null;
779 if (!File.Exists (fn)) {
780 using (TextWriter tw = File.CreateText (fn)) {
784 fs1 = new FileStream (fn, FileMode.Open, FileAccess.Read);
785 fs2 = new FileStream (fn, FileMode.Create, FileAccess.Write);
791 if (File.Exists (fn))
797 [Category("TargetJvmNotSupported")] // File sharing not supported for TARGET_JVM
798 [ExpectedException (typeof (IOException))]
799 public void CtorAccess1Write2Write ()
801 string fn = Path.Combine (TempFolder, "temp");
802 FileStream fs1 = null;
803 FileStream fs2 = null;
805 if (File.Exists (fn))
807 fs1 = new FileStream (fn, FileMode.Create, FileAccess.Write);
808 fs2 = new FileStream (fn, FileMode.Create, FileAccess.Write);
814 if (File.Exists (fn))
820 [ExpectedException (typeof (UnauthorizedAccessException))]
821 public void CtorReadDirectoryAsFile ()
823 FileStream stream = null;
825 stream = new FileStream (TempFolder, FileMode.Open, FileAccess.Read);
833 public void FileShare_Delete ()
835 string fn = Path.Combine (TempFolder, "temp");
837 using (Stream s = new FileStream (fn, FileMode.OpenOrCreate, FileAccess.Write, FileShare.Delete)) {
838 s.Write (new byte [1] { 0x5 }, 0, 1);
842 using (Stream s = new FileStream (fn, FileMode.OpenOrCreate, FileAccess.Write, FileShare.ReadWrite | FileShare.Delete)) {
843 s.Write (new byte [1] { 0x5 }, 0, 1);
847 using (Stream s = new FileStream (fn, FileMode.OpenOrCreate, FileAccess.Write, FileShare.Write | FileShare.Delete)) {
848 s.Write (new byte [1] { 0x5 }, 0, 1);
852 using (Stream s = new FileStream (fn, FileMode.OpenOrCreate, FileAccess.Write, FileShare.Read | FileShare.Delete)) {
853 s.Write (new byte [1] { 0x5 }, 0, 1);
857 using (Stream s = new FileStream (fn, FileMode.OpenOrCreate, FileAccess.Write, FileShare.Inheritable | FileShare.Delete)) {
858 s.Write (new byte [1] { 0x5 }, 0, 1);
866 string path = TempFolder + DSC + "FileStreamTest.Write";
870 FileStream stream = new FileStream (path, FileMode.CreateNew, FileAccess.ReadWrite, FileShare.ReadWrite, 8);
872 byte[] outbytes = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16 };
873 byte[] bytes = new byte[15];
875 // Check that the data is flushed when we overflow the buffer
876 // with a large amount of data
877 stream.Write (outbytes, 0, 5);
878 stream.Write (outbytes, 5, 10);
879 stream.Seek (0, SeekOrigin.Begin);
881 stream.Read (bytes, 0, 15);
882 for (int i = 0; i < 15; ++i)
883 Assert.AreEqual (i + 1, bytes[i], "#1");
885 // Check that the data is flushed when we overflow the buffer
886 // with a small amount of data
887 stream.Write (outbytes, 0, 7);
888 stream.Write (outbytes, 7, 7);
889 stream.Write (outbytes, 14, 1);
891 stream.Read (bytes, 0, 15);
892 stream.Seek (15, SeekOrigin.Begin);
893 for (int i = 0; i < 15; ++i)
894 Assert.AreEqual (i + 1, bytes[i], "#2");
899 public void Length ()
901 // Test that the Length property takes into account the data
903 string path = TempFolder + DSC + "FileStreamTest.Length";
907 FileStream stream = new FileStream (path, FileMode.CreateNew);
909 byte[] outbytes = new byte[] { 1, 2, 3, 4 };
911 stream.Write (outbytes, 0, 4);
912 Assert.AreEqual (4, stream.Length);
919 string path = TempFolder + DSC + "FileStreamTest.Flush";
920 FileStream stream = null;
921 FileStream stream2 = null;
926 stream = new FileStream (path, FileMode.CreateNew, FileAccess.ReadWrite, FileShare.ReadWrite);
927 stream2 = new FileStream (path, FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite);
929 stream.Write (new byte[] { 1, 2, 3, 4, 5 }, 0, 5);
931 byte[] bytes = new byte[5];
932 stream2.Read (bytes, 0, 5);
933 Assert.AreEqual (0, bytes[0], "#A1");
934 Assert.AreEqual (0, bytes[1], "#A2");
935 Assert.AreEqual (0, bytes[2], "#A3");
936 Assert.AreEqual (0, bytes[3], "#A4");
939 stream2.Read (bytes, 0, 5);
940 Assert.AreEqual (1, bytes[0], "#B1");
941 Assert.AreEqual (2, bytes[1], "#B2");
942 Assert.AreEqual (3, bytes[2], "#B3");
943 Assert.AreEqual (4, bytes[3], "#B4");
954 public void TestDefaultProperties ()
956 string path = TempFolder + Path.DirectorySeparatorChar + "testfilestream.tmp.2";
959 FileStream stream = new FileStream (path, FileMode.Create);
961 Assert.IsTrue (stream.CanRead, "#A1");
962 Assert.IsTrue (stream.CanSeek, "#A2");
963 Assert.IsTrue (stream.CanWrite, "#A3");
964 Assert.IsFalse (stream.IsAsync, "#A4");
965 Assert.IsTrue (stream.Name.EndsWith (path), "#A5");
966 Assert.AreEqual (0, stream.Position, "#A6");
967 Assert.AreEqual ("System.IO.FileStream", stream.ToString (), "#A7");
971 stream = new FileStream (path, FileMode.OpenOrCreate, FileAccess.Read);
972 Assert.IsTrue (stream.CanRead, "#B1");
973 Assert.IsTrue (stream.CanSeek, "#B2");
974 Assert.IsFalse (stream.CanWrite, "#B3");
975 Assert.IsFalse (stream.IsAsync, "#B4");
976 Assert.IsTrue (stream.Name.EndsWith (path), "#B5");
977 Assert.AreEqual (0, stream.Position, "#B6");
978 Assert.AreEqual ("System.IO.FileStream", stream.ToString (), "#B7");
981 stream = new FileStream (path, FileMode.Truncate, FileAccess.Write, FileShare.ReadWrite);
982 Assert.IsFalse (stream.CanRead, "#C1");
983 Assert.IsTrue (stream.CanSeek, "#C2");
984 Assert.IsTrue (stream.CanWrite, "#C3");
985 Assert.IsFalse (stream.IsAsync, "#C4");
986 Assert.IsTrue (stream.Name.EndsWith ("testfilestream.tmp.2"), "#C5");
987 Assert.AreEqual (0, stream.Position, "#C6");
988 Assert.AreEqual ("System.IO.FileStream", stream.ToString (), "#C7");
993 [Category ("NotWorking")]
994 // Bug: 71371 -> duplicate and WONTFIX.
995 public void TestLock_FailsOnMono ()
997 string path = TempFolder + Path.DirectorySeparatorChar + "TestLock";
1000 FileStream stream = new FileStream (path, FileMode.CreateNew, FileAccess.ReadWrite);
1002 stream.Write (new Byte[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }, 0, 10);
1005 stream = new FileStream (path, FileMode.Open, FileAccess.ReadWrite);
1009 FileStream stream2 = new FileStream (path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
1011 byte[] bytes = new byte[5];
1013 stream2.Read (bytes, 0, 5);
1015 } catch (Exception e) {
1016 Assert.AreEqual (typeof (IOException), e.GetType (), "#2");
1025 [Category("TargetJvmNotSupported")] // File locking not supported for TARGET_JVM
1026 public void TestLock ()
1028 string path = TempFolder + Path.DirectorySeparatorChar + "TestLock";
1031 FileStream stream = new FileStream (path, FileMode.CreateNew, FileAccess.ReadWrite);
1033 stream.Write (new Byte[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }, 0, 10);
1036 stream = new FileStream (path, FileMode.Open, FileAccess.ReadWrite);
1040 FileStream stream2 = new FileStream (path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
1042 byte[] bytes = new byte[5];
1044 stream2.Read (bytes, 0, 5);
1045 Assert.Fail ("#A1");
1046 } catch (Exception) {
1047 // Bug #71371: on MS.NET you get an IOException detailing a lock
1048 // Assert.AreEqual (typeof (IOException), e.GetType (), "#A2");
1051 stream2.Seek (5, SeekOrigin.Begin);
1052 stream2.Read (bytes, 0, 5);
1054 Assert.AreEqual (5, bytes[0], "#B1");
1055 Assert.AreEqual (6, bytes[1], "#B2");
1056 Assert.AreEqual (7, bytes[2], "#B3");
1057 Assert.AreEqual (8, bytes[3], "#B4");
1058 Assert.AreEqual (9, bytes[4], "#B5");
1060 stream.Unlock (0, 5);
1061 stream2.Seek (0, SeekOrigin.Begin);
1062 stream2.Read (bytes, 0, 5);
1064 Assert.AreEqual (0, bytes[0], "#C1");
1065 Assert.AreEqual (1, bytes[1], "#C2");
1066 Assert.AreEqual (2, bytes[2], "#C3");
1067 Assert.AreEqual (3, bytes[3], "#C4");
1068 Assert.AreEqual (4, bytes[4], "#C5");
1079 string path = TempFolder + DSC + "FST.Seek.Test";
1082 FileStream stream = new FileStream (path, FileMode.CreateNew, FileAccess.ReadWrite, FileShare.ReadWrite);
1083 FileStream stream2 = new FileStream (path, FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite);
1085 stream.Write (new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 10 }, 0, 9);
1086 Assert.AreEqual (5, stream2.Seek (5, SeekOrigin.Begin), "#1");
1087 Assert.AreEqual (-1, stream2.ReadByte (), "#2");
1089 Assert.AreEqual (2, stream2.Seek (-3, SeekOrigin.Current), "#3");
1090 Assert.AreEqual (-1, stream2.ReadByte (), "#4");
1092 Assert.AreEqual (12, stream.Seek (3, SeekOrigin.Current), "#5");
1093 Assert.AreEqual (-1, stream.ReadByte (), "#6");
1095 Assert.AreEqual (5, stream.Seek (-7, SeekOrigin.Current), "#7");
1096 Assert.AreEqual (6, stream.ReadByte (), "#8");
1098 Assert.AreEqual (5, stream2.Seek (5, SeekOrigin.Begin), "#9");
1099 Assert.AreEqual (6, stream2.ReadByte (), "#10");
1107 public void TestSeek ()
1109 string path = TempFolder + Path.DirectorySeparatorChar + "TestSeek";
1112 FileStream stream = new FileStream (path, FileMode.CreateNew, FileAccess.ReadWrite, FileShare.ReadWrite);
1113 stream.Write (new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }, 0, 10);
1115 stream.Seek (5, SeekOrigin.End);
1116 Assert.AreEqual (-1, stream.ReadByte (), "#1");
1118 stream.Seek (-5, SeekOrigin.End);
1119 Assert.AreEqual (6, stream.ReadByte (), "#2");
1122 stream.Seek (-11, SeekOrigin.End);
1124 } catch (Exception e) {
1125 Assert.AreEqual (typeof (IOException), e.GetType (), "#4");
1128 stream.Seek (19, SeekOrigin.Begin);
1129 Assert.AreEqual (-1, stream.ReadByte (), "#5");
1131 stream.Seek (1, SeekOrigin.Begin);
1132 Assert.AreEqual (2, stream.ReadByte (), "#6");
1134 stream.Seek (3, SeekOrigin.Current);
1135 Assert.AreEqual (6, stream.ReadByte (), "#7");
1137 stream.Seek (-2, SeekOrigin.Current);
1138 Assert.AreEqual (5, stream.ReadByte (), "#8");
1142 // Test that seeks work correctly when seeking inside the buffer
1143 stream.Seek (0, SeekOrigin.Begin);
1144 stream.WriteByte (0);
1145 stream.WriteByte (1);
1146 stream.Seek (0, SeekOrigin.Begin);
1147 byte[] buf = new byte[1];
1149 stream.Write (buf, 0, 1);
1150 stream.Write (buf, 0, 1);
1152 stream.Seek (0, SeekOrigin.Begin);
1153 Assert.AreEqual (2, stream.ReadByte (), "#9");
1154 Assert.AreEqual (2, stream.ReadByte (), "#10");
1161 public void TestClose ()
1163 string path = TempFolder + Path.DirectorySeparatorChar + "TestClose";
1166 FileStream stream = new FileStream (path, FileMode.CreateNew, FileAccess.ReadWrite);
1168 stream.Write (new byte[] { 1, 2, 3, 4 }, 0, 4);
1174 Assert.Fail ("#A1");
1175 } catch (Exception e) {
1176 Assert.AreEqual (typeof (ObjectDisposedException), e.GetType (), "#A2");
1180 stream.WriteByte (64);
1181 Assert.Fail ("#B1");
1182 } catch (Exception e) {
1183 Assert.AreEqual (typeof (ObjectDisposedException), e.GetType (), "#B2");
1188 Assert.Fail ("#C1");
1189 } catch (Exception e) {
1190 Assert.AreEqual (typeof (ObjectDisposedException), e.GetType (), "#C2");
1194 long l = stream.Length;
1195 Assert.Fail ("#D1");
1196 } catch (Exception e) {
1197 Assert.AreEqual (typeof (ObjectDisposedException), e.GetType (), "#D2");
1201 long l = stream.Position;
1202 Assert.Fail ("#E1");
1203 } catch (Exception e) {
1204 Assert.AreEqual (typeof (ObjectDisposedException), e.GetType (), "#E2");
1207 Assert.IsFalse (stream.CanRead, "#F1");
1208 Assert.IsFalse (stream.CanSeek, "#F2");
1209 Assert.IsFalse (stream.CanWrite, "#F3");
1210 Assert.IsTrue (stream.Name.EndsWith (path), "#F4");
1217 /// Checks whether the <see cref="FileStream" /> throws a <see cref="NotSupportedException" />
1218 /// when the stream is opened with access mode <see cref="FileAccess.Read" /> and the
1219 /// <see cref="FileStream.Write(byte[], int, int)" /> method is called.
1222 [ExpectedException (typeof (NotSupportedException))]
1223 public void TestWriteVerifyAccessMode ()
1225 string path = TempFolder + Path.DirectorySeparatorChar + "temp";
1228 FileStream stream = null;
1232 buffer = Encoding.ASCII.GetBytes ("test");
1233 stream = new FileStream (path, FileMode.OpenOrCreate, FileAccess.Read);
1234 stream.Write (buffer, 0, buffer.Length);
1243 /// Checks whether the <see cref="FileStream" /> throws a <see cref="NotSupportedException" />
1244 /// when the stream is opened with access mode <see cref="FileAccess.Read" /> and the
1245 /// <see cref="FileStream.WriteByte(byte)" /> method is called.
1248 [ExpectedException (typeof (NotSupportedException))]
1249 public void TestWriteByteVerifyAccessMode ()
1251 string path = TempFolder + Path.DirectorySeparatorChar + "temp";
1254 FileStream stream = null;
1257 stream = new FileStream (path, FileMode.OpenOrCreate, FileAccess.Read);
1258 stream.WriteByte (Byte.MinValue);
1267 /// Checks whether the <see cref="FileStream" /> throws a <see cref="NotSupportedException" />
1268 /// when the stream is opened with access mode <see cref="FileAccess.Write" /> and the
1269 /// <see cref="FileStream.Read(byte[], int, int)" /> method is called.
1272 [ExpectedException (typeof (NotSupportedException))]
1273 public void TestReadVerifyAccessMode ()
1275 string path = TempFolder + Path.DirectorySeparatorChar + "temp";
1278 FileStream stream = null;
1279 byte[] buffer = new byte[100];
1282 stream = new FileStream (path, FileMode.OpenOrCreate, FileAccess.Write);
1283 stream.Read (buffer, 0, buffer.Length);
1291 /// Checks whether the <see cref="FileStream" /> throws a <see cref="NotSupportedException" />
1292 /// when the stream is opened with access mode <see cref="FileAccess.Write" /> and the
1293 /// <see cref="FileStream.ReadByte()" /> method is called.
1296 [ExpectedException (typeof (NotSupportedException))]
1297 public void TestReadByteVerifyAccessMode ()
1299 string path = TempFolder + Path.DirectorySeparatorChar + "temp";
1302 FileStream stream = null;
1305 stream = new FileStream (path, FileMode.OpenOrCreate, FileAccess.Write);
1306 int readByte = stream.ReadByte ();
1314 #if !TARGET_JVM // No support IntPtr file handles under TARGET_JVM
1315 // Check that the stream is flushed even when it doesn't own the
1318 public void TestFlushNotOwningHandle ()
1320 string path = Path.Combine (TempFolder, "TestFlushNotOwningHandle");
1323 FileStream s = new FileStream (path, FileMode.Create);
1324 using (FileStream s2 = new FileStream (s.Handle, FileAccess.Write, false)) {
1325 byte[] buf = new byte[2];
1327 s2.Write (buf, 0, 1);
1331 Assert.AreEqual ((int) '1', s.ReadByte ());
1334 #endif // TARGET_JVM
1336 private void DeleteFile (string path)
1338 if (File.Exists (path))
1343 [ExpectedException (typeof (ArgumentOutOfRangeException))]
1344 public void Read_OffsetNegative ()
1346 string path = TempFolder + Path.DirectorySeparatorChar + "temp";
1349 using (FileStream stream = new FileStream (path, FileMode.OpenOrCreate, FileAccess.Read)) {
1350 stream.Read (new byte[0], -1, 1);
1355 [ExpectedException (typeof (ArgumentException))]
1356 public void Read_OffsetOverflow ()
1358 string path = TempFolder + Path.DirectorySeparatorChar + "temp";
1361 using (FileStream stream = new FileStream (path, FileMode.OpenOrCreate, FileAccess.Read)) {
1362 stream.Read (new byte[0], Int32.MaxValue, 1);
1367 [ExpectedException (typeof (ArgumentOutOfRangeException))]
1368 public void Read_CountNegative ()
1370 string path = TempFolder + Path.DirectorySeparatorChar + "temp";
1373 using (FileStream stream = new FileStream (path, FileMode.OpenOrCreate, FileAccess.Read)) {
1374 stream.Read (new byte[0], 1, -1);
1379 [ExpectedException (typeof (ArgumentException))]
1380 public void Read_CountOverflow ()
1382 string path = TempFolder + Path.DirectorySeparatorChar + "temp";
1385 using (FileStream stream = new FileStream (path, FileMode.OpenOrCreate, FileAccess.Read)) {
1386 stream.Read (new byte[0], 1, Int32.MaxValue);
1391 [ExpectedException (typeof (ArgumentOutOfRangeException))]
1392 public void Write_OffsetNegative ()
1394 string path = TempFolder + Path.DirectorySeparatorChar + "temp";
1397 using (FileStream stream = new FileStream (path, FileMode.OpenOrCreate, FileAccess.Write)) {
1398 stream.Write (new byte[0], -1, 1);
1403 [ExpectedException (typeof (ArgumentException))]
1404 public void Write_OffsetOverflow ()
1406 string path = TempFolder + Path.DirectorySeparatorChar + "temp";
1409 using (FileStream stream = new FileStream (path, FileMode.OpenOrCreate, FileAccess.Write)) {
1410 stream.Write (new byte[0], Int32.MaxValue, 1);
1415 [ExpectedException (typeof (ArgumentOutOfRangeException))]
1416 public void Write_CountNegative ()
1418 string path = TempFolder + Path.DirectorySeparatorChar + "temp";
1421 using (FileStream stream = new FileStream (path, FileMode.OpenOrCreate, FileAccess.Write)) {
1422 stream.Write (new byte[0], 1, -1);
1427 [ExpectedException (typeof (ArgumentException))]
1428 public void Write_CountOverflow ()
1430 string path = TempFolder + Path.DirectorySeparatorChar + "temp";
1433 using (FileStream stream = new FileStream (path, FileMode.OpenOrCreate, FileAccess.Write)) {
1434 stream.Write (new byte[0], 1, Int32.MaxValue);
1439 [ExpectedException (typeof (ArgumentException))]
1440 public void Seek_InvalidSeekOrigin ()
1442 string path = TempFolder + Path.DirectorySeparatorChar + "temp";
1445 using (FileStream stream = new FileStream (path, FileMode.OpenOrCreate, FileAccess.Read)) {
1446 stream.Seek (0, (SeekOrigin) (-1));
1450 #if !TARGET_JVM // No support IntPtr file handles under TARGET_JVM
1452 [ExpectedException (typeof (ArgumentException))]
1453 public void Constructor_InvalidFileHandle ()
1455 new FileStream ((IntPtr) (-1L), FileAccess.Read);
1457 #endif // TARGET_JVM
1460 public void PositionAfterSetLength ()
1462 string path = TempFolder + Path.DirectorySeparatorChar + "temp";
1465 using (FileStream stream = new FileStream (path, FileMode.OpenOrCreate, FileAccess.Write)) {
1466 stream.SetLength (32);
1467 stream.Position = 32;
1468 stream.SetLength (16);
1469 Assert.AreEqual (16, stream.Position);
1474 [ExpectedException (typeof (ObjectDisposedException))]
1475 public void SetLength_Disposed ()
1477 string path = TempFolder + Path.DirectorySeparatorChar + "temp";
1479 FileStream stream = new FileStream (path, FileMode.OpenOrCreate, FileAccess.Write);
1481 stream.SetLength (16);
1485 [ExpectedException (typeof (ObjectDisposedException))]
1486 public void Position_Disposed ()
1488 string path = TempFolder + Path.DirectorySeparatorChar + "temp";
1490 FileStream stream = new FileStream (path, FileMode.OpenOrCreate, FileAccess.Read);
1492 stream.Position = 0;
1496 [Category("TargetJvmNotSupported")] // Async IO not supported for TARGET_JVM
1497 [ExpectedException (typeof (ObjectDisposedException))]
1498 public void BeginRead_Disposed ()
1500 string path = TempFolder + Path.DirectorySeparatorChar + "temp";
1502 FileStream stream = new FileStream (path, FileMode.OpenOrCreate, FileAccess.Read);
1504 stream.EndRead (stream.BeginRead (new byte[8], 0, 8, null, null));
1508 [Category("TargetJvmNotSupported")] // Async IO not supported for TARGET_JVM
1509 [ExpectedException (typeof (ObjectDisposedException))]
1510 public void BeginWrite_Disposed ()
1512 string path = TempFolder + Path.DirectorySeparatorChar + "temp";
1514 FileStream stream = new FileStream (path, FileMode.OpenOrCreate, FileAccess.Write);
1516 stream.EndWrite (stream.BeginWrite (new byte[8], 0, 8, null, null));
1519 static IAsyncResult DoBeginWrite(Stream stream, ManualResetEvent mre, byte[] RandomBuffer)
1521 return stream.BeginWrite (RandomBuffer, 0, RandomBuffer.Length, ar => {
1522 stream.EndWrite (ar);
1524 // we don't supply an ManualResetEvent so this will throw an NRE on the second run
1525 // which nunit-console will ignore (but other test runners don't like that)
1529 DoBeginWrite (stream, null, RandomBuffer).AsyncWaitHandle.WaitOne ();
1535 public void BeginWrite_Recursive ()
1537 string path = TempFolder + Path.DirectorySeparatorChar + "temp";
1540 using (FileStream stream = new FileStream (path, FileMode.OpenOrCreate, FileAccess.Write)) {
1541 var mre = new ManualResetEvent (false);
1542 var RandomBuffer = new byte[1024];
1543 DoBeginWrite (stream, mre, RandomBuffer);
1544 Assert.IsTrue (mre.WaitOne (5000), "#1");
1549 [Category("TargetJvmNotSupported")] // File locking not supported for TARGET_JVM
1550 [ExpectedException (typeof (ObjectDisposedException))]
1551 public void Lock_Disposed ()
1553 string path = TempFolder + Path.DirectorySeparatorChar + "temp";
1555 FileStream stream = new FileStream (path, FileMode.OpenOrCreate, FileAccess.Write);
1561 [Category("TargetJvmNotSupported")] // File locking not supported for TARGET_JVM
1562 [ExpectedException (typeof (ObjectDisposedException))]
1563 public void Unlock_Disposed ()
1565 string path = TempFolder + Path.DirectorySeparatorChar + "temp";
1567 FileStream stream = new FileStream (path, FileMode.OpenOrCreate, FileAccess.Write);
1569 stream.Unlock (0, 1);
1573 public void ReadBytePastEndOfStream ()
1575 string path = TempFolder + Path.DirectorySeparatorChar + "temp";
1577 using (FileStream stream = new FileStream (path, FileMode.OpenOrCreate, FileAccess.Read)) {
1578 stream.Seek (0, SeekOrigin.End);
1579 Assert.AreEqual (-1, stream.ReadByte ());
1585 [ExpectedException (typeof (NotSupportedException))]
1586 public void SetLengthWithClosedBaseStream ()
1588 string fn = Path.Combine (TempFolder, "temp");
1590 FileStream fs = new FileStream (fn, FileMode.Create);
1591 BufferedStream bs = new BufferedStream (fs);
1594 bs.SetLength (1000);
1601 public void LengthAfterWrite ()
1603 string path = TempFolder + DSC + "oneofthefilescreated.txt";
1604 FileStream fs = null;
1607 fs = new FileStream (path, FileMode.CreateNew);
1608 fs.WriteByte (Convert.ToByte ('A'));
1609 byte [] buffer = Encoding.ASCII.GetBytes (" is a first character.");
1610 fs.Write (buffer, 0, buffer.Length);
1611 fs.Seek (0, SeekOrigin.Begin);
1612 char a = Convert.ToChar (fs.ReadByte ());
1613 Assert.AreEqual ('A', a, "#A1");
1614 Assert.AreEqual (23, fs.Length, "#A2");
1615 int nread = fs.Read (buffer, 0, 5);
1616 Assert.AreEqual (5, nread, "#A3");
1624 [Category("TargetJvmNotSupported")] // FileOptions.DeleteOnClose not supported for TARGET_JVM
1626 public void DeleteOnClose ()
1628 string path = TempFolder + DSC + "created.txt";
1630 FileStream fs = new FileStream (path, FileMode.CreateNew, FileAccess.Write, FileShare.None, 1024,
1631 FileOptions.DeleteOnClose);
1632 Assert.AreEqual (true, File.Exists (path), "DOC#1");
1634 Assert.AreEqual (false, File.Exists (path), "DOC#2");
1640 public void WriteWithExposedHandle ()
1642 string path = TempFolder + DSC + "exposed-handle.txt";
1643 FileStream fs1 = null;
1644 FileStream fs2 = null;
1648 fs1 = new FileStream (path, FileMode.Create);
1649 fs2 = new FileStream (fs1.SafeFileHandle, FileAccess.ReadWrite);
1650 fs1.WriteByte (Convert.ToByte ('H'));
1651 fs1.WriteByte (Convert.ToByte ('E'));
1652 fs1.WriteByte (Convert.ToByte ('L'));
1653 fs2.WriteByte (Convert.ToByte ('L'));
1654 fs2.WriteByte (Convert.ToByte ('O'));
1655 long fs1Pos = fs1.Position;
1661 var check = Encoding.ASCII.GetString (File.ReadAllBytes (path));
1662 Assert.AreEqual ("HELLO", check, "EXPOSED#1");
1663 Assert.AreEqual (5, fs1Pos, "EXPOSED#2");