(DISTFILES): Comment out a few missing files.
[mono-project.git] / mcs / class / corlib / Test / System.IO / FileStreamTest.cs
blobfd93ed592567c9bd791648b0e1f3c6e341a64cd8
1 // FileStreamTests.cs - NUnit2 Test Cases for System.IO.FileStream class
2 //
3 // Authors:
4 // Ville Palo (vi64pa@koti.soon.fi)
5 // Gert Driesen (gert.driesen@ardatis.com)
6 // Gonzalo Paniagua Javier (gonzalo@ximian.com)
7 //
8 // (C) Ville Palo
9 // (c) 2003 Ximian, Inc. (http://www.ximian.com)
10 //
13 using NUnit.Framework;
14 using System;
15 using System.IO;
16 using System.Text;
18 namespace MonoTests.System.IO
20 [TestFixture]
21 public class FileStreamTest : Assertion
23 string TempFolder = Path.Combine (Path.GetTempPath (), "MonoTests.System.IO.Tests");
24 static readonly char DSC = Path.DirectorySeparatorChar;
26 [TearDown]
27 public void TearDown()
29 if (Directory.Exists (TempFolder))
30 Directory.Delete (TempFolder, true);
33 [SetUp]
34 public void SetUp ()
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";
45 DeleteFile (path);
46 FileStream stream = null;
47 try {
48 stream = new FileStream (path, FileMode.Create);
49 } finally {
51 if (stream != null)
52 stream.Close ();
53 DeleteFile (path);
57 [Test]
58 [ExpectedException (typeof (ArgumentException))]
59 public void CtorArgumentException1 ()
61 FileStream stream;
62 stream = new FileStream ("", FileMode.Create);
63 stream.Close ();
66 [Test]
67 [ExpectedException (typeof (ArgumentNullException))]
68 public void CtorArgumentNullException ()
70 FileStream stream = new FileStream (null, FileMode.Create);
71 stream.Close ();
74 [Test]
75 [ExpectedException (typeof (FileNotFoundException))]
76 public void CtorFileNotFoundException1 ()
78 string path = TempFolder + DSC + "thisfileshouldnotexists.test";
79 DeleteFile (path);
80 FileStream stream = null;
81 try {
82 stream = new FileStream (TempFolder + DSC + "thisfileshouldnotexists.test", FileMode.Open);
83 } finally {
84 if (stream != null)
85 stream.Close ();
86 DeleteFile (path);
90 [Test]
91 [ExpectedException (typeof (FileNotFoundException))]
92 public void CtorFileNotFoundException2 ()
94 string path = TempFolder + DSC + "thisfileshouldNOTexists.test";
95 DeleteFile (path);
96 FileStream stream = null;
98 try {
99 stream = new FileStream (TempFolder + DSC + "thisfileshouldNOTexists.test", FileMode.Truncate);
100 } finally {
101 if (stream != null)
102 stream.Close ();
104 DeleteFile (path);
108 [Test]
109 [ExpectedException (typeof (IOException))]
110 public void CtorIOException1 ()
112 string path = TempFolder + DSC + "thisfileshouldexists.test";
113 FileStream stream = null;
114 DeleteFile (path);
115 try {
116 stream = new FileStream (path, FileMode.CreateNew);
117 stream.Close ();
118 stream = null;
119 stream = new FileStream (path, FileMode.CreateNew);
120 } finally {
122 if (stream != null)
123 stream.Close ();
124 DeleteFile (path);
129 [Test]
130 [ExpectedException (typeof (ArgumentOutOfRangeException))]
131 public void CtorArgumentOutOfRangeException1 ()
133 FileStream stream = null;
134 string path = TempFolder + Path.DirectorySeparatorChar + "temp";
135 DeleteFile (path);
136 try {
137 stream = new FileStream (path, FileMode.Append | FileMode.CreateNew);
138 } finally {
139 if (stream != null)
140 stream.Close ();
141 DeleteFile (path);
145 [Test]
146 [ExpectedException (typeof (ArgumentOutOfRangeException))]
147 public void CtorArgumentOutOfRangeException2 ()
149 FileStream stream = null;
150 string path = TempFolder + Path.DirectorySeparatorChar + "temp";
151 DeleteFile (path);
152 try {
153 stream = new FileStream ("test.test.test", FileMode.Append | FileMode.Open);
154 } finally {
155 if (stream != null)
156 stream.Close ();
157 DeleteFile (path);
161 [Test]
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;
170 try {
171 stream = new FileStream (path + DSC + "eitherthisfile.test", FileMode.CreateNew);
172 } finally {
174 if (stream != null)
175 stream.Close ();
177 if (Directory.Exists (path))
178 Directory.Delete (path, true);
182 [Test]
183 [ExpectedException (typeof (ArgumentOutOfRangeException))]
184 public void CtorArgumentOutOfRangeException3 ()
186 string path = TempFolder + DSC + "CtorArgumentOutOfRangeException1";
187 DeleteFile (path);
189 FileStream stream = null;
190 try {
191 stream = new FileStream (path, FileMode.CreateNew, FileAccess.Read, FileShare.None | FileShare.Inheritable);
192 } finally {
193 if (stream != null)
194 stream.Close ();
195 DeleteFile (path);
199 [Test]
200 [ExpectedException (typeof (ArgumentOutOfRangeException))]
201 public void CtorArgumentOutOfRangeException4 ()
203 string path = TempFolder + DSC + "CtorArgumentOutOfRangeException4";
204 DeleteFile (path);
206 FileStream stream = null;
207 try {
208 stream = new FileStream (path, FileMode.OpenOrCreate, FileAccess.Read, FileShare.ReadWrite, -1);
209 } finally {
210 if (stream != null)
211 stream.Close ();
212 DeleteFile (path);
216 [Test]
217 [ExpectedException (typeof (ArgumentOutOfRangeException))]
218 public void CtorBufferSizeZero ()
220 // Buffer size can't be zero
222 string path = Path.Combine (TempFolder, "CtorBufferSizeZero");
223 DeleteFile (path);
225 FileStream stream = null;
226 try {
227 stream = new FileStream (path, FileMode.CreateNew, FileAccess.Write, FileShare.ReadWrite, 0);
228 } finally {
229 if (stream != null)
230 stream.Close ();
231 DeleteFile (path);
235 [Test]
236 [ExpectedException (typeof (ArgumentException))]
237 public void CtorArgumentException2 ()
239 // FileMode.CreateNew && FileAccess.Read
241 string path = TempFolder + Path.DirectorySeparatorChar + "temp";
242 FileStream stream = null;
244 DeleteFile (path);
246 try {
247 stream = new FileStream (".test.test.test.2", FileMode.CreateNew, FileAccess.Read, FileShare.None | FileShare.Write);
248 } finally {
250 if (stream != null)
251 stream.Close ();
252 DeleteFile (path);
257 [Test]
258 [ExpectedException (typeof (ArgumentOutOfRangeException))]
259 public void CtorArgumentOutOfRangeException5 ()
261 string path = TempFolder + Path.DirectorySeparatorChar + "temp";
262 DeleteFile (path);
264 FileStream stream = null;
265 try {
266 stream = new FileStream (path, FileMode.CreateNew, FileAccess.Read, FileShare.Inheritable | FileShare.ReadWrite);
267 } finally {
268 if (stream != null)
269 stream.Close ();
270 DeleteFile (path);
275 [Test]
276 [ExpectedException (typeof (ArgumentException))]
277 public void CtorArgumentException3 ()
279 string path = TempFolder + Path.DirectorySeparatorChar + "temp";
280 FileStream stream = null;
282 DeleteFile (path);
284 try {
285 stream = new FileStream (".test.test.test.2", FileMode.Truncate, FileAccess.Read);
286 } finally {
287 if (stream != null)
288 stream.Close ();
290 DeleteFile (path);
294 [Test, ExpectedException (typeof (IOException))]
295 public void CtorIOException2 ()
297 FileStream stream = null;
298 try {
299 stream = new FileStream (new IntPtr (Int32.MaxValue), FileAccess.Read);
300 } finally {
301 if (stream != null)
302 stream.Close ();
306 [Test, ExpectedException(typeof(IOException))]
307 public void CtorIOException ()
309 string path = TempFolder + DSC + "CTorIOException.Test";
310 FileStream stream = null;
311 FileStream stream2 = null;
312 DeleteFile (path);
314 try {
315 stream = new FileStream (path, FileMode.CreateNew);
317 // used by an another process
318 stream2 = new FileStream (path, FileMode.OpenOrCreate);
319 } finally {
320 if (stream != null)
321 stream.Close ();
322 if (stream2 != null)
323 stream2.Close ();
324 DeleteFile (path);
328 [Test]
329 public void CtorAccess1Read2Read ()
331 FileStream fs = null;
332 FileStream fs2 = null;
333 try {
334 if (!File.Exists ("temp")) {
335 TextWriter tw = File.CreateText ("temp");
336 tw.Write ("FOO");
337 tw.Close ();
339 fs = new FileStream ("temp", FileMode.Open, FileAccess.Read);
340 fs2 = new FileStream ("temp", FileMode.Open, FileAccess.Read);
341 } finally {
342 if (fs != null)
343 fs.Close ();
344 if (fs2 != null)
345 fs2.Close ();
346 if (File.Exists ("temp"))
347 File.Delete ("temp");
351 [Test]
352 [ExpectedException (typeof (IOException))]
353 public void CtorAccess1Read2Write ()
355 FileStream fs = null;
356 try {
357 if (!File.Exists ("temp")) {
358 using (TextWriter tw = File.CreateText ("temp")) {
359 tw.Write ("FOO");
362 fs = new FileStream ("temp", FileMode.Open, FileAccess.Read);
363 fs = new FileStream ("temp", FileMode.Create, FileAccess.Write);
364 } finally {
365 if (fs != null)
366 fs.Close ();
367 if (File.Exists ("temp"))
368 File.Delete ("temp");
372 [Test]
373 [ExpectedException (typeof (IOException))]
374 public void CtorAccess1Write2Write ()
376 FileStream fs = null;
377 try {
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);
382 } finally {
383 if (fs != null)
384 fs.Close ();
385 if (File.Exists ("temp"))
386 File.Delete ("temp");
390 [Test]
391 public void Write ()
393 string path = TempFolder + DSC + "FileStreamTest.Write";
395 DeleteFile (path);
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]);
422 stream.Close ();
425 [Test]
426 public void Length ()
428 // Test that the Length property takes into account the data
429 // in the buffer
430 string path = TempFolder + DSC + "FileStreamTest.Length";
432 DeleteFile (path);
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);
440 stream.Close ();
443 [Test]
444 public void Flush ()
446 string path = TempFolder + DSC + "FileStreamTest.Flush";
447 FileStream stream = null;
448 FileStream stream2 = null;
450 DeleteFile (path);
452 try {
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]);
466 stream.Flush ();
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]);
472 } finally {
473 if (stream != null)
474 stream.Close ();
475 if (stream2 != null)
476 stream2.Close ();
478 DeleteFile (path);
482 public void TestDefaultProperties ()
484 string path = TempFolder + Path.DirectorySeparatorChar + "testfilestream.tmp.2";
485 DeleteFile (path);
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());
496 stream.Close ();
497 DeleteFile (path);
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());
507 stream.Close ();
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());
517 stream.Close ();
518 DeleteFile (path);
521 public void TestLock()
523 string path = TempFolder + Path.DirectorySeparatorChar + "TestLock";
524 DeleteFile (path);
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);
529 stream.Close ();
531 stream = new FileStream (path, FileMode.Open, FileAccess.ReadWrite);
533 stream.Lock (0, 5);
535 FileStream stream2 = new FileStream (path , FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
537 byte [] bytes = new byte [5];
538 try {
539 stream2.Read (bytes, 0, 5);
540 Fail ();
541 } catch (Exception e) {
543 // locked
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]);
556 stream.Unlock (0,5);
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]);
566 stream.Close ();
567 stream2.Close ();
569 DeleteFile (path);
572 [Test]
573 public void Seek ()
575 string path = TempFolder + DSC + "FST.Seek.Test";
576 DeleteFile (path);
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 ());
597 stream.Close ();
598 stream2.Close ();
600 DeleteFile (path);
603 public void TestSeek ()
605 string path = TempFolder + Path.DirectorySeparatorChar + "TestSeek";
606 DeleteFile (path);
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 ());
617 try {
618 stream.Seek (-11, SeekOrigin.End);
619 Fail ();
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 ());
636 stream.Flush ();
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];
644 buf [0] = 2;
645 stream.Write (buf, 0, 1);
646 stream.Write (buf, 0, 1);
647 stream.Flush ();
648 stream.Seek (0, SeekOrigin.Begin);
649 AssertEquals ("test#08", 2, stream.ReadByte ());
650 AssertEquals ("test#09", 2, stream.ReadByte ());
652 stream.Close ();
654 DeleteFile (path);
657 public void TestClose ()
659 string path = TempFolder + Path.DirectorySeparatorChar + "TestClose";
660 DeleteFile (path);
662 FileStream stream = new FileStream (path, FileMode.CreateNew, FileAccess.ReadWrite);
664 stream.Write (new byte [] {1, 2, 3, 4}, 0, 4);
665 stream.ReadByte ();
666 stream.Close ();
668 try {
669 stream.ReadByte ();
670 Fail ();
671 } catch (Exception e) {
672 AssertEquals ("test#01", typeof (ObjectDisposedException), e.GetType ());
675 try {
676 stream.WriteByte (64);
677 Fail ();
678 } catch (Exception e) {
679 AssertEquals ("test#02", typeof (ObjectDisposedException), e.GetType ());
682 try {
683 stream.Flush ();
684 Fail ();
685 } catch (Exception e) {
686 AssertEquals ("test#03", typeof (ObjectDisposedException), e.GetType ());
689 try {
690 long l = stream.Length;
691 Fail ();
692 } catch (Exception e) {
693 AssertEquals ("test#04", typeof (ObjectDisposedException), e.GetType ());
696 try {
697 long l = stream.Position;
698 Fail ();
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));
708 DeleteFile (path);
712 /// <summary>
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.
716 /// </summary>
717 [Test]
718 [ExpectedException (typeof(NotSupportedException))]
719 public void TestWriteVerifyAccessMode ()
721 string path = TempFolder + Path.DirectorySeparatorChar + "temp";
722 DeleteFile (path);
724 FileStream stream = null;
725 byte[] buffer;
727 try {
728 buffer = Encoding.ASCII.GetBytes ("test");
729 stream = new FileStream (path, FileMode.OpenOrCreate, FileAccess.Read);
730 stream.Write (buffer, 0, buffer.Length);
731 } finally {
732 if (stream != null)
733 stream.Close();
734 DeleteFile (path);
738 /// <summary>
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.
742 /// </summary>
743 [Test]
744 [ExpectedException (typeof (NotSupportedException))]
745 public void TestWriteByteVerifyAccessMode ()
747 string path = TempFolder + Path.DirectorySeparatorChar + "temp";
748 DeleteFile (path);
750 FileStream stream = null;
752 try {
753 stream = new FileStream (path, FileMode.OpenOrCreate, FileAccess.Read);
754 stream.WriteByte (Byte.MinValue);
755 } finally {
756 if (stream != null)
757 stream.Close ();
758 DeleteFile (path);
762 /// <summary>
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.
766 /// </summary>
767 [Test]
768 [ExpectedException (typeof (NotSupportedException))]
769 public void TestReadVerifyAccessMode ()
771 string path = TempFolder + Path.DirectorySeparatorChar + "temp";
772 DeleteFile (path);
774 FileStream stream = null;
775 byte[] buffer = new byte [100];
777 try {
778 stream = new FileStream (path, FileMode.OpenOrCreate, FileAccess.Write);
779 stream.Read (buffer, 0, buffer.Length);
780 } finally {
781 if (stream != null)
782 stream.Close ();
786 /// <summary>
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.
790 /// </summary>
791 [Test]
792 [ExpectedException (typeof (NotSupportedException))]
793 public void TestReadByteVerifyAccessMode ()
795 string path = TempFolder + Path.DirectorySeparatorChar + "temp";
796 DeleteFile (path);
798 FileStream stream = null;
800 try {
801 stream = new FileStream (path, FileMode.OpenOrCreate, FileAccess.Write);
802 int readByte = stream.ReadByte ();
803 } finally {
804 if (stream != null)
805 stream.Close();
806 DeleteFile (path);
810 // Check that the stream is flushed even when it doesn't own the
811 // handle
812 [Test]
813 public void TestFlushNotOwningHandle ()
815 string path = Path.Combine (TempFolder, "TestFlushNotOwningHandle");
816 DeleteFile (path);
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];
821 buf [0] = (int)'1';
822 s2.Write (buf, 0, 1);
825 s.Position = 0;
826 AssertEquals ((int)'1', s.ReadByte ());
827 s.Close ();
830 private void DeleteFile (string path)
832 if (File.Exists (path))
833 File.Delete (path);
836 [Test]
837 [ExpectedException (typeof (ArgumentOutOfRangeException))]
838 public void Read_OffsetNegative ()
840 string path = TempFolder + Path.DirectorySeparatorChar + "temp";
841 DeleteFile (path);
843 using (FileStream stream = new FileStream (path, FileMode.OpenOrCreate, FileAccess.Read)) {
844 stream.Read (new byte[0], -1, 1);
848 [Test]
849 [ExpectedException (typeof (ArgumentException))]
850 public void Read_OffsetOverflow ()
852 string path = TempFolder + Path.DirectorySeparatorChar + "temp";
853 DeleteFile (path);
855 using (FileStream stream = new FileStream (path, FileMode.OpenOrCreate, FileAccess.Read)) {
856 stream.Read (new byte[0], Int32.MaxValue, 1);
860 [Test]
861 [ExpectedException (typeof (ArgumentOutOfRangeException))]
862 public void Read_CountNegative ()
864 string path = TempFolder + Path.DirectorySeparatorChar + "temp";
865 DeleteFile (path);
867 using (FileStream stream = new FileStream (path, FileMode.OpenOrCreate, FileAccess.Read)) {
868 stream.Read (new byte[0], 1, -1);
872 [Test]
873 [ExpectedException (typeof (ArgumentException))]
874 public void Read_CountOverflow ()
876 string path = TempFolder + Path.DirectorySeparatorChar + "temp";
877 DeleteFile (path);
879 using (FileStream stream = new FileStream (path, FileMode.OpenOrCreate, FileAccess.Read)) {
880 stream.Read (new byte[0], 1, Int32.MaxValue);
884 [Test]
885 [ExpectedException (typeof (ArgumentOutOfRangeException))]
886 public void Write_OffsetNegative ()
888 string path = TempFolder + Path.DirectorySeparatorChar + "temp";
889 DeleteFile (path);
891 using (FileStream stream = new FileStream (path, FileMode.OpenOrCreate, FileAccess.Write)) {
892 stream.Write (new byte[0], -1, 1);
896 [Test]
897 [ExpectedException (typeof (ArgumentException))]
898 public void Write_OffsetOverflow ()
900 string path = TempFolder + Path.DirectorySeparatorChar + "temp";
901 DeleteFile (path);
903 using (FileStream stream = new FileStream (path, FileMode.OpenOrCreate, FileAccess.Write)) {
904 stream.Write (new byte[0], Int32.MaxValue, 1);
908 [Test]
909 [ExpectedException (typeof (ArgumentOutOfRangeException))]
910 public void Write_CountNegative ()
912 string path = TempFolder + Path.DirectorySeparatorChar + "temp";
913 DeleteFile (path);
915 using (FileStream stream = new FileStream (path, FileMode.OpenOrCreate, FileAccess.Write)) {
916 stream.Write (new byte[0], 1, -1);
920 [Test]
921 [ExpectedException (typeof (ArgumentException))]
922 public void Write_CountOverflow ()
924 string path = TempFolder + Path.DirectorySeparatorChar + "temp";
925 DeleteFile (path);
927 using (FileStream stream = new FileStream (path, FileMode.OpenOrCreate, FileAccess.Write)) {
928 stream.Write (new byte[0], 1, Int32.MaxValue);
932 [Test]
933 [ExpectedException (typeof (ArgumentException))]
934 public void Seek_InvalidSeekOrigin ()
936 string path = TempFolder + Path.DirectorySeparatorChar + "temp";
937 DeleteFile (path);
939 using (FileStream stream = new FileStream (path, FileMode.OpenOrCreate, FileAccess.Read)) {
940 stream.Seek (0, (SeekOrigin) (-1));
944 [Test]
945 [ExpectedException (typeof (ArgumentException))]
946 public void Constructor_InvalidFileHandle ()
948 new FileStream ((IntPtr)(-1), FileAccess.Read);
951 [Test]
952 public void PositionAfterSetLength ()
954 string path = TempFolder + Path.DirectorySeparatorChar + "temp";
955 DeleteFile (path);
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);
965 [Test]
966 [ExpectedException (typeof (ObjectDisposedException))]
967 public void SetLength_Disposed ()
969 string path = TempFolder + Path.DirectorySeparatorChar + "temp";
970 DeleteFile (path);
971 FileStream stream = new FileStream (path, FileMode.OpenOrCreate, FileAccess.Write);
972 stream.Close ();
973 stream.SetLength (16);
976 [Test]
977 [ExpectedException (typeof (ObjectDisposedException))]
978 public void Position_Disposed ()
980 string path = TempFolder + Path.DirectorySeparatorChar + "temp";
981 DeleteFile (path);
982 FileStream stream = new FileStream (path, FileMode.OpenOrCreate, FileAccess.Read);
983 stream.Close ();
984 stream.Position = 0;
987 [Test]
988 [ExpectedException (typeof (ObjectDisposedException))]
989 public void BeginRead_Disposed ()
991 string path = TempFolder + Path.DirectorySeparatorChar + "temp";
992 DeleteFile (path);
993 FileStream stream = new FileStream (path, FileMode.OpenOrCreate, FileAccess.Read);
994 stream.Close ();
995 stream.EndRead (stream.BeginRead (new byte[8], 0, 8, null, null));
998 [Test]
999 [ExpectedException (typeof (ObjectDisposedException))]
1000 public void BeginWrite_Disposed ()
1002 string path = TempFolder + Path.DirectorySeparatorChar + "temp";
1003 DeleteFile (path);
1004 FileStream stream = new FileStream (path, FileMode.OpenOrCreate, FileAccess.Write);
1005 stream.Close ();
1006 stream.EndWrite (stream.BeginWrite (new byte[8], 0, 8, null, null));
1009 [Test]
1010 [ExpectedException (typeof (ObjectDisposedException))]
1011 public void Lock_Disposed ()
1013 string path = TempFolder + Path.DirectorySeparatorChar + "temp";
1014 DeleteFile (path);
1015 FileStream stream = new FileStream (path, FileMode.OpenOrCreate, FileAccess.Write);
1016 stream.Close ();
1017 stream.Lock (0,1);
1020 [Test]
1021 [ExpectedException (typeof (ObjectDisposedException))]
1022 public void Unlock_Disposed ()
1024 string path = TempFolder + Path.DirectorySeparatorChar + "temp";
1025 DeleteFile (path);
1026 FileStream stream = new FileStream (path, FileMode.OpenOrCreate, FileAccess.Write);
1027 stream.Close ();
1028 stream.Unlock (0,1);
1031 [Test]
1032 public void ReadBytePastEndOfStream ()
1034 string path = TempFolder + Path.DirectorySeparatorChar + "temp";
1035 DeleteFile (path);
1036 using (FileStream stream = new FileStream (path, FileMode.OpenOrCreate, FileAccess.Read)) {
1037 stream.Seek (0, SeekOrigin.End);
1038 AssertEquals ("ReadByte", -1, stream.ReadByte ());
1039 stream.Close ();
1043 [Test]
1044 [ExpectedException (typeof (NotSupportedException))]
1045 public void SetLengthWithClosedBaseStream ()
1047 FileStream fs = new FileStream ("temp", FileMode.Create);
1048 BufferedStream bs = new BufferedStream (fs);
1049 fs.Close ();
1051 bs.SetLength (1000);