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