fc3d15f665b3801cfb3984f5b4b3e9af3fa283ca
[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
13 using NUnit.Framework;
14 using System;
15 using System.IO;
16 using System.Text;
17
18 namespace MonoTests.System.IO
19 {
20         public class FileStreamTest : TestCase
21         {
22                 string TempFolder = Path.Combine (Path.GetTempPath (), "MonoTests.System.IO.Tests");
23
24                 public FileStreamTest() 
25                 {
26                         if (Directory.Exists (TempFolder))
27                                 Directory.Delete (TempFolder, true);
28
29                         Directory.CreateDirectory (TempFolder);
30                 }
31
32                 ~FileStreamTest()
33                 {
34                         if (Directory.Exists (TempFolder))
35                                 Directory.Delete (TempFolder, true);
36                 }
37
38                 [SetUp]
39                 protected override void SetUp ()
40                 {
41                         if (!Directory.Exists (TempFolder))                             
42                                 Directory.CreateDirectory (TempFolder);
43                 }
44
45                 [TearDown]
46                 protected override void TearDown ()
47                 {
48                 }
49                 
50                 public void TestCtr ()
51                 {
52                         string path = TempFolder + "/testfilestream.tmp.1";
53                         DeleteFile (path);
54                         FileStream stream = null;
55                         try {
56                                 stream = new FileStream (path, FileMode.Create);
57                         } finally {
58
59                                 if (stream != null)
60                                         stream.Close ();
61                                 DeleteFile (path);                      
62                         }
63                 }
64
65                 [Test]
66                 [ExpectedException (typeof (ArgumentException))]
67                 public void CtorArgumentException1 ()
68                 {
69                         FileStream stream;
70                         stream = new FileStream ("", FileMode.Create);
71                         stream.Close ();
72                 }                       
73
74                 [Test]
75                 [ExpectedException (typeof (ArgumentNullException))]
76                 public void CtorArgumentNullException ()
77                 {
78                         FileStream stream = new FileStream (null, FileMode.Create);
79                         stream.Close ();
80                 }
81
82                 [Test]
83                 [ExpectedException (typeof (FileNotFoundException))]
84                 public void CtorFileNotFoundException1 ()
85                 {
86                         string path = TempFolder + "/thisfileshouldnotexists.test";
87                         DeleteFile (path);
88                         FileStream stream = null;
89                         try {                           
90                                 stream = new FileStream (TempFolder + "/thisfileshouldnotexists.test", FileMode.Open);
91                         } finally {
92                                 if (stream != null)
93                                         stream.Close ();
94                                 DeleteFile (path);
95                         }
96                 }                       
97
98                 [Test]
99                 [ExpectedException (typeof (FileNotFoundException))]
100                 public void CtorFileNotFoundException2 ()
101                 {
102                         string path = TempFolder + "/thisfileshouldNOTexists.test";
103                         DeleteFile (path);
104                         FileStream stream = null;
105
106                         try {
107                                 stream = new FileStream (TempFolder + "/thisfileshouldNOTexists.test", FileMode.Truncate);
108                         } finally {
109                                 if (stream != null)
110                                         stream.Close ();
111
112                                 DeleteFile (path);
113                         }
114                 } 
115
116                 [Test]
117                 [ExpectedException (typeof (IOException))]
118                 public void CtorIOException1 ()
119                 {
120                         string path = TempFolder + "/thisfileshouldexists.test";
121                         FileStream stream = null;
122                         DeleteFile (path);
123                         try {
124                                 stream = new FileStream (path, FileMode.CreateNew);
125                                 stream.Close ();
126                                 stream = null;
127                                 stream = new FileStream (path, FileMode.CreateNew);
128                         } finally {
129                                 
130                                 if (stream != null)
131                                         stream.Close ();
132                                 DeleteFile (path);
133                         } 
134
135                 }
136
137                 [Test]
138                 [ExpectedException (typeof (ArgumentOutOfRangeException))]
139                 public void CtorArgumentOutOfRangeException1 ()
140                 {
141                         FileStream stream = null;
142                         string path = TempFolder + Path.DirectorySeparatorChar + "temp";
143                         DeleteFile (path);
144                         try {
145                                 stream = new FileStream (path, FileMode.Append | FileMode.CreateNew);
146                         } finally {
147                                 if (stream != null)
148                                         stream.Close ();
149                                 DeleteFile (path);
150                         }                       
151                 }                       
152
153                 [Test]
154                 [ExpectedException (typeof (ArgumentOutOfRangeException))]
155                 public void CtorArgumentOutOfRangeException2 ()
156                 {
157                         FileStream stream = null;
158                         string path = TempFolder + Path.DirectorySeparatorChar + "temp";
159                         DeleteFile (path);
160                         try {
161                                 stream = new FileStream ("test.test.test", FileMode.Append | FileMode.Open);
162                         } finally {
163                                 if (stream != null)
164                                         stream.Close ();
165                                 DeleteFile (path);
166                         }                       
167                 }
168
169                 [Test]
170                 [ExpectedException (typeof (DirectoryNotFoundException))]
171                 public void CtorDirectoryNotFoundException ()
172                 {
173                         string path = TempFolder + "/thisDicrectoryShouldNotExists";
174                         if (Directory.Exists (path))
175                                 Directory.Delete (path, true);
176
177                         FileStream stream = null;                               
178                         try {
179                                 stream = new FileStream (path + "/eitherthisfile.test", FileMode.CreateNew);
180                         } finally {
181
182                                 if (stream != null)
183                                         stream.Close ();
184
185                                 if (Directory.Exists (path))
186                                         Directory.Delete (path, true);
187                         }                               
188                 }
189
190                 [Test]
191                 [ExpectedException (typeof (ArgumentOutOfRangeException))]
192                 public void CtorArgumentOutOfRangeException3 ()
193                 {
194                         string path = TempFolder + "/CtorArgumentOutOfRangeException1";
195                         DeleteFile (path);
196                         
197                         FileStream stream = null;
198                         try {
199                                 stream = new FileStream (path, FileMode.CreateNew, FileAccess.Read, FileShare.None | FileShare.Inheritable);
200                         } finally {
201                                 if (stream != null)
202                                         stream.Close ();
203                                 DeleteFile (path);
204                         }
205                 }
206
207                 [Test]
208                 [ExpectedException (typeof (ArgumentOutOfRangeException))]
209                 public void CtorArgumentOutOfRangeException4 ()
210                 {
211                         string path = TempFolder + "/CtorArgumentOutOfRangeException2";
212                         DeleteFile (path);
213
214                         FileStream stream = null;
215                         try {
216                                 stream = new FileStream (path, FileMode.Truncate, FileAccess.Read, FileShare.ReadWrite, -1);
217                         } finally {
218                                 if (stream != null)
219                                         stream.Close ();
220                                 DeleteFile (path);
221                         }
222                 }                       
223                                 
224                 [Test]
225                 [ExpectedException (typeof (ArgumentException))]
226                 public void CtorArgumentException2 ()
227                 {
228                         // FileMode.CreateNew && FileAccess.Read
229
230                         string path = TempFolder + Path.DirectorySeparatorChar + "temp";
231                         FileStream stream = null;
232
233                         DeleteFile (path);
234
235                         try {
236                                 stream = new FileStream (".test.test.test.2", FileMode.CreateNew, FileAccess.Read, FileShare.None | FileShare.Write);
237                         } finally {                             
238
239                                 if (stream != null)
240                                         stream.Close ();
241                                 DeleteFile (path);
242                         }
243                 }
244
245
246                 [Test]
247                 [ExpectedException (typeof (ArgumentOutOfRangeException))]
248                 public void CtorArgumentOutOfRangeException5 ()
249                 {
250                         string path = TempFolder + Path.DirectorySeparatorChar + "temp";
251                         DeleteFile (path);
252
253                         FileStream stream = null;
254                         try {
255                                 stream = new FileStream (path, FileMode.CreateNew, FileAccess.Read, FileShare.Inheritable | FileShare.ReadWrite);
256                         } finally {
257                                 if (stream != null)
258                                         stream.Close ();
259                                 DeleteFile (path);
260                         }
261                 }               
262
263                 
264                 [Test]
265                 [ExpectedException (typeof (ArgumentException))]
266                 public void CtorArgumentException3 ()
267                 {
268                         string path = TempFolder + Path.DirectorySeparatorChar + "temp";
269                         FileStream stream = null;
270
271                         DeleteFile (path);
272                         
273                         try {
274                                 stream = new FileStream (".test.test.test.2", FileMode.Truncate, FileAccess.Read);
275                         } finally {
276                                 if (stream != null)
277                                         stream.Close ();
278                                 
279                                 DeleteFile (path);
280                         }                       
281                 }
282
283                 [Test]
284                 [ExpectedException (typeof (IOException))]
285                 public void CtorIOException2 ()
286                 {
287                         FileStream stream = null;
288                         try {
289                                 stream = new FileStream (new IntPtr (12), FileAccess.Read);
290                         } finally {
291                                 if (stream != null)
292                                         stream.Close ();
293                         }
294                 }
295
296                 [Test]
297                 [ExpectedException(typeof(IOException))]
298                 public void CtorIOException ()
299                 {                       
300                         string path = TempFolder + "/CTorIOException.Test";
301                         FileStream stream = null;
302                         FileStream stream2 = null;
303                         DeleteFile (path);
304
305                         try {
306                                 stream = new FileStream (path, FileMode.CreateNew);
307                         
308                                 // used by an another process
309                                 stream2 = new FileStream (path, FileMode.OpenOrCreate);
310                         } finally {
311                                 if (stream != null)
312                                         stream.Close ();
313                                 if (stream2 != null)
314                                         stream2.Close ();
315                                 DeleteFile (path);
316                         }
317                 }
318                 
319                 [Test]
320                 public void Flush ()
321                 {
322                         string path = TempFolder + "/FileStreamTest.Flush";
323                         FileStream stream = null;
324                         FileStream stream2 = null;
325
326                         DeleteFile (path);
327                         
328                         try {
329                                 stream = new FileStream (path, FileMode.CreateNew, FileAccess.ReadWrite, FileShare.ReadWrite);
330                                 stream2 = new FileStream (path, FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite);
331
332                                 stream.Write (new byte [] {1, 2, 3, 4, 5}, 0, 5);
333                                                 
334                                 byte [] bytes = new byte [5];
335                                 stream2.Read (bytes, 0, 5);
336                                 
337                                 Assertion.AssertEquals ("test#01", 0, bytes [0]);
338                                 Assertion.AssertEquals ("test#02", 0, bytes [1]);
339                                 Assertion.AssertEquals ("test#03", 0, bytes [2]);
340                                 Assertion.AssertEquals ("test#04", 0, bytes [3]);
341                                 
342                                 stream.Flush ();
343                                 stream2.Read (bytes, 0, 5);                     
344                                 Assertion.AssertEquals ("test#05", 1, bytes [0]);
345                                 Assertion.AssertEquals ("test#06", 2, bytes [1]);
346                                 Assertion.AssertEquals ("test#07", 3, bytes [2]);
347                                 Assertion.AssertEquals ("test#08", 4, bytes [3]);
348                         } finally {
349                                 if (stream != null)
350                                         stream.Close ();
351                                 if (stream2 != null)
352                                         stream2.Close ();
353                                 
354                                 DeleteFile (path);
355                         }
356                 }
357                 
358                 public void TestDefaultProperties ()
359                 {
360                         string path = TempFolder + Path.DirectorySeparatorChar + "testfilestream.tmp.2";
361                         DeleteFile (path);
362
363                         FileStream stream = new FileStream (path, FileMode.Create);
364                         
365                         AssertEquals ("test#01", true, stream.CanRead);
366                         AssertEquals ("test#02", true, stream.CanSeek);
367                         AssertEquals ("test#03", true, stream.CanWrite);
368                         AssertEquals ("test#04", false, stream.IsAsync);
369                         AssertEquals ("test#05", true, stream.Name.EndsWith (path));
370                         AssertEquals ("test#06", 0, stream.Position);
371                         AssertEquals ("test#07", "System.IO.FileStream", stream.ToString());                    
372                         stream.Close ();
373                         DeleteFile (path);
374
375                         stream = new FileStream (path, FileMode.OpenOrCreate, FileAccess.Read);
376                         AssertEquals ("test#08", true, stream.CanRead);
377                         AssertEquals ("test#09", true, stream.CanSeek);
378                         AssertEquals ("test#10", false, stream.CanWrite);
379                         AssertEquals ("test#11", false, stream.IsAsync);
380                         AssertEquals ("test#12", true, stream.Name.EndsWith (path));
381                         AssertEquals ("test#13", 0, stream.Position);
382                         AssertEquals ("test#14", "System.IO.FileStream", stream.ToString());                    
383                         stream.Close ();
384                         
385                         stream = new FileStream (path, FileMode.Truncate, FileAccess.Write, FileShare.ReadWrite);
386                         AssertEquals ("test#15", false, stream.CanRead);
387                         AssertEquals ("test#16", true, stream.CanSeek);
388                         AssertEquals ("test#17", true, stream.CanWrite);
389                         AssertEquals ("test#18", false, stream.IsAsync);
390                         AssertEquals ("test#19", true, stream.Name.EndsWith ("testfilestream.tmp.2"));
391                         AssertEquals ("test#20", 0, stream.Position);
392                         AssertEquals ("test#21", "System.IO.FileStream", stream.ToString());                    
393                         stream.Close ();
394                         DeleteFile (path);
395                 }
396                 
397                 public void TestLock()
398                 {
399                         string path = TempFolder + Path.DirectorySeparatorChar + "TestLock";
400                         DeleteFile (path);
401
402                         FileStream stream = new FileStream (path, FileMode.CreateNew, FileAccess.ReadWrite);
403                                                 
404                         stream.Write (new Byte [] {0,1,2,3,4,5,6,7,8,9,10}, 0, 10);                                     
405                         stream.Close ();
406
407                         stream = new FileStream (path, FileMode.Open, FileAccess.ReadWrite);
408                         
409                         stream.Lock (0, 5);
410                         
411                         FileStream stream2 = new FileStream (path , FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
412                         
413                         byte [] bytes = new byte [5];
414                         try {                           
415                                 stream2.Read (bytes, 0, 5);
416                                 Fail ();
417                         } catch (Exception e) {
418                                 
419                                 // locked
420                                 AssertEquals ("test#01", typeof (IOException), e.GetType ());
421                         }
422                                         
423                         stream2.Seek (5, SeekOrigin.Begin);                             
424                         stream2.Read (bytes, 0, 5);                             
425                         
426                         AssertEquals ("test#02", 5, bytes [0]);
427                         AssertEquals ("test#03", 6, bytes [1]);                 
428                         AssertEquals ("test#04", 7, bytes [2]); 
429                         AssertEquals ("test#05", 8, bytes [3]);
430                         AssertEquals ("test#06", 9, bytes [4]);
431                         
432                         stream.Unlock (0,5);
433                         stream2.Seek (0, SeekOrigin.Begin);     
434                         stream2.Read (bytes, 0, 5);
435                         
436                         AssertEquals ("test#02", 0, bytes [0]);
437                         AssertEquals ("test#03", 1, bytes [1]);                 
438                         AssertEquals ("test#04", 2, bytes [2]); 
439                         AssertEquals ("test#05", 3, bytes [3]);
440                         AssertEquals ("test#06", 4, bytes [4]);
441                                                 
442                         stream.Close ();
443                         stream2.Close ();
444                         
445                         DeleteFile (path);                              
446                 }
447
448                 [Test]
449                 public void Seek ()
450                 {
451                         string path = TempFolder + "/FST.Seek.Test";
452                         DeleteFile (path);                      
453
454                         FileStream stream = new FileStream (path, FileMode.CreateNew, FileAccess.ReadWrite, FileShare.ReadWrite);
455                         FileStream stream2 = new FileStream (path, FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite);
456                         
457                         stream.Write (new byte [] {1, 2, 3, 4, 5, 6, 7, 8, 10}, 0, 9);
458                         Assertion.AssertEquals ("test#01", 5, stream2.Seek (5, SeekOrigin.Begin));
459                         Assertion.AssertEquals ("test#02", -1, stream2.ReadByte ());
460                         
461                         Assertion.AssertEquals ("test#03", 2, stream2.Seek (-3, SeekOrigin.Current));
462                         Assertion.AssertEquals ("test#04", -1, stream2.ReadByte ());
463                         
464                         Assertion.AssertEquals ("test#05", 12, stream.Seek (3, SeekOrigin.Current));
465                         Assertion.AssertEquals ("test#06", -1, stream.ReadByte ());
466
467                         Assertion.AssertEquals ("test#07", 5, stream.Seek (-7, SeekOrigin.Current));
468                         Assertion.AssertEquals ("test#08", 6, stream.ReadByte ());
469
470                         Assertion.AssertEquals ("test#09", 5, stream2.Seek (5, SeekOrigin.Begin));
471                         Assertion.AssertEquals ("test#10", 6, stream2.ReadByte ());
472                                                 
473                         stream.Close ();
474                         stream2.Close ();
475                         
476                         DeleteFile (path);
477                 }
478
479                 public void TestSeek ()
480                 {
481                                         string path = TempFolder + Path.DirectorySeparatorChar + "TestSeek";
482                                         DeleteFile (path);
483
484                                         FileStream stream = new FileStream (path, FileMode.CreateNew, FileAccess.ReadWrite, FileShare.ReadWrite);
485                         stream.Write (new byte[] {1, 2, 3, 4, 5, 6, 7, 8 , 9, 10}, 0, 10);
486                         
487                         stream.Seek (5, SeekOrigin.End);
488                         AssertEquals ("test#01", -1, stream.ReadByte ());
489
490                         stream.Seek (-5, SeekOrigin.End);
491                         AssertEquals ("test#02", 6, stream.ReadByte ());
492                         
493                         try {
494                                 stream.Seek (-11, SeekOrigin.End);
495                                 Fail ();
496                         } catch (Exception e) {
497                                 AssertEquals ("test#03", typeof (IOException), e.GetType ());
498                         }
499                         
500                                         stream.Seek (19, SeekOrigin.Begin);
501                                         AssertEquals ("test#04", -1, stream.ReadByte ());
502
503                                         stream.Seek (1, SeekOrigin.Begin);
504                                         AssertEquals ("test#05", 2, stream.ReadByte ());
505                         
506                                         stream.Seek (3, SeekOrigin.Current);
507                                         AssertEquals ("test#06", 6, stream.ReadByte ());
508
509                                         stream.Seek (-2, SeekOrigin.Current);
510                                         AssertEquals ("test#07", 5, stream.ReadByte ());
511
512                                         stream.Flush ();
513
514                                         // Test that seeks work correctly when seeking inside the buffer
515                                         stream.Seek (0, SeekOrigin.Begin);
516                                         stream.WriteByte (0);
517                                         stream.WriteByte (1);
518                                         stream.Seek (0, SeekOrigin.Begin);
519                                         byte[] buf = new byte [1];
520                                         buf [0] = 2;
521                                         stream.Write (buf, 0, 1);
522                                         stream.Write (buf, 0, 1);
523                                         stream.Flush ();
524                                         stream.Seek (0, SeekOrigin.Begin);
525                                         AssertEquals ("test#08", 2, stream.ReadByte ());
526                                         AssertEquals ("test#09", 2, stream.ReadByte ());
527
528                                         stream.Close ();
529
530                                         DeleteFile (path);
531                 }
532                 
533                 public void TestClose ()
534                 {
535                         string path = TempFolder + Path.DirectorySeparatorChar + "TestClose";
536                         DeleteFile (path);
537                         
538                         FileStream stream = new FileStream (path, FileMode.CreateNew, FileAccess.ReadWrite);
539
540                         stream.Write (new byte [] {1, 2, 3, 4}, 0, 4);
541                         stream.ReadByte ();                     
542                         stream.Close ();
543                         
544                         try {                   
545                                 stream.ReadByte ();
546                                 Fail ();
547                         } catch (Exception e) {
548                                 AssertEquals ("test#01", typeof (ObjectDisposedException), e.GetType ());
549                         }
550                         
551                         try {                   
552                                 stream.WriteByte (64);
553                                 Fail ();
554                         } catch (Exception e) {
555                                 AssertEquals ("test#02", typeof (ObjectDisposedException), e.GetType ());
556                         }
557                         
558                         try {                   
559                                 stream.Flush ();
560                                 Fail ();
561                         } catch (Exception e) {
562                                 AssertEquals ("test#03", typeof (ObjectDisposedException), e.GetType ());
563                         }
564                         
565                         try { 
566                                 long l = stream.Length;
567                                 Fail ();
568                         } catch (Exception e) {
569                                 AssertEquals ("test#04", typeof (ObjectDisposedException), e.GetType ());
570                         }
571                         
572                         try { 
573                                 long l = stream.Position;
574                                 Fail ();
575                         } catch (Exception e) {
576                                 AssertEquals ("test#05", typeof (ObjectDisposedException), e.GetType ());
577                         }
578
579                         AssertEquals ("test#06", false, stream.CanRead);
580                         AssertEquals ("test#07", false, stream.CanSeek);
581                         AssertEquals ("test#08", false, stream.CanWrite);                       
582                         AssertEquals ("test#09", true, stream.Name.EndsWith (path));
583
584                         DeleteFile (path);                      
585                 }
586
587
588                 /// <summary>
589                 /// Checks whether the <see cref="FileStream" /> throws a <see cref="NotSupportedException" />
590                 /// when the stream is opened with access mode <see cref="FileAccess.Read" /> and the
591                 /// <see cref="FileStream.Write(byte[], int, int)" /> method is called.
592                 /// </summary>
593                 [Test]
594                 [ExpectedException (typeof(NotSupportedException))]
595                 public void TestWriteVerifyAccessMode ()
596                 {
597                         string path = TempFolder + Path.DirectorySeparatorChar + "temp";
598                         DeleteFile (path);
599
600                         FileStream stream = null;
601                         byte[] buffer;
602
603                         try {
604                                 buffer = Encoding.ASCII.GetBytes ("test");
605                                 stream = new FileStream (path, FileMode.OpenOrCreate, FileAccess.Read);
606                                 stream.Write (buffer, 0, buffer.Length);
607                         } finally {
608                                 if (stream != null)
609                                         stream.Close();
610                                 DeleteFile (path);
611                         }
612                 }
613
614                 /// <summary>
615                 /// Checks whether the <see cref="FileStream" /> throws a <see cref="NotSupportedException" />
616                 /// when the stream is opened with access mode <see cref="FileAccess.Read" /> and the
617                 /// <see cref="FileStream.WriteByte(byte)" /> method is called.
618                 /// </summary>
619                 [Test]
620                 [ExpectedException (typeof (NotSupportedException))]
621                 public void TestWriteByteVerifyAccessMode ()
622                 {
623                         string path = TempFolder + Path.DirectorySeparatorChar + "temp";
624                         DeleteFile (path);
625
626                         FileStream stream = null;
627
628                         try {
629                                 stream = new FileStream (path, FileMode.OpenOrCreate, FileAccess.Read);
630                                 stream.WriteByte (Byte.MinValue);
631                         } finally {
632                                 if (stream != null)
633                                         stream.Close ();
634                                 DeleteFile (path);
635                         }
636                 }
637
638                 /// <summary>
639                 /// Checks whether the <see cref="FileStream" /> throws a <see cref="NotSupportedException" />
640                 /// when the stream is opened with access mode <see cref="FileAccess.Write" /> and the
641                 /// <see cref="FileStream.Read(byte[], int, int)" /> method is called.
642                 /// </summary>
643                 [Test]
644                 [ExpectedException (typeof (NotSupportedException))]
645                 public void TestReadVerifyAccessMode ()
646                 {
647                         string path = TempFolder + Path.DirectorySeparatorChar + "temp";
648                         DeleteFile (path);
649
650                         FileStream stream = null;
651                         byte[] buffer = new byte [100];
652
653                         try {
654                                 stream = new FileStream (path, FileMode.OpenOrCreate, FileAccess.Write);
655                                 stream.Read (buffer, 0, buffer.Length);
656                         } finally {
657                                 if (stream != null)
658                                         stream.Close ();
659                         }
660                 }
661
662                 /// <summary>
663                 /// Checks whether the <see cref="FileStream" /> throws a <see cref="NotSupportedException" />
664                 /// when the stream is opened with access mode <see cref="FileAccess.Write" /> and the
665                 /// <see cref="FileStream.ReadByte()" /> method is called.
666                 /// </summary>
667                 [Test]
668                 [ExpectedException (typeof (NotSupportedException))]
669                 public void TestReadByteVerifyAccessMode ()
670                 {
671                         string path = TempFolder + Path.DirectorySeparatorChar + "temp";
672                         DeleteFile (path);
673
674                         FileStream stream = null;
675
676                         try {
677                                 stream = new FileStream (path, FileMode.OpenOrCreate, FileAccess.Write);
678                                 int readByte = stream.ReadByte ();
679                         } finally {
680                                 if (stream != null)
681                                         stream.Close();
682                                 DeleteFile (path);
683                         }
684                 }
685
686                 private void DeleteFile (string path) 
687                 {
688                         if (File.Exists (path))
689                                 File.Delete (path);
690                 }
691                         
692                         
693         }
694 }
695