1 // FileStreamTests.cs - NUnit2 Test Cases for System.IO.FileStream class
4 // Ville Palo (vi64pa@koti.soon.fi)
5 // Gert Driesen (gert.driesen@ardatis.com)
6 // Gonzalo Paniagua Javier (gonzalo@ximian.com)
9 // (c) 2003 Ximian, Inc. (http://www.ximian.com)
13 using NUnit.Framework;
18 namespace MonoTests.System.IO
20 public class FileStreamTest : TestCase
22 string TempFolder = Path.Combine (Path.GetTempPath (), "MonoTests.System.IO.Tests");
24 public FileStreamTest()
26 if (Directory.Exists (TempFolder))
27 Directory.Delete (TempFolder, true);
29 Directory.CreateDirectory (TempFolder);
34 if (Directory.Exists (TempFolder))
35 Directory.Delete (TempFolder, true);
39 protected override void SetUp ()
41 if (!Directory.Exists (TempFolder))
42 Directory.CreateDirectory (TempFolder);
46 protected override void TearDown ()
50 public void TestCtr ()
52 string path = TempFolder + "/testfilestream.tmp.1";
54 FileStream stream = null;
56 stream = new FileStream (path, FileMode.Create);
66 [ExpectedException (typeof (ArgumentException))]
67 public void CtorArgumentException1 ()
70 stream = new FileStream ("", FileMode.Create);
75 [ExpectedException (typeof (ArgumentNullException))]
76 public void CtorArgumentNullException ()
78 FileStream stream = new FileStream (null, FileMode.Create);
83 [ExpectedException (typeof (FileNotFoundException))]
84 public void CtorFileNotFoundException1 ()
86 string path = TempFolder + "/thisfileshouldnotexists.test";
88 FileStream stream = null;
90 stream = new FileStream (TempFolder + "/thisfileshouldnotexists.test", FileMode.Open);
99 [ExpectedException (typeof (FileNotFoundException))]
100 public void CtorFileNotFoundException2 ()
102 string path = TempFolder + "/thisfileshouldNOTexists.test";
104 FileStream stream = null;
107 stream = new FileStream (TempFolder + "/thisfileshouldNOTexists.test", FileMode.Truncate);
117 [ExpectedException (typeof (IOException))]
118 public void CtorIOException1 ()
120 string path = TempFolder + "/thisfileshouldexists.test";
121 FileStream stream = null;
124 stream = new FileStream (path, FileMode.CreateNew);
127 stream = new FileStream (path, FileMode.CreateNew);
138 [ExpectedException (typeof (ArgumentOutOfRangeException))]
139 public void CtorArgumentOutOfRangeException1 ()
141 FileStream stream = null;
142 string path = TempFolder + Path.DirectorySeparatorChar + "temp";
145 stream = new FileStream (path, FileMode.Append | FileMode.CreateNew);
154 [ExpectedException (typeof (ArgumentOutOfRangeException))]
155 public void CtorArgumentOutOfRangeException2 ()
157 FileStream stream = null;
158 string path = TempFolder + Path.DirectorySeparatorChar + "temp";
161 stream = new FileStream ("test.test.test", FileMode.Append | FileMode.Open);
170 [ExpectedException (typeof (DirectoryNotFoundException))]
171 public void CtorDirectoryNotFoundException ()
173 string path = TempFolder + "/thisDicrectoryShouldNotExists";
174 if (Directory.Exists (path))
175 Directory.Delete (path, true);
177 FileStream stream = null;
179 stream = new FileStream (path + "/eitherthisfile.test", FileMode.CreateNew);
185 if (Directory.Exists (path))
186 Directory.Delete (path, true);
191 [ExpectedException (typeof (ArgumentOutOfRangeException))]
192 public void CtorArgumentOutOfRangeException3 ()
194 string path = TempFolder + "/CtorArgumentOutOfRangeException1";
197 FileStream stream = null;
199 stream = new FileStream (path, FileMode.CreateNew, FileAccess.Read, FileShare.None | FileShare.Inheritable);
208 [ExpectedException (typeof (ArgumentOutOfRangeException))]
209 public void CtorArgumentOutOfRangeException4 ()
211 string path = TempFolder + "/CtorArgumentOutOfRangeException2";
214 FileStream stream = null;
216 stream = new FileStream (path, FileMode.Truncate, FileAccess.Read, FileShare.ReadWrite, -1);
225 [ExpectedException (typeof (ArgumentException))]
226 public void CtorArgumentException2 ()
228 // FileMode.CreateNew && FileAccess.Read
230 string path = TempFolder + Path.DirectorySeparatorChar + "temp";
231 FileStream stream = null;
236 stream = new FileStream (".test.test.test.2", FileMode.CreateNew, FileAccess.Read, FileShare.None | FileShare.Write);
247 [ExpectedException (typeof (ArgumentOutOfRangeException))]
248 public void CtorArgumentOutOfRangeException5 ()
250 string path = TempFolder + Path.DirectorySeparatorChar + "temp";
253 FileStream stream = null;
255 stream = new FileStream (path, FileMode.CreateNew, FileAccess.Read, FileShare.Inheritable | FileShare.ReadWrite);
265 [ExpectedException (typeof (ArgumentException))]
266 public void CtorArgumentException3 ()
268 string path = TempFolder + Path.DirectorySeparatorChar + "temp";
269 FileStream stream = null;
274 stream = new FileStream (".test.test.test.2", FileMode.Truncate, FileAccess.Read);
284 [ExpectedException (typeof (IOException))]
285 public void CtorIOException2 ()
287 FileStream stream = null;
289 stream = new FileStream (new IntPtr (12), FileAccess.Read);
297 [ExpectedException(typeof(IOException))]
298 public void CtorIOException ()
300 string path = TempFolder + "/CTorIOException.Test";
301 FileStream stream = null;
302 FileStream stream2 = null;
306 stream = new FileStream (path, FileMode.CreateNew);
308 // used by an another process
309 stream2 = new FileStream (path, FileMode.OpenOrCreate);
322 string path = TempFolder + "/FileStreamTest.Flush";
323 FileStream stream = null;
324 FileStream stream2 = null;
329 stream = new FileStream (path, FileMode.CreateNew, FileAccess.ReadWrite, FileShare.ReadWrite);
330 stream2 = new FileStream (path, FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite);
332 stream.Write (new byte [] {1, 2, 3, 4, 5}, 0, 5);
334 byte [] bytes = new byte [5];
335 stream2.Read (bytes, 0, 5);
337 Assertion.AssertEquals ("test#01", 0, bytes [0]);
338 Assertion.AssertEquals ("test#02", 0, bytes [1]);
339 Assertion.AssertEquals ("test#03", 0, bytes [2]);
340 Assertion.AssertEquals ("test#04", 0, bytes [3]);
343 stream2.Read (bytes, 0, 5);
344 Assertion.AssertEquals ("test#05", 1, bytes [0]);
345 Assertion.AssertEquals ("test#06", 2, bytes [1]);
346 Assertion.AssertEquals ("test#07", 3, bytes [2]);
347 Assertion.AssertEquals ("test#08", 4, bytes [3]);
358 public void TestDefaultProperties ()
360 string path = TempFolder + Path.DirectorySeparatorChar + "testfilestream.tmp.2";
363 FileStream stream = new FileStream (path, FileMode.Create);
365 AssertEquals ("test#01", true, stream.CanRead);
366 AssertEquals ("test#02", true, stream.CanSeek);
367 AssertEquals ("test#03", true, stream.CanWrite);
368 AssertEquals ("test#04", false, stream.IsAsync);
369 AssertEquals ("test#05", true, stream.Name.EndsWith (path));
370 AssertEquals ("test#06", 0, stream.Position);
371 AssertEquals ("test#07", "System.IO.FileStream", stream.ToString());
375 stream = new FileStream (path, FileMode.OpenOrCreate, FileAccess.Read);
376 AssertEquals ("test#08", true, stream.CanRead);
377 AssertEquals ("test#09", true, stream.CanSeek);
378 AssertEquals ("test#10", false, stream.CanWrite);
379 AssertEquals ("test#11", false, stream.IsAsync);
380 AssertEquals ("test#12", true, stream.Name.EndsWith (path));
381 AssertEquals ("test#13", 0, stream.Position);
382 AssertEquals ("test#14", "System.IO.FileStream", stream.ToString());
385 stream = new FileStream (path, FileMode.Truncate, FileAccess.Write, FileShare.ReadWrite);
386 AssertEquals ("test#15", false, stream.CanRead);
387 AssertEquals ("test#16", true, stream.CanSeek);
388 AssertEquals ("test#17", true, stream.CanWrite);
389 AssertEquals ("test#18", false, stream.IsAsync);
390 AssertEquals ("test#19", true, stream.Name.EndsWith ("testfilestream.tmp.2"));
391 AssertEquals ("test#20", 0, stream.Position);
392 AssertEquals ("test#21", "System.IO.FileStream", stream.ToString());
397 public void TestLock()
399 string path = TempFolder + Path.DirectorySeparatorChar + "TestLock";
402 FileStream stream = new FileStream (path, FileMode.CreateNew, FileAccess.ReadWrite);
404 stream.Write (new Byte [] {0,1,2,3,4,5,6,7,8,9,10}, 0, 10);
407 stream = new FileStream (path, FileMode.Open, FileAccess.ReadWrite);
411 FileStream stream2 = new FileStream (path , FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
413 byte [] bytes = new byte [5];
415 stream2.Read (bytes, 0, 5);
417 } catch (Exception e) {
420 AssertEquals ("test#01", typeof (IOException), e.GetType ());
423 stream2.Seek (5, SeekOrigin.Begin);
424 stream2.Read (bytes, 0, 5);
426 AssertEquals ("test#02", 5, bytes [0]);
427 AssertEquals ("test#03", 6, bytes [1]);
428 AssertEquals ("test#04", 7, bytes [2]);
429 AssertEquals ("test#05", 8, bytes [3]);
430 AssertEquals ("test#06", 9, bytes [4]);
433 stream2.Seek (0, SeekOrigin.Begin);
434 stream2.Read (bytes, 0, 5);
436 AssertEquals ("test#02", 0, bytes [0]);
437 AssertEquals ("test#03", 1, bytes [1]);
438 AssertEquals ("test#04", 2, bytes [2]);
439 AssertEquals ("test#05", 3, bytes [3]);
440 AssertEquals ("test#06", 4, bytes [4]);
451 string path = TempFolder + "/FST.Seek.Test";
454 FileStream stream = new FileStream (path, FileMode.CreateNew, FileAccess.ReadWrite, FileShare.ReadWrite);
455 FileStream stream2 = new FileStream (path, FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite);
457 stream.Write (new byte [] {1, 2, 3, 4, 5, 6, 7, 8, 10}, 0, 9);
458 Assertion.AssertEquals ("test#01", 5, stream2.Seek (5, SeekOrigin.Begin));
459 Assertion.AssertEquals ("test#02", -1, stream2.ReadByte ());
461 Assertion.AssertEquals ("test#03", 2, stream2.Seek (-3, SeekOrigin.Current));
462 Assertion.AssertEquals ("test#04", -1, stream2.ReadByte ());
464 Assertion.AssertEquals ("test#05", 12, stream.Seek (3, SeekOrigin.Current));
465 Assertion.AssertEquals ("test#06", -1, stream.ReadByte ());
467 Assertion.AssertEquals ("test#07", 5, stream.Seek (-7, SeekOrigin.Current));
468 Assertion.AssertEquals ("test#08", 6, stream.ReadByte ());
470 Assertion.AssertEquals ("test#09", 5, stream2.Seek (5, SeekOrigin.Begin));
471 Assertion.AssertEquals ("test#10", 6, stream2.ReadByte ());
479 public void TestSeek ()
481 string path = TempFolder + Path.DirectorySeparatorChar + "TestSeek";
484 FileStream stream = new FileStream (path, FileMode.CreateNew, FileAccess.ReadWrite, FileShare.ReadWrite);
485 stream.Write (new byte[] {1, 2, 3, 4, 5, 6, 7, 8 , 9, 10}, 0, 10);
487 stream.Seek (5, SeekOrigin.End);
488 AssertEquals ("test#01", -1, stream.ReadByte ());
490 stream.Seek (-5, SeekOrigin.End);
491 AssertEquals ("test#02", 6, stream.ReadByte ());
494 stream.Seek (-11, SeekOrigin.End);
496 } catch (Exception e) {
497 AssertEquals ("test#03", typeof (IOException), e.GetType ());
500 stream.Seek (19, SeekOrigin.Begin);
501 AssertEquals ("test#04", -1, stream.ReadByte ());
503 stream.Seek (1, SeekOrigin.Begin);
504 AssertEquals ("test#05", 2, stream.ReadByte ());
506 stream.Seek (3, SeekOrigin.Current);
507 AssertEquals ("test#06", 6, stream.ReadByte ());
509 stream.Seek (-2, SeekOrigin.Current);
510 AssertEquals ("test#07", 5, stream.ReadByte ());
514 // Test that seeks work correctly when seeking inside the buffer
515 stream.Seek (0, SeekOrigin.Begin);
516 stream.WriteByte (0);
517 stream.WriteByte (1);
518 stream.Seek (0, SeekOrigin.Begin);
519 byte[] buf = new byte [1];
521 stream.Write (buf, 0, 1);
522 stream.Write (buf, 0, 1);
524 stream.Seek (0, SeekOrigin.Begin);
525 AssertEquals ("test#08", 2, stream.ReadByte ());
526 AssertEquals ("test#09", 2, stream.ReadByte ());
533 public void TestClose ()
535 string path = TempFolder + Path.DirectorySeparatorChar + "TestClose";
538 FileStream stream = new FileStream (path, FileMode.CreateNew, FileAccess.ReadWrite);
540 stream.Write (new byte [] {1, 2, 3, 4}, 0, 4);
547 } catch (Exception e) {
548 AssertEquals ("test#01", typeof (ObjectDisposedException), e.GetType ());
552 stream.WriteByte (64);
554 } catch (Exception e) {
555 AssertEquals ("test#02", typeof (ObjectDisposedException), e.GetType ());
561 } catch (Exception e) {
562 AssertEquals ("test#03", typeof (ObjectDisposedException), e.GetType ());
566 long l = stream.Length;
568 } catch (Exception e) {
569 AssertEquals ("test#04", typeof (ObjectDisposedException), e.GetType ());
573 long l = stream.Position;
575 } catch (Exception e) {
576 AssertEquals ("test#05", typeof (ObjectDisposedException), e.GetType ());
579 AssertEquals ("test#06", false, stream.CanRead);
580 AssertEquals ("test#07", false, stream.CanSeek);
581 AssertEquals ("test#08", false, stream.CanWrite);
582 AssertEquals ("test#09", true, stream.Name.EndsWith (path));
589 /// Checks whether the <see cref="FileStream" /> throws a <see cref="NotSupportedException" />
590 /// when the stream is opened with access mode <see cref="FileAccess.Read" /> and the
591 /// <see cref="FileStream.Write(byte[], int, int)" /> method is called.
594 [ExpectedException (typeof(NotSupportedException))]
595 public void TestWriteVerifyAccessMode ()
597 string path = TempFolder + Path.DirectorySeparatorChar + "temp";
600 FileStream stream = null;
604 buffer = Encoding.ASCII.GetBytes ("test");
605 stream = new FileStream (path, FileMode.OpenOrCreate, FileAccess.Read);
606 stream.Write (buffer, 0, buffer.Length);
615 /// Checks whether the <see cref="FileStream" /> throws a <see cref="NotSupportedException" />
616 /// when the stream is opened with access mode <see cref="FileAccess.Read" /> and the
617 /// <see cref="FileStream.WriteByte(byte)" /> method is called.
620 [ExpectedException (typeof (NotSupportedException))]
621 public void TestWriteByteVerifyAccessMode ()
623 string path = TempFolder + Path.DirectorySeparatorChar + "temp";
626 FileStream stream = null;
629 stream = new FileStream (path, FileMode.OpenOrCreate, FileAccess.Read);
630 stream.WriteByte (Byte.MinValue);
639 /// Checks whether the <see cref="FileStream" /> throws a <see cref="NotSupportedException" />
640 /// when the stream is opened with access mode <see cref="FileAccess.Write" /> and the
641 /// <see cref="FileStream.Read(byte[], int, int)" /> method is called.
644 [ExpectedException (typeof (NotSupportedException))]
645 public void TestReadVerifyAccessMode ()
647 string path = TempFolder + Path.DirectorySeparatorChar + "temp";
650 FileStream stream = null;
651 byte[] buffer = new byte [100];
654 stream = new FileStream (path, FileMode.OpenOrCreate, FileAccess.Write);
655 stream.Read (buffer, 0, buffer.Length);
663 /// Checks whether the <see cref="FileStream" /> throws a <see cref="NotSupportedException" />
664 /// when the stream is opened with access mode <see cref="FileAccess.Write" /> and the
665 /// <see cref="FileStream.ReadByte()" /> method is called.
668 [ExpectedException (typeof (NotSupportedException))]
669 public void TestReadByteVerifyAccessMode ()
671 string path = TempFolder + Path.DirectorySeparatorChar + "temp";
674 FileStream stream = null;
677 stream = new FileStream (path, FileMode.OpenOrCreate, FileAccess.Write);
678 int readByte = stream.ReadByte ();
686 private void DeleteFile (string path)
688 if (File.Exists (path))