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);
826 public void FileShare_Delete ()
828 string fn = Path.Combine (TempFolder, "temp");
830 using (Stream s = new FileStream (fn, FileMode.OpenOrCreate, FileAccess.Write, FileShare.Delete)) {
831 s.Write (new byte [1] { 0x5 }, 0, 1);
835 using (Stream s = new FileStream (fn, FileMode.OpenOrCreate, FileAccess.Write, FileShare.ReadWrite | FileShare.Delete)) {
836 s.Write (new byte [1] { 0x5 }, 0, 1);
840 using (Stream s = new FileStream (fn, FileMode.OpenOrCreate, FileAccess.Write, FileShare.Write | FileShare.Delete)) {
841 s.Write (new byte [1] { 0x5 }, 0, 1);
845 using (Stream s = new FileStream (fn, FileMode.OpenOrCreate, FileAccess.Write, FileShare.Read | FileShare.Delete)) {
846 s.Write (new byte [1] { 0x5 }, 0, 1);
850 using (Stream s = new FileStream (fn, FileMode.OpenOrCreate, FileAccess.Write, FileShare.Inheritable | FileShare.Delete)) {
851 s.Write (new byte [1] { 0x5 }, 0, 1);
860 string path = TempFolder + DSC + "FileStreamTest.Write";
864 FileStream stream = new FileStream (path, FileMode.CreateNew, FileAccess.ReadWrite, FileShare.ReadWrite, 8);
866 byte[] outbytes = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16 };
867 byte[] bytes = new byte[15];
869 // Check that the data is flushed when we overflow the buffer
870 // with a large amount of data
871 stream.Write (outbytes, 0, 5);
872 stream.Write (outbytes, 5, 10);
873 stream.Seek (0, SeekOrigin.Begin);
875 stream.Read (bytes, 0, 15);
876 for (int i = 0; i < 15; ++i)
877 Assert.AreEqual (i + 1, bytes[i], "#1");
879 // Check that the data is flushed when we overflow the buffer
880 // with a small amount of data
881 stream.Write (outbytes, 0, 7);
882 stream.Write (outbytes, 7, 7);
883 stream.Write (outbytes, 14, 1);
885 stream.Read (bytes, 0, 15);
886 stream.Seek (15, SeekOrigin.Begin);
887 for (int i = 0; i < 15; ++i)
888 Assert.AreEqual (i + 1, bytes[i], "#2");
893 public void Length ()
895 // Test that the Length property takes into account the data
897 string path = TempFolder + DSC + "FileStreamTest.Length";
901 FileStream stream = new FileStream (path, FileMode.CreateNew);
903 byte[] outbytes = new byte[] { 1, 2, 3, 4 };
905 stream.Write (outbytes, 0, 4);
906 Assert.AreEqual (4, stream.Length);
913 string path = TempFolder + DSC + "FileStreamTest.Flush";
914 FileStream stream = null;
915 FileStream stream2 = null;
920 stream = new FileStream (path, FileMode.CreateNew, FileAccess.ReadWrite, FileShare.ReadWrite);
921 stream2 = new FileStream (path, FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite);
923 stream.Write (new byte[] { 1, 2, 3, 4, 5 }, 0, 5);
925 byte[] bytes = new byte[5];
926 stream2.Read (bytes, 0, 5);
927 Assert.AreEqual (0, bytes[0], "#A1");
928 Assert.AreEqual (0, bytes[1], "#A2");
929 Assert.AreEqual (0, bytes[2], "#A3");
930 Assert.AreEqual (0, bytes[3], "#A4");
933 stream2.Read (bytes, 0, 5);
934 Assert.AreEqual (1, bytes[0], "#B1");
935 Assert.AreEqual (2, bytes[1], "#B2");
936 Assert.AreEqual (3, bytes[2], "#B3");
937 Assert.AreEqual (4, bytes[3], "#B4");
948 public void TestDefaultProperties ()
950 string path = TempFolder + Path.DirectorySeparatorChar + "testfilestream.tmp.2";
953 FileStream stream = new FileStream (path, FileMode.Create);
955 Assert.IsTrue (stream.CanRead, "#A1");
956 Assert.IsTrue (stream.CanSeek, "#A2");
957 Assert.IsTrue (stream.CanWrite, "#A3");
958 Assert.IsFalse (stream.IsAsync, "#A4");
959 Assert.IsTrue (stream.Name.EndsWith (path), "#A5");
960 Assert.AreEqual (0, stream.Position, "#A6");
961 Assert.AreEqual ("System.IO.FileStream", stream.ToString (), "#A7");
965 stream = new FileStream (path, FileMode.OpenOrCreate, FileAccess.Read);
966 Assert.IsTrue (stream.CanRead, "#B1");
967 Assert.IsTrue (stream.CanSeek, "#B2");
968 Assert.IsFalse (stream.CanWrite, "#B3");
969 Assert.IsFalse (stream.IsAsync, "#B4");
970 Assert.IsTrue (stream.Name.EndsWith (path), "#B5");
971 Assert.AreEqual (0, stream.Position, "#B6");
972 Assert.AreEqual ("System.IO.FileStream", stream.ToString (), "#B7");
975 stream = new FileStream (path, FileMode.Truncate, FileAccess.Write, FileShare.ReadWrite);
976 Assert.IsFalse (stream.CanRead, "#C1");
977 Assert.IsTrue (stream.CanSeek, "#C2");
978 Assert.IsTrue (stream.CanWrite, "#C3");
979 Assert.IsFalse (stream.IsAsync, "#C4");
980 Assert.IsTrue (stream.Name.EndsWith ("testfilestream.tmp.2"), "#C5");
981 Assert.AreEqual (0, stream.Position, "#C6");
982 Assert.AreEqual ("System.IO.FileStream", stream.ToString (), "#C7");
987 [Category ("NotWorking")]
988 // Bug: 71371 -> duplicate and WONTFIX.
989 public void TestLock_FailsOnMono ()
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);
1009 } catch (Exception e) {
1010 Assert.AreEqual (typeof (IOException), e.GetType (), "#2");
1019 [Category("TargetJvmNotSupported")] // File locking not supported for TARGET_JVM
1020 public void TestLock ()
1022 string path = TempFolder + Path.DirectorySeparatorChar + "TestLock";
1025 FileStream stream = new FileStream (path, FileMode.CreateNew, FileAccess.ReadWrite);
1027 stream.Write (new Byte[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }, 0, 10);
1030 stream = new FileStream (path, FileMode.Open, FileAccess.ReadWrite);
1034 FileStream stream2 = new FileStream (path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
1036 byte[] bytes = new byte[5];
1038 stream2.Read (bytes, 0, 5);
1039 Assert.Fail ("#A1");
1040 } catch (Exception) {
1041 // Bug #71371: on MS.NET you get an IOException detailing a lock
1042 // Assert.AreEqual (typeof (IOException), e.GetType (), "#A2");
1045 stream2.Seek (5, SeekOrigin.Begin);
1046 stream2.Read (bytes, 0, 5);
1048 Assert.AreEqual (5, bytes[0], "#B1");
1049 Assert.AreEqual (6, bytes[1], "#B2");
1050 Assert.AreEqual (7, bytes[2], "#B3");
1051 Assert.AreEqual (8, bytes[3], "#B4");
1052 Assert.AreEqual (9, bytes[4], "#B5");
1054 stream.Unlock (0, 5);
1055 stream2.Seek (0, SeekOrigin.Begin);
1056 stream2.Read (bytes, 0, 5);
1058 Assert.AreEqual (0, bytes[0], "#C1");
1059 Assert.AreEqual (1, bytes[1], "#C2");
1060 Assert.AreEqual (2, bytes[2], "#C3");
1061 Assert.AreEqual (3, bytes[3], "#C4");
1062 Assert.AreEqual (4, bytes[4], "#C5");
1073 string path = TempFolder + DSC + "FST.Seek.Test";
1076 FileStream stream = new FileStream (path, FileMode.CreateNew, FileAccess.ReadWrite, FileShare.ReadWrite);
1077 FileStream stream2 = new FileStream (path, FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite);
1079 stream.Write (new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 10 }, 0, 9);
1080 Assert.AreEqual (5, stream2.Seek (5, SeekOrigin.Begin), "#1");
1081 Assert.AreEqual (-1, stream2.ReadByte (), "#2");
1083 Assert.AreEqual (2, stream2.Seek (-3, SeekOrigin.Current), "#3");
1084 Assert.AreEqual (-1, stream2.ReadByte (), "#4");
1086 Assert.AreEqual (12, stream.Seek (3, SeekOrigin.Current), "#5");
1087 Assert.AreEqual (-1, stream.ReadByte (), "#6");
1089 Assert.AreEqual (5, stream.Seek (-7, SeekOrigin.Current), "#7");
1090 Assert.AreEqual (6, stream.ReadByte (), "#8");
1092 Assert.AreEqual (5, stream2.Seek (5, SeekOrigin.Begin), "#9");
1093 Assert.AreEqual (6, stream2.ReadByte (), "#10");
1101 public void TestSeek ()
1103 string path = TempFolder + Path.DirectorySeparatorChar + "TestSeek";
1106 FileStream stream = new FileStream (path, FileMode.CreateNew, FileAccess.ReadWrite, FileShare.ReadWrite);
1107 stream.Write (new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }, 0, 10);
1109 stream.Seek (5, SeekOrigin.End);
1110 Assert.AreEqual (-1, stream.ReadByte (), "#1");
1112 stream.Seek (-5, SeekOrigin.End);
1113 Assert.AreEqual (6, stream.ReadByte (), "#2");
1116 stream.Seek (-11, SeekOrigin.End);
1118 } catch (Exception e) {
1119 Assert.AreEqual (typeof (IOException), e.GetType (), "#4");
1122 stream.Seek (19, SeekOrigin.Begin);
1123 Assert.AreEqual (-1, stream.ReadByte (), "#5");
1125 stream.Seek (1, SeekOrigin.Begin);
1126 Assert.AreEqual (2, stream.ReadByte (), "#6");
1128 stream.Seek (3, SeekOrigin.Current);
1129 Assert.AreEqual (6, stream.ReadByte (), "#7");
1131 stream.Seek (-2, SeekOrigin.Current);
1132 Assert.AreEqual (5, stream.ReadByte (), "#8");
1136 // Test that seeks work correctly when seeking inside the buffer
1137 stream.Seek (0, SeekOrigin.Begin);
1138 stream.WriteByte (0);
1139 stream.WriteByte (1);
1140 stream.Seek (0, SeekOrigin.Begin);
1141 byte[] buf = new byte[1];
1143 stream.Write (buf, 0, 1);
1144 stream.Write (buf, 0, 1);
1146 stream.Seek (0, SeekOrigin.Begin);
1147 Assert.AreEqual (2, stream.ReadByte (), "#9");
1148 Assert.AreEqual (2, stream.ReadByte (), "#10");
1155 public void TestClose ()
1157 string path = TempFolder + Path.DirectorySeparatorChar + "TestClose";
1160 FileStream stream = new FileStream (path, FileMode.CreateNew, FileAccess.ReadWrite);
1162 stream.Write (new byte[] { 1, 2, 3, 4 }, 0, 4);
1168 Assert.Fail ("#A1");
1169 } catch (Exception e) {
1170 Assert.AreEqual (typeof (ObjectDisposedException), e.GetType (), "#A2");
1174 stream.WriteByte (64);
1175 Assert.Fail ("#B1");
1176 } catch (Exception e) {
1177 Assert.AreEqual (typeof (ObjectDisposedException), e.GetType (), "#B2");
1182 Assert.Fail ("#C1");
1183 } catch (Exception e) {
1184 Assert.AreEqual (typeof (ObjectDisposedException), e.GetType (), "#C2");
1188 long l = stream.Length;
1189 Assert.Fail ("#D1");
1190 } catch (Exception e) {
1191 Assert.AreEqual (typeof (ObjectDisposedException), e.GetType (), "#D2");
1195 long l = stream.Position;
1196 Assert.Fail ("#E1");
1197 } catch (Exception e) {
1198 Assert.AreEqual (typeof (ObjectDisposedException), e.GetType (), "#E2");
1201 Assert.IsFalse (stream.CanRead, "#F1");
1202 Assert.IsFalse (stream.CanSeek, "#F2");
1203 Assert.IsFalse (stream.CanWrite, "#F3");
1204 Assert.IsTrue (stream.Name.EndsWith (path), "#F4");
1211 /// Checks whether the <see cref="FileStream" /> throws a <see cref="NotSupportedException" />
1212 /// when the stream is opened with access mode <see cref="FileAccess.Read" /> and the
1213 /// <see cref="FileStream.Write(byte[], int, int)" /> method is called.
1216 [ExpectedException (typeof (NotSupportedException))]
1217 public void TestWriteVerifyAccessMode ()
1219 string path = TempFolder + Path.DirectorySeparatorChar + "temp";
1222 FileStream stream = null;
1226 buffer = Encoding.ASCII.GetBytes ("test");
1227 stream = new FileStream (path, FileMode.OpenOrCreate, FileAccess.Read);
1228 stream.Write (buffer, 0, buffer.Length);
1237 /// Checks whether the <see cref="FileStream" /> throws a <see cref="NotSupportedException" />
1238 /// when the stream is opened with access mode <see cref="FileAccess.Read" /> and the
1239 /// <see cref="FileStream.WriteByte(byte)" /> method is called.
1242 [ExpectedException (typeof (NotSupportedException))]
1243 public void TestWriteByteVerifyAccessMode ()
1245 string path = TempFolder + Path.DirectorySeparatorChar + "temp";
1248 FileStream stream = null;
1251 stream = new FileStream (path, FileMode.OpenOrCreate, FileAccess.Read);
1252 stream.WriteByte (Byte.MinValue);
1261 /// Checks whether the <see cref="FileStream" /> throws a <see cref="NotSupportedException" />
1262 /// when the stream is opened with access mode <see cref="FileAccess.Write" /> and the
1263 /// <see cref="FileStream.Read(byte[], int, int)" /> method is called.
1266 [ExpectedException (typeof (NotSupportedException))]
1267 public void TestReadVerifyAccessMode ()
1269 string path = TempFolder + Path.DirectorySeparatorChar + "temp";
1272 FileStream stream = null;
1273 byte[] buffer = new byte[100];
1276 stream = new FileStream (path, FileMode.OpenOrCreate, FileAccess.Write);
1277 stream.Read (buffer, 0, buffer.Length);
1285 /// Checks whether the <see cref="FileStream" /> throws a <see cref="NotSupportedException" />
1286 /// when the stream is opened with access mode <see cref="FileAccess.Write" /> and the
1287 /// <see cref="FileStream.ReadByte()" /> method is called.
1290 [ExpectedException (typeof (NotSupportedException))]
1291 public void TestReadByteVerifyAccessMode ()
1293 string path = TempFolder + Path.DirectorySeparatorChar + "temp";
1296 FileStream stream = null;
1299 stream = new FileStream (path, FileMode.OpenOrCreate, FileAccess.Write);
1300 int readByte = stream.ReadByte ();
1308 #if !TARGET_JVM // No support IntPtr file handles under TARGET_JVM
1309 // Check that the stream is flushed even when it doesn't own the
1312 public void TestFlushNotOwningHandle ()
1314 string path = Path.Combine (TempFolder, "TestFlushNotOwningHandle");
1317 FileStream s = new FileStream (path, FileMode.Create);
1318 using (FileStream s2 = new FileStream (s.Handle, FileAccess.Write, false)) {
1319 byte[] buf = new byte[2];
1321 s2.Write (buf, 0, 1);
1325 Assert.AreEqual ((int) '1', s.ReadByte ());
1328 #endif // TARGET_JVM
1330 private void DeleteFile (string path)
1332 if (File.Exists (path))
1337 [ExpectedException (typeof (ArgumentOutOfRangeException))]
1338 public void Read_OffsetNegative ()
1340 string path = TempFolder + Path.DirectorySeparatorChar + "temp";
1343 using (FileStream stream = new FileStream (path, FileMode.OpenOrCreate, FileAccess.Read)) {
1344 stream.Read (new byte[0], -1, 1);
1349 [ExpectedException (typeof (ArgumentException))]
1350 public void Read_OffsetOverflow ()
1352 string path = TempFolder + Path.DirectorySeparatorChar + "temp";
1355 using (FileStream stream = new FileStream (path, FileMode.OpenOrCreate, FileAccess.Read)) {
1356 stream.Read (new byte[0], Int32.MaxValue, 1);
1361 [ExpectedException (typeof (ArgumentOutOfRangeException))]
1362 public void Read_CountNegative ()
1364 string path = TempFolder + Path.DirectorySeparatorChar + "temp";
1367 using (FileStream stream = new FileStream (path, FileMode.OpenOrCreate, FileAccess.Read)) {
1368 stream.Read (new byte[0], 1, -1);
1373 [ExpectedException (typeof (ArgumentException))]
1374 public void Read_CountOverflow ()
1376 string path = TempFolder + Path.DirectorySeparatorChar + "temp";
1379 using (FileStream stream = new FileStream (path, FileMode.OpenOrCreate, FileAccess.Read)) {
1380 stream.Read (new byte[0], 1, Int32.MaxValue);
1385 [ExpectedException (typeof (ArgumentOutOfRangeException))]
1386 public void Write_OffsetNegative ()
1388 string path = TempFolder + Path.DirectorySeparatorChar + "temp";
1391 using (FileStream stream = new FileStream (path, FileMode.OpenOrCreate, FileAccess.Write)) {
1392 stream.Write (new byte[0], -1, 1);
1397 [ExpectedException (typeof (ArgumentException))]
1398 public void Write_OffsetOverflow ()
1400 string path = TempFolder + Path.DirectorySeparatorChar + "temp";
1403 using (FileStream stream = new FileStream (path, FileMode.OpenOrCreate, FileAccess.Write)) {
1404 stream.Write (new byte[0], Int32.MaxValue, 1);
1409 [ExpectedException (typeof (ArgumentOutOfRangeException))]
1410 public void Write_CountNegative ()
1412 string path = TempFolder + Path.DirectorySeparatorChar + "temp";
1415 using (FileStream stream = new FileStream (path, FileMode.OpenOrCreate, FileAccess.Write)) {
1416 stream.Write (new byte[0], 1, -1);
1421 [ExpectedException (typeof (ArgumentException))]
1422 public void Write_CountOverflow ()
1424 string path = TempFolder + Path.DirectorySeparatorChar + "temp";
1427 using (FileStream stream = new FileStream (path, FileMode.OpenOrCreate, FileAccess.Write)) {
1428 stream.Write (new byte[0], 1, Int32.MaxValue);
1433 [ExpectedException (typeof (ArgumentException))]
1434 public void Seek_InvalidSeekOrigin ()
1436 string path = TempFolder + Path.DirectorySeparatorChar + "temp";
1439 using (FileStream stream = new FileStream (path, FileMode.OpenOrCreate, FileAccess.Read)) {
1440 stream.Seek (0, (SeekOrigin) (-1));
1444 #if !TARGET_JVM // No support IntPtr file handles under TARGET_JVM
1446 [ExpectedException (typeof (ArgumentException))]
1447 public void Constructor_InvalidFileHandle ()
1449 new FileStream ((IntPtr) (-1L), FileAccess.Read);
1451 #endif // TARGET_JVM
1454 public void PositionAfterSetLength ()
1456 string path = TempFolder + Path.DirectorySeparatorChar + "temp";
1459 using (FileStream stream = new FileStream (path, FileMode.OpenOrCreate, FileAccess.Write)) {
1460 stream.SetLength (32);
1461 stream.Position = 32;
1462 stream.SetLength (16);
1463 Assert.AreEqual (16, stream.Position);
1468 [ExpectedException (typeof (ObjectDisposedException))]
1469 public void SetLength_Disposed ()
1471 string path = TempFolder + Path.DirectorySeparatorChar + "temp";
1473 FileStream stream = new FileStream (path, FileMode.OpenOrCreate, FileAccess.Write);
1475 stream.SetLength (16);
1479 [ExpectedException (typeof (ObjectDisposedException))]
1480 public void Position_Disposed ()
1482 string path = TempFolder + Path.DirectorySeparatorChar + "temp";
1484 FileStream stream = new FileStream (path, FileMode.OpenOrCreate, FileAccess.Read);
1486 stream.Position = 0;
1490 [Category("TargetJvmNotSupported")] // Async IO not supported for TARGET_JVM
1491 [ExpectedException (typeof (ObjectDisposedException))]
1492 public void BeginRead_Disposed ()
1494 string path = TempFolder + Path.DirectorySeparatorChar + "temp";
1496 FileStream stream = new FileStream (path, FileMode.OpenOrCreate, FileAccess.Read);
1498 stream.EndRead (stream.BeginRead (new byte[8], 0, 8, null, null));
1502 [Category("TargetJvmNotSupported")] // Async IO not supported for TARGET_JVM
1503 [ExpectedException (typeof (ObjectDisposedException))]
1504 public void BeginWrite_Disposed ()
1506 string path = TempFolder + Path.DirectorySeparatorChar + "temp";
1508 FileStream stream = new FileStream (path, FileMode.OpenOrCreate, FileAccess.Write);
1510 stream.EndWrite (stream.BeginWrite (new byte[8], 0, 8, null, null));
1514 [Category("TargetJvmNotSupported")] // File locking not supported for TARGET_JVM
1515 [ExpectedException (typeof (ObjectDisposedException))]
1516 public void Lock_Disposed ()
1518 string path = TempFolder + Path.DirectorySeparatorChar + "temp";
1520 FileStream stream = new FileStream (path, FileMode.OpenOrCreate, FileAccess.Write);
1526 [Category("TargetJvmNotSupported")] // File locking not supported for TARGET_JVM
1527 [ExpectedException (typeof (ObjectDisposedException))]
1528 public void Unlock_Disposed ()
1530 string path = TempFolder + Path.DirectorySeparatorChar + "temp";
1532 FileStream stream = new FileStream (path, FileMode.OpenOrCreate, FileAccess.Write);
1534 stream.Unlock (0, 1);
1538 public void ReadBytePastEndOfStream ()
1540 string path = TempFolder + Path.DirectorySeparatorChar + "temp";
1542 using (FileStream stream = new FileStream (path, FileMode.OpenOrCreate, FileAccess.Read)) {
1543 stream.Seek (0, SeekOrigin.End);
1544 Assert.AreEqual (-1, stream.ReadByte ());
1550 [ExpectedException (typeof (NotSupportedException))]
1551 public void SetLengthWithClosedBaseStream ()
1553 string fn = Path.Combine (TempFolder, "temp");
1555 FileStream fs = new FileStream (fn, FileMode.Create);
1556 BufferedStream bs = new BufferedStream (fs);
1559 bs.SetLength (1000);
1566 public void LengthAfterWrite ()
1568 string path = TempFolder + DSC + "oneofthefilescreated.txt";
1569 FileStream fs = null;
1572 fs = new FileStream (path, FileMode.CreateNew);
1573 fs.WriteByte (Convert.ToByte ('A'));
1574 byte [] buffer = Encoding.ASCII.GetBytes (" is a first character.");
1575 fs.Write (buffer, 0, buffer.Length);
1576 fs.Seek (0, SeekOrigin.Begin);
1577 char a = Convert.ToChar (fs.ReadByte ());
1578 Assert.AreEqual ('A', a, "#A1");
1579 Assert.AreEqual (23, fs.Length, "#A2");
1580 int nread = fs.Read (buffer, 0, 5);
1581 Assert.AreEqual (5, nread, "#A3");
1590 [Category("TargetJvmNotSupported")] // FileOptions.DeleteOnClose not supported for TARGET_JVM
1592 public void DeleteOnClose ()
1594 string path = TempFolder + DSC + "created.txt";
1596 FileStream fs = new FileStream (path, FileMode.CreateNew, FileAccess.Write, FileShare.None, 1024,
1597 FileOptions.DeleteOnClose);
1598 Assert.AreEqual (true, File.Exists (path), "DOC#1");
1600 Assert.AreEqual (false, File.Exists (path), "DOC#2");