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