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 ()
90 const string file_name = "thisfileshouldnotexist.test";
92 string path = TempFolder + DSC + file_name;
94 FileStream stream = null;
96 stream = new FileStream (TempFolder + DSC + file_name, FileMode.Open);
98 } catch (FileNotFoundException ex) {
99 Assert.AreEqual (typeof (FileNotFoundException), ex.GetType (), "#A2");
100 Assert.AreEqual (path, ex.FileName, "#A3");
101 Assert.IsNull (ex.InnerException, "#A4");
102 Assert.IsNotNull (ex.Message, "#A5");
103 Assert.IsTrue (ex.Message.IndexOf (path) != -1, "#A6");
105 if (stream != null) {
113 string orignalCurrentDir = Directory.GetCurrentDirectory ();
114 Directory.SetCurrentDirectory (TempFolder);
116 // If TempFolder is a symlink, Mono will follow it and ex.FileName below will contain
117 // the real directory name, not that of the TempFolder symlink and the test will fail
118 // (happens e.g. on Android M)
119 string realTempDir = Directory.GetCurrentDirectory ();
120 path = realTempDir + DSC + file_name;
123 stream = new FileStream (file_name, FileMode.Open);
125 } catch (FileNotFoundException ex) {
126 Assert.AreEqual (typeof (FileNotFoundException), ex.GetType (), "#B2");
127 Assert.AreEqual (path, ex.FileName, "#B3");
128 Assert.IsNull (ex.InnerException, "#B4");
129 Assert.IsNotNull (ex.Message, "#B5");
130 Assert.IsTrue (ex.Message.IndexOf (path) != -1, "#B6");
132 // restore original current directory
133 Directory.SetCurrentDirectory (orignalCurrentDir);
141 public void CtorFileNotFoundException_Mode_Truncate ()
143 const string file_name = "thisfileshouldNOTexist.test";
145 string path = TempFolder + DSC + file_name;
147 FileStream stream = null;
149 stream = new FileStream (path, FileMode.Truncate);
151 } catch (FileNotFoundException ex) {
152 Assert.AreEqual (typeof (FileNotFoundException), ex.GetType (), "#A2");
153 Assert.AreEqual (path, ex.FileName, "#A3");
154 Assert.IsNull (ex.InnerException, "#A4");
155 Assert.IsNotNull (ex.Message, "#A5");
156 Assert.IsTrue (ex.Message.IndexOf (path) != -1, "#A6");
158 if (stream != null) {
166 string orignalCurrentDir = Directory.GetCurrentDirectory ();
167 Directory.SetCurrentDirectory (TempFolder);
169 // If TempFolder is a symlink, Mono will follow it and ex.FileName below will contain
170 // the real directory name, not that of the TempFolder symlink and the test will fail
171 // (happens e.g. on Android M)
172 string realTempDir = Directory.GetCurrentDirectory ();
173 path = realTempDir + DSC + file_name;
176 stream = new FileStream (file_name, FileMode.Truncate);
178 } catch (FileNotFoundException ex) {
179 Assert.AreEqual (typeof (FileNotFoundException), ex.GetType (), "#B2");
180 Assert.AreEqual (path, ex.FileName, "#B3");
181 Assert.IsNull (ex.InnerException, "#B4");
182 Assert.IsNotNull (ex.Message, "#B5");
183 Assert.IsTrue (ex.Message.IndexOf (path) != -1, "#B6");
185 // restore original current directory
186 Directory.SetCurrentDirectory (orignalCurrentDir);
194 public void CtorIOException1 ()
196 const string file_name = "thisfileshouldexists.test";
198 string path = TempFolder + DSC + file_name;
199 FileStream stream = null;
202 stream = new FileStream (path, FileMode.CreateNew);
205 stream = new FileStream (path, FileMode.CreateNew);
207 } catch (IOException ex) {
208 Assert.AreEqual (typeof (IOException), ex.GetType (), "#A2");
209 Assert.IsNull (ex.InnerException, "#A3");
210 Assert.IsNotNull (ex.Message, "#A4");
211 Assert.IsTrue (ex.Message.IndexOf (path) != -1, "#A5");
213 if (stream != null) {
221 string orignalCurrentDir = Directory.GetCurrentDirectory ();
222 Directory.SetCurrentDirectory (TempFolder);
224 // If TempFolder is a symlink, Mono will follow it and ex.FileName below will contain
225 // the real directory name, not that of the TempFolder symlink and the test will fail
226 // (happens e.g. on Android M)
227 string realTempDir = Directory.GetCurrentDirectory ();
228 path = realTempDir + DSC + file_name;
231 stream = new FileStream (file_name, FileMode.CreateNew);
234 stream = new FileStream (file_name, FileMode.CreateNew);
236 } catch (IOException ex) {
237 Assert.AreEqual (typeof (IOException), ex.GetType (), "#B2");
238 Assert.IsNull (ex.InnerException, "#B3");
239 Assert.IsNotNull (ex.Message, "#B4");
240 Assert.IsTrue (ex.Message.IndexOf (path) != -1, "#B5");
242 // restore original current directory
243 Directory.SetCurrentDirectory (orignalCurrentDir);
251 [ExpectedException (typeof (ArgumentOutOfRangeException))]
252 public void CtorArgumentOutOfRangeException1 ()
254 FileStream stream = null;
255 string path = TempFolder + Path.DirectorySeparatorChar + "temp";
258 stream = new FileStream (path, FileMode.Append | FileMode.CreateNew);
267 [ExpectedException (typeof (ArgumentOutOfRangeException))]
268 public void CtorArgumentOutOfRangeException2 ()
270 FileStream stream = null;
271 string path = TempFolder + Path.DirectorySeparatorChar + "temp";
274 stream = new FileStream ("test.test.test", FileMode.Append | FileMode.Open);
282 private void CtorDirectoryNotFoundException (FileMode mode)
284 string path = TempFolder + DSC + "thisDirectoryShouldNotExists";
285 if (Directory.Exists (path))
286 Directory.Delete (path, true);
288 FileStream stream = null;
290 stream = new FileStream (path + DSC + "eitherthisfile.test", mode);
296 if (Directory.Exists (path))
297 Directory.Delete (path, true);
302 [ExpectedException (typeof (DirectoryNotFoundException))]
303 public void CtorDirectoryNotFoundException_CreateNew ()
305 CtorDirectoryNotFoundException (FileMode.CreateNew);
309 [ExpectedException (typeof (DirectoryNotFoundException))]
310 public void CtorDirectoryNotFoundException_Create ()
312 CtorDirectoryNotFoundException (FileMode.Create);
316 [ExpectedException (typeof (DirectoryNotFoundException))]
317 public void CtorDirectoryNotFoundException_Open ()
319 CtorDirectoryNotFoundException (FileMode.Open);
323 [ExpectedException (typeof (DirectoryNotFoundException))]
324 public void CtorDirectoryNotFoundException_OpenOrCreate ()
326 CtorDirectoryNotFoundException (FileMode.OpenOrCreate);
330 [ExpectedException (typeof (DirectoryNotFoundException))]
331 public void CtorDirectoryNotFoundException_Truncate ()
333 CtorDirectoryNotFoundException (FileMode.Truncate);
337 [ExpectedException (typeof (DirectoryNotFoundException))]
338 public void CtorDirectoryNotFoundException_Append ()
340 CtorDirectoryNotFoundException (FileMode.Append);
344 public void CtorDirectoryNotFound_RelativePath ()
346 string orignalCurrentDir = Directory.GetCurrentDirectory ();
347 Directory.SetCurrentDirectory (TempFolder);
349 // If TempFolder is a symlink, Mono will follow it and ex.FileName below will contain
350 // the real directory name, not that of the TempFolder symlink and the test will fail
351 // (happens e.g. on Android M)
352 string realTempDir = Directory.GetCurrentDirectory ();
354 string relativePath = "DirectoryDoesNotExist" + Path.DirectorySeparatorChar + "file.txt";
355 string fullPath = Path.Combine (realTempDir, relativePath);
357 new FileStream (relativePath, FileMode.Open);
359 } catch (DirectoryNotFoundException ex) {
360 Assert.AreEqual (typeof (DirectoryNotFoundException), ex.GetType (), "#A2");
361 Assert.IsNull (ex.InnerException, "#A3");
362 Assert.IsNotNull (ex.Message, "#A4");
363 Assert.IsTrue (ex.Message.IndexOf (fullPath) != -1, "#A5");
365 // restore original current directory
366 Directory.SetCurrentDirectory (orignalCurrentDir);
371 // FileShare.Inheritable is ignored, but file does not exist
372 [ExpectedException (typeof (FileNotFoundException))]
373 public void CtorArgumentOutOfRangeException3 ()
375 string path = TempFolder + DSC + "CtorArgumentOutOfRangeException1";
378 FileStream stream = null;
380 stream = new FileStream (path, FileMode.Open, FileAccess.Read, FileShare.Inheritable);
389 [ExpectedException (typeof (ArgumentOutOfRangeException))]
390 public void CtorArgumentOutOfRangeException4 ()
392 string path = TempFolder + DSC + "CtorArgumentOutOfRangeException4";
395 FileStream stream = null;
397 stream = new FileStream (path, FileMode.OpenOrCreate, FileAccess.Read, FileShare.ReadWrite, -1);
406 [ExpectedException (typeof (ArgumentOutOfRangeException))]
407 public void CtorBufferSizeZero ()
409 // Buffer size can't be zero
411 string path = Path.Combine (TempFolder, "CtorBufferSizeZero");
414 FileStream stream = null;
416 stream = new FileStream (path, FileMode.CreateNew, FileAccess.Write, FileShare.ReadWrite, 0);
425 [ExpectedException (typeof (ArgumentException))]
426 public void CtorArgumentException2 ()
428 // FileMode.CreateNew && FileAccess.Read
430 string path = TempFolder + Path.DirectorySeparatorChar + "temp";
431 FileStream stream = null;
436 stream = new FileStream (".test.test.test.2", FileMode.CreateNew, FileAccess.Read, FileShare.None | FileShare.Write);
447 // FileShare.Inheritable is ignored, but file does not exist
448 [ExpectedException (typeof (FileNotFoundException))]
449 public void CtorArgumentOutOfRangeException5 ()
451 string path = TempFolder + Path.DirectorySeparatorChar + "temp";
454 FileStream stream = null;
456 stream = new FileStream (path, FileMode.Open, FileAccess.Read, FileShare.Inheritable | FileShare.ReadWrite);
466 [ExpectedException (typeof (ArgumentException))]
467 public void CtorArgumentException3 ()
469 string path = TempFolder + Path.DirectorySeparatorChar + "temp";
470 FileStream stream = null;
475 stream = new FileStream (".test.test.test.2", FileMode.Truncate, FileAccess.Read);
485 public void ModeAndAccessCombinations ()
487 string path = TempFolder + Path.DirectorySeparatorChar + "temp";
489 FileStream stream = null;
493 // Append access can be requested only in write-only mode
494 stream = new FileStream (path, FileMode.Append, FileAccess.Read);
496 } catch (ArgumentException ex) {
497 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#A2");
505 // Append / ReadWrite
507 // Append access can be requested only in write-only mode
508 stream = new FileStream (path, FileMode.Append, FileAccess.ReadWrite);
510 } catch (ArgumentException ex) {
511 // make sure it is exact this exception, and not a derived type
512 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#B2");
522 stream = new FileStream (path, FileMode.Append, FileAccess.Write);
532 stream = new FileStream (path, FileMode.Create, FileAccess.Read);
534 } catch (ArgumentException ex) {
535 // make sure it is exact this exception, and not a derived type
536 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#C2");
544 // Create / ReadWrite
546 stream = new FileStream (path, FileMode.Create, FileAccess.ReadWrite);
556 stream = new FileStream (path, FileMode.Create, FileAccess.Write);
566 stream = new FileStream (path, FileMode.CreateNew, FileAccess.Read);
568 } catch (ArgumentException ex) {
569 // make sure it is exact this exception, and not a derived type
570 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#D2");
578 // CreateNew / ReadWrite
580 stream = new FileStream (path, FileMode.CreateNew, FileAccess.ReadWrite);
590 stream = new FileStream (path, FileMode.CreateNew, FileAccess.Write);
600 stream = new FileStream (path, FileMode.Open, FileAccess.Read);
602 } catch (FileNotFoundException ex) {
603 // make sure it is exact this exception, and not a derived type
604 Assert.AreEqual (typeof (FileNotFoundException), ex.GetType (), "#E2");
605 Assert.AreEqual (path, ex.FileName, "#E3");
606 Assert.IsNull (ex.InnerException, "#E4");
607 Assert.IsNotNull (ex.Message, "#E5");
608 Assert.IsTrue (ex.Message.IndexOf (path) != -1, "#E6");
618 stream = new FileStream (path, FileMode.Open, FileAccess.ReadWrite);
620 } catch (FileNotFoundException ex) {
621 // make sure it is exact this exception, and not a derived type
622 Assert.AreEqual (typeof (FileNotFoundException), ex.GetType (), "#F2");
623 Assert.AreEqual (path, ex.FileName, "#F3");
624 Assert.IsNull (ex.InnerException, "#F4");
625 Assert.IsNotNull (ex.Message, "#F5");
626 Assert.IsTrue (ex.Message.IndexOf (path) != -1, "#F6");
636 stream = new FileStream (path, FileMode.Open, FileAccess.Write);
638 } catch (FileNotFoundException ex) {
639 // make sure it is exact this exception, and not a derived type
640 Assert.AreEqual (typeof (FileNotFoundException), ex.GetType (), "#G2");
641 Assert.AreEqual (path, ex.FileName, "#G3");
642 Assert.IsNull (ex.InnerException, "#G4");
643 Assert.IsNotNull (ex.Message, "#G5");
644 Assert.IsTrue (ex.Message.IndexOf (path) != -1, "#G6");
652 // OpenOrCreate / Read
654 stream = new FileStream (path, FileMode.OpenOrCreate, FileAccess.Read);
662 // OpenOrCreate / ReadWrite
664 stream = new FileStream (path, FileMode.OpenOrCreate, FileAccess.ReadWrite);
672 // OpenOrCreate / Write
674 stream = new FileStream (path, FileMode.OpenOrCreate, FileAccess.Write);
684 stream = new FileStream (path, FileMode.Truncate, FileAccess.Read);
686 } catch (ArgumentException ex) {
687 // make sure it is exact this exception, and not a derived type
688 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#H2");
696 // Truncate / ReadWrite
698 stream = new FileStream (path, FileMode.Truncate, FileAccess.ReadWrite);
700 } catch (FileNotFoundException ex) {
701 // make sure it is exact this exception, and not a derived type
702 Assert.AreEqual (typeof (FileNotFoundException), ex.GetType (), "#I2");
703 Assert.AreEqual (path, ex.FileName, "#I3");
704 Assert.IsNull (ex.InnerException, "#I4");
705 Assert.IsNotNull (ex.Message, "#I5");
706 Assert.IsTrue (ex.Message.IndexOf (path) != -1, "#I6");
716 stream = new FileStream (path, FileMode.Truncate, FileAccess.Write);
718 } catch (FileNotFoundException ex) {
719 // make sure it is exact this exception, and not a derived type
720 Assert.AreEqual (typeof (FileNotFoundException), ex.GetType (), "#J2");
721 Assert.AreEqual (path, ex.FileName, "#J3");
722 Assert.IsNull (ex.InnerException, "#J4");
723 Assert.IsNotNull (ex.Message, "#J5");
724 Assert.IsTrue (ex.Message.IndexOf (path) != -1, "#J6");
733 [Test, ExpectedException (typeof (IOException))]
734 public void CtorIOException2 ()
736 FileStream stream = null;
738 stream = new FileStream (new IntPtr (Int32.MaxValue), FileAccess.Read);
745 [Test, ExpectedException (typeof (IOException))]
746 public void CtorIOException ()
748 string path = TempFolder + DSC + "CTorIOException.Test";
749 FileStream stream = null;
750 FileStream stream2 = null;
754 stream = new FileStream (path, FileMode.CreateNew);
756 // used by an another process
757 stream2 = new FileStream (path, FileMode.OpenOrCreate);
768 public void CtorAccess1Read2Read ()
770 FileStream fs = null;
771 FileStream fs2 = null;
772 string fn = Path.Combine (TempFolder, "temp");
774 if (!File.Exists (fn)) {
775 TextWriter tw = File.CreateText (fn);
779 fs = new FileStream (fn, FileMode.Open, FileAccess.Read);
780 fs2 = new FileStream (fn, FileMode.Open, FileAccess.Read);
786 if (File.Exists (fn))
792 [ExpectedException (typeof (IOException))]
793 public void CtorAccess1Read2Write ()
795 string fn = Path.Combine (TempFolder, "temp");
796 FileStream fs1 = null;
797 FileStream fs2 = null;
799 if (!File.Exists (fn)) {
800 using (TextWriter tw = File.CreateText (fn)) {
804 fs1 = new FileStream (fn, FileMode.Open, FileAccess.Read);
805 fs2 = new FileStream (fn, FileMode.Create, FileAccess.Write);
811 if (File.Exists (fn))
817 [ExpectedException (typeof (IOException))]
818 public void CtorAccess1Write2Write ()
820 string fn = Path.Combine (TempFolder, "temp");
821 FileStream fs1 = null;
822 FileStream fs2 = null;
824 if (File.Exists (fn))
826 fs1 = new FileStream (fn, FileMode.Create, FileAccess.Write);
827 fs2 = new FileStream (fn, FileMode.Create, FileAccess.Write);
833 if (File.Exists (fn))
839 [ExpectedException (typeof (UnauthorizedAccessException))]
840 public void CtorReadDirectoryAsFile ()
842 FileStream stream = null;
844 stream = new FileStream (TempFolder, FileMode.Open, FileAccess.Read);
852 public void FileShare_Delete ()
854 string fn = Path.Combine (TempFolder, "temp");
856 using (Stream s = new FileStream (fn, FileMode.OpenOrCreate, FileAccess.Write, FileShare.Delete)) {
857 s.Write (new byte [1] { 0x5 }, 0, 1);
861 using (Stream s = new FileStream (fn, FileMode.OpenOrCreate, FileAccess.Write, FileShare.ReadWrite | FileShare.Delete)) {
862 s.Write (new byte [1] { 0x5 }, 0, 1);
866 using (Stream s = new FileStream (fn, FileMode.OpenOrCreate, FileAccess.Write, FileShare.Write | FileShare.Delete)) {
867 s.Write (new byte [1] { 0x5 }, 0, 1);
871 using (Stream s = new FileStream (fn, FileMode.OpenOrCreate, FileAccess.Write, FileShare.Read | FileShare.Delete)) {
872 s.Write (new byte [1] { 0x5 }, 0, 1);
876 using (Stream s = new FileStream (fn, FileMode.OpenOrCreate, FileAccess.Write, FileShare.Inheritable | FileShare.Delete)) {
877 s.Write (new byte [1] { 0x5 }, 0, 1);
885 string path = TempFolder + DSC + "FileStreamTest.Write";
889 FileStream stream = new FileStream (path, FileMode.CreateNew, FileAccess.ReadWrite, FileShare.ReadWrite, 8);
891 byte[] outbytes = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16 };
892 byte[] bytes = new byte[15];
894 // Check that the data is flushed when we overflow the buffer
895 // with a large amount of data
896 stream.Write (outbytes, 0, 5);
897 stream.Write (outbytes, 5, 10);
898 stream.Seek (0, SeekOrigin.Begin);
900 stream.Read (bytes, 0, 15);
901 for (int i = 0; i < 15; ++i)
902 Assert.AreEqual (i + 1, bytes[i], "#1");
904 // Check that the data is flushed when we overflow the buffer
905 // with a small amount of data
906 stream.Write (outbytes, 0, 7);
907 stream.Write (outbytes, 7, 7);
908 stream.Write (outbytes, 14, 1);
910 stream.Read (bytes, 0, 15);
911 stream.Seek (15, SeekOrigin.Begin);
912 for (int i = 0; i < 15; ++i)
913 Assert.AreEqual (i + 1, bytes[i], "#2");
918 public void Length ()
920 // Test that the Length property takes into account the data
922 string path = TempFolder + DSC + "FileStreamTest.Length";
926 FileStream stream = new FileStream (path, FileMode.CreateNew);
928 byte[] outbytes = new byte[] { 1, 2, 3, 4 };
930 stream.Write (outbytes, 0, 4);
931 Assert.AreEqual (4, stream.Length);
938 string path = TempFolder + DSC + "FileStreamTest.Flush";
939 FileStream stream = null;
940 FileStream stream2 = null;
945 stream = new FileStream (path, FileMode.CreateNew, FileAccess.ReadWrite, FileShare.ReadWrite);
946 stream2 = new FileStream (path, FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite);
948 stream.Write (new byte[] { 1, 2, 3, 4, 5 }, 0, 5);
950 byte[] bytes = new byte[5];
951 stream2.Read (bytes, 0, 5);
952 Assert.AreEqual (0, bytes[0], "#A1");
953 Assert.AreEqual (0, bytes[1], "#A2");
954 Assert.AreEqual (0, bytes[2], "#A3");
955 Assert.AreEqual (0, bytes[3], "#A4");
958 stream2.Read (bytes, 0, 5);
959 Assert.AreEqual (1, bytes[0], "#B1");
960 Assert.AreEqual (2, bytes[1], "#B2");
961 Assert.AreEqual (3, bytes[2], "#B3");
962 Assert.AreEqual (4, bytes[3], "#B4");
973 public void TestDefaultProperties ()
975 string path = TempFolder + Path.DirectorySeparatorChar + "testfilestream.tmp.2";
978 FileStream stream = new FileStream (path, FileMode.Create);
980 Assert.IsTrue (stream.CanRead, "#A1");
981 Assert.IsTrue (stream.CanSeek, "#A2");
982 Assert.IsTrue (stream.CanWrite, "#A3");
983 Assert.IsFalse (stream.IsAsync, "#A4");
984 Assert.IsTrue (stream.Name.EndsWith (path), "#A5");
985 Assert.AreEqual (0, stream.Position, "#A6");
986 Assert.AreEqual ("System.IO.FileStream", stream.ToString (), "#A7");
990 stream = new FileStream (path, FileMode.OpenOrCreate, FileAccess.Read);
991 Assert.IsTrue (stream.CanRead, "#B1");
992 Assert.IsTrue (stream.CanSeek, "#B2");
993 Assert.IsFalse (stream.CanWrite, "#B3");
994 Assert.IsFalse (stream.IsAsync, "#B4");
995 Assert.IsTrue (stream.Name.EndsWith (path), "#B5");
996 Assert.AreEqual (0, stream.Position, "#B6");
997 Assert.AreEqual ("System.IO.FileStream", stream.ToString (), "#B7");
1000 stream = new FileStream (path, FileMode.Truncate, FileAccess.Write, FileShare.ReadWrite);
1001 Assert.IsFalse (stream.CanRead, "#C1");
1002 Assert.IsTrue (stream.CanSeek, "#C2");
1003 Assert.IsTrue (stream.CanWrite, "#C3");
1004 Assert.IsFalse (stream.IsAsync, "#C4");
1005 Assert.IsTrue (stream.Name.EndsWith ("testfilestream.tmp.2"), "#C5");
1006 Assert.AreEqual (0, stream.Position, "#C6");
1007 Assert.AreEqual ("System.IO.FileStream", stream.ToString (), "#C7");
1012 [Category ("NotWorking")]
1013 // Bug: 71371 -> duplicate and WONTFIX.
1014 public void TestLock_FailsOnMono ()
1016 string path = TempFolder + Path.DirectorySeparatorChar + "TestLock";
1019 FileStream stream = new FileStream (path, FileMode.CreateNew, FileAccess.ReadWrite);
1021 stream.Write (new Byte[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }, 0, 10);
1024 stream = new FileStream (path, FileMode.Open, FileAccess.ReadWrite);
1028 FileStream stream2 = new FileStream (path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
1030 byte[] bytes = new byte[5];
1032 stream2.Read (bytes, 0, 5);
1034 } catch (Exception e) {
1035 Assert.AreEqual (typeof (IOException), e.GetType (), "#2");
1044 public void TestLock ()
1046 string path = TempFolder + Path.DirectorySeparatorChar + "TestLock";
1049 FileStream stream = new FileStream (path, FileMode.CreateNew, FileAccess.ReadWrite);
1051 stream.Write (new Byte[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }, 0, 10);
1054 stream = new FileStream (path, FileMode.Open, FileAccess.ReadWrite);
1058 FileStream stream2 = new FileStream (path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
1060 byte[] bytes = new byte[5];
1062 stream2.Read (bytes, 0, 5);
1063 Assert.Fail ("#A1");
1064 } catch (Exception) {
1065 // Bug #71371: on MS.NET you get an IOException detailing a lock
1066 // Assert.AreEqual (typeof (IOException), e.GetType (), "#A2");
1069 stream2.Seek (5, SeekOrigin.Begin);
1070 stream2.Read (bytes, 0, 5);
1072 Assert.AreEqual (5, bytes[0], "#B1");
1073 Assert.AreEqual (6, bytes[1], "#B2");
1074 Assert.AreEqual (7, bytes[2], "#B3");
1075 Assert.AreEqual (8, bytes[3], "#B4");
1076 Assert.AreEqual (9, bytes[4], "#B5");
1078 stream.Unlock (0, 5);
1079 stream2.Seek (0, SeekOrigin.Begin);
1080 stream2.Read (bytes, 0, 5);
1082 Assert.AreEqual (0, bytes[0], "#C1");
1083 Assert.AreEqual (1, bytes[1], "#C2");
1084 Assert.AreEqual (2, bytes[2], "#C3");
1085 Assert.AreEqual (3, bytes[3], "#C4");
1086 Assert.AreEqual (4, bytes[4], "#C5");
1097 string path = TempFolder + DSC + "FST.Seek.Test";
1100 FileStream stream = new FileStream (path, FileMode.CreateNew, FileAccess.ReadWrite, FileShare.ReadWrite);
1101 FileStream stream2 = new FileStream (path, FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite);
1103 stream.Write (new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 10 }, 0, 9);
1104 Assert.AreEqual (5, stream2.Seek (5, SeekOrigin.Begin), "#1");
1105 Assert.AreEqual (-1, stream2.ReadByte (), "#2");
1107 Assert.AreEqual (2, stream2.Seek (-3, SeekOrigin.Current), "#3");
1108 Assert.AreEqual (-1, stream2.ReadByte (), "#4");
1110 Assert.AreEqual (12, stream.Seek (3, SeekOrigin.Current), "#5");
1111 Assert.AreEqual (-1, stream.ReadByte (), "#6");
1113 Assert.AreEqual (5, stream.Seek (-7, SeekOrigin.Current), "#7");
1114 Assert.AreEqual (6, stream.ReadByte (), "#8");
1116 Assert.AreEqual (5, stream2.Seek (5, SeekOrigin.Begin), "#9");
1117 Assert.AreEqual (6, stream2.ReadByte (), "#10");
1125 public void TestSeek ()
1127 string path = TempFolder + Path.DirectorySeparatorChar + "TestSeek";
1130 FileStream stream = new FileStream (path, FileMode.CreateNew, FileAccess.ReadWrite, FileShare.ReadWrite);
1131 stream.Write (new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }, 0, 10);
1133 stream.Seek (5, SeekOrigin.End);
1134 Assert.AreEqual (-1, stream.ReadByte (), "#1");
1136 stream.Seek (-5, SeekOrigin.End);
1137 Assert.AreEqual (6, stream.ReadByte (), "#2");
1140 stream.Seek (-11, SeekOrigin.End);
1142 } catch (Exception e) {
1143 Assert.AreEqual (typeof (IOException), e.GetType (), "#4");
1146 stream.Seek (19, SeekOrigin.Begin);
1147 Assert.AreEqual (-1, stream.ReadByte (), "#5");
1149 stream.Seek (1, SeekOrigin.Begin);
1150 Assert.AreEqual (2, stream.ReadByte (), "#6");
1152 stream.Seek (3, SeekOrigin.Current);
1153 Assert.AreEqual (6, stream.ReadByte (), "#7");
1155 stream.Seek (-2, SeekOrigin.Current);
1156 Assert.AreEqual (5, stream.ReadByte (), "#8");
1160 // Test that seeks work correctly when seeking inside the buffer
1161 stream.Seek (0, SeekOrigin.Begin);
1162 stream.WriteByte (0);
1163 stream.WriteByte (1);
1164 stream.Seek (0, SeekOrigin.Begin);
1165 byte[] buf = new byte[1];
1167 stream.Write (buf, 0, 1);
1168 stream.Write (buf, 0, 1);
1170 stream.Seek (0, SeekOrigin.Begin);
1171 Assert.AreEqual (2, stream.ReadByte (), "#9");
1172 Assert.AreEqual (2, stream.ReadByte (), "#10");
1179 public void TestClose ()
1181 string path = TempFolder + Path.DirectorySeparatorChar + "TestClose";
1184 FileStream stream = new FileStream (path, FileMode.CreateNew, FileAccess.ReadWrite);
1186 stream.Write (new byte[] { 1, 2, 3, 4 }, 0, 4);
1192 Assert.Fail ("#A1");
1193 } catch (Exception e) {
1194 Assert.AreEqual (typeof (ObjectDisposedException), e.GetType (), "#A2");
1198 stream.WriteByte (64);
1199 Assert.Fail ("#B1");
1200 } catch (Exception e) {
1201 Assert.AreEqual (typeof (ObjectDisposedException), e.GetType (), "#B2");
1206 Assert.Fail ("#C1");
1207 } catch (Exception e) {
1208 Assert.AreEqual (typeof (ObjectDisposedException), e.GetType (), "#C2");
1212 long l = stream.Length;
1213 Assert.Fail ("#D1");
1214 } catch (Exception e) {
1215 Assert.AreEqual (typeof (ObjectDisposedException), e.GetType (), "#D2");
1219 long l = stream.Position;
1220 Assert.Fail ("#E1");
1221 } catch (Exception e) {
1222 Assert.AreEqual (typeof (ObjectDisposedException), e.GetType (), "#E2");
1225 Assert.IsFalse (stream.CanRead, "#F1");
1226 Assert.IsFalse (stream.CanSeek, "#F2");
1227 Assert.IsFalse (stream.CanWrite, "#F3");
1228 Assert.IsTrue (stream.Name.EndsWith (path), "#F4");
1235 /// Checks whether the <see cref="FileStream" /> throws a <see cref="NotSupportedException" />
1236 /// when the stream is opened with access mode <see cref="FileAccess.Read" /> and the
1237 /// <see cref="FileStream.Write(byte[], int, int)" /> method is called.
1240 [ExpectedException (typeof (NotSupportedException))]
1241 public void TestWriteVerifyAccessMode ()
1243 string path = TempFolder + Path.DirectorySeparatorChar + "temp";
1246 FileStream stream = null;
1250 buffer = Encoding.ASCII.GetBytes ("test");
1251 stream = new FileStream (path, FileMode.OpenOrCreate, FileAccess.Read);
1252 stream.Write (buffer, 0, buffer.Length);
1261 /// Checks whether the <see cref="FileStream" /> throws a <see cref="NotSupportedException" />
1262 /// when the stream is opened with access mode <see cref="FileAccess.Read" /> and the
1263 /// <see cref="FileStream.WriteByte(byte)" /> method is called.
1266 [ExpectedException (typeof (NotSupportedException))]
1267 public void TestWriteByteVerifyAccessMode ()
1269 string path = TempFolder + Path.DirectorySeparatorChar + "temp";
1272 FileStream stream = null;
1275 stream = new FileStream (path, FileMode.OpenOrCreate, FileAccess.Read);
1276 stream.WriteByte (Byte.MinValue);
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.Read(byte[], int, int)" /> method is called.
1290 [ExpectedException (typeof (NotSupportedException))]
1291 public void TestReadVerifyAccessMode ()
1293 string path = TempFolder + Path.DirectorySeparatorChar + "temp";
1296 FileStream stream = null;
1297 byte[] buffer = new byte[100];
1300 stream = new FileStream (path, FileMode.OpenOrCreate, FileAccess.Write);
1301 stream.Read (buffer, 0, buffer.Length);
1309 /// Checks whether the <see cref="FileStream" /> throws a <see cref="NotSupportedException" />
1310 /// when the stream is opened with access mode <see cref="FileAccess.Write" /> and the
1311 /// <see cref="FileStream.ReadByte()" /> method is called.
1314 [ExpectedException (typeof (NotSupportedException))]
1315 public void TestReadByteVerifyAccessMode ()
1317 string path = TempFolder + Path.DirectorySeparatorChar + "temp";
1320 FileStream stream = null;
1323 stream = new FileStream (path, FileMode.OpenOrCreate, FileAccess.Write);
1324 int readByte = stream.ReadByte ();
1332 // Check that the stream is flushed even when it doesn't own the
1335 public void TestFlushNotOwningHandle ()
1337 string path = Path.Combine (TempFolder, "TestFlushNotOwningHandle");
1340 FileStream s = new FileStream (path, FileMode.Create);
1341 using (FileStream s2 = new FileStream (s.Handle, FileAccess.Write, false)) {
1342 byte[] buf = new byte[2];
1344 s2.Write (buf, 0, 1);
1348 Assert.AreEqual ((int) '1', s.ReadByte ());
1352 private void DeleteFile (string path)
1354 if (File.Exists (path))
1359 [ExpectedException (typeof (ArgumentOutOfRangeException))]
1360 public void Read_OffsetNegative ()
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_OffsetOverflow ()
1374 string path = TempFolder + Path.DirectorySeparatorChar + "temp";
1377 using (FileStream stream = new FileStream (path, FileMode.OpenOrCreate, FileAccess.Read)) {
1378 stream.Read (new byte[0], Int32.MaxValue, 1);
1383 [ExpectedException (typeof (ArgumentOutOfRangeException))]
1384 public void Read_CountNegative ()
1386 string path = TempFolder + Path.DirectorySeparatorChar + "temp";
1389 using (FileStream stream = new FileStream (path, FileMode.OpenOrCreate, FileAccess.Read)) {
1390 stream.Read (new byte[0], 1, -1);
1395 [ExpectedException (typeof (ArgumentException))]
1396 public void Read_CountOverflow ()
1398 string path = TempFolder + Path.DirectorySeparatorChar + "temp";
1401 using (FileStream stream = new FileStream (path, FileMode.OpenOrCreate, FileAccess.Read)) {
1402 stream.Read (new byte[0], 1, Int32.MaxValue);
1407 [ExpectedException (typeof (ArgumentOutOfRangeException))]
1408 public void Write_OffsetNegative ()
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_OffsetOverflow ()
1422 string path = TempFolder + Path.DirectorySeparatorChar + "temp";
1425 using (FileStream stream = new FileStream (path, FileMode.OpenOrCreate, FileAccess.Write)) {
1426 stream.Write (new byte[0], Int32.MaxValue, 1);
1431 [ExpectedException (typeof (ArgumentOutOfRangeException))]
1432 public void Write_CountNegative ()
1434 string path = TempFolder + Path.DirectorySeparatorChar + "temp";
1437 using (FileStream stream = new FileStream (path, FileMode.OpenOrCreate, FileAccess.Write)) {
1438 stream.Write (new byte[0], 1, -1);
1443 [ExpectedException (typeof (ArgumentException))]
1444 public void Write_CountOverflow ()
1446 string path = TempFolder + Path.DirectorySeparatorChar + "temp";
1449 using (FileStream stream = new FileStream (path, FileMode.OpenOrCreate, FileAccess.Write)) {
1450 stream.Write (new byte[0], 1, Int32.MaxValue);
1455 [ExpectedException (typeof (ArgumentException))]
1456 public void Seek_InvalidSeekOrigin ()
1458 string path = TempFolder + Path.DirectorySeparatorChar + "temp";
1461 using (FileStream stream = new FileStream (path, FileMode.OpenOrCreate, FileAccess.Read)) {
1462 stream.Seek (0, (SeekOrigin) (-1));
1467 [ExpectedException (typeof (ArgumentException))]
1468 public void Constructor_InvalidFileHandle ()
1470 new FileStream ((IntPtr) (-1L), FileAccess.Read);
1474 public void PositionAfterSetLength ()
1476 string path = TempFolder + Path.DirectorySeparatorChar + "temp";
1479 using (FileStream stream = new FileStream (path, FileMode.OpenOrCreate, FileAccess.Write)) {
1480 stream.SetLength (32);
1481 stream.Position = 32;
1482 stream.SetLength (16);
1483 Assert.AreEqual (16, stream.Position);
1488 [ExpectedException (typeof (ObjectDisposedException))]
1489 public void SetLength_Disposed ()
1491 string path = TempFolder + Path.DirectorySeparatorChar + "temp";
1493 FileStream stream = new FileStream (path, FileMode.OpenOrCreate, FileAccess.Write);
1495 stream.SetLength (16);
1499 [ExpectedException (typeof (ObjectDisposedException))]
1500 public void Position_Disposed ()
1502 string path = TempFolder + Path.DirectorySeparatorChar + "temp";
1504 FileStream stream = new FileStream (path, FileMode.OpenOrCreate, FileAccess.Read);
1506 stream.Position = 0;
1510 [ExpectedException (typeof (ObjectDisposedException))]
1511 public void BeginRead_Disposed ()
1513 string path = TempFolder + Path.DirectorySeparatorChar + "temp";
1515 FileStream stream = new FileStream (path, FileMode.OpenOrCreate, FileAccess.Read);
1517 stream.EndRead (stream.BeginRead (new byte[8], 0, 8, null, null));
1521 [ExpectedException (typeof (ObjectDisposedException))]
1522 public void BeginWrite_Disposed ()
1524 string path = TempFolder + Path.DirectorySeparatorChar + "temp";
1526 FileStream stream = new FileStream (path, FileMode.OpenOrCreate, FileAccess.Write);
1528 stream.EndWrite (stream.BeginWrite (new byte[8], 0, 8, null, null));
1531 static IAsyncResult DoBeginWrite(Stream stream, ManualResetEvent mre, byte[] RandomBuffer)
1533 return stream.BeginWrite (RandomBuffer, 0, RandomBuffer.Length, ar => {
1534 stream.EndWrite (ar);
1536 // we don't supply an ManualResetEvent so this will throw an NRE on the second run
1537 // which nunit-console will ignore (but other test runners don't like that)
1541 DoBeginWrite (stream, null, RandomBuffer).AsyncWaitHandle.WaitOne ();
1547 public void BeginWrite_Recursive ()
1549 string path = TempFolder + Path.DirectorySeparatorChar + "temp";
1552 using (FileStream stream = new FileStream (path, FileMode.OpenOrCreate, FileAccess.Write)) {
1553 var mre = new ManualResetEvent (false);
1554 var RandomBuffer = new byte[1024];
1555 DoBeginWrite (stream, mre, RandomBuffer);
1556 Assert.IsTrue (mre.WaitOne (5000), "#1");
1561 [ExpectedException (typeof (ObjectDisposedException))]
1562 public void Lock_Disposed ()
1564 string path = TempFolder + Path.DirectorySeparatorChar + "temp";
1566 FileStream stream = new FileStream (path, FileMode.OpenOrCreate, FileAccess.Write);
1572 [ExpectedException (typeof (ObjectDisposedException))]
1573 public void Unlock_Disposed ()
1575 string path = TempFolder + Path.DirectorySeparatorChar + "temp";
1577 FileStream stream = new FileStream (path, FileMode.OpenOrCreate, FileAccess.Write);
1579 stream.Unlock (0, 1);
1583 public void ReadBytePastEndOfStream ()
1585 string path = TempFolder + Path.DirectorySeparatorChar + "temp";
1587 using (FileStream stream = new FileStream (path, FileMode.OpenOrCreate, FileAccess.Read)) {
1588 stream.Seek (0, SeekOrigin.End);
1589 Assert.AreEqual (-1, stream.ReadByte ());
1595 [ExpectedException (typeof (NotSupportedException))]
1596 public void SetLengthWithClosedBaseStream ()
1598 string fn = Path.Combine (TempFolder, "temp");
1600 FileStream fs = new FileStream (fn, FileMode.Create);
1601 BufferedStream bs = new BufferedStream (fs);
1604 bs.SetLength (1000);
1611 public void LengthAfterWrite ()
1613 string path = TempFolder + DSC + "oneofthefilescreated.txt";
1614 FileStream fs = null;
1617 fs = new FileStream (path, FileMode.CreateNew);
1618 fs.WriteByte (Convert.ToByte ('A'));
1619 byte [] buffer = Encoding.ASCII.GetBytes (" is a first character.");
1620 fs.Write (buffer, 0, buffer.Length);
1621 fs.Seek (0, SeekOrigin.Begin);
1622 char a = Convert.ToChar (fs.ReadByte ());
1623 Assert.AreEqual ('A', a, "#A1");
1624 Assert.AreEqual (23, fs.Length, "#A2");
1625 int nread = fs.Read (buffer, 0, 5);
1626 Assert.AreEqual (5, nread, "#A3");
1635 public void DeleteOnClose ()
1637 string path = TempFolder + DSC + "created.txt";
1639 FileStream fs = new FileStream (path, FileMode.CreateNew, FileAccess.Write, FileShare.None, 1024,
1640 FileOptions.DeleteOnClose);
1641 Assert.AreEqual (true, File.Exists (path), "DOC#1");
1643 Assert.AreEqual (false, File.Exists (path), "DOC#2");
1649 public void WriteWithExposedHandle ()
1651 string path = TempFolder + DSC + "exposed-handle.txt";
1652 FileStream fs1 = null;
1653 FileStream fs2 = null;
1657 fs1 = new FileStream (path, FileMode.Create);
1658 fs2 = new FileStream (fs1.SafeFileHandle, FileAccess.ReadWrite);
1659 fs1.WriteByte (Convert.ToByte ('H'));
1660 fs1.WriteByte (Convert.ToByte ('E'));
1661 fs1.WriteByte (Convert.ToByte ('L'));
1662 fs2.WriteByte (Convert.ToByte ('L'));
1663 fs2.WriteByte (Convert.ToByte ('O'));
1664 long fs1Pos = fs1.Position;
1670 var check = Encoding.ASCII.GetString (File.ReadAllBytes (path));
1671 Assert.AreEqual ("HELLO", check, "EXPOSED#1");
1672 Assert.AreEqual (5, fs1Pos, "EXPOSED#2");