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