avoid writing stuff to the assembly dir. use the temp folder
[mono-project.git] / mcs / class / corlib / Test / System.IO / FileStreamTest.cs
blob58802f35b98acf5e90f44ca27ae99e62605cd154
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 string fn = Path.Combine (TempFolder, "temp");
334 try {
335 if (!File.Exists (fn)) {
336 TextWriter tw = File.CreateText (fn);
337 tw.Write ("FOO");
338 tw.Close ();
340 fs = new FileStream (fn, FileMode.Open, FileAccess.Read);
341 fs2 = new FileStream (fn, FileMode.Open, FileAccess.Read);
342 } finally {
343 if (fs != null)
344 fs.Close ();
345 if (fs2 != null)
346 fs2.Close ();
347 if (File.Exists (fn))
348 File.Delete (fn);
352 [Test]
353 [ExpectedException (typeof (IOException))]
354 public void CtorAccess1Read2Write ()
356 string fn = Path.Combine (TempFolder, "temp");
357 FileStream fs = null;
358 try {
359 if (!File.Exists (fn)) {
360 using (TextWriter tw = File.CreateText (fn)) {
361 tw.Write ("FOO");
364 fs = new FileStream (fn, FileMode.Open, FileAccess.Read);
365 fs = new FileStream (fn, FileMode.Create, FileAccess.Write);
366 } finally {
367 if (fs != null)
368 fs.Close ();
369 if (File.Exists (fn))
370 File.Delete (fn);
374 [Test]
375 [ExpectedException (typeof (IOException))]
376 public void CtorAccess1Write2Write ()
378 string fn = Path.Combine (TempFolder, "temp");
379 FileStream fs = null;
380 try {
381 if (File.Exists (fn))
382 File.Delete (fn);
383 fs = new FileStream (fn, FileMode.Create, FileAccess.Write);
384 fs = new FileStream (fn, FileMode.Create, FileAccess.Write);
385 } finally {
386 if (fs != null)
387 fs.Close ();
388 if (File.Exists (fn))
389 File.Delete (fn);
393 [Test]
394 public void Write ()
396 string path = TempFolder + DSC + "FileStreamTest.Write";
398 DeleteFile (path);
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]);
425 stream.Close ();
428 [Test]
429 public void Length ()
431 // Test that the Length property takes into account the data
432 // in the buffer
433 string path = TempFolder + DSC + "FileStreamTest.Length";
435 DeleteFile (path);
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);
443 stream.Close ();
446 [Test]
447 public void Flush ()
449 string path = TempFolder + DSC + "FileStreamTest.Flush";
450 FileStream stream = null;
451 FileStream stream2 = null;
453 DeleteFile (path);
455 try {
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]);
469 stream.Flush ();
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]);
475 } finally {
476 if (stream != null)
477 stream.Close ();
478 if (stream2 != null)
479 stream2.Close ();
481 DeleteFile (path);
485 public void TestDefaultProperties ()
487 string path = TempFolder + Path.DirectorySeparatorChar + "testfilestream.tmp.2";
488 DeleteFile (path);
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());
499 stream.Close ();
500 DeleteFile (path);
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());
510 stream.Close ();
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());
520 stream.Close ();
521 DeleteFile (path);
524 [Category("NotWorking")]
525 // Bug: 71371
526 public void TestLock_FailsOnMono ()
528 string path = TempFolder + Path.DirectorySeparatorChar + "TestLock";
529 DeleteFile (path);
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);
534 stream.Close ();
536 stream = new FileStream (path, FileMode.Open, FileAccess.ReadWrite);
538 stream.Lock (0, 5);
540 FileStream stream2 = new FileStream (path , FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
542 byte [] bytes = new byte [5];
543 try {
544 stream2.Read (bytes, 0, 5);
545 Fail ();
546 } catch (Exception e) {
548 // locked
549 AssertEquals ("test#01", typeof (IOException), e.GetType ());
552 stream.Close ();
553 stream2.Close ();
555 DeleteFile (path);
558 public void TestLock()
560 string path = TempFolder + Path.DirectorySeparatorChar + "TestLock";
561 DeleteFile (path);
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);
566 stream.Close ();
568 stream = new FileStream (path, FileMode.Open, FileAccess.ReadWrite);
570 stream.Lock (0, 5);
572 FileStream stream2 = new FileStream (path , FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
574 byte [] bytes = new byte [5];
575 try {
576 stream2.Read (bytes, 0, 5);
577 Fail ();
578 } catch (Exception e) {
580 // locked
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]);
595 stream.Unlock (0,5);
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]);
605 stream.Close ();
606 stream2.Close ();
608 DeleteFile (path);
611 [Test]
612 public void Seek ()
614 string path = TempFolder + DSC + "FST.Seek.Test";
615 DeleteFile (path);
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 ());
636 stream.Close ();
637 stream2.Close ();
639 DeleteFile (path);
642 public void TestSeek ()
644 string path = TempFolder + Path.DirectorySeparatorChar + "TestSeek";
645 DeleteFile (path);
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 ());
656 try {
657 stream.Seek (-11, SeekOrigin.End);
658 Fail ();
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 ());
675 stream.Flush ();
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];
683 buf [0] = 2;
684 stream.Write (buf, 0, 1);
685 stream.Write (buf, 0, 1);
686 stream.Flush ();
687 stream.Seek (0, SeekOrigin.Begin);
688 AssertEquals ("test#08", 2, stream.ReadByte ());
689 AssertEquals ("test#09", 2, stream.ReadByte ());
691 stream.Close ();
693 DeleteFile (path);
696 public void TestClose ()
698 string path = TempFolder + Path.DirectorySeparatorChar + "TestClose";
699 DeleteFile (path);
701 FileStream stream = new FileStream (path, FileMode.CreateNew, FileAccess.ReadWrite);
703 stream.Write (new byte [] {1, 2, 3, 4}, 0, 4);
704 stream.ReadByte ();
705 stream.Close ();
707 try {
708 stream.ReadByte ();
709 Fail ();
710 } catch (Exception e) {
711 AssertEquals ("test#01", typeof (ObjectDisposedException), e.GetType ());
714 try {
715 stream.WriteByte (64);
716 Fail ();
717 } catch (Exception e) {
718 AssertEquals ("test#02", typeof (ObjectDisposedException), e.GetType ());
721 try {
722 stream.Flush ();
723 Fail ();
724 } catch (Exception e) {
725 AssertEquals ("test#03", typeof (ObjectDisposedException), e.GetType ());
728 try {
729 long l = stream.Length;
730 Fail ();
731 } catch (Exception e) {
732 AssertEquals ("test#04", typeof (ObjectDisposedException), e.GetType ());
735 try {
736 long l = stream.Position;
737 Fail ();
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));
747 DeleteFile (path);
751 /// <summary>
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.
755 /// </summary>
756 [Test]
757 [ExpectedException (typeof(NotSupportedException))]
758 public void TestWriteVerifyAccessMode ()
760 string path = TempFolder + Path.DirectorySeparatorChar + "temp";
761 DeleteFile (path);
763 FileStream stream = null;
764 byte[] buffer;
766 try {
767 buffer = Encoding.ASCII.GetBytes ("test");
768 stream = new FileStream (path, FileMode.OpenOrCreate, FileAccess.Read);
769 stream.Write (buffer, 0, buffer.Length);
770 } finally {
771 if (stream != null)
772 stream.Close();
773 DeleteFile (path);
777 /// <summary>
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.
781 /// </summary>
782 [Test]
783 [ExpectedException (typeof (NotSupportedException))]
784 public void TestWriteByteVerifyAccessMode ()
786 string path = TempFolder + Path.DirectorySeparatorChar + "temp";
787 DeleteFile (path);
789 FileStream stream = null;
791 try {
792 stream = new FileStream (path, FileMode.OpenOrCreate, FileAccess.Read);
793 stream.WriteByte (Byte.MinValue);
794 } finally {
795 if (stream != null)
796 stream.Close ();
797 DeleteFile (path);
801 /// <summary>
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.
805 /// </summary>
806 [Test]
807 [ExpectedException (typeof (NotSupportedException))]
808 public void TestReadVerifyAccessMode ()
810 string path = TempFolder + Path.DirectorySeparatorChar + "temp";
811 DeleteFile (path);
813 FileStream stream = null;
814 byte[] buffer = new byte [100];
816 try {
817 stream = new FileStream (path, FileMode.OpenOrCreate, FileAccess.Write);
818 stream.Read (buffer, 0, buffer.Length);
819 } finally {
820 if (stream != null)
821 stream.Close ();
825 /// <summary>
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.
829 /// </summary>
830 [Test]
831 [ExpectedException (typeof (NotSupportedException))]
832 public void TestReadByteVerifyAccessMode ()
834 string path = TempFolder + Path.DirectorySeparatorChar + "temp";
835 DeleteFile (path);
837 FileStream stream = null;
839 try {
840 stream = new FileStream (path, FileMode.OpenOrCreate, FileAccess.Write);
841 int readByte = stream.ReadByte ();
842 } finally {
843 if (stream != null)
844 stream.Close();
845 DeleteFile (path);
849 // Check that the stream is flushed even when it doesn't own the
850 // handle
851 [Test]
852 public void TestFlushNotOwningHandle ()
854 string path = Path.Combine (TempFolder, "TestFlushNotOwningHandle");
855 DeleteFile (path);
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];
860 buf [0] = (int)'1';
861 s2.Write (buf, 0, 1);
864 s.Position = 0;
865 AssertEquals ((int)'1', s.ReadByte ());
866 s.Close ();
869 private void DeleteFile (string path)
871 if (File.Exists (path))
872 File.Delete (path);
875 [Test]
876 [ExpectedException (typeof (ArgumentOutOfRangeException))]
877 public void Read_OffsetNegative ()
879 string path = TempFolder + Path.DirectorySeparatorChar + "temp";
880 DeleteFile (path);
882 using (FileStream stream = new FileStream (path, FileMode.OpenOrCreate, FileAccess.Read)) {
883 stream.Read (new byte[0], -1, 1);
887 [Test]
888 [ExpectedException (typeof (ArgumentException))]
889 public void Read_OffsetOverflow ()
891 string path = TempFolder + Path.DirectorySeparatorChar + "temp";
892 DeleteFile (path);
894 using (FileStream stream = new FileStream (path, FileMode.OpenOrCreate, FileAccess.Read)) {
895 stream.Read (new byte[0], Int32.MaxValue, 1);
899 [Test]
900 [ExpectedException (typeof (ArgumentOutOfRangeException))]
901 public void Read_CountNegative ()
903 string path = TempFolder + Path.DirectorySeparatorChar + "temp";
904 DeleteFile (path);
906 using (FileStream stream = new FileStream (path, FileMode.OpenOrCreate, FileAccess.Read)) {
907 stream.Read (new byte[0], 1, -1);
911 [Test]
912 [ExpectedException (typeof (ArgumentException))]
913 public void Read_CountOverflow ()
915 string path = TempFolder + Path.DirectorySeparatorChar + "temp";
916 DeleteFile (path);
918 using (FileStream stream = new FileStream (path, FileMode.OpenOrCreate, FileAccess.Read)) {
919 stream.Read (new byte[0], 1, Int32.MaxValue);
923 [Test]
924 [ExpectedException (typeof (ArgumentOutOfRangeException))]
925 public void Write_OffsetNegative ()
927 string path = TempFolder + Path.DirectorySeparatorChar + "temp";
928 DeleteFile (path);
930 using (FileStream stream = new FileStream (path, FileMode.OpenOrCreate, FileAccess.Write)) {
931 stream.Write (new byte[0], -1, 1);
935 [Test]
936 [ExpectedException (typeof (ArgumentException))]
937 public void Write_OffsetOverflow ()
939 string path = TempFolder + Path.DirectorySeparatorChar + "temp";
940 DeleteFile (path);
942 using (FileStream stream = new FileStream (path, FileMode.OpenOrCreate, FileAccess.Write)) {
943 stream.Write (new byte[0], Int32.MaxValue, 1);
947 [Test]
948 [ExpectedException (typeof (ArgumentOutOfRangeException))]
949 public void Write_CountNegative ()
951 string path = TempFolder + Path.DirectorySeparatorChar + "temp";
952 DeleteFile (path);
954 using (FileStream stream = new FileStream (path, FileMode.OpenOrCreate, FileAccess.Write)) {
955 stream.Write (new byte[0], 1, -1);
959 [Test]
960 [ExpectedException (typeof (ArgumentException))]
961 public void Write_CountOverflow ()
963 string path = TempFolder + Path.DirectorySeparatorChar + "temp";
964 DeleteFile (path);
966 using (FileStream stream = new FileStream (path, FileMode.OpenOrCreate, FileAccess.Write)) {
967 stream.Write (new byte[0], 1, Int32.MaxValue);
971 [Test]
972 [ExpectedException (typeof (ArgumentException))]
973 public void Seek_InvalidSeekOrigin ()
975 string path = TempFolder + Path.DirectorySeparatorChar + "temp";
976 DeleteFile (path);
978 using (FileStream stream = new FileStream (path, FileMode.OpenOrCreate, FileAccess.Read)) {
979 stream.Seek (0, (SeekOrigin) (-1));
983 [Test]
984 [ExpectedException (typeof (ArgumentException))]
985 public void Constructor_InvalidFileHandle ()
987 new FileStream ((IntPtr)(-1), FileAccess.Read);
990 [Test]
991 public void PositionAfterSetLength ()
993 string path = TempFolder + Path.DirectorySeparatorChar + "temp";
994 DeleteFile (path);
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);
1004 [Test]
1005 [ExpectedException (typeof (ObjectDisposedException))]
1006 public void SetLength_Disposed ()
1008 string path = TempFolder + Path.DirectorySeparatorChar + "temp";
1009 DeleteFile (path);
1010 FileStream stream = new FileStream (path, FileMode.OpenOrCreate, FileAccess.Write);
1011 stream.Close ();
1012 stream.SetLength (16);
1015 [Test]
1016 [ExpectedException (typeof (ObjectDisposedException))]
1017 public void Position_Disposed ()
1019 string path = TempFolder + Path.DirectorySeparatorChar + "temp";
1020 DeleteFile (path);
1021 FileStream stream = new FileStream (path, FileMode.OpenOrCreate, FileAccess.Read);
1022 stream.Close ();
1023 stream.Position = 0;
1026 [Test]
1027 [ExpectedException (typeof (ObjectDisposedException))]
1028 public void BeginRead_Disposed ()
1030 string path = TempFolder + Path.DirectorySeparatorChar + "temp";
1031 DeleteFile (path);
1032 FileStream stream = new FileStream (path, FileMode.OpenOrCreate, FileAccess.Read);
1033 stream.Close ();
1034 stream.EndRead (stream.BeginRead (new byte[8], 0, 8, null, null));
1037 [Test]
1038 [ExpectedException (typeof (ObjectDisposedException))]
1039 public void BeginWrite_Disposed ()
1041 string path = TempFolder + Path.DirectorySeparatorChar + "temp";
1042 DeleteFile (path);
1043 FileStream stream = new FileStream (path, FileMode.OpenOrCreate, FileAccess.Write);
1044 stream.Close ();
1045 stream.EndWrite (stream.BeginWrite (new byte[8], 0, 8, null, null));
1048 [Test]
1049 [ExpectedException (typeof (ObjectDisposedException))]
1050 public void Lock_Disposed ()
1052 string path = TempFolder + Path.DirectorySeparatorChar + "temp";
1053 DeleteFile (path);
1054 FileStream stream = new FileStream (path, FileMode.OpenOrCreate, FileAccess.Write);
1055 stream.Close ();
1056 stream.Lock (0,1);
1059 [Test]
1060 [ExpectedException (typeof (ObjectDisposedException))]
1061 public void Unlock_Disposed ()
1063 string path = TempFolder + Path.DirectorySeparatorChar + "temp";
1064 DeleteFile (path);
1065 FileStream stream = new FileStream (path, FileMode.OpenOrCreate, FileAccess.Write);
1066 stream.Close ();
1067 stream.Unlock (0,1);
1070 [Test]
1071 public void ReadBytePastEndOfStream ()
1073 string path = TempFolder + Path.DirectorySeparatorChar + "temp";
1074 DeleteFile (path);
1075 using (FileStream stream = new FileStream (path, FileMode.OpenOrCreate, FileAccess.Read)) {
1076 stream.Seek (0, SeekOrigin.End);
1077 AssertEquals ("ReadByte", -1, stream.ReadByte ());
1078 stream.Close ();
1082 [Test]
1083 [ExpectedException (typeof (NotSupportedException))]
1084 public void SetLengthWithClosedBaseStream ()
1086 string fn = Path.Combine (TempFolder, "temp");
1087 try {
1088 FileStream fs = new FileStream (fn, FileMode.Create);
1089 BufferedStream bs = new BufferedStream (fs);
1090 fs.Close ();
1092 bs.SetLength (1000);
1093 } finally {
1094 File.Delete (fn);