16d9196ec231b1a0c17a2c47eb351fbb59a21c96
[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                 [Test, ExpectedException (typeof (IOException))]
716                 public void CtorIOException ()
717                 {
718                         string path = TempFolder + DSC + "CTorIOException.Test";
719                         FileStream stream = null;
720                         FileStream stream2 = null;
721                         DeleteFile (path);
722
723                         try {
724                                 stream = new FileStream (path, FileMode.CreateNew);
725
726                                 // used by an another process
727                                 stream2 = new FileStream (path, FileMode.OpenOrCreate);
728                         } finally {
729                                 if (stream != null)
730                                         stream.Close ();
731                                 if (stream2 != null)
732                                         stream2.Close ();
733                                 DeleteFile (path);
734                         }
735                 }
736
737                 [Test]
738                 public void CtorAccess1Read2Read ()
739                 {
740                         FileStream fs = null;
741                         FileStream fs2 = null;
742                         string fn = Path.Combine (TempFolder, "temp");
743                         try {
744                                 if (!File.Exists (fn)) {
745                                         TextWriter tw = File.CreateText (fn);
746                                         tw.Write ("FOO");
747                                         tw.Close ();
748                                 }
749                                 fs = new FileStream (fn, FileMode.Open, FileAccess.Read);
750                                 fs2 = new FileStream (fn, FileMode.Open, FileAccess.Read);
751                         } finally {
752                                 if (fs != null)
753                                         fs.Close ();
754                                 if (fs2 != null)
755                                         fs2.Close ();
756                                 if (File.Exists (fn))
757                                         File.Delete (fn);
758                         }
759                 }
760
761                 [Test]
762                 [ExpectedException (typeof (IOException))]
763                 public void CtorAccess1Read2Write ()
764                 {
765                         string fn = Path.Combine (TempFolder, "temp");
766                         FileStream fs = null;
767                         try {
768                                 if (!File.Exists (fn)) {
769                                         using (TextWriter tw = File.CreateText (fn)) {
770                                                 tw.Write ("FOO");
771                                         }
772                                 }
773                                 fs = new FileStream (fn, FileMode.Open, FileAccess.Read);
774                                 fs = new FileStream (fn, FileMode.Create, FileAccess.Write);
775                         } finally {
776                                 if (fs != null)
777                                         fs.Close ();
778                                 if (File.Exists (fn))
779                                         File.Delete (fn);
780                         }
781                 }
782
783                 [Test]
784                 [ExpectedException (typeof (IOException))]
785                 public void CtorAccess1Write2Write ()
786                 {
787                         string fn = Path.Combine (TempFolder, "temp");
788                         FileStream fs = null;
789                         try {
790                                 if (File.Exists (fn))
791                                         File.Delete (fn);
792                                 fs = new FileStream (fn, FileMode.Create, FileAccess.Write);
793                                 fs = new FileStream (fn, FileMode.Create, FileAccess.Write);
794                         } finally {
795                                 if (fs != null)
796                                         fs.Close ();
797                                 if (File.Exists (fn))
798                                         File.Delete (fn);
799                         }
800                 }
801
802                 [Test]
803                 [ExpectedException (typeof (UnauthorizedAccessException))]
804                 public void CtorReadDirectoryAsFile ()
805                 {
806                         FileStream stream = null;
807                         try {
808                                 stream = new FileStream (TempFolder, FileMode.Open, FileAccess.Read);
809                         } finally {
810                                 if (stream != null)
811                                         stream.Close ();
812                         }
813                 }
814
815                 [Test]
816                 public void Write ()
817                 {
818                         string path = TempFolder + DSC + "FileStreamTest.Write";
819
820                         DeleteFile (path);
821
822                         FileStream stream = new FileStream (path, FileMode.CreateNew, FileAccess.ReadWrite, FileShare.ReadWrite, 8);
823
824                         byte[] outbytes = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16 };
825                         byte[] bytes = new byte[15];
826
827                         // Check that the data is flushed when we overflow the buffer
828                         // with a large amount of data
829                         stream.Write (outbytes, 0, 5);
830                         stream.Write (outbytes, 5, 10);
831                         stream.Seek (0, SeekOrigin.Begin);
832
833                         stream.Read (bytes, 0, 15);
834                         for (int i = 0; i < 15; ++i)
835                                 Assert.AreEqual (i + 1, bytes[i], "#1");
836
837                         // Check that the data is flushed when we overflow the buffer
838                         // with a small amount of data
839                         stream.Write (outbytes, 0, 7);
840                         stream.Write (outbytes, 7, 7);
841                         stream.Write (outbytes, 14, 1);
842
843                         stream.Read (bytes, 0, 15);
844                         stream.Seek (15, SeekOrigin.Begin);
845                         for (int i = 0; i < 15; ++i)
846                                 Assert.AreEqual (i + 1, bytes[i], "#2");
847                         stream.Close ();
848                 }
849
850                 [Test]
851                 public void Length ()
852                 {
853                         // Test that the Length property takes into account the data
854                         // in the buffer
855                         string path = TempFolder + DSC + "FileStreamTest.Length";
856
857                         DeleteFile (path);
858
859                         FileStream stream = new FileStream (path, FileMode.CreateNew);
860
861                         byte[] outbytes = new byte[] { 1, 2, 3, 4 };
862
863                         stream.Write (outbytes, 0, 4);
864                         Assert.AreEqual (4, stream.Length);
865                         stream.Close ();
866                 }
867
868                 [Test]
869                 public void Flush ()
870                 {
871                         string path = TempFolder + DSC + "FileStreamTest.Flush";
872                         FileStream stream = null;
873                         FileStream stream2 = null;
874
875                         DeleteFile (path);
876
877                         try {
878                                 stream = new FileStream (path, FileMode.CreateNew, FileAccess.ReadWrite, FileShare.ReadWrite);
879                                 stream2 = new FileStream (path, FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite);
880
881                                 stream.Write (new byte[] { 1, 2, 3, 4, 5 }, 0, 5);
882
883                                 byte[] bytes = new byte[5];
884                                 stream2.Read (bytes, 0, 5);
885                                 Assert.AreEqual (0, bytes[0], "#A1");
886                                 Assert.AreEqual (0, bytes[1], "#A2");
887                                 Assert.AreEqual (0, bytes[2], "#A3");
888                                 Assert.AreEqual (0, bytes[3], "#A4");
889
890                                 stream.Flush ();
891                                 stream2.Read (bytes, 0, 5);
892                                 Assert.AreEqual (1, bytes[0], "#B1");
893                                 Assert.AreEqual (2, bytes[1], "#B2");
894                                 Assert.AreEqual (3, bytes[2], "#B3");
895                                 Assert.AreEqual (4, bytes[3], "#B4");
896                         } finally {
897                                 if (stream != null)
898                                         stream.Close ();
899                                 if (stream2 != null)
900                                         stream2.Close ();
901
902                                 DeleteFile (path);
903                         }
904                 }
905
906                 public void TestDefaultProperties ()
907                 {
908                         string path = TempFolder + Path.DirectorySeparatorChar + "testfilestream.tmp.2";
909                         DeleteFile (path);
910
911                         FileStream stream = new FileStream (path, FileMode.Create);
912
913                         Assert.IsTrue (stream.CanRead, "#A1");
914                         Assert.IsTrue (stream.CanSeek, "#A2");
915                         Assert.IsTrue (stream.CanWrite, "#A3");
916                         Assert.IsFalse (stream.IsAsync, "#A4");
917                         Assert.IsTrue (stream.Name.EndsWith (path), "#A5");
918                         Assert.AreEqual (0, stream.Position, "#A6");
919                         Assert.AreEqual ("System.IO.FileStream", stream.ToString (), "#A7");
920                         stream.Close ();
921                         DeleteFile (path);
922
923                         stream = new FileStream (path, FileMode.OpenOrCreate, FileAccess.Read);
924                         Assert.IsTrue (stream.CanRead, "#B1");
925                         Assert.IsTrue (stream.CanSeek, "#B2");
926                         Assert.IsFalse (stream.CanWrite, "#B3");
927                         Assert.IsFalse (stream.IsAsync, "#B4");
928                         Assert.IsTrue (stream.Name.EndsWith (path), "#B5");
929                         Assert.AreEqual (0, stream.Position, "#B6");
930                         Assert.AreEqual ("System.IO.FileStream", stream.ToString (), "#B7");
931                         stream.Close ();
932
933                         stream = new FileStream (path, FileMode.Truncate, FileAccess.Write, FileShare.ReadWrite);
934                         Assert.IsFalse (stream.CanRead, "#C1");
935                         Assert.IsTrue (stream.CanSeek, "#C2");
936                         Assert.IsTrue (stream.CanWrite, "#C3");
937                         Assert.IsFalse (stream.IsAsync, "#C4");
938                         Assert.IsTrue (stream.Name.EndsWith ("testfilestream.tmp.2"), "#C5");
939                         Assert.AreEqual (0, stream.Position, "#C6");
940                         Assert.AreEqual ("System.IO.FileStream", stream.ToString (), "#C7");
941                         stream.Close ();
942                         DeleteFile (path);
943                 }
944
945                 [Category ("NotWorking")]
946                 // Bug: 71371 -> duplicate and WONTFIX.
947                 public void TestLock_FailsOnMono ()
948                 {
949                         string path = TempFolder + Path.DirectorySeparatorChar + "TestLock";
950                         DeleteFile (path);
951
952                         FileStream stream = new FileStream (path, FileMode.CreateNew, FileAccess.ReadWrite);
953
954                         stream.Write (new Byte[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }, 0, 10);
955                         stream.Close ();
956
957                         stream = new FileStream (path, FileMode.Open, FileAccess.ReadWrite);
958
959                         stream.Lock (0, 5);
960
961                         FileStream stream2 = new FileStream (path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
962
963                         byte[] bytes = new byte[5];
964                         try {
965                                 stream2.Read (bytes, 0, 5);
966                                 Assert.Fail ("#1");
967                         } catch (Exception e) {
968                                 Assert.AreEqual (typeof (IOException), e.GetType (), "#2");
969                         }
970
971                         stream.Close ();
972                         stream2.Close ();
973
974                         DeleteFile (path);
975                 }
976
977                 public void TestLock ()
978                 {
979                         string path = TempFolder + Path.DirectorySeparatorChar + "TestLock";
980                         DeleteFile (path);
981
982                         FileStream stream = new FileStream (path, FileMode.CreateNew, FileAccess.ReadWrite);
983
984                         stream.Write (new Byte[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }, 0, 10);
985                         stream.Close ();
986
987                         stream = new FileStream (path, FileMode.Open, FileAccess.ReadWrite);
988
989                         stream.Lock (0, 5);
990
991                         FileStream stream2 = new FileStream (path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
992
993                         byte[] bytes = new byte[5];
994                         try {
995                                 stream2.Read (bytes, 0, 5);
996                                 Assert.Fail ("#A1");
997                         } catch (Exception) {
998                                 // Bug #71371: on MS.NET you get an IOException detailing a lock
999                                 // Assert.AreEqual (typeof (IOException), e.GetType (), "#A2");
1000                         }
1001
1002                         stream2.Seek (5, SeekOrigin.Begin);
1003                         stream2.Read (bytes, 0, 5);
1004
1005                         Assert.AreEqual (5, bytes[0], "#B1");
1006                         Assert.AreEqual (6, bytes[1], "#B2");
1007                         Assert.AreEqual (7, bytes[2], "#B3");
1008                         Assert.AreEqual (8, bytes[3], "#B4");
1009                         Assert.AreEqual (9, bytes[4], "#B5");
1010
1011                         stream.Unlock (0, 5);
1012                         stream2.Seek (0, SeekOrigin.Begin);
1013                         stream2.Read (bytes, 0, 5);
1014
1015                         Assert.AreEqual (0, bytes[0], "#C1");
1016                         Assert.AreEqual (1, bytes[1], "#C2");
1017                         Assert.AreEqual (2, bytes[2], "#C3");
1018                         Assert.AreEqual (3, bytes[3], "#C4");
1019                         Assert.AreEqual (4, bytes[4], "#C5");
1020
1021                         stream.Close ();
1022                         stream2.Close ();
1023
1024                         DeleteFile (path);
1025                 }
1026
1027                 [Test]
1028                 public void Seek ()
1029                 {
1030                         string path = TempFolder + DSC + "FST.Seek.Test";
1031                         DeleteFile (path);
1032
1033                         FileStream stream = new FileStream (path, FileMode.CreateNew, FileAccess.ReadWrite, FileShare.ReadWrite);
1034                         FileStream stream2 = new FileStream (path, FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite);
1035
1036                         stream.Write (new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 10 }, 0, 9);
1037                         Assert.AreEqual (5, stream2.Seek (5, SeekOrigin.Begin), "#1");
1038                         Assert.AreEqual (-1, stream2.ReadByte (), "#2");
1039
1040                         Assert.AreEqual (2, stream2.Seek (-3, SeekOrigin.Current), "#3");
1041                         Assert.AreEqual (-1, stream2.ReadByte (), "#4");
1042
1043                         Assert.AreEqual (12, stream.Seek (3, SeekOrigin.Current), "#5");
1044                         Assert.AreEqual (-1, stream.ReadByte (), "#6");
1045
1046                         Assert.AreEqual (5, stream.Seek (-7, SeekOrigin.Current), "#7");
1047                         Assert.AreEqual (6, stream.ReadByte (), "#8");
1048
1049                         Assert.AreEqual (5, stream2.Seek (5, SeekOrigin.Begin), "#9");
1050                         Assert.AreEqual (6, stream2.ReadByte (), "#10");
1051
1052                         stream.Close ();
1053                         stream2.Close ();
1054
1055                         DeleteFile (path);
1056                 }
1057
1058                 public void TestSeek ()
1059                 {
1060                         string path = TempFolder + Path.DirectorySeparatorChar + "TestSeek";
1061                         DeleteFile (path);
1062
1063                         FileStream stream = new FileStream (path, FileMode.CreateNew, FileAccess.ReadWrite, FileShare.ReadWrite);
1064                         stream.Write (new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }, 0, 10);
1065
1066                         stream.Seek (5, SeekOrigin.End);
1067                         Assert.AreEqual (-1, stream.ReadByte (), "#1");
1068
1069                         stream.Seek (-5, SeekOrigin.End);
1070                         Assert.AreEqual (6, stream.ReadByte (), "#2");
1071
1072                         try {
1073                                 stream.Seek (-11, SeekOrigin.End);
1074                                 Assert.Fail ("#3");
1075                         } catch (Exception e) {
1076                                 Assert.AreEqual (typeof (IOException), e.GetType (), "#4");
1077                         }
1078
1079                         stream.Seek (19, SeekOrigin.Begin);
1080                         Assert.AreEqual (-1, stream.ReadByte (), "#5");
1081
1082                         stream.Seek (1, SeekOrigin.Begin);
1083                         Assert.AreEqual (2, stream.ReadByte (), "#6");
1084
1085                         stream.Seek (3, SeekOrigin.Current);
1086                         Assert.AreEqual (6, stream.ReadByte (), "#7");
1087
1088                         stream.Seek (-2, SeekOrigin.Current);
1089                         Assert.AreEqual (5, stream.ReadByte (), "#8");
1090
1091                         stream.Flush ();
1092
1093                         // Test that seeks work correctly when seeking inside the buffer
1094                         stream.Seek (0, SeekOrigin.Begin);
1095                         stream.WriteByte (0);
1096                         stream.WriteByte (1);
1097                         stream.Seek (0, SeekOrigin.Begin);
1098                         byte[] buf = new byte[1];
1099                         buf[0] = 2;
1100                         stream.Write (buf, 0, 1);
1101                         stream.Write (buf, 0, 1);
1102                         stream.Flush ();
1103                         stream.Seek (0, SeekOrigin.Begin);
1104                         Assert.AreEqual (2, stream.ReadByte (), "#9");
1105                         Assert.AreEqual (2, stream.ReadByte (), "#10");
1106
1107                         stream.Close ();
1108
1109                         DeleteFile (path);
1110                 }
1111
1112                 public void TestClose ()
1113                 {
1114                         string path = TempFolder + Path.DirectorySeparatorChar + "TestClose";
1115                         DeleteFile (path);
1116
1117                         FileStream stream = new FileStream (path, FileMode.CreateNew, FileAccess.ReadWrite);
1118
1119                         stream.Write (new byte[] { 1, 2, 3, 4 }, 0, 4);
1120                         stream.ReadByte ();
1121                         stream.Close ();
1122
1123                         try {
1124                                 stream.ReadByte ();
1125                                 Assert.Fail ("#A1");
1126                         } catch (Exception e) {
1127                                 Assert.AreEqual (typeof (ObjectDisposedException), e.GetType (), "#A2");
1128                         }
1129
1130                         try {
1131                                 stream.WriteByte (64);
1132                                 Assert.Fail ("#B1");
1133                         } catch (Exception e) {
1134                                 Assert.AreEqual (typeof (ObjectDisposedException), e.GetType (), "#B2");
1135                         }
1136
1137                         try {
1138                                 stream.Flush ();
1139                                 Assert.Fail ("#C1");
1140                         } catch (Exception e) {
1141                                 Assert.AreEqual (typeof (ObjectDisposedException), e.GetType (), "#C2");
1142                         }
1143
1144                         try {
1145                                 long l = stream.Length;
1146                                 Assert.Fail ("#D1");
1147                         } catch (Exception e) {
1148                                 Assert.AreEqual (typeof (ObjectDisposedException), e.GetType (), "#D2");
1149                         }
1150
1151                         try {
1152                                 long l = stream.Position;
1153                                 Assert.Fail ("#E1");
1154                         } catch (Exception e) {
1155                                 Assert.AreEqual (typeof (ObjectDisposedException), e.GetType (), "#E2");
1156                         }
1157
1158                         Assert.IsFalse (stream.CanRead, "#F1");
1159                         Assert.IsFalse (stream.CanSeek, "#F2");
1160                         Assert.IsFalse (stream.CanWrite, "#F3");
1161                         Assert.IsTrue (stream.Name.EndsWith (path), "#F4");
1162
1163                         DeleteFile (path);
1164                 }
1165
1166
1167                 /// <summary>
1168                 /// Checks whether the <see cref="FileStream" /> throws a <see cref="NotSupportedException" />
1169                 /// when the stream is opened with access mode <see cref="FileAccess.Read" /> and the
1170                 /// <see cref="FileStream.Write(byte[], int, int)" /> method is called.
1171                 /// </summary>
1172                 [Test]
1173                 [ExpectedException (typeof (NotSupportedException))]
1174                 public void TestWriteVerifyAccessMode ()
1175                 {
1176                         string path = TempFolder + Path.DirectorySeparatorChar + "temp";
1177                         DeleteFile (path);
1178
1179                         FileStream stream = null;
1180                         byte[] buffer;
1181
1182                         try {
1183                                 buffer = Encoding.ASCII.GetBytes ("test");
1184                                 stream = new FileStream (path, FileMode.OpenOrCreate, FileAccess.Read);
1185                                 stream.Write (buffer, 0, buffer.Length);
1186                         } finally {
1187                                 if (stream != null)
1188                                         stream.Close ();
1189                                 DeleteFile (path);
1190                         }
1191                 }
1192
1193                 /// <summary>
1194                 /// Checks whether the <see cref="FileStream" /> throws a <see cref="NotSupportedException" />
1195                 /// when the stream is opened with access mode <see cref="FileAccess.Read" /> and the
1196                 /// <see cref="FileStream.WriteByte(byte)" /> method is called.
1197                 /// </summary>
1198                 [Test]
1199                 [ExpectedException (typeof (NotSupportedException))]
1200                 public void TestWriteByteVerifyAccessMode ()
1201                 {
1202                         string path = TempFolder + Path.DirectorySeparatorChar + "temp";
1203                         DeleteFile (path);
1204
1205                         FileStream stream = null;
1206
1207                         try {
1208                                 stream = new FileStream (path, FileMode.OpenOrCreate, FileAccess.Read);
1209                                 stream.WriteByte (Byte.MinValue);
1210                         } finally {
1211                                 if (stream != null)
1212                                         stream.Close ();
1213                                 DeleteFile (path);
1214                         }
1215                 }
1216
1217                 /// <summary>
1218                 /// Checks whether the <see cref="FileStream" /> throws a <see cref="NotSupportedException" />
1219                 /// when the stream is opened with access mode <see cref="FileAccess.Write" /> and the
1220                 /// <see cref="FileStream.Read(byte[], int, int)" /> method is called.
1221                 /// </summary>
1222                 [Test]
1223                 [ExpectedException (typeof (NotSupportedException))]
1224                 public void TestReadVerifyAccessMode ()
1225                 {
1226                         string path = TempFolder + Path.DirectorySeparatorChar + "temp";
1227                         DeleteFile (path);
1228
1229                         FileStream stream = null;
1230                         byte[] buffer = new byte[100];
1231
1232                         try {
1233                                 stream = new FileStream (path, FileMode.OpenOrCreate, FileAccess.Write);
1234                                 stream.Read (buffer, 0, buffer.Length);
1235                         } finally {
1236                                 if (stream != null)
1237                                         stream.Close ();
1238                         }
1239                 }
1240
1241                 /// <summary>
1242                 /// Checks whether the <see cref="FileStream" /> throws a <see cref="NotSupportedException" />
1243                 /// when the stream is opened with access mode <see cref="FileAccess.Write" /> and the
1244                 /// <see cref="FileStream.ReadByte()" /> method is called.
1245                 /// </summary>
1246                 [Test]
1247                 [ExpectedException (typeof (NotSupportedException))]
1248                 public void TestReadByteVerifyAccessMode ()
1249                 {
1250                         string path = TempFolder + Path.DirectorySeparatorChar + "temp";
1251                         DeleteFile (path);
1252
1253                         FileStream stream = null;
1254
1255                         try {
1256                                 stream = new FileStream (path, FileMode.OpenOrCreate, FileAccess.Write);
1257                                 int readByte = stream.ReadByte ();
1258                         } finally {
1259                                 if (stream != null)
1260                                         stream.Close ();
1261                                 DeleteFile (path);
1262                         }
1263                 }
1264
1265 #if !TARGET_JVM // No support IntPtr file handles under TARGET_JVM
1266                 // Check that the stream is flushed even when it doesn't own the
1267                 // handle
1268                 [Test]
1269                 public void TestFlushNotOwningHandle ()
1270                 {
1271                         string path = Path.Combine (TempFolder, "TestFlushNotOwningHandle");
1272                         DeleteFile (path);
1273
1274                         FileStream s = new FileStream (path, FileMode.Create);
1275                         using (FileStream s2 = new FileStream (s.Handle, FileAccess.Write, false)) {
1276                                 byte[] buf = new byte[2];
1277                                 buf[0] = (int) '1';
1278                                 s2.Write (buf, 0, 1);
1279                         }
1280
1281                         s.Position = 0;
1282                         Assert.AreEqual ((int) '1', s.ReadByte ());
1283                         s.Close ();
1284                 }
1285 #endif // TARGET_JVM
1286
1287                 private void DeleteFile (string path)
1288                 {
1289                         if (File.Exists (path))
1290                                 File.Delete (path);
1291                 }
1292
1293                 [Test]
1294                 [ExpectedException (typeof (ArgumentOutOfRangeException))]
1295                 public void Read_OffsetNegative ()
1296                 {
1297                         string path = TempFolder + Path.DirectorySeparatorChar + "temp";
1298                         DeleteFile (path);
1299
1300                         using (FileStream stream = new FileStream (path, FileMode.OpenOrCreate, FileAccess.Read)) {
1301                                 stream.Read (new byte[0], -1, 1);
1302                         }
1303                 }
1304
1305                 [Test]
1306                 [ExpectedException (typeof (ArgumentException))]
1307                 public void Read_OffsetOverflow ()
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], Int32.MaxValue, 1);
1314                         }
1315                 }
1316
1317                 [Test]
1318                 [ExpectedException (typeof (ArgumentOutOfRangeException))]
1319                 public void Read_CountNegative ()
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], 1, -1);
1326                         }
1327                 }
1328
1329                 [Test]
1330                 [ExpectedException (typeof (ArgumentException))]
1331                 public void Read_CountOverflow ()
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, Int32.MaxValue);
1338                         }
1339                 }
1340
1341                 [Test]
1342                 [ExpectedException (typeof (ArgumentOutOfRangeException))]
1343                 public void Write_OffsetNegative ()
1344                 {
1345                         string path = TempFolder + Path.DirectorySeparatorChar + "temp";
1346                         DeleteFile (path);
1347
1348                         using (FileStream stream = new FileStream (path, FileMode.OpenOrCreate, FileAccess.Write)) {
1349                                 stream.Write (new byte[0], -1, 1);
1350                         }
1351                 }
1352
1353                 [Test]
1354                 [ExpectedException (typeof (ArgumentException))]
1355                 public void Write_OffsetOverflow ()
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], Int32.MaxValue, 1);
1362                         }
1363                 }
1364
1365                 [Test]
1366                 [ExpectedException (typeof (ArgumentOutOfRangeException))]
1367                 public void Write_CountNegative ()
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], 1, -1);
1374                         }
1375                 }
1376
1377                 [Test]
1378                 [ExpectedException (typeof (ArgumentException))]
1379                 public void Write_CountOverflow ()
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, Int32.MaxValue);
1386                         }
1387                 }
1388
1389                 [Test]
1390                 [ExpectedException (typeof (ArgumentException))]
1391                 public void Seek_InvalidSeekOrigin ()
1392                 {
1393                         string path = TempFolder + Path.DirectorySeparatorChar + "temp";
1394                         DeleteFile (path);
1395
1396                         using (FileStream stream = new FileStream (path, FileMode.OpenOrCreate, FileAccess.Read)) {
1397                                 stream.Seek (0, (SeekOrigin) (-1));
1398                         }
1399                 }
1400
1401 #if !TARGET_JVM // No support IntPtr file handles under TARGET_JVM
1402                 [Test]
1403                 [ExpectedException (typeof (ArgumentException))]
1404                 public void Constructor_InvalidFileHandle ()
1405                 {
1406                         new FileStream ((IntPtr) (-1L), FileAccess.Read);
1407                 }
1408 #endif // TARGET_JVM
1409
1410                 [Test]
1411                 public void PositionAfterSetLength ()
1412                 {
1413                         string path = TempFolder + Path.DirectorySeparatorChar + "temp";
1414                         DeleteFile (path);
1415
1416                         using (FileStream stream = new FileStream (path, FileMode.OpenOrCreate, FileAccess.Write)) {
1417                                 stream.SetLength (32);
1418                                 stream.Position = 32;
1419                                 stream.SetLength (16);
1420                                 Assert.AreEqual (16, stream.Position);
1421                         }
1422                 }
1423
1424                 [Test]
1425                 [ExpectedException (typeof (ObjectDisposedException))]
1426                 public void SetLength_Disposed ()
1427                 {
1428                         string path = TempFolder + Path.DirectorySeparatorChar + "temp";
1429                         DeleteFile (path);
1430                         FileStream stream = new FileStream (path, FileMode.OpenOrCreate, FileAccess.Write);
1431                         stream.Close ();
1432                         stream.SetLength (16);
1433                 }
1434
1435                 [Test]
1436                 [ExpectedException (typeof (ObjectDisposedException))]
1437                 public void Position_Disposed ()
1438                 {
1439                         string path = TempFolder + Path.DirectorySeparatorChar + "temp";
1440                         DeleteFile (path);
1441                         FileStream stream = new FileStream (path, FileMode.OpenOrCreate, FileAccess.Read);
1442                         stream.Close ();
1443                         stream.Position = 0;
1444                 }
1445
1446                 [Test]
1447                 [ExpectedException (typeof (ObjectDisposedException))]
1448                 public void BeginRead_Disposed ()
1449                 {
1450                         string path = TempFolder + Path.DirectorySeparatorChar + "temp";
1451                         DeleteFile (path);
1452                         FileStream stream = new FileStream (path, FileMode.OpenOrCreate, FileAccess.Read);
1453                         stream.Close ();
1454                         stream.EndRead (stream.BeginRead (new byte[8], 0, 8, null, null));
1455                 }
1456
1457                 [Test]
1458                 [ExpectedException (typeof (ObjectDisposedException))]
1459                 public void BeginWrite_Disposed ()
1460                 {
1461                         string path = TempFolder + Path.DirectorySeparatorChar + "temp";
1462                         DeleteFile (path);
1463                         FileStream stream = new FileStream (path, FileMode.OpenOrCreate, FileAccess.Write);
1464                         stream.Close ();
1465                         stream.EndWrite (stream.BeginWrite (new byte[8], 0, 8, null, null));
1466                 }
1467
1468                 [Test]
1469                 [ExpectedException (typeof (ObjectDisposedException))]
1470                 public void Lock_Disposed ()
1471                 {
1472                         string path = TempFolder + Path.DirectorySeparatorChar + "temp";
1473                         DeleteFile (path);
1474                         FileStream stream = new FileStream (path, FileMode.OpenOrCreate, FileAccess.Write);
1475                         stream.Close ();
1476                         stream.Lock (0, 1);
1477                 }
1478
1479                 [Test]
1480                 [ExpectedException (typeof (ObjectDisposedException))]
1481                 public void Unlock_Disposed ()
1482                 {
1483                         string path = TempFolder + Path.DirectorySeparatorChar + "temp";
1484                         DeleteFile (path);
1485                         FileStream stream = new FileStream (path, FileMode.OpenOrCreate, FileAccess.Write);
1486                         stream.Close ();
1487                         stream.Unlock (0, 1);
1488                 }
1489
1490                 [Test]
1491                 public void ReadBytePastEndOfStream ()
1492                 {
1493                         string path = TempFolder + Path.DirectorySeparatorChar + "temp";
1494                         DeleteFile (path);
1495                         using (FileStream stream = new FileStream (path, FileMode.OpenOrCreate, FileAccess.Read)) {
1496                                 stream.Seek (0, SeekOrigin.End);
1497                                 Assert.AreEqual (-1, stream.ReadByte ());
1498                                 stream.Close ();
1499                         }
1500                 }
1501
1502                 [Test]
1503                 [ExpectedException (typeof (NotSupportedException))]
1504                 public void SetLengthWithClosedBaseStream ()
1505                 {
1506                         string fn = Path.Combine (TempFolder, "temp");
1507                         try {
1508                                 FileStream fs = new FileStream (fn, FileMode.Create);
1509                                 BufferedStream bs = new BufferedStream (fs);
1510                                 fs.Close ();
1511
1512                                 bs.SetLength (1000);
1513                         } finally {
1514                                 File.Delete (fn);
1515                         }
1516                 }
1517
1518                 [Test]
1519                 public void LengthAfterWrite ()
1520                 {
1521                         string path = TempFolder + DSC + "oneofthefilescreated.txt";
1522                         FileStream fs = null;
1523                         DeleteFile (path);
1524                         try {
1525                                 fs = new FileStream (path, FileMode.CreateNew);
1526                                 fs.WriteByte (Convert.ToByte ('A'));
1527                                 byte [] buffer = Encoding.ASCII.GetBytes (" is a first character.");
1528                                 fs.Write (buffer, 0, buffer.Length);
1529                                 fs.Seek (0, SeekOrigin.Begin);
1530                                 char a = Convert.ToChar (fs.ReadByte ());
1531                                 Assert.AreEqual ('A', a, "#A1");
1532                                 Assert.AreEqual (23, fs.Length, "#A2");
1533                                 int nread = fs.Read (buffer, 0, 5);
1534                                 Assert.AreEqual (5, nread, "#A3");
1535                         } finally {
1536                                 if (fs != null)
1537                                         fs.Close ();
1538                                 DeleteFile (path);
1539                         }
1540                 }
1541
1542 #if NET_2_0
1543                 [Test] public void DeleteOnClose ()
1544                 {
1545                         string path = TempFolder + DSC + "created.txt";
1546                         DeleteFile (path);
1547                         FileStream fs = new FileStream (path, FileMode.CreateNew, FileAccess.Write, FileShare.None, 1024,
1548                                                         FileOptions.DeleteOnClose);
1549                         Assert.AreEqual (true, File.Exists (path), "DOC#1");
1550                         fs.Close ();
1551                         Assert.AreEqual (false, File.Exists (path), "DOC#2");
1552                         
1553                 }
1554 #endif
1555         }
1556 }
1557