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 public void CtorFileNotFoundException_Mode_Open ()
77 string path = TempFolder + DSC + "thisfileshouldnotexists.test";
79 FileStream stream = null;
81 stream = new FileStream (TempFolder + DSC + "thisfileshouldnotexists.test", FileMode.Open);
83 } catch (FileNotFoundException ex) {
84 Assert.AreEqual (typeof (FileNotFoundException), ex.GetType (), "#A2");
85 Assert.AreEqual (path, ex.FileName, "#A3");
86 Assert.IsNull (ex.InnerException, "#A4");
87 Assert.IsNotNull (ex.Message, "#A5");
88 Assert.IsTrue (ex.Message.IndexOf (path) != -1, "#A6");
98 string orignalCurrentDir = Directory.GetCurrentDirectory ();
99 Directory.SetCurrentDirectory (TempFolder);
101 stream = new FileStream ("thisfileshouldnotexists.test", FileMode.Open);
103 } catch (FileNotFoundException ex) {
104 Assert.AreEqual (typeof (FileNotFoundException), ex.GetType (), "#B2");
105 Assert.AreEqual (path, ex.FileName, "#B3");
106 Assert.IsNull (ex.InnerException, "#B4");
107 Assert.IsNotNull (ex.Message, "#B5");
108 Assert.IsTrue (ex.Message.IndexOf (path) != -1, "#B6");
110 // restore original current directory
111 Directory.SetCurrentDirectory (orignalCurrentDir);
119 public void CtorFileNotFoundException_Mode_Truncate ()
122 string path = TempFolder + DSC + "thisfileshouldNOTexists.test";
124 FileStream stream = null;
126 stream = new FileStream (path, FileMode.Truncate);
128 } catch (FileNotFoundException ex) {
129 Assert.AreEqual (typeof (FileNotFoundException), ex.GetType (), "#A2");
130 Assert.AreEqual (path, ex.FileName, "#A3");
131 Assert.IsNull (ex.InnerException, "#A4");
132 Assert.IsNotNull (ex.Message, "#A5");
133 Assert.IsTrue (ex.Message.IndexOf (path) != -1, "#A6");
135 if (stream != null) {
143 string orignalCurrentDir = Directory.GetCurrentDirectory ();
144 Directory.SetCurrentDirectory (TempFolder);
146 stream = new FileStream ("thisfileshouldNOTexists.test", FileMode.Truncate);
148 } catch (FileNotFoundException ex) {
149 Assert.AreEqual (typeof (FileNotFoundException), ex.GetType (), "#B2");
150 Assert.AreEqual (path, ex.FileName, "#B3");
151 Assert.IsNull (ex.InnerException, "#B4");
152 Assert.IsNotNull (ex.Message, "#B5");
153 Assert.IsTrue (ex.Message.IndexOf (path) != -1, "#B6");
155 // restore original current directory
156 Directory.SetCurrentDirectory (orignalCurrentDir);
164 public void CtorIOException1 ()
167 string path = TempFolder + DSC + "thisfileshouldexists.test";
168 FileStream stream = null;
171 stream = new FileStream (path, FileMode.CreateNew);
174 stream = new FileStream (path, FileMode.CreateNew);
176 } catch (IOException ex) {
177 Assert.AreEqual (typeof (IOException), ex.GetType (), "#A2");
178 Assert.IsNull (ex.InnerException, "#A3");
179 Assert.IsNotNull (ex.Message, "#A4");
180 Assert.IsTrue (ex.Message.IndexOf (path) != -1, "#A5");
182 if (stream != null) {
190 string orignalCurrentDir = Directory.GetCurrentDirectory ();
191 Directory.SetCurrentDirectory (TempFolder);
193 stream = new FileStream ("thisfileshouldexists.test", FileMode.CreateNew);
196 stream = new FileStream ("thisfileshouldexists.test", FileMode.CreateNew);
198 } catch (IOException ex) {
199 Assert.AreEqual (typeof (IOException), ex.GetType (), "#B2");
200 Assert.IsNull (ex.InnerException, "#B3");
201 Assert.IsNotNull (ex.Message, "#B4");
202 Assert.IsTrue (ex.Message.IndexOf (path) != -1, "#B5");
204 // restore original current directory
205 Directory.SetCurrentDirectory (orignalCurrentDir);
213 [ExpectedException (typeof (ArgumentOutOfRangeException))]
214 public void CtorArgumentOutOfRangeException1 ()
216 FileStream stream = null;
217 string path = TempFolder + Path.DirectorySeparatorChar + "temp";
220 stream = new FileStream (path, FileMode.Append | FileMode.CreateNew);
229 [ExpectedException (typeof (ArgumentOutOfRangeException))]
230 public void CtorArgumentOutOfRangeException2 ()
232 FileStream stream = null;
233 string path = TempFolder + Path.DirectorySeparatorChar + "temp";
236 stream = new FileStream ("test.test.test", FileMode.Append | FileMode.Open);
244 private void CtorDirectoryNotFoundException (FileMode mode)
246 string path = TempFolder + DSC + "thisDirectoryShouldNotExists";
247 if (Directory.Exists (path))
248 Directory.Delete (path, true);
250 FileStream stream = null;
252 stream = new FileStream (path + DSC + "eitherthisfile.test", mode);
258 if (Directory.Exists (path))
259 Directory.Delete (path, true);
264 [ExpectedException (typeof (DirectoryNotFoundException))]
265 public void CtorDirectoryNotFoundException_CreateNew ()
267 CtorDirectoryNotFoundException (FileMode.CreateNew);
271 [ExpectedException (typeof (DirectoryNotFoundException))]
272 public void CtorDirectoryNotFoundException_Create ()
274 CtorDirectoryNotFoundException (FileMode.Create);
278 [ExpectedException (typeof (DirectoryNotFoundException))]
279 public void CtorDirectoryNotFoundException_Open ()
281 CtorDirectoryNotFoundException (FileMode.Open);
285 [ExpectedException (typeof (DirectoryNotFoundException))]
286 public void CtorDirectoryNotFoundException_OpenOrCreate ()
288 CtorDirectoryNotFoundException (FileMode.OpenOrCreate);
292 [ExpectedException (typeof (DirectoryNotFoundException))]
293 public void CtorDirectoryNotFoundException_Truncate ()
295 CtorDirectoryNotFoundException (FileMode.Truncate);
299 [ExpectedException (typeof (DirectoryNotFoundException))]
300 public void CtorDirectoryNotFoundException_Append ()
302 CtorDirectoryNotFoundException (FileMode.Append);
306 public void CtorDirectoryNotFound_RelativePath ()
308 string orignalCurrentDir = Directory.GetCurrentDirectory ();
309 Directory.SetCurrentDirectory (TempFolder);
310 string relativePath = "DirectoryDoesNotExist" + Path.DirectorySeparatorChar + "file.txt";
311 string fullPath = Path.Combine (TempFolder, relativePath);
313 new FileStream (relativePath, FileMode.Open);
315 } catch (DirectoryNotFoundException ex) {
316 Assert.AreEqual (typeof (DirectoryNotFoundException), ex.GetType (), "#A2");
317 Assert.IsNull (ex.InnerException, "#A3");
318 Assert.IsNotNull (ex.Message, "#A4");
319 Assert.IsTrue (ex.Message.IndexOf (fullPath) != -1, "#A5");
321 // restore original current directory
322 Directory.SetCurrentDirectory (orignalCurrentDir);
328 // FileShare.Inheritable is ignored, but file does not exist
329 [ExpectedException (typeof (FileNotFoundException))]
331 // share: Enum value was out of legal range.
332 // (FileShare.Inheritable is not valid)
333 [ExpectedException (typeof (ArgumentOutOfRangeException))]
335 public void CtorArgumentOutOfRangeException3 ()
337 string path = TempFolder + DSC + "CtorArgumentOutOfRangeException1";
340 FileStream stream = null;
342 stream = new FileStream (path, FileMode.Open, FileAccess.Read, FileShare.Inheritable);
351 [ExpectedException (typeof (ArgumentOutOfRangeException))]
352 public void CtorArgumentOutOfRangeException4 ()
354 string path = TempFolder + DSC + "CtorArgumentOutOfRangeException4";
357 FileStream stream = null;
359 stream = new FileStream (path, FileMode.OpenOrCreate, FileAccess.Read, FileShare.ReadWrite, -1);
368 [ExpectedException (typeof (ArgumentOutOfRangeException))]
369 public void CtorBufferSizeZero ()
371 // Buffer size can't be zero
373 string path = Path.Combine (TempFolder, "CtorBufferSizeZero");
376 FileStream stream = null;
378 stream = new FileStream (path, FileMode.CreateNew, FileAccess.Write, FileShare.ReadWrite, 0);
387 [ExpectedException (typeof (ArgumentException))]
388 public void CtorArgumentException2 ()
390 // FileMode.CreateNew && FileAccess.Read
392 string path = TempFolder + Path.DirectorySeparatorChar + "temp";
393 FileStream stream = null;
398 stream = new FileStream (".test.test.test.2", FileMode.CreateNew, FileAccess.Read, FileShare.None | FileShare.Write);
410 // FileShare.Inheritable is ignored, but file does not exist
411 [ExpectedException (typeof (FileNotFoundException))]
413 // share: Enum value was out of legal range.
414 // (FileShare.Inheritable is not valid)
415 [ExpectedException (typeof (ArgumentOutOfRangeException))]
417 public void CtorArgumentOutOfRangeException5 ()
419 string path = TempFolder + Path.DirectorySeparatorChar + "temp";
422 FileStream stream = null;
424 stream = new FileStream (path, FileMode.Open, FileAccess.Read, FileShare.Inheritable | FileShare.ReadWrite);
434 [ExpectedException (typeof (ArgumentException))]
435 public void CtorArgumentException3 ()
437 string path = TempFolder + Path.DirectorySeparatorChar + "temp";
438 FileStream stream = null;
443 stream = new FileStream (".test.test.test.2", FileMode.Truncate, FileAccess.Read);
453 public void ModeAndAccessCombinations ()
455 string path = TempFolder + Path.DirectorySeparatorChar + "temp";
457 FileStream stream = null;
461 // Append access can be requested only in write-only mode
462 stream = new FileStream (path, FileMode.Append, FileAccess.Read);
464 } catch (ArgumentException ex) {
465 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#A2");
473 // Append / ReadWrite
475 // Append access can be requested only in write-only mode
476 stream = new FileStream (path, FileMode.Append, FileAccess.ReadWrite);
478 } catch (ArgumentException ex) {
479 // make sure it is exact this exception, and not a derived type
480 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#B2");
490 stream = new FileStream (path, FileMode.Append, FileAccess.Write);
500 stream = new FileStream (path, FileMode.Create, FileAccess.Read);
502 } catch (ArgumentException ex) {
503 // make sure it is exact this exception, and not a derived type
504 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#C2");
512 // Create / ReadWrite
514 stream = new FileStream (path, FileMode.Create, FileAccess.ReadWrite);
524 stream = new FileStream (path, FileMode.Create, FileAccess.Write);
534 stream = new FileStream (path, FileMode.CreateNew, FileAccess.Read);
536 } catch (ArgumentException ex) {
537 // make sure it is exact this exception, and not a derived type
538 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#D2");
546 // CreateNew / ReadWrite
548 stream = new FileStream (path, FileMode.CreateNew, FileAccess.ReadWrite);
558 stream = new FileStream (path, FileMode.CreateNew, FileAccess.Write);
568 stream = new FileStream (path, FileMode.Open, FileAccess.Read);
570 } catch (FileNotFoundException ex) {
571 // make sure it is exact this exception, and not a derived type
572 Assert.AreEqual (typeof (FileNotFoundException), ex.GetType (), "#E2");
573 Assert.AreEqual (path, ex.FileName, "#E3");
574 Assert.IsNull (ex.InnerException, "#E4");
575 Assert.IsNotNull (ex.Message, "#E5");
576 Assert.IsTrue (ex.Message.IndexOf (path) != -1, "#E6");
586 stream = new FileStream (path, FileMode.Open, FileAccess.ReadWrite);
588 } catch (FileNotFoundException ex) {
589 // make sure it is exact this exception, and not a derived type
590 Assert.AreEqual (typeof (FileNotFoundException), ex.GetType (), "#F2");
591 Assert.AreEqual (path, ex.FileName, "#F3");
592 Assert.IsNull (ex.InnerException, "#F4");
593 Assert.IsNotNull (ex.Message, "#F5");
594 Assert.IsTrue (ex.Message.IndexOf (path) != -1, "#F6");
604 stream = new FileStream (path, FileMode.Open, FileAccess.Write);
606 } catch (FileNotFoundException ex) {
607 // make sure it is exact this exception, and not a derived type
608 Assert.AreEqual (typeof (FileNotFoundException), ex.GetType (), "#G2");
609 Assert.AreEqual (path, ex.FileName, "#G3");
610 Assert.IsNull (ex.InnerException, "#G4");
611 Assert.IsNotNull (ex.Message, "#G5");
612 Assert.IsTrue (ex.Message.IndexOf (path) != -1, "#G6");
620 // OpenOrCreate / Read
622 stream = new FileStream (path, FileMode.OpenOrCreate, FileAccess.Read);
630 // OpenOrCreate / ReadWrite
632 stream = new FileStream (path, FileMode.OpenOrCreate, FileAccess.ReadWrite);
640 // OpenOrCreate / Write
642 stream = new FileStream (path, FileMode.OpenOrCreate, FileAccess.Write);
652 stream = new FileStream (path, FileMode.Truncate, FileAccess.Read);
654 } catch (ArgumentException ex) {
655 // make sure it is exact this exception, and not a derived type
656 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#H2");
664 // Truncate / ReadWrite
666 stream = new FileStream (path, FileMode.Truncate, FileAccess.ReadWrite);
668 } catch (FileNotFoundException ex) {
669 // make sure it is exact this exception, and not a derived type
670 Assert.AreEqual (typeof (FileNotFoundException), ex.GetType (), "#I2");
671 Assert.AreEqual (path, ex.FileName, "#I3");
672 Assert.IsNull (ex.InnerException, "#I4");
673 Assert.IsNotNull (ex.Message, "#I5");
674 Assert.IsTrue (ex.Message.IndexOf (path) != -1, "#I6");
684 stream = new FileStream (path, FileMode.Truncate, FileAccess.Write);
686 } catch (FileNotFoundException ex) {
687 // make sure it is exact this exception, and not a derived type
688 Assert.AreEqual (typeof (FileNotFoundException), ex.GetType (), "#J2");
689 Assert.AreEqual (path, ex.FileName, "#J3");
690 Assert.IsNull (ex.InnerException, "#J4");
691 Assert.IsNotNull (ex.Message, "#J5");
692 Assert.IsTrue (ex.Message.IndexOf (path) != -1, "#J6");
701 #if !TARGET_JVM // No support IntPtr file handles under TARGET_JVM
702 [Test, ExpectedException (typeof (IOException))]
703 public void CtorIOException2 ()
705 FileStream stream = null;
707 stream = new FileStream (new IntPtr (Int32.MaxValue), FileAccess.Read);
715 [Category("TargetJvmNotSupported")] // File sharing not supported for TARGET_JVM
716 [Test, ExpectedException (typeof (IOException))]
717 public void CtorIOException ()
719 string path = TempFolder + DSC + "CTorIOException.Test";
720 FileStream stream = null;
721 FileStream stream2 = null;
725 stream = new FileStream (path, FileMode.CreateNew);
727 // used by an another process
728 stream2 = new FileStream (path, FileMode.OpenOrCreate);
739 public void CtorAccess1Read2Read ()
741 FileStream fs = null;
742 FileStream fs2 = null;
743 string fn = Path.Combine (TempFolder, "temp");
745 if (!File.Exists (fn)) {
746 TextWriter tw = File.CreateText (fn);
750 fs = new FileStream (fn, FileMode.Open, FileAccess.Read);
751 fs2 = new FileStream (fn, FileMode.Open, FileAccess.Read);
757 if (File.Exists (fn))
763 [Category("TargetJvmNotSupported")] // File sharing not supported for TARGET_JVM
764 [ExpectedException (typeof (IOException))]
765 public void CtorAccess1Read2Write ()
767 string fn = Path.Combine (TempFolder, "temp");
768 FileStream fs1 = null;
769 FileStream fs2 = null;
771 if (!File.Exists (fn)) {
772 using (TextWriter tw = File.CreateText (fn)) {
776 fs1 = new FileStream (fn, FileMode.Open, FileAccess.Read);
777 fs2 = new FileStream (fn, FileMode.Create, FileAccess.Write);
783 if (File.Exists (fn))
789 [Category("TargetJvmNotSupported")] // File sharing not supported for TARGET_JVM
790 [ExpectedException (typeof (IOException))]
791 public void CtorAccess1Write2Write ()
793 string fn = Path.Combine (TempFolder, "temp");
794 FileStream fs1 = null;
795 FileStream fs2 = null;
797 if (File.Exists (fn))
799 fs1 = new FileStream (fn, FileMode.Create, FileAccess.Write);
800 fs2 = new FileStream (fn, FileMode.Create, FileAccess.Write);
806 if (File.Exists (fn))
812 [ExpectedException (typeof (UnauthorizedAccessException))]
813 public void CtorReadDirectoryAsFile ()
815 FileStream stream = null;
817 stream = new FileStream (TempFolder, FileMode.Open, FileAccess.Read);
827 string path = TempFolder + DSC + "FileStreamTest.Write";
831 FileStream stream = new FileStream (path, FileMode.CreateNew, FileAccess.ReadWrite, FileShare.ReadWrite, 8);
833 byte[] outbytes = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16 };
834 byte[] bytes = new byte[15];
836 // Check that the data is flushed when we overflow the buffer
837 // with a large amount of data
838 stream.Write (outbytes, 0, 5);
839 stream.Write (outbytes, 5, 10);
840 stream.Seek (0, SeekOrigin.Begin);
842 stream.Read (bytes, 0, 15);
843 for (int i = 0; i < 15; ++i)
844 Assert.AreEqual (i + 1, bytes[i], "#1");
846 // Check that the data is flushed when we overflow the buffer
847 // with a small amount of data
848 stream.Write (outbytes, 0, 7);
849 stream.Write (outbytes, 7, 7);
850 stream.Write (outbytes, 14, 1);
852 stream.Read (bytes, 0, 15);
853 stream.Seek (15, SeekOrigin.Begin);
854 for (int i = 0; i < 15; ++i)
855 Assert.AreEqual (i + 1, bytes[i], "#2");
860 public void Length ()
862 // Test that the Length property takes into account the data
864 string path = TempFolder + DSC + "FileStreamTest.Length";
868 FileStream stream = new FileStream (path, FileMode.CreateNew);
870 byte[] outbytes = new byte[] { 1, 2, 3, 4 };
872 stream.Write (outbytes, 0, 4);
873 Assert.AreEqual (4, stream.Length);
880 string path = TempFolder + DSC + "FileStreamTest.Flush";
881 FileStream stream = null;
882 FileStream stream2 = null;
887 stream = new FileStream (path, FileMode.CreateNew, FileAccess.ReadWrite, FileShare.ReadWrite);
888 stream2 = new FileStream (path, FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite);
890 stream.Write (new byte[] { 1, 2, 3, 4, 5 }, 0, 5);
892 byte[] bytes = new byte[5];
893 stream2.Read (bytes, 0, 5);
894 Assert.AreEqual (0, bytes[0], "#A1");
895 Assert.AreEqual (0, bytes[1], "#A2");
896 Assert.AreEqual (0, bytes[2], "#A3");
897 Assert.AreEqual (0, bytes[3], "#A4");
900 stream2.Read (bytes, 0, 5);
901 Assert.AreEqual (1, bytes[0], "#B1");
902 Assert.AreEqual (2, bytes[1], "#B2");
903 Assert.AreEqual (3, bytes[2], "#B3");
904 Assert.AreEqual (4, bytes[3], "#B4");
915 public void TestDefaultProperties ()
917 string path = TempFolder + Path.DirectorySeparatorChar + "testfilestream.tmp.2";
920 FileStream stream = new FileStream (path, FileMode.Create);
922 Assert.IsTrue (stream.CanRead, "#A1");
923 Assert.IsTrue (stream.CanSeek, "#A2");
924 Assert.IsTrue (stream.CanWrite, "#A3");
925 Assert.IsFalse (stream.IsAsync, "#A4");
926 Assert.IsTrue (stream.Name.EndsWith (path), "#A5");
927 Assert.AreEqual (0, stream.Position, "#A6");
928 Assert.AreEqual ("System.IO.FileStream", stream.ToString (), "#A7");
932 stream = new FileStream (path, FileMode.OpenOrCreate, FileAccess.Read);
933 Assert.IsTrue (stream.CanRead, "#B1");
934 Assert.IsTrue (stream.CanSeek, "#B2");
935 Assert.IsFalse (stream.CanWrite, "#B3");
936 Assert.IsFalse (stream.IsAsync, "#B4");
937 Assert.IsTrue (stream.Name.EndsWith (path), "#B5");
938 Assert.AreEqual (0, stream.Position, "#B6");
939 Assert.AreEqual ("System.IO.FileStream", stream.ToString (), "#B7");
942 stream = new FileStream (path, FileMode.Truncate, FileAccess.Write, FileShare.ReadWrite);
943 Assert.IsFalse (stream.CanRead, "#C1");
944 Assert.IsTrue (stream.CanSeek, "#C2");
945 Assert.IsTrue (stream.CanWrite, "#C3");
946 Assert.IsFalse (stream.IsAsync, "#C4");
947 Assert.IsTrue (stream.Name.EndsWith ("testfilestream.tmp.2"), "#C5");
948 Assert.AreEqual (0, stream.Position, "#C6");
949 Assert.AreEqual ("System.IO.FileStream", stream.ToString (), "#C7");
954 [Category ("NotWorking")]
955 // Bug: 71371 -> duplicate and WONTFIX.
956 public void TestLock_FailsOnMono ()
958 string path = TempFolder + Path.DirectorySeparatorChar + "TestLock";
961 FileStream stream = new FileStream (path, FileMode.CreateNew, FileAccess.ReadWrite);
963 stream.Write (new Byte[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }, 0, 10);
966 stream = new FileStream (path, FileMode.Open, FileAccess.ReadWrite);
970 FileStream stream2 = new FileStream (path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
972 byte[] bytes = new byte[5];
974 stream2.Read (bytes, 0, 5);
976 } catch (Exception e) {
977 Assert.AreEqual (typeof (IOException), e.GetType (), "#2");
986 #if TARGET_JVM // File locking is not implemented.
987 [Category ("NotWorking")]
989 public void TestLock ()
991 string path = TempFolder + Path.DirectorySeparatorChar + "TestLock";
994 FileStream stream = new FileStream (path, FileMode.CreateNew, FileAccess.ReadWrite);
996 stream.Write (new Byte[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }, 0, 10);
999 stream = new FileStream (path, FileMode.Open, FileAccess.ReadWrite);
1003 FileStream stream2 = new FileStream (path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
1005 byte[] bytes = new byte[5];
1007 stream2.Read (bytes, 0, 5);
1008 Assert.Fail ("#A1");
1009 } catch (Exception) {
1010 // Bug #71371: on MS.NET you get an IOException detailing a lock
1011 // Assert.AreEqual (typeof (IOException), e.GetType (), "#A2");
1014 stream2.Seek (5, SeekOrigin.Begin);
1015 stream2.Read (bytes, 0, 5);
1017 Assert.AreEqual (5, bytes[0], "#B1");
1018 Assert.AreEqual (6, bytes[1], "#B2");
1019 Assert.AreEqual (7, bytes[2], "#B3");
1020 Assert.AreEqual (8, bytes[3], "#B4");
1021 Assert.AreEqual (9, bytes[4], "#B5");
1023 stream.Unlock (0, 5);
1024 stream2.Seek (0, SeekOrigin.Begin);
1025 stream2.Read (bytes, 0, 5);
1027 Assert.AreEqual (0, bytes[0], "#C1");
1028 Assert.AreEqual (1, bytes[1], "#C2");
1029 Assert.AreEqual (2, bytes[2], "#C3");
1030 Assert.AreEqual (3, bytes[3], "#C4");
1031 Assert.AreEqual (4, bytes[4], "#C5");
1042 string path = TempFolder + DSC + "FST.Seek.Test";
1045 FileStream stream = new FileStream (path, FileMode.CreateNew, FileAccess.ReadWrite, FileShare.ReadWrite);
1046 FileStream stream2 = new FileStream (path, FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite);
1048 stream.Write (new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 10 }, 0, 9);
1049 Assert.AreEqual (5, stream2.Seek (5, SeekOrigin.Begin), "#1");
1050 Assert.AreEqual (-1, stream2.ReadByte (), "#2");
1052 Assert.AreEqual (2, stream2.Seek (-3, SeekOrigin.Current), "#3");
1053 Assert.AreEqual (-1, stream2.ReadByte (), "#4");
1055 Assert.AreEqual (12, stream.Seek (3, SeekOrigin.Current), "#5");
1056 Assert.AreEqual (-1, stream.ReadByte (), "#6");
1058 Assert.AreEqual (5, stream.Seek (-7, SeekOrigin.Current), "#7");
1059 Assert.AreEqual (6, stream.ReadByte (), "#8");
1061 Assert.AreEqual (5, stream2.Seek (5, SeekOrigin.Begin), "#9");
1062 Assert.AreEqual (6, stream2.ReadByte (), "#10");
1070 public void TestSeek ()
1072 string path = TempFolder + Path.DirectorySeparatorChar + "TestSeek";
1075 FileStream stream = new FileStream (path, FileMode.CreateNew, FileAccess.ReadWrite, FileShare.ReadWrite);
1076 stream.Write (new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }, 0, 10);
1078 stream.Seek (5, SeekOrigin.End);
1079 Assert.AreEqual (-1, stream.ReadByte (), "#1");
1081 stream.Seek (-5, SeekOrigin.End);
1082 Assert.AreEqual (6, stream.ReadByte (), "#2");
1085 stream.Seek (-11, SeekOrigin.End);
1087 } catch (Exception e) {
1088 Assert.AreEqual (typeof (IOException), e.GetType (), "#4");
1091 stream.Seek (19, SeekOrigin.Begin);
1092 Assert.AreEqual (-1, stream.ReadByte (), "#5");
1094 stream.Seek (1, SeekOrigin.Begin);
1095 Assert.AreEqual (2, stream.ReadByte (), "#6");
1097 stream.Seek (3, SeekOrigin.Current);
1098 Assert.AreEqual (6, stream.ReadByte (), "#7");
1100 stream.Seek (-2, SeekOrigin.Current);
1101 Assert.AreEqual (5, stream.ReadByte (), "#8");
1105 // Test that seeks work correctly when seeking inside the buffer
1106 stream.Seek (0, SeekOrigin.Begin);
1107 stream.WriteByte (0);
1108 stream.WriteByte (1);
1109 stream.Seek (0, SeekOrigin.Begin);
1110 byte[] buf = new byte[1];
1112 stream.Write (buf, 0, 1);
1113 stream.Write (buf, 0, 1);
1115 stream.Seek (0, SeekOrigin.Begin);
1116 Assert.AreEqual (2, stream.ReadByte (), "#9");
1117 Assert.AreEqual (2, stream.ReadByte (), "#10");
1124 public void TestClose ()
1126 string path = TempFolder + Path.DirectorySeparatorChar + "TestClose";
1129 FileStream stream = new FileStream (path, FileMode.CreateNew, FileAccess.ReadWrite);
1131 stream.Write (new byte[] { 1, 2, 3, 4 }, 0, 4);
1137 Assert.Fail ("#A1");
1138 } catch (Exception e) {
1139 Assert.AreEqual (typeof (ObjectDisposedException), e.GetType (), "#A2");
1143 stream.WriteByte (64);
1144 Assert.Fail ("#B1");
1145 } catch (Exception e) {
1146 Assert.AreEqual (typeof (ObjectDisposedException), e.GetType (), "#B2");
1151 Assert.Fail ("#C1");
1152 } catch (Exception e) {
1153 Assert.AreEqual (typeof (ObjectDisposedException), e.GetType (), "#C2");
1157 long l = stream.Length;
1158 Assert.Fail ("#D1");
1159 } catch (Exception e) {
1160 Assert.AreEqual (typeof (ObjectDisposedException), e.GetType (), "#D2");
1164 long l = stream.Position;
1165 Assert.Fail ("#E1");
1166 } catch (Exception e) {
1167 Assert.AreEqual (typeof (ObjectDisposedException), e.GetType (), "#E2");
1170 Assert.IsFalse (stream.CanRead, "#F1");
1171 Assert.IsFalse (stream.CanSeek, "#F2");
1172 Assert.IsFalse (stream.CanWrite, "#F3");
1173 Assert.IsTrue (stream.Name.EndsWith (path), "#F4");
1180 /// Checks whether the <see cref="FileStream" /> throws a <see cref="NotSupportedException" />
1181 /// when the stream is opened with access mode <see cref="FileAccess.Read" /> and the
1182 /// <see cref="FileStream.Write(byte[], int, int)" /> method is called.
1185 [ExpectedException (typeof (NotSupportedException))]
1186 public void TestWriteVerifyAccessMode ()
1188 string path = TempFolder + Path.DirectorySeparatorChar + "temp";
1191 FileStream stream = null;
1195 buffer = Encoding.ASCII.GetBytes ("test");
1196 stream = new FileStream (path, FileMode.OpenOrCreate, FileAccess.Read);
1197 stream.Write (buffer, 0, buffer.Length);
1206 /// Checks whether the <see cref="FileStream" /> throws a <see cref="NotSupportedException" />
1207 /// when the stream is opened with access mode <see cref="FileAccess.Read" /> and the
1208 /// <see cref="FileStream.WriteByte(byte)" /> method is called.
1211 [ExpectedException (typeof (NotSupportedException))]
1212 public void TestWriteByteVerifyAccessMode ()
1214 string path = TempFolder + Path.DirectorySeparatorChar + "temp";
1217 FileStream stream = null;
1220 stream = new FileStream (path, FileMode.OpenOrCreate, FileAccess.Read);
1221 stream.WriteByte (Byte.MinValue);
1230 /// Checks whether the <see cref="FileStream" /> throws a <see cref="NotSupportedException" />
1231 /// when the stream is opened with access mode <see cref="FileAccess.Write" /> and the
1232 /// <see cref="FileStream.Read(byte[], int, int)" /> method is called.
1235 [ExpectedException (typeof (NotSupportedException))]
1236 public void TestReadVerifyAccessMode ()
1238 string path = TempFolder + Path.DirectorySeparatorChar + "temp";
1241 FileStream stream = null;
1242 byte[] buffer = new byte[100];
1245 stream = new FileStream (path, FileMode.OpenOrCreate, FileAccess.Write);
1246 stream.Read (buffer, 0, buffer.Length);
1254 /// Checks whether the <see cref="FileStream" /> throws a <see cref="NotSupportedException" />
1255 /// when the stream is opened with access mode <see cref="FileAccess.Write" /> and the
1256 /// <see cref="FileStream.ReadByte()" /> method is called.
1259 [ExpectedException (typeof (NotSupportedException))]
1260 public void TestReadByteVerifyAccessMode ()
1262 string path = TempFolder + Path.DirectorySeparatorChar + "temp";
1265 FileStream stream = null;
1268 stream = new FileStream (path, FileMode.OpenOrCreate, FileAccess.Write);
1269 int readByte = stream.ReadByte ();
1277 #if !TARGET_JVM // No support IntPtr file handles under TARGET_JVM
1278 // Check that the stream is flushed even when it doesn't own the
1281 public void TestFlushNotOwningHandle ()
1283 string path = Path.Combine (TempFolder, "TestFlushNotOwningHandle");
1286 FileStream s = new FileStream (path, FileMode.Create);
1287 using (FileStream s2 = new FileStream (s.Handle, FileAccess.Write, false)) {
1288 byte[] buf = new byte[2];
1290 s2.Write (buf, 0, 1);
1294 Assert.AreEqual ((int) '1', s.ReadByte ());
1297 #endif // TARGET_JVM
1299 private void DeleteFile (string path)
1301 if (File.Exists (path))
1306 [ExpectedException (typeof (ArgumentOutOfRangeException))]
1307 public void Read_OffsetNegative ()
1309 string path = TempFolder + Path.DirectorySeparatorChar + "temp";
1312 using (FileStream stream = new FileStream (path, FileMode.OpenOrCreate, FileAccess.Read)) {
1313 stream.Read (new byte[0], -1, 1);
1318 [ExpectedException (typeof (ArgumentException))]
1319 public void Read_OffsetOverflow ()
1321 string path = TempFolder + Path.DirectorySeparatorChar + "temp";
1324 using (FileStream stream = new FileStream (path, FileMode.OpenOrCreate, FileAccess.Read)) {
1325 stream.Read (new byte[0], Int32.MaxValue, 1);
1330 [ExpectedException (typeof (ArgumentOutOfRangeException))]
1331 public void Read_CountNegative ()
1333 string path = TempFolder + Path.DirectorySeparatorChar + "temp";
1336 using (FileStream stream = new FileStream (path, FileMode.OpenOrCreate, FileAccess.Read)) {
1337 stream.Read (new byte[0], 1, -1);
1342 [ExpectedException (typeof (ArgumentException))]
1343 public void Read_CountOverflow ()
1345 string path = TempFolder + Path.DirectorySeparatorChar + "temp";
1348 using (FileStream stream = new FileStream (path, FileMode.OpenOrCreate, FileAccess.Read)) {
1349 stream.Read (new byte[0], 1, Int32.MaxValue);
1354 [ExpectedException (typeof (ArgumentOutOfRangeException))]
1355 public void Write_OffsetNegative ()
1357 string path = TempFolder + Path.DirectorySeparatorChar + "temp";
1360 using (FileStream stream = new FileStream (path, FileMode.OpenOrCreate, FileAccess.Write)) {
1361 stream.Write (new byte[0], -1, 1);
1366 [ExpectedException (typeof (ArgumentException))]
1367 public void Write_OffsetOverflow ()
1369 string path = TempFolder + Path.DirectorySeparatorChar + "temp";
1372 using (FileStream stream = new FileStream (path, FileMode.OpenOrCreate, FileAccess.Write)) {
1373 stream.Write (new byte[0], Int32.MaxValue, 1);
1378 [ExpectedException (typeof (ArgumentOutOfRangeException))]
1379 public void Write_CountNegative ()
1381 string path = TempFolder + Path.DirectorySeparatorChar + "temp";
1384 using (FileStream stream = new FileStream (path, FileMode.OpenOrCreate, FileAccess.Write)) {
1385 stream.Write (new byte[0], 1, -1);
1390 [ExpectedException (typeof (ArgumentException))]
1391 public void Write_CountOverflow ()
1393 string path = TempFolder + Path.DirectorySeparatorChar + "temp";
1396 using (FileStream stream = new FileStream (path, FileMode.OpenOrCreate, FileAccess.Write)) {
1397 stream.Write (new byte[0], 1, Int32.MaxValue);
1402 [ExpectedException (typeof (ArgumentException))]
1403 public void Seek_InvalidSeekOrigin ()
1405 string path = TempFolder + Path.DirectorySeparatorChar + "temp";
1408 using (FileStream stream = new FileStream (path, FileMode.OpenOrCreate, FileAccess.Read)) {
1409 stream.Seek (0, (SeekOrigin) (-1));
1413 #if !TARGET_JVM // No support IntPtr file handles under TARGET_JVM
1415 [ExpectedException (typeof (ArgumentException))]
1416 public void Constructor_InvalidFileHandle ()
1418 new FileStream ((IntPtr) (-1L), FileAccess.Read);
1420 #endif // TARGET_JVM
1423 public void PositionAfterSetLength ()
1425 string path = TempFolder + Path.DirectorySeparatorChar + "temp";
1428 using (FileStream stream = new FileStream (path, FileMode.OpenOrCreate, FileAccess.Write)) {
1429 stream.SetLength (32);
1430 stream.Position = 32;
1431 stream.SetLength (16);
1432 Assert.AreEqual (16, stream.Position);
1437 [ExpectedException (typeof (ObjectDisposedException))]
1438 public void SetLength_Disposed ()
1440 string path = TempFolder + Path.DirectorySeparatorChar + "temp";
1442 FileStream stream = new FileStream (path, FileMode.OpenOrCreate, FileAccess.Write);
1444 stream.SetLength (16);
1448 [ExpectedException (typeof (ObjectDisposedException))]
1449 public void Position_Disposed ()
1451 string path = TempFolder + Path.DirectorySeparatorChar + "temp";
1453 FileStream stream = new FileStream (path, FileMode.OpenOrCreate, FileAccess.Read);
1455 stream.Position = 0;
1459 [Category("TargetJvmNotSupported")] // Async IO not supported for TARGET_JVM
1460 [ExpectedException (typeof (ObjectDisposedException))]
1461 public void BeginRead_Disposed ()
1463 string path = TempFolder + Path.DirectorySeparatorChar + "temp";
1465 FileStream stream = new FileStream (path, FileMode.OpenOrCreate, FileAccess.Read);
1467 stream.EndRead (stream.BeginRead (new byte[8], 0, 8, null, null));
1471 [Category("TargetJvmNotSupported")] // Async IO not supported for TARGET_JVM
1472 [ExpectedException (typeof (ObjectDisposedException))]
1473 public void BeginWrite_Disposed ()
1475 string path = TempFolder + Path.DirectorySeparatorChar + "temp";
1477 FileStream stream = new FileStream (path, FileMode.OpenOrCreate, FileAccess.Write);
1479 stream.EndWrite (stream.BeginWrite (new byte[8], 0, 8, null, null));
1483 [Category("TargetJvmNotSupported")] // File locking not supported for TARGET_JVM
1484 [ExpectedException (typeof (ObjectDisposedException))]
1485 public void Lock_Disposed ()
1487 string path = TempFolder + Path.DirectorySeparatorChar + "temp";
1489 FileStream stream = new FileStream (path, FileMode.OpenOrCreate, FileAccess.Write);
1495 [Category("TargetJvmNotSupported")] // File locking not supported for TARGET_JVM
1496 [ExpectedException (typeof (ObjectDisposedException))]
1497 public void Unlock_Disposed ()
1499 string path = TempFolder + Path.DirectorySeparatorChar + "temp";
1501 FileStream stream = new FileStream (path, FileMode.OpenOrCreate, FileAccess.Write);
1503 stream.Unlock (0, 1);
1507 public void ReadBytePastEndOfStream ()
1509 string path = TempFolder + Path.DirectorySeparatorChar + "temp";
1511 using (FileStream stream = new FileStream (path, FileMode.OpenOrCreate, FileAccess.Read)) {
1512 stream.Seek (0, SeekOrigin.End);
1513 Assert.AreEqual (-1, stream.ReadByte ());
1519 [ExpectedException (typeof (NotSupportedException))]
1520 public void SetLengthWithClosedBaseStream ()
1522 string fn = Path.Combine (TempFolder, "temp");
1524 FileStream fs = new FileStream (fn, FileMode.Create);
1525 BufferedStream bs = new BufferedStream (fs);
1528 bs.SetLength (1000);
1535 public void LengthAfterWrite ()
1537 string path = TempFolder + DSC + "oneofthefilescreated.txt";
1538 FileStream fs = null;
1541 fs = new FileStream (path, FileMode.CreateNew);
1542 fs.WriteByte (Convert.ToByte ('A'));
1543 byte [] buffer = Encoding.ASCII.GetBytes (" is a first character.");
1544 fs.Write (buffer, 0, buffer.Length);
1545 fs.Seek (0, SeekOrigin.Begin);
1546 char a = Convert.ToChar (fs.ReadByte ());
1547 Assert.AreEqual ('A', a, "#A1");
1548 Assert.AreEqual (23, fs.Length, "#A2");
1549 int nread = fs.Read (buffer, 0, 5);
1550 Assert.AreEqual (5, nread, "#A3");
1559 [Category("TargetJvmNotSupported")] // FileOptions.DeleteOnClose not supported for TARGET_JVM
1560 [Test] public void DeleteOnClose ()
1562 string path = TempFolder + DSC + "created.txt";
1564 FileStream fs = new FileStream (path, FileMode.CreateNew, FileAccess.Write, FileShare.None, 1024,
1565 FileOptions.DeleteOnClose);
1566 Assert.AreEqual (true, File.Exists (path), "DOC#1");
1568 Assert.AreEqual (false, File.Exists (path), "DOC#2");