[System.Runtime.Serialization] Static writer fix.
[mono.git] / mcs / class / corlib / Test / System.IO / FileStreamTest.cs
1 // FileStreamTests.cs - NUnit2 Test Cases for System.IO.FileStream 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 // Copyright 2011 Xamarin Inc (http://www.xamarin.com).
11 // 
12
13 using NUnit.Framework;
14 using System;
15 using System.IO;
16 using System.Runtime.InteropServices;
17 using System.Text;
18 using System.Threading;
19 using Microsoft.Win32.SafeHandles;
20
21 namespace MonoTests.System.IO
22 {
23         [TestFixture]
24         public class FileStreamTest
25         {
26                 string TempFolder = Path.Combine (Path.GetTempPath (), "MonoTests.System.IO.Tests");
27                 static readonly char DSC = Path.DirectorySeparatorChar;
28                 static bool MacOSX = false;
29
30                 [DllImport ("libc")]
31                 static extern int uname (IntPtr buf);
32
33                 [TearDown]
34                 public void TearDown ()
35                 {
36                         if (Directory.Exists (TempFolder))
37                                 Directory.Delete (TempFolder, true);
38                 }
39
40                 [SetUp]
41                 public void SetUp ()
42                 {
43                         if (Directory.Exists (TempFolder))
44                                 Directory.Delete (TempFolder, true);
45
46                         Directory.CreateDirectory (TempFolder);
47 #if !MOBILE                     
48                         // from XplatUI.cs
49                         IntPtr buf = Marshal.AllocHGlobal (8192);
50                         if (uname (buf) == 0)
51                                 MacOSX = Marshal.PtrToStringAnsi (buf) == "Darwin";
52                         Marshal.FreeHGlobal (buf);
53 #endif
54                 }
55
56                 public void TestCtr ()
57                 {
58                         string path = TempFolder + DSC + "testfilestream.tmp.1";
59                         DeleteFile (path);
60                         FileStream stream = null;
61                         try {
62                                 stream = new FileStream (path, FileMode.Create);
63                         } finally {
64
65                                 if (stream != null)
66                                         stream.Close ();
67                                 DeleteFile (path);
68                         }
69                 }
70
71                 [Test]
72                 [ExpectedException (typeof (ArgumentException))]
73                 public void CtorArgumentException1 ()
74                 {
75                         FileStream stream;
76                         stream = new FileStream ("", FileMode.Create);
77                         stream.Close ();
78                 }
79
80                 [Test]
81                 [ExpectedException (typeof (ArgumentNullException))]
82                 public void CtorArgumentNullException ()
83                 {
84                         FileStream stream = new FileStream (null, FileMode.Create);
85                         stream.Close ();
86                 }
87
88                 [Test]
89                 public void CtorFileNotFoundException_Mode_Open ()
90                 {
91                         const string file_name = "thisfileshouldnotexist.test";
92                         // absolute path
93                         string path = TempFolder + DSC + file_name;
94                         DeleteFile (path);
95                         FileStream stream = null;
96                         try {
97                                 stream = new FileStream (TempFolder + DSC + file_name, FileMode.Open);
98                                 Assert.Fail ("#A1");
99                         } catch (FileNotFoundException ex) {
100                                 Assert.AreEqual (typeof (FileNotFoundException), ex.GetType (), "#A2");
101                                 Assert.AreEqual (path, ex.FileName, "#A3");
102                                 Assert.IsNull (ex.InnerException, "#A4");
103                                 Assert.IsNotNull (ex.Message, "#A5");
104                                 Assert.IsTrue (ex.Message.IndexOf (path) != -1, "#A6");
105                         } finally {
106                                 if (stream != null) {
107                                         stream.Close ();
108                                         stream = null;
109                                 }
110                                 DeleteFile (path);
111                         }
112
113                         // relative path
114                         string orignalCurrentDir = Directory.GetCurrentDirectory ();
115                         Directory.SetCurrentDirectory (TempFolder);
116
117                         // If TempFolder is a symlink, Mono will follow it and ex.FileName below will contain
118                         // the real directory name, not that of the TempFolder symlink and the test will fail
119                         // (happens e.g. on Android M)
120                         string realTempDir = Directory.GetCurrentDirectory ();
121                         path = realTempDir + DSC + file_name;
122
123                         try {
124                                 stream = new FileStream (file_name, FileMode.Open);
125                                 Assert.Fail ("#B1");
126                         } catch (FileNotFoundException ex) {
127                                 Assert.AreEqual (typeof (FileNotFoundException), ex.GetType (), "#B2");
128                                 Assert.AreEqual (path, ex.FileName, "#B3");
129                                 Assert.IsNull (ex.InnerException, "#B4");
130                                 Assert.IsNotNull (ex.Message, "#B5");
131                                 Assert.IsTrue (ex.Message.IndexOf (path) != -1, "#B6");
132                         } finally {
133                                 // restore original current directory
134                                 Directory.SetCurrentDirectory (orignalCurrentDir);
135                                 if (stream != null)
136                                         stream.Close ();
137                                 DeleteFile (path);
138                         }
139                 }
140
141                 [Test]
142                 public void CtorFileNotFoundException_Mode_Truncate ()
143                 {
144                         const string file_name = "thisfileshouldNOTexist.test";
145                         // absolute path
146                         string path = TempFolder + DSC + file_name;
147                         DeleteFile (path);
148                         FileStream stream = null;
149                         try {
150                                 stream = new FileStream (path, FileMode.Truncate);
151                                 Assert.Fail ("#A1");
152                         } catch (FileNotFoundException ex) {
153                                 Assert.AreEqual (typeof (FileNotFoundException), ex.GetType (), "#A2");
154                                 Assert.AreEqual (path, ex.FileName, "#A3");
155                                 Assert.IsNull (ex.InnerException, "#A4");
156                                 Assert.IsNotNull (ex.Message, "#A5");
157                                 Assert.IsTrue (ex.Message.IndexOf (path) != -1, "#A6");
158                         } finally {
159                                 if (stream != null) {
160                                         stream.Close ();
161                                         stream = null;
162                                 }
163                                 DeleteFile (path);
164                         }
165
166                         // relative path
167                         string orignalCurrentDir = Directory.GetCurrentDirectory ();
168                         Directory.SetCurrentDirectory (TempFolder);
169
170                         // If TempFolder is a symlink, Mono will follow it and ex.FileName below will contain
171                         // the real directory name, not that of the TempFolder symlink and the test will fail
172                         // (happens e.g. on Android M)
173                         string realTempDir = Directory.GetCurrentDirectory ();
174                         path = realTempDir + DSC + file_name;
175
176                         try {
177                                 stream = new FileStream (file_name, FileMode.Truncate);
178                                 Assert.Fail ("#B1");
179                         } catch (FileNotFoundException ex) {
180                                 Assert.AreEqual (typeof (FileNotFoundException), ex.GetType (), "#B2");
181                                 Assert.AreEqual (path, ex.FileName, "#B3");
182                                 Assert.IsNull (ex.InnerException, "#B4");
183                                 Assert.IsNotNull (ex.Message, "#B5");
184                                 Assert.IsTrue (ex.Message.IndexOf (path) != -1, "#B6");
185                         } finally {
186                                 // restore original current directory
187                                 Directory.SetCurrentDirectory (orignalCurrentDir);
188                                 if (stream != null)
189                                         stream.Close ();
190                                 DeleteFile (path);
191                         }
192                 }
193
194                 [Test]
195                 public void CtorIOException1 ()
196                 {
197                         const string file_name = "thisfileshouldexists.test";
198                         // absolute path
199                         string path = TempFolder + DSC + file_name;
200                         FileStream stream = null;
201                         DeleteFile (path);
202                         try {
203                                 stream = new FileStream (path, FileMode.CreateNew);
204                                 stream.Close ();
205                                 stream = null;
206                                 stream = new FileStream (path, FileMode.CreateNew);
207                                 Assert.Fail ("#A1");
208                         } catch (IOException ex) {
209                                 Assert.AreEqual (typeof (IOException), ex.GetType (), "#A2");
210                                 Assert.IsNull (ex.InnerException, "#A3");
211                                 Assert.IsNotNull (ex.Message, "#A4");
212                                 Assert.IsTrue (ex.Message.IndexOf (path) != -1, "#A5");
213                         } finally {
214                                 if (stream != null) {
215                                         stream.Close ();
216                                         stream = null;
217                                 }
218                                 DeleteFile (path);
219                         }
220
221                         // relative path
222                         string orignalCurrentDir = Directory.GetCurrentDirectory ();
223                         Directory.SetCurrentDirectory (TempFolder);
224
225                         // If TempFolder is a symlink, Mono will follow it and ex.FileName below will contain
226                         // the real directory name, not that of the TempFolder symlink and the test will fail
227                         // (happens e.g. on Android M)
228                         string realTempDir = Directory.GetCurrentDirectory ();
229                         path = realTempDir + DSC + file_name;
230
231                         try {
232                                 stream = new FileStream (file_name, FileMode.CreateNew);
233                                 stream.Close ();
234                                 stream = null;
235                                 stream = new FileStream (file_name, FileMode.CreateNew);
236                                 Assert.Fail ("#B1");
237                         } catch (IOException ex) {
238                                 Assert.AreEqual (typeof (IOException), ex.GetType (), "#B2");
239                                 Assert.IsNull (ex.InnerException, "#B3");
240                                 Assert.IsNotNull (ex.Message, "#B4");
241                                 Assert.IsTrue (ex.Message.IndexOf (path) != -1, "#B5");
242                         } finally {
243                                 // restore original current directory
244                                 Directory.SetCurrentDirectory (orignalCurrentDir);
245                                 if (stream != null)
246                                         stream.Close ();
247                                 DeleteFile (path);
248                         }
249                 }
250
251                 [Test]
252                 [ExpectedException (typeof (ArgumentOutOfRangeException))]
253                 public void CtorArgumentOutOfRangeException1 ()
254                 {
255                         FileStream stream = null;
256                         string path = TempFolder + Path.DirectorySeparatorChar + "temp";
257                         DeleteFile (path);
258                         try {
259                                 stream = new FileStream (path, FileMode.Append | FileMode.CreateNew);
260                         } finally {
261                                 if (stream != null)
262                                         stream.Close ();
263                                 DeleteFile (path);
264                         }
265                 }
266
267                 [Test]
268                 [ExpectedException (typeof (ArgumentOutOfRangeException))]
269                 public void CtorArgumentOutOfRangeException2 ()
270                 {
271                         FileStream stream = null;
272                         string path = TempFolder + Path.DirectorySeparatorChar + "temp";
273                         DeleteFile (path);
274                         try {
275                                 stream = new FileStream ("test.test.test", FileMode.Append | FileMode.Open);
276                         } finally {
277                                 if (stream != null)
278                                         stream.Close ();
279                                 DeleteFile (path);
280                         }
281                 }
282
283                 private void CtorDirectoryNotFoundException (FileMode mode)
284                 {
285                         string path = TempFolder + DSC + "thisDirectoryShouldNotExists";
286                         if (Directory.Exists (path))
287                                 Directory.Delete (path, true);
288
289                         FileStream stream = null;
290                         try {
291                                 stream = new FileStream (path + DSC + "eitherthisfile.test", mode);
292                         } finally {
293
294                                 if (stream != null)
295                                         stream.Close ();
296
297                                 if (Directory.Exists (path))
298                                         Directory.Delete (path, true);
299                         }
300                 }
301
302                 [Test]
303                 [ExpectedException (typeof (DirectoryNotFoundException))]
304                 public void CtorDirectoryNotFoundException_CreateNew ()
305                 {
306                         CtorDirectoryNotFoundException (FileMode.CreateNew);
307                 }
308
309                 [Test]
310                 [ExpectedException (typeof (DirectoryNotFoundException))]
311                 public void CtorDirectoryNotFoundException_Create ()
312                 {
313                         CtorDirectoryNotFoundException (FileMode.Create);
314                 }
315
316                 [Test]
317                 [ExpectedException (typeof (DirectoryNotFoundException))]
318                 public void CtorDirectoryNotFoundException_Open ()
319                 {
320                         CtorDirectoryNotFoundException (FileMode.Open);
321                 }
322
323                 [Test]
324                 [ExpectedException (typeof (DirectoryNotFoundException))]
325                 public void CtorDirectoryNotFoundException_OpenOrCreate ()
326                 {
327                         CtorDirectoryNotFoundException (FileMode.OpenOrCreate);
328                 }
329
330                 [Test]
331                 [ExpectedException (typeof (DirectoryNotFoundException))]
332                 public void CtorDirectoryNotFoundException_Truncate ()
333                 {
334                         CtorDirectoryNotFoundException (FileMode.Truncate);
335                 }
336
337                 [Test]
338                 [ExpectedException (typeof (DirectoryNotFoundException))]
339                 public void CtorDirectoryNotFoundException_Append ()
340                 {
341                         CtorDirectoryNotFoundException (FileMode.Append);
342                 }
343
344                 [Test]
345                 public void CtorDirectoryNotFound_RelativePath ()
346                 {
347                         string orignalCurrentDir = Directory.GetCurrentDirectory ();
348                         Directory.SetCurrentDirectory (TempFolder);
349
350                         // If TempFolder is a symlink, Mono will follow it and ex.FileName below will contain
351                         // the real directory name, not that of the TempFolder symlink and the test will fail
352                         // (happens e.g. on Android M)
353                         string realTempDir = Directory.GetCurrentDirectory ();
354                         
355                         string relativePath = "DirectoryDoesNotExist" + Path.DirectorySeparatorChar + "file.txt";
356                         string fullPath = Path.Combine (realTempDir, relativePath);
357                         try {
358                                 new FileStream (relativePath, FileMode.Open);
359                                 Assert.Fail ("#A1");
360                         } catch (DirectoryNotFoundException ex) {
361                                 Assert.AreEqual (typeof (DirectoryNotFoundException), ex.GetType (), "#A2");
362                                 Assert.IsNull (ex.InnerException, "#A3");
363                                 Assert.IsNotNull (ex.Message, "#A4");
364                                 Assert.IsTrue (ex.Message.IndexOf (fullPath) != -1, "#A5");
365                         } finally {
366                                 // restore original current directory
367                                 Directory.SetCurrentDirectory (orignalCurrentDir);
368                         }
369                 }
370
371                 [Test]
372                 // FileShare.Inheritable is ignored, but file does not exist
373                 [ExpectedException (typeof (FileNotFoundException))]
374                 public void CtorArgumentOutOfRangeException3 ()
375                 {
376                         string path = TempFolder + DSC + "CtorArgumentOutOfRangeException1";
377                         DeleteFile (path);
378
379                         FileStream stream = null;
380                         try {
381                                 stream = new FileStream (path, FileMode.Open, FileAccess.Read, FileShare.Inheritable);
382                         } finally {
383                                 if (stream != null)
384                                         stream.Close ();
385                                 DeleteFile (path);
386                         }
387                 }
388
389                 [Test]
390                 [ExpectedException (typeof (ArgumentOutOfRangeException))]
391                 public void CtorArgumentOutOfRangeException4 ()
392                 {
393                         string path = TempFolder + DSC + "CtorArgumentOutOfRangeException4";
394                         DeleteFile (path);
395
396                         FileStream stream = null;
397                         try {
398                                 stream = new FileStream (path, FileMode.OpenOrCreate, FileAccess.Read, FileShare.ReadWrite, -1);
399                         } finally {
400                                 if (stream != null)
401                                         stream.Close ();
402                                 DeleteFile (path);
403                         }
404                 }
405
406                 [Test]
407                 [ExpectedException (typeof (ArgumentOutOfRangeException))]
408                 public void CtorBufferSizeZero ()
409                 {
410                         // Buffer size can't be zero
411
412                         string path = Path.Combine (TempFolder, "CtorBufferSizeZero");
413                         DeleteFile (path);
414
415                         FileStream stream = null;
416                         try {
417                                 stream = new FileStream (path, FileMode.CreateNew, FileAccess.Write, FileShare.ReadWrite, 0);
418                         } finally {
419                                 if (stream != null)
420                                         stream.Close ();
421                                 DeleteFile (path);
422                         }
423                 }
424
425                 [Test]
426                 [ExpectedException (typeof (ArgumentException))]
427                 public void CtorArgumentException2 ()
428                 {
429                         // FileMode.CreateNew && FileAccess.Read
430
431                         string path = TempFolder + Path.DirectorySeparatorChar + "temp";
432                         FileStream stream = null;
433
434                         DeleteFile (path);
435
436                         try {
437                                 stream = new FileStream (".test.test.test.2", FileMode.CreateNew, FileAccess.Read, FileShare.None | FileShare.Write);
438                         } finally {
439
440                                 if (stream != null)
441                                         stream.Close ();
442                                 DeleteFile (path);
443                         }
444                 }
445
446
447                 [Test]
448                 // FileShare.Inheritable is ignored, but file does not exist
449                 [ExpectedException (typeof (FileNotFoundException))]
450                 public void CtorArgumentOutOfRangeException5 ()
451                 {
452                         string path = TempFolder + Path.DirectorySeparatorChar + "temp";
453                         DeleteFile (path);
454
455                         FileStream stream = null;
456                         try {
457                                 stream = new FileStream (path, FileMode.Open, FileAccess.Read, FileShare.Inheritable | FileShare.ReadWrite);
458                         } finally {
459                                 if (stream != null)
460                                         stream.Close ();
461                                 DeleteFile (path);
462                         }
463                 }
464
465
466                 [Test]
467                 [ExpectedException (typeof (ArgumentException))]
468                 public void CtorArgumentException3 ()
469                 {
470                         string path = TempFolder + Path.DirectorySeparatorChar + "temp";
471                         FileStream stream = null;
472
473                         DeleteFile (path);
474
475                         try {
476                                 stream = new FileStream (".test.test.test.2", FileMode.Truncate, FileAccess.Read);
477                         } finally {
478                                 if (stream != null)
479                                         stream.Close ();
480
481                                 DeleteFile (path);
482                         }
483                 }
484
485                 [Test]
486                 public void ModeAndAccessCombinations ()
487                 {
488                         string path = TempFolder + Path.DirectorySeparatorChar + "temp";
489                         DeleteFile (path);
490                         FileStream stream = null;
491
492                         // Append / Read
493                         try {
494                                 // Append access can be requested only in write-only mode
495                                 stream = new FileStream (path, FileMode.Append, FileAccess.Read);
496                                 Assert.Fail ("#A1");
497                         } catch (ArgumentException ex) {
498                                 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#A2");
499                         } finally {
500                                 if (stream != null)
501                                         stream.Close ();
502
503                                 DeleteFile (path);
504                         }
505
506                         // Append / ReadWrite
507                         try {
508                                 // Append access can be requested only in write-only mode
509                                 stream = new FileStream (path, FileMode.Append, FileAccess.ReadWrite);
510                                 Assert.Fail ("#B1");
511                         } catch (ArgumentException ex) {
512                                 // make sure it is exact this exception, and not a derived type
513                                 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#B2");
514                         } finally {
515                                 if (stream != null)
516                                         stream.Close ();
517
518                                 DeleteFile (path);
519                         }
520
521                         // Append / Write
522                         try {
523                                 stream = new FileStream (path, FileMode.Append, FileAccess.Write);
524                         } finally {
525                                 if (stream != null)
526                                         stream.Close ();
527
528                                 DeleteFile (path);
529                         }
530
531                         // Create / Read
532                         try {
533                                 stream = new FileStream (path, FileMode.Create, FileAccess.Read);
534                                 Assert.Fail ("#C1");
535                         } catch (ArgumentException ex) {
536                                 // make sure it is exact this exception, and not a derived type
537                                 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#C2");
538                         } finally {
539                                 if (stream != null)
540                                         stream.Close ();
541
542                                 DeleteFile (path);
543                         }
544
545                         // Create / ReadWrite
546                         try {
547                                 stream = new FileStream (path, FileMode.Create, FileAccess.ReadWrite);
548                         } finally {
549                                 if (stream != null)
550                                         stream.Close ();
551
552                                 DeleteFile (path);
553                         }
554
555                         // Create / Write
556                         try {
557                                 stream = new FileStream (path, FileMode.Create, FileAccess.Write);
558                         } finally {
559                                 if (stream != null)
560                                         stream.Close ();
561
562                                 DeleteFile (path);
563                         }
564
565                         // CreateNew / Read
566                         try {
567                                 stream = new FileStream (path, FileMode.CreateNew, FileAccess.Read);
568                                 Assert.Fail ("#D1");
569                         } catch (ArgumentException ex) {
570                                 // make sure it is exact this exception, and not a derived type
571                                 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#D2");
572                         } finally {
573                                 if (stream != null)
574                                         stream.Close ();
575
576                                 DeleteFile (path);
577                         }
578
579                         // CreateNew / ReadWrite
580                         try {
581                                 stream = new FileStream (path, FileMode.CreateNew, FileAccess.ReadWrite);
582                         } finally {
583                                 if (stream != null)
584                                         stream.Close ();
585
586                                 DeleteFile (path);
587                         }
588
589                         // CreateNew / Write
590                         try {
591                                 stream = new FileStream (path, FileMode.CreateNew, FileAccess.Write);
592                         } finally {
593                                 if (stream != null)
594                                         stream.Close ();
595
596                                 DeleteFile (path);
597                         }
598
599                         // Open / Read
600                         try {
601                                 stream = new FileStream (path, FileMode.Open, FileAccess.Read);
602                                 Assert.Fail ("#E1");
603                         } catch (FileNotFoundException ex) {
604                                 // make sure it is exact this exception, and not a derived type
605                                 Assert.AreEqual (typeof (FileNotFoundException), ex.GetType (), "#E2");
606                                 Assert.AreEqual (path, ex.FileName, "#E3");
607                                 Assert.IsNull (ex.InnerException, "#E4");
608                                 Assert.IsNotNull (ex.Message, "#E5");
609                                 Assert.IsTrue (ex.Message.IndexOf (path) != -1, "#E6");
610                         } finally {
611                                 if (stream != null)
612                                         stream.Close ();
613
614                                 DeleteFile (path);
615                         }
616
617                         // Open / ReadWrite
618                         try {
619                                 stream = new FileStream (path, FileMode.Open, FileAccess.ReadWrite);
620                                 Assert.Fail ("#F1");
621                         } catch (FileNotFoundException ex) {
622                                 // make sure it is exact this exception, and not a derived type
623                                 Assert.AreEqual (typeof (FileNotFoundException), ex.GetType (), "#F2");
624                                 Assert.AreEqual (path, ex.FileName, "#F3");
625                                 Assert.IsNull (ex.InnerException, "#F4");
626                                 Assert.IsNotNull (ex.Message, "#F5");
627                                 Assert.IsTrue (ex.Message.IndexOf (path) != -1, "#F6");
628                         } finally {
629                                 if (stream != null)
630                                         stream.Close ();
631
632                                 DeleteFile (path);
633                         }
634
635                         // Open / Write
636                         try {
637                                 stream = new FileStream (path, FileMode.Open, FileAccess.Write);
638                                 Assert.Fail ("#G1");
639                         } catch (FileNotFoundException ex) {
640                                 // make sure it is exact this exception, and not a derived type
641                                 Assert.AreEqual (typeof (FileNotFoundException), ex.GetType (), "#G2");
642                                 Assert.AreEqual (path, ex.FileName, "#G3");
643                                 Assert.IsNull (ex.InnerException, "#G4");
644                                 Assert.IsNotNull (ex.Message, "#G5");
645                                 Assert.IsTrue (ex.Message.IndexOf (path) != -1, "#G6");
646                         } finally {
647                                 if (stream != null)
648                                         stream.Close ();
649
650                                 DeleteFile (path);
651                         }
652
653                         // OpenOrCreate / Read
654                         try {
655                                 stream = new FileStream (path, FileMode.OpenOrCreate, FileAccess.Read);
656                         } finally {
657                                 if (stream != null)
658                                         stream.Close ();
659
660                                 DeleteFile (path);
661                         }
662
663                         // OpenOrCreate / ReadWrite
664                         try {
665                                 stream = new FileStream (path, FileMode.OpenOrCreate, FileAccess.ReadWrite);
666                         } finally {
667                                 if (stream != null)
668                                         stream.Close ();
669
670                                 DeleteFile (path);
671                         }
672
673                         // OpenOrCreate / Write
674                         try {
675                                 stream = new FileStream (path, FileMode.OpenOrCreate, FileAccess.Write);
676                         } finally {
677                                 if (stream != null)
678                                         stream.Close ();
679
680                                 DeleteFile (path);
681                         }
682
683                         // Truncate / Read
684                         try {
685                                 stream = new FileStream (path, FileMode.Truncate, FileAccess.Read);
686                                 Assert.Fail ("#H1");
687                         } catch (ArgumentException ex) {
688                                 // make sure it is exact this exception, and not a derived type
689                                 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#H2");
690                         } finally {
691                                 if (stream != null)
692                                         stream.Close ();
693
694                                 DeleteFile (path);
695                         }
696
697                         // Truncate / ReadWrite
698                         try {
699                                 stream = new FileStream (path, FileMode.Truncate, FileAccess.ReadWrite);
700                                 Assert.Fail ("#I1");
701                         } catch (FileNotFoundException ex) {
702                                 // make sure it is exact this exception, and not a derived type
703                                 Assert.AreEqual (typeof (FileNotFoundException), ex.GetType (), "#I2");
704                                 Assert.AreEqual (path, ex.FileName, "#I3");
705                                 Assert.IsNull (ex.InnerException, "#I4");
706                                 Assert.IsNotNull (ex.Message, "#I5");
707                                 Assert.IsTrue (ex.Message.IndexOf (path) != -1, "#I6");
708                         } finally {
709                                 if (stream != null)
710                                         stream.Close ();
711
712                                 DeleteFile (path);
713                         }
714
715                         // Truncate / Write
716                         try {
717                                 stream = new FileStream (path, FileMode.Truncate, FileAccess.Write);
718                                 Assert.Fail ("#J1");
719                         } catch (FileNotFoundException ex) {
720                                 // make sure it is exact this exception, and not a derived type
721                                 Assert.AreEqual (typeof (FileNotFoundException), ex.GetType (), "#J2");
722                                 Assert.AreEqual (path, ex.FileName, "#J3");
723                                 Assert.IsNull (ex.InnerException, "#J4");
724                                 Assert.IsNotNull (ex.Message, "#J5");
725                                 Assert.IsTrue (ex.Message.IndexOf (path) != -1, "#J6");
726                         } finally {
727                                 if (stream != null)
728                                         stream.Close ();
729
730                                 DeleteFile (path);
731                         }
732                 }
733
734                 [Test, ExpectedException (typeof (IOException))]
735                 public void CtorIOException2 ()
736                 {
737                         FileStream stream = null;
738                         try {
739                                 stream = new FileStream (new IntPtr (Int32.MaxValue), FileAccess.Read);
740                         } finally {
741                                 if (stream != null)
742                                         stream.Close ();
743                         }
744                 }
745
746                 [Test, ExpectedException (typeof (IOException))]
747                 public void CtorIOException ()
748                 {
749                         string path = TempFolder + DSC + "CTorIOException.Test";
750                         FileStream stream = null;
751                         FileStream stream2 = null;
752                         DeleteFile (path);
753
754                         try {
755                                 stream = new FileStream (path, FileMode.CreateNew);
756
757                                 // used by an another process
758                                 stream2 = new FileStream (path, FileMode.OpenOrCreate);
759                         } finally {
760                                 if (stream != null)
761                                         stream.Close ();
762                                 if (stream2 != null)
763                                         stream2.Close ();
764                                 DeleteFile (path);
765                         }
766                 }
767
768                 [Test]
769                 public void CtorAccess1Read2Read ()
770                 {
771                         FileStream fs = null;
772                         FileStream fs2 = null;
773                         string fn = Path.Combine (TempFolder, "temp");
774                         try {
775                                 if (!File.Exists (fn)) {
776                                         TextWriter tw = File.CreateText (fn);
777                                         tw.Write ("FOO");
778                                         tw.Close ();
779                                 }
780                                 fs = new FileStream (fn, FileMode.Open, FileAccess.Read);
781                                 fs2 = new FileStream (fn, FileMode.Open, FileAccess.Read);
782                         } finally {
783                                 if (fs != null)
784                                         fs.Close ();
785                                 if (fs2 != null)
786                                         fs2.Close ();
787                                 if (File.Exists (fn))
788                                         File.Delete (fn);
789                         }
790                 }
791
792                 [Test]
793                 [ExpectedException (typeof (IOException))]
794                 public void CtorAccess1Read2Write ()
795                 {
796                         string fn = Path.Combine (TempFolder, "temp");
797                         FileStream fs1 = null;
798                         FileStream fs2 = null;
799                         try {
800                                 if (!File.Exists (fn)) {
801                                         using (TextWriter tw = File.CreateText (fn)) {
802                                                 tw.Write ("FOO");
803                                         }
804                                 }
805                                 fs1 = new FileStream (fn, FileMode.Open, FileAccess.Read);
806                                 fs2 = new FileStream (fn, FileMode.Create, FileAccess.Write);
807                         } finally {
808                                 if (fs1 != null)
809                                         fs1.Close ();
810                                 if (fs2 != null)
811                                         fs2.Close ();
812                                 if (File.Exists (fn))
813                                         File.Delete (fn);
814                         }
815                 }
816
817                 [Test]
818                 [ExpectedException (typeof (IOException))]
819                 public void CtorAccess1Write2Write ()
820                 {
821                         string fn = Path.Combine (TempFolder, "temp");
822                         FileStream fs1 = null;
823                         FileStream fs2 = null;
824                         try {
825                                 if (File.Exists (fn))
826                                         File.Delete (fn);
827                                 fs1 = new FileStream (fn, FileMode.Create, FileAccess.Write);
828                                 fs2 = new FileStream (fn, FileMode.Create, FileAccess.Write);
829                         } finally {
830                                 if (fs1 != null)
831                                         fs1.Close ();
832                                 if (fs2 != null)
833                                         fs2.Close ();
834                                 if (File.Exists (fn))
835                                         File.Delete (fn);
836                         }
837                 }
838
839                 [Test]
840                 [ExpectedException (typeof (UnauthorizedAccessException))]
841                 public void CtorReadDirectoryAsFile ()
842                 {
843                         FileStream stream = null;
844                         try {
845                                 stream = new FileStream (TempFolder, FileMode.Open, FileAccess.Read);
846                         } finally {
847                                 if (stream != null)
848                                         stream.Close ();
849                         }
850                 }
851
852                 [Test] // bug #79250
853                 public void FileShare_Delete ()
854                 {
855                         string fn = Path.Combine (TempFolder, "temp");
856                         
857                         using (Stream s = new FileStream (fn, FileMode.OpenOrCreate, FileAccess.Write, FileShare.Delete)) {
858                                 s.Write (new byte [1] { 0x5 }, 0, 1);
859                                 File.Delete (fn);
860                         }
861
862                         using (Stream s = new FileStream (fn, FileMode.OpenOrCreate, FileAccess.Write, FileShare.ReadWrite | FileShare.Delete)) {
863                                 s.Write (new byte [1] { 0x5 }, 0, 1);
864                                 File.Delete (fn);
865                         }
866
867                         using (Stream s = new FileStream (fn, FileMode.OpenOrCreate, FileAccess.Write, FileShare.Write | FileShare.Delete)) {
868                                 s.Write (new byte [1] { 0x5 }, 0, 1);
869                                 File.Delete (fn);
870                         }
871
872                         using (Stream s = new FileStream (fn, FileMode.OpenOrCreate, FileAccess.Write, FileShare.Read | FileShare.Delete)) {
873                                 s.Write (new byte [1] { 0x5 }, 0, 1);
874                                 File.Delete (fn);
875                         }
876
877                         using (Stream s = new FileStream (fn, FileMode.OpenOrCreate, FileAccess.Write, FileShare.Inheritable | FileShare.Delete)) {
878                                 s.Write (new byte [1] { 0x5 }, 0, 1);
879                                 File.Delete (fn);
880                         }
881                 }
882
883                 [Test]
884                 public void Write ()
885                 {
886                         string path = TempFolder + DSC + "FileStreamTest.Write";
887
888                         DeleteFile (path);
889
890                         FileStream stream = new FileStream (path, FileMode.CreateNew, FileAccess.ReadWrite, FileShare.ReadWrite, 8);
891
892                         byte[] outbytes = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16 };
893                         byte[] bytes = new byte[15];
894
895                         // Check that the data is flushed when we overflow the buffer
896                         // with a large amount of data
897                         stream.Write (outbytes, 0, 5);
898                         stream.Write (outbytes, 5, 10);
899                         stream.Seek (0, SeekOrigin.Begin);
900
901                         stream.Read (bytes, 0, 15);
902                         for (int i = 0; i < 15; ++i)
903                                 Assert.AreEqual (i + 1, bytes[i], "#1");
904
905                         // Check that the data is flushed when we overflow the buffer
906                         // with a small amount of data
907                         stream.Write (outbytes, 0, 7);
908                         stream.Write (outbytes, 7, 7);
909                         stream.Write (outbytes, 14, 1);
910
911                         stream.Read (bytes, 0, 15);
912                         stream.Seek (15, SeekOrigin.Begin);
913                         for (int i = 0; i < 15; ++i)
914                                 Assert.AreEqual (i + 1, bytes[i], "#2");
915                         stream.Close ();
916                 }
917
918                 [Test]
919                 public void Length ()
920                 {
921                         // Test that the Length property takes into account the data
922                         // in the buffer
923                         string path = TempFolder + DSC + "FileStreamTest.Length";
924
925                         DeleteFile (path);
926
927                         FileStream stream = new FileStream (path, FileMode.CreateNew);
928
929                         byte[] outbytes = new byte[] { 1, 2, 3, 4 };
930
931                         stream.Write (outbytes, 0, 4);
932                         Assert.AreEqual (4, stream.Length);
933                         stream.Close ();
934                 }
935
936                 [Test]
937                 public void Flush ()
938                 {
939                         string path = TempFolder + DSC + "FileStreamTest.Flush";
940                         FileStream stream = null;
941                         FileStream stream2 = null;
942
943                         DeleteFile (path);
944
945                         try {
946                                 stream = new FileStream (path, FileMode.CreateNew, FileAccess.ReadWrite, FileShare.ReadWrite);
947                                 stream2 = new FileStream (path, FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite);
948
949                                 stream.Write (new byte[] { 1, 2, 3, 4, 5 }, 0, 5);
950
951                                 byte[] bytes = new byte[5];
952                                 stream2.Read (bytes, 0, 5);
953                                 Assert.AreEqual (0, bytes[0], "#A1");
954                                 Assert.AreEqual (0, bytes[1], "#A2");
955                                 Assert.AreEqual (0, bytes[2], "#A3");
956                                 Assert.AreEqual (0, bytes[3], "#A4");
957
958                                 stream.Flush ();
959                                 stream2.Read (bytes, 0, 5);
960                                 Assert.AreEqual (1, bytes[0], "#B1");
961                                 Assert.AreEqual (2, bytes[1], "#B2");
962                                 Assert.AreEqual (3, bytes[2], "#B3");
963                                 Assert.AreEqual (4, bytes[3], "#B4");
964                         } finally {
965                                 if (stream != null)
966                                         stream.Close ();
967                                 if (stream2 != null)
968                                         stream2.Close ();
969
970                                 DeleteFile (path);
971                         }
972                 }
973
974                 public void TestDefaultProperties ()
975                 {
976                         string path = TempFolder + Path.DirectorySeparatorChar + "testfilestream.tmp.2";
977                         DeleteFile (path);
978
979                         FileStream stream = new FileStream (path, FileMode.Create);
980
981                         Assert.IsTrue (stream.CanRead, "#A1");
982                         Assert.IsTrue (stream.CanSeek, "#A2");
983                         Assert.IsTrue (stream.CanWrite, "#A3");
984                         Assert.IsFalse (stream.IsAsync, "#A4");
985                         Assert.IsTrue (stream.Name.EndsWith (path), "#A5");
986                         Assert.AreEqual (0, stream.Position, "#A6");
987                         Assert.AreEqual ("System.IO.FileStream", stream.ToString (), "#A7");
988                         stream.Close ();
989                         DeleteFile (path);
990
991                         stream = new FileStream (path, FileMode.OpenOrCreate, FileAccess.Read);
992                         Assert.IsTrue (stream.CanRead, "#B1");
993                         Assert.IsTrue (stream.CanSeek, "#B2");
994                         Assert.IsFalse (stream.CanWrite, "#B3");
995                         Assert.IsFalse (stream.IsAsync, "#B4");
996                         Assert.IsTrue (stream.Name.EndsWith (path), "#B5");
997                         Assert.AreEqual (0, stream.Position, "#B6");
998                         Assert.AreEqual ("System.IO.FileStream", stream.ToString (), "#B7");
999                         stream.Close ();
1000
1001                         stream = new FileStream (path, FileMode.Truncate, FileAccess.Write, FileShare.ReadWrite);
1002                         Assert.IsFalse (stream.CanRead, "#C1");
1003                         Assert.IsTrue (stream.CanSeek, "#C2");
1004                         Assert.IsTrue (stream.CanWrite, "#C3");
1005                         Assert.IsFalse (stream.IsAsync, "#C4");
1006                         Assert.IsTrue (stream.Name.EndsWith ("testfilestream.tmp.2"), "#C5");
1007                         Assert.AreEqual (0, stream.Position, "#C6");
1008                         Assert.AreEqual ("System.IO.FileStream", stream.ToString (), "#C7");
1009                         stream.Close ();
1010                         DeleteFile (path);
1011                 }
1012
1013                 [Category ("NotWorking")]
1014                 // Bug: 71371 -> duplicate and WONTFIX.
1015                 public void TestLock_FailsOnMono ()
1016                 {
1017                         string path = TempFolder + Path.DirectorySeparatorChar + "TestLock";
1018                         DeleteFile (path);
1019
1020                         FileStream stream = new FileStream (path, FileMode.CreateNew, FileAccess.ReadWrite);
1021
1022                         stream.Write (new Byte[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }, 0, 10);
1023                         stream.Close ();
1024
1025                         stream = new FileStream (path, FileMode.Open, FileAccess.ReadWrite);
1026
1027                         stream.Lock (0, 5);
1028
1029                         FileStream stream2 = new FileStream (path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
1030
1031                         byte[] bytes = new byte[5];
1032                         try {
1033                                 stream2.Read (bytes, 0, 5);
1034                                 Assert.Fail ("#1");
1035                         } catch (Exception e) {
1036                                 Assert.AreEqual (typeof (IOException), e.GetType (), "#2");
1037                         }
1038
1039                         stream.Close ();
1040                         stream2.Close ();
1041
1042                         DeleteFile (path);
1043                 }
1044
1045                 public void TestLock ()
1046                 {
1047                         string path = TempFolder + Path.DirectorySeparatorChar + "TestLock";
1048                         DeleteFile (path);
1049
1050                         FileStream stream = new FileStream (path, FileMode.CreateNew, FileAccess.ReadWrite);
1051
1052                         stream.Write (new Byte[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }, 0, 10);
1053                         stream.Close ();
1054
1055                         stream = new FileStream (path, FileMode.Open, FileAccess.ReadWrite);
1056
1057                         stream.Lock (0, 5);
1058
1059                         FileStream stream2 = new FileStream (path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
1060
1061                         byte[] bytes = new byte[5];
1062                         try {
1063                                 stream2.Read (bytes, 0, 5);
1064                                 Assert.Fail ("#A1");
1065                         } catch (Exception) {
1066                                 // Bug #71371: on MS.NET you get an IOException detailing a lock
1067                                 // Assert.AreEqual (typeof (IOException), e.GetType (), "#A2");
1068                         }
1069
1070                         stream2.Seek (5, SeekOrigin.Begin);
1071                         stream2.Read (bytes, 0, 5);
1072
1073                         Assert.AreEqual (5, bytes[0], "#B1");
1074                         Assert.AreEqual (6, bytes[1], "#B2");
1075                         Assert.AreEqual (7, bytes[2], "#B3");
1076                         Assert.AreEqual (8, bytes[3], "#B4");
1077                         Assert.AreEqual (9, bytes[4], "#B5");
1078
1079                         stream.Unlock (0, 5);
1080                         stream2.Seek (0, SeekOrigin.Begin);
1081                         stream2.Read (bytes, 0, 5);
1082
1083                         Assert.AreEqual (0, bytes[0], "#C1");
1084                         Assert.AreEqual (1, bytes[1], "#C2");
1085                         Assert.AreEqual (2, bytes[2], "#C3");
1086                         Assert.AreEqual (3, bytes[3], "#C4");
1087                         Assert.AreEqual (4, bytes[4], "#C5");
1088
1089                         stream.Close ();
1090                         stream2.Close ();
1091
1092                         DeleteFile (path);
1093                 }
1094
1095                 [Test]
1096                 public void Seek ()
1097                 {
1098                         string path = TempFolder + DSC + "FST.Seek.Test";
1099                         DeleteFile (path);
1100
1101                         FileStream stream = new FileStream (path, FileMode.CreateNew, FileAccess.ReadWrite, FileShare.ReadWrite);
1102                         FileStream stream2 = new FileStream (path, FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite);
1103
1104                         stream.Write (new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 10 }, 0, 9);
1105                         Assert.AreEqual (5, stream2.Seek (5, SeekOrigin.Begin), "#1");
1106                         Assert.AreEqual (-1, stream2.ReadByte (), "#2");
1107
1108                         Assert.AreEqual (2, stream2.Seek (-3, SeekOrigin.Current), "#3");
1109                         Assert.AreEqual (-1, stream2.ReadByte (), "#4");
1110
1111                         Assert.AreEqual (12, stream.Seek (3, SeekOrigin.Current), "#5");
1112                         Assert.AreEqual (-1, stream.ReadByte (), "#6");
1113
1114                         Assert.AreEqual (5, stream.Seek (-7, SeekOrigin.Current), "#7");
1115                         Assert.AreEqual (6, stream.ReadByte (), "#8");
1116
1117                         Assert.AreEqual (5, stream2.Seek (5, SeekOrigin.Begin), "#9");
1118                         Assert.AreEqual (6, stream2.ReadByte (), "#10");
1119
1120                         stream.Close ();
1121                         stream2.Close ();
1122
1123                         DeleteFile (path);
1124                 }
1125
1126                 public void TestSeek ()
1127                 {
1128                         string path = TempFolder + Path.DirectorySeparatorChar + "TestSeek";
1129                         DeleteFile (path);
1130
1131                         FileStream stream = new FileStream (path, FileMode.CreateNew, FileAccess.ReadWrite, FileShare.ReadWrite);
1132                         stream.Write (new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }, 0, 10);
1133
1134                         stream.Seek (5, SeekOrigin.End);
1135                         Assert.AreEqual (-1, stream.ReadByte (), "#1");
1136
1137                         stream.Seek (-5, SeekOrigin.End);
1138                         Assert.AreEqual (6, stream.ReadByte (), "#2");
1139
1140                         try {
1141                                 stream.Seek (-11, SeekOrigin.End);
1142                                 Assert.Fail ("#3");
1143                         } catch (Exception e) {
1144                                 Assert.AreEqual (typeof (IOException), e.GetType (), "#4");
1145                         }
1146
1147                         stream.Seek (19, SeekOrigin.Begin);
1148                         Assert.AreEqual (-1, stream.ReadByte (), "#5");
1149
1150                         stream.Seek (1, SeekOrigin.Begin);
1151                         Assert.AreEqual (2, stream.ReadByte (), "#6");
1152
1153                         stream.Seek (3, SeekOrigin.Current);
1154                         Assert.AreEqual (6, stream.ReadByte (), "#7");
1155
1156                         stream.Seek (-2, SeekOrigin.Current);
1157                         Assert.AreEqual (5, stream.ReadByte (), "#8");
1158
1159                         stream.Flush ();
1160
1161                         // Test that seeks work correctly when seeking inside the buffer
1162                         stream.Seek (0, SeekOrigin.Begin);
1163                         stream.WriteByte (0);
1164                         stream.WriteByte (1);
1165                         stream.Seek (0, SeekOrigin.Begin);
1166                         byte[] buf = new byte[1];
1167                         buf[0] = 2;
1168                         stream.Write (buf, 0, 1);
1169                         stream.Write (buf, 0, 1);
1170                         stream.Flush ();
1171                         stream.Seek (0, SeekOrigin.Begin);
1172                         Assert.AreEqual (2, stream.ReadByte (), "#9");
1173                         Assert.AreEqual (2, stream.ReadByte (), "#10");
1174
1175                         stream.Close ();
1176
1177                         DeleteFile (path);
1178                 }
1179
1180                 public void TestClose ()
1181                 {
1182                         string path = TempFolder + Path.DirectorySeparatorChar + "TestClose";
1183                         DeleteFile (path);
1184
1185                         FileStream stream = new FileStream (path, FileMode.CreateNew, FileAccess.ReadWrite);
1186
1187                         stream.Write (new byte[] { 1, 2, 3, 4 }, 0, 4);
1188                         stream.ReadByte ();
1189                         stream.Close ();
1190
1191                         try {
1192                                 stream.ReadByte ();
1193                                 Assert.Fail ("#A1");
1194                         } catch (Exception e) {
1195                                 Assert.AreEqual (typeof (ObjectDisposedException), e.GetType (), "#A2");
1196                         }
1197
1198                         try {
1199                                 stream.WriteByte (64);
1200                                 Assert.Fail ("#B1");
1201                         } catch (Exception e) {
1202                                 Assert.AreEqual (typeof (ObjectDisposedException), e.GetType (), "#B2");
1203                         }
1204
1205                         try {
1206                                 stream.Flush ();
1207                                 Assert.Fail ("#C1");
1208                         } catch (Exception e) {
1209                                 Assert.AreEqual (typeof (ObjectDisposedException), e.GetType (), "#C2");
1210                         }
1211
1212                         try {
1213                                 long l = stream.Length;
1214                                 Assert.Fail ("#D1");
1215                         } catch (Exception e) {
1216                                 Assert.AreEqual (typeof (ObjectDisposedException), e.GetType (), "#D2");
1217                         }
1218
1219                         try {
1220                                 long l = stream.Position;
1221                                 Assert.Fail ("#E1");
1222                         } catch (Exception e) {
1223                                 Assert.AreEqual (typeof (ObjectDisposedException), e.GetType (), "#E2");
1224                         }
1225
1226                         Assert.IsFalse (stream.CanRead, "#F1");
1227                         Assert.IsFalse (stream.CanSeek, "#F2");
1228                         Assert.IsFalse (stream.CanWrite, "#F3");
1229                         Assert.IsTrue (stream.Name.EndsWith (path), "#F4");
1230
1231                         DeleteFile (path);
1232                 }
1233
1234
1235                 /// <summary>
1236                 /// Checks whether the <see cref="FileStream" /> throws a <see cref="NotSupportedException" />
1237                 /// when the stream is opened with access mode <see cref="FileAccess.Read" /> and the
1238                 /// <see cref="FileStream.Write(byte[], int, int)" /> method is called.
1239                 /// </summary>
1240                 [Test]
1241                 [ExpectedException (typeof (NotSupportedException))]
1242                 public void TestWriteVerifyAccessMode ()
1243                 {
1244                         string path = TempFolder + Path.DirectorySeparatorChar + "temp";
1245                         DeleteFile (path);
1246
1247                         FileStream stream = null;
1248                         byte[] buffer;
1249
1250                         try {
1251                                 buffer = Encoding.ASCII.GetBytes ("test");
1252                                 stream = new FileStream (path, FileMode.OpenOrCreate, FileAccess.Read);
1253                                 stream.Write (buffer, 0, buffer.Length);
1254                         } finally {
1255                                 if (stream != null)
1256                                         stream.Close ();
1257                                 DeleteFile (path);
1258                         }
1259                 }
1260
1261                 /// <summary>
1262                 /// Checks whether the <see cref="FileStream" /> throws a <see cref="NotSupportedException" />
1263                 /// when the stream is opened with access mode <see cref="FileAccess.Read" /> and the
1264                 /// <see cref="FileStream.WriteByte(byte)" /> method is called.
1265                 /// </summary>
1266                 [Test]
1267                 [ExpectedException (typeof (NotSupportedException))]
1268                 public void TestWriteByteVerifyAccessMode ()
1269                 {
1270                         string path = TempFolder + Path.DirectorySeparatorChar + "temp";
1271                         DeleteFile (path);
1272
1273                         FileStream stream = null;
1274
1275                         try {
1276                                 stream = new FileStream (path, FileMode.OpenOrCreate, FileAccess.Read);
1277                                 stream.WriteByte (Byte.MinValue);
1278                         } finally {
1279                                 if (stream != null)
1280                                         stream.Close ();
1281                                 DeleteFile (path);
1282                         }
1283                 }
1284
1285                 /// <summary>
1286                 /// Checks whether the <see cref="FileStream" /> throws a <see cref="NotSupportedException" />
1287                 /// when the stream is opened with access mode <see cref="FileAccess.Write" /> and the
1288                 /// <see cref="FileStream.Read(byte[], int, int)" /> method is called.
1289                 /// </summary>
1290                 [Test]
1291                 [ExpectedException (typeof (NotSupportedException))]
1292                 public void TestReadVerifyAccessMode ()
1293                 {
1294                         string path = TempFolder + Path.DirectorySeparatorChar + "temp";
1295                         DeleteFile (path);
1296
1297                         FileStream stream = null;
1298                         byte[] buffer = new byte[100];
1299
1300                         try {
1301                                 stream = new FileStream (path, FileMode.OpenOrCreate, FileAccess.Write);
1302                                 stream.Read (buffer, 0, buffer.Length);
1303                         } finally {
1304                                 if (stream != null)
1305                                         stream.Close ();
1306                         }
1307                 }
1308
1309                 /// <summary>
1310                 /// Checks whether the <see cref="FileStream" /> throws a <see cref="NotSupportedException" />
1311                 /// when the stream is opened with access mode <see cref="FileAccess.Write" /> and the
1312                 /// <see cref="FileStream.ReadByte()" /> method is called.
1313                 /// </summary>
1314                 [Test]
1315                 [ExpectedException (typeof (NotSupportedException))]
1316                 public void TestReadByteVerifyAccessMode ()
1317                 {
1318                         string path = TempFolder + Path.DirectorySeparatorChar + "temp";
1319                         DeleteFile (path);
1320
1321                         FileStream stream = null;
1322
1323                         try {
1324                                 stream = new FileStream (path, FileMode.OpenOrCreate, FileAccess.Write);
1325                                 int readByte = stream.ReadByte ();
1326                         } finally {
1327                                 if (stream != null)
1328                                         stream.Close ();
1329                                 DeleteFile (path);
1330                         }
1331                 }
1332
1333                 // Check that the stream is flushed even when it doesn't own the
1334                 // handle
1335                 [Test]
1336                 public void TestFlushNotOwningHandle ()
1337                 {
1338                         string path = Path.Combine (TempFolder, "TestFlushNotOwningHandle");
1339                         DeleteFile (path);
1340
1341                         FileStream s = new FileStream (path, FileMode.Create);
1342                         using (FileStream s2 = new FileStream (s.Handle, FileAccess.Write, false)) {
1343                                 byte[] buf = new byte[2];
1344                                 buf[0] = (int) '1';
1345                                 s2.Write (buf, 0, 1);
1346                         }
1347
1348                         s.Position = 0;
1349                         Assert.AreEqual ((int) '1', s.ReadByte ());
1350                         s.Close ();
1351                 }
1352
1353                 private void DeleteFile (string path)
1354                 {
1355                         if (File.Exists (path))
1356                                 File.Delete (path);
1357                 }
1358
1359                 [Test]
1360                 [ExpectedException (typeof (ArgumentOutOfRangeException))]
1361                 public void Read_OffsetNegative ()
1362                 {
1363                         string path = TempFolder + Path.DirectorySeparatorChar + "temp";
1364                         DeleteFile (path);
1365
1366                         using (FileStream stream = new FileStream (path, FileMode.OpenOrCreate, FileAccess.Read)) {
1367                                 stream.Read (new byte[0], -1, 1);
1368                         }
1369                 }
1370
1371                 [Test]
1372                 [ExpectedException (typeof (ArgumentException))]
1373                 public void Read_OffsetOverflow ()
1374                 {
1375                         string path = TempFolder + Path.DirectorySeparatorChar + "temp";
1376                         DeleteFile (path);
1377
1378                         using (FileStream stream = new FileStream (path, FileMode.OpenOrCreate, FileAccess.Read)) {
1379                                 stream.Read (new byte[0], Int32.MaxValue, 1);
1380                         }
1381                 }
1382
1383                 [Test]
1384                 [ExpectedException (typeof (ArgumentOutOfRangeException))]
1385                 public void Read_CountNegative ()
1386                 {
1387                         string path = TempFolder + Path.DirectorySeparatorChar + "temp";
1388                         DeleteFile (path);
1389
1390                         using (FileStream stream = new FileStream (path, FileMode.OpenOrCreate, FileAccess.Read)) {
1391                                 stream.Read (new byte[0], 1, -1);
1392                         }
1393                 }
1394
1395                 [Test]
1396                 [ExpectedException (typeof (ArgumentException))]
1397                 public void Read_CountOverflow ()
1398                 {
1399                         string path = TempFolder + Path.DirectorySeparatorChar + "temp";
1400                         DeleteFile (path);
1401
1402                         using (FileStream stream = new FileStream (path, FileMode.OpenOrCreate, FileAccess.Read)) {
1403                                 stream.Read (new byte[0], 1, Int32.MaxValue);
1404                         }
1405                 }
1406
1407                 [Test]
1408                 [ExpectedException (typeof (ArgumentOutOfRangeException))]
1409                 public void Write_OffsetNegative ()
1410                 {
1411                         string path = TempFolder + Path.DirectorySeparatorChar + "temp";
1412                         DeleteFile (path);
1413
1414                         using (FileStream stream = new FileStream (path, FileMode.OpenOrCreate, FileAccess.Write)) {
1415                                 stream.Write (new byte[0], -1, 1);
1416                         }
1417                 }
1418
1419                 [Test]
1420                 [ExpectedException (typeof (ArgumentException))]
1421                 public void Write_OffsetOverflow ()
1422                 {
1423                         string path = TempFolder + Path.DirectorySeparatorChar + "temp";
1424                         DeleteFile (path);
1425
1426                         using (FileStream stream = new FileStream (path, FileMode.OpenOrCreate, FileAccess.Write)) {
1427                                 stream.Write (new byte[0], Int32.MaxValue, 1);
1428                         }
1429                 }
1430
1431                 [Test]
1432                 [ExpectedException (typeof (ArgumentOutOfRangeException))]
1433                 public void Write_CountNegative ()
1434                 {
1435                         string path = TempFolder + Path.DirectorySeparatorChar + "temp";
1436                         DeleteFile (path);
1437
1438                         using (FileStream stream = new FileStream (path, FileMode.OpenOrCreate, FileAccess.Write)) {
1439                                 stream.Write (new byte[0], 1, -1);
1440                         }
1441                 }
1442
1443                 [Test]
1444                 [ExpectedException (typeof (ArgumentException))]
1445                 public void Write_CountOverflow ()
1446                 {
1447                         string path = TempFolder + Path.DirectorySeparatorChar + "temp";
1448                         DeleteFile (path);
1449
1450                         using (FileStream stream = new FileStream (path, FileMode.OpenOrCreate, FileAccess.Write)) {
1451                                 stream.Write (new byte[0], 1, Int32.MaxValue);
1452                         }
1453                 }
1454
1455                 [Test]
1456                 [ExpectedException (typeof (ArgumentException))]
1457                 public void Seek_InvalidSeekOrigin ()
1458                 {
1459                         string path = TempFolder + Path.DirectorySeparatorChar + "temp";
1460                         DeleteFile (path);
1461
1462                         using (FileStream stream = new FileStream (path, FileMode.OpenOrCreate, FileAccess.Read)) {
1463                                 stream.Seek (0, (SeekOrigin) (-1));
1464                         }
1465                 }
1466
1467                 [Test]
1468                 [ExpectedException (typeof (ArgumentException))]
1469                 public void Constructor_InvalidFileHandle ()
1470                 {
1471                         new FileStream ((IntPtr) (-1L), FileAccess.Read);
1472                 }
1473
1474                 [Test]
1475                 public void PositionAfterSetLength ()
1476                 {
1477                         string path = TempFolder + Path.DirectorySeparatorChar + "temp";
1478                         DeleteFile (path);
1479
1480                         using (FileStream stream = new FileStream (path, FileMode.OpenOrCreate, FileAccess.Write)) {
1481                                 stream.SetLength (32);
1482                                 stream.Position = 32;
1483                                 stream.SetLength (16);
1484                                 Assert.AreEqual (16, stream.Position);
1485                         }
1486                 }
1487
1488                 [Test]
1489                 [ExpectedException (typeof (ObjectDisposedException))]
1490                 public void SetLength_Disposed ()
1491                 {
1492                         string path = TempFolder + Path.DirectorySeparatorChar + "temp";
1493                         DeleteFile (path);
1494                         FileStream stream = new FileStream (path, FileMode.OpenOrCreate, FileAccess.Write);
1495                         stream.Close ();
1496                         stream.SetLength (16);
1497                 }
1498
1499                 [Test]
1500                 [ExpectedException (typeof (ObjectDisposedException))]
1501                 public void Position_Disposed ()
1502                 {
1503                         string path = TempFolder + Path.DirectorySeparatorChar + "temp";
1504                         DeleteFile (path);
1505                         FileStream stream = new FileStream (path, FileMode.OpenOrCreate, FileAccess.Read);
1506                         stream.Close ();
1507                         stream.Position = 0;
1508                 }
1509
1510                 [Test]
1511                 [ExpectedException (typeof (ObjectDisposedException))]
1512                 public void BeginRead_Disposed ()
1513                 {
1514                         string path = TempFolder + Path.DirectorySeparatorChar + "temp";
1515                         DeleteFile (path);
1516                         FileStream stream = new FileStream (path, FileMode.OpenOrCreate, FileAccess.Read);
1517                         stream.Close ();
1518                         stream.EndRead (stream.BeginRead (new byte[8], 0, 8, null, null));
1519                 }
1520
1521                 [Test]
1522                 [ExpectedException (typeof (ObjectDisposedException))]
1523                 public void BeginWrite_Disposed ()
1524                 {
1525                         string path = TempFolder + Path.DirectorySeparatorChar + "temp";
1526                         DeleteFile (path);
1527                         FileStream stream = new FileStream (path, FileMode.OpenOrCreate, FileAccess.Write);
1528                         stream.Close ();
1529                         stream.EndWrite (stream.BeginWrite (new byte[8], 0, 8, null, null));
1530                 }
1531
1532                 static IAsyncResult DoBeginWrite(Stream stream, ManualResetEvent mre, byte[] RandomBuffer)
1533                 {
1534                         return stream.BeginWrite (RandomBuffer, 0, RandomBuffer.Length, ar => {
1535                                 IAsyncResult begin_write_recursive_ares;
1536
1537                                 try {
1538                                         stream.EndWrite (ar);
1539
1540                                         // we don't supply an ManualResetEvent so this will throw an NRE on the second run
1541                                         // which nunit-console will ignore (but other test runners don't like that)
1542                                         if (mre == null)
1543                                                 return;
1544
1545                                         begin_write_recursive_ares = DoBeginWrite (stream, null, RandomBuffer);
1546                                         begin_write_recursive_ares.AsyncWaitHandle.WaitOne ();
1547
1548                                         mre.Set ();
1549                                 } catch (ObjectDisposedException e) {
1550                                         Console.WriteLine ("stream was disposed: {0}", e);
1551                                 }
1552                         }, null);
1553                 }
1554
1555                 [Test]
1556                 public void BeginWrite_Recursive ()
1557                 {
1558                         string path = TempFolder + Path.DirectorySeparatorChar + "temp";
1559                         DeleteFile (path);
1560
1561                         IAsyncResult begin_write_ares = null;
1562
1563                         try {
1564                                 using (FileStream stream = new FileStream (path, FileMode.OpenOrCreate, FileAccess.Write)) {
1565                                         var mre = new ManualResetEvent (false);
1566                                         var RandomBuffer = new byte[1024];
1567                                         begin_write_ares = DoBeginWrite (stream, mre, RandomBuffer);
1568                                         Assert.IsTrue (mre.WaitOne (5000), "#1");
1569                                 }
1570                         } finally {
1571                                 if (begin_write_ares != null)
1572                                         begin_write_ares.AsyncWaitHandle.WaitOne ();
1573                         }
1574                 }
1575
1576                 [Test]
1577                 [ExpectedException (typeof (ObjectDisposedException))]
1578                 public void Lock_Disposed ()
1579                 {
1580                         string path = TempFolder + Path.DirectorySeparatorChar + "temp";
1581                         DeleteFile (path);
1582                         FileStream stream = new FileStream (path, FileMode.OpenOrCreate, FileAccess.Write);
1583                         stream.Close ();
1584                         stream.Lock (0, 1);
1585                 }
1586
1587                 [Test]
1588                 [ExpectedException (typeof (ObjectDisposedException))]
1589                 public void Unlock_Disposed ()
1590                 {
1591                         string path = TempFolder + Path.DirectorySeparatorChar + "temp";
1592                         DeleteFile (path);
1593                         FileStream stream = new FileStream (path, FileMode.OpenOrCreate, FileAccess.Write);
1594                         stream.Close ();
1595                         stream.Unlock (0, 1);
1596                 }
1597
1598                 [Test]
1599                 public void ReadBytePastEndOfStream ()
1600                 {
1601                         string path = TempFolder + Path.DirectorySeparatorChar + "temp";
1602                         DeleteFile (path);
1603                         using (FileStream stream = new FileStream (path, FileMode.OpenOrCreate, FileAccess.Read)) {
1604                                 stream.Seek (0, SeekOrigin.End);
1605                                 Assert.AreEqual (-1, stream.ReadByte ());
1606                                 stream.Close ();
1607                         }
1608                 }
1609
1610                 [Test]
1611                 [ExpectedException (typeof (NotSupportedException))]
1612                 public void SetLengthWithClosedBaseStream ()
1613                 {
1614                         string fn = Path.Combine (TempFolder, "temp");
1615                         try {
1616                                 FileStream fs = new FileStream (fn, FileMode.Create);
1617                                 BufferedStream bs = new BufferedStream (fs);
1618                                 fs.Close ();
1619
1620                                 bs.SetLength (1000);
1621                         } finally {
1622                                 File.Delete (fn);
1623                         }
1624                 }
1625
1626                 [Test]
1627                 public void LengthAfterWrite ()
1628                 {
1629                         string path = TempFolder + DSC + "oneofthefilescreated.txt";
1630                         FileStream fs = null;
1631                         DeleteFile (path);
1632                         try {
1633                                 fs = new FileStream (path, FileMode.CreateNew);
1634                                 fs.WriteByte (Convert.ToByte ('A'));
1635                                 byte [] buffer = Encoding.ASCII.GetBytes (" is a first character.");
1636                                 fs.Write (buffer, 0, buffer.Length);
1637                                 fs.Seek (0, SeekOrigin.Begin);
1638                                 char a = Convert.ToChar (fs.ReadByte ());
1639                                 Assert.AreEqual ('A', a, "#A1");
1640                                 Assert.AreEqual (23, fs.Length, "#A2");
1641                                 int nread = fs.Read (buffer, 0, 5);
1642                                 Assert.AreEqual (5, nread, "#A3");
1643                         } finally {
1644                                 if (fs != null)
1645                                         fs.Close ();
1646                                 DeleteFile (path);
1647                         }
1648                 }
1649
1650                 [Test]
1651                 public void DeleteOnClose ()
1652                 {
1653                         string path = TempFolder + DSC + "created.txt";
1654                         DeleteFile (path);
1655                         FileStream fs = new FileStream (path, FileMode.CreateNew, FileAccess.Write, FileShare.None, 1024,
1656                                                         FileOptions.DeleteOnClose);
1657                         Assert.AreEqual (true, File.Exists (path), "DOC#1");
1658                         fs.Close ();
1659                         Assert.AreEqual (false, File.Exists (path), "DOC#2");
1660                         
1661                 }
1662
1663                 [Test]
1664                 public void OpenCharDeviceRepeatedly ()
1665                 {
1666                         // https://bugzilla.xamarin.com/show_bug.cgi?id=38408
1667                         try {
1668                                 using (var f = new FileStream ("/dev/zero", FileMode.Open))
1669                                 {
1670                                 }
1671                         } catch (FileNotFoundException) {
1672                                 // Only run this test on platforms where /dev/zero exists
1673                                 Assert.Ignore();
1674                         }
1675
1676                         // this shouldn't throw
1677                         using (var g = new FileStream ("/dev/zero", FileMode.Open))
1678                         {
1679                         }
1680                 }
1681
1682 #if !MOBILE
1683                 [Test]
1684                 public void WriteWithExposedHandle ()
1685                 {
1686                         string path = TempFolder + DSC + "exposed-handle.txt";
1687                         FileStream fs1 = null;
1688                         FileStream fs2 = null;
1689                         DeleteFile (path);
1690                         
1691                         try {
1692                                 fs1 = new FileStream (path, FileMode.Create);
1693                                 fs2 = new FileStream (fs1.SafeFileHandle, FileAccess.ReadWrite);
1694                                 fs1.WriteByte (Convert.ToByte ('H'));
1695                                 fs1.WriteByte (Convert.ToByte ('E'));
1696                                 fs1.WriteByte (Convert.ToByte ('L'));
1697                                 fs2.WriteByte (Convert.ToByte ('L'));
1698                                 fs2.WriteByte (Convert.ToByte ('O'));
1699                                 long fs1Pos = fs1.Position;
1700                                 fs1.Flush ();
1701                                 fs2.Flush (); 
1702                                 fs1.Close ();
1703                                 fs2.Close ();
1704
1705                                 var check = Encoding.ASCII.GetString (File.ReadAllBytes (path));
1706                                 Assert.AreEqual ("HELLO", check, "EXPOSED#1");
1707                                 Assert.AreEqual (5, fs1Pos, "EXPOSED#2");
1708                         } finally {
1709                                 if (fs1 != null)
1710                                         fs1.Close ();
1711                                 if (fs2 != null)
1712                                         fs2.Close ();
1713                                 DeleteFile (path);
1714                         }
1715                 }
1716
1717                 [Test]
1718                 public void Ctor_InvalidSafeHandle ()
1719                 {
1720                         var sf = new SafeFileHandle (IntPtr.Zero, true);
1721                         try {
1722                                 new FileStream (sf, FileAccess.ReadWrite);
1723                                 Assert.Fail ("#1");
1724                         } catch (ArgumentException) {
1725                         }
1726                 }
1727 #endif
1728         }
1729 }