Improve a safety check when writing data into StatBuffer
[mono.git] / mcs / class / corlib / Test / System.IO / FileInfoTest.cs
1 // FileInfoTest.cs - NUnit Test Cases for System.IO.FileInfo class
2 //
3 // Ville Palo (vi64pa@koti.soon.fi)
4 // 
5 // (C) 2003 Ville Palo
6 // 
7
8 using System;
9 using System.IO;
10 using System.Runtime.Serialization;
11 using System.Runtime.Serialization.Formatters.Binary;
12
13 using NUnit.Framework;
14
15 namespace MonoTests.System.IO
16 {
17         [TestFixture]
18         public class FileInfoTest
19         {
20                 string TempFolder = Path.Combine (Path.GetTempPath (), "MonoTests.System.IO.Tests");
21                 static readonly char DSC = Path.DirectorySeparatorChar;
22
23                 [SetUp]
24                 public void SetUp ()
25                 {
26                         DeleteDirectory (TempFolder);
27                         Directory.CreateDirectory (TempFolder);
28                 }
29
30                 [TearDown]
31                 public void TearDown ()
32                 {
33                         DeleteDirectory (TempFolder);
34                 }
35
36                 [Test] // ctor (String)
37                 public void Constructor1 ()
38                 {
39                         string path = TempFolder + DSC + "FIT.Ctr.Test";
40                         DeleteFile (path);
41
42                         FileInfo info = new FileInfo (path);
43                         Assert.IsTrue (info.DirectoryName.EndsWith (".Tests"), "#1");
44                         Assert.IsFalse (info.Exists, "#2");
45                         Assert.AreEqual (".Test", info.Extension, "#3");
46                         Assert.AreEqual ("FIT.Ctr.Test", info.Name, "#4");
47                 }
48
49                 [Test] // ctor (String)
50                 public void Constructor1_FileName_Null ()
51                 {
52                         try {
53                                 new FileInfo (null);
54                                 Assert.Fail ("#1");
55                         } catch (ArgumentNullException ex) {
56                                 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
57                                 Assert.IsNull (ex.InnerException, "#3");
58                                 Assert.IsNotNull (ex.Message, "#4");
59                                 Assert.AreEqual ("fileName", ex.ParamName, "#5");
60                         }
61                 }
62
63                 [Test] // ctor (String)
64                 public void Constructor1_FileName_Empty ()
65                 {
66                         try {
67                                 new FileInfo (string.Empty);
68                                 Assert.Fail ("#1");
69                         } catch (ArgumentException ex) {
70                                 // Empty file name is not legal
71                                 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
72                                 Assert.IsNull (ex.InnerException, "#3");
73                                 Assert.IsNotNull (ex.Message, "#4");
74                                 Assert.IsNull (ex.ParamName, "#5");
75                         }
76                 }
77
78                 [Test] // ctor (String)
79                 public void Constructor1_FileName_InvalidPathChars ()
80                 {
81                         string path = string.Empty;
82                         foreach (char c in Path.InvalidPathChars)
83                                 path += c;
84                         try {
85                                 new FileInfo (path);
86                         } catch (ArgumentException ex) {
87                                 // The path contains illegal characters
88                                 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
89                                 Assert.IsNull (ex.InnerException, "#3");
90                                 Assert.IsNotNull (ex.Message, "#4");
91                                 Assert.IsNull (ex.ParamName, "#5");
92                         }
93                 }
94
95                 [Test] // ctor (String)
96                 public void Constructor1_FileName_Whitespace ()
97                 {
98                         try {
99                                 new FileInfo ("      ");
100                                 Assert.Fail ("#1");
101                         } catch (ArgumentException ex) {
102                                 // The path is not of a legal form
103                                 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
104                                 Assert.IsNull (ex.InnerException, "#3");
105                                 Assert.IsNotNull (ex.Message, "#4");
106                                 Assert.IsNull (ex.ParamName, "#5");
107                         }
108                 }
109
110                 [Test]
111                 public void DirectoryTest ()
112                 {
113                         string path = TempFolder + DSC + "FIT.Directory.Test";
114                         DeleteFile (path);
115                         
116                         FileInfo info = new FileInfo (path);
117                         DirectoryInfo dir = info.Directory;
118                         Assert.AreEqual ("MonoTests.System.IO.Tests", dir.Name);
119                 }
120                 
121                 [Test]
122                 public void Exists ()
123                 {
124                         string path = TempFolder + DSC + "FIT.Exists.Test";
125                         DeleteFile (path);
126                         
127                         try {
128                                 FileInfo info = new FileInfo (path);
129                                 Assert.IsFalse (info.Exists, "#1");
130                         
131                                 File.Create (path).Close ();
132                                 Assert.IsFalse (info.Exists, "#2");
133                                 info = new FileInfo (path);
134                                 Assert.IsTrue (info.Exists, "#3");
135                                 info = new FileInfo (TempFolder);
136                                 Assert.IsFalse (info.Exists, "#4");
137                         } finally {
138                                 DeleteFile (path);
139                         }
140                 }
141
142 #if !MOBILE
143                 [Test]
144                 [Category ("NotWorking")]
145                 public void IsReadOnly ()
146                 {
147                         string path = TempFolder + DSC + "FIT.IsReadOnly.Test";
148                         DeleteFile (path);
149                         
150                         try {
151                                 using (FileStream stream = File.Create (path)) {
152                                         stream.WriteByte (12);
153                                         stream.Close ();
154                                 }
155
156                                 FileInfo info1 = new FileInfo (path);
157                                 Assert.IsFalse (info1.IsReadOnly, "#1");
158
159                                 FileInfo info2 = new FileInfo (path);
160                                 info2.IsReadOnly = true;
161                                 Assert.IsFalse (info1.IsReadOnly, "#2");
162                                 Assert.IsTrue (info2.IsReadOnly, "#3");
163
164                                 FileInfo info3 = new FileInfo (path);
165                                 Assert.IsTrue (info3.IsReadOnly, "#4");
166                                 info3.IsReadOnly = false;
167                                 Assert.IsFalse (info1.IsReadOnly, "#4");
168                                 Assert.IsTrue (info2.IsReadOnly, "#5");
169                                 Assert.IsFalse (info3.IsReadOnly, "#6");
170                         } finally {
171                                 File.SetAttributes (path, FileAttributes.Normal);
172                                 DeleteFile (path);
173                         }
174                 }
175 #endif
176
177                 [Test]
178                 public void Length ()
179                 {
180                         string path = TempFolder + DSC + "FIT.Length.Test";
181                         DeleteFile (path);
182                         
183                         try {
184                                 FileStream stream = File.Create (path);
185                                 FileInfo info = new FileInfo (path);
186                                 Assert.AreEqual (0, info.Length, "#1");
187                                 stream.WriteByte (12);
188                                 stream.Flush ();
189                                 Assert.AreEqual (0, info.Length, "#2");
190                                 info = new FileInfo (path);
191                                 Assert.AreEqual (1, info.Length, "#3");
192                                 stream.Close ();
193                         } finally {
194                                 DeleteFile (path);
195                         }
196                 }
197                 
198                 [Test]
199                 public void Length_FileDoesNotExist ()
200                 {
201                         string path = TempFolder + DSC + "FIT.LengthException.Test";
202                         DeleteFile (path);
203                         FileInfo info = new FileInfo (path);
204                         try {
205                                 long l = info.Length;
206                                 Assert.Fail ("#1:" + l);
207                         } catch (FileNotFoundException ex) {
208                                 Assert.AreEqual (typeof (FileNotFoundException), ex.GetType (), "#2");
209                                 Assert.AreEqual (path, ex.FileName, "#3");
210                                 Assert.IsNull (ex.InnerException, "#4");
211                                 Assert.IsNotNull (ex.Message, "#5");
212                         }
213                 }
214                 
215                 [Test]
216                 public void AppendText ()
217                 {
218                         string path = TempFolder + DSC + "FIT.AppendText.Test";
219                         DeleteFile (path);
220                         
221                         try {
222                                 FileInfo info = new FileInfo (path);
223                                 Assert.IsFalse (info.Exists, "#1");
224                         
225                                 StreamWriter writer = info.AppendText ();
226                                 info = new FileInfo (path);
227                                 Assert.IsTrue (info.Exists, "#2");
228                                 
229                                 writer.Write ("aaa");
230                                 writer.Flush ();
231                                 writer.Close ();
232                         
233                                 Assert.AreEqual (0, info.Length, "#3");
234                                 info = new FileInfo (path);
235                                 Assert.AreEqual (3, info.Length, "#4");
236                         } finally {
237                                 DeleteFile (path);
238                         }
239                 }
240
241                 [Test] // CopyTo (String)
242                 public void CopyTo1 ()
243                 {
244                         string path1 = TempFolder + DSC + "FIT.CopyTo.Source.Test";
245                         string path2 = TempFolder + DSC + "FIT.CopyTo.Dest.Test";
246                         DeleteFile (path1);
247                         DeleteFile (path2);
248                         try {
249                                 File.Create (path1).Close ();
250                         
251                                 FileInfo info = new FileInfo (path1);
252                                 Assert.IsTrue (info.Exists, "#1");
253
254                                 FileInfo info2 = info.CopyTo (path2);
255                                 info = new FileInfo (path1);
256                                 Assert.IsTrue (info2.Exists, "#2");
257                         } finally {
258                                 DeleteFile (path1);
259                                 DeleteFile (path2);
260                         }
261                 }
262
263                 [Test] // CopyTo (String)
264                 public void CopyTo1_DestFileName_AlreadyExists ()
265                 {
266                         string path1 = TempFolder + DSC + "FIT.CopyToException.Source.Test";
267                         string path2 = TempFolder + DSC + "FIT.CopyToException.Dest.Test";
268
269                         try {
270                                 DeleteFile (path1);
271                                 DeleteFile (path2);
272                                 File.Create (path1).Close ();
273                                 File.Create (path2).Close ();
274                                 FileInfo info = new FileInfo (path1);
275                                 try {
276                                         info.CopyTo (path2);
277                                         Assert.Fail ("#1");
278                                 } catch (IOException ex) {
279                                         // The file '...' already exists.
280                                         Assert.AreEqual (typeof (IOException), ex.GetType (), "#2");
281                                         Assert.IsNull (ex.InnerException, "#3");
282                                         Assert.IsNotNull (ex.Message, "#4");
283                                         Assert.IsTrue (ex.Message.IndexOf (path2) != -1, "#5");
284                                 }
285                         } finally {
286                                 DeleteFile (path1);
287                                 DeleteFile (path2);
288                         }
289                 }
290
291                 [Test] // CopyTo (String)
292                 public void CopyTo1_DestFileName_Null ()
293                 {
294                         string path = TempFolder + DSC + "FIT.CopyToArgumentNullException.Test";
295                         DeleteFile (path);
296                         try {
297                                 File.Create (path).Close ();
298                                 FileInfo info = new FileInfo (path);
299                                 try {
300                                         info.CopyTo (null);
301                                         Assert.Fail ("#1");
302                                 } catch (ArgumentNullException ex) {
303                                         Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
304                                         Assert.IsNull (ex.InnerException, "#3");
305                                         Assert.IsNotNull (ex.Message, "#4");
306                                         Assert.AreEqual ("destFileName", ex.ParamName, "#5");
307                                 }
308                         } finally {
309                                 DeleteFile (path);
310                         }
311                 }
312
313                 [Test] // CopyTo (String)
314                 public void CopyTo1_DestFileName_Empty ()
315                 {
316                         string path = TempFolder + DSC + "FIT.CopyToArgument1Exception.Test";
317                         DeleteFile (path);
318
319                         try {
320                                 File.Create (path).Close ();
321                                 FileInfo info = new FileInfo (path);
322                                 try {
323                                         info.CopyTo (string.Empty);
324                                         Assert.Fail ("#1");
325                                 } catch (ArgumentException ex) {
326                                         // Empty file name is not legal
327                                         Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
328                                         Assert.IsNull (ex.InnerException, "#3");
329                                         Assert.IsNotNull (ex.Message, "#4");
330                                         Assert.AreEqual ("destFileName", ex.ParamName, "#5");
331                                 }
332                         } finally {
333                                 DeleteFile (path);
334                         }
335                 }
336
337                 [Test] // CopyTo (String)
338                 public void CopyTo1_DestFileName_Whitespace ()
339                 {
340                         string path = TempFolder + DSC + "FIT.CopyToArgument2Exception.Test";
341                         DeleteFile (path);
342
343                         try {
344                                 File.Create (path).Close ();
345                                 FileInfo info = new FileInfo (path);
346                                 try {
347                                         info.CopyTo ("    ");
348                                         Assert.Fail ("#1");
349                                 } catch (ArgumentException ex) {
350                                         // The path is not of a legal form
351                                         Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
352                                         Assert.IsNull (ex.InnerException, "#3");
353                                         Assert.IsNotNull (ex.Message, "#4");
354                                         Assert.IsNull (ex.ParamName, "#5");
355                                 }
356                         } finally {
357                                 DeleteFile (path);
358                         }
359                 }
360
361                 [Test] // CopyTo (String)
362                 public void CopyTo1_DestFileName_InvalidPathChars ()
363                 {
364                         string path = TempFolder + DSC + "FIT.CopyToArgument4Exception.Test";
365                         string path2 = string.Empty;
366                         DeleteFile (path);
367
368                         try {
369                                 File.Create (path).Close ();
370                                 FileInfo info = new FileInfo (path);
371                                 foreach (char c in Path.InvalidPathChars)
372                                         path2 += c;
373                                 try {
374                                         info.CopyTo (path2);
375                                         Assert.Fail ("#1");
376                                 } catch (ArgumentException ex) {
377                                         // Illegal characters in path
378                                         Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
379                                         Assert.IsNull (ex.InnerException, "#3");
380                                         Assert.IsNotNull (ex.Message, "#4");
381                                         Assert.IsNull (ex.ParamName, "#5");
382                                 }
383                         } finally {
384                                 DeleteFile (path);
385                         }
386                 }
387
388                 [Test] // CopyTo (String, Boolean)
389                 public void CopyTo2 ()
390                 {
391                         string path1 = TempFolder + DSC + "FIT.CopyTo2.Source.Test";
392                         string path2 = TempFolder + DSC + "FIT.CopyTo2.Dest.Test";
393                         DeleteFile (path1);
394                         DeleteFile (path2);
395                         try {
396                                 File.Create (path1).Close ();
397                                 File.Create (path2).Close ();
398                                 FileInfo info = new FileInfo (path1);
399
400                                 FileInfo info2 = info.CopyTo (path2, true);
401                                 info = new FileInfo (path1);
402                                 Assert.IsTrue (info.Exists, "#1");
403                                 Assert.IsTrue (info2.Exists, "#2");
404                         } finally {
405                                 DeleteFile (path1);
406                                 DeleteFile (path2);
407                         }
408                 }
409
410                 [Test] // CopyTo (String, Boolean)
411                 public void CopyTo2_DestFileName_AlreadyExists ()
412                 {
413                         string path1 = TempFolder + DSC + "FIT.CopyToException.Source.Test";
414                         string path2 = TempFolder + DSC + "FIT.CopyToException.Dest.Test";
415
416                         try {
417                                 DeleteFile (path1);
418                                 DeleteFile (path2);
419                                 File.Create (path1).Close ();
420                                 File.Create (path2).Close ();
421                                 FileInfo info = new FileInfo (path1);
422                                 try {
423                                         info.CopyTo (path2, false);
424                                         Assert.Fail ("#1");
425                                 } catch (IOException ex) {
426                                         // The file '...' already exists.
427                                         Assert.AreEqual (typeof (IOException), ex.GetType (), "#2");
428                                         Assert.IsNull (ex.InnerException, "#3");
429                                         Assert.IsNotNull (ex.Message, "#4");
430                                         Assert.IsTrue (ex.Message.IndexOf (path2) != -1, "#5");
431                                 }
432                         } finally {
433                                 DeleteFile (path1);
434                                 DeleteFile (path2);
435                         }
436                 }
437
438                 [Test] // CopyTo (String, Boolean)
439                 public void CopyTo2_DestFileName_Null ()
440                 {
441                         string path = TempFolder + DSC + "FIT.CopyToArgumentNullException.Test";
442                         DeleteFile (path);
443                         try {
444                                 File.Create (path).Close ();
445                                 FileInfo info = new FileInfo (path);
446                                 try {
447                                         info.CopyTo (null, false);
448                                         Assert.Fail ("#1");
449                                 } catch (ArgumentNullException ex) {
450                                         Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
451                                         Assert.IsNull (ex.InnerException, "#3");
452                                         Assert.IsNotNull (ex.Message, "#4");
453                                         Assert.AreEqual ("destFileName", ex.ParamName, "#5");
454                                 }
455                         } finally {
456                                 DeleteFile (path);
457                         }
458                 }
459
460                 [Test] // CopyTo (String, Boolean)
461                 public void CopyTo2_DestFileName_Empty ()
462                 {
463                         string path = TempFolder + DSC + "FIT.CopyToArgument1Exception.Test";
464                         DeleteFile (path);
465                         
466                         try {
467                                 File.Create (path).Close ();
468                                 FileInfo info = new FileInfo (path);
469                                 try {
470                                         info.CopyTo (string.Empty, false);
471                                         Assert.Fail ("#1");
472                                 } catch (ArgumentException ex) {
473                                         // Empty file name is not legal
474                                         Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
475                                         Assert.IsNull (ex.InnerException, "#3");
476                                         Assert.IsNotNull (ex.Message, "#4");
477                                         Assert.AreEqual ("destFileName", ex.ParamName, "#5");
478                                 }
479                         } finally {
480                                 DeleteFile (path);
481                         }
482                 }
483
484                 [Test] // CopyTo (String, Boolean)
485                 public void CopyTo2_DestFileName_Whitespace ()
486                 {
487                         string path = TempFolder + DSC + "FIT.CopyToArgument2Exception.Test";
488                         DeleteFile (path);
489                         
490                         try {
491                                 File.Create (path).Close ();
492                                 FileInfo info = new FileInfo (path);
493                                 try {
494                                         info.CopyTo ("    ", false);
495                                         Assert.Fail ("#1");
496                                 } catch (ArgumentException ex) {
497                                         // The path is not of a legal form
498                                         Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
499                                         Assert.IsNull (ex.InnerException, "#3");
500                                         Assert.IsNotNull (ex.Message, "#4");
501                                         Assert.IsNull (ex.ParamName, "#5");
502                                 }
503                         } finally {
504                                 DeleteFile (path);
505                         }
506                 }
507
508                 [Test] // CopyTo (String, Boolean)
509                 public void CopyTo2_DestFileName_InvalidPathChars ()
510                 {
511                         string path = TempFolder + DSC + "FIT.CopyToArgument4Exception.Test";
512                         string path2 = string.Empty;
513                         DeleteFile (path);
514                         
515                         try {
516                                 File.Create (path).Close ();
517                                 FileInfo info = new FileInfo (path);
518                                 foreach (char c in Path.InvalidPathChars)
519                                         path2 += c;
520                                 try {
521                                         info.CopyTo (path2, false);
522                                         Assert.Fail ("#1");
523                                 } catch (ArgumentException ex) {
524                                         // Illegal characters in path
525                                         Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
526                                         Assert.IsNull (ex.InnerException, "#3");
527                                         Assert.IsNotNull (ex.Message, "#4");
528                                         Assert.IsNull (ex.ParamName, "#5");
529                                 }
530                         } finally {
531                                 DeleteFile (path);
532                         }
533                 }
534                 
535                 [Test]
536                 public void Create ()
537                 {
538                         string path = TempFolder + DSC + "FIT.Create.Test";
539                         DeleteFile (path);
540                         
541                         try {
542                                 FileInfo info = new FileInfo (path);
543                                 Assert.IsFalse (info.Exists, "#1");
544                                 FileStream stream = info.Create ();
545                                 Assert.IsFalse (info.Exists, "#2");
546                                 info = new FileInfo (path);
547                                 Assert.IsTrue (info.Exists, "#3");
548                                 Assert.IsTrue (stream.CanRead, "#4");
549                                 Assert.IsTrue (stream.CanWrite, "#5");
550                                 Assert.IsTrue (stream.CanSeek, "#6");
551                                 stream.Close ();
552                         } finally {
553                                 DeleteFile (path);
554                         }
555                 }
556                 
557                 [Test]
558                 public void CreateText ()
559                 {
560                         string path = TempFolder + DSC + "FIT.CreateText.Test";
561                         DeleteFile (path);
562                         
563                         try {
564                                 FileInfo info = new FileInfo (path);
565                                 Assert.IsFalse (info.Exists, "#1");
566                                 StreamWriter writer = info.CreateText ();
567                                 writer.WriteLine ("test");
568                                 writer.Close ();
569                                 info = new FileInfo (path);
570                                 Assert.IsTrue (info.Exists, "#2");
571                         } finally {
572                                 DeleteFile (path);
573                         }
574                 }
575                 
576                 [Test]
577                 public void CreateText_Directory ()
578                 {
579                         FileInfo info = new FileInfo (TempFolder);
580                         try {
581                                 info.CreateText ();
582                                 Assert.Fail ("#1");
583                         } catch (UnauthorizedAccessException ex) {
584                                 Assert.AreEqual (typeof (UnauthorizedAccessException), ex.GetType (), "#2");
585                                 Assert.IsNull (ex.InnerException, "#3");
586                                 Assert.IsNotNull (ex.Message, "#4");
587                         }
588                 }
589                 
590                 [Test]
591                 public void Delete ()
592                 {
593                         string path = TempFolder + DSC + "FIT.Delete.Test";
594                         DeleteFile (path);
595                         
596                         try {
597                                 FileInfo info = new FileInfo (path);
598                                 Assert.IsFalse (info.Exists, "#1");
599                                 info.Create ().Close ();
600                                 info = new FileInfo (path);
601                                 Assert.IsTrue (info.Exists, "#2");
602                                 info.Delete ();
603                                 Assert.IsTrue (info.Exists, "#3");
604                                 info = new FileInfo (path);
605                                 Assert.IsFalse (info.Exists, "#4");
606                         } finally {
607                                 DeleteFile (path);
608                         }
609                 }
610                 
611                 [Test]
612                 public void Delete_Directory ()
613                 {
614                         FileInfo info = new FileInfo (TempFolder);
615                         try {
616                                 info.Delete ();
617                                 Assert.Fail ("#1");
618                         } catch (UnauthorizedAccessException ex) {
619                                 Assert.AreEqual (typeof (UnauthorizedAccessException), ex.GetType (), "#2");
620                                 Assert.IsNull (ex.InnerException, "#3");
621                                 Assert.IsNotNull (ex.Message, "#4");
622                         }
623                 }
624                 
625                 [Test]
626                 public void MoveTo ()
627                 {
628                         string path1 = TempFolder + DSC + "FIT.MoveTo.Source.Test";
629                         string path2 = TempFolder + DSC + "FIT.MoveTo.Dest.Test";
630                         DeleteFile (path1);
631                         DeleteFile (path2);
632                         
633                         try {
634                                 File.Create (path1).Close ();
635                                 FileInfo info1 = new FileInfo (path1);
636                                 FileInfo info2 = new FileInfo (path2);
637                                 Assert.IsTrue (info1.Exists, "#A1");
638                                 Assert.AreEqual (path1, info1.FullName, "#A2");
639                                 Assert.IsFalse (info2.Exists, "#A3");
640                                 Assert.AreEqual (path2, info2.FullName, "#A4");
641
642                                 info1.MoveTo (path2);
643                                 info2 = new FileInfo (path2);
644                                 Assert.IsTrue (info1.Exists, "#B1");
645                                 Assert.AreEqual (path2, info1.FullName, "#B2");
646                                 Assert.IsTrue (info2.Exists, "#B3");
647                                 Assert.AreEqual (path2, info2.FullName, "#B4");
648                         } finally {
649                                 DeleteFile (path1);
650                                 DeleteFile (path2);
651                         }
652                 }
653
654                 [Test] //Covers #18361
655                 public void MoveTo_SameName ()
656                 {
657                         string name = "FIT.MoveTo.SameName.Test";
658                         string path1 = TempFolder + DSC + name;
659                         string path2 = name;
660                         DeleteFile (path1);
661                         DeleteFile (path2);
662                         
663                         try {
664                                 File.Create (path1).Close ();
665                                 FileInfo info1 = new FileInfo (path1);
666                                 FileInfo info2 = new FileInfo (path2);
667                                 Assert.IsTrue (info1.Exists, "#A1");
668                                 Assert.IsFalse (info2.Exists, "#A2");
669
670                                 info1.MoveTo (path2);
671                                 info1 = new FileInfo (path1);
672                                 info2 = new FileInfo (path2);
673                                 Assert.IsFalse (info1.Exists, "#B1");
674                                 Assert.IsTrue (info2.Exists, "#B2");
675                         } finally {
676                                 DeleteFile (path1);
677                                 DeleteFile (path2);
678                         }
679                 }
680
681                 [Test]
682                 public void MoveTo_DestFileName_AlreadyExists ()
683                 {
684                         string sourceFile = TempFolder + DSC + "FIT.MoveTo.Source.Test";
685                         string destFile;
686                         FileInfo info;
687
688                         // move to same directory
689                         File.Create (sourceFile).Close ();
690                         info = new FileInfo (sourceFile);
691                         try {
692                                 info.MoveTo (TempFolder);
693                                 Assert.Fail ("#A1");
694                         } catch (IOException ex) {
695                                 // Cannot create a file when that file already exists
696                                 Assert.AreEqual (typeof (IOException), ex.GetType (), "#A2");
697                                 Assert.IsNull (ex.InnerException, "#A3");
698                                 Assert.IsNotNull (ex.Message, "#A4");
699                                 Assert.IsFalse (ex.Message.IndexOf (sourceFile) != -1, "#A5");
700                                 Assert.IsFalse (ex.Message.IndexOf (TempFolder) != -1, "#A6");
701                         } finally {
702                                 DeleteFile (sourceFile);
703                         }
704
705                         // move to exist file
706                         File.Create (sourceFile).Close ();
707                         destFile = TempFolder + DSC + "FIT.MoveTo.Dest.Test";
708                         File.Create (destFile).Close ();
709                         info = new FileInfo (sourceFile);
710                         try {
711                                 info.MoveTo (destFile);
712                                 Assert.Fail ("#B1");
713                         } catch (IOException ex) {
714                                 // Cannot create a file when that file already exists
715                                 Assert.AreEqual (typeof (IOException), ex.GetType (), "#B2");
716                                 Assert.IsNull (ex.InnerException, "#B3");
717                                 Assert.IsNotNull (ex.Message, "#B4");
718                                 Assert.IsFalse (ex.Message.IndexOf (sourceFile) != -1, "#B5");
719                                 Assert.IsFalse (ex.Message.IndexOf (destFile) != -1, "#B6");
720                         } finally {
721                                 DeleteFile (sourceFile);
722                                 DeleteFile (destFile);
723                         }
724
725                         // move to existing directory
726                         File.Create (sourceFile).Close ();
727                         destFile = TempFolder + Path.DirectorySeparatorChar + "bar";
728                         Directory.CreateDirectory (destFile);
729                         info = new FileInfo (sourceFile);
730                         try {
731                                 info.MoveTo (destFile);
732                                 Assert.Fail ("#C1");
733                         } catch (IOException ex) {
734                                 // Cannot create a file when that file already exists
735                                 Assert.AreEqual (typeof (IOException), ex.GetType (), "#C2");
736                                 Assert.IsNull (ex.InnerException, "#C3");
737                                 Assert.IsNotNull (ex.Message, "#C4");
738                                 Assert.IsFalse (ex.Message.IndexOf (sourceFile) != -1, "#C5");
739                                 Assert.IsFalse (ex.Message.IndexOf (destFile) != -1, "#C6");
740                         } finally {
741                                 DeleteFile (sourceFile);
742                                 DeleteDirectory (destFile);
743                         }
744                 }
745
746                 [Test]
747                 public void MoveTo_DestFileName_DirectoryDoesNotExist ()
748                 {
749                         string sourceFile = TempFolder + Path.DirectorySeparatorChar + "foo";
750                         string destFile = Path.Combine (Path.Combine (TempFolder, "doesnotexist"), "b");
751                         DeleteFile (sourceFile);
752                         try {
753                                 File.Create (sourceFile).Close ();
754                                 FileInfo info = new FileInfo (sourceFile);
755                                 try {
756                                         info.MoveTo (destFile);
757                                         Assert.Fail ("#1");
758                                 } catch (DirectoryNotFoundException ex) {
759                                         // Could not find a part of the path
760                                         Assert.AreEqual (typeof (DirectoryNotFoundException), ex.GetType (), "#2");
761                                         Assert.IsNull (ex.InnerException, "#3");
762                                         Assert.IsNotNull (ex.Message, "#4");
763                                 }
764                         } finally {
765                                 DeleteFile (sourceFile);
766                         }
767                 }
768
769                 [Test]
770                 public void MoveTo_DestFileName_Null ()
771                 {
772                         string path = TempFolder + DSC + "FIT.MoveToArgumentNullException.Test";
773                         DeleteFile (path);
774
775                         try {
776                                 File.Create (path).Close ();
777                                 FileInfo info = new FileInfo (path);
778                                 try {
779                                         info.MoveTo (null);
780                                         Assert.Fail ("#1");
781                                 } catch (ArgumentNullException ex) {
782                                         Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
783                                         Assert.IsNull (ex.InnerException, "#3");
784                                         Assert.IsNotNull (ex.Message, "#4");
785                                         Assert.AreEqual ("destFileName", ex.ParamName, "#5");
786                                 }
787                         } finally {
788                                 DeleteFile (path);
789                         }
790                 }
791
792                 [Test]
793                 public void MoveTo_DestFileName_Empty ()
794                 {
795                         string path = TempFolder + DSC + "FIT.MoveToArgumentException.Test";
796                         DeleteFile (path);
797
798                         try {
799                                 File.Create (path).Close ();
800                                 FileInfo info = new FileInfo (path);
801                                 try {
802                                         info.MoveTo (string.Empty);
803                                         Assert.Fail ("#1");
804                                 } catch (ArgumentException ex) {
805                                         // Empty file name is not legal
806                                         Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
807                                         Assert.IsNull (ex.InnerException, "#3");
808                                         Assert.IsNotNull (ex.Message, "#4");
809                                         Assert.AreEqual ("destFileName", ex.ParamName, "#5");
810                                 }
811                         } finally {
812                                 DeleteFile (path);
813                         }
814                 }
815
816                 [Test]
817                 public void MoveTo_DestFileName_Whitespace ()
818                 {
819                         string path = TempFolder + DSC + "FIT.MoveToArgumentException.Test";
820                         DeleteFile (path);
821
822                         try {
823                                 File.Create (path).Close ();
824                                 FileInfo info = new FileInfo (path);
825                                 try {
826                                         info.MoveTo ("   ");
827                                         Assert.Fail ("#1");
828                                 } catch (ArgumentException ex) {
829                                         // The path is not of a legal form
830                                         Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
831                                         Assert.IsNull (ex.InnerException, "#3");
832                                         Assert.IsNotNull (ex.Message, "#4");
833                                         Assert.IsNull (ex.ParamName, "#5");
834                                 }
835                         } finally {
836                                 DeleteFile (path);
837                         }
838                 }
839
840                 [Test]
841                 public void MoveTo_FileDoesNotExist ()
842                 {
843                         string path1 = TempFolder + DSC + "FIT.MoveToFileNotFoundException.Src";
844                         string path2 = TempFolder + DSC + "FIT.MoveToFileNotFoundException.Dst";
845                         DeleteFile (path1);
846                         DeleteFile (path2);
847                         
848                         try {
849                                 FileInfo info = new FileInfo (path1);
850                                 try {
851                                         info.MoveTo (path2);
852                                         Assert.Fail ("#1");
853                                 } catch (FileNotFoundException ex) {
854                                         // Unable to find the specified file
855                                         Assert.AreEqual (typeof (FileNotFoundException), ex.GetType (), "#2");
856                                         Assert.IsNull (ex.FileName, "#2");
857                                         Assert.IsNull (ex.InnerException, "#3");
858                                         Assert.IsNotNull (ex.Message, "#4");
859                                 }
860                         } finally {
861                                 DeleteFile (path1);
862                                 DeleteFile (path2);
863                         }
864                 }
865
866                 [Test]
867                 public void MoveTo_Same ()
868                 {
869                         string path = TempFolder + DSC + "FIT.MoveToSame.Test";
870                         DeleteFile (path);
871
872                         try {
873                                 File.Create (path).Close ();
874                                 FileInfo info = new FileInfo (path);
875                                 info.MoveTo (path);
876                                 Assert.IsTrue (info.Exists, "#1");
877                                 Assert.IsTrue (File.Exists (path), "#2");
878                         } finally {
879                                 DeleteFile (path);
880                         }
881                 }
882
883 #if !MOBILE
884                 [Test]
885                 public void Replace1 ()
886                 {
887                         string path1 = TempFolder + DSC + "FIT.Replace.Source.Test";
888                         string path2 = TempFolder + DSC + "FIT.Replace.Dest.Test";
889                         string path3 = TempFolder + DSC + "FIT.Replace.Back.Test";
890                         
891                         DeleteFile (path1);
892                         DeleteFile (path2);
893                         DeleteFile (path3);
894                         try {
895                                 File.Create (path1).Close ();
896                                 File.Create (path2).Close ();
897                                 File.Create (path3).Close ();                           
898                                 FileInfo info = new FileInfo (path1);
899                                 Assert.IsTrue (info.Exists, "#1");
900                                 FileInfo info2 = info.Replace (path2, path3);
901                                 Assert.IsTrue (info2.Exists, "#2");                             
902                                 FileInfo info3 = new FileInfo (path3);
903                                 Assert.IsTrue (info3.Exists, "#3");                                                     
904                         } finally {
905                                 DeleteFile (path2);
906                                 DeleteFile (path3);
907                         }
908                 }
909
910                 [Test]
911                 public void Replace1_Backup_Null ()
912                 {
913                         string path1 = TempFolder + DSC + "FIT.Replace.Source.Test";
914                         string path2 = TempFolder + DSC + "FIT.Replace.Dest.Test";
915                         
916                         DeleteFile (path1);
917                         DeleteFile (path2);
918                         try {
919                                 File.Create (path1).Close ();
920                                 File.Create (path2).Close ();
921                                 FileInfo info = new FileInfo (path1);
922                                 Assert.IsTrue (info.Exists, "#1");
923                                 FileInfo info2 = info.Replace (path2, null);
924                                 Assert.IsTrue (info2.Exists, "#2");
925                                 info = new FileInfo (path1);
926                                 Assert.IsFalse (info.Exists, "#3");                             
927                         } finally {
928                                 DeleteFile (path2);
929                         } 
930                 }
931
932                 [Test]
933                 public void Replace1_DestFileName_Null ()
934                 {
935                         string path1 = TempFolder + DSC + "FIT.Replace.Source.Test";
936                         DeleteFile (path1);
937                         try {
938                                 try {
939                                         File.Create (path1).Close ();
940                                         FileInfo info = new FileInfo (path1);
941                                         info.Replace (null, null);
942                                         Assert.Fail ("#1");                                             
943                                 } catch (ArgumentNullException ex) {
944                                         Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");                          
945                                         Assert.IsNull (ex.InnerException, "#3");
946                                         Assert.IsNotNull (ex.Message, "#4");    
947                                 }
948                         } finally {
949                                 DeleteFile (path1);
950                         }
951                 }
952
953                 [Test]
954                 public void Replace1_DestFileName_Empty ()
955                 {
956                         string path1 = TempFolder + DSC + "FIT.Replace.Source.Test";
957                         DeleteFile (path1);
958                         try {
959                                 try {
960                                         File.Create (path1).Close ();
961                                         FileInfo info = new FileInfo (path1);
962                                         info.Replace (string.Empty, null);
963                                         Assert.Fail ("#1");
964                                 } catch (ArgumentException ex) {
965                                         Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
966                                         Assert.IsNull (ex.InnerException, "#3");
967                                         Assert.IsNotNull (ex.Message, "#4");
968                                 }
969                         } finally {
970                                 DeleteFile (path1);
971                         }
972                 }
973                 
974                 [Test]
975                 public void Replace1_DestFileName_WhiteSpace ()
976                 {
977                         string path1 = TempFolder + DSC + "FIT.Replace.Source.Test";
978                         DeleteFile (path1);
979                         try {
980                                 try {
981                                         File.Create (path1).Close ();
982                                         FileInfo info = new FileInfo (path1);
983                                         info.Replace ("     ", null);
984                                         Assert.Fail ("#1");
985                                 } catch (ArgumentException ex) {
986                                         Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
987                                         Assert.IsNull (ex.InnerException, "#3");
988                                         Assert.IsNotNull (ex.Message, "#4");
989                                 }
990                         } finally {
991                                 DeleteFile (path1);
992                         }
993                 }
994
995                 [Test] 
996                 public void Replace1_DestFileName_InvalidPathChars ()
997                 {
998                         string path1 = TempFolder + DSC + "FIT.Replace.Source.Test";
999                         string path2 = string.Empty;
1000                         DeleteFile (path1);
1001
1002                         try {
1003                                 File.Create (path1).Close ();
1004                                 FileInfo info = new FileInfo (path1);
1005                                 foreach (char c in Path.InvalidPathChars)
1006                                         path2 += c;
1007                                 try {
1008                                         info.Replace (path2, null);
1009                                         Assert.Fail ("#1");
1010                                 } catch (ArgumentException ex) {
1011                                         // Illegal characters in path
1012                                         Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
1013                                         Assert.IsNull (ex.InnerException, "#3");
1014                                         Assert.IsNotNull (ex.Message, "#4");
1015                                         Assert.IsNull (ex.ParamName, "#5");
1016                                 }
1017                         } finally {
1018                                 DeleteFile (path1);
1019                         }
1020                 }
1021
1022                 [Test]
1023                 public void Replace1_DestFileName_FileNotFound ()
1024                 {
1025                         string path1 = TempFolder + DSC + "FIT.Replace.Source.Test";
1026                         string path2 = TempFolder + DSC + "FIT.Replace.Dest.Test";
1027                         DeleteFile (path1);
1028                         DeleteFile (path2);
1029
1030                         try {
1031                                 try {
1032                                         File.Create (path1).Close ();
1033                                         FileInfo info = new FileInfo (path1);
1034                                         info.Replace (path2, null);
1035                                         Assert.Fail ("#1");
1036                                 } catch (FileNotFoundException ex) {
1037                                         Assert.AreEqual (typeof (FileNotFoundException), ex.GetType (), "#2");
1038                                         Assert.IsNull (ex.InnerException, "#3");
1039                                         Assert.IsNotNull (ex.Message, "#4");
1040                                 }
1041                         } finally {
1042                                 DeleteFile (path1);                     
1043                         }
1044                 }
1045
1046                 [Test]
1047                 public void Replace1_DestFileName_IsReadOnly ()
1048                 {
1049                         string path1 = TempFolder + DSC + "FIT.Replace.Source.Test";
1050                         string path2 = TempFolder + DSC + "FIT.Replace.Dest.Test";
1051                         DeleteFile (path1);
1052                         DeleteFile (path2);
1053
1054                         try {
1055                                 try {
1056                                         File.Create (path1).Close ();
1057                                         File.Create (path2).Close ();
1058                                         File.SetAttributes (path2, FileAttributes.ReadOnly);
1059                                         FileInfo info = new FileInfo (path1);
1060                                         info.Replace (path2, null);
1061                                         Assert.Fail ("#1");
1062                                 } catch (UnauthorizedAccessException ex) {
1063                                         Assert.AreEqual (typeof (UnauthorizedAccessException), ex.GetType (), "#2");
1064                                         Assert.IsNull (ex.InnerException, "#3");
1065                                         Assert.IsNotNull (ex.Message, "#4");
1066                                 }
1067                         } finally {
1068                                 File.SetAttributes (path2, FileAttributes.Normal);
1069                                 DeleteFile (path1);
1070                                 DeleteFile (path2);                     
1071                         }
1072                 }
1073
1074                 [Test]
1075                 public void Replace1_Source_FileNotFound ()
1076                 {
1077                         string path1 = TempFolder + DSC + "FIT.Replace.Source.Test";
1078                         string path2 = TempFolder + DSC + "FIT.Replace.Dest.Test";
1079                         DeleteFile (path2);
1080
1081                         try {
1082                                 try {
1083                                         File.Create (path2).Close ();
1084                                         FileInfo info = new FileInfo (path1);
1085                                         info.Replace (path2, null);
1086                                         Assert.Fail ("#1");
1087                                 } catch (FileNotFoundException ex) {
1088                                         Assert.AreEqual (typeof (FileNotFoundException), ex.GetType (), "#2");
1089                                         Assert.IsNull (ex.InnerException, "#3");
1090                                         Assert.IsNotNull (ex.Message, "#4");
1091                                 }
1092                         } finally {
1093                                 DeleteFile (path2);                     
1094                         }               
1095                 }
1096 #endif
1097
1098                 [Test]
1099                 public void Open ()
1100                 {
1101                         string path = TempFolder + DSC + "FIT.Open.Test";
1102                         DeleteFile (path);
1103                         FileStream stream = null;
1104                         try {
1105                                 FileInfo info = new FileInfo (path);
1106                                 stream = info.Open (FileMode.CreateNew);
1107                                 Assert.IsTrue (stream.CanRead, "#A1");
1108                                 Assert.IsTrue (stream.CanSeek, "#A2");
1109                                 Assert.IsTrue (stream.CanWrite, "#A3");
1110                                 stream.Close ();
1111                                 
1112                                 stream = info.Open (FileMode.Open);
1113                                 Assert.IsTrue (stream.CanRead, "#B1");
1114                                 Assert.IsTrue (stream.CanSeek, "#B2");
1115                                 Assert.IsTrue (stream.CanWrite, "#B3");
1116                                 stream.Close ();
1117                                 
1118                                 stream = info.Open (FileMode.Append, FileAccess.Write);
1119                                 Assert.IsFalse (stream.CanRead, "#C1");
1120                                 Assert.IsTrue (stream.CanSeek, "#C2");
1121                                 Assert.IsTrue (stream.CanWrite, "#C3");
1122                                 stream.Close ();
1123
1124                                 stream = info.Open (FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite);
1125                                 Assert.IsTrue (stream.CanRead, "#D1");
1126                                 Assert.IsTrue (stream.CanSeek, "#D2");
1127                                 Assert.IsTrue (stream.CanWrite, "#D3");
1128                                 stream.Close ();
1129                         } finally {
1130                                 if (stream != null)
1131                                         stream.Close ();
1132                                 DeleteFile (path);
1133                         }
1134                 }
1135                 
1136                 [Test]
1137                 public void Open_FileDoesNotExist ()
1138                 {
1139                         string path = TempFolder + DSC + "FIT.OpenFileNotFoundException.Test";
1140                         DeleteFile (path);
1141                         
1142                         FileInfo info = new FileInfo (path);
1143                         try {
1144                                 info.Open (FileMode.Open);
1145                                 Assert.Fail ("#1");
1146                         } catch (FileNotFoundException ex) {
1147                                 Assert.AreEqual (typeof (FileNotFoundException), ex.GetType (), "#2");
1148                                 Assert.AreEqual (path, ex.FileName, "#3");
1149                                 Assert.IsNull (ex.InnerException, "#4");
1150                                 Assert.IsNotNull (ex.Message, "#5");
1151                         }
1152                 }
1153                 
1154                 [Test]
1155                 public void OpenRead ()
1156                 {
1157                         string path = TempFolder + DSC + "FIT.OpenRead.Test";
1158                         DeleteFile (path);
1159                         FileStream stream = null;
1160                         try {
1161                                 File.Create (path).Close ();
1162                                 FileInfo info = new FileInfo (path);
1163                                 stream = info.OpenRead ();
1164                                 Assert.IsTrue (stream.CanRead, "#1");
1165                                 Assert.IsTrue (stream.CanSeek, "#2");
1166                                 Assert.IsFalse (stream.CanWrite, "#3");
1167                                 stream.Close ();
1168                                 
1169                         } finally {
1170                                 if (stream != null)
1171                                         stream.Close ();
1172                                 DeleteFile (path);
1173                         }
1174                 }
1175                 
1176                 [Test]
1177                 public void OpenRead_FileLock ()
1178                 {
1179                         string path = TempFolder + DSC + "FIT.OpenReadIOException.Test";
1180                         DeleteFile (path);
1181                         FileStream stream = null;
1182                         
1183                         try {
1184                                 stream = File.Create (path);
1185                                 FileInfo info = new FileInfo (path);
1186                                 try {
1187                                         info.OpenRead ();
1188                                         Assert.Fail ("#1");
1189                                 } catch (IOException ex) {
1190                                         // The process cannot access the file because
1191                                         // it is being used by another process
1192                                         Assert.AreEqual (typeof (IOException), ex.GetType (), "#2");
1193                                         Assert.IsNull (ex.InnerException, "#3");
1194                                         Assert.IsNotNull (ex.Message, "#4");
1195                                 }
1196                         } finally {
1197                                 if (stream != null)
1198                                         stream.Close ();
1199                                 DeleteFile (path);
1200                         }
1201                 }
1202
1203                 [Test]
1204                 public void OpenRead_Directory ()
1205                 {
1206                         FileInfo info = new FileInfo (TempFolder);
1207                         try {
1208                                 info.OpenRead ();
1209                                 Assert.Fail ("#1");
1210                         } catch (UnauthorizedAccessException ex) {
1211                                 Assert.AreEqual (typeof (UnauthorizedAccessException), ex.GetType (), "#2");
1212                                 Assert.IsNull (ex.InnerException, "#3");
1213                                 Assert.IsNotNull (ex.Message, "#4");
1214                         }
1215                 }
1216                 
1217                 [Test]
1218                 public void OpenText ()
1219                 {
1220                         string path = TempFolder + DSC + "FIT.OpenText.Test";
1221                         DeleteFile (path);
1222                         StreamReader reader = null;
1223                         try {
1224                                 File.Create (path).Close ();
1225                                 FileInfo info = new FileInfo (path);
1226                                 reader = info.OpenText ();
1227                                 Assert.AreEqual (-1, reader.Peek ());
1228                         } finally {
1229                                 if (reader != null)
1230                                         reader.Close ();
1231                                 DeleteFile (path);
1232                         }
1233                 }
1234                 
1235                 [Test]
1236                 public void OpenText_FileDoesNotExist ()
1237                 {
1238                         string path = TempFolder + DSC + "FIT.OpenTextFileNotFoundException.Test";
1239                         DeleteFile (path);
1240                         FileInfo info = new FileInfo (path);
1241                         try {
1242                                 info.OpenText ();
1243                                 Assert.Fail ("#1");
1244                         } catch (FileNotFoundException ex) {
1245                                 Assert.AreEqual (typeof (FileNotFoundException), ex.GetType (), "#2");
1246                                 Assert.AreEqual (path, ex.FileName, "#3");
1247                                 Assert.IsNull (ex.InnerException, "#4");
1248                                 Assert.IsNotNull (ex.Message, "#5");
1249                         }
1250                 }
1251
1252                 [Test]
1253                 public void OpenText_Directory ()
1254                 {
1255                         FileInfo info = new FileInfo (TempFolder);
1256                         try {
1257                                 info.OpenText ();
1258                                 Assert.Fail ("#1");
1259                         } catch (UnauthorizedAccessException ex) {
1260                                 Assert.AreEqual (typeof (UnauthorizedAccessException), ex.GetType (), "#2");
1261                                 Assert.IsNull (ex.InnerException, "#3");
1262                                 Assert.IsNotNull (ex.Message, "#4");
1263                         }
1264                 }
1265
1266                 [Test]
1267                 public void OpenWrite ()
1268                 {
1269                         string path = TempFolder + DSC + "FIT.OpenWrite.Test";
1270                         DeleteFile (path);
1271                         FileStream stream = null;
1272                         try {
1273                                 File.Create (path).Close ();
1274                                 FileInfo info = new FileInfo (path);
1275                                 stream = info.OpenWrite ();
1276                                 Assert.IsFalse (stream.CanRead, "#1");
1277                                 Assert.IsTrue (stream.CanSeek, "#2");
1278                                 Assert.IsTrue (stream.CanWrite, "#3");
1279                         } finally {
1280                                 if (stream != null)
1281                                         stream.Close ();
1282                                 DeleteFile (path);
1283                         }
1284                 }
1285                 
1286                 [Test]
1287                 public void OpenWrite_Directory ()
1288                 {
1289                         FileInfo info = new FileInfo (TempFolder);
1290                         try {
1291                                 info.OpenWrite ();
1292                                 Assert.Fail ("#1");
1293                         } catch (UnauthorizedAccessException ex) {
1294                                 Assert.AreEqual (typeof (UnauthorizedAccessException), ex.GetType (), "#2");
1295                                 Assert.IsNull (ex.InnerException, "#3");
1296                                 Assert.IsNotNull (ex.Message, "#4");
1297                         }
1298                 }
1299 #if !MOBILE                     
1300                 [Test]
1301                 public void Serialization ()
1302                 {
1303                         FileInfo info;
1304                         SerializationInfo si;
1305                         
1306                         info = new FileInfo ("Test");
1307                         si = new SerializationInfo (typeof (FileInfo), new FormatterConverter ());
1308                         info.GetObjectData (si, new StreamingContext ());
1309
1310                         Assert.AreEqual (2, si.MemberCount, "#A1");
1311                         Assert.AreEqual ("Test", si.GetString ("OriginalPath"), "#A2");
1312                         Assert.AreEqual (Path.Combine (Directory.GetCurrentDirectory (), "Test"), si.GetString ("FullPath"), "#A3");
1313
1314                         info = new FileInfo (TempFolder);
1315                         si = new SerializationInfo (typeof (FileInfo), new FormatterConverter ());
1316                         info.GetObjectData (si, new StreamingContext ());
1317
1318                         Assert.AreEqual (2, si.MemberCount, "#B1");
1319                         Assert.AreEqual (TempFolder, si.GetString ("OriginalPath"), "#B2");
1320                         Assert.AreEqual (TempFolder, si.GetString ("FullPath"), "#B3");
1321                 }
1322
1323                 [Test]
1324                 public void Deserialization ()
1325                 {
1326                         FileInfo info = new FileInfo ("Test");
1327
1328                         MemoryStream ms = new MemoryStream ();
1329                         BinaryFormatter bf = new BinaryFormatter ();
1330                         bf.Serialize (ms, info);
1331                         ms.Position = 0;
1332
1333                         FileInfo clone = (FileInfo) bf.Deserialize (ms);
1334                         Assert.AreEqual (info.Name, clone.Name, "#1");
1335                         Assert.AreEqual (info.FullName, clone.FullName, "#2");
1336                 }
1337 #endif
1338
1339                 [Test]
1340                 [Category ("MobileNotWorking")]
1341                 public void ToStringVariety ()
1342                 {
1343                         Assert.AreEqual ("foo", new FileInfo ("foo").ToString ());
1344                         Assert.AreEqual ("c:/foo", new FileInfo ("c:/foo").ToString ());
1345                         Assert.AreEqual ("/usr/local/foo", new FileInfo ("/usr/local/foo").ToString ());
1346                         Assert.AreEqual ("c:\\foo", new FileInfo ("c:\\foo").ToString ());
1347                         Assert.AreEqual ("/usr/local\\foo", new FileInfo ("/usr/local\\foo").ToString ());
1348                         Assert.AreEqual ("foo/BAR/baz", new FileInfo ("foo/BAR/baz").ToString ());
1349                         Assert.AreEqual ("c:/documents and settings", new FileInfo ("c:/documents and settings").ToString ());
1350                         Assert.AreEqual ("c:/docUme~1", new FileInfo ("c:/docUme~1").ToString ());
1351                 }
1352
1353                 void DeleteFile (string path)
1354                 {
1355                         if (File.Exists (path))
1356                                 File.Delete (path);
1357                 }
1358
1359                 void DeleteDirectory (string path)
1360                 {
1361                         if (Directory.Exists (path))
1362                                 Directory.Delete (path, true);
1363                 }
1364         }
1365 }