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;
30 public void TearDown ()
32 if (Directory.Exists (TempFolder))
33 Directory.Delete (TempFolder, true);
39 if (Directory.Exists (TempFolder))
40 Directory.Delete (TempFolder, true);
42 Directory.CreateDirectory (TempFolder);
45 public void TestCtr ()
47 string path = TempFolder + DSC + "testfilestream.tmp.1";
49 FileStream stream = null;
51 stream = new FileStream (path, FileMode.Create);
61 [ExpectedException (typeof (ArgumentException))]
62 public void CtorArgumentException1 ()
65 stream = new FileStream ("", FileMode.Create);
70 [ExpectedException (typeof (ArgumentNullException))]
71 public void CtorArgumentNullException ()
73 FileStream stream = new FileStream (null, FileMode.Create);
78 public void CtorFileNotFoundException_Mode_Open ()
80 const string file_name = "thisfileshouldnotexist.test";
82 string path = TempFolder + DSC + file_name;
84 FileStream stream = null;
86 stream = new FileStream (TempFolder + DSC + file_name, FileMode.Open);
88 } catch (FileNotFoundException ex) {
89 Assert.AreEqual (typeof (FileNotFoundException), ex.GetType (), "#A2");
90 Assert.AreEqual (path, ex.FileName, "#A3");
91 Assert.IsNull (ex.InnerException, "#A4");
92 Assert.IsNotNull (ex.Message, "#A5");
93 Assert.IsTrue (ex.Message.IndexOf (path) != -1, "#A6");
103 string orignalCurrentDir = Directory.GetCurrentDirectory ();
104 Directory.SetCurrentDirectory (TempFolder);
106 // If TempFolder is a symlink, Mono will follow it and ex.FileName below will contain
107 // the real directory name, not that of the TempFolder symlink and the test will fail
108 // (happens e.g. on Android M)
109 string realTempDir = Directory.GetCurrentDirectory ();
110 path = realTempDir + DSC + file_name;
113 stream = new FileStream (file_name, FileMode.Open);
115 } catch (FileNotFoundException ex) {
116 Assert.AreEqual (typeof (FileNotFoundException), ex.GetType (), "#B2");
117 Assert.AreEqual (path, ex.FileName, "#B3");
118 Assert.IsNull (ex.InnerException, "#B4");
119 Assert.IsNotNull (ex.Message, "#B5");
120 Assert.IsTrue (ex.Message.IndexOf (path) != -1, "#B6");
122 // restore original current directory
123 Directory.SetCurrentDirectory (orignalCurrentDir);
131 public void CtorFileNotFoundException_Mode_Truncate ()
133 const string file_name = "thisfileshouldNOTexist.test";
135 string path = TempFolder + DSC + file_name;
137 FileStream stream = null;
139 stream = new FileStream (path, FileMode.Truncate);
141 } catch (FileNotFoundException ex) {
142 Assert.AreEqual (typeof (FileNotFoundException), ex.GetType (), "#A2");
143 Assert.AreEqual (path, ex.FileName, "#A3");
144 Assert.IsNull (ex.InnerException, "#A4");
145 Assert.IsNotNull (ex.Message, "#A5");
146 Assert.IsTrue (ex.Message.IndexOf (path) != -1, "#A6");
148 if (stream != null) {
156 string orignalCurrentDir = Directory.GetCurrentDirectory ();
157 Directory.SetCurrentDirectory (TempFolder);
159 // If TempFolder is a symlink, Mono will follow it and ex.FileName below will contain
160 // the real directory name, not that of the TempFolder symlink and the test will fail
161 // (happens e.g. on Android M)
162 string realTempDir = Directory.GetCurrentDirectory ();
163 path = realTempDir + DSC + file_name;
166 stream = new FileStream (file_name, FileMode.Truncate);
168 } catch (FileNotFoundException ex) {
169 Assert.AreEqual (typeof (FileNotFoundException), ex.GetType (), "#B2");
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 ()
186 const string file_name = "thisfileshouldexists.test";
188 string path = TempFolder + DSC + file_name;
189 FileStream stream = null;
192 stream = new FileStream (path, FileMode.CreateNew);
195 stream = new FileStream (path, FileMode.CreateNew);
197 } catch (IOException ex) {
198 Assert.AreEqual (typeof (IOException), ex.GetType (), "#A2");
199 Assert.IsNull (ex.InnerException, "#A3");
200 Assert.IsNotNull (ex.Message, "#A4");
201 Assert.IsTrue (ex.Message.IndexOf (path) != -1, "#A5");
203 if (stream != null) {
211 string orignalCurrentDir = Directory.GetCurrentDirectory ();
212 Directory.SetCurrentDirectory (TempFolder);
214 // If TempFolder is a symlink, Mono will follow it and ex.FileName below will contain
215 // the real directory name, not that of the TempFolder symlink and the test will fail
216 // (happens e.g. on Android M)
217 string realTempDir = Directory.GetCurrentDirectory ();
218 path = realTempDir + DSC + file_name;
221 stream = new FileStream (file_name, FileMode.CreateNew);
224 stream = new FileStream (file_name, FileMode.CreateNew);
226 } catch (IOException ex) {
227 Assert.AreEqual (typeof (IOException), ex.GetType (), "#B2");
228 Assert.IsNull (ex.InnerException, "#B3");
229 Assert.IsNotNull (ex.Message, "#B4");
230 Assert.IsTrue (ex.Message.IndexOf (path) != -1, "#B5");
232 // restore original current directory
233 Directory.SetCurrentDirectory (orignalCurrentDir);
241 [ExpectedException (typeof (ArgumentOutOfRangeException))]
242 public void CtorArgumentOutOfRangeException1 ()
244 FileStream stream = null;
245 string path = TempFolder + Path.DirectorySeparatorChar + "temp";
248 stream = new FileStream (path, FileMode.Append | FileMode.CreateNew);
257 [ExpectedException (typeof (ArgumentOutOfRangeException))]
258 public void CtorArgumentOutOfRangeException2 ()
260 FileStream stream = null;
261 string path = TempFolder + Path.DirectorySeparatorChar + "temp";
264 stream = new FileStream ("test.test.test", FileMode.Append | FileMode.Open);
272 private void CtorDirectoryNotFoundException (FileMode mode)
274 string path = TempFolder + DSC + "thisDirectoryShouldNotExists";
275 if (Directory.Exists (path))
276 Directory.Delete (path, true);
278 FileStream stream = null;
280 stream = new FileStream (path + DSC + "eitherthisfile.test", mode);
286 if (Directory.Exists (path))
287 Directory.Delete (path, true);
292 [ExpectedException (typeof (DirectoryNotFoundException))]
293 public void CtorDirectoryNotFoundException_CreateNew ()
295 CtorDirectoryNotFoundException (FileMode.CreateNew);
299 [ExpectedException (typeof (DirectoryNotFoundException))]
300 public void CtorDirectoryNotFoundException_Create ()
302 CtorDirectoryNotFoundException (FileMode.Create);
306 [ExpectedException (typeof (DirectoryNotFoundException))]
307 public void CtorDirectoryNotFoundException_Open ()
309 CtorDirectoryNotFoundException (FileMode.Open);
313 [ExpectedException (typeof (DirectoryNotFoundException))]
314 public void CtorDirectoryNotFoundException_OpenOrCreate ()
316 CtorDirectoryNotFoundException (FileMode.OpenOrCreate);
320 [ExpectedException (typeof (DirectoryNotFoundException))]
321 public void CtorDirectoryNotFoundException_Truncate ()
323 CtorDirectoryNotFoundException (FileMode.Truncate);
327 [ExpectedException (typeof (DirectoryNotFoundException))]
328 public void CtorDirectoryNotFoundException_Append ()
330 CtorDirectoryNotFoundException (FileMode.Append);
334 public void CtorDirectoryNotFound_RelativePath ()
336 string orignalCurrentDir = Directory.GetCurrentDirectory ();
337 Directory.SetCurrentDirectory (TempFolder);
339 // If TempFolder is a symlink, Mono will follow it and ex.FileName below will contain
340 // the real directory name, not that of the TempFolder symlink and the test will fail
341 // (happens e.g. on Android M)
342 string realTempDir = Directory.GetCurrentDirectory ();
344 string relativePath = "DirectoryDoesNotExist" + Path.DirectorySeparatorChar + "file.txt";
345 string fullPath = Path.Combine (realTempDir, relativePath);
347 new FileStream (relativePath, FileMode.Open);
349 } catch (DirectoryNotFoundException ex) {
350 Assert.AreEqual (typeof (DirectoryNotFoundException), ex.GetType (), "#A2");
351 Assert.IsNull (ex.InnerException, "#A3");
352 Assert.IsNotNull (ex.Message, "#A4");
353 Assert.IsTrue (ex.Message.IndexOf (fullPath) != -1, "#A5");
355 // restore original current directory
356 Directory.SetCurrentDirectory (orignalCurrentDir);
361 // FileShare.Inheritable is ignored, but file does not exist
362 [ExpectedException (typeof (FileNotFoundException))]
363 public void CtorArgumentOutOfRangeException3 ()
365 string path = TempFolder + DSC + "CtorArgumentOutOfRangeException1";
368 FileStream stream = null;
370 stream = new FileStream (path, FileMode.Open, FileAccess.Read, FileShare.Inheritable);
379 [ExpectedException (typeof (ArgumentOutOfRangeException))]
380 public void CtorArgumentOutOfRangeException4 ()
382 string path = TempFolder + DSC + "CtorArgumentOutOfRangeException4";
385 FileStream stream = null;
387 stream = new FileStream (path, FileMode.OpenOrCreate, FileAccess.Read, FileShare.ReadWrite, -1);
396 [ExpectedException (typeof (ArgumentOutOfRangeException))]
397 public void CtorBufferSizeZero ()
399 // Buffer size can't be zero
401 string path = Path.Combine (TempFolder, "CtorBufferSizeZero");
404 FileStream stream = null;
406 stream = new FileStream (path, FileMode.CreateNew, FileAccess.Write, FileShare.ReadWrite, 0);
415 [ExpectedException (typeof (ArgumentException))]
416 public void CtorArgumentException2 ()
418 // FileMode.CreateNew && FileAccess.Read
420 string path = TempFolder + Path.DirectorySeparatorChar + "temp";
421 FileStream stream = null;
426 stream = new FileStream (".test.test.test.2", FileMode.CreateNew, FileAccess.Read, FileShare.None | FileShare.Write);
437 // FileShare.Inheritable is ignored, but file does not exist
438 [ExpectedException (typeof (FileNotFoundException))]
439 public void CtorArgumentOutOfRangeException5 ()
441 string path = TempFolder + Path.DirectorySeparatorChar + "temp";
444 FileStream stream = null;
446 stream = new FileStream (path, FileMode.Open, FileAccess.Read, FileShare.Inheritable | FileShare.ReadWrite);
456 [ExpectedException (typeof (ArgumentException))]
457 public void CtorArgumentException3 ()
459 string path = TempFolder + Path.DirectorySeparatorChar + "temp";
460 FileStream stream = null;
465 stream = new FileStream (".test.test.test.2", FileMode.Truncate, FileAccess.Read);
475 public void ModeAndAccessCombinations ()
477 string path = TempFolder + Path.DirectorySeparatorChar + "temp";
479 FileStream stream = null;
483 // Append access can be requested only in write-only mode
484 stream = new FileStream (path, FileMode.Append, FileAccess.Read);
486 } catch (ArgumentException ex) {
487 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#A2");
495 // Append / ReadWrite
497 // Append access can be requested only in write-only mode
498 stream = new FileStream (path, FileMode.Append, FileAccess.ReadWrite);
500 } catch (ArgumentException ex) {
501 // make sure it is exact this exception, and not a derived type
502 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#B2");
512 stream = new FileStream (path, FileMode.Append, FileAccess.Write);
522 stream = new FileStream (path, FileMode.Create, FileAccess.Read);
524 } catch (ArgumentException ex) {
525 // make sure it is exact this exception, and not a derived type
526 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#C2");
534 // Create / ReadWrite
536 stream = new FileStream (path, FileMode.Create, FileAccess.ReadWrite);
546 stream = new FileStream (path, FileMode.Create, FileAccess.Write);
556 stream = new FileStream (path, FileMode.CreateNew, FileAccess.Read);
558 } catch (ArgumentException ex) {
559 // make sure it is exact this exception, and not a derived type
560 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#D2");
568 // CreateNew / ReadWrite
570 stream = new FileStream (path, FileMode.CreateNew, FileAccess.ReadWrite);
580 stream = new FileStream (path, FileMode.CreateNew, FileAccess.Write);
590 stream = new FileStream (path, FileMode.Open, FileAccess.Read);
592 } catch (FileNotFoundException ex) {
593 // make sure it is exact this exception, and not a derived type
594 Assert.AreEqual (typeof (FileNotFoundException), ex.GetType (), "#E2");
595 Assert.AreEqual (path, ex.FileName, "#E3");
596 Assert.IsNull (ex.InnerException, "#E4");
597 Assert.IsNotNull (ex.Message, "#E5");
598 Assert.IsTrue (ex.Message.IndexOf (path) != -1, "#E6");
608 stream = new FileStream (path, FileMode.Open, FileAccess.ReadWrite);
610 } catch (FileNotFoundException ex) {
611 // make sure it is exact this exception, and not a derived type
612 Assert.AreEqual (typeof (FileNotFoundException), ex.GetType (), "#F2");
613 Assert.AreEqual (path, ex.FileName, "#F3");
614 Assert.IsNull (ex.InnerException, "#F4");
615 Assert.IsNotNull (ex.Message, "#F5");
616 Assert.IsTrue (ex.Message.IndexOf (path) != -1, "#F6");
626 stream = new FileStream (path, FileMode.Open, FileAccess.Write);
628 } catch (FileNotFoundException ex) {
629 // make sure it is exact this exception, and not a derived type
630 Assert.AreEqual (typeof (FileNotFoundException), ex.GetType (), "#G2");
631 Assert.AreEqual (path, ex.FileName, "#G3");
632 Assert.IsNull (ex.InnerException, "#G4");
633 Assert.IsNotNull (ex.Message, "#G5");
634 Assert.IsTrue (ex.Message.IndexOf (path) != -1, "#G6");
642 // OpenOrCreate / Read
644 stream = new FileStream (path, FileMode.OpenOrCreate, FileAccess.Read);
652 // OpenOrCreate / ReadWrite
654 stream = new FileStream (path, FileMode.OpenOrCreate, FileAccess.ReadWrite);
662 // OpenOrCreate / Write
664 stream = new FileStream (path, FileMode.OpenOrCreate, FileAccess.Write);
674 stream = new FileStream (path, FileMode.Truncate, FileAccess.Read);
676 } catch (ArgumentException ex) {
677 // make sure it is exact this exception, and not a derived type
678 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#H2");
686 // Truncate / ReadWrite
688 stream = new FileStream (path, FileMode.Truncate, FileAccess.ReadWrite);
690 } catch (FileNotFoundException ex) {
691 // make sure it is exact this exception, and not a derived type
692 Assert.AreEqual (typeof (FileNotFoundException), ex.GetType (), "#I2");
693 Assert.AreEqual (path, ex.FileName, "#I3");
694 Assert.IsNull (ex.InnerException, "#I4");
695 Assert.IsNotNull (ex.Message, "#I5");
696 Assert.IsTrue (ex.Message.IndexOf (path) != -1, "#I6");
706 stream = new FileStream (path, FileMode.Truncate, FileAccess.Write);
708 } catch (FileNotFoundException ex) {
709 // make sure it is exact this exception, and not a derived type
710 Assert.AreEqual (typeof (FileNotFoundException), ex.GetType (), "#J2");
711 Assert.AreEqual (path, ex.FileName, "#J3");
712 Assert.IsNull (ex.InnerException, "#J4");
713 Assert.IsNotNull (ex.Message, "#J5");
714 Assert.IsTrue (ex.Message.IndexOf (path) != -1, "#J6");
723 [Test, ExpectedException (typeof (IOException))]
724 public void CtorIOException2 ()
726 FileStream stream = null;
728 stream = new FileStream (new IntPtr (Int32.MaxValue), FileAccess.Read);
735 [Test, ExpectedException (typeof (IOException))]
736 public void CtorIOException ()
738 string path = TempFolder + DSC + "CTorIOException.Test";
739 FileStream stream = null;
740 FileStream stream2 = null;
744 stream = new FileStream (path, FileMode.CreateNew);
746 // used by an another process
747 stream2 = new FileStream (path, FileMode.OpenOrCreate);
758 public void CtorAccess1Read2Read ()
760 FileStream fs = null;
761 FileStream fs2 = null;
762 string fn = Path.Combine (TempFolder, "temp");
764 if (!File.Exists (fn)) {
765 TextWriter tw = File.CreateText (fn);
769 fs = new FileStream (fn, FileMode.Open, FileAccess.Read);
770 fs2 = new FileStream (fn, FileMode.Open, FileAccess.Read);
776 if (File.Exists (fn))
782 [ExpectedException (typeof (IOException))]
783 public void CtorAccess1Read2Write ()
785 string fn = Path.Combine (TempFolder, "temp");
786 FileStream fs1 = null;
787 FileStream fs2 = null;
789 if (!File.Exists (fn)) {
790 using (TextWriter tw = File.CreateText (fn)) {
794 fs1 = new FileStream (fn, FileMode.Open, FileAccess.Read);
795 fs2 = new FileStream (fn, FileMode.Create, FileAccess.Write);
801 if (File.Exists (fn))
807 [ExpectedException (typeof (IOException))]
808 public void CtorAccess1Write2Write ()
810 string fn = Path.Combine (TempFolder, "temp");
811 FileStream fs1 = null;
812 FileStream fs2 = null;
814 if (File.Exists (fn))
816 fs1 = new FileStream (fn, FileMode.Create, FileAccess.Write);
817 fs2 = new FileStream (fn, FileMode.Create, FileAccess.Write);
823 if (File.Exists (fn))
829 [ExpectedException (typeof (UnauthorizedAccessException))]
830 public void CtorReadDirectoryAsFile ()
832 FileStream stream = null;
834 stream = new FileStream (TempFolder, FileMode.Open, FileAccess.Read);
842 public void FileShare_Delete ()
844 string fn = Path.Combine (TempFolder, "temp");
846 using (Stream s = new FileStream (fn, FileMode.OpenOrCreate, FileAccess.Write, FileShare.Delete)) {
847 s.Write (new byte [1] { 0x5 }, 0, 1);
851 using (Stream s = new FileStream (fn, FileMode.OpenOrCreate, FileAccess.Write, FileShare.ReadWrite | FileShare.Delete)) {
852 s.Write (new byte [1] { 0x5 }, 0, 1);
856 using (Stream s = new FileStream (fn, FileMode.OpenOrCreate, FileAccess.Write, FileShare.Write | FileShare.Delete)) {
857 s.Write (new byte [1] { 0x5 }, 0, 1);
861 using (Stream s = new FileStream (fn, FileMode.OpenOrCreate, FileAccess.Write, FileShare.Read | FileShare.Delete)) {
862 s.Write (new byte [1] { 0x5 }, 0, 1);
866 using (Stream s = new FileStream (fn, FileMode.OpenOrCreate, FileAccess.Write, FileShare.Inheritable | FileShare.Delete)) {
867 s.Write (new byte [1] { 0x5 }, 0, 1);
875 string path = TempFolder + DSC + "FileStreamTest.Write";
879 FileStream stream = new FileStream (path, FileMode.CreateNew, FileAccess.ReadWrite, FileShare.ReadWrite, 8);
881 byte[] outbytes = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16 };
882 byte[] bytes = new byte[15];
884 // Check that the data is flushed when we overflow the buffer
885 // with a large amount of data
886 stream.Write (outbytes, 0, 5);
887 stream.Write (outbytes, 5, 10);
888 stream.Seek (0, SeekOrigin.Begin);
890 stream.Read (bytes, 0, 15);
891 for (int i = 0; i < 15; ++i)
892 Assert.AreEqual (i + 1, bytes[i], "#1");
894 // Check that the data is flushed when we overflow the buffer
895 // with a small amount of data
896 stream.Write (outbytes, 0, 7);
897 stream.Write (outbytes, 7, 7);
898 stream.Write (outbytes, 14, 1);
900 stream.Read (bytes, 0, 15);
901 stream.Seek (15, SeekOrigin.Begin);
902 for (int i = 0; i < 15; ++i)
903 Assert.AreEqual (i + 1, bytes[i], "#2");
908 public void Length ()
910 // Test that the Length property takes into account the data
912 string path = TempFolder + DSC + "FileStreamTest.Length";
916 FileStream stream = new FileStream (path, FileMode.CreateNew);
918 byte[] outbytes = new byte[] { 1, 2, 3, 4 };
920 stream.Write (outbytes, 0, 4);
921 Assert.AreEqual (4, stream.Length);
928 string path = TempFolder + DSC + "FileStreamTest.Flush";
929 FileStream stream = null;
930 FileStream stream2 = null;
935 stream = new FileStream (path, FileMode.CreateNew, FileAccess.ReadWrite, FileShare.ReadWrite);
936 stream2 = new FileStream (path, FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite);
938 stream.Write (new byte[] { 1, 2, 3, 4, 5 }, 0, 5);
940 byte[] bytes = new byte[5];
941 stream2.Read (bytes, 0, 5);
942 Assert.AreEqual (0, bytes[0], "#A1");
943 Assert.AreEqual (0, bytes[1], "#A2");
944 Assert.AreEqual (0, bytes[2], "#A3");
945 Assert.AreEqual (0, bytes[3], "#A4");
948 stream2.Read (bytes, 0, 5);
949 Assert.AreEqual (1, bytes[0], "#B1");
950 Assert.AreEqual (2, bytes[1], "#B2");
951 Assert.AreEqual (3, bytes[2], "#B3");
952 Assert.AreEqual (4, bytes[3], "#B4");
963 public void TestDefaultProperties ()
965 string path = TempFolder + Path.DirectorySeparatorChar + "testfilestream.tmp.2";
968 FileStream stream = new FileStream (path, FileMode.Create);
970 Assert.IsTrue (stream.CanRead, "#A1");
971 Assert.IsTrue (stream.CanSeek, "#A2");
972 Assert.IsTrue (stream.CanWrite, "#A3");
973 Assert.IsFalse (stream.IsAsync, "#A4");
974 Assert.IsTrue (stream.Name.EndsWith (path), "#A5");
975 Assert.AreEqual (0, stream.Position, "#A6");
976 Assert.AreEqual ("System.IO.FileStream", stream.ToString (), "#A7");
980 stream = new FileStream (path, FileMode.OpenOrCreate, FileAccess.Read);
981 Assert.IsTrue (stream.CanRead, "#B1");
982 Assert.IsTrue (stream.CanSeek, "#B2");
983 Assert.IsFalse (stream.CanWrite, "#B3");
984 Assert.IsFalse (stream.IsAsync, "#B4");
985 Assert.IsTrue (stream.Name.EndsWith (path), "#B5");
986 Assert.AreEqual (0, stream.Position, "#B6");
987 Assert.AreEqual ("System.IO.FileStream", stream.ToString (), "#B7");
990 stream = new FileStream (path, FileMode.Truncate, FileAccess.Write, FileShare.ReadWrite);
991 Assert.IsFalse (stream.CanRead, "#C1");
992 Assert.IsTrue (stream.CanSeek, "#C2");
993 Assert.IsTrue (stream.CanWrite, "#C3");
994 Assert.IsFalse (stream.IsAsync, "#C4");
995 Assert.IsTrue (stream.Name.EndsWith ("testfilestream.tmp.2"), "#C5");
996 Assert.AreEqual (0, stream.Position, "#C6");
997 Assert.AreEqual ("System.IO.FileStream", stream.ToString (), "#C7");
1002 [Category ("NotWorking")]
1003 // Bug: 71371 -> duplicate and WONTFIX.
1004 public void TestLock_FailsOnMono ()
1006 string path = TempFolder + Path.DirectorySeparatorChar + "TestLock";
1009 FileStream stream = new FileStream (path, FileMode.CreateNew, FileAccess.ReadWrite);
1011 stream.Write (new Byte[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }, 0, 10);
1014 stream = new FileStream (path, FileMode.Open, FileAccess.ReadWrite);
1018 FileStream stream2 = new FileStream (path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
1020 byte[] bytes = new byte[5];
1022 stream2.Read (bytes, 0, 5);
1024 } catch (Exception e) {
1025 Assert.AreEqual (typeof (IOException), e.GetType (), "#2");
1034 public void TestLock ()
1036 string path = TempFolder + Path.DirectorySeparatorChar + "TestLock";
1039 FileStream stream = new FileStream (path, FileMode.CreateNew, FileAccess.ReadWrite);
1041 stream.Write (new Byte[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }, 0, 10);
1044 stream = new FileStream (path, FileMode.Open, FileAccess.ReadWrite);
1048 FileStream stream2 = new FileStream (path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
1050 byte[] bytes = new byte[5];
1052 stream2.Read (bytes, 0, 5);
1053 Assert.Fail ("#A1");
1054 } catch (Exception) {
1055 // Bug #71371: on MS.NET you get an IOException detailing a lock
1056 // Assert.AreEqual (typeof (IOException), e.GetType (), "#A2");
1059 stream2.Seek (5, SeekOrigin.Begin);
1060 stream2.Read (bytes, 0, 5);
1062 Assert.AreEqual (5, bytes[0], "#B1");
1063 Assert.AreEqual (6, bytes[1], "#B2");
1064 Assert.AreEqual (7, bytes[2], "#B3");
1065 Assert.AreEqual (8, bytes[3], "#B4");
1066 Assert.AreEqual (9, bytes[4], "#B5");
1068 stream.Unlock (0, 5);
1069 stream2.Seek (0, SeekOrigin.Begin);
1070 stream2.Read (bytes, 0, 5);
1072 Assert.AreEqual (0, bytes[0], "#C1");
1073 Assert.AreEqual (1, bytes[1], "#C2");
1074 Assert.AreEqual (2, bytes[2], "#C3");
1075 Assert.AreEqual (3, bytes[3], "#C4");
1076 Assert.AreEqual (4, bytes[4], "#C5");
1087 string path = TempFolder + DSC + "FST.Seek.Test";
1090 FileStream stream = new FileStream (path, FileMode.CreateNew, FileAccess.ReadWrite, FileShare.ReadWrite);
1091 FileStream stream2 = new FileStream (path, FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite);
1093 stream.Write (new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 10 }, 0, 9);
1094 Assert.AreEqual (5, stream2.Seek (5, SeekOrigin.Begin), "#1");
1095 Assert.AreEqual (-1, stream2.ReadByte (), "#2");
1097 Assert.AreEqual (2, stream2.Seek (-3, SeekOrigin.Current), "#3");
1098 Assert.AreEqual (-1, stream2.ReadByte (), "#4");
1100 Assert.AreEqual (12, stream.Seek (3, SeekOrigin.Current), "#5");
1101 Assert.AreEqual (-1, stream.ReadByte (), "#6");
1103 Assert.AreEqual (5, stream.Seek (-7, SeekOrigin.Current), "#7");
1104 Assert.AreEqual (6, stream.ReadByte (), "#8");
1106 Assert.AreEqual (5, stream2.Seek (5, SeekOrigin.Begin), "#9");
1107 Assert.AreEqual (6, stream2.ReadByte (), "#10");
1115 public void TestSeek ()
1117 string path = TempFolder + Path.DirectorySeparatorChar + "TestSeek";
1120 FileStream stream = new FileStream (path, FileMode.CreateNew, FileAccess.ReadWrite, FileShare.ReadWrite);
1121 stream.Write (new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }, 0, 10);
1123 stream.Seek (5, SeekOrigin.End);
1124 Assert.AreEqual (-1, stream.ReadByte (), "#1");
1126 stream.Seek (-5, SeekOrigin.End);
1127 Assert.AreEqual (6, stream.ReadByte (), "#2");
1130 stream.Seek (-11, SeekOrigin.End);
1132 } catch (Exception e) {
1133 Assert.AreEqual (typeof (IOException), e.GetType (), "#4");
1136 stream.Seek (19, SeekOrigin.Begin);
1137 Assert.AreEqual (-1, stream.ReadByte (), "#5");
1139 stream.Seek (1, SeekOrigin.Begin);
1140 Assert.AreEqual (2, stream.ReadByte (), "#6");
1142 stream.Seek (3, SeekOrigin.Current);
1143 Assert.AreEqual (6, stream.ReadByte (), "#7");
1145 stream.Seek (-2, SeekOrigin.Current);
1146 Assert.AreEqual (5, stream.ReadByte (), "#8");
1150 // Test that seeks work correctly when seeking inside the buffer
1151 stream.Seek (0, SeekOrigin.Begin);
1152 stream.WriteByte (0);
1153 stream.WriteByte (1);
1154 stream.Seek (0, SeekOrigin.Begin);
1155 byte[] buf = new byte[1];
1157 stream.Write (buf, 0, 1);
1158 stream.Write (buf, 0, 1);
1160 stream.Seek (0, SeekOrigin.Begin);
1161 Assert.AreEqual (2, stream.ReadByte (), "#9");
1162 Assert.AreEqual (2, stream.ReadByte (), "#10");
1169 public void TestClose ()
1171 string path = TempFolder + Path.DirectorySeparatorChar + "TestClose";
1174 FileStream stream = new FileStream (path, FileMode.CreateNew, FileAccess.ReadWrite);
1176 stream.Write (new byte[] { 1, 2, 3, 4 }, 0, 4);
1182 Assert.Fail ("#A1");
1183 } catch (Exception e) {
1184 Assert.AreEqual (typeof (ObjectDisposedException), e.GetType (), "#A2");
1188 stream.WriteByte (64);
1189 Assert.Fail ("#B1");
1190 } catch (Exception e) {
1191 Assert.AreEqual (typeof (ObjectDisposedException), e.GetType (), "#B2");
1196 Assert.Fail ("#C1");
1197 } catch (Exception e) {
1198 Assert.AreEqual (typeof (ObjectDisposedException), e.GetType (), "#C2");
1202 long l = stream.Length;
1203 Assert.Fail ("#D1");
1204 } catch (Exception e) {
1205 Assert.AreEqual (typeof (ObjectDisposedException), e.GetType (), "#D2");
1209 long l = stream.Position;
1210 Assert.Fail ("#E1");
1211 } catch (Exception e) {
1212 Assert.AreEqual (typeof (ObjectDisposedException), e.GetType (), "#E2");
1215 Assert.IsFalse (stream.CanRead, "#F1");
1216 Assert.IsFalse (stream.CanSeek, "#F2");
1217 Assert.IsFalse (stream.CanWrite, "#F3");
1218 Assert.IsTrue (stream.Name.EndsWith (path), "#F4");
1225 /// Checks whether the <see cref="FileStream" /> throws a <see cref="NotSupportedException" />
1226 /// when the stream is opened with access mode <see cref="FileAccess.Read" /> and the
1227 /// <see cref="FileStream.Write(byte[], int, int)" /> method is called.
1230 [ExpectedException (typeof (NotSupportedException))]
1231 public void TestWriteVerifyAccessMode ()
1233 string path = TempFolder + Path.DirectorySeparatorChar + "temp";
1236 FileStream stream = null;
1240 buffer = Encoding.ASCII.GetBytes ("test");
1241 stream = new FileStream (path, FileMode.OpenOrCreate, FileAccess.Read);
1242 stream.Write (buffer, 0, buffer.Length);
1251 /// Checks whether the <see cref="FileStream" /> throws a <see cref="NotSupportedException" />
1252 /// when the stream is opened with access mode <see cref="FileAccess.Read" /> and the
1253 /// <see cref="FileStream.WriteByte(byte)" /> method is called.
1256 [ExpectedException (typeof (NotSupportedException))]
1257 public void TestWriteByteVerifyAccessMode ()
1259 string path = TempFolder + Path.DirectorySeparatorChar + "temp";
1262 FileStream stream = null;
1265 stream = new FileStream (path, FileMode.OpenOrCreate, FileAccess.Read);
1266 stream.WriteByte (Byte.MinValue);
1275 /// Checks whether the <see cref="FileStream" /> throws a <see cref="NotSupportedException" />
1276 /// when the stream is opened with access mode <see cref="FileAccess.Write" /> and the
1277 /// <see cref="FileStream.Read(byte[], int, int)" /> method is called.
1280 [ExpectedException (typeof (NotSupportedException))]
1281 public void TestReadVerifyAccessMode ()
1283 string path = TempFolder + Path.DirectorySeparatorChar + "temp";
1286 FileStream stream = null;
1287 byte[] buffer = new byte[100];
1290 stream = new FileStream (path, FileMode.OpenOrCreate, FileAccess.Write);
1291 stream.Read (buffer, 0, buffer.Length);
1299 /// Checks whether the <see cref="FileStream" /> throws a <see cref="NotSupportedException" />
1300 /// when the stream is opened with access mode <see cref="FileAccess.Write" /> and the
1301 /// <see cref="FileStream.ReadByte()" /> method is called.
1304 [ExpectedException (typeof (NotSupportedException))]
1305 public void TestReadByteVerifyAccessMode ()
1307 string path = TempFolder + Path.DirectorySeparatorChar + "temp";
1310 FileStream stream = null;
1313 stream = new FileStream (path, FileMode.OpenOrCreate, FileAccess.Write);
1314 int readByte = stream.ReadByte ();
1322 // Check that the stream is flushed even when it doesn't own the
1325 public void TestFlushNotOwningHandle ()
1327 string path = Path.Combine (TempFolder, "TestFlushNotOwningHandle");
1330 FileStream s = new FileStream (path, FileMode.Create);
1331 using (FileStream s2 = new FileStream (s.Handle, FileAccess.Write, false)) {
1332 byte[] buf = new byte[2];
1334 s2.Write (buf, 0, 1);
1338 Assert.AreEqual ((int) '1', s.ReadByte ());
1342 private void DeleteFile (string path)
1344 if (File.Exists (path))
1349 [ExpectedException (typeof (ArgumentOutOfRangeException))]
1350 public void Read_OffsetNegative ()
1352 string path = TempFolder + Path.DirectorySeparatorChar + "temp";
1355 using (FileStream stream = new FileStream (path, FileMode.OpenOrCreate, FileAccess.Read)) {
1356 stream.Read (new byte[0], -1, 1);
1361 [ExpectedException (typeof (ArgumentException))]
1362 public void Read_OffsetOverflow ()
1364 string path = TempFolder + Path.DirectorySeparatorChar + "temp";
1367 using (FileStream stream = new FileStream (path, FileMode.OpenOrCreate, FileAccess.Read)) {
1368 stream.Read (new byte[0], Int32.MaxValue, 1);
1373 [ExpectedException (typeof (ArgumentOutOfRangeException))]
1374 public void Read_CountNegative ()
1376 string path = TempFolder + Path.DirectorySeparatorChar + "temp";
1379 using (FileStream stream = new FileStream (path, FileMode.OpenOrCreate, FileAccess.Read)) {
1380 stream.Read (new byte[0], 1, -1);
1385 [ExpectedException (typeof (ArgumentException))]
1386 public void Read_CountOverflow ()
1388 string path = TempFolder + Path.DirectorySeparatorChar + "temp";
1391 using (FileStream stream = new FileStream (path, FileMode.OpenOrCreate, FileAccess.Read)) {
1392 stream.Read (new byte[0], 1, Int32.MaxValue);
1397 [ExpectedException (typeof (ArgumentOutOfRangeException))]
1398 public void Write_OffsetNegative ()
1400 string path = TempFolder + Path.DirectorySeparatorChar + "temp";
1403 using (FileStream stream = new FileStream (path, FileMode.OpenOrCreate, FileAccess.Write)) {
1404 stream.Write (new byte[0], -1, 1);
1409 [ExpectedException (typeof (ArgumentException))]
1410 public void Write_OffsetOverflow ()
1412 string path = TempFolder + Path.DirectorySeparatorChar + "temp";
1415 using (FileStream stream = new FileStream (path, FileMode.OpenOrCreate, FileAccess.Write)) {
1416 stream.Write (new byte[0], Int32.MaxValue, 1);
1421 [ExpectedException (typeof (ArgumentOutOfRangeException))]
1422 public void Write_CountNegative ()
1424 string path = TempFolder + Path.DirectorySeparatorChar + "temp";
1427 using (FileStream stream = new FileStream (path, FileMode.OpenOrCreate, FileAccess.Write)) {
1428 stream.Write (new byte[0], 1, -1);
1433 [ExpectedException (typeof (ArgumentException))]
1434 public void Write_CountOverflow ()
1436 string path = TempFolder + Path.DirectorySeparatorChar + "temp";
1439 using (FileStream stream = new FileStream (path, FileMode.OpenOrCreate, FileAccess.Write)) {
1440 stream.Write (new byte[0], 1, Int32.MaxValue);
1445 [ExpectedException (typeof (ArgumentException))]
1446 public void Seek_InvalidSeekOrigin ()
1448 string path = TempFolder + Path.DirectorySeparatorChar + "temp";
1451 using (FileStream stream = new FileStream (path, FileMode.OpenOrCreate, FileAccess.Read)) {
1452 stream.Seek (0, (SeekOrigin) (-1));
1457 [ExpectedException (typeof (ArgumentException))]
1458 public void Constructor_InvalidFileHandle ()
1460 new FileStream ((IntPtr) (-1L), FileAccess.Read);
1464 public void PositionAfterSetLength ()
1466 string path = TempFolder + Path.DirectorySeparatorChar + "temp";
1469 using (FileStream stream = new FileStream (path, FileMode.OpenOrCreate, FileAccess.Write)) {
1470 stream.SetLength (32);
1471 stream.Position = 32;
1472 stream.SetLength (16);
1473 Assert.AreEqual (16, stream.Position);
1478 [ExpectedException (typeof (ObjectDisposedException))]
1479 public void SetLength_Disposed ()
1481 string path = TempFolder + Path.DirectorySeparatorChar + "temp";
1483 FileStream stream = new FileStream (path, FileMode.OpenOrCreate, FileAccess.Write);
1485 stream.SetLength (16);
1489 [ExpectedException (typeof (ObjectDisposedException))]
1490 public void Position_Disposed ()
1492 string path = TempFolder + Path.DirectorySeparatorChar + "temp";
1494 FileStream stream = new FileStream (path, FileMode.OpenOrCreate, FileAccess.Read);
1496 stream.Position = 0;
1500 [ExpectedException (typeof (ObjectDisposedException))]
1501 public void BeginRead_Disposed ()
1503 string path = TempFolder + Path.DirectorySeparatorChar + "temp";
1505 FileStream stream = new FileStream (path, FileMode.OpenOrCreate, FileAccess.Read);
1507 stream.EndRead (stream.BeginRead (new byte[8], 0, 8, null, null));
1511 [ExpectedException (typeof (ObjectDisposedException))]
1512 public void BeginWrite_Disposed ()
1514 string path = TempFolder + Path.DirectorySeparatorChar + "temp";
1516 FileStream stream = new FileStream (path, FileMode.OpenOrCreate, FileAccess.Write);
1518 stream.EndWrite (stream.BeginWrite (new byte[8], 0, 8, null, null));
1521 static IAsyncResult DoBeginWrite(Stream stream, ManualResetEvent mre, byte[] RandomBuffer)
1523 return stream.BeginWrite (RandomBuffer, 0, RandomBuffer.Length, ar => {
1524 IAsyncResult begin_write_recursive_ares;
1527 stream.EndWrite (ar);
1529 // we don't supply an ManualResetEvent so this will throw an NRE on the second run
1530 // which nunit-console will ignore (but other test runners don't like that)
1534 begin_write_recursive_ares = DoBeginWrite (stream, null, RandomBuffer);
1535 begin_write_recursive_ares.AsyncWaitHandle.WaitOne ();
1538 } catch (ObjectDisposedException e) {
1539 Console.WriteLine ("stream was disposed: {0}", e);
1545 public void BeginWrite_Recursive ()
1547 string path = TempFolder + Path.DirectorySeparatorChar + "temp";
1550 IAsyncResult begin_write_ares = null;
1553 using (FileStream stream = new FileStream (path, FileMode.OpenOrCreate, FileAccess.Write)) {
1554 var mre = new ManualResetEvent (false);
1555 var RandomBuffer = new byte[1024];
1556 begin_write_ares = DoBeginWrite (stream, mre, RandomBuffer);
1557 Assert.IsTrue (mre.WaitOne (5000), "#1");
1560 if (begin_write_ares != null)
1561 begin_write_ares.AsyncWaitHandle.WaitOne ();
1566 [ExpectedException (typeof (ObjectDisposedException))]
1567 public void Lock_Disposed ()
1569 string path = TempFolder + Path.DirectorySeparatorChar + "temp";
1571 FileStream stream = new FileStream (path, FileMode.OpenOrCreate, FileAccess.Write);
1577 [ExpectedException (typeof (ObjectDisposedException))]
1578 public void Unlock_Disposed ()
1580 string path = TempFolder + Path.DirectorySeparatorChar + "temp";
1582 FileStream stream = new FileStream (path, FileMode.OpenOrCreate, FileAccess.Write);
1584 stream.Unlock (0, 1);
1588 public void ReadBytePastEndOfStream ()
1590 string path = TempFolder + Path.DirectorySeparatorChar + "temp";
1592 using (FileStream stream = new FileStream (path, FileMode.OpenOrCreate, FileAccess.Read)) {
1593 stream.Seek (0, SeekOrigin.End);
1594 Assert.AreEqual (-1, stream.ReadByte ());
1600 [ExpectedException (typeof (NotSupportedException))]
1601 public void SetLengthWithClosedBaseStream ()
1603 string fn = Path.Combine (TempFolder, "temp");
1605 FileStream fs = new FileStream (fn, FileMode.Create);
1606 BufferedStream bs = new BufferedStream (fs);
1609 bs.SetLength (1000);
1616 public void LengthAfterWrite ()
1618 string path = TempFolder + DSC + "oneofthefilescreated.txt";
1619 FileStream fs = null;
1622 fs = new FileStream (path, FileMode.CreateNew);
1623 fs.WriteByte (Convert.ToByte ('A'));
1624 byte [] buffer = Encoding.ASCII.GetBytes (" is a first character.");
1625 fs.Write (buffer, 0, buffer.Length);
1626 fs.Seek (0, SeekOrigin.Begin);
1627 char a = Convert.ToChar (fs.ReadByte ());
1628 Assert.AreEqual ('A', a, "#A1");
1629 Assert.AreEqual (23, fs.Length, "#A2");
1630 int nread = fs.Read (buffer, 0, 5);
1631 Assert.AreEqual (5, nread, "#A3");
1640 public void DeleteOnClose ()
1642 string path = TempFolder + DSC + "created.txt";
1644 FileStream fs = new FileStream (path, FileMode.CreateNew, FileAccess.Write, FileShare.None, 1024,
1645 FileOptions.DeleteOnClose);
1646 Assert.AreEqual (true, File.Exists (path), "DOC#1");
1648 Assert.AreEqual (false, File.Exists (path), "DOC#2");
1653 public void OpenCharDeviceRepeatedly ()
1655 // https://bugzilla.xamarin.com/show_bug.cgi?id=38408
1657 using (var f = new FileStream ("/dev/zero", FileMode.Open))
1660 } catch (FileNotFoundException) {
1661 // Only run this test on platforms where /dev/zero exists
1663 } catch (DirectoryNotFoundException) {
1664 // Only run this test on platforms where /dev exists
1668 // this shouldn't throw
1669 using (var g = new FileStream ("/dev/zero", FileMode.Open))
1676 public void WriteWithExposedHandle ()
1678 string path = TempFolder + DSC + "exposed-handle.txt";
1679 FileStream fs1 = null;
1680 FileStream fs2 = null;
1684 fs1 = new FileStream (path, FileMode.Create);
1685 fs2 = new FileStream (fs1.SafeFileHandle, FileAccess.ReadWrite);
1686 fs1.WriteByte (Convert.ToByte ('H'));
1687 fs1.WriteByte (Convert.ToByte ('E'));
1688 fs1.WriteByte (Convert.ToByte ('L'));
1689 fs2.WriteByte (Convert.ToByte ('L'));
1690 fs2.WriteByte (Convert.ToByte ('O'));
1691 long fs1Pos = fs1.Position;
1697 var check = Encoding.ASCII.GetString (File.ReadAllBytes (path));
1698 Assert.AreEqual ("HELLO", check, "EXPOSED#1");
1699 Assert.AreEqual (5, fs1Pos, "EXPOSED#2");
1710 public void Ctor_InvalidSafeHandle ()
1712 var sf = new SafeFileHandle (IntPtr.Zero, true);
1714 new FileStream (sf, FileAccess.ReadWrite);
1716 } catch (ArgumentException) {
1721 [Test] // Covers #11699
1722 public void ReadWriteFileLength ()
1724 int bufferSize = 128;
1726 int writeLength = bufferSize + 1;
1728 string path = TempFolder + DSC + "readwritefilelength.tmp";
1731 File.WriteAllBytes (path, new byte [readLength + 1]);
1733 using (var file = new FileStream (path, FileMode.Open, FileAccess.ReadWrite, FileShare.Read,
1734 bufferSize, FileOptions.SequentialScan))
1736 file.Read (new byte [readLength], 0, readLength);
1737 file.Write (new byte [writeLength], 0, writeLength);
1739 Assert.AreEqual (readLength + writeLength, file.Length);