1 // StdioFileStreamTest.cs - NUnit2 Test Cases for Mono.Unix.StdioFileStream 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;
19 namespace MonoTests.System.IO
22 public class StdioFileStreamTest {
24 string TempFolder = Path.Combine (Path.GetTempPath (), "MonoTests.Mono.Unix.Tests");
25 static readonly char DSC = Path.DirectorySeparatorChar;
28 public void TearDown()
30 if (Directory.Exists (TempFolder))
31 Directory.Delete (TempFolder, true);
37 if (Directory.Exists (TempFolder))
38 Directory.Delete (TempFolder, true);
40 Directory.CreateDirectory (TempFolder);
43 public void TestCtr ()
45 string path = TempFolder + DSC + "testfilestream.tmp.1";
47 StdioFileStream stream = null;
49 stream = new StdioFileStream (path, FileMode.Create);
59 [ExpectedException (typeof (ArgumentException))]
60 public void CtorArgumentException1 ()
62 StdioFileStream stream;
63 stream = new StdioFileStream ("", FileMode.Create);
68 [ExpectedException (typeof (ArgumentNullException))]
69 public void CtorArgumentNullException ()
71 StdioFileStream stream = new StdioFileStream (null, FileMode.Create);
76 [ExpectedException (typeof (FileNotFoundException))]
77 public void CtorFileNotFoundException1 ()
79 string path = TempFolder + DSC + "thisfileshouldnotexists.test";
81 StdioFileStream stream = null;
83 stream = new StdioFileStream (path, FileMode.Open);
92 [ExpectedException (typeof (FileNotFoundException))]
93 public void CtorFileNotFoundException2 ()
95 string path = TempFolder + DSC + "thisfileshouldNOTexists.test";
97 StdioFileStream stream = null;
100 stream = new StdioFileStream (path, FileMode.Truncate);
110 [ExpectedException (typeof (IOException))]
111 public void CtorIOException1 ()
113 string path = TempFolder + DSC + "thisfileshouldexists.test";
114 StdioFileStream stream = null;
117 stream = new StdioFileStream (path, FileMode.CreateNew);
120 stream = new StdioFileStream (path, FileMode.CreateNew);
130 [ExpectedException (typeof (ArgumentOutOfRangeException))]
131 public void CtorArgumentOutOfRangeException1 ()
133 StdioFileStream stream = null;
134 string path = TempFolder + Path.DirectorySeparatorChar + "temp";
137 stream = new StdioFileStream (path, FileMode.Append | FileMode.CreateNew);
146 [ExpectedException (typeof (ArgumentOutOfRangeException))]
147 public void CtorArgumentOutOfRangeException2 ()
149 StdioFileStream stream = null;
150 string path = TempFolder + Path.DirectorySeparatorChar + "temp";
153 stream = new StdioFileStream ("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 StdioFileStream stream = null;
171 stream = new StdioFileStream (path + DSC + "eitherthisfile.test", FileMode.CreateNew);
177 if (Directory.Exists (path))
178 Directory.Delete (path, true);
183 [ExpectedException (typeof (ArgumentException))]
184 public void CtorArgumentException3 ()
186 string path = TempFolder + Path.DirectorySeparatorChar + "temp";
187 StdioFileStream stream = null;
192 stream = new StdioFileStream (".test.test.test.2", FileMode.Truncate, FileAccess.Read);
201 // StdioFileStream doesn't mimic the "no writing by another object" rule
202 [/* Test, */ ExpectedException(typeof(IOException))]
203 public void CtorIOException ()
205 string path = TempFolder + DSC + "CTorIOException.Test";
206 StdioFileStream stream = null;
207 StdioFileStream stream2 = null;
211 stream = new StdioFileStream (path, FileMode.CreateNew);
213 // used by an another process
214 stream2 = new StdioFileStream (path, FileMode.OpenOrCreate);
225 public void CtorAccess1Read2Read ()
227 StdioFileStream fs = null;
228 StdioFileStream fs2 = null;
229 string tempPath = Path.Combine (Path.GetTempPath (), "temp");
231 if (!File.Exists (tempPath)) {
232 TextWriter tw = File.CreateText (tempPath);
236 fs = new StdioFileStream (tempPath, FileMode.Open, FileAccess.Read);
237 fs2 = new StdioFileStream (tempPath, FileMode.Open, FileAccess.Read);
243 if (File.Exists (tempPath))
244 File.Delete (tempPath);
251 string path = TempFolder + DSC + "StdioFileStreamTest.Write";
255 StdioFileStream stream = new StdioFileStream (path, FileMode.CreateNew, FileAccess.ReadWrite);
257 byte[] outbytes = new byte [] {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16};
258 byte[] bytes = new byte [15];
260 // Check that the data is flushed when we overflow the buffer
261 // with a large amount of data
262 stream.Write (outbytes, 0, 5);
263 stream.Write (outbytes, 5, 10);
264 stream.Seek (0, SeekOrigin.Begin);
266 stream.Read (bytes, 0, 15);
267 for (int i = 0; i < 15; ++i)
268 Assert.AreEqual (i + 1, bytes [i]);
270 // Check that the data is flushed when we overflow the buffer
271 // with a small amount of data
272 stream.Write (outbytes, 0, 7);
273 stream.Write (outbytes, 7, 7);
274 stream.Write (outbytes, 14, 1);
276 stream.Read (bytes, 0, 15);
277 stream.Seek (15, SeekOrigin.Begin);
278 for (int i = 0; i < 15; ++i)
279 Assert.AreEqual (i + 1, bytes [i]);
284 public void Length ()
286 // Test that the Length property takes into account the data
288 string path = TempFolder + DSC + "StdioFileStreamTest.Length";
292 StdioFileStream stream = new StdioFileStream (path, FileMode.CreateNew);
294 byte[] outbytes = new byte [] {1, 2, 3, 4};
296 stream.Write (outbytes, 0, 4);
297 Assert.AreEqual (stream.Length, 4);
305 // This test depends too much on the internal implementation of stdio's FILE
307 string path = TempFolder + DSC + "StdioFileStreamTest.Flush";
308 StdioFileStream stream = null;
309 StdioFileStream stream2 = null;
314 stream = new StdioFileStream (path, FileMode.CreateNew, FileAccess.ReadWrite);
315 stream2 = new StdioFileStream (path, FileMode.Open, FileAccess.ReadWrite);
317 stream.Write (new byte [] {1, 2, 3, 4, 5}, 0, 5);
319 byte [] bytes = new byte [5];
320 stream2.Read (bytes, 0, 5);
322 Assert.AreEqual (0, bytes [0], "test#01");
323 Assert.AreEqual (0, bytes [1], "test#02");
324 Assert.AreEqual (0, bytes [2], "test#03");
325 Assert.AreEqual (0, bytes [3], "test#04");
328 stream2.Read (bytes, 0, 5);
329 Assert.AreEqual (1, bytes [0], "test#05");
330 Assert.AreEqual (2, bytes [1], "test#06");
331 Assert.AreEqual (3, bytes [2], "test#07");
332 Assert.AreEqual (4, bytes [3], "test#08");
339 Console.WriteLine ("P: " + path);
346 public void TestDefaultProperties ()
349 string path = TempFolder + Path.DirectorySeparatorChar + "testStdioFileStream.tmp.2";
352 StdioFileStream stream = new StdioFileStream (path, FileMode.Create);
354 Assert.AreEqual (true, stream.CanRead, "test#01");
355 Assert.AreEqual (true, stream.CanSeek, "test#02");
356 Assert.AreEqual (true, stream.CanWrite, "test#03");
357 Assert.AreEqual (0, stream.Position, "test#06");
358 Assert.AreEqual ("Mono.Unix.StdioFileStream", stream.ToString(), "test#07");
362 stream = new StdioFileStream (path, FileMode.OpenOrCreate, FileAccess.Read);
363 Assert.AreEqual (true, stream.CanRead, "test#08");
364 Assert.AreEqual (true, stream.CanSeek, "test#09");
365 Assert.AreEqual (false, stream.CanWrite, "test#10");
366 Assert.AreEqual (0, stream.Position, "test#13");
367 Assert.AreEqual ("Mono.Unix.StdioFileStream", stream.ToString(), "test#14");
370 stream = new StdioFileStream (path, FileMode.Truncate, FileAccess.Write);
371 Assert.AreEqual (false, stream.CanRead, "test#15");
372 Assert.AreEqual (true, stream.CanSeek, "test#16");
373 Assert.AreEqual (true, stream.CanWrite, "test#17");
374 Assert.AreEqual (0, stream.Position, "test#20");
375 Assert.AreEqual ("Mono.Unix.StdioFileStream", stream.ToString(), "test#21");
381 // HACK: the values for `fp.ToString()' assume glibc, and may change under
382 // a different C library (due to structure of fpos_t).
384 public void PositionAfterWrite ()
387 string path = TempFolder + DSC + "FST.Position.Test";
390 StdioFileStream stream = new StdioFileStream (path, FileMode.CreateNew,
391 FileAccess.ReadWrite);
395 Assert.AreEqual (0, stream.Position, "test #01");
396 Assert.AreEqual ("(Mono.Unix.FilePosition 00000000",
397 (fp = stream.FilePosition).ToString().Substring (0, 32), "test#02");
400 byte[] message = new byte[]{
401 (byte) 'H', (byte) 'e', (byte) 'l', (byte) 'l', (byte) 'o', (byte) ' ',
402 (byte) 'W', (byte) 'o', (byte) 'r', (byte) 'l', (byte) 'd',
405 stream.Write (message, 0, message.Length);
407 Assert.AreEqual (11, stream.Position, "test #03");
408 Assert.AreEqual (message.Length, stream.Position, "test #04");
409 Assert.AreEqual ("(Mono.Unix.FilePosition 0B000000",
410 (fp = stream.FilePosition).ToString().Substring (0, 32), "test#04");
418 string path = TempFolder + DSC + "FST.Seek.Test";
421 StdioFileStream stream = new StdioFileStream (path, FileMode.CreateNew, FileAccess.ReadWrite);
422 StdioFileStream stream2 = new StdioFileStream (path, FileMode.Open, FileAccess.ReadWrite);
424 stream.Write (new byte [] {1, 2, 3, 4, 5, 6, 7, 8, 10}, 0, 9);
425 Assert.AreEqual (5, stream2.Seek (5, SeekOrigin.Begin), "test#01");
426 Assert.AreEqual (-1, stream2.ReadByte (), "test#02");
428 Assert.AreEqual (2, stream2.Seek (-3, SeekOrigin.Current), "test#03");
429 Assert.AreEqual (-1, stream2.ReadByte (), "test#04");
431 Assert.AreEqual (12, stream.Seek (3, SeekOrigin.Current), "test#05");
432 Assert.AreEqual (-1, stream.ReadByte (), "test#06");
434 Assert.AreEqual (5, stream.Seek (-7, SeekOrigin.Current), "test#07");
435 Assert.AreEqual (6, stream.ReadByte (), "test#08");
437 Assert.AreEqual (5, stream2.Seek (5, SeekOrigin.Begin), "test#09");
438 Assert.AreEqual (6, stream2.ReadByte (), "test#10");
447 public void TestSeek ()
449 string path = TempFolder + Path.DirectorySeparatorChar + "TestSeek";
452 StdioFileStream stream = new StdioFileStream (path, FileMode.CreateNew, FileAccess.ReadWrite);
453 stream.Write (new byte[] {1, 2, 3, 4, 5, 6, 7, 8 , 9, 10}, 0, 10);
455 stream.Seek (5, SeekOrigin.End);
456 Assert.AreEqual (-1, stream.ReadByte (), "test#01");
458 stream.Seek (-5, SeekOrigin.End);
459 Assert.AreEqual (6, stream.ReadByte (), "test#02");
462 stream.Seek (-11, SeekOrigin.End);
464 } catch (Exception e) {
465 Assert.AreEqual (typeof (IOException), e.GetType (), "test#03");
468 stream.Seek (19, SeekOrigin.Begin);
469 Assert.AreEqual (-1, stream.ReadByte (), "test#04");
471 stream.Seek (1, SeekOrigin.Begin);
472 Assert.AreEqual (2, stream.ReadByte (), "test#05");
474 stream.Seek (3, SeekOrigin.Current);
475 Assert.AreEqual (6, stream.ReadByte (), "test#06");
477 stream.Seek (-2, SeekOrigin.Current);
478 Assert.AreEqual (5, stream.ReadByte (), "test#07");
482 // Test that seeks work correctly when seeking inside the buffer
483 stream.Seek (0, SeekOrigin.Begin);
484 stream.WriteByte (0);
485 stream.WriteByte (1);
486 stream.Seek (0, SeekOrigin.Begin);
487 byte[] buf = new byte [1];
489 stream.Write (buf, 0, 1);
490 stream.Write (buf, 0, 1);
492 stream.Seek (0, SeekOrigin.Begin);
493 Assert.AreEqual (2, stream.ReadByte (), "test#08");
494 Assert.AreEqual (2, stream.ReadByte (), "test#09");
502 public void TestClose ()
505 string path = TempFolder + Path.DirectorySeparatorChar + "TestClose";
508 StdioFileStream stream = new StdioFileStream (path, FileMode.CreateNew, FileAccess.ReadWrite);
510 stream.Write (new byte [] {1, 2, 3, 4}, 0, 4);
517 } catch (Exception e) {
518 Assert.AreEqual (typeof (ObjectDisposedException), e.GetType (), "test#01");
522 stream.WriteByte (64);
524 } catch (Exception e) {
525 Assert.AreEqual (typeof (ObjectDisposedException), e.GetType (), "test#02");
531 } catch (Exception e) {
532 Assert.AreEqual (typeof (ObjectDisposedException), e.GetType (), "test#03");
536 long l = stream.Length;
538 } catch (Exception e) {
539 Assert.AreEqual (typeof (ObjectDisposedException), e.GetType (), "test#04");
543 long l = stream.Position;
545 } catch (Exception e) {
546 Assert.AreEqual (typeof (ObjectDisposedException), e.GetType (), "test#05");
550 FilePosition fp = stream.FilePosition;
553 } catch (Exception e) {
554 Assert.AreEqual (typeof (ObjectDisposedException), e.GetType (), "test#05");
557 Assert.AreEqual (false, stream.CanRead, "test#06");
558 Assert.AreEqual (false, stream.CanSeek, "test#07");
559 Assert.AreEqual (false, stream.CanWrite, "test#08");
567 /// Checks whether the <see cref="StdioFileStream" /> throws a <see cref="NotSupportedException" />
568 /// when the stream is opened with access mode <see cref="FileAccess.Read" /> and the
569 /// <see cref="StdioFileStream.Write(byte[], int, int)" /> method is called.
572 [ExpectedException (typeof(NotSupportedException))]
573 public void TestWriteVerifyAccessMode ()
575 string path = TempFolder + Path.DirectorySeparatorChar + "temp";
578 StdioFileStream stream = null;
582 buffer = Encoding.ASCII.GetBytes ("test");
583 stream = new StdioFileStream (path, FileMode.OpenOrCreate, FileAccess.Read);
584 stream.Write (buffer, 0, buffer.Length);
593 /// Checks whether the <see cref="StdioFileStream" /> throws a <see cref="NotSupportedException" />
594 /// when the stream is opened with access mode <see cref="FileAccess.Read" /> and the
595 /// <see cref="StdioFileStream.WriteByte(byte)" /> method is called.
598 [ExpectedException (typeof (NotSupportedException))]
599 public void TestWriteByteVerifyAccessMode ()
601 string path = TempFolder + Path.DirectorySeparatorChar + "temp";
604 StdioFileStream stream = null;
607 stream = new StdioFileStream (path, FileMode.OpenOrCreate, FileAccess.Read);
608 stream.WriteByte (Byte.MinValue);
617 /// Checks whether the <see cref="StdioFileStream" /> throws a <see cref="NotSupportedException" />
618 /// when the stream is opened with access mode <see cref="FileAccess.Write" /> and the
619 /// <see cref="StdioFileStream.Read(byte[], int, int)" /> method is called.
622 [ExpectedException (typeof (NotSupportedException))]
623 public void TestReadVerifyAccessMode ()
625 string path = TempFolder + Path.DirectorySeparatorChar + "temp";
628 StdioFileStream stream = null;
629 byte[] buffer = new byte [100];
632 stream = new StdioFileStream (path, FileMode.OpenOrCreate, FileAccess.Write);
633 stream.Read (buffer, 0, buffer.Length);
641 /// Checks whether the <see cref="StdioFileStream" /> throws a <see cref="NotSupportedException" />
642 /// when the stream is opened with access mode <see cref="FileAccess.Write" /> and the
643 /// <see cref="StdioFileStream.ReadByte()" /> method is called.
646 [ExpectedException (typeof (NotSupportedException))]
647 public void TestReadByteVerifyAccessMode ()
649 string path = TempFolder + Path.DirectorySeparatorChar + "temp";
652 StdioFileStream stream = null;
655 stream = new StdioFileStream (path, FileMode.OpenOrCreate, FileAccess.Write);
656 int readByte = stream.ReadByte ();
664 // Check that the stream is flushed even when it doesn't own the
667 public void TestFlushNotOwningHandle ()
669 string path = Path.Combine (TempFolder, "TestFlushNotOwningHandle");
672 StdioFileStream s = new StdioFileStream (path, FileMode.Create);
673 using (StdioFileStream s2 = new StdioFileStream (s.Handle, FileAccess.Write, false)) {
674 byte[] buf = new byte [2];
676 s2.Write (buf, 0, 1);
680 Assert.AreEqual (s.ReadByte (), (int)'1');
684 private void DeleteFile (string path)
686 if (File.Exists (path))
691 [ExpectedException (typeof (ArgumentOutOfRangeException))]
692 public void Read_OffsetNegative ()
694 string path = TempFolder + Path.DirectorySeparatorChar + "temp";
697 using (StdioFileStream stream = new StdioFileStream (path, FileMode.OpenOrCreate, FileAccess.Read)) {
698 stream.Read (new byte[0], -1, 1);
703 [ExpectedException (typeof (ArgumentException))]
704 public void Read_OffsetOverflow ()
706 string path = TempFolder + Path.DirectorySeparatorChar + "temp";
709 using (StdioFileStream stream = new StdioFileStream (path, FileMode.OpenOrCreate, FileAccess.Read)) {
710 stream.Read (new byte[0], Int32.MaxValue, 1);
715 [ExpectedException (typeof (ArgumentOutOfRangeException))]
716 public void Read_CountNegative ()
718 string path = TempFolder + Path.DirectorySeparatorChar + "temp";
721 using (StdioFileStream stream = new StdioFileStream (path, FileMode.OpenOrCreate, FileAccess.Read)) {
722 stream.Read (new byte[0], 1, -1);
727 [ExpectedException (typeof (ArgumentException))]
728 public void Read_CountOverflow ()
730 string path = TempFolder + Path.DirectorySeparatorChar + "temp";
733 using (StdioFileStream stream = new StdioFileStream (path, FileMode.OpenOrCreate, FileAccess.Read)) {
734 stream.Read (new byte[0], 1, Int32.MaxValue);
739 [ExpectedException (typeof (ArgumentOutOfRangeException))]
740 public void Write_OffsetNegative ()
742 string path = TempFolder + Path.DirectorySeparatorChar + "temp";
745 using (StdioFileStream stream = new StdioFileStream (path, FileMode.OpenOrCreate, FileAccess.Write)) {
746 stream.Write (new byte[0], -1, 1);
751 [ExpectedException (typeof (ArgumentException))]
752 public void Write_OffsetOverflow ()
754 string path = TempFolder + Path.DirectorySeparatorChar + "temp";
757 using (StdioFileStream stream = new StdioFileStream (path, FileMode.OpenOrCreate, FileAccess.Write)) {
758 stream.Write (new byte[0], Int32.MaxValue, 1);
763 [ExpectedException (typeof (ArgumentOutOfRangeException))]
764 public void Write_CountNegative ()
766 string path = TempFolder + Path.DirectorySeparatorChar + "temp";
769 using (StdioFileStream stream = new StdioFileStream (path, FileMode.OpenOrCreate, FileAccess.Write)) {
770 stream.Write (new byte[0], 1, -1);
775 [ExpectedException (typeof (ArgumentException))]
776 public void Write_CountOverflow ()
778 string path = TempFolder + Path.DirectorySeparatorChar + "temp";
781 using (StdioFileStream stream = new StdioFileStream (path, FileMode.OpenOrCreate, FileAccess.Write)) {
782 stream.Write (new byte[0], 1, Int32.MaxValue);
787 [ExpectedException (typeof (ArgumentException))]
788 public void Seek_InvalidSeekOrigin ()
790 string path = TempFolder + Path.DirectorySeparatorChar + "temp";
793 using (StdioFileStream stream = new StdioFileStream (path, FileMode.OpenOrCreate, FileAccess.Read)) {
794 stream.Seek (0, (SeekOrigin) (-1));
799 // This test is invalid as StdioFileStream does not check for
800 // -1 as a special invalid file handle, it tests against *zero*
805 //[ExpectedException (typeof (ArgumentException))]
806 //public void Constructor_InvalidFileHandle ()
808 // new StdioFileStream ((IntPtr)(-1), FileAccess.Read);
812 [ExpectedException (typeof (ObjectDisposedException))]
813 public void Position_Disposed ()
815 string path = TempFolder + Path.DirectorySeparatorChar + "temp";
817 StdioFileStream stream = new StdioFileStream (path, FileMode.OpenOrCreate, FileAccess.Read);
823 [ExpectedException (typeof (ObjectDisposedException))]
824 public void Flush_Disposed ()
826 string path = TempFolder + Path.DirectorySeparatorChar + "temp";
828 StdioFileStream stream = new StdioFileStream (path, FileMode.OpenOrCreate, FileAccess.Write);
834 [ExpectedException (typeof (ObjectDisposedException))]
835 public void Seek_Disposed ()
837 string path = TempFolder + Path.DirectorySeparatorChar + "temp";
839 StdioFileStream stream = new StdioFileStream (path, FileMode.OpenOrCreate, FileAccess.Write);
841 stream.Seek (0, SeekOrigin.Begin);
845 public void ReadBytePastEndOfStream ()
847 string path = TempFolder + Path.DirectorySeparatorChar + "temp";
849 using (StdioFileStream stream = new StdioFileStream (path, FileMode.OpenOrCreate, FileAccess.Read)) {
850 stream.Seek (0, SeekOrigin.End);
851 Assert.AreEqual (-1, stream.ReadByte (), "ReadByte");
857 [ExpectedException (typeof (NotSupportedException))]
858 public void SetLengthWithClosedBaseStream ()
860 string path = TempFolder + Path.DirectorySeparatorChar + "temp";
861 StdioFileStream fs = new StdioFileStream (path, FileMode.Create);
862 BufferedStream bs = new BufferedStream (fs);