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)
13 using NUnit
.Framework
;
18 namespace MonoTests
.System
.IO
21 public class FileStreamTest
: Assertion
23 string TempFolder
= Path
.Combine (Path
.GetTempPath (), "MonoTests.System.IO.Tests");
24 static readonly char DSC
= Path
.DirectorySeparatorChar
;
27 public void TearDown()
29 if (Directory
.Exists (TempFolder
))
30 Directory
.Delete (TempFolder
, true);
36 if (Directory
.Exists (TempFolder
))
37 Directory
.Delete (TempFolder
, true);
39 Directory
.CreateDirectory (TempFolder
);
42 public void TestCtr ()
44 string path
= TempFolder
+ DSC
+ "testfilestream.tmp.1";
46 FileStream stream
= null;
48 stream
= new FileStream (path
, FileMode
.Create
);
58 [ExpectedException (typeof (ArgumentException
))]
59 public void CtorArgumentException1 ()
62 stream
= new FileStream ("", FileMode
.Create
);
67 [ExpectedException (typeof (ArgumentNullException
))]
68 public void CtorArgumentNullException ()
70 FileStream stream
= new FileStream (null, FileMode
.Create
);
75 [ExpectedException (typeof (FileNotFoundException
))]
76 public void CtorFileNotFoundException1 ()
78 string path
= TempFolder
+ DSC
+ "thisfileshouldnotexists.test";
80 FileStream stream
= null;
82 stream
= new FileStream (TempFolder
+ DSC
+ "thisfileshouldnotexists.test", FileMode
.Open
);
91 [ExpectedException (typeof (FileNotFoundException
))]
92 public void CtorFileNotFoundException2 ()
94 string path
= TempFolder
+ DSC
+ "thisfileshouldNOTexists.test";
96 FileStream stream
= null;
99 stream
= new FileStream (TempFolder
+ DSC
+ "thisfileshouldNOTexists.test", FileMode
.Truncate
);
109 [ExpectedException (typeof (IOException
))]
110 public void CtorIOException1 ()
112 string path
= TempFolder
+ DSC
+ "thisfileshouldexists.test";
113 FileStream stream
= null;
116 stream
= new FileStream (path
, FileMode
.CreateNew
);
119 stream
= new FileStream (path
, FileMode
.CreateNew
);
130 [ExpectedException (typeof (ArgumentOutOfRangeException
))]
131 public void CtorArgumentOutOfRangeException1 ()
133 FileStream stream
= null;
134 string path
= TempFolder
+ Path
.DirectorySeparatorChar
+ "temp";
137 stream
= new FileStream (path
, FileMode
.Append
| FileMode
.CreateNew
);
146 [ExpectedException (typeof (ArgumentOutOfRangeException
))]
147 public void CtorArgumentOutOfRangeException2 ()
149 FileStream stream
= null;
150 string path
= TempFolder
+ Path
.DirectorySeparatorChar
+ "temp";
153 stream
= new FileStream ("test.test.test", FileMode
.Append
| FileMode
.Open
);
162 [ExpectedException (typeof (DirectoryNotFoundException
))]
163 public void CtorDirectoryNotFoundException ()
165 string path
= TempFolder
+ DSC
+ "thisDirectoryShouldNotExists";
166 if (Directory
.Exists (path
))
167 Directory
.Delete (path
, true);
169 FileStream stream
= null;
171 stream
= new FileStream (path
+ DSC
+ "eitherthisfile.test", FileMode
.CreateNew
);
177 if (Directory
.Exists (path
))
178 Directory
.Delete (path
, true);
183 [ExpectedException (typeof (ArgumentOutOfRangeException
))]
184 public void CtorArgumentOutOfRangeException3 ()
186 string path
= TempFolder
+ DSC
+ "CtorArgumentOutOfRangeException1";
189 FileStream stream
= null;
191 stream
= new FileStream (path
, FileMode
.CreateNew
, FileAccess
.Read
, FileShare
.None
| FileShare
.Inheritable
);
200 [ExpectedException (typeof (ArgumentOutOfRangeException
))]
201 public void CtorArgumentOutOfRangeException4 ()
203 string path
= TempFolder
+ DSC
+ "CtorArgumentOutOfRangeException4";
206 FileStream stream
= null;
208 stream
= new FileStream (path
, FileMode
.OpenOrCreate
, FileAccess
.Read
, FileShare
.ReadWrite
, -1);
217 [ExpectedException (typeof (ArgumentOutOfRangeException
))]
218 public void CtorBufferSizeZero ()
220 // Buffer size can't be zero
222 string path
= Path
.Combine (TempFolder
, "CtorBufferSizeZero");
225 FileStream stream
= null;
227 stream
= new FileStream (path
, FileMode
.CreateNew
, FileAccess
.Write
, FileShare
.ReadWrite
, 0);
236 [ExpectedException (typeof (ArgumentException
))]
237 public void CtorArgumentException2 ()
239 // FileMode.CreateNew && FileAccess.Read
241 string path
= TempFolder
+ Path
.DirectorySeparatorChar
+ "temp";
242 FileStream stream
= null;
247 stream
= new FileStream (".test.test.test.2", FileMode
.CreateNew
, FileAccess
.Read
, FileShare
.None
| FileShare
.Write
);
258 [ExpectedException (typeof (ArgumentOutOfRangeException
))]
259 public void CtorArgumentOutOfRangeException5 ()
261 string path
= TempFolder
+ Path
.DirectorySeparatorChar
+ "temp";
264 FileStream stream
= null;
266 stream
= new FileStream (path
, FileMode
.CreateNew
, FileAccess
.Read
, FileShare
.Inheritable
| FileShare
.ReadWrite
);
276 [ExpectedException (typeof (ArgumentException
))]
277 public void CtorArgumentException3 ()
279 string path
= TempFolder
+ Path
.DirectorySeparatorChar
+ "temp";
280 FileStream stream
= null;
285 stream
= new FileStream (".test.test.test.2", FileMode
.Truncate
, FileAccess
.Read
);
294 [Test
, ExpectedException (typeof (IOException
))]
295 public void CtorIOException2 ()
297 FileStream stream
= null;
299 stream
= new FileStream (new IntPtr (Int32
.MaxValue
), FileAccess
.Read
);
306 [Test
, ExpectedException(typeof(IOException
))]
307 public void CtorIOException ()
309 string path
= TempFolder
+ DSC
+ "CTorIOException.Test";
310 FileStream stream
= null;
311 FileStream stream2
= null;
315 stream
= new FileStream (path
, FileMode
.CreateNew
);
317 // used by an another process
318 stream2
= new FileStream (path
, FileMode
.OpenOrCreate
);
329 public void CtorAccess1Read2Read ()
331 FileStream fs
= null;
332 FileStream fs2
= null;
333 string fn
= Path
.Combine (TempFolder
, "temp");
335 if (!File
.Exists (fn
)) {
336 TextWriter tw
= File
.CreateText (fn
);
340 fs
= new FileStream (fn
, FileMode
.Open
, FileAccess
.Read
);
341 fs2
= new FileStream (fn
, FileMode
.Open
, FileAccess
.Read
);
347 if (File
.Exists (fn
))
353 [ExpectedException (typeof (IOException
))]
354 public void CtorAccess1Read2Write ()
356 string fn
= Path
.Combine (TempFolder
, "temp");
357 FileStream fs
= null;
359 if (!File
.Exists (fn
)) {
360 using (TextWriter tw
= File
.CreateText (fn
)) {
364 fs
= new FileStream (fn
, FileMode
.Open
, FileAccess
.Read
);
365 fs
= new FileStream (fn
, FileMode
.Create
, FileAccess
.Write
);
369 if (File
.Exists (fn
))
375 [ExpectedException (typeof (IOException
))]
376 public void CtorAccess1Write2Write ()
378 string fn
= Path
.Combine (TempFolder
, "temp");
379 FileStream fs
= null;
381 if (File
.Exists (fn
))
383 fs
= new FileStream (fn
, FileMode
.Create
, FileAccess
.Write
);
384 fs
= new FileStream (fn
, FileMode
.Create
, FileAccess
.Write
);
388 if (File
.Exists (fn
))
396 string path
= TempFolder
+ DSC
+ "FileStreamTest.Write";
400 FileStream stream
= new FileStream (path
, FileMode
.CreateNew
, FileAccess
.ReadWrite
, FileShare
.ReadWrite
, 8);
402 byte[] outbytes
= new byte [] {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16}
;
403 byte[] bytes
= new byte [15];
405 // Check that the data is flushed when we overflow the buffer
406 // with a large amount of data
407 stream
.Write (outbytes
, 0, 5);
408 stream
.Write (outbytes
, 5, 10);
409 stream
.Seek (0, SeekOrigin
.Begin
);
411 stream
.Read (bytes
, 0, 15);
412 for (int i
= 0; i
< 15; ++i
)
413 AssertEquals (i
+ 1, bytes
[i
]);
415 // Check that the data is flushed when we overflow the buffer
416 // with a small amount of data
417 stream
.Write (outbytes
, 0, 7);
418 stream
.Write (outbytes
, 7, 7);
419 stream
.Write (outbytes
, 14, 1);
421 stream
.Read (bytes
, 0, 15);
422 stream
.Seek (15, SeekOrigin
.Begin
);
423 for (int i
= 0; i
< 15; ++i
)
424 AssertEquals (i
+ 1, bytes
[i
]);
429 public void Length ()
431 // Test that the Length property takes into account the data
433 string path
= TempFolder
+ DSC
+ "FileStreamTest.Length";
437 FileStream stream
= new FileStream (path
, FileMode
.CreateNew
);
439 byte[] outbytes
= new byte [] {1, 2, 3, 4}
;
441 stream
.Write (outbytes
, 0, 4);
442 AssertEquals (stream
.Length
, 4);
449 string path
= TempFolder
+ DSC
+ "FileStreamTest.Flush";
450 FileStream stream
= null;
451 FileStream stream2
= null;
456 stream
= new FileStream (path
, FileMode
.CreateNew
, FileAccess
.ReadWrite
, FileShare
.ReadWrite
);
457 stream2
= new FileStream (path
, FileMode
.Open
, FileAccess
.ReadWrite
, FileShare
.ReadWrite
);
459 stream
.Write (new byte [] {1, 2, 3, 4, 5}
, 0, 5);
461 byte [] bytes
= new byte [5];
462 stream2
.Read (bytes
, 0, 5);
464 AssertEquals ("test#01", 0, bytes
[0]);
465 AssertEquals ("test#02", 0, bytes
[1]);
466 AssertEquals ("test#03", 0, bytes
[2]);
467 AssertEquals ("test#04", 0, bytes
[3]);
470 stream2
.Read (bytes
, 0, 5);
471 AssertEquals ("test#05", 1, bytes
[0]);
472 AssertEquals ("test#06", 2, bytes
[1]);
473 AssertEquals ("test#07", 3, bytes
[2]);
474 AssertEquals ("test#08", 4, bytes
[3]);
485 public void TestDefaultProperties ()
487 string path
= TempFolder
+ Path
.DirectorySeparatorChar
+ "testfilestream.tmp.2";
490 FileStream stream
= new FileStream (path
, FileMode
.Create
);
492 AssertEquals ("test#01", true, stream
.CanRead
);
493 AssertEquals ("test#02", true, stream
.CanSeek
);
494 AssertEquals ("test#03", true, stream
.CanWrite
);
495 AssertEquals ("test#04", false, stream
.IsAsync
);
496 AssertEquals ("test#05", true, stream
.Name
.EndsWith (path
));
497 AssertEquals ("test#06", 0, stream
.Position
);
498 AssertEquals ("test#07", "System.IO.FileStream", stream
.ToString());
502 stream
= new FileStream (path
, FileMode
.OpenOrCreate
, FileAccess
.Read
);
503 AssertEquals ("test#08", true, stream
.CanRead
);
504 AssertEquals ("test#09", true, stream
.CanSeek
);
505 AssertEquals ("test#10", false, stream
.CanWrite
);
506 AssertEquals ("test#11", false, stream
.IsAsync
);
507 AssertEquals ("test#12", true, stream
.Name
.EndsWith (path
));
508 AssertEquals ("test#13", 0, stream
.Position
);
509 AssertEquals ("test#14", "System.IO.FileStream", stream
.ToString());
512 stream
= new FileStream (path
, FileMode
.Truncate
, FileAccess
.Write
, FileShare
.ReadWrite
);
513 AssertEquals ("test#15", false, stream
.CanRead
);
514 AssertEquals ("test#16", true, stream
.CanSeek
);
515 AssertEquals ("test#17", true, stream
.CanWrite
);
516 AssertEquals ("test#18", false, stream
.IsAsync
);
517 AssertEquals ("test#19", true, stream
.Name
.EndsWith ("testfilestream.tmp.2"));
518 AssertEquals ("test#20", 0, stream
.Position
);
519 AssertEquals ("test#21", "System.IO.FileStream", stream
.ToString());
524 [Category("NotWorking")]
526 public void TestLock_FailsOnMono ()
528 string path
= TempFolder
+ Path
.DirectorySeparatorChar
+ "TestLock";
531 FileStream stream
= new FileStream (path
, FileMode
.CreateNew
, FileAccess
.ReadWrite
);
533 stream
.Write (new Byte
[] {0,1,2,3,4,5,6,7,8,9,10}
, 0, 10);
536 stream
= new FileStream (path
, FileMode
.Open
, FileAccess
.ReadWrite
);
540 FileStream stream2
= new FileStream (path
, FileMode
.Open
, FileAccess
.Read
, FileShare
.ReadWrite
);
542 byte [] bytes
= new byte [5];
544 stream2
.Read (bytes
, 0, 5);
546 } catch (Exception e
) {
549 AssertEquals ("test#01", typeof (IOException
), e
.GetType ());
558 public void TestLock()
560 string path
= TempFolder
+ Path
.DirectorySeparatorChar
+ "TestLock";
563 FileStream stream
= new FileStream (path
, FileMode
.CreateNew
, FileAccess
.ReadWrite
);
565 stream
.Write (new Byte
[] {0,1,2,3,4,5,6,7,8,9,10}
, 0, 10);
568 stream
= new FileStream (path
, FileMode
.Open
, FileAccess
.ReadWrite
);
572 FileStream stream2
= new FileStream (path
, FileMode
.Open
, FileAccess
.Read
, FileShare
.ReadWrite
);
574 byte [] bytes
= new byte [5];
576 stream2
.Read (bytes
, 0, 5);
578 } catch (Exception e
) {
581 // AssertEquals ("test#01", typeof (IOException), e.GetType ());
583 // Moved into the previous test case.
586 stream2
.Seek (5, SeekOrigin
.Begin
);
587 stream2
.Read (bytes
, 0, 5);
589 AssertEquals ("test#02", 5, bytes
[0]);
590 AssertEquals ("test#03", 6, bytes
[1]);
591 AssertEquals ("test#04", 7, bytes
[2]);
592 AssertEquals ("test#05", 8, bytes
[3]);
593 AssertEquals ("test#06", 9, bytes
[4]);
596 stream2
.Seek (0, SeekOrigin
.Begin
);
597 stream2
.Read (bytes
, 0, 5);
599 AssertEquals ("test#02", 0, bytes
[0]);
600 AssertEquals ("test#03", 1, bytes
[1]);
601 AssertEquals ("test#04", 2, bytes
[2]);
602 AssertEquals ("test#05", 3, bytes
[3]);
603 AssertEquals ("test#06", 4, bytes
[4]);
614 string path
= TempFolder
+ DSC
+ "FST.Seek.Test";
617 FileStream stream
= new FileStream (path
, FileMode
.CreateNew
, FileAccess
.ReadWrite
, FileShare
.ReadWrite
);
618 FileStream stream2
= new FileStream (path
, FileMode
.Open
, FileAccess
.ReadWrite
, FileShare
.ReadWrite
);
620 stream
.Write (new byte [] {1, 2, 3, 4, 5, 6, 7, 8, 10}
, 0, 9);
621 AssertEquals ("test#01", 5, stream2
.Seek (5, SeekOrigin
.Begin
));
622 AssertEquals ("test#02", -1, stream2
.ReadByte ());
624 AssertEquals ("test#03", 2, stream2
.Seek (-3, SeekOrigin
.Current
));
625 AssertEquals ("test#04", -1, stream2
.ReadByte ());
627 AssertEquals ("test#05", 12, stream
.Seek (3, SeekOrigin
.Current
));
628 AssertEquals ("test#06", -1, stream
.ReadByte ());
630 AssertEquals ("test#07", 5, stream
.Seek (-7, SeekOrigin
.Current
));
631 AssertEquals ("test#08", 6, stream
.ReadByte ());
633 AssertEquals ("test#09", 5, stream2
.Seek (5, SeekOrigin
.Begin
));
634 AssertEquals ("test#10", 6, stream2
.ReadByte ());
642 public void TestSeek ()
644 string path
= TempFolder
+ Path
.DirectorySeparatorChar
+ "TestSeek";
647 FileStream stream
= new FileStream (path
, FileMode
.CreateNew
, FileAccess
.ReadWrite
, FileShare
.ReadWrite
);
648 stream
.Write (new byte[] {1, 2, 3, 4, 5, 6, 7, 8 , 9, 10}
, 0, 10);
650 stream
.Seek (5, SeekOrigin
.End
);
651 AssertEquals ("test#01", -1, stream
.ReadByte ());
653 stream
.Seek (-5, SeekOrigin
.End
);
654 AssertEquals ("test#02", 6, stream
.ReadByte ());
657 stream
.Seek (-11, SeekOrigin
.End
);
659 } catch (Exception e
) {
660 AssertEquals ("test#03", typeof (IOException
), e
.GetType ());
663 stream
.Seek (19, SeekOrigin
.Begin
);
664 AssertEquals ("test#04", -1, stream
.ReadByte ());
666 stream
.Seek (1, SeekOrigin
.Begin
);
667 AssertEquals ("test#05", 2, stream
.ReadByte ());
669 stream
.Seek (3, SeekOrigin
.Current
);
670 AssertEquals ("test#06", 6, stream
.ReadByte ());
672 stream
.Seek (-2, SeekOrigin
.Current
);
673 AssertEquals ("test#07", 5, stream
.ReadByte ());
677 // Test that seeks work correctly when seeking inside the buffer
678 stream
.Seek (0, SeekOrigin
.Begin
);
679 stream
.WriteByte (0);
680 stream
.WriteByte (1);
681 stream
.Seek (0, SeekOrigin
.Begin
);
682 byte[] buf
= new byte [1];
684 stream
.Write (buf
, 0, 1);
685 stream
.Write (buf
, 0, 1);
687 stream
.Seek (0, SeekOrigin
.Begin
);
688 AssertEquals ("test#08", 2, stream
.ReadByte ());
689 AssertEquals ("test#09", 2, stream
.ReadByte ());
696 public void TestClose ()
698 string path
= TempFolder
+ Path
.DirectorySeparatorChar
+ "TestClose";
701 FileStream stream
= new FileStream (path
, FileMode
.CreateNew
, FileAccess
.ReadWrite
);
703 stream
.Write (new byte [] {1, 2, 3, 4}
, 0, 4);
710 } catch (Exception e
) {
711 AssertEquals ("test#01", typeof (ObjectDisposedException
), e
.GetType ());
715 stream
.WriteByte (64);
717 } catch (Exception e
) {
718 AssertEquals ("test#02", typeof (ObjectDisposedException
), e
.GetType ());
724 } catch (Exception e
) {
725 AssertEquals ("test#03", typeof (ObjectDisposedException
), e
.GetType ());
729 long l
= stream
.Length
;
731 } catch (Exception e
) {
732 AssertEquals ("test#04", typeof (ObjectDisposedException
), e
.GetType ());
736 long l
= stream
.Position
;
738 } catch (Exception e
) {
739 AssertEquals ("test#05", typeof (ObjectDisposedException
), e
.GetType ());
742 AssertEquals ("test#06", false, stream
.CanRead
);
743 AssertEquals ("test#07", false, stream
.CanSeek
);
744 AssertEquals ("test#08", false, stream
.CanWrite
);
745 AssertEquals ("test#09", true, stream
.Name
.EndsWith (path
));
752 /// Checks whether the <see cref="FileStream" /> throws a <see cref="NotSupportedException" />
753 /// when the stream is opened with access mode <see cref="FileAccess.Read" /> and the
754 /// <see cref="FileStream.Write(byte[], int, int)" /> method is called.
757 [ExpectedException (typeof(NotSupportedException
))]
758 public void TestWriteVerifyAccessMode ()
760 string path
= TempFolder
+ Path
.DirectorySeparatorChar
+ "temp";
763 FileStream stream
= null;
767 buffer
= Encoding
.ASCII
.GetBytes ("test");
768 stream
= new FileStream (path
, FileMode
.OpenOrCreate
, FileAccess
.Read
);
769 stream
.Write (buffer
, 0, buffer
.Length
);
778 /// Checks whether the <see cref="FileStream" /> throws a <see cref="NotSupportedException" />
779 /// when the stream is opened with access mode <see cref="FileAccess.Read" /> and the
780 /// <see cref="FileStream.WriteByte(byte)" /> method is called.
783 [ExpectedException (typeof (NotSupportedException
))]
784 public void TestWriteByteVerifyAccessMode ()
786 string path
= TempFolder
+ Path
.DirectorySeparatorChar
+ "temp";
789 FileStream stream
= null;
792 stream
= new FileStream (path
, FileMode
.OpenOrCreate
, FileAccess
.Read
);
793 stream
.WriteByte (Byte
.MinValue
);
802 /// Checks whether the <see cref="FileStream" /> throws a <see cref="NotSupportedException" />
803 /// when the stream is opened with access mode <see cref="FileAccess.Write" /> and the
804 /// <see cref="FileStream.Read(byte[], int, int)" /> method is called.
807 [ExpectedException (typeof (NotSupportedException
))]
808 public void TestReadVerifyAccessMode ()
810 string path
= TempFolder
+ Path
.DirectorySeparatorChar
+ "temp";
813 FileStream stream
= null;
814 byte[] buffer
= new byte [100];
817 stream
= new FileStream (path
, FileMode
.OpenOrCreate
, FileAccess
.Write
);
818 stream
.Read (buffer
, 0, buffer
.Length
);
826 /// Checks whether the <see cref="FileStream" /> throws a <see cref="NotSupportedException" />
827 /// when the stream is opened with access mode <see cref="FileAccess.Write" /> and the
828 /// <see cref="FileStream.ReadByte()" /> method is called.
831 [ExpectedException (typeof (NotSupportedException
))]
832 public void TestReadByteVerifyAccessMode ()
834 string path
= TempFolder
+ Path
.DirectorySeparatorChar
+ "temp";
837 FileStream stream
= null;
840 stream
= new FileStream (path
, FileMode
.OpenOrCreate
, FileAccess
.Write
);
841 int readByte
= stream
.ReadByte ();
849 // Check that the stream is flushed even when it doesn't own the
852 public void TestFlushNotOwningHandle ()
854 string path
= Path
.Combine (TempFolder
, "TestFlushNotOwningHandle");
857 FileStream s
= new FileStream (path
, FileMode
.Create
);
858 using (FileStream s2
= new FileStream (s
.Handle
, FileAccess
.Write
, false)) {
859 byte[] buf
= new byte [2];
861 s2
.Write (buf
, 0, 1);
865 AssertEquals ((int)'1', s
.ReadByte ());
869 private void DeleteFile (string path
)
871 if (File
.Exists (path
))
876 [ExpectedException (typeof (ArgumentOutOfRangeException
))]
877 public void Read_OffsetNegative ()
879 string path
= TempFolder
+ Path
.DirectorySeparatorChar
+ "temp";
882 using (FileStream stream
= new FileStream (path
, FileMode
.OpenOrCreate
, FileAccess
.Read
)) {
883 stream
.Read (new byte[0], -1, 1);
888 [ExpectedException (typeof (ArgumentException
))]
889 public void Read_OffsetOverflow ()
891 string path
= TempFolder
+ Path
.DirectorySeparatorChar
+ "temp";
894 using (FileStream stream
= new FileStream (path
, FileMode
.OpenOrCreate
, FileAccess
.Read
)) {
895 stream
.Read (new byte[0], Int32
.MaxValue
, 1);
900 [ExpectedException (typeof (ArgumentOutOfRangeException
))]
901 public void Read_CountNegative ()
903 string path
= TempFolder
+ Path
.DirectorySeparatorChar
+ "temp";
906 using (FileStream stream
= new FileStream (path
, FileMode
.OpenOrCreate
, FileAccess
.Read
)) {
907 stream
.Read (new byte[0], 1, -1);
912 [ExpectedException (typeof (ArgumentException
))]
913 public void Read_CountOverflow ()
915 string path
= TempFolder
+ Path
.DirectorySeparatorChar
+ "temp";
918 using (FileStream stream
= new FileStream (path
, FileMode
.OpenOrCreate
, FileAccess
.Read
)) {
919 stream
.Read (new byte[0], 1, Int32
.MaxValue
);
924 [ExpectedException (typeof (ArgumentOutOfRangeException
))]
925 public void Write_OffsetNegative ()
927 string path
= TempFolder
+ Path
.DirectorySeparatorChar
+ "temp";
930 using (FileStream stream
= new FileStream (path
, FileMode
.OpenOrCreate
, FileAccess
.Write
)) {
931 stream
.Write (new byte[0], -1, 1);
936 [ExpectedException (typeof (ArgumentException
))]
937 public void Write_OffsetOverflow ()
939 string path
= TempFolder
+ Path
.DirectorySeparatorChar
+ "temp";
942 using (FileStream stream
= new FileStream (path
, FileMode
.OpenOrCreate
, FileAccess
.Write
)) {
943 stream
.Write (new byte[0], Int32
.MaxValue
, 1);
948 [ExpectedException (typeof (ArgumentOutOfRangeException
))]
949 public void Write_CountNegative ()
951 string path
= TempFolder
+ Path
.DirectorySeparatorChar
+ "temp";
954 using (FileStream stream
= new FileStream (path
, FileMode
.OpenOrCreate
, FileAccess
.Write
)) {
955 stream
.Write (new byte[0], 1, -1);
960 [ExpectedException (typeof (ArgumentException
))]
961 public void Write_CountOverflow ()
963 string path
= TempFolder
+ Path
.DirectorySeparatorChar
+ "temp";
966 using (FileStream stream
= new FileStream (path
, FileMode
.OpenOrCreate
, FileAccess
.Write
)) {
967 stream
.Write (new byte[0], 1, Int32
.MaxValue
);
972 [ExpectedException (typeof (ArgumentException
))]
973 public void Seek_InvalidSeekOrigin ()
975 string path
= TempFolder
+ Path
.DirectorySeparatorChar
+ "temp";
978 using (FileStream stream
= new FileStream (path
, FileMode
.OpenOrCreate
, FileAccess
.Read
)) {
979 stream
.Seek (0, (SeekOrigin
) (-1));
984 [ExpectedException (typeof (ArgumentException
))]
985 public void Constructor_InvalidFileHandle ()
987 new FileStream ((IntPtr
)(-1), FileAccess
.Read
);
991 public void PositionAfterSetLength ()
993 string path
= TempFolder
+ Path
.DirectorySeparatorChar
+ "temp";
996 using (FileStream stream
= new FileStream (path
, FileMode
.OpenOrCreate
, FileAccess
.Write
)) {
997 stream
.SetLength (32);
998 stream
.Position
= 32;
999 stream
.SetLength (16);
1000 AssertEquals ("Position==16", 16, stream
.Position
);
1005 [ExpectedException (typeof (ObjectDisposedException
))]
1006 public void SetLength_Disposed ()
1008 string path
= TempFolder
+ Path
.DirectorySeparatorChar
+ "temp";
1010 FileStream stream
= new FileStream (path
, FileMode
.OpenOrCreate
, FileAccess
.Write
);
1012 stream
.SetLength (16);
1016 [ExpectedException (typeof (ObjectDisposedException
))]
1017 public void Position_Disposed ()
1019 string path
= TempFolder
+ Path
.DirectorySeparatorChar
+ "temp";
1021 FileStream stream
= new FileStream (path
, FileMode
.OpenOrCreate
, FileAccess
.Read
);
1023 stream
.Position
= 0;
1027 [ExpectedException (typeof (ObjectDisposedException
))]
1028 public void BeginRead_Disposed ()
1030 string path
= TempFolder
+ Path
.DirectorySeparatorChar
+ "temp";
1032 FileStream stream
= new FileStream (path
, FileMode
.OpenOrCreate
, FileAccess
.Read
);
1034 stream
.EndRead (stream
.BeginRead (new byte[8], 0, 8, null, null));
1038 [ExpectedException (typeof (ObjectDisposedException
))]
1039 public void BeginWrite_Disposed ()
1041 string path
= TempFolder
+ Path
.DirectorySeparatorChar
+ "temp";
1043 FileStream stream
= new FileStream (path
, FileMode
.OpenOrCreate
, FileAccess
.Write
);
1045 stream
.EndWrite (stream
.BeginWrite (new byte[8], 0, 8, null, null));
1049 [ExpectedException (typeof (ObjectDisposedException
))]
1050 public void Lock_Disposed ()
1052 string path
= TempFolder
+ Path
.DirectorySeparatorChar
+ "temp";
1054 FileStream stream
= new FileStream (path
, FileMode
.OpenOrCreate
, FileAccess
.Write
);
1060 [ExpectedException (typeof (ObjectDisposedException
))]
1061 public void Unlock_Disposed ()
1063 string path
= TempFolder
+ Path
.DirectorySeparatorChar
+ "temp";
1065 FileStream stream
= new FileStream (path
, FileMode
.OpenOrCreate
, FileAccess
.Write
);
1067 stream
.Unlock (0,1);
1071 public void ReadBytePastEndOfStream ()
1073 string path
= TempFolder
+ Path
.DirectorySeparatorChar
+ "temp";
1075 using (FileStream stream
= new FileStream (path
, FileMode
.OpenOrCreate
, FileAccess
.Read
)) {
1076 stream
.Seek (0, SeekOrigin
.End
);
1077 AssertEquals ("ReadByte", -1, stream
.ReadByte ());
1083 [ExpectedException (typeof (NotSupportedException
))]
1084 public void SetLengthWithClosedBaseStream ()
1086 string fn
= Path
.Combine (TempFolder
, "temp");
1088 FileStream fs
= new FileStream (fn
, FileMode
.Create
);
1089 BufferedStream bs
= new BufferedStream (fs
);
1092 bs
.SetLength (1000);