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;
334 if (!File
.Exists ("temp")) {
335 TextWriter tw
= File
.CreateText ("temp");
339 fs
= new FileStream ("temp", FileMode
.Open
, FileAccess
.Read
);
340 fs2
= new FileStream ("temp", FileMode
.Open
, FileAccess
.Read
);
346 if (File
.Exists ("temp"))
347 File
.Delete ("temp");
352 [ExpectedException (typeof (IOException
))]
353 public void CtorAccess1Read2Write ()
355 FileStream fs
= null;
357 if (!File
.Exists ("temp")) {
358 using (TextWriter tw
= File
.CreateText ("temp")) {
362 fs
= new FileStream ("temp", FileMode
.Open
, FileAccess
.Read
);
363 fs
= new FileStream ("temp", FileMode
.Create
, FileAccess
.Write
);
367 if (File
.Exists ("temp"))
368 File
.Delete ("temp");
373 [ExpectedException (typeof (IOException
))]
374 public void CtorAccess1Write2Write ()
376 FileStream fs
= null;
378 if (File
.Exists ("temp"))
379 File
.Delete ("temp");
380 fs
= new FileStream ("temp", FileMode
.Create
, FileAccess
.Write
);
381 fs
= new FileStream ("temp", FileMode
.Create
, FileAccess
.Write
);
385 if (File
.Exists ("temp"))
386 File
.Delete ("temp");
393 string path
= TempFolder
+ DSC
+ "FileStreamTest.Write";
397 FileStream stream
= new FileStream (path
, FileMode
.CreateNew
, FileAccess
.ReadWrite
, FileShare
.ReadWrite
, 8);
399 byte[] outbytes
= new byte [] {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16}
;
400 byte[] bytes
= new byte [15];
402 // Check that the data is flushed when we overflow the buffer
403 // with a large amount of data
404 stream
.Write (outbytes
, 0, 5);
405 stream
.Write (outbytes
, 5, 10);
406 stream
.Seek (0, SeekOrigin
.Begin
);
408 stream
.Read (bytes
, 0, 15);
409 for (int i
= 0; i
< 15; ++i
)
410 AssertEquals (i
+ 1, bytes
[i
]);
412 // Check that the data is flushed when we overflow the buffer
413 // with a small amount of data
414 stream
.Write (outbytes
, 0, 7);
415 stream
.Write (outbytes
, 7, 7);
416 stream
.Write (outbytes
, 14, 1);
418 stream
.Read (bytes
, 0, 15);
419 stream
.Seek (15, SeekOrigin
.Begin
);
420 for (int i
= 0; i
< 15; ++i
)
421 AssertEquals (i
+ 1, bytes
[i
]);
426 public void Length ()
428 // Test that the Length property takes into account the data
430 string path
= TempFolder
+ DSC
+ "FileStreamTest.Length";
434 FileStream stream
= new FileStream (path
, FileMode
.CreateNew
);
436 byte[] outbytes
= new byte [] {1, 2, 3, 4}
;
438 stream
.Write (outbytes
, 0, 4);
439 AssertEquals (stream
.Length
, 4);
446 string path
= TempFolder
+ DSC
+ "FileStreamTest.Flush";
447 FileStream stream
= null;
448 FileStream stream2
= null;
453 stream
= new FileStream (path
, FileMode
.CreateNew
, FileAccess
.ReadWrite
, FileShare
.ReadWrite
);
454 stream2
= new FileStream (path
, FileMode
.Open
, FileAccess
.ReadWrite
, FileShare
.ReadWrite
);
456 stream
.Write (new byte [] {1, 2, 3, 4, 5}
, 0, 5);
458 byte [] bytes
= new byte [5];
459 stream2
.Read (bytes
, 0, 5);
461 AssertEquals ("test#01", 0, bytes
[0]);
462 AssertEquals ("test#02", 0, bytes
[1]);
463 AssertEquals ("test#03", 0, bytes
[2]);
464 AssertEquals ("test#04", 0, bytes
[3]);
467 stream2
.Read (bytes
, 0, 5);
468 AssertEquals ("test#05", 1, bytes
[0]);
469 AssertEquals ("test#06", 2, bytes
[1]);
470 AssertEquals ("test#07", 3, bytes
[2]);
471 AssertEquals ("test#08", 4, bytes
[3]);
482 public void TestDefaultProperties ()
484 string path
= TempFolder
+ Path
.DirectorySeparatorChar
+ "testfilestream.tmp.2";
487 FileStream stream
= new FileStream (path
, FileMode
.Create
);
489 AssertEquals ("test#01", true, stream
.CanRead
);
490 AssertEquals ("test#02", true, stream
.CanSeek
);
491 AssertEquals ("test#03", true, stream
.CanWrite
);
492 AssertEquals ("test#04", false, stream
.IsAsync
);
493 AssertEquals ("test#05", true, stream
.Name
.EndsWith (path
));
494 AssertEquals ("test#06", 0, stream
.Position
);
495 AssertEquals ("test#07", "System.IO.FileStream", stream
.ToString());
499 stream
= new FileStream (path
, FileMode
.OpenOrCreate
, FileAccess
.Read
);
500 AssertEquals ("test#08", true, stream
.CanRead
);
501 AssertEquals ("test#09", true, stream
.CanSeek
);
502 AssertEquals ("test#10", false, stream
.CanWrite
);
503 AssertEquals ("test#11", false, stream
.IsAsync
);
504 AssertEquals ("test#12", true, stream
.Name
.EndsWith (path
));
505 AssertEquals ("test#13", 0, stream
.Position
);
506 AssertEquals ("test#14", "System.IO.FileStream", stream
.ToString());
509 stream
= new FileStream (path
, FileMode
.Truncate
, FileAccess
.Write
, FileShare
.ReadWrite
);
510 AssertEquals ("test#15", false, stream
.CanRead
);
511 AssertEquals ("test#16", true, stream
.CanSeek
);
512 AssertEquals ("test#17", true, stream
.CanWrite
);
513 AssertEquals ("test#18", false, stream
.IsAsync
);
514 AssertEquals ("test#19", true, stream
.Name
.EndsWith ("testfilestream.tmp.2"));
515 AssertEquals ("test#20", 0, stream
.Position
);
516 AssertEquals ("test#21", "System.IO.FileStream", stream
.ToString());
521 public void TestLock()
523 string path
= TempFolder
+ Path
.DirectorySeparatorChar
+ "TestLock";
526 FileStream stream
= new FileStream (path
, FileMode
.CreateNew
, FileAccess
.ReadWrite
);
528 stream
.Write (new Byte
[] {0,1,2,3,4,5,6,7,8,9,10}
, 0, 10);
531 stream
= new FileStream (path
, FileMode
.Open
, FileAccess
.ReadWrite
);
535 FileStream stream2
= new FileStream (path
, FileMode
.Open
, FileAccess
.Read
, FileShare
.ReadWrite
);
537 byte [] bytes
= new byte [5];
539 stream2
.Read (bytes
, 0, 5);
541 } catch (Exception e
) {
544 AssertEquals ("test#01", typeof (IOException
), e
.GetType ());
547 stream2
.Seek (5, SeekOrigin
.Begin
);
548 stream2
.Read (bytes
, 0, 5);
550 AssertEquals ("test#02", 5, bytes
[0]);
551 AssertEquals ("test#03", 6, bytes
[1]);
552 AssertEquals ("test#04", 7, bytes
[2]);
553 AssertEquals ("test#05", 8, bytes
[3]);
554 AssertEquals ("test#06", 9, bytes
[4]);
557 stream2
.Seek (0, SeekOrigin
.Begin
);
558 stream2
.Read (bytes
, 0, 5);
560 AssertEquals ("test#02", 0, bytes
[0]);
561 AssertEquals ("test#03", 1, bytes
[1]);
562 AssertEquals ("test#04", 2, bytes
[2]);
563 AssertEquals ("test#05", 3, bytes
[3]);
564 AssertEquals ("test#06", 4, bytes
[4]);
575 string path
= TempFolder
+ DSC
+ "FST.Seek.Test";
578 FileStream stream
= new FileStream (path
, FileMode
.CreateNew
, FileAccess
.ReadWrite
, FileShare
.ReadWrite
);
579 FileStream stream2
= new FileStream (path
, FileMode
.Open
, FileAccess
.ReadWrite
, FileShare
.ReadWrite
);
581 stream
.Write (new byte [] {1, 2, 3, 4, 5, 6, 7, 8, 10}
, 0, 9);
582 AssertEquals ("test#01", 5, stream2
.Seek (5, SeekOrigin
.Begin
));
583 AssertEquals ("test#02", -1, stream2
.ReadByte ());
585 AssertEquals ("test#03", 2, stream2
.Seek (-3, SeekOrigin
.Current
));
586 AssertEquals ("test#04", -1, stream2
.ReadByte ());
588 AssertEquals ("test#05", 12, stream
.Seek (3, SeekOrigin
.Current
));
589 AssertEquals ("test#06", -1, stream
.ReadByte ());
591 AssertEquals ("test#07", 5, stream
.Seek (-7, SeekOrigin
.Current
));
592 AssertEquals ("test#08", 6, stream
.ReadByte ());
594 AssertEquals ("test#09", 5, stream2
.Seek (5, SeekOrigin
.Begin
));
595 AssertEquals ("test#10", 6, stream2
.ReadByte ());
603 public void TestSeek ()
605 string path
= TempFolder
+ Path
.DirectorySeparatorChar
+ "TestSeek";
608 FileStream stream
= new FileStream (path
, FileMode
.CreateNew
, FileAccess
.ReadWrite
, FileShare
.ReadWrite
);
609 stream
.Write (new byte[] {1, 2, 3, 4, 5, 6, 7, 8 , 9, 10}
, 0, 10);
611 stream
.Seek (5, SeekOrigin
.End
);
612 AssertEquals ("test#01", -1, stream
.ReadByte ());
614 stream
.Seek (-5, SeekOrigin
.End
);
615 AssertEquals ("test#02", 6, stream
.ReadByte ());
618 stream
.Seek (-11, SeekOrigin
.End
);
620 } catch (Exception e
) {
621 AssertEquals ("test#03", typeof (IOException
), e
.GetType ());
624 stream
.Seek (19, SeekOrigin
.Begin
);
625 AssertEquals ("test#04", -1, stream
.ReadByte ());
627 stream
.Seek (1, SeekOrigin
.Begin
);
628 AssertEquals ("test#05", 2, stream
.ReadByte ());
630 stream
.Seek (3, SeekOrigin
.Current
);
631 AssertEquals ("test#06", 6, stream
.ReadByte ());
633 stream
.Seek (-2, SeekOrigin
.Current
);
634 AssertEquals ("test#07", 5, stream
.ReadByte ());
638 // Test that seeks work correctly when seeking inside the buffer
639 stream
.Seek (0, SeekOrigin
.Begin
);
640 stream
.WriteByte (0);
641 stream
.WriteByte (1);
642 stream
.Seek (0, SeekOrigin
.Begin
);
643 byte[] buf
= new byte [1];
645 stream
.Write (buf
, 0, 1);
646 stream
.Write (buf
, 0, 1);
648 stream
.Seek (0, SeekOrigin
.Begin
);
649 AssertEquals ("test#08", 2, stream
.ReadByte ());
650 AssertEquals ("test#09", 2, stream
.ReadByte ());
657 public void TestClose ()
659 string path
= TempFolder
+ Path
.DirectorySeparatorChar
+ "TestClose";
662 FileStream stream
= new FileStream (path
, FileMode
.CreateNew
, FileAccess
.ReadWrite
);
664 stream
.Write (new byte [] {1, 2, 3, 4}
, 0, 4);
671 } catch (Exception e
) {
672 AssertEquals ("test#01", typeof (ObjectDisposedException
), e
.GetType ());
676 stream
.WriteByte (64);
678 } catch (Exception e
) {
679 AssertEquals ("test#02", typeof (ObjectDisposedException
), e
.GetType ());
685 } catch (Exception e
) {
686 AssertEquals ("test#03", typeof (ObjectDisposedException
), e
.GetType ());
690 long l
= stream
.Length
;
692 } catch (Exception e
) {
693 AssertEquals ("test#04", typeof (ObjectDisposedException
), e
.GetType ());
697 long l
= stream
.Position
;
699 } catch (Exception e
) {
700 AssertEquals ("test#05", typeof (ObjectDisposedException
), e
.GetType ());
703 AssertEquals ("test#06", false, stream
.CanRead
);
704 AssertEquals ("test#07", false, stream
.CanSeek
);
705 AssertEquals ("test#08", false, stream
.CanWrite
);
706 AssertEquals ("test#09", true, stream
.Name
.EndsWith (path
));
713 /// Checks whether the <see cref="FileStream" /> throws a <see cref="NotSupportedException" />
714 /// when the stream is opened with access mode <see cref="FileAccess.Read" /> and the
715 /// <see cref="FileStream.Write(byte[], int, int)" /> method is called.
718 [ExpectedException (typeof(NotSupportedException
))]
719 public void TestWriteVerifyAccessMode ()
721 string path
= TempFolder
+ Path
.DirectorySeparatorChar
+ "temp";
724 FileStream stream
= null;
728 buffer
= Encoding
.ASCII
.GetBytes ("test");
729 stream
= new FileStream (path
, FileMode
.OpenOrCreate
, FileAccess
.Read
);
730 stream
.Write (buffer
, 0, buffer
.Length
);
739 /// Checks whether the <see cref="FileStream" /> throws a <see cref="NotSupportedException" />
740 /// when the stream is opened with access mode <see cref="FileAccess.Read" /> and the
741 /// <see cref="FileStream.WriteByte(byte)" /> method is called.
744 [ExpectedException (typeof (NotSupportedException
))]
745 public void TestWriteByteVerifyAccessMode ()
747 string path
= TempFolder
+ Path
.DirectorySeparatorChar
+ "temp";
750 FileStream stream
= null;
753 stream
= new FileStream (path
, FileMode
.OpenOrCreate
, FileAccess
.Read
);
754 stream
.WriteByte (Byte
.MinValue
);
763 /// Checks whether the <see cref="FileStream" /> throws a <see cref="NotSupportedException" />
764 /// when the stream is opened with access mode <see cref="FileAccess.Write" /> and the
765 /// <see cref="FileStream.Read(byte[], int, int)" /> method is called.
768 [ExpectedException (typeof (NotSupportedException
))]
769 public void TestReadVerifyAccessMode ()
771 string path
= TempFolder
+ Path
.DirectorySeparatorChar
+ "temp";
774 FileStream stream
= null;
775 byte[] buffer
= new byte [100];
778 stream
= new FileStream (path
, FileMode
.OpenOrCreate
, FileAccess
.Write
);
779 stream
.Read (buffer
, 0, buffer
.Length
);
787 /// Checks whether the <see cref="FileStream" /> throws a <see cref="NotSupportedException" />
788 /// when the stream is opened with access mode <see cref="FileAccess.Write" /> and the
789 /// <see cref="FileStream.ReadByte()" /> method is called.
792 [ExpectedException (typeof (NotSupportedException
))]
793 public void TestReadByteVerifyAccessMode ()
795 string path
= TempFolder
+ Path
.DirectorySeparatorChar
+ "temp";
798 FileStream stream
= null;
801 stream
= new FileStream (path
, FileMode
.OpenOrCreate
, FileAccess
.Write
);
802 int readByte
= stream
.ReadByte ();
810 // Check that the stream is flushed even when it doesn't own the
813 public void TestFlushNotOwningHandle ()
815 string path
= Path
.Combine (TempFolder
, "TestFlushNotOwningHandle");
818 FileStream s
= new FileStream (path
, FileMode
.Create
);
819 using (FileStream s2
= new FileStream (s
.Handle
, FileAccess
.Write
, false)) {
820 byte[] buf
= new byte [2];
822 s2
.Write (buf
, 0, 1);
826 AssertEquals ((int)'1', s
.ReadByte ());
830 private void DeleteFile (string path
)
832 if (File
.Exists (path
))
837 [ExpectedException (typeof (ArgumentOutOfRangeException
))]
838 public void Read_OffsetNegative ()
840 string path
= TempFolder
+ Path
.DirectorySeparatorChar
+ "temp";
843 using (FileStream stream
= new FileStream (path
, FileMode
.OpenOrCreate
, FileAccess
.Read
)) {
844 stream
.Read (new byte[0], -1, 1);
849 [ExpectedException (typeof (ArgumentException
))]
850 public void Read_OffsetOverflow ()
852 string path
= TempFolder
+ Path
.DirectorySeparatorChar
+ "temp";
855 using (FileStream stream
= new FileStream (path
, FileMode
.OpenOrCreate
, FileAccess
.Read
)) {
856 stream
.Read (new byte[0], Int32
.MaxValue
, 1);
861 [ExpectedException (typeof (ArgumentOutOfRangeException
))]
862 public void Read_CountNegative ()
864 string path
= TempFolder
+ Path
.DirectorySeparatorChar
+ "temp";
867 using (FileStream stream
= new FileStream (path
, FileMode
.OpenOrCreate
, FileAccess
.Read
)) {
868 stream
.Read (new byte[0], 1, -1);
873 [ExpectedException (typeof (ArgumentException
))]
874 public void Read_CountOverflow ()
876 string path
= TempFolder
+ Path
.DirectorySeparatorChar
+ "temp";
879 using (FileStream stream
= new FileStream (path
, FileMode
.OpenOrCreate
, FileAccess
.Read
)) {
880 stream
.Read (new byte[0], 1, Int32
.MaxValue
);
885 [ExpectedException (typeof (ArgumentOutOfRangeException
))]
886 public void Write_OffsetNegative ()
888 string path
= TempFolder
+ Path
.DirectorySeparatorChar
+ "temp";
891 using (FileStream stream
= new FileStream (path
, FileMode
.OpenOrCreate
, FileAccess
.Write
)) {
892 stream
.Write (new byte[0], -1, 1);
897 [ExpectedException (typeof (ArgumentException
))]
898 public void Write_OffsetOverflow ()
900 string path
= TempFolder
+ Path
.DirectorySeparatorChar
+ "temp";
903 using (FileStream stream
= new FileStream (path
, FileMode
.OpenOrCreate
, FileAccess
.Write
)) {
904 stream
.Write (new byte[0], Int32
.MaxValue
, 1);
909 [ExpectedException (typeof (ArgumentOutOfRangeException
))]
910 public void Write_CountNegative ()
912 string path
= TempFolder
+ Path
.DirectorySeparatorChar
+ "temp";
915 using (FileStream stream
= new FileStream (path
, FileMode
.OpenOrCreate
, FileAccess
.Write
)) {
916 stream
.Write (new byte[0], 1, -1);
921 [ExpectedException (typeof (ArgumentException
))]
922 public void Write_CountOverflow ()
924 string path
= TempFolder
+ Path
.DirectorySeparatorChar
+ "temp";
927 using (FileStream stream
= new FileStream (path
, FileMode
.OpenOrCreate
, FileAccess
.Write
)) {
928 stream
.Write (new byte[0], 1, Int32
.MaxValue
);
933 [ExpectedException (typeof (ArgumentException
))]
934 public void Seek_InvalidSeekOrigin ()
936 string path
= TempFolder
+ Path
.DirectorySeparatorChar
+ "temp";
939 using (FileStream stream
= new FileStream (path
, FileMode
.OpenOrCreate
, FileAccess
.Read
)) {
940 stream
.Seek (0, (SeekOrigin
) (-1));
945 [ExpectedException (typeof (ArgumentException
))]
946 public void Constructor_InvalidFileHandle ()
948 new FileStream ((IntPtr
)(-1), FileAccess
.Read
);
952 public void PositionAfterSetLength ()
954 string path
= TempFolder
+ Path
.DirectorySeparatorChar
+ "temp";
957 using (FileStream stream
= new FileStream (path
, FileMode
.OpenOrCreate
, FileAccess
.Write
)) {
958 stream
.SetLength (32);
959 stream
.Position
= 32;
960 stream
.SetLength (16);
961 AssertEquals ("Position==16", 16, stream
.Position
);
966 [ExpectedException (typeof (ObjectDisposedException
))]
967 public void SetLength_Disposed ()
969 string path
= TempFolder
+ Path
.DirectorySeparatorChar
+ "temp";
971 FileStream stream
= new FileStream (path
, FileMode
.OpenOrCreate
, FileAccess
.Write
);
973 stream
.SetLength (16);
977 [ExpectedException (typeof (ObjectDisposedException
))]
978 public void Position_Disposed ()
980 string path
= TempFolder
+ Path
.DirectorySeparatorChar
+ "temp";
982 FileStream stream
= new FileStream (path
, FileMode
.OpenOrCreate
, FileAccess
.Read
);
988 [ExpectedException (typeof (ObjectDisposedException
))]
989 public void BeginRead_Disposed ()
991 string path
= TempFolder
+ Path
.DirectorySeparatorChar
+ "temp";
993 FileStream stream
= new FileStream (path
, FileMode
.OpenOrCreate
, FileAccess
.Read
);
995 stream
.EndRead (stream
.BeginRead (new byte[8], 0, 8, null, null));
999 [ExpectedException (typeof (ObjectDisposedException
))]
1000 public void BeginWrite_Disposed ()
1002 string path
= TempFolder
+ Path
.DirectorySeparatorChar
+ "temp";
1004 FileStream stream
= new FileStream (path
, FileMode
.OpenOrCreate
, FileAccess
.Write
);
1006 stream
.EndWrite (stream
.BeginWrite (new byte[8], 0, 8, null, null));
1010 [ExpectedException (typeof (ObjectDisposedException
))]
1011 public void Lock_Disposed ()
1013 string path
= TempFolder
+ Path
.DirectorySeparatorChar
+ "temp";
1015 FileStream stream
= new FileStream (path
, FileMode
.OpenOrCreate
, FileAccess
.Write
);
1021 [ExpectedException (typeof (ObjectDisposedException
))]
1022 public void Unlock_Disposed ()
1024 string path
= TempFolder
+ Path
.DirectorySeparatorChar
+ "temp";
1026 FileStream stream
= new FileStream (path
, FileMode
.OpenOrCreate
, FileAccess
.Write
);
1028 stream
.Unlock (0,1);
1032 public void ReadBytePastEndOfStream ()
1034 string path
= TempFolder
+ Path
.DirectorySeparatorChar
+ "temp";
1036 using (FileStream stream
= new FileStream (path
, FileMode
.OpenOrCreate
, FileAccess
.Read
)) {
1037 stream
.Seek (0, SeekOrigin
.End
);
1038 AssertEquals ("ReadByte", -1, stream
.ReadByte ());
1044 [ExpectedException (typeof (NotSupportedException
))]
1045 public void SetLengthWithClosedBaseStream ()
1047 FileStream fs
= new FileStream ("temp", FileMode
.Create
);
1048 BufferedStream bs
= new BufferedStream (fs
);
1051 bs
.SetLength (1000);