[System] UriKind.RelativeOrAbsolute workaround.
[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                         try {
230                                 if (!File.Exists ("temp")) {
231                                         TextWriter tw = File.CreateText ("temp");
232                                         tw.Write ("FOO");
233                                         tw.Close ();
234                                 }
235                                 fs = new StdioFileStream ("temp", FileMode.Open, FileAccess.Read);
236                                 fs2 = new StdioFileStream ("temp", FileMode.Open, FileAccess.Read);
237                         } finally {
238                                 if (fs != null)
239                                         fs.Close ();
240                                 if (fs2 != null)
241                                         fs2.Close ();
242                                 if (File.Exists ("temp"))
243                                         File.Delete ("temp");
244                         }
245                 }
246
247                 [Test]
248                 public void Write ()
249                 {
250                         string path = TempFolder + DSC + "StdioFileStreamTest.Write";
251
252                         DeleteFile (path);
253
254                         StdioFileStream stream = new StdioFileStream (path, FileMode.CreateNew, FileAccess.ReadWrite);
255
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];
258
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);
264
265                         stream.Read (bytes, 0, 15);
266                         for (int i = 0; i < 15; ++i)
267                                 Assert.AreEqual (i + 1, bytes [i]);
268
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);
274
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]);
279                         stream.Close ();
280                 }
281
282                 [Test]
283                 public void Length ()
284                 {
285                         // Test that the Length property takes into account the data
286                         // in the buffer
287                         string path = TempFolder + DSC + "StdioFileStreamTest.Length";
288
289                         DeleteFile (path);
290
291                         StdioFileStream stream = new StdioFileStream (path, FileMode.CreateNew);
292
293                         byte[] outbytes = new byte [] {1, 2, 3, 4};
294
295                         stream.Write (outbytes, 0, 4);
296                         Assert.AreEqual (stream.Length, 4);
297                         stream.Close ();
298                 }
299
300                 [Test]
301                 public void Flush ()
302                 {
303 #if XXX
304                     // This test depends too much on the internal implementation of stdio's FILE
305                     
306                         string path = TempFolder + DSC + "StdioFileStreamTest.Flush";
307                         StdioFileStream stream = null;
308                         StdioFileStream stream2 = null;
309
310                         DeleteFile (path);
311
312                         try {
313                                 stream = new StdioFileStream (path, FileMode.CreateNew, FileAccess.ReadWrite);
314                                 stream2 = new StdioFileStream (path, FileMode.Open, FileAccess.ReadWrite);
315
316                                 stream.Write (new byte [] {1, 2, 3, 4, 5}, 0, 5);
317
318                                 byte [] bytes = new byte [5];
319                                 stream2.Read (bytes, 0, 5);
320
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");
325
326                                 stream.Flush ();
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");
332                         } finally {
333                                 if (stream != null)
334                                         stream.Close ();
335                                 if (stream2 != null)
336                                         stream2.Close ();
337
338                                 Console.WriteLine ("P: " + path);
339                                 //DeleteFile (path);
340                         }
341 #endif
342                 }
343
344                 [Test]
345                 public void TestDefaultProperties ()
346                 {
347 #if XXX
348                         string path = TempFolder + Path.DirectorySeparatorChar + "testStdioFileStream.tmp.2";
349                         DeleteFile (path);
350
351                         StdioFileStream stream = new StdioFileStream (path, FileMode.Create);
352
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");
358                         stream.Close ();
359                         DeleteFile (path);
360
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");                    
367                         stream.Close ();
368
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");                    
375                         stream.Close ();
376                         DeleteFile (path);
377 #endif
378                 }
379
380                 // HACK: the values for `fp.ToString()' assume glibc, and may change under
381                 // a different C library (due to structure of fpos_t).
382                 [Test]
383                 public void PositionAfterWrite ()
384                 {
385 #if XXX
386                         string path = TempFolder + DSC + "FST.Position.Test";
387                         DeleteFile (path);                      
388
389                         StdioFileStream stream = new StdioFileStream (path, FileMode.CreateNew, 
390                                 FileAccess.ReadWrite);
391
392                         FilePosition fp;
393
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");
397                         fp.Dispose ();
398
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',
402                         };
403
404                         stream.Write (message, 0, message.Length);
405
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");
410                         fp.Dispose ();
411 #endif
412                 }
413
414                 [Test]
415                 public void Seek ()
416                 {
417                         string path = TempFolder + DSC + "FST.Seek.Test";
418                         DeleteFile (path);                      
419
420                         StdioFileStream stream = new StdioFileStream (path, FileMode.CreateNew, FileAccess.ReadWrite);
421                         StdioFileStream stream2 = new StdioFileStream (path, FileMode.Open, FileAccess.ReadWrite);
422
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");
426
427                         Assert.AreEqual (2, stream2.Seek (-3, SeekOrigin.Current), "test#03");
428                         Assert.AreEqual (-1, stream2.ReadByte (), "test#04");
429
430                         Assert.AreEqual (12, stream.Seek (3, SeekOrigin.Current), "test#05");
431                         Assert.AreEqual (-1, stream.ReadByte (), "test#06");
432
433                         Assert.AreEqual (5, stream.Seek (-7, SeekOrigin.Current), "test#07");
434                         Assert.AreEqual (6, stream.ReadByte (), "test#08");
435
436                         Assert.AreEqual (5, stream2.Seek (5, SeekOrigin.Begin), "test#09");
437                         Assert.AreEqual (6, stream2.ReadByte (), "test#10");
438
439                         stream.Close ();
440                         stream2.Close ();
441
442                         DeleteFile (path);
443                 }
444
445                 [Test]
446                 public void TestSeek ()
447                 {
448                         string path = TempFolder + Path.DirectorySeparatorChar + "TestSeek";
449                         DeleteFile (path);
450
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);
453
454                         stream.Seek (5, SeekOrigin.End);
455                         Assert.AreEqual (-1, stream.ReadByte (), "test#01");
456
457                         stream.Seek (-5, SeekOrigin.End);
458                         Assert.AreEqual (6, stream.ReadByte (), "test#02");
459
460                         try {
461                                 stream.Seek (-11, SeekOrigin.End);
462                                 Assert.Fail ();
463                         } catch (Exception e) {
464                                 Assert.AreEqual (typeof (IOException), e.GetType (), "test#03");
465                         }
466
467                         stream.Seek (19, SeekOrigin.Begin);
468                         Assert.AreEqual (-1, stream.ReadByte (), "test#04");
469
470                         stream.Seek (1, SeekOrigin.Begin);
471                         Assert.AreEqual (2, stream.ReadByte (), "test#05");
472
473                         stream.Seek (3, SeekOrigin.Current);
474                         Assert.AreEqual (6, stream.ReadByte (), "test#06");
475
476                         stream.Seek (-2, SeekOrigin.Current);
477                         Assert.AreEqual (5, stream.ReadByte (), "test#07");
478
479                         stream.Flush ();
480
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];
487                         buf [0] = 2;
488                         stream.Write (buf, 0, 1);
489                         stream.Write (buf, 0, 1);
490                         stream.Flush ();
491                         stream.Seek (0, SeekOrigin.Begin);
492                         Assert.AreEqual (2, stream.ReadByte (), "test#08");
493                         Assert.AreEqual (2, stream.ReadByte (), "test#09");
494
495                         stream.Close ();
496
497                         DeleteFile (path);
498                 }
499
500                 [Test]
501                 public void TestClose ()
502                 {
503 #if FALSE
504                         string path = TempFolder + Path.DirectorySeparatorChar + "TestClose";
505                         DeleteFile (path);
506
507                         StdioFileStream stream = new StdioFileStream (path, FileMode.CreateNew, FileAccess.ReadWrite);
508
509                         stream.Write (new byte [] {1, 2, 3, 4}, 0, 4);
510                         stream.ReadByte ();                     
511                         stream.Close ();
512
513                         try {                   
514                                 stream.ReadByte ();
515                                 Assert.Fail ();
516                         } catch (Exception e) {
517                                 Assert.AreEqual (typeof (ObjectDisposedException), e.GetType (), "test#01");
518                         }
519
520                         try {                   
521                                 stream.WriteByte (64);
522                                 Assert.Fail ();
523                         } catch (Exception e) {
524                                 Assert.AreEqual (typeof (ObjectDisposedException), e.GetType (), "test#02");
525                         }
526
527                         try {                   
528                                 stream.Flush ();
529                                 Assert.Fail ();
530                         } catch (Exception e) {
531                                 Assert.AreEqual (typeof (ObjectDisposedException), e.GetType (), "test#03");
532                         }
533
534                         try { 
535                                 long l = stream.Length;
536                                 Assert.Fail ();
537                         } catch (Exception e) {
538                                 Assert.AreEqual (typeof (ObjectDisposedException), e.GetType (), "test#04");
539                         }
540
541                         try { 
542                                 long l = stream.Position;
543                                 Assert.Fail ();
544                         } catch (Exception e) {
545                                 Assert.AreEqual (typeof (ObjectDisposedException), e.GetType (), "test#05");
546                         }
547
548                         try { 
549                                 FilePosition fp = stream.FilePosition;
550                                 fp.Dispose ();
551                                 Assert.Fail ();
552                         } catch (Exception e) {
553                                 Assert.AreEqual (typeof (ObjectDisposedException), e.GetType (), "test#05");
554                         }
555
556                         Assert.AreEqual (false, stream.CanRead, "test#06");
557                         Assert.AreEqual (false, stream.CanSeek, "test#07");
558                         Assert.AreEqual (false, stream.CanWrite, "test#08");                    
559
560                         DeleteFile (path);                      
561 #endif
562                 }
563
564
565                 /// <summary>
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.
569                 /// </summary>
570                 [Test]
571                 [ExpectedException (typeof(NotSupportedException))]
572                 public void TestWriteVerifyAccessMode ()
573                 {
574                         string path = TempFolder + Path.DirectorySeparatorChar + "temp";
575                         DeleteFile (path);
576
577                         StdioFileStream stream = null;
578                         byte[] buffer;
579
580                         try {
581                                 buffer = Encoding.ASCII.GetBytes ("test");
582                                 stream = new StdioFileStream (path, FileMode.OpenOrCreate, FileAccess.Read);
583                                 stream.Write (buffer, 0, buffer.Length);
584                         } finally {
585                                 if (stream != null)
586                                         stream.Close();
587                                 DeleteFile (path);
588                         }
589                 }
590
591                 /// <summary>
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.
595                 /// </summary>
596                 [Test]
597                 [ExpectedException (typeof (NotSupportedException))]
598                 public void TestWriteByteVerifyAccessMode ()
599                 {
600                         string path = TempFolder + Path.DirectorySeparatorChar + "temp";
601                         DeleteFile (path);
602
603                         StdioFileStream stream = null;
604
605                         try {
606                                 stream = new StdioFileStream (path, FileMode.OpenOrCreate, FileAccess.Read);
607                                 stream.WriteByte (Byte.MinValue);
608                         } finally {
609                                 if (stream != null)
610                                         stream.Close ();
611                                 DeleteFile (path);
612                         }
613                 }
614
615                 /// <summary>
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.
619                 /// </summary>
620                 [Test]
621                 [ExpectedException (typeof (NotSupportedException))]
622                 public void TestReadVerifyAccessMode ()
623                 {
624                         string path = TempFolder + Path.DirectorySeparatorChar + "temp";
625                         DeleteFile (path);
626
627                         StdioFileStream stream = null;
628                         byte[] buffer = new byte [100];
629
630                         try {
631                                 stream = new StdioFileStream (path, FileMode.OpenOrCreate, FileAccess.Write);
632                                 stream.Read (buffer, 0, buffer.Length);
633                         } finally {
634                                 if (stream != null)
635                                         stream.Close ();
636                         }
637                 }
638
639                 /// <summary>
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.
643                 /// </summary>
644                 [Test]
645                 [ExpectedException (typeof (NotSupportedException))]
646                 public void TestReadByteVerifyAccessMode ()
647                 {
648                         string path = TempFolder + Path.DirectorySeparatorChar + "temp";
649                         DeleteFile (path);
650
651                         StdioFileStream stream = null;
652
653                         try {
654                                 stream = new StdioFileStream (path, FileMode.OpenOrCreate, FileAccess.Write);
655                                 int readByte = stream.ReadByte ();
656                         } finally {
657                                 if (stream != null)
658                                         stream.Close();
659                                 DeleteFile (path);
660                         }
661                 }
662
663                 // Check that the stream is flushed even when it doesn't own the
664                 // handle
665                 [Test]
666                 public void TestFlushNotOwningHandle ()
667                 {
668                         string path = Path.Combine (TempFolder, "TestFlushNotOwningHandle");
669                         DeleteFile (path);
670
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];
674                                 buf [0] = (int)'1';
675                                 s2.Write (buf, 0, 1);
676                         }
677
678                         s.Position = 0;
679                         Assert.AreEqual (s.ReadByte (), (int)'1');
680                         s.Close ();
681                 }
682
683                 private void DeleteFile (string path) 
684                 {
685                         if (File.Exists (path))
686                                 File.Delete (path);
687                 }
688
689                 [Test]
690                 [ExpectedException (typeof (ArgumentOutOfRangeException))]
691                 public void Read_OffsetNegative ()
692                 {
693                         string path = TempFolder + Path.DirectorySeparatorChar + "temp";
694                         DeleteFile (path);
695
696                         using (StdioFileStream stream = new StdioFileStream (path, FileMode.OpenOrCreate, FileAccess.Read)) {
697                                 stream.Read (new byte[0], -1, 1);
698                         }
699                 }
700
701                 [Test]
702                 [ExpectedException (typeof (ArgumentException))]
703                 public void Read_OffsetOverflow ()
704                 {
705                         string path = TempFolder + Path.DirectorySeparatorChar + "temp";
706                         DeleteFile (path);
707
708                         using (StdioFileStream stream = new StdioFileStream (path, FileMode.OpenOrCreate, FileAccess.Read)) {
709                                 stream.Read (new byte[0], Int32.MaxValue, 1);
710                         }
711                 }
712
713                 [Test]
714                 [ExpectedException (typeof (ArgumentOutOfRangeException))]
715                 public void Read_CountNegative ()
716                 {
717                         string path = TempFolder + Path.DirectorySeparatorChar + "temp";
718                         DeleteFile (path);
719
720                         using (StdioFileStream stream = new StdioFileStream (path, FileMode.OpenOrCreate, FileAccess.Read)) {
721                                 stream.Read (new byte[0], 1, -1);
722                         }
723                 }
724
725                 [Test]
726                 [ExpectedException (typeof (ArgumentException))]
727                 public void Read_CountOverflow ()
728                 {
729                         string path = TempFolder + Path.DirectorySeparatorChar + "temp";
730                         DeleteFile (path);
731
732                         using (StdioFileStream stream = new StdioFileStream (path, FileMode.OpenOrCreate, FileAccess.Read)) {
733                                 stream.Read (new byte[0], 1, Int32.MaxValue);
734                         }
735                 }
736
737                 [Test]
738                 [ExpectedException (typeof (ArgumentOutOfRangeException))]
739                 public void Write_OffsetNegative ()
740                 {
741                         string path = TempFolder + Path.DirectorySeparatorChar + "temp";
742                         DeleteFile (path);
743
744                         using (StdioFileStream stream = new StdioFileStream (path, FileMode.OpenOrCreate, FileAccess.Write)) {
745                                 stream.Write (new byte[0], -1, 1);
746                         }
747                 }
748
749                 [Test]
750                 [ExpectedException (typeof (ArgumentException))]
751                 public void Write_OffsetOverflow ()
752                 {
753                         string path = TempFolder + Path.DirectorySeparatorChar + "temp";
754                         DeleteFile (path);
755
756                         using (StdioFileStream stream = new StdioFileStream (path, FileMode.OpenOrCreate, FileAccess.Write)) {
757                                 stream.Write (new byte[0], Int32.MaxValue, 1);
758                         }
759                 }
760
761                 [Test]
762                 [ExpectedException (typeof (ArgumentOutOfRangeException))]
763                 public void Write_CountNegative ()
764                 {
765                         string path = TempFolder + Path.DirectorySeparatorChar + "temp";
766                         DeleteFile (path);
767
768                         using (StdioFileStream stream = new StdioFileStream (path, FileMode.OpenOrCreate, FileAccess.Write)) {
769                                 stream.Write (new byte[0], 1, -1);
770                         }
771                 }
772
773                 [Test]
774                 [ExpectedException (typeof (ArgumentException))]
775                 public void Write_CountOverflow ()
776                 {
777                         string path = TempFolder + Path.DirectorySeparatorChar + "temp";
778                         DeleteFile (path);
779
780                         using (StdioFileStream stream = new StdioFileStream (path, FileMode.OpenOrCreate, FileAccess.Write)) {
781                                 stream.Write (new byte[0], 1, Int32.MaxValue);
782                         }
783                 }
784
785                 [Test]
786                 [ExpectedException (typeof (ArgumentException))]
787                 public void Seek_InvalidSeekOrigin () 
788                 {
789                         string path = TempFolder + Path.DirectorySeparatorChar + "temp";
790                         DeleteFile (path);
791
792                         using (StdioFileStream stream = new StdioFileStream (path, FileMode.OpenOrCreate, FileAccess.Read)) {
793                                 stream.Seek (0, (SeekOrigin) (-1));
794                         }
795                 }
796
797                 //
798                 // This test is invalid as StdioFileStream does not check for
799                 // -1 as a special invalid file handle, it tests against *zero* 
800                 // only.
801                 // See bug: 76506
802                 //
803                 //[Test]
804                 //[ExpectedException (typeof (ArgumentException))]
805                 //public void Constructor_InvalidFileHandle () 
806                 //{
807                 //              new StdioFileStream ((IntPtr)(-1), FileAccess.Read);
808                 //}
809
810                 [Test]
811                 [ExpectedException (typeof (ObjectDisposedException))]
812                 public void Position_Disposed () 
813                 {
814                         string path = TempFolder + Path.DirectorySeparatorChar + "temp";
815                         DeleteFile (path);
816                         StdioFileStream stream = new StdioFileStream (path, FileMode.OpenOrCreate, FileAccess.Read);
817                         stream.Close ();
818                         stream.Position = 0;
819                 }
820
821                 [Test]
822                 [ExpectedException (typeof (ObjectDisposedException))]
823                 public void Flush_Disposed () 
824                 {
825                         string path = TempFolder + Path.DirectorySeparatorChar + "temp";
826                         DeleteFile (path);
827                         StdioFileStream stream = new StdioFileStream (path, FileMode.OpenOrCreate, FileAccess.Write);
828                         stream.Close ();
829                         stream.Flush ();
830                 }
831
832                 [Test]
833                 [ExpectedException (typeof (ObjectDisposedException))]
834                 public void Seek_Disposed () 
835                 {
836                         string path = TempFolder + Path.DirectorySeparatorChar + "temp";
837                         DeleteFile (path);
838                         StdioFileStream stream = new StdioFileStream (path, FileMode.OpenOrCreate, FileAccess.Write);
839                         stream.Close ();
840                         stream.Seek (0, SeekOrigin.Begin);
841                 }
842
843                 [Test]
844                 public void ReadBytePastEndOfStream () 
845                 {
846                         string path = TempFolder + Path.DirectorySeparatorChar + "temp";
847                         DeleteFile (path);
848                         using (StdioFileStream stream = new StdioFileStream (path, FileMode.OpenOrCreate, FileAccess.Read)) {
849                                 stream.Seek (0, SeekOrigin.End);
850                                 Assert.AreEqual (-1, stream.ReadByte (), "ReadByte");
851                                 stream.Close ();
852                         }
853                 }
854
855                 [Test]
856                 [ExpectedException (typeof (NotSupportedException))]
857                 public void SetLengthWithClosedBaseStream ()
858                 {
859                         StdioFileStream fs = new StdioFileStream ("temp", FileMode.Create);
860                         BufferedStream bs = new BufferedStream (fs);
861                         fs.Close ();
862
863                         bs.SetLength (1000);
864                 }
865         }
866 }
867