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;
230 if (!File.Exists ("temp")) {
231 TextWriter tw = File.CreateText ("temp");
235 fs = new StdioFileStream ("temp", FileMode.Open, FileAccess.Read);
236 fs2 = new StdioFileStream ("temp", FileMode.Open, FileAccess.Read);
242 if (File.Exists ("temp"))
243 File.Delete ("temp");
250 string path = TempFolder + DSC + "StdioFileStreamTest.Write";
254 StdioFileStream stream = new StdioFileStream (path, FileMode.CreateNew, FileAccess.ReadWrite);
256 byte[] outbytes = new byte [] {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16};
257 byte[] bytes = new byte [15];
259 // Check that the data is flushed when we overflow the buffer
260 // with a large amount of data
261 stream.Write (outbytes, 0, 5);
262 stream.Write (outbytes, 5, 10);
263 stream.Seek (0, SeekOrigin.Begin);
265 stream.Read (bytes, 0, 15);
266 for (int i = 0; i < 15; ++i)
267 Assert.AreEqual (i + 1, bytes [i]);
269 // Check that the data is flushed when we overflow the buffer
270 // with a small amount of data
271 stream.Write (outbytes, 0, 7);
272 stream.Write (outbytes, 7, 7);
273 stream.Write (outbytes, 14, 1);
275 stream.Read (bytes, 0, 15);
276 stream.Seek (15, SeekOrigin.Begin);
277 for (int i = 0; i < 15; ++i)
278 Assert.AreEqual (i + 1, bytes [i]);
283 public void Length ()
285 // Test that the Length property takes into account the data
287 string path = TempFolder + DSC + "StdioFileStreamTest.Length";
291 StdioFileStream stream = new StdioFileStream (path, FileMode.CreateNew);
293 byte[] outbytes = new byte [] {1, 2, 3, 4};
295 stream.Write (outbytes, 0, 4);
296 Assert.AreEqual (stream.Length, 4);
304 // This test depends too much on the internal implementation of stdio's FILE
306 string path = TempFolder + DSC + "StdioFileStreamTest.Flush";
307 StdioFileStream stream = null;
308 StdioFileStream stream2 = null;
313 stream = new StdioFileStream (path, FileMode.CreateNew, FileAccess.ReadWrite);
314 stream2 = new StdioFileStream (path, FileMode.Open, FileAccess.ReadWrite);
316 stream.Write (new byte [] {1, 2, 3, 4, 5}, 0, 5);
318 byte [] bytes = new byte [5];
319 stream2.Read (bytes, 0, 5);
321 Assert.AreEqual (0, bytes [0], "test#01");
322 Assert.AreEqual (0, bytes [1], "test#02");
323 Assert.AreEqual (0, bytes [2], "test#03");
324 Assert.AreEqual (0, bytes [3], "test#04");
327 stream2.Read (bytes, 0, 5);
328 Assert.AreEqual (1, bytes [0], "test#05");
329 Assert.AreEqual (2, bytes [1], "test#06");
330 Assert.AreEqual (3, bytes [2], "test#07");
331 Assert.AreEqual (4, bytes [3], "test#08");
338 Console.WriteLine ("P: " + path);
345 public void TestDefaultProperties ()
348 string path = TempFolder + Path.DirectorySeparatorChar + "testStdioFileStream.tmp.2";
351 StdioFileStream stream = new StdioFileStream (path, FileMode.Create);
353 Assert.AreEqual (true, stream.CanRead, "test#01");
354 Assert.AreEqual (true, stream.CanSeek, "test#02");
355 Assert.AreEqual (true, stream.CanWrite, "test#03");
356 Assert.AreEqual (0, stream.Position, "test#06");
357 Assert.AreEqual ("Mono.Unix.StdioFileStream", stream.ToString(), "test#07");
361 stream = new StdioFileStream (path, FileMode.OpenOrCreate, FileAccess.Read);
362 Assert.AreEqual (true, stream.CanRead, "test#08");
363 Assert.AreEqual (true, stream.CanSeek, "test#09");
364 Assert.AreEqual (false, stream.CanWrite, "test#10");
365 Assert.AreEqual (0, stream.Position, "test#13");
366 Assert.AreEqual ("Mono.Unix.StdioFileStream", stream.ToString(), "test#14");
369 stream = new StdioFileStream (path, FileMode.Truncate, FileAccess.Write);
370 Assert.AreEqual (false, stream.CanRead, "test#15");
371 Assert.AreEqual (true, stream.CanSeek, "test#16");
372 Assert.AreEqual (true, stream.CanWrite, "test#17");
373 Assert.AreEqual (0, stream.Position, "test#20");
374 Assert.AreEqual ("Mono.Unix.StdioFileStream", stream.ToString(), "test#21");
380 // HACK: the values for `fp.ToString()' assume glibc, and may change under
381 // a different C library (due to structure of fpos_t).
383 public void PositionAfterWrite ()
386 string path = TempFolder + DSC + "FST.Position.Test";
389 StdioFileStream stream = new StdioFileStream (path, FileMode.CreateNew,
390 FileAccess.ReadWrite);
394 Assert.AreEqual (0, stream.Position, "test #01");
395 Assert.AreEqual ("(Mono.Unix.FilePosition 00000000",
396 (fp = stream.FilePosition).ToString().Substring (0, 32), "test#02");
399 byte[] message = new byte[]{
400 (byte) 'H', (byte) 'e', (byte) 'l', (byte) 'l', (byte) 'o', (byte) ' ',
401 (byte) 'W', (byte) 'o', (byte) 'r', (byte) 'l', (byte) 'd',
404 stream.Write (message, 0, message.Length);
406 Assert.AreEqual (11, stream.Position, "test #03");
407 Assert.AreEqual (message.Length, stream.Position, "test #04");
408 Assert.AreEqual ("(Mono.Unix.FilePosition 0B000000",
409 (fp = stream.FilePosition).ToString().Substring (0, 32), "test#04");
417 string path = TempFolder + DSC + "FST.Seek.Test";
420 StdioFileStream stream = new StdioFileStream (path, FileMode.CreateNew, FileAccess.ReadWrite);
421 StdioFileStream stream2 = new StdioFileStream (path, FileMode.Open, FileAccess.ReadWrite);
423 stream.Write (new byte [] {1, 2, 3, 4, 5, 6, 7, 8, 10}, 0, 9);
424 Assert.AreEqual (5, stream2.Seek (5, SeekOrigin.Begin), "test#01");
425 Assert.AreEqual (-1, stream2.ReadByte (), "test#02");
427 Assert.AreEqual (2, stream2.Seek (-3, SeekOrigin.Current), "test#03");
428 Assert.AreEqual (-1, stream2.ReadByte (), "test#04");
430 Assert.AreEqual (12, stream.Seek (3, SeekOrigin.Current), "test#05");
431 Assert.AreEqual (-1, stream.ReadByte (), "test#06");
433 Assert.AreEqual (5, stream.Seek (-7, SeekOrigin.Current), "test#07");
434 Assert.AreEqual (6, stream.ReadByte (), "test#08");
436 Assert.AreEqual (5, stream2.Seek (5, SeekOrigin.Begin), "test#09");
437 Assert.AreEqual (6, stream2.ReadByte (), "test#10");
446 public void TestSeek ()
448 string path = TempFolder + Path.DirectorySeparatorChar + "TestSeek";
451 StdioFileStream stream = new StdioFileStream (path, FileMode.CreateNew, FileAccess.ReadWrite);
452 stream.Write (new byte[] {1, 2, 3, 4, 5, 6, 7, 8 , 9, 10}, 0, 10);
454 stream.Seek (5, SeekOrigin.End);
455 Assert.AreEqual (-1, stream.ReadByte (), "test#01");
457 stream.Seek (-5, SeekOrigin.End);
458 Assert.AreEqual (6, stream.ReadByte (), "test#02");
461 stream.Seek (-11, SeekOrigin.End);
463 } catch (Exception e) {
464 Assert.AreEqual (typeof (IOException), e.GetType (), "test#03");
467 stream.Seek (19, SeekOrigin.Begin);
468 Assert.AreEqual (-1, stream.ReadByte (), "test#04");
470 stream.Seek (1, SeekOrigin.Begin);
471 Assert.AreEqual (2, stream.ReadByte (), "test#05");
473 stream.Seek (3, SeekOrigin.Current);
474 Assert.AreEqual (6, stream.ReadByte (), "test#06");
476 stream.Seek (-2, SeekOrigin.Current);
477 Assert.AreEqual (5, stream.ReadByte (), "test#07");
481 // Test that seeks work correctly when seeking inside the buffer
482 stream.Seek (0, SeekOrigin.Begin);
483 stream.WriteByte (0);
484 stream.WriteByte (1);
485 stream.Seek (0, SeekOrigin.Begin);
486 byte[] buf = new byte [1];
488 stream.Write (buf, 0, 1);
489 stream.Write (buf, 0, 1);
491 stream.Seek (0, SeekOrigin.Begin);
492 Assert.AreEqual (2, stream.ReadByte (), "test#08");
493 Assert.AreEqual (2, stream.ReadByte (), "test#09");
501 public void TestClose ()
504 string path = TempFolder + Path.DirectorySeparatorChar + "TestClose";
507 StdioFileStream stream = new StdioFileStream (path, FileMode.CreateNew, FileAccess.ReadWrite);
509 stream.Write (new byte [] {1, 2, 3, 4}, 0, 4);
516 } catch (Exception e) {
517 Assert.AreEqual (typeof (ObjectDisposedException), e.GetType (), "test#01");
521 stream.WriteByte (64);
523 } catch (Exception e) {
524 Assert.AreEqual (typeof (ObjectDisposedException), e.GetType (), "test#02");
530 } catch (Exception e) {
531 Assert.AreEqual (typeof (ObjectDisposedException), e.GetType (), "test#03");
535 long l = stream.Length;
537 } catch (Exception e) {
538 Assert.AreEqual (typeof (ObjectDisposedException), e.GetType (), "test#04");
542 long l = stream.Position;
544 } catch (Exception e) {
545 Assert.AreEqual (typeof (ObjectDisposedException), e.GetType (), "test#05");
549 FilePosition fp = stream.FilePosition;
552 } catch (Exception e) {
553 Assert.AreEqual (typeof (ObjectDisposedException), e.GetType (), "test#05");
556 Assert.AreEqual (false, stream.CanRead, "test#06");
557 Assert.AreEqual (false, stream.CanSeek, "test#07");
558 Assert.AreEqual (false, stream.CanWrite, "test#08");
566 /// Checks whether the <see cref="StdioFileStream" /> throws a <see cref="NotSupportedException" />
567 /// when the stream is opened with access mode <see cref="FileAccess.Read" /> and the
568 /// <see cref="StdioFileStream.Write(byte[], int, int)" /> method is called.
571 [ExpectedException (typeof(NotSupportedException))]
572 public void TestWriteVerifyAccessMode ()
574 string path = TempFolder + Path.DirectorySeparatorChar + "temp";
577 StdioFileStream stream = null;
581 buffer = Encoding.ASCII.GetBytes ("test");
582 stream = new StdioFileStream (path, FileMode.OpenOrCreate, FileAccess.Read);
583 stream.Write (buffer, 0, buffer.Length);
592 /// Checks whether the <see cref="StdioFileStream" /> throws a <see cref="NotSupportedException" />
593 /// when the stream is opened with access mode <see cref="FileAccess.Read" /> and the
594 /// <see cref="StdioFileStream.WriteByte(byte)" /> method is called.
597 [ExpectedException (typeof (NotSupportedException))]
598 public void TestWriteByteVerifyAccessMode ()
600 string path = TempFolder + Path.DirectorySeparatorChar + "temp";
603 StdioFileStream stream = null;
606 stream = new StdioFileStream (path, FileMode.OpenOrCreate, FileAccess.Read);
607 stream.WriteByte (Byte.MinValue);
616 /// Checks whether the <see cref="StdioFileStream" /> throws a <see cref="NotSupportedException" />
617 /// when the stream is opened with access mode <see cref="FileAccess.Write" /> and the
618 /// <see cref="StdioFileStream.Read(byte[], int, int)" /> method is called.
621 [ExpectedException (typeof (NotSupportedException))]
622 public void TestReadVerifyAccessMode ()
624 string path = TempFolder + Path.DirectorySeparatorChar + "temp";
627 StdioFileStream stream = null;
628 byte[] buffer = new byte [100];
631 stream = new StdioFileStream (path, FileMode.OpenOrCreate, FileAccess.Write);
632 stream.Read (buffer, 0, buffer.Length);
640 /// Checks whether the <see cref="StdioFileStream" /> throws a <see cref="NotSupportedException" />
641 /// when the stream is opened with access mode <see cref="FileAccess.Write" /> and the
642 /// <see cref="StdioFileStream.ReadByte()" /> method is called.
645 [ExpectedException (typeof (NotSupportedException))]
646 public void TestReadByteVerifyAccessMode ()
648 string path = TempFolder + Path.DirectorySeparatorChar + "temp";
651 StdioFileStream stream = null;
654 stream = new StdioFileStream (path, FileMode.OpenOrCreate, FileAccess.Write);
655 int readByte = stream.ReadByte ();
663 // Check that the stream is flushed even when it doesn't own the
666 public void TestFlushNotOwningHandle ()
668 string path = Path.Combine (TempFolder, "TestFlushNotOwningHandle");
671 StdioFileStream s = new StdioFileStream (path, FileMode.Create);
672 using (StdioFileStream s2 = new StdioFileStream (s.Handle, FileAccess.Write, false)) {
673 byte[] buf = new byte [2];
675 s2.Write (buf, 0, 1);
679 Assert.AreEqual (s.ReadByte (), (int)'1');
683 private void DeleteFile (string path)
685 if (File.Exists (path))
690 [ExpectedException (typeof (ArgumentOutOfRangeException))]
691 public void Read_OffsetNegative ()
693 string path = TempFolder + Path.DirectorySeparatorChar + "temp";
696 using (StdioFileStream stream = new StdioFileStream (path, FileMode.OpenOrCreate, FileAccess.Read)) {
697 stream.Read (new byte[0], -1, 1);
702 [ExpectedException (typeof (ArgumentException))]
703 public void Read_OffsetOverflow ()
705 string path = TempFolder + Path.DirectorySeparatorChar + "temp";
708 using (StdioFileStream stream = new StdioFileStream (path, FileMode.OpenOrCreate, FileAccess.Read)) {
709 stream.Read (new byte[0], Int32.MaxValue, 1);
714 [ExpectedException (typeof (ArgumentOutOfRangeException))]
715 public void Read_CountNegative ()
717 string path = TempFolder + Path.DirectorySeparatorChar + "temp";
720 using (StdioFileStream stream = new StdioFileStream (path, FileMode.OpenOrCreate, FileAccess.Read)) {
721 stream.Read (new byte[0], 1, -1);
726 [ExpectedException (typeof (ArgumentException))]
727 public void Read_CountOverflow ()
729 string path = TempFolder + Path.DirectorySeparatorChar + "temp";
732 using (StdioFileStream stream = new StdioFileStream (path, FileMode.OpenOrCreate, FileAccess.Read)) {
733 stream.Read (new byte[0], 1, Int32.MaxValue);
738 [ExpectedException (typeof (ArgumentOutOfRangeException))]
739 public void Write_OffsetNegative ()
741 string path = TempFolder + Path.DirectorySeparatorChar + "temp";
744 using (StdioFileStream stream = new StdioFileStream (path, FileMode.OpenOrCreate, FileAccess.Write)) {
745 stream.Write (new byte[0], -1, 1);
750 [ExpectedException (typeof (ArgumentException))]
751 public void Write_OffsetOverflow ()
753 string path = TempFolder + Path.DirectorySeparatorChar + "temp";
756 using (StdioFileStream stream = new StdioFileStream (path, FileMode.OpenOrCreate, FileAccess.Write)) {
757 stream.Write (new byte[0], Int32.MaxValue, 1);
762 [ExpectedException (typeof (ArgumentOutOfRangeException))]
763 public void Write_CountNegative ()
765 string path = TempFolder + Path.DirectorySeparatorChar + "temp";
768 using (StdioFileStream stream = new StdioFileStream (path, FileMode.OpenOrCreate, FileAccess.Write)) {
769 stream.Write (new byte[0], 1, -1);
774 [ExpectedException (typeof (ArgumentException))]
775 public void Write_CountOverflow ()
777 string path = TempFolder + Path.DirectorySeparatorChar + "temp";
780 using (StdioFileStream stream = new StdioFileStream (path, FileMode.OpenOrCreate, FileAccess.Write)) {
781 stream.Write (new byte[0], 1, Int32.MaxValue);
786 [ExpectedException (typeof (ArgumentException))]
787 public void Seek_InvalidSeekOrigin ()
789 string path = TempFolder + Path.DirectorySeparatorChar + "temp";
792 using (StdioFileStream stream = new StdioFileStream (path, FileMode.OpenOrCreate, FileAccess.Read)) {
793 stream.Seek (0, (SeekOrigin) (-1));
798 // This test is invalid as StdioFileStream does not check for
799 // -1 as a special invalid file handle, it tests against *zero*
804 //[ExpectedException (typeof (ArgumentException))]
805 //public void Constructor_InvalidFileHandle ()
807 // new StdioFileStream ((IntPtr)(-1), FileAccess.Read);
811 [ExpectedException (typeof (ObjectDisposedException))]
812 public void Position_Disposed ()
814 string path = TempFolder + Path.DirectorySeparatorChar + "temp";
816 StdioFileStream stream = new StdioFileStream (path, FileMode.OpenOrCreate, FileAccess.Read);
822 [ExpectedException (typeof (ObjectDisposedException))]
823 public void Flush_Disposed ()
825 string path = TempFolder + Path.DirectorySeparatorChar + "temp";
827 StdioFileStream stream = new StdioFileStream (path, FileMode.OpenOrCreate, FileAccess.Write);
833 [ExpectedException (typeof (ObjectDisposedException))]
834 public void Seek_Disposed ()
836 string path = TempFolder + Path.DirectorySeparatorChar + "temp";
838 StdioFileStream stream = new StdioFileStream (path, FileMode.OpenOrCreate, FileAccess.Write);
840 stream.Seek (0, SeekOrigin.Begin);
844 public void ReadBytePastEndOfStream ()
846 string path = TempFolder + Path.DirectorySeparatorChar + "temp";
848 using (StdioFileStream stream = new StdioFileStream (path, FileMode.OpenOrCreate, FileAccess.Read)) {
849 stream.Seek (0, SeekOrigin.End);
850 Assert.AreEqual (-1, stream.ReadByte (), "ReadByte");
856 [ExpectedException (typeof (NotSupportedException))]
857 public void SetLengthWithClosedBaseStream ()
859 StdioFileStream fs = new StdioFileStream ("temp", FileMode.Create);
860 BufferedStream bs = new BufferedStream (fs);