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 [Test, ExpectedException (typeof (IOException))]
716 public void CtorIOException ()
718 string path = TempFolder + DSC + "CTorIOException.Test";
719 FileStream stream = null;
720 FileStream stream2 = null;
724 stream = new FileStream (path, FileMode.CreateNew);
726 // used by an another process
727 stream2 = new FileStream (path, FileMode.OpenOrCreate);
738 public void CtorAccess1Read2Read ()
740 FileStream fs = null;
741 FileStream fs2 = null;
742 string fn = Path.Combine (TempFolder, "temp");
744 if (!File.Exists (fn)) {
745 TextWriter tw = File.CreateText (fn);
749 fs = new FileStream (fn, FileMode.Open, FileAccess.Read);
750 fs2 = new FileStream (fn, FileMode.Open, FileAccess.Read);
756 if (File.Exists (fn))
762 [ExpectedException (typeof (IOException))]
763 public void CtorAccess1Read2Write ()
765 string fn = Path.Combine (TempFolder, "temp");
766 FileStream fs = null;
768 if (!File.Exists (fn)) {
769 using (TextWriter tw = File.CreateText (fn)) {
773 fs = new FileStream (fn, FileMode.Open, FileAccess.Read);
774 fs = new FileStream (fn, FileMode.Create, FileAccess.Write);
778 if (File.Exists (fn))
784 [ExpectedException (typeof (IOException))]
785 public void CtorAccess1Write2Write ()
787 string fn = Path.Combine (TempFolder, "temp");
788 FileStream fs = null;
790 if (File.Exists (fn))
792 fs = new FileStream (fn, FileMode.Create, FileAccess.Write);
793 fs = new FileStream (fn, FileMode.Create, FileAccess.Write);
797 if (File.Exists (fn))
803 [ExpectedException (typeof (UnauthorizedAccessException))]
804 public void CtorReadDirectoryAsFile ()
806 FileStream stream = null;
808 stream = new FileStream (TempFolder, FileMode.Open, FileAccess.Read);
818 string path = TempFolder + DSC + "FileStreamTest.Write";
822 FileStream stream = new FileStream (path, FileMode.CreateNew, FileAccess.ReadWrite, FileShare.ReadWrite, 8);
824 byte[] outbytes = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16 };
825 byte[] bytes = new byte[15];
827 // Check that the data is flushed when we overflow the buffer
828 // with a large amount of data
829 stream.Write (outbytes, 0, 5);
830 stream.Write (outbytes, 5, 10);
831 stream.Seek (0, SeekOrigin.Begin);
833 stream.Read (bytes, 0, 15);
834 for (int i = 0; i < 15; ++i)
835 Assert.AreEqual (i + 1, bytes[i], "#1");
837 // Check that the data is flushed when we overflow the buffer
838 // with a small amount of data
839 stream.Write (outbytes, 0, 7);
840 stream.Write (outbytes, 7, 7);
841 stream.Write (outbytes, 14, 1);
843 stream.Read (bytes, 0, 15);
844 stream.Seek (15, SeekOrigin.Begin);
845 for (int i = 0; i < 15; ++i)
846 Assert.AreEqual (i + 1, bytes[i], "#2");
851 public void Length ()
853 // Test that the Length property takes into account the data
855 string path = TempFolder + DSC + "FileStreamTest.Length";
859 FileStream stream = new FileStream (path, FileMode.CreateNew);
861 byte[] outbytes = new byte[] { 1, 2, 3, 4 };
863 stream.Write (outbytes, 0, 4);
864 Assert.AreEqual (4, stream.Length);
871 string path = TempFolder + DSC + "FileStreamTest.Flush";
872 FileStream stream = null;
873 FileStream stream2 = null;
878 stream = new FileStream (path, FileMode.CreateNew, FileAccess.ReadWrite, FileShare.ReadWrite);
879 stream2 = new FileStream (path, FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite);
881 stream.Write (new byte[] { 1, 2, 3, 4, 5 }, 0, 5);
883 byte[] bytes = new byte[5];
884 stream2.Read (bytes, 0, 5);
885 Assert.AreEqual (0, bytes[0], "#A1");
886 Assert.AreEqual (0, bytes[1], "#A2");
887 Assert.AreEqual (0, bytes[2], "#A3");
888 Assert.AreEqual (0, bytes[3], "#A4");
891 stream2.Read (bytes, 0, 5);
892 Assert.AreEqual (1, bytes[0], "#B1");
893 Assert.AreEqual (2, bytes[1], "#B2");
894 Assert.AreEqual (3, bytes[2], "#B3");
895 Assert.AreEqual (4, bytes[3], "#B4");
906 public void TestDefaultProperties ()
908 string path = TempFolder + Path.DirectorySeparatorChar + "testfilestream.tmp.2";
911 FileStream stream = new FileStream (path, FileMode.Create);
913 Assert.IsTrue (stream.CanRead, "#A1");
914 Assert.IsTrue (stream.CanSeek, "#A2");
915 Assert.IsTrue (stream.CanWrite, "#A3");
916 Assert.IsFalse (stream.IsAsync, "#A4");
917 Assert.IsTrue (stream.Name.EndsWith (path), "#A5");
918 Assert.AreEqual (0, stream.Position, "#A6");
919 Assert.AreEqual ("System.IO.FileStream", stream.ToString (), "#A7");
923 stream = new FileStream (path, FileMode.OpenOrCreate, FileAccess.Read);
924 Assert.IsTrue (stream.CanRead, "#B1");
925 Assert.IsTrue (stream.CanSeek, "#B2");
926 Assert.IsFalse (stream.CanWrite, "#B3");
927 Assert.IsFalse (stream.IsAsync, "#B4");
928 Assert.IsTrue (stream.Name.EndsWith (path), "#B5");
929 Assert.AreEqual (0, stream.Position, "#B6");
930 Assert.AreEqual ("System.IO.FileStream", stream.ToString (), "#B7");
933 stream = new FileStream (path, FileMode.Truncate, FileAccess.Write, FileShare.ReadWrite);
934 Assert.IsFalse (stream.CanRead, "#C1");
935 Assert.IsTrue (stream.CanSeek, "#C2");
936 Assert.IsTrue (stream.CanWrite, "#C3");
937 Assert.IsFalse (stream.IsAsync, "#C4");
938 Assert.IsTrue (stream.Name.EndsWith ("testfilestream.tmp.2"), "#C5");
939 Assert.AreEqual (0, stream.Position, "#C6");
940 Assert.AreEqual ("System.IO.FileStream", stream.ToString (), "#C7");
945 [Category ("NotWorking")]
946 // Bug: 71371 -> duplicate and WONTFIX.
947 public void TestLock_FailsOnMono ()
949 string path = TempFolder + Path.DirectorySeparatorChar + "TestLock";
952 FileStream stream = new FileStream (path, FileMode.CreateNew, FileAccess.ReadWrite);
954 stream.Write (new Byte[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }, 0, 10);
957 stream = new FileStream (path, FileMode.Open, FileAccess.ReadWrite);
961 FileStream stream2 = new FileStream (path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
963 byte[] bytes = new byte[5];
965 stream2.Read (bytes, 0, 5);
967 } catch (Exception e) {
968 Assert.AreEqual (typeof (IOException), e.GetType (), "#2");
977 public void TestLock ()
979 string path = TempFolder + Path.DirectorySeparatorChar + "TestLock";
982 FileStream stream = new FileStream (path, FileMode.CreateNew, FileAccess.ReadWrite);
984 stream.Write (new Byte[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }, 0, 10);
987 stream = new FileStream (path, FileMode.Open, FileAccess.ReadWrite);
991 FileStream stream2 = new FileStream (path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
993 byte[] bytes = new byte[5];
995 stream2.Read (bytes, 0, 5);
997 } catch (Exception) {
998 // Bug #71371: on MS.NET you get an IOException detailing a lock
999 // Assert.AreEqual (typeof (IOException), e.GetType (), "#A2");
1002 stream2.Seek (5, SeekOrigin.Begin);
1003 stream2.Read (bytes, 0, 5);
1005 Assert.AreEqual (5, bytes[0], "#B1");
1006 Assert.AreEqual (6, bytes[1], "#B2");
1007 Assert.AreEqual (7, bytes[2], "#B3");
1008 Assert.AreEqual (8, bytes[3], "#B4");
1009 Assert.AreEqual (9, bytes[4], "#B5");
1011 stream.Unlock (0, 5);
1012 stream2.Seek (0, SeekOrigin.Begin);
1013 stream2.Read (bytes, 0, 5);
1015 Assert.AreEqual (0, bytes[0], "#C1");
1016 Assert.AreEqual (1, bytes[1], "#C2");
1017 Assert.AreEqual (2, bytes[2], "#C3");
1018 Assert.AreEqual (3, bytes[3], "#C4");
1019 Assert.AreEqual (4, bytes[4], "#C5");
1030 string path = TempFolder + DSC + "FST.Seek.Test";
1033 FileStream stream = new FileStream (path, FileMode.CreateNew, FileAccess.ReadWrite, FileShare.ReadWrite);
1034 FileStream stream2 = new FileStream (path, FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite);
1036 stream.Write (new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 10 }, 0, 9);
1037 Assert.AreEqual (5, stream2.Seek (5, SeekOrigin.Begin), "#1");
1038 Assert.AreEqual (-1, stream2.ReadByte (), "#2");
1040 Assert.AreEqual (2, stream2.Seek (-3, SeekOrigin.Current), "#3");
1041 Assert.AreEqual (-1, stream2.ReadByte (), "#4");
1043 Assert.AreEqual (12, stream.Seek (3, SeekOrigin.Current), "#5");
1044 Assert.AreEqual (-1, stream.ReadByte (), "#6");
1046 Assert.AreEqual (5, stream.Seek (-7, SeekOrigin.Current), "#7");
1047 Assert.AreEqual (6, stream.ReadByte (), "#8");
1049 Assert.AreEqual (5, stream2.Seek (5, SeekOrigin.Begin), "#9");
1050 Assert.AreEqual (6, stream2.ReadByte (), "#10");
1058 public void TestSeek ()
1060 string path = TempFolder + Path.DirectorySeparatorChar + "TestSeek";
1063 FileStream stream = new FileStream (path, FileMode.CreateNew, FileAccess.ReadWrite, FileShare.ReadWrite);
1064 stream.Write (new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }, 0, 10);
1066 stream.Seek (5, SeekOrigin.End);
1067 Assert.AreEqual (-1, stream.ReadByte (), "#1");
1069 stream.Seek (-5, SeekOrigin.End);
1070 Assert.AreEqual (6, stream.ReadByte (), "#2");
1073 stream.Seek (-11, SeekOrigin.End);
1075 } catch (Exception e) {
1076 Assert.AreEqual (typeof (IOException), e.GetType (), "#4");
1079 stream.Seek (19, SeekOrigin.Begin);
1080 Assert.AreEqual (-1, stream.ReadByte (), "#5");
1082 stream.Seek (1, SeekOrigin.Begin);
1083 Assert.AreEqual (2, stream.ReadByte (), "#6");
1085 stream.Seek (3, SeekOrigin.Current);
1086 Assert.AreEqual (6, stream.ReadByte (), "#7");
1088 stream.Seek (-2, SeekOrigin.Current);
1089 Assert.AreEqual (5, stream.ReadByte (), "#8");
1093 // Test that seeks work correctly when seeking inside the buffer
1094 stream.Seek (0, SeekOrigin.Begin);
1095 stream.WriteByte (0);
1096 stream.WriteByte (1);
1097 stream.Seek (0, SeekOrigin.Begin);
1098 byte[] buf = new byte[1];
1100 stream.Write (buf, 0, 1);
1101 stream.Write (buf, 0, 1);
1103 stream.Seek (0, SeekOrigin.Begin);
1104 Assert.AreEqual (2, stream.ReadByte (), "#9");
1105 Assert.AreEqual (2, stream.ReadByte (), "#10");
1112 public void TestClose ()
1114 string path = TempFolder + Path.DirectorySeparatorChar + "TestClose";
1117 FileStream stream = new FileStream (path, FileMode.CreateNew, FileAccess.ReadWrite);
1119 stream.Write (new byte[] { 1, 2, 3, 4 }, 0, 4);
1125 Assert.Fail ("#A1");
1126 } catch (Exception e) {
1127 Assert.AreEqual (typeof (ObjectDisposedException), e.GetType (), "#A2");
1131 stream.WriteByte (64);
1132 Assert.Fail ("#B1");
1133 } catch (Exception e) {
1134 Assert.AreEqual (typeof (ObjectDisposedException), e.GetType (), "#B2");
1139 Assert.Fail ("#C1");
1140 } catch (Exception e) {
1141 Assert.AreEqual (typeof (ObjectDisposedException), e.GetType (), "#C2");
1145 long l = stream.Length;
1146 Assert.Fail ("#D1");
1147 } catch (Exception e) {
1148 Assert.AreEqual (typeof (ObjectDisposedException), e.GetType (), "#D2");
1152 long l = stream.Position;
1153 Assert.Fail ("#E1");
1154 } catch (Exception e) {
1155 Assert.AreEqual (typeof (ObjectDisposedException), e.GetType (), "#E2");
1158 Assert.IsFalse (stream.CanRead, "#F1");
1159 Assert.IsFalse (stream.CanSeek, "#F2");
1160 Assert.IsFalse (stream.CanWrite, "#F3");
1161 Assert.IsTrue (stream.Name.EndsWith (path), "#F4");
1168 /// Checks whether the <see cref="FileStream" /> throws a <see cref="NotSupportedException" />
1169 /// when the stream is opened with access mode <see cref="FileAccess.Read" /> and the
1170 /// <see cref="FileStream.Write(byte[], int, int)" /> method is called.
1173 [ExpectedException (typeof (NotSupportedException))]
1174 public void TestWriteVerifyAccessMode ()
1176 string path = TempFolder + Path.DirectorySeparatorChar + "temp";
1179 FileStream stream = null;
1183 buffer = Encoding.ASCII.GetBytes ("test");
1184 stream = new FileStream (path, FileMode.OpenOrCreate, FileAccess.Read);
1185 stream.Write (buffer, 0, buffer.Length);
1194 /// Checks whether the <see cref="FileStream" /> throws a <see cref="NotSupportedException" />
1195 /// when the stream is opened with access mode <see cref="FileAccess.Read" /> and the
1196 /// <see cref="FileStream.WriteByte(byte)" /> method is called.
1199 [ExpectedException (typeof (NotSupportedException))]
1200 public void TestWriteByteVerifyAccessMode ()
1202 string path = TempFolder + Path.DirectorySeparatorChar + "temp";
1205 FileStream stream = null;
1208 stream = new FileStream (path, FileMode.OpenOrCreate, FileAccess.Read);
1209 stream.WriteByte (Byte.MinValue);
1218 /// Checks whether the <see cref="FileStream" /> throws a <see cref="NotSupportedException" />
1219 /// when the stream is opened with access mode <see cref="FileAccess.Write" /> and the
1220 /// <see cref="FileStream.Read(byte[], int, int)" /> method is called.
1223 [ExpectedException (typeof (NotSupportedException))]
1224 public void TestReadVerifyAccessMode ()
1226 string path = TempFolder + Path.DirectorySeparatorChar + "temp";
1229 FileStream stream = null;
1230 byte[] buffer = new byte[100];
1233 stream = new FileStream (path, FileMode.OpenOrCreate, FileAccess.Write);
1234 stream.Read (buffer, 0, buffer.Length);
1242 /// Checks whether the <see cref="FileStream" /> throws a <see cref="NotSupportedException" />
1243 /// when the stream is opened with access mode <see cref="FileAccess.Write" /> and the
1244 /// <see cref="FileStream.ReadByte()" /> method is called.
1247 [ExpectedException (typeof (NotSupportedException))]
1248 public void TestReadByteVerifyAccessMode ()
1250 string path = TempFolder + Path.DirectorySeparatorChar + "temp";
1253 FileStream stream = null;
1256 stream = new FileStream (path, FileMode.OpenOrCreate, FileAccess.Write);
1257 int readByte = stream.ReadByte ();
1265 #if !TARGET_JVM // No support IntPtr file handles under TARGET_JVM
1266 // Check that the stream is flushed even when it doesn't own the
1269 public void TestFlushNotOwningHandle ()
1271 string path = Path.Combine (TempFolder, "TestFlushNotOwningHandle");
1274 FileStream s = new FileStream (path, FileMode.Create);
1275 using (FileStream s2 = new FileStream (s.Handle, FileAccess.Write, false)) {
1276 byte[] buf = new byte[2];
1278 s2.Write (buf, 0, 1);
1282 Assert.AreEqual ((int) '1', s.ReadByte ());
1285 #endif // TARGET_JVM
1287 private void DeleteFile (string path)
1289 if (File.Exists (path))
1294 [ExpectedException (typeof (ArgumentOutOfRangeException))]
1295 public void Read_OffsetNegative ()
1297 string path = TempFolder + Path.DirectorySeparatorChar + "temp";
1300 using (FileStream stream = new FileStream (path, FileMode.OpenOrCreate, FileAccess.Read)) {
1301 stream.Read (new byte[0], -1, 1);
1306 [ExpectedException (typeof (ArgumentException))]
1307 public void Read_OffsetOverflow ()
1309 string path = TempFolder + Path.DirectorySeparatorChar + "temp";
1312 using (FileStream stream = new FileStream (path, FileMode.OpenOrCreate, FileAccess.Read)) {
1313 stream.Read (new byte[0], Int32.MaxValue, 1);
1318 [ExpectedException (typeof (ArgumentOutOfRangeException))]
1319 public void Read_CountNegative ()
1321 string path = TempFolder + Path.DirectorySeparatorChar + "temp";
1324 using (FileStream stream = new FileStream (path, FileMode.OpenOrCreate, FileAccess.Read)) {
1325 stream.Read (new byte[0], 1, -1);
1330 [ExpectedException (typeof (ArgumentException))]
1331 public void Read_CountOverflow ()
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, Int32.MaxValue);
1342 [ExpectedException (typeof (ArgumentOutOfRangeException))]
1343 public void Write_OffsetNegative ()
1345 string path = TempFolder + Path.DirectorySeparatorChar + "temp";
1348 using (FileStream stream = new FileStream (path, FileMode.OpenOrCreate, FileAccess.Write)) {
1349 stream.Write (new byte[0], -1, 1);
1354 [ExpectedException (typeof (ArgumentException))]
1355 public void Write_OffsetOverflow ()
1357 string path = TempFolder + Path.DirectorySeparatorChar + "temp";
1360 using (FileStream stream = new FileStream (path, FileMode.OpenOrCreate, FileAccess.Write)) {
1361 stream.Write (new byte[0], Int32.MaxValue, 1);
1366 [ExpectedException (typeof (ArgumentOutOfRangeException))]
1367 public void Write_CountNegative ()
1369 string path = TempFolder + Path.DirectorySeparatorChar + "temp";
1372 using (FileStream stream = new FileStream (path, FileMode.OpenOrCreate, FileAccess.Write)) {
1373 stream.Write (new byte[0], 1, -1);
1378 [ExpectedException (typeof (ArgumentException))]
1379 public void Write_CountOverflow ()
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, Int32.MaxValue);
1390 [ExpectedException (typeof (ArgumentException))]
1391 public void Seek_InvalidSeekOrigin ()
1393 string path = TempFolder + Path.DirectorySeparatorChar + "temp";
1396 using (FileStream stream = new FileStream (path, FileMode.OpenOrCreate, FileAccess.Read)) {
1397 stream.Seek (0, (SeekOrigin) (-1));
1401 #if !TARGET_JVM // No support IntPtr file handles under TARGET_JVM
1403 [ExpectedException (typeof (ArgumentException))]
1404 public void Constructor_InvalidFileHandle ()
1406 new FileStream ((IntPtr) (-1L), FileAccess.Read);
1408 #endif // TARGET_JVM
1411 public void PositionAfterSetLength ()
1413 string path = TempFolder + Path.DirectorySeparatorChar + "temp";
1416 using (FileStream stream = new FileStream (path, FileMode.OpenOrCreate, FileAccess.Write)) {
1417 stream.SetLength (32);
1418 stream.Position = 32;
1419 stream.SetLength (16);
1420 Assert.AreEqual (16, stream.Position);
1425 [ExpectedException (typeof (ObjectDisposedException))]
1426 public void SetLength_Disposed ()
1428 string path = TempFolder + Path.DirectorySeparatorChar + "temp";
1430 FileStream stream = new FileStream (path, FileMode.OpenOrCreate, FileAccess.Write);
1432 stream.SetLength (16);
1436 [ExpectedException (typeof (ObjectDisposedException))]
1437 public void Position_Disposed ()
1439 string path = TempFolder + Path.DirectorySeparatorChar + "temp";
1441 FileStream stream = new FileStream (path, FileMode.OpenOrCreate, FileAccess.Read);
1443 stream.Position = 0;
1447 [ExpectedException (typeof (ObjectDisposedException))]
1448 public void BeginRead_Disposed ()
1450 string path = TempFolder + Path.DirectorySeparatorChar + "temp";
1452 FileStream stream = new FileStream (path, FileMode.OpenOrCreate, FileAccess.Read);
1454 stream.EndRead (stream.BeginRead (new byte[8], 0, 8, null, null));
1458 [ExpectedException (typeof (ObjectDisposedException))]
1459 public void BeginWrite_Disposed ()
1461 string path = TempFolder + Path.DirectorySeparatorChar + "temp";
1463 FileStream stream = new FileStream (path, FileMode.OpenOrCreate, FileAccess.Write);
1465 stream.EndWrite (stream.BeginWrite (new byte[8], 0, 8, null, null));
1469 [ExpectedException (typeof (ObjectDisposedException))]
1470 public void Lock_Disposed ()
1472 string path = TempFolder + Path.DirectorySeparatorChar + "temp";
1474 FileStream stream = new FileStream (path, FileMode.OpenOrCreate, FileAccess.Write);
1480 [ExpectedException (typeof (ObjectDisposedException))]
1481 public void Unlock_Disposed ()
1483 string path = TempFolder + Path.DirectorySeparatorChar + "temp";
1485 FileStream stream = new FileStream (path, FileMode.OpenOrCreate, FileAccess.Write);
1487 stream.Unlock (0, 1);
1491 public void ReadBytePastEndOfStream ()
1493 string path = TempFolder + Path.DirectorySeparatorChar + "temp";
1495 using (FileStream stream = new FileStream (path, FileMode.OpenOrCreate, FileAccess.Read)) {
1496 stream.Seek (0, SeekOrigin.End);
1497 Assert.AreEqual (-1, stream.ReadByte ());
1503 [ExpectedException (typeof (NotSupportedException))]
1504 public void SetLengthWithClosedBaseStream ()
1506 string fn = Path.Combine (TempFolder, "temp");
1508 FileStream fs = new FileStream (fn, FileMode.Create);
1509 BufferedStream bs = new BufferedStream (fs);
1512 bs.SetLength (1000);
1519 public void LengthAfterWrite ()
1521 string path = TempFolder + DSC + "oneofthefilescreated.txt";
1522 FileStream fs = null;
1525 fs = new FileStream (path, FileMode.CreateNew);
1526 fs.WriteByte (Convert.ToByte ('A'));
1527 byte [] buffer = Encoding.ASCII.GetBytes (" is a first character.");
1528 fs.Write (buffer, 0, buffer.Length);
1529 fs.Seek (0, SeekOrigin.Begin);
1530 char a = Convert.ToChar (fs.ReadByte ());
1531 Assert.AreEqual ('A', a, "#A1");
1532 Assert.AreEqual (23, fs.Length, "#A2");
1533 int nread = fs.Read (buffer, 0, 5);
1534 Assert.AreEqual (5, nread, "#A3");
1543 [Test] public void DeleteOnClose ()
1545 string path = TempFolder + DSC + "created.txt";
1547 FileStream fs = new FileStream (path, FileMode.CreateNew, FileAccess.Write, FileShare.None, 1024,
1548 FileOptions.DeleteOnClose);
1549 Assert.AreEqual (true, File.Exists (path), "DOC#1");
1551 Assert.AreEqual (false, File.Exists (path), "DOC#2");