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)
12 using NUnit.Framework;
17 namespace MonoTests.System.IO
20 public class FileStreamTest
22 string TempFolder = Path.Combine (Path.GetTempPath (), "MonoTests.System.IO.Tests");
23 static readonly char DSC = Path.DirectorySeparatorChar;
26 public void TearDown ()
28 if (Directory.Exists (TempFolder))
29 Directory.Delete (TempFolder, true);
35 if (Directory.Exists (TempFolder))
36 Directory.Delete (TempFolder, true);
38 Directory.CreateDirectory (TempFolder);
41 public void TestCtr ()
43 string path = TempFolder + DSC + "testfilestream.tmp.1";
45 FileStream stream = null;
47 stream = new FileStream (path, FileMode.Create);
57 [ExpectedException (typeof (ArgumentException))]
58 public void CtorArgumentException1 ()
61 stream = new FileStream ("", FileMode.Create);
66 [ExpectedException (typeof (ArgumentNullException))]
67 public void CtorArgumentNullException ()
69 FileStream stream = new FileStream (null, FileMode.Create);
74 [ExpectedException (typeof (FileNotFoundException))]
75 public void CtorFileNotFoundException1 ()
77 string path = TempFolder + DSC + "thisfileshouldnotexists.test";
79 FileStream stream = null;
81 stream = new FileStream (TempFolder + DSC + "thisfileshouldnotexists.test", FileMode.Open);
90 [ExpectedException (typeof (FileNotFoundException))]
91 public void CtorFileNotFoundException2 ()
93 string path = TempFolder + DSC + "thisfileshouldNOTexists.test";
95 FileStream stream = null;
98 stream = new FileStream (TempFolder + DSC + "thisfileshouldNOTexists.test", FileMode.Truncate);
108 [ExpectedException (typeof (IOException))]
109 public void CtorIOException1 ()
111 string path = TempFolder + DSC + "thisfileshouldexists.test";
112 FileStream stream = null;
115 stream = new FileStream (path, FileMode.CreateNew);
118 stream = new FileStream (path, FileMode.CreateNew);
129 [ExpectedException (typeof (ArgumentOutOfRangeException))]
130 public void CtorArgumentOutOfRangeException1 ()
132 FileStream stream = null;
133 string path = TempFolder + Path.DirectorySeparatorChar + "temp";
136 stream = new FileStream (path, FileMode.Append | FileMode.CreateNew);
145 [ExpectedException (typeof (ArgumentOutOfRangeException))]
146 public void CtorArgumentOutOfRangeException2 ()
148 FileStream stream = null;
149 string path = TempFolder + Path.DirectorySeparatorChar + "temp";
152 stream = new FileStream ("test.test.test", FileMode.Append | FileMode.Open);
160 private void CtorDirectoryNotFoundException (FileMode mode)
162 string path = TempFolder + DSC + "thisDirectoryShouldNotExists";
163 if (Directory.Exists (path))
164 Directory.Delete (path, true);
166 FileStream stream = null;
168 stream = new FileStream (path + DSC + "eitherthisfile.test", mode);
174 if (Directory.Exists (path))
175 Directory.Delete (path, true);
180 [ExpectedException (typeof (DirectoryNotFoundException))]
181 public void CtorDirectoryNotFoundException_CreateNew ()
183 CtorDirectoryNotFoundException (FileMode.CreateNew);
187 [ExpectedException (typeof (DirectoryNotFoundException))]
188 public void CtorDirectoryNotFoundException_Create ()
190 CtorDirectoryNotFoundException (FileMode.Create);
194 [ExpectedException (typeof (DirectoryNotFoundException))]
195 public void CtorDirectoryNotFoundException_Open ()
197 CtorDirectoryNotFoundException (FileMode.Open);
201 [ExpectedException (typeof (DirectoryNotFoundException))]
202 public void CtorDirectoryNotFoundException_OpenOrCreate ()
204 CtorDirectoryNotFoundException (FileMode.OpenOrCreate);
208 [ExpectedException (typeof (DirectoryNotFoundException))]
209 public void CtorDirectoryNotFoundException_Truncate ()
211 CtorDirectoryNotFoundException (FileMode.Truncate);
215 [ExpectedException (typeof (DirectoryNotFoundException))]
216 public void CtorDirectoryNotFoundException_Append ()
218 CtorDirectoryNotFoundException (FileMode.Append);
223 // FileShare.Inheritable is ignored, but file does not exist
224 [ExpectedException (typeof (FileNotFoundException))]
226 // share: Enum value was out of legal range.
227 // (FileShare.Inheritable is not valid)
228 [ExpectedException (typeof (ArgumentOutOfRangeException))]
230 public void CtorArgumentOutOfRangeException3 ()
232 string path = TempFolder + DSC + "CtorArgumentOutOfRangeException1";
235 FileStream stream = null;
237 stream = new FileStream (path, FileMode.Open, FileAccess.Read, FileShare.Inheritable);
246 [ExpectedException (typeof (ArgumentOutOfRangeException))]
247 public void CtorArgumentOutOfRangeException4 ()
249 string path = TempFolder + DSC + "CtorArgumentOutOfRangeException4";
252 FileStream stream = null;
254 stream = new FileStream (path, FileMode.OpenOrCreate, FileAccess.Read, FileShare.ReadWrite, -1);
263 [ExpectedException (typeof (ArgumentOutOfRangeException))]
264 public void CtorBufferSizeZero ()
266 // Buffer size can't be zero
268 string path = Path.Combine (TempFolder, "CtorBufferSizeZero");
271 FileStream stream = null;
273 stream = new FileStream (path, FileMode.CreateNew, FileAccess.Write, FileShare.ReadWrite, 0);
282 [ExpectedException (typeof (ArgumentException))]
283 public void CtorArgumentException2 ()
285 // FileMode.CreateNew && FileAccess.Read
287 string path = TempFolder + Path.DirectorySeparatorChar + "temp";
288 FileStream stream = null;
293 stream = new FileStream (".test.test.test.2", FileMode.CreateNew, FileAccess.Read, FileShare.None | FileShare.Write);
305 // FileShare.Inheritable is ignored, but file does not exist
306 [ExpectedException (typeof (FileNotFoundException))]
308 // share: Enum value was out of legal range.
309 // (FileShare.Inheritable is not valid)
310 [ExpectedException (typeof (ArgumentOutOfRangeException))]
312 public void CtorArgumentOutOfRangeException5 ()
314 string path = TempFolder + Path.DirectorySeparatorChar + "temp";
317 FileStream stream = null;
319 stream = new FileStream (path, FileMode.Open, FileAccess.Read, FileShare.Inheritable | FileShare.ReadWrite);
329 [ExpectedException (typeof (ArgumentException))]
330 public void CtorArgumentException3 ()
332 string path = TempFolder + Path.DirectorySeparatorChar + "temp";
333 FileStream stream = null;
338 stream = new FileStream (".test.test.test.2", FileMode.Truncate, FileAccess.Read);
348 public void ModeAndAccessCombinations ()
350 string path = TempFolder + Path.DirectorySeparatorChar + "temp";
352 FileStream stream = null;
356 // Append access can be requested only in write-only mode
357 stream = new FileStream (path, FileMode.Append, FileAccess.Read);
359 } catch (ArgumentException ex) {
360 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#A2");
368 // Append / ReadWrite
370 // Append access can be requested only in write-only mode
371 stream = new FileStream (path, FileMode.Append, FileAccess.ReadWrite);
373 } catch (ArgumentException ex) {
374 // make sure it is exact this exception, and not a derived type
375 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#B2");
385 stream = new FileStream (path, FileMode.Append, FileAccess.Write);
395 stream = new FileStream (path, FileMode.Create, FileAccess.Read);
397 } catch (ArgumentException ex) {
398 // make sure it is exact this exception, and not a derived type
399 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#C2");
407 // Create / ReadWrite
409 stream = new FileStream (path, FileMode.Create, FileAccess.ReadWrite);
419 stream = new FileStream (path, FileMode.Create, FileAccess.Write);
429 stream = new FileStream (path, FileMode.CreateNew, FileAccess.Read);
431 } catch (ArgumentException ex) {
432 // make sure it is exact this exception, and not a derived type
433 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#D2");
441 // CreateNew / ReadWrite
443 stream = new FileStream (path, FileMode.CreateNew, FileAccess.ReadWrite);
453 stream = new FileStream (path, FileMode.CreateNew, FileAccess.Write);
463 stream = new FileStream (path, FileMode.Open, FileAccess.Read);
465 } catch (FileNotFoundException ex) {
466 // make sure it is exact this exception, and not a derived type
467 Assert.AreEqual (typeof (FileNotFoundException), ex.GetType (), "#E2");
477 stream = new FileStream (path, FileMode.Open, FileAccess.ReadWrite);
479 } catch (FileNotFoundException ex) {
480 // make sure it is exact this exception, and not a derived type
481 Assert.AreEqual (typeof (FileNotFoundException), ex.GetType (), "#F2");
491 stream = new FileStream (path, FileMode.Open, FileAccess.Write);
493 } catch (FileNotFoundException ex) {
494 // make sure it is exact this exception, and not a derived type
495 Assert.AreEqual (typeof (FileNotFoundException), ex.GetType (), "#G2");
503 // OpenOrCreate / Read
505 stream = new FileStream (path, FileMode.OpenOrCreate, FileAccess.Read);
513 // OpenOrCreate / ReadWrite
515 stream = new FileStream (path, FileMode.OpenOrCreate, FileAccess.ReadWrite);
523 // OpenOrCreate / Write
525 stream = new FileStream (path, FileMode.OpenOrCreate, FileAccess.Write);
535 stream = new FileStream (path, FileMode.Truncate, FileAccess.Read);
537 } catch (ArgumentException ex) {
538 // make sure it is exact this exception, and not a derived type
539 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#H2");
547 // Truncate / ReadWrite
549 stream = new FileStream (path, FileMode.Truncate, FileAccess.ReadWrite);
551 } catch (FileNotFoundException ex) {
552 // make sure it is exact this exception, and not a derived type
553 Assert.AreEqual (typeof (FileNotFoundException), ex.GetType (), "#I2");
563 stream = new FileStream (path, FileMode.Truncate, FileAccess.Write);
565 } catch (FileNotFoundException ex) {
566 // make sure it is exact this exception, and not a derived type
567 Assert.AreEqual (typeof (FileNotFoundException), ex.GetType (), "#J2");
576 [Test, ExpectedException (typeof (IOException))]
577 public void CtorIOException2 ()
579 FileStream stream = null;
581 stream = new FileStream (new IntPtr (Int32.MaxValue), FileAccess.Read);
588 [Test, ExpectedException (typeof (IOException))]
589 public void CtorIOException ()
591 string path = TempFolder + DSC + "CTorIOException.Test";
592 FileStream stream = null;
593 FileStream stream2 = null;
597 stream = new FileStream (path, FileMode.CreateNew);
599 // used by an another process
600 stream2 = new FileStream (path, FileMode.OpenOrCreate);
611 public void CtorAccess1Read2Read ()
613 FileStream fs = null;
614 FileStream fs2 = null;
615 string fn = Path.Combine (TempFolder, "temp");
617 if (!File.Exists (fn)) {
618 TextWriter tw = File.CreateText (fn);
622 fs = new FileStream (fn, FileMode.Open, FileAccess.Read);
623 fs2 = new FileStream (fn, FileMode.Open, FileAccess.Read);
629 if (File.Exists (fn))
635 [ExpectedException (typeof (IOException))]
636 public void CtorAccess1Read2Write ()
638 string fn = Path.Combine (TempFolder, "temp");
639 FileStream fs = null;
641 if (!File.Exists (fn)) {
642 using (TextWriter tw = File.CreateText (fn)) {
646 fs = new FileStream (fn, FileMode.Open, FileAccess.Read);
647 fs = new FileStream (fn, FileMode.Create, FileAccess.Write);
651 if (File.Exists (fn))
657 [ExpectedException (typeof (IOException))]
658 public void CtorAccess1Write2Write ()
660 string fn = Path.Combine (TempFolder, "temp");
661 FileStream fs = null;
663 if (File.Exists (fn))
665 fs = new FileStream (fn, FileMode.Create, FileAccess.Write);
666 fs = new FileStream (fn, FileMode.Create, FileAccess.Write);
670 if (File.Exists (fn))
676 [ExpectedException (typeof (UnauthorizedAccessException))]
677 public void CtorReadDirectoryAsFile ()
679 FileStream stream = null;
681 stream = new FileStream (TempFolder, FileMode.Open, FileAccess.Read);
691 string path = TempFolder + DSC + "FileStreamTest.Write";
695 FileStream stream = new FileStream (path, FileMode.CreateNew, FileAccess.ReadWrite, FileShare.ReadWrite, 8);
697 byte[] outbytes = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16 };
698 byte[] bytes = new byte[15];
700 // Check that the data is flushed when we overflow the buffer
701 // with a large amount of data
702 stream.Write (outbytes, 0, 5);
703 stream.Write (outbytes, 5, 10);
704 stream.Seek (0, SeekOrigin.Begin);
706 stream.Read (bytes, 0, 15);
707 for (int i = 0; i < 15; ++i)
708 Assert.AreEqual (i + 1, bytes[i], "#1");
710 // Check that the data is flushed when we overflow the buffer
711 // with a small amount of data
712 stream.Write (outbytes, 0, 7);
713 stream.Write (outbytes, 7, 7);
714 stream.Write (outbytes, 14, 1);
716 stream.Read (bytes, 0, 15);
717 stream.Seek (15, SeekOrigin.Begin);
718 for (int i = 0; i < 15; ++i)
719 Assert.AreEqual (i + 1, bytes[i], "#2");
724 public void Length ()
726 // Test that the Length property takes into account the data
728 string path = TempFolder + DSC + "FileStreamTest.Length";
732 FileStream stream = new FileStream (path, FileMode.CreateNew);
734 byte[] outbytes = new byte[] { 1, 2, 3, 4 };
736 stream.Write (outbytes, 0, 4);
737 Assert.AreEqual (4, stream.Length);
744 string path = TempFolder + DSC + "FileStreamTest.Flush";
745 FileStream stream = null;
746 FileStream stream2 = null;
751 stream = new FileStream (path, FileMode.CreateNew, FileAccess.ReadWrite, FileShare.ReadWrite);
752 stream2 = new FileStream (path, FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite);
754 stream.Write (new byte[] { 1, 2, 3, 4, 5 }, 0, 5);
756 byte[] bytes = new byte[5];
757 stream2.Read (bytes, 0, 5);
758 Assert.AreEqual (0, bytes[0], "#A1");
759 Assert.AreEqual (0, bytes[1], "#A2");
760 Assert.AreEqual (0, bytes[2], "#A3");
761 Assert.AreEqual (0, bytes[3], "#A4");
764 stream2.Read (bytes, 0, 5);
765 Assert.AreEqual (1, bytes[0], "#B1");
766 Assert.AreEqual (2, bytes[1], "#B2");
767 Assert.AreEqual (3, bytes[2], "#B3");
768 Assert.AreEqual (4, bytes[3], "#B4");
779 public void TestDefaultProperties ()
781 string path = TempFolder + Path.DirectorySeparatorChar + "testfilestream.tmp.2";
784 FileStream stream = new FileStream (path, FileMode.Create);
786 Assert.IsTrue (stream.CanRead, "#A1");
787 Assert.IsTrue (stream.CanSeek, "#A2");
788 Assert.IsTrue (stream.CanWrite, "#A3");
789 Assert.IsFalse (stream.IsAsync, "#A4");
790 Assert.IsTrue (stream.Name.EndsWith (path), "#A5");
791 Assert.AreEqual (0, stream.Position, "#A6");
792 Assert.AreEqual ("System.IO.FileStream", stream.ToString (), "#A7");
796 stream = new FileStream (path, FileMode.OpenOrCreate, FileAccess.Read);
797 Assert.IsTrue (stream.CanRead, "#B1");
798 Assert.IsTrue (stream.CanSeek, "#B2");
799 Assert.IsFalse (stream.CanWrite, "#B3");
800 Assert.IsFalse (stream.IsAsync, "#B4");
801 Assert.IsTrue (stream.Name.EndsWith (path), "#B5");
802 Assert.AreEqual (0, stream.Position, "#B6");
803 Assert.AreEqual ("System.IO.FileStream", stream.ToString (), "#B7");
806 stream = new FileStream (path, FileMode.Truncate, FileAccess.Write, FileShare.ReadWrite);
807 Assert.IsFalse (stream.CanRead, "#C1");
808 Assert.IsTrue (stream.CanSeek, "#C2");
809 Assert.IsTrue (stream.CanWrite, "#C3");
810 Assert.IsFalse (stream.IsAsync, "#C4");
811 Assert.IsTrue (stream.Name.EndsWith ("testfilestream.tmp.2"), "#C5");
812 Assert.AreEqual (0, stream.Position, "#C6");
813 Assert.AreEqual ("System.IO.FileStream", stream.ToString (), "#C7");
818 [Category ("NotWorking")]
819 // Bug: 71371 -> duplicate and WONTFIX.
820 public void TestLock_FailsOnMono ()
822 string path = TempFolder + Path.DirectorySeparatorChar + "TestLock";
825 FileStream stream = new FileStream (path, FileMode.CreateNew, FileAccess.ReadWrite);
827 stream.Write (new Byte[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }, 0, 10);
830 stream = new FileStream (path, FileMode.Open, FileAccess.ReadWrite);
834 FileStream stream2 = new FileStream (path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
836 byte[] bytes = new byte[5];
838 stream2.Read (bytes, 0, 5);
840 } catch (Exception e) {
841 Assert.AreEqual (typeof (IOException), e.GetType (), "#2");
850 public void TestLock ()
852 string path = TempFolder + Path.DirectorySeparatorChar + "TestLock";
855 FileStream stream = new FileStream (path, FileMode.CreateNew, FileAccess.ReadWrite);
857 stream.Write (new Byte[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }, 0, 10);
860 stream = new FileStream (path, FileMode.Open, FileAccess.ReadWrite);
864 FileStream stream2 = new FileStream (path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
866 byte[] bytes = new byte[5];
868 stream2.Read (bytes, 0, 5);
870 } catch (Exception) {
871 // Bug #71371: on MS.NET you get an IOException detailing a lock
872 // Assert.AreEqual (typeof (IOException), e.GetType (), "#A2");
875 stream2.Seek (5, SeekOrigin.Begin);
876 stream2.Read (bytes, 0, 5);
878 Assert.AreEqual (5, bytes[0], "#B1");
879 Assert.AreEqual (6, bytes[1], "#B2");
880 Assert.AreEqual (7, bytes[2], "#B3");
881 Assert.AreEqual (8, bytes[3], "#B4");
882 Assert.AreEqual (9, bytes[4], "#B5");
884 stream.Unlock (0, 5);
885 stream2.Seek (0, SeekOrigin.Begin);
886 stream2.Read (bytes, 0, 5);
888 Assert.AreEqual (0, bytes[0], "#C1");
889 Assert.AreEqual (1, bytes[1], "#C2");
890 Assert.AreEqual (2, bytes[2], "#C3");
891 Assert.AreEqual (3, bytes[3], "#C4");
892 Assert.AreEqual (4, bytes[4], "#C5");
903 string path = TempFolder + DSC + "FST.Seek.Test";
906 FileStream stream = new FileStream (path, FileMode.CreateNew, FileAccess.ReadWrite, FileShare.ReadWrite);
907 FileStream stream2 = new FileStream (path, FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite);
909 stream.Write (new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 10 }, 0, 9);
910 Assert.AreEqual (5, stream2.Seek (5, SeekOrigin.Begin), "#1");
911 Assert.AreEqual (-1, stream2.ReadByte (), "#2");
913 Assert.AreEqual (2, stream2.Seek (-3, SeekOrigin.Current), "#3");
914 Assert.AreEqual (-1, stream2.ReadByte (), "#4");
916 Assert.AreEqual (12, stream.Seek (3, SeekOrigin.Current), "#5");
917 Assert.AreEqual (-1, stream.ReadByte (), "#6");
919 Assert.AreEqual (5, stream.Seek (-7, SeekOrigin.Current), "#7");
920 Assert.AreEqual (6, stream.ReadByte (), "#8");
922 Assert.AreEqual (5, stream2.Seek (5, SeekOrigin.Begin), "#9");
923 Assert.AreEqual (6, stream2.ReadByte (), "#10");
931 public void TestSeek ()
933 string path = TempFolder + Path.DirectorySeparatorChar + "TestSeek";
936 FileStream stream = new FileStream (path, FileMode.CreateNew, FileAccess.ReadWrite, FileShare.ReadWrite);
937 stream.Write (new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }, 0, 10);
939 stream.Seek (5, SeekOrigin.End);
940 Assert.AreEqual (-1, stream.ReadByte (), "#1");
942 stream.Seek (-5, SeekOrigin.End);
943 Assert.AreEqual (6, stream.ReadByte (), "#2");
946 stream.Seek (-11, SeekOrigin.End);
948 } catch (Exception e) {
949 Assert.AreEqual (typeof (IOException), e.GetType (), "#4");
952 stream.Seek (19, SeekOrigin.Begin);
953 Assert.AreEqual (-1, stream.ReadByte (), "#5");
955 stream.Seek (1, SeekOrigin.Begin);
956 Assert.AreEqual (2, stream.ReadByte (), "#6");
958 stream.Seek (3, SeekOrigin.Current);
959 Assert.AreEqual (6, stream.ReadByte (), "#7");
961 stream.Seek (-2, SeekOrigin.Current);
962 Assert.AreEqual (5, stream.ReadByte (), "#8");
966 // Test that seeks work correctly when seeking inside the buffer
967 stream.Seek (0, SeekOrigin.Begin);
968 stream.WriteByte (0);
969 stream.WriteByte (1);
970 stream.Seek (0, SeekOrigin.Begin);
971 byte[] buf = new byte[1];
973 stream.Write (buf, 0, 1);
974 stream.Write (buf, 0, 1);
976 stream.Seek (0, SeekOrigin.Begin);
977 Assert.AreEqual (2, stream.ReadByte (), "#9");
978 Assert.AreEqual (2, stream.ReadByte (), "#10");
985 public void TestClose ()
987 string path = TempFolder + Path.DirectorySeparatorChar + "TestClose";
990 FileStream stream = new FileStream (path, FileMode.CreateNew, FileAccess.ReadWrite);
992 stream.Write (new byte[] { 1, 2, 3, 4 }, 0, 4);
999 } catch (Exception e) {
1000 Assert.AreEqual (typeof (ObjectDisposedException), e.GetType (), "#A2");
1004 stream.WriteByte (64);
1005 Assert.Fail ("#B1");
1006 } catch (Exception e) {
1007 Assert.AreEqual (typeof (ObjectDisposedException), e.GetType (), "#B2");
1012 Assert.Fail ("#C1");
1013 } catch (Exception e) {
1014 Assert.AreEqual (typeof (ObjectDisposedException), e.GetType (), "#C2");
1018 long l = stream.Length;
1019 Assert.Fail ("#D1");
1020 } catch (Exception e) {
1021 Assert.AreEqual (typeof (ObjectDisposedException), e.GetType (), "#D2");
1025 long l = stream.Position;
1026 Assert.Fail ("#E1");
1027 } catch (Exception e) {
1028 Assert.AreEqual (typeof (ObjectDisposedException), e.GetType (), "#E2");
1031 Assert.IsFalse (stream.CanRead, "#F1");
1032 Assert.IsFalse (stream.CanSeek, "#F2");
1033 Assert.IsFalse (stream.CanWrite, "#F3");
1034 Assert.IsTrue (stream.Name.EndsWith (path), "#F4");
1041 /// Checks whether the <see cref="FileStream" /> throws a <see cref="NotSupportedException" />
1042 /// when the stream is opened with access mode <see cref="FileAccess.Read" /> and the
1043 /// <see cref="FileStream.Write(byte[], int, int)" /> method is called.
1046 [ExpectedException (typeof (NotSupportedException))]
1047 public void TestWriteVerifyAccessMode ()
1049 string path = TempFolder + Path.DirectorySeparatorChar + "temp";
1052 FileStream stream = null;
1056 buffer = Encoding.ASCII.GetBytes ("test");
1057 stream = new FileStream (path, FileMode.OpenOrCreate, FileAccess.Read);
1058 stream.Write (buffer, 0, buffer.Length);
1067 /// Checks whether the <see cref="FileStream" /> throws a <see cref="NotSupportedException" />
1068 /// when the stream is opened with access mode <see cref="FileAccess.Read" /> and the
1069 /// <see cref="FileStream.WriteByte(byte)" /> method is called.
1072 [ExpectedException (typeof (NotSupportedException))]
1073 public void TestWriteByteVerifyAccessMode ()
1075 string path = TempFolder + Path.DirectorySeparatorChar + "temp";
1078 FileStream stream = null;
1081 stream = new FileStream (path, FileMode.OpenOrCreate, FileAccess.Read);
1082 stream.WriteByte (Byte.MinValue);
1091 /// Checks whether the <see cref="FileStream" /> throws a <see cref="NotSupportedException" />
1092 /// when the stream is opened with access mode <see cref="FileAccess.Write" /> and the
1093 /// <see cref="FileStream.Read(byte[], int, int)" /> method is called.
1096 [ExpectedException (typeof (NotSupportedException))]
1097 public void TestReadVerifyAccessMode ()
1099 string path = TempFolder + Path.DirectorySeparatorChar + "temp";
1102 FileStream stream = null;
1103 byte[] buffer = new byte[100];
1106 stream = new FileStream (path, FileMode.OpenOrCreate, FileAccess.Write);
1107 stream.Read (buffer, 0, buffer.Length);
1115 /// Checks whether the <see cref="FileStream" /> throws a <see cref="NotSupportedException" />
1116 /// when the stream is opened with access mode <see cref="FileAccess.Write" /> and the
1117 /// <see cref="FileStream.ReadByte()" /> method is called.
1120 [ExpectedException (typeof (NotSupportedException))]
1121 public void TestReadByteVerifyAccessMode ()
1123 string path = TempFolder + Path.DirectorySeparatorChar + "temp";
1126 FileStream stream = null;
1129 stream = new FileStream (path, FileMode.OpenOrCreate, FileAccess.Write);
1130 int readByte = stream.ReadByte ();
1138 // Check that the stream is flushed even when it doesn't own the
1141 public void TestFlushNotOwningHandle ()
1143 string path = Path.Combine (TempFolder, "TestFlushNotOwningHandle");
1146 FileStream s = new FileStream (path, FileMode.Create);
1147 using (FileStream s2 = new FileStream (s.Handle, FileAccess.Write, false)) {
1148 byte[] buf = new byte[2];
1150 s2.Write (buf, 0, 1);
1154 Assert.AreEqual ((int) '1', s.ReadByte ());
1158 private void DeleteFile (string path)
1160 if (File.Exists (path))
1165 [ExpectedException (typeof (ArgumentOutOfRangeException))]
1166 public void Read_OffsetNegative ()
1168 string path = TempFolder + Path.DirectorySeparatorChar + "temp";
1171 using (FileStream stream = new FileStream (path, FileMode.OpenOrCreate, FileAccess.Read)) {
1172 stream.Read (new byte[0], -1, 1);
1177 [ExpectedException (typeof (ArgumentException))]
1178 public void Read_OffsetOverflow ()
1180 string path = TempFolder + Path.DirectorySeparatorChar + "temp";
1183 using (FileStream stream = new FileStream (path, FileMode.OpenOrCreate, FileAccess.Read)) {
1184 stream.Read (new byte[0], Int32.MaxValue, 1);
1189 [ExpectedException (typeof (ArgumentOutOfRangeException))]
1190 public void Read_CountNegative ()
1192 string path = TempFolder + Path.DirectorySeparatorChar + "temp";
1195 using (FileStream stream = new FileStream (path, FileMode.OpenOrCreate, FileAccess.Read)) {
1196 stream.Read (new byte[0], 1, -1);
1201 [ExpectedException (typeof (ArgumentException))]
1202 public void Read_CountOverflow ()
1204 string path = TempFolder + Path.DirectorySeparatorChar + "temp";
1207 using (FileStream stream = new FileStream (path, FileMode.OpenOrCreate, FileAccess.Read)) {
1208 stream.Read (new byte[0], 1, Int32.MaxValue);
1213 [ExpectedException (typeof (ArgumentOutOfRangeException))]
1214 public void Write_OffsetNegative ()
1216 string path = TempFolder + Path.DirectorySeparatorChar + "temp";
1219 using (FileStream stream = new FileStream (path, FileMode.OpenOrCreate, FileAccess.Write)) {
1220 stream.Write (new byte[0], -1, 1);
1225 [ExpectedException (typeof (ArgumentException))]
1226 public void Write_OffsetOverflow ()
1228 string path = TempFolder + Path.DirectorySeparatorChar + "temp";
1231 using (FileStream stream = new FileStream (path, FileMode.OpenOrCreate, FileAccess.Write)) {
1232 stream.Write (new byte[0], Int32.MaxValue, 1);
1237 [ExpectedException (typeof (ArgumentOutOfRangeException))]
1238 public void Write_CountNegative ()
1240 string path = TempFolder + Path.DirectorySeparatorChar + "temp";
1243 using (FileStream stream = new FileStream (path, FileMode.OpenOrCreate, FileAccess.Write)) {
1244 stream.Write (new byte[0], 1, -1);
1249 [ExpectedException (typeof (ArgumentException))]
1250 public void Write_CountOverflow ()
1252 string path = TempFolder + Path.DirectorySeparatorChar + "temp";
1255 using (FileStream stream = new FileStream (path, FileMode.OpenOrCreate, FileAccess.Write)) {
1256 stream.Write (new byte[0], 1, Int32.MaxValue);
1261 [ExpectedException (typeof (ArgumentException))]
1262 public void Seek_InvalidSeekOrigin ()
1264 string path = TempFolder + Path.DirectorySeparatorChar + "temp";
1267 using (FileStream stream = new FileStream (path, FileMode.OpenOrCreate, FileAccess.Read)) {
1268 stream.Seek (0, (SeekOrigin) (-1));
1273 [ExpectedException (typeof (ArgumentException))]
1274 public void Constructor_InvalidFileHandle ()
1276 new FileStream ((IntPtr) (-1L), FileAccess.Read);
1280 public void PositionAfterSetLength ()
1282 string path = TempFolder + Path.DirectorySeparatorChar + "temp";
1285 using (FileStream stream = new FileStream (path, FileMode.OpenOrCreate, FileAccess.Write)) {
1286 stream.SetLength (32);
1287 stream.Position = 32;
1288 stream.SetLength (16);
1289 Assert.AreEqual (16, stream.Position);
1294 [ExpectedException (typeof (ObjectDisposedException))]
1295 public void SetLength_Disposed ()
1297 string path = TempFolder + Path.DirectorySeparatorChar + "temp";
1299 FileStream stream = new FileStream (path, FileMode.OpenOrCreate, FileAccess.Write);
1301 stream.SetLength (16);
1305 [ExpectedException (typeof (ObjectDisposedException))]
1306 public void Position_Disposed ()
1308 string path = TempFolder + Path.DirectorySeparatorChar + "temp";
1310 FileStream stream = new FileStream (path, FileMode.OpenOrCreate, FileAccess.Read);
1312 stream.Position = 0;
1316 [ExpectedException (typeof (ObjectDisposedException))]
1317 public void BeginRead_Disposed ()
1319 string path = TempFolder + Path.DirectorySeparatorChar + "temp";
1321 FileStream stream = new FileStream (path, FileMode.OpenOrCreate, FileAccess.Read);
1323 stream.EndRead (stream.BeginRead (new byte[8], 0, 8, null, null));
1327 [ExpectedException (typeof (ObjectDisposedException))]
1328 public void BeginWrite_Disposed ()
1330 string path = TempFolder + Path.DirectorySeparatorChar + "temp";
1332 FileStream stream = new FileStream (path, FileMode.OpenOrCreate, FileAccess.Write);
1334 stream.EndWrite (stream.BeginWrite (new byte[8], 0, 8, null, null));
1338 [ExpectedException (typeof (ObjectDisposedException))]
1339 public void Lock_Disposed ()
1341 string path = TempFolder + Path.DirectorySeparatorChar + "temp";
1343 FileStream stream = new FileStream (path, FileMode.OpenOrCreate, FileAccess.Write);
1349 [ExpectedException (typeof (ObjectDisposedException))]
1350 public void Unlock_Disposed ()
1352 string path = TempFolder + Path.DirectorySeparatorChar + "temp";
1354 FileStream stream = new FileStream (path, FileMode.OpenOrCreate, FileAccess.Write);
1356 stream.Unlock (0, 1);
1360 public void ReadBytePastEndOfStream ()
1362 string path = TempFolder + Path.DirectorySeparatorChar + "temp";
1364 using (FileStream stream = new FileStream (path, FileMode.OpenOrCreate, FileAccess.Read)) {
1365 stream.Seek (0, SeekOrigin.End);
1366 Assert.AreEqual (-1, stream.ReadByte ());
1372 [ExpectedException (typeof (NotSupportedException))]
1373 public void SetLengthWithClosedBaseStream ()
1375 string fn = Path.Combine (TempFolder, "temp");
1377 FileStream fs = new FileStream (fn, FileMode.Create);
1378 BufferedStream bs = new BufferedStream (fs);
1381 bs.SetLength (1000);
1388 public void LengthAfterWrite ()
1390 string path = TempFolder + DSC + "oneofthefilescreated.txt";
1391 FileStream fs = null;
1394 fs = new FileStream (path, FileMode.CreateNew);
1395 fs.WriteByte (Convert.ToByte ('A'));
1396 byte [] buffer = Encoding.ASCII.GetBytes (" is a first character.");
1397 fs.Write (buffer, 0, buffer.Length);
1398 fs.Seek (0, SeekOrigin.Begin);
1399 char a = Convert.ToChar (fs.ReadByte ());
1400 Assert.AreEqual ('A', a, "#A1");
1401 Assert.AreEqual (23, fs.Length, "#A2");
1402 int nread = fs.Read (buffer, 0, 5);
1403 Assert.AreEqual (5, nread, "#A3");
1412 [Test] public void DeleteOnClose ()
1414 string path = TempFolder + DSC + "created.txt";
1416 FileStream fs = new FileStream (path, FileMode.CreateNew, FileAccess.Write, FileShare.None, 1024,
1417 FileOptions.DeleteOnClose);
1418 Assert.AreEqual (true, File.Exists (path), "DOC#1");
1420 Assert.AreEqual (false, File.Exists (path), "DOC#2");