Merge pull request #853 from echampet/onclick
[mono.git] / mcs / class / Mono.Posix / Test / Mono.Unix / StdioFileStreamTest.cs
1 // StdioFileStreamTest.cs - NUnit2 Test Cases for Mono.Unix.StdioFileStream 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 // 
11
12
13 using NUnit.Framework;
14 using System;
15 using System.IO;
16 using System.Text;
17 using Mono.Unix;
18
19 namespace MonoTests.System.IO
20 {
21         [TestFixture]
22         public class StdioFileStreamTest {
23
24                 string TempFolder = Path.Combine (Path.GetTempPath (), "MonoTests.Mono.Unix.Tests");
25                 static readonly char DSC = Path.DirectorySeparatorChar;
26
27                 [TearDown]
28                 public void TearDown()
29                 {
30                         if (Directory.Exists (TempFolder))
31                                 Directory.Delete (TempFolder, true);
32                 }
33
34                 [SetUp]
35                 public void SetUp ()
36                 {
37                         if (Directory.Exists (TempFolder))
38                                 Directory.Delete (TempFolder, true);
39
40                         Directory.CreateDirectory (TempFolder);
41                 }
42
43                 public void TestCtr ()
44                 {
45                         string path = TempFolder + DSC + "testfilestream.tmp.1";
46                         DeleteFile (path);
47                         StdioFileStream stream = null;
48                         try {
49                                 stream = new StdioFileStream (path, FileMode.Create);
50                         } finally {
51
52                                 if (stream != null)
53                                         stream.Close ();
54                                 DeleteFile (path);                      
55                         }
56                 }
57
58                 [Test]
59                 [ExpectedException (typeof (ArgumentException))]
60                 public void CtorArgumentException1 ()
61                 {
62                         StdioFileStream stream;
63                         stream = new StdioFileStream ("", FileMode.Create);
64                         stream.Close ();
65                 }                       
66
67                 [Test]
68                 [ExpectedException (typeof (ArgumentNullException))]
69                 public void CtorArgumentNullException ()
70                 {
71                         StdioFileStream stream = new StdioFileStream (null, FileMode.Create);
72                         stream.Close ();
73                 }
74
75                 [Test]
76                 [ExpectedException (typeof (FileNotFoundException))]
77                 public void CtorFileNotFoundException1 ()
78                 {
79                         string path = TempFolder + DSC + "thisfileshouldnotexists.test";
80                         DeleteFile (path);
81                         StdioFileStream stream = null;
82                         try {                           
83                                 stream = new StdioFileStream (path, FileMode.Open);
84                         } finally {
85                                 if (stream != null)
86                                         stream.Close ();
87                                 DeleteFile (path);
88                         }
89                 }                       
90
91                 [Test]
92                 [ExpectedException (typeof (FileNotFoundException))]
93                 public void CtorFileNotFoundException2 ()
94                 {
95                         string path = TempFolder + DSC + "thisfileshouldNOTexists.test";
96                         DeleteFile (path);
97                         StdioFileStream stream = null;
98
99                         try {
100                                 stream = new StdioFileStream (path, FileMode.Truncate);
101                         } finally {
102                                 if (stream != null)
103                                         stream.Close ();
104
105                                 DeleteFile (path);
106                         }
107                 } 
108
109                 [Test]
110                 [ExpectedException (typeof (IOException))]
111                 public void CtorIOException1 ()
112                 {
113                         string path = TempFolder + DSC + "thisfileshouldexists.test";
114                         StdioFileStream stream = null;
115                         DeleteFile (path);
116                         try {
117                                 stream = new StdioFileStream (path, FileMode.CreateNew);
118                                 stream.Close ();
119                                 stream = null;
120                                 stream = new StdioFileStream (path, FileMode.CreateNew);
121                         } finally {
122
123                                 if (stream != null)
124                                         stream.Close ();
125                                 DeleteFile (path);
126                         } 
127                 }
128
129                 [Test]
130                 [ExpectedException (typeof (ArgumentOutOfRangeException))]
131                 public void CtorArgumentOutOfRangeException1 ()
132                 {
133                         StdioFileStream stream = null;
134                         string path = TempFolder + Path.DirectorySeparatorChar + "temp";
135                         DeleteFile (path);
136                         try {
137                                 stream = new StdioFileStream (path, FileMode.Append | FileMode.CreateNew);
138                         } finally {
139                                 if (stream != null)
140                                         stream.Close ();
141                                 DeleteFile (path);
142                         }                       
143                 }                       
144
145                 [Test]
146                 [ExpectedException (typeof (ArgumentOutOfRangeException))]
147                 public void CtorArgumentOutOfRangeException2 ()
148                 {
149                         StdioFileStream stream = null;
150                         string path = TempFolder + Path.DirectorySeparatorChar + "temp";
151                         DeleteFile (path);
152                         try {
153                                 stream = new StdioFileStream ("test.test.test", FileMode.Append | FileMode.Open);
154                         } finally {
155                                 if (stream != null)
156                                         stream.Close ();
157                                 DeleteFile (path);
158                         }                       
159                 }
160
161                 [Test]
162                 [ExpectedException (typeof (DirectoryNotFoundException))]
163                 public void CtorDirectoryNotFoundException ()
164                 {
165                         string path = TempFolder + DSC + "thisDirectoryShouldNotExists";
166                         if (Directory.Exists (path))
167                                 Directory.Delete (path, true);
168
169                         StdioFileStream stream = null;                          
170                         try {
171                                 stream = new StdioFileStream (path + DSC + "eitherthisfile.test", FileMode.CreateNew);
172                         } finally {
173
174                                 if (stream != null)
175                                         stream.Close ();
176
177                                 if (Directory.Exists (path))
178                                         Directory.Delete (path, true);
179                         }                               
180                 }
181
182                 [Test]
183                 [ExpectedException (typeof (ArgumentException))]
184                 public void CtorArgumentException3 ()
185                 {
186                         string path = TempFolder + Path.DirectorySeparatorChar + "temp";
187                         StdioFileStream stream = null;
188
189                         DeleteFile (path);
190
191                         try {
192                                 stream = new StdioFileStream (".test.test.test.2", FileMode.Truncate, FileAccess.Read);
193                         } finally {
194                                 if (stream != null)
195                                         stream.Close ();
196
197                                 DeleteFile (path);
198                         }                       
199                 }
200
201                 // StdioFileStream doesn't mimic the "no writing by another object" rule
202                 [/* Test, */ ExpectedException(typeof(IOException))]
203                 public void CtorIOException ()
204                 {                       
205                         string path = TempFolder + DSC + "CTorIOException.Test";
206                         StdioFileStream stream = null;
207                         StdioFileStream stream2 = null;
208                         DeleteFile (path);
209
210                         try {
211                                 stream = new StdioFileStream (path, FileMode.CreateNew);
212
213                                 // used by an another process
214                                 stream2 = new StdioFileStream (path, FileMode.OpenOrCreate);
215                         } finally {
216                                 if (stream != null)
217                                         stream.Close ();
218                                 if (stream2 != null)
219                                         stream2.Close ();
220                                 DeleteFile (path);
221                         }
222                 }
223
224                 [Test]
225                 public void CtorAccess1Read2Read ()
226                 {
227                         StdioFileStream fs = null;
228                         StdioFileStream fs2 = null;
229                         string tempPath = Path.Combine (Path.GetTempPath (), "temp");
230                         try {
231                                 if (!File.Exists (tempPath)) {
232                                         TextWriter tw = File.CreateText (tempPath);
233                                         tw.Write ("FOO");
234                                         tw.Close ();
235                                 }
236                                 fs = new StdioFileStream (tempPath, FileMode.Open, FileAccess.Read);
237                                 fs2 = new StdioFileStream (tempPath, FileMode.Open, FileAccess.Read);
238                         } finally {
239                                 if (fs != null)
240                                         fs.Close ();
241                                 if (fs2 != null)
242                                         fs2.Close ();
243                                 if (File.Exists (tempPath))
244                                         File.Delete (tempPath);
245                         }
246                 }
247
248                 [Test]
249                 public void Write ()
250                 {
251                         string path = TempFolder + DSC + "StdioFileStreamTest.Write";
252
253                         DeleteFile (path);
254
255                         StdioFileStream stream = new StdioFileStream (path, FileMode.CreateNew, FileAccess.ReadWrite);
256
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];
259
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);
265
266                         stream.Read (bytes, 0, 15);
267                         for (int i = 0; i < 15; ++i)
268                                 Assert.AreEqual (i + 1, bytes [i]);
269
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);
275
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]);
280                         stream.Close ();
281                 }
282
283                 [Test]
284                 public void Length ()
285                 {
286                         // Test that the Length property takes into account the data
287                         // in the buffer
288                         string path = TempFolder + DSC + "StdioFileStreamTest.Length";
289
290                         DeleteFile (path);
291
292                         StdioFileStream stream = new StdioFileStream (path, FileMode.CreateNew);
293
294                         byte[] outbytes = new byte [] {1, 2, 3, 4};
295
296                         stream.Write (outbytes, 0, 4);
297                         Assert.AreEqual (stream.Length, 4);
298                         stream.Close ();
299                 }
300
301                 [Test]
302                 public void Flush ()
303                 {
304 #if XXX
305                     // This test depends too much on the internal implementation of stdio's FILE
306                     
307                         string path = TempFolder + DSC + "StdioFileStreamTest.Flush";
308                         StdioFileStream stream = null;
309                         StdioFileStream stream2 = null;
310
311                         DeleteFile (path);
312
313                         try {
314                                 stream = new StdioFileStream (path, FileMode.CreateNew, FileAccess.ReadWrite);
315                                 stream2 = new StdioFileStream (path, FileMode.Open, FileAccess.ReadWrite);
316
317                                 stream.Write (new byte [] {1, 2, 3, 4, 5}, 0, 5);
318
319                                 byte [] bytes = new byte [5];
320                                 stream2.Read (bytes, 0, 5);
321
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");
326
327                                 stream.Flush ();
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");
333                         } finally {
334                                 if (stream != null)
335                                         stream.Close ();
336                                 if (stream2 != null)
337                                         stream2.Close ();
338
339                                 Console.WriteLine ("P: " + path);
340                                 //DeleteFile (path);
341                         }
342 #endif
343                 }
344
345                 [Test]
346                 public void TestDefaultProperties ()
347                 {
348 #if XXX
349                         string path = TempFolder + Path.DirectorySeparatorChar + "testStdioFileStream.tmp.2";
350                         DeleteFile (path);
351
352                         StdioFileStream stream = new StdioFileStream (path, FileMode.Create);
353
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");
359                         stream.Close ();
360                         DeleteFile (path);
361
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");                    
368                         stream.Close ();
369
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");                    
376                         stream.Close ();
377                         DeleteFile (path);
378 #endif
379                 }
380
381                 // HACK: the values for `fp.ToString()' assume glibc, and may change under
382                 // a different C library (due to structure of fpos_t).
383                 [Test]
384                 public void PositionAfterWrite ()
385                 {
386 #if XXX
387                         string path = TempFolder + DSC + "FST.Position.Test";
388                         DeleteFile (path);                      
389
390                         StdioFileStream stream = new StdioFileStream (path, FileMode.CreateNew, 
391                                 FileAccess.ReadWrite);
392
393                         FilePosition fp;
394
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");
398                         fp.Dispose ();
399
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',
403                         };
404
405                         stream.Write (message, 0, message.Length);
406
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");
411                         fp.Dispose ();
412 #endif
413                 }
414
415                 [Test]
416                 public void Seek ()
417                 {
418                         string path = TempFolder + DSC + "FST.Seek.Test";
419                         DeleteFile (path);                      
420
421                         StdioFileStream stream = new StdioFileStream (path, FileMode.CreateNew, FileAccess.ReadWrite);
422                         StdioFileStream stream2 = new StdioFileStream (path, FileMode.Open, FileAccess.ReadWrite);
423
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");
427
428                         Assert.AreEqual (2, stream2.Seek (-3, SeekOrigin.Current), "test#03");
429                         Assert.AreEqual (-1, stream2.ReadByte (), "test#04");
430
431                         Assert.AreEqual (12, stream.Seek (3, SeekOrigin.Current), "test#05");
432                         Assert.AreEqual (-1, stream.ReadByte (), "test#06");
433
434                         Assert.AreEqual (5, stream.Seek (-7, SeekOrigin.Current), "test#07");
435                         Assert.AreEqual (6, stream.ReadByte (), "test#08");
436
437                         Assert.AreEqual (5, stream2.Seek (5, SeekOrigin.Begin), "test#09");
438                         Assert.AreEqual (6, stream2.ReadByte (), "test#10");
439
440                         stream.Close ();
441                         stream2.Close ();
442
443                         DeleteFile (path);
444                 }
445
446                 [Test]
447                 public void TestSeek ()
448                 {
449                         string path = TempFolder + Path.DirectorySeparatorChar + "TestSeek";
450                         DeleteFile (path);
451
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);
454
455                         stream.Seek (5, SeekOrigin.End);
456                         Assert.AreEqual (-1, stream.ReadByte (), "test#01");
457
458                         stream.Seek (-5, SeekOrigin.End);
459                         Assert.AreEqual (6, stream.ReadByte (), "test#02");
460
461                         try {
462                                 stream.Seek (-11, SeekOrigin.End);
463                                 Assert.Fail ();
464                         } catch (Exception e) {
465                                 Assert.AreEqual (typeof (IOException), e.GetType (), "test#03");
466                         }
467
468                         stream.Seek (19, SeekOrigin.Begin);
469                         Assert.AreEqual (-1, stream.ReadByte (), "test#04");
470
471                         stream.Seek (1, SeekOrigin.Begin);
472                         Assert.AreEqual (2, stream.ReadByte (), "test#05");
473
474                         stream.Seek (3, SeekOrigin.Current);
475                         Assert.AreEqual (6, stream.ReadByte (), "test#06");
476
477                         stream.Seek (-2, SeekOrigin.Current);
478                         Assert.AreEqual (5, stream.ReadByte (), "test#07");
479
480                         stream.Flush ();
481
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];
488                         buf [0] = 2;
489                         stream.Write (buf, 0, 1);
490                         stream.Write (buf, 0, 1);
491                         stream.Flush ();
492                         stream.Seek (0, SeekOrigin.Begin);
493                         Assert.AreEqual (2, stream.ReadByte (), "test#08");
494                         Assert.AreEqual (2, stream.ReadByte (), "test#09");
495
496                         stream.Close ();
497
498                         DeleteFile (path);
499                 }
500
501                 [Test]
502                 public void TestClose ()
503                 {
504 #if FALSE
505                         string path = TempFolder + Path.DirectorySeparatorChar + "TestClose";
506                         DeleteFile (path);
507
508                         StdioFileStream stream = new StdioFileStream (path, FileMode.CreateNew, FileAccess.ReadWrite);
509
510                         stream.Write (new byte [] {1, 2, 3, 4}, 0, 4);
511                         stream.ReadByte ();                     
512                         stream.Close ();
513
514                         try {                   
515                                 stream.ReadByte ();
516                                 Assert.Fail ();
517                         } catch (Exception e) {
518                                 Assert.AreEqual (typeof (ObjectDisposedException), e.GetType (), "test#01");
519                         }
520
521                         try {                   
522                                 stream.WriteByte (64);
523                                 Assert.Fail ();
524                         } catch (Exception e) {
525                                 Assert.AreEqual (typeof (ObjectDisposedException), e.GetType (), "test#02");
526                         }
527
528                         try {                   
529                                 stream.Flush ();
530                                 Assert.Fail ();
531                         } catch (Exception e) {
532                                 Assert.AreEqual (typeof (ObjectDisposedException), e.GetType (), "test#03");
533                         }
534
535                         try { 
536                                 long l = stream.Length;
537                                 Assert.Fail ();
538                         } catch (Exception e) {
539                                 Assert.AreEqual (typeof (ObjectDisposedException), e.GetType (), "test#04");
540                         }
541
542                         try { 
543                                 long l = stream.Position;
544                                 Assert.Fail ();
545                         } catch (Exception e) {
546                                 Assert.AreEqual (typeof (ObjectDisposedException), e.GetType (), "test#05");
547                         }
548
549                         try { 
550                                 FilePosition fp = stream.FilePosition;
551                                 fp.Dispose ();
552                                 Assert.Fail ();
553                         } catch (Exception e) {
554                                 Assert.AreEqual (typeof (ObjectDisposedException), e.GetType (), "test#05");
555                         }
556
557                         Assert.AreEqual (false, stream.CanRead, "test#06");
558                         Assert.AreEqual (false, stream.CanSeek, "test#07");
559                         Assert.AreEqual (false, stream.CanWrite, "test#08");                    
560
561                         DeleteFile (path);                      
562 #endif
563                 }
564
565
566                 /// <summary>
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.
570                 /// </summary>
571                 [Test]
572                 [ExpectedException (typeof(NotSupportedException))]
573                 public void TestWriteVerifyAccessMode ()
574                 {
575                         string path = TempFolder + Path.DirectorySeparatorChar + "temp";
576                         DeleteFile (path);
577
578                         StdioFileStream stream = null;
579                         byte[] buffer;
580
581                         try {
582                                 buffer = Encoding.ASCII.GetBytes ("test");
583                                 stream = new StdioFileStream (path, FileMode.OpenOrCreate, FileAccess.Read);
584                                 stream.Write (buffer, 0, buffer.Length);
585                         } finally {
586                                 if (stream != null)
587                                         stream.Close();
588                                 DeleteFile (path);
589                         }
590                 }
591
592                 /// <summary>
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.
596                 /// </summary>
597                 [Test]
598                 [ExpectedException (typeof (NotSupportedException))]
599                 public void TestWriteByteVerifyAccessMode ()
600                 {
601                         string path = TempFolder + Path.DirectorySeparatorChar + "temp";
602                         DeleteFile (path);
603
604                         StdioFileStream stream = null;
605
606                         try {
607                                 stream = new StdioFileStream (path, FileMode.OpenOrCreate, FileAccess.Read);
608                                 stream.WriteByte (Byte.MinValue);
609                         } finally {
610                                 if (stream != null)
611                                         stream.Close ();
612                                 DeleteFile (path);
613                         }
614                 }
615
616                 /// <summary>
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.
620                 /// </summary>
621                 [Test]
622                 [ExpectedException (typeof (NotSupportedException))]
623                 public void TestReadVerifyAccessMode ()
624                 {
625                         string path = TempFolder + Path.DirectorySeparatorChar + "temp";
626                         DeleteFile (path);
627
628                         StdioFileStream stream = null;
629                         byte[] buffer = new byte [100];
630
631                         try {
632                                 stream = new StdioFileStream (path, FileMode.OpenOrCreate, FileAccess.Write);
633                                 stream.Read (buffer, 0, buffer.Length);
634                         } finally {
635                                 if (stream != null)
636                                         stream.Close ();
637                         }
638                 }
639
640                 /// <summary>
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.
644                 /// </summary>
645                 [Test]
646                 [ExpectedException (typeof (NotSupportedException))]
647                 public void TestReadByteVerifyAccessMode ()
648                 {
649                         string path = TempFolder + Path.DirectorySeparatorChar + "temp";
650                         DeleteFile (path);
651
652                         StdioFileStream stream = null;
653
654                         try {
655                                 stream = new StdioFileStream (path, FileMode.OpenOrCreate, FileAccess.Write);
656                                 int readByte = stream.ReadByte ();
657                         } finally {
658                                 if (stream != null)
659                                         stream.Close();
660                                 DeleteFile (path);
661                         }
662                 }
663
664                 // Check that the stream is flushed even when it doesn't own the
665                 // handle
666                 [Test]
667                 public void TestFlushNotOwningHandle ()
668                 {
669                         string path = Path.Combine (TempFolder, "TestFlushNotOwningHandle");
670                         DeleteFile (path);
671
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];
675                                 buf [0] = (int)'1';
676                                 s2.Write (buf, 0, 1);
677                         }
678
679                         s.Position = 0;
680                         Assert.AreEqual (s.ReadByte (), (int)'1');
681                         s.Close ();
682                 }
683
684                 private void DeleteFile (string path) 
685                 {
686                         if (File.Exists (path))
687                                 File.Delete (path);
688                 }
689
690                 [Test]
691                 [ExpectedException (typeof (ArgumentOutOfRangeException))]
692                 public void Read_OffsetNegative ()
693                 {
694                         string path = TempFolder + Path.DirectorySeparatorChar + "temp";
695                         DeleteFile (path);
696
697                         using (StdioFileStream stream = new StdioFileStream (path, FileMode.OpenOrCreate, FileAccess.Read)) {
698                                 stream.Read (new byte[0], -1, 1);
699                         }
700                 }
701
702                 [Test]
703                 [ExpectedException (typeof (ArgumentException))]
704                 public void Read_OffsetOverflow ()
705                 {
706                         string path = TempFolder + Path.DirectorySeparatorChar + "temp";
707                         DeleteFile (path);
708
709                         using (StdioFileStream stream = new StdioFileStream (path, FileMode.OpenOrCreate, FileAccess.Read)) {
710                                 stream.Read (new byte[0], Int32.MaxValue, 1);
711                         }
712                 }
713
714                 [Test]
715                 [ExpectedException (typeof (ArgumentOutOfRangeException))]
716                 public void Read_CountNegative ()
717                 {
718                         string path = TempFolder + Path.DirectorySeparatorChar + "temp";
719                         DeleteFile (path);
720
721                         using (StdioFileStream stream = new StdioFileStream (path, FileMode.OpenOrCreate, FileAccess.Read)) {
722                                 stream.Read (new byte[0], 1, -1);
723                         }
724                 }
725
726                 [Test]
727                 [ExpectedException (typeof (ArgumentException))]
728                 public void Read_CountOverflow ()
729                 {
730                         string path = TempFolder + Path.DirectorySeparatorChar + "temp";
731                         DeleteFile (path);
732
733                         using (StdioFileStream stream = new StdioFileStream (path, FileMode.OpenOrCreate, FileAccess.Read)) {
734                                 stream.Read (new byte[0], 1, Int32.MaxValue);
735                         }
736                 }
737
738                 [Test]
739                 [ExpectedException (typeof (ArgumentOutOfRangeException))]
740                 public void Write_OffsetNegative ()
741                 {
742                         string path = TempFolder + Path.DirectorySeparatorChar + "temp";
743                         DeleteFile (path);
744
745                         using (StdioFileStream stream = new StdioFileStream (path, FileMode.OpenOrCreate, FileAccess.Write)) {
746                                 stream.Write (new byte[0], -1, 1);
747                         }
748                 }
749
750                 [Test]
751                 [ExpectedException (typeof (ArgumentException))]
752                 public void Write_OffsetOverflow ()
753                 {
754                         string path = TempFolder + Path.DirectorySeparatorChar + "temp";
755                         DeleteFile (path);
756
757                         using (StdioFileStream stream = new StdioFileStream (path, FileMode.OpenOrCreate, FileAccess.Write)) {
758                                 stream.Write (new byte[0], Int32.MaxValue, 1);
759                         }
760                 }
761
762                 [Test]
763                 [ExpectedException (typeof (ArgumentOutOfRangeException))]
764                 public void Write_CountNegative ()
765                 {
766                         string path = TempFolder + Path.DirectorySeparatorChar + "temp";
767                         DeleteFile (path);
768
769                         using (StdioFileStream stream = new StdioFileStream (path, FileMode.OpenOrCreate, FileAccess.Write)) {
770                                 stream.Write (new byte[0], 1, -1);
771                         }
772                 }
773
774                 [Test]
775                 [ExpectedException (typeof (ArgumentException))]
776                 public void Write_CountOverflow ()
777                 {
778                         string path = TempFolder + Path.DirectorySeparatorChar + "temp";
779                         DeleteFile (path);
780
781                         using (StdioFileStream stream = new StdioFileStream (path, FileMode.OpenOrCreate, FileAccess.Write)) {
782                                 stream.Write (new byte[0], 1, Int32.MaxValue);
783                         }
784                 }
785
786                 [Test]
787                 [ExpectedException (typeof (ArgumentException))]
788                 public void Seek_InvalidSeekOrigin () 
789                 {
790                         string path = TempFolder + Path.DirectorySeparatorChar + "temp";
791                         DeleteFile (path);
792
793                         using (StdioFileStream stream = new StdioFileStream (path, FileMode.OpenOrCreate, FileAccess.Read)) {
794                                 stream.Seek (0, (SeekOrigin) (-1));
795                         }
796                 }
797
798                 //
799                 // This test is invalid as StdioFileStream does not check for
800                 // -1 as a special invalid file handle, it tests against *zero* 
801                 // only.
802                 // See bug: 76506
803                 //
804                 //[Test]
805                 //[ExpectedException (typeof (ArgumentException))]
806                 //public void Constructor_InvalidFileHandle () 
807                 //{
808                 //              new StdioFileStream ((IntPtr)(-1), FileAccess.Read);
809                 //}
810
811                 [Test]
812                 [ExpectedException (typeof (ObjectDisposedException))]
813                 public void Position_Disposed () 
814                 {
815                         string path = TempFolder + Path.DirectorySeparatorChar + "temp";
816                         DeleteFile (path);
817                         StdioFileStream stream = new StdioFileStream (path, FileMode.OpenOrCreate, FileAccess.Read);
818                         stream.Close ();
819                         stream.Position = 0;
820                 }
821
822                 [Test]
823                 [ExpectedException (typeof (ObjectDisposedException))]
824                 public void Flush_Disposed () 
825                 {
826                         string path = TempFolder + Path.DirectorySeparatorChar + "temp";
827                         DeleteFile (path);
828                         StdioFileStream stream = new StdioFileStream (path, FileMode.OpenOrCreate, FileAccess.Write);
829                         stream.Close ();
830                         stream.Flush ();
831                 }
832
833                 [Test]
834                 [ExpectedException (typeof (ObjectDisposedException))]
835                 public void Seek_Disposed () 
836                 {
837                         string path = TempFolder + Path.DirectorySeparatorChar + "temp";
838                         DeleteFile (path);
839                         StdioFileStream stream = new StdioFileStream (path, FileMode.OpenOrCreate, FileAccess.Write);
840                         stream.Close ();
841                         stream.Seek (0, SeekOrigin.Begin);
842                 }
843
844                 [Test]
845                 public void ReadBytePastEndOfStream () 
846                 {
847                         string path = TempFolder + Path.DirectorySeparatorChar + "temp";
848                         DeleteFile (path);
849                         using (StdioFileStream stream = new StdioFileStream (path, FileMode.OpenOrCreate, FileAccess.Read)) {
850                                 stream.Seek (0, SeekOrigin.End);
851                                 Assert.AreEqual (-1, stream.ReadByte (), "ReadByte");
852                                 stream.Close ();
853                         }
854                 }
855
856                 [Test]
857                 [ExpectedException (typeof (NotSupportedException))]
858                 public void SetLengthWithClosedBaseStream ()
859                 {
860                         string path = TempFolder + Path.DirectorySeparatorChar + "temp";
861                         StdioFileStream fs = new StdioFileStream (path, FileMode.Create);
862                         BufferedStream bs = new BufferedStream (fs);
863                         fs.Close ();
864
865                         bs.SetLength (1000);
866                 }
867         }
868 }
869