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