* roottypes.cs: Rename from tree.cs.
[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
9 using NUnit.Framework;
10 using System;
11 using System.IO;
12
13 namespace MonoTests.System.IO
14 {
15         [TestFixture]
16         public class FileInfoTest : Assertion
17         {
18                 string TempFolder = Path.Combine (Path.GetTempPath (), "MonoTests.System.IO.Tests");
19                 static readonly char DSC = Path.DirectorySeparatorChar;
20
21                 [SetUp]
22                 protected void SetUp() {
23                         if (Directory.Exists (TempFolder))
24                                 Directory.Delete (TempFolder, true);
25                         Directory.CreateDirectory (TempFolder);
26                 }
27                 
28                 [TearDown]
29                 protected void TearDown() {
30                         if (Directory.Exists (TempFolder))
31                                 Directory.Delete (TempFolder, true);
32                 }
33                 
34                 [Test]
35                 public void Ctr ()
36                 {
37                         string path = TempFolder + DSC + "FIT.Ctr.Test";
38                         DeleteFile (path);
39                         
40                         FileInfo info = new FileInfo (path);
41                         AssertEquals ("test#01", true, info.DirectoryName.EndsWith (".Tests"));
42                         AssertEquals ("test#02", false, info.Exists);
43                         AssertEquals ("test#03", ".Test", info.Extension);
44                         AssertEquals ("test#05", "FIT.Ctr.Test", info.Name);                    
45                 }
46                 
47                 [Test]
48                 [ExpectedException(typeof(ArgumentNullException))]
49                 public void CtorArgumentNullException ()
50                 {
51                         FileInfo info = new FileInfo (null);                    
52                 }
53                 
54                 [Test]
55                 [ExpectedException(typeof(ArgumentException))]
56                 public void CtorArgumentException1 ()
57                 {
58                         FileInfo info = new FileInfo ("");                      
59                 }
60                 
61                 [Test]
62                 [ExpectedException(typeof(ArgumentException))]
63                 public void CtorArgumentException2 ()
64                 {
65                         FileInfo info = new FileInfo ("      ");                        
66                 }
67
68                 [Test]
69                 [ExpectedException(typeof(ArgumentException))]
70                 public void CtorArgumentException3 ()
71                 {
72                         string path = "";
73                         foreach (char c in Path.InvalidPathChars) {
74                                 path += c;
75                         }
76                         FileInfo info = new FileInfo (path);                    
77                 }
78                                
79                 [Test]
80                 public void DirectoryTest ()
81                 {
82                         string path = TempFolder + DSC + "FIT.Directory.Test";
83                         DeleteFile (path);
84                         
85                         FileInfo info = new FileInfo (path);
86                         DirectoryInfo dir = info.Directory;
87                         AssertEquals ("test#01", "MonoTests.System.IO.Tests", dir.Name);
88                 }
89                 
90                 [Test]
91                 public void Exists ()
92                 {
93                         string path = TempFolder + DSC + "FIT.Exists.Test";
94                         DeleteFile (path);
95                         
96                         try {
97                                 FileInfo info = new FileInfo (path);
98                                 AssertEquals ("test#01", false, info.Exists);
99                         
100                                 File.Create (path).Close ();
101                                 AssertEquals ("test#02", false, info.Exists);
102                                 info = new FileInfo (path);
103                                 AssertEquals ("test#03", true, info.Exists);                    
104                         } finally {
105                                 DeleteFile (path);
106                         }
107                 }
108                 
109                 [Test]
110                 public void Length ()
111                 {
112                         string path = TempFolder + DSC + "FIT.Length.Test";
113                         DeleteFile (path);
114                         
115                         try {
116                                 FileStream stream = File.Create (path);
117                                 FileInfo info = new FileInfo (path);
118                                 AssertEquals ("test#01", 0, info.Length);
119                                 stream.WriteByte (12);
120                                 stream.Flush ();
121                                 AssertEquals ("test#02", 0, info.Length);
122                                 info = new FileInfo (path);
123                                 AssertEquals ("test#03", 1, info.Length);
124                                 stream.Close ();
125                         } finally {
126                                 DeleteFile (path);
127                         }
128                 }
129                 
130                 [Test]
131                 [ExpectedException (typeof (FileNotFoundException))]
132                 public void LengthException ()
133                 {
134                         string path = TempFolder + DSC + "FIT.LengthException.Test";
135                         DeleteFile (path);
136                         FileInfo info = new FileInfo (path);
137                         long l = info.Length;
138                 }
139                 
140                 [Test]
141                 public void AppendText ()
142                 {
143                         string path = TempFolder + DSC + "FIT.AppendText.Test";
144                         DeleteFile (path);
145                         
146                         try {
147                                 FileInfo info = new FileInfo (path);
148                         
149                                 AssertEquals ("test#01", false, info.Exists);
150                         
151                                 StreamWriter writer = info.AppendText ();
152                                 info = new FileInfo (path);
153                                 AssertEquals ("test#02", true, info.Exists );
154                                 
155                                 writer.Write ("aaa");
156                                 writer.Flush ();
157                                 writer.Close ();
158                         
159                                 AssertEquals ("test#03", 0, info.Length);
160                                 info = new FileInfo (path);
161                                 AssertEquals ("test#04", 3, info.Length);
162                         } finally {
163                                 DeleteFile (path);
164                         }
165                         
166                 }
167                 
168                 [Test]
169                 public void CopyTo ()
170                 {
171                         string path1 = TempFolder + DSC + "FIT.CopyTo.Source.Test";
172                         string path2 = TempFolder + DSC + "FIT.CopyTo.Dest.Test";
173                         DeleteFile (path1);
174                         DeleteFile (path2);
175                         try {
176                                 File.Create (path1).Close ();
177                         
178                                 FileInfo info = new FileInfo (path1);
179                                 AssertEquals ("test#01", true, info.Exists);
180                                                 
181                                 FileInfo info2 = info.CopyTo (path2);
182                                 info = new FileInfo (path1);
183                                 AssertEquals ("test#02", true, info2.Exists);
184                         } finally {
185                                 DeleteFile (path1);
186                                 DeleteFile (path2);                     
187                         }
188                 }
189                 
190                 [Test]
191                 public void CopyTo2 ()
192                 {
193                         string path1 = TempFolder + DSC + "FIT.CopyTo2.Source.Test";
194                         string path2 = TempFolder + DSC + "FIT.CopyTo2.Dest.Test";
195                         DeleteFile (path1);
196                         DeleteFile (path2);
197                         try {
198                                 File.Create (path1).Close ();
199                                 File.Create (path2).Close ();           
200                                 FileInfo info = new FileInfo (path1);
201                         
202                                 FileInfo info2 = info.CopyTo (path2, true);
203                                 info = new FileInfo (path1);
204                                 AssertEquals ("test#01", true, info.Exists);
205                                 AssertEquals ("test#02", true, info2.Exists);
206                         } finally {
207                                 DeleteFile (path1);
208                                 DeleteFile (path2);
209                         }
210                 }
211                 
212                 [Test]
213                 [ExpectedException (typeof (IOException))]
214                 public void CopyToIOException ()
215                 {
216                         string path1 = TempFolder + DSC + "FIT.CopyToException.Source.Test";
217                         string path2 = TempFolder + DSC + "FIT.CopyToException.Dest.Test";
218
219                         try {
220                                 DeleteFile (path1);
221                                 DeleteFile (path2);
222                                 File.Create (path1).Close ();
223                                 File.Create (path2).Close ();           
224                                 FileInfo info = new FileInfo (path1);                   
225                                 FileInfo info2 = info.CopyTo (path2);                   
226                         } finally {
227                                 DeleteFile (path1);
228                                 DeleteFile (path2);
229                         }
230                 }
231
232                 [Test]
233                 [ExpectedException (typeof (IOException))]
234                 public void CopyToIOException2 ()
235                 {
236                         string path1 = TempFolder + DSC + "FIT.CopyToException.Source.Test";
237                         string path2 = TempFolder + DSC + "FIT.CopyToException.Dest.Test";
238
239                         try {
240                                 DeleteFile (path1);
241                                 DeleteFile (path2);
242                                 File.Create (path1).Close ();
243                                 File.Create (path2).Close ();           
244                                 FileInfo info = new FileInfo (path1);                   
245                                 FileInfo info2 = info.CopyTo (path2, false);
246                         } finally {
247                                 DeleteFile (path1);
248                                 DeleteFile (path2);
249                         }
250                 }
251                 
252                 [Test]
253                 [ExpectedException (typeof (ArgumentNullException))]
254                 public void CopyToArgumentNullException ()
255                 {
256                         string path = TempFolder + DSC + "FIT.CopyToArgumentNullException.Test";
257                         DeleteFile (path);
258                         try {
259                                 File.Create (path).Close ();
260                                 FileInfo info = new FileInfo (path);
261                                 info.CopyTo (null, false);
262                         } finally {
263                                 DeleteFile (path);
264                         }
265                 }
266
267                 [Test]
268                 [ExpectedException (typeof (ArgumentException))]
269                 public void CopyToArgumentException1 ()
270                 {
271                         string path = TempFolder + DSC + "FIT.CopyToArgument1Exception.Test";
272                         DeleteFile (path);
273                         
274                         try {
275                                 File.Create (path).Close ();
276                                 FileInfo info = new FileInfo (path);
277                                 info.CopyTo ("", false);
278                         } finally {
279                                 DeleteFile (path);
280                         }
281                 }
282
283                 [Test]
284                 [ExpectedException (typeof (ArgumentException))]
285                 public void CopyToArgumentException2 ()
286                 {
287                         string path = TempFolder + DSC + "FIT.CopyToArgument2Exception.Test";
288                         DeleteFile (path);
289                         
290                         try {
291                                 File.Create (path).Close ();
292                                 FileInfo info = new FileInfo (path);
293                                 info.CopyTo ("    ", false);
294                         } finally {
295                                 DeleteFile (path);
296                         }
297                 }
298
299                 [Test]
300                 [ExpectedException (typeof (ArgumentException))]
301                 public void CopyToArgumentException3 ()
302                 {
303                         string path = TempFolder + DSC + "FIT.CopyToArgument3Exception.Test";
304                         DeleteFile (path);
305                         
306                         try {
307                                 File.Create (path).Close ();
308                                 FileInfo info = new FileInfo (path);
309                                 info.CopyTo ("    ", false);
310                         } finally {
311                                 DeleteFile (path);
312                         }
313                 }
314
315                 [Test]
316                 [ExpectedException (typeof (ArgumentException))]
317                 public void CopyToArgumentException4 ()
318                 {
319                         string path = TempFolder + DSC + "FIT.CopyToArgument4Exception.Test";
320                         string path2 = "";
321                         DeleteFile (path);
322                         
323                         try {
324                                 File.Create (path).Close ();
325                                 FileInfo info = new FileInfo (path);
326                         
327                                 foreach (char c in Path.InvalidPathChars) {
328                                         path2 += c;
329                                 }
330                                 info.CopyTo (path2, false);
331                         } finally {
332                                 DeleteFile (path);
333                         }
334                 }
335                 
336                 [Test]
337                 public void Create ()
338                 {
339                         string path = TempFolder + DSC + "FIT.Create.Test";
340                         DeleteFile (path);
341                         
342                         try {
343                                 FileInfo info = new FileInfo (path);
344                                 AssertEquals ("test#01", false, info.Exists);
345                                 FileStream stream = info.Create ();                             
346                                 AssertEquals ("test#02", false, info.Exists);
347                                 info = new FileInfo (path);
348                                 AssertEquals ("test#03", true, info.Exists);
349                                 AssertEquals ("test#04", true, stream.CanRead);
350                                 AssertEquals ("test#05", true, stream.CanWrite);
351                                 AssertEquals ("test#06", true, stream.CanSeek);
352                                 stream.Close ();
353                         } finally {
354                                 DeleteFile (path);
355                         }
356                 }
357                 
358                 [Test]
359                 public void CreateText ()
360                 {
361                         string path = TempFolder + DSC + "FIT.CreateText.Test";
362                         DeleteFile (path);
363                         
364                         try {
365                                 FileInfo info = new FileInfo (path);
366                                 AssertEquals ("test#01", false, info.Exists);
367                                 StreamWriter writer = info.CreateText ();
368                                 writer.WriteLine ("test");
369                                 writer.Close ();
370                                 info = new FileInfo (path);
371                                 AssertEquals ("test#02", true, info.Exists);
372                         } finally {
373                                 DeleteFile (path);
374                         }
375                 }
376                 
377                 [Test]
378                 [ExpectedException (typeof (UnauthorizedAccessException))]
379                 public void CreateTextUnauthorizedAccessException ()
380                 {
381                         string path = TempFolder;
382                         
383                         FileInfo info = new FileInfo (path);
384                         AssertEquals ("test#01", false, info.Exists);
385                         StreamWriter writer = info.CreateText ();
386                         
387                 }
388                 
389                 [Test]
390                 public void Delete ()
391                 {
392                         string path = TempFolder + DSC + "FIT.Delete.Test";
393                         DeleteFile (path);
394                         
395                         try {
396                                 FileInfo info = new FileInfo (path);
397                                 AssertEquals ("test#01", false, info.Exists);
398                                 info.Create ().Close ();
399                                 info = new FileInfo (path);
400                                 AssertEquals ("test#02", true, info.Exists);
401                                 info.Delete ();
402                                 AssertEquals ("test#03", true, info.Exists);
403                                 info = new FileInfo (path);
404                                 AssertEquals ("test#04", false, info.Exists);                                                           
405                         } finally {
406                                 DeleteFile (path);
407                         }
408                 }
409                 
410                 [Test]
411                 [ExpectedException (typeof (UnauthorizedAccessException))]
412                 public void DeleteUnauthorizedAccessException ()
413                 {
414                         string path = TempFolder;
415                         FileInfo info = new FileInfo (path);
416                         info.Delete ();                 
417                 }
418                 
419                 [Test]
420                 public void MoveTo ()
421                 {
422                         string path1 = TempFolder + DSC + "FIT.MoveTo.Source.Test";
423                         string path2 = TempFolder + DSC + "FIT.MoveTo.Dest.Test";
424                         DeleteFile (path1);
425                         DeleteFile (path2);
426                         
427                         try {
428                                 File.Create (path1).Close ();
429                                 FileInfo info = new FileInfo (path1);
430                                 FileInfo info2 = new FileInfo (path2);
431                                 AssertEquals ("test#01", false, info2.Exists);
432                                 
433                                 info.MoveTo (path2);
434                                 info2 = new FileInfo (path2);
435                                 AssertEquals ("test#02", true, info2.Exists);   
436                         } finally {
437                                 DeleteFile (path1);
438                                 DeleteFile (path2);                     
439                         }                       
440                 }
441                 
442                 [Test]
443                 [ExpectedException (typeof (IOException))]
444                 public void MoveToIOException ()
445                 {
446                         string path1 = TempFolder + DSC + "FIT.MoveToIOException.Source.Test";
447                         string path2 = TempFolder + DSC + "FIT.MoveToIOException.Dest.Test";
448                         DeleteFile (path1);
449                         DeleteFile (path2);
450
451                         try {
452                                 File.Create (path1).Close ();
453                                 File.Create (path2).Close ();
454                                 
455                                 FileInfo info = new FileInfo (path1);
456                                 info.MoveTo (path2);
457                         } finally {
458                                 DeleteFile (path1);
459                                 DeleteFile (path2);
460                         }
461                 }
462
463                 [Test]
464                 [ExpectedException (typeof (ArgumentNullException))]
465                 public void MoveToArgumentNullException ()
466                 {
467                         string path = TempFolder + DSC + "FIT.MoveToArgumentNullException.Test";
468                         DeleteFile (path);
469
470                         try {
471                                 File.Create (path).Close ();                            
472                                 FileInfo info = new FileInfo (path);
473                                 info.MoveTo (null);
474                         } finally {
475                                 DeleteFile (path);
476                         }
477                 }
478
479                 [Test]
480                 [ExpectedException (typeof (ArgumentException))]
481                 public void MoveToArgumentException ()
482                 {
483                         string path = TempFolder + DSC + "FIT.MoveToArgumentException.Test";
484                         DeleteFile (path);
485
486                         try {
487                                 File.Create (path).Close ();                            
488                                 FileInfo info = new FileInfo (path);
489                                 info.MoveTo ("   ");
490                         } finally {
491                                 DeleteFile (path);
492                         }
493                 }
494
495                 /// <summary>
496                 /// msdn says this should throw UnauthorizedAccessException, byt
497                 /// it throws IOException.
498                 /// </summary>
499                 [Test]
500                 [ExpectedException (typeof (IOException))]
501                 public void MoveToUnauthorizedAccessException ()
502                 {
503                         string path = TempFolder + DSC + "FIT.UnauthorizedAccessException.Test";
504                         DeleteFile (path);
505
506                         try {
507                                 File.Create (path).Close ();
508                                 FileInfo info = new FileInfo (path);
509                                 info.MoveTo (TempFolder);
510                         } finally {
511                                 DeleteFile (path);
512                         }
513                 }
514                 
515                 [Test]
516                 [ExpectedException (typeof (FileNotFoundException))]
517                 public void MoveToFileNotFoundException ()
518                 {
519                         string path1 = TempFolder + DSC + "FIT.MoveToFileNotFoundException.Src";
520                         string path2 = TempFolder + DSC + "FIT.MoveToFileNotFoundException.Dst";
521                         DeleteFile (path1);
522                         DeleteFile (path2);
523                         
524                         try {
525                                 FileInfo info = new FileInfo (path1);
526                                 info.MoveTo (path2);
527                         } finally {
528                                 DeleteFile (path1);
529                                 DeleteFile (path2);
530                         }
531                 }
532                                 
533                 [Test]
534                 public void Open ()
535                 {
536                         string path = TempFolder + DSC + "FIT.Open.Test";
537                         DeleteFile (path);
538                         FileStream stream = null;
539                         try {
540                                 FileInfo info = new FileInfo (path);
541                                 stream = info.Open (FileMode.CreateNew);
542                                 AssertEquals ("test#01", true, stream.CanRead);
543                                 AssertEquals ("test#02", true, stream.CanSeek);
544                                 AssertEquals ("test#03", true, stream.CanWrite);                                
545                                 stream.Close ();
546                                 
547                                 stream = info.Open (FileMode.Open);                             
548                                 AssertEquals ("test#04", true, stream.CanRead);
549                                 AssertEquals ("test#05", true, stream.CanSeek);
550                                 AssertEquals ("test#06", true, stream.CanWrite);                                
551                                 stream.Close ();
552                                 
553                                 stream = info.Open (FileMode.Append, FileAccess.Write);
554                                 AssertEquals ("test#07", false, stream.CanRead);
555                                 AssertEquals ("test#08", true, stream.CanSeek);
556                                 AssertEquals ("test#09", true, stream.CanWrite);                                
557                                 stream.Close ();                                
558
559                                 stream = info.Open (FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite);
560                                 AssertEquals ("test#10", true, stream.CanRead);
561                                 AssertEquals ("test#11", true, stream.CanSeek);
562                                 AssertEquals ("test#12", true, stream.CanWrite);                                
563                                 stream.Close ();                                                                                                
564                         } finally {
565                                 if (stream != null)
566                                         stream.Close ();
567                                 DeleteFile (path);
568                         }
569                 }
570                 
571                 [Test]
572                 [ExpectedException(typeof(FileNotFoundException))]
573                 public void OpenFileNotFoundException ()
574                 {
575                         string path = TempFolder + DSC + "FIT.OpenFileNotFoundException.Test";
576                         DeleteFile (path);
577                         
578                         FileInfo info = new FileInfo (path);
579                         info.Open (FileMode.Open);
580                 }
581                 
582                 [Test]
583                 public void OpenRead ()
584                 {
585                         string path = TempFolder + DSC + "FIT.OpenRead.Test";
586                         DeleteFile (path);
587                         FileStream stream = null;
588                         try {
589                                 File.Create (path).Close ();
590                                 FileInfo info = new FileInfo (path);
591                                 stream = info.OpenRead ();
592                                 AssertEquals ("test#01", true, stream.CanRead);
593                                 AssertEquals ("test#02", true, stream.CanSeek);
594                                 AssertEquals ("test#03", false, stream.CanWrite);
595                                 stream.Close ();
596                                 
597                         } finally {
598                                 if (stream != null)
599                                         stream.Close ();
600                                 DeleteFile (path);
601                         }
602                 }
603                 
604                 [Test]
605                 [ExpectedException(typeof (IOException))]
606                 public void OpenReadIOException ()
607                 {
608                         string path = TempFolder + DSC + "FIT.OpenReadIOException.Test";
609                         DeleteFile (path);
610                         FileStream stream = null;
611                         
612                         try {
613                                 stream = File.Create (path);
614                                 FileInfo info = new FileInfo (path);
615                                 info.OpenRead ();
616                         } finally {
617                                 if (stream != null)
618                                         stream.Close ();
619                                 DeleteFile (path);
620                         }
621                 }
622
623                 [Test]
624                 [ExpectedException(typeof (UnauthorizedAccessException))]
625                 public void OpenReadUnauthorizedAccessException ()
626                 {
627                         string path = TempFolder;
628                         
629                         FileInfo info = new FileInfo (path);
630                         info.OpenRead ();
631                 }
632                 
633                 [Test]
634                 public void OpenText ()
635                 {
636                         string path = TempFolder + DSC + "FIT.OpenText.Test";
637                         DeleteFile (path);
638                         StreamReader reader = null;
639                         try {
640                                 File.Create (path).Close ();
641                                 FileInfo info = new FileInfo (path);
642                                 reader = info.OpenText ();
643                                 AssertEquals ("test#01", -1, reader.Peek ());           
644                         } finally {
645                                 
646                                 if (reader != null)
647                                         reader.Close ();
648                                 DeleteFile (path);
649                         }
650                 }
651                 
652                 [Test]
653                 [ExpectedException(typeof (FileNotFoundException))]
654                 public void OpenTextFileNotFoundException ()
655                 {
656                         string path = TempFolder + DSC + "FIT.OpenTextFileNotFoundException.Test";
657                         DeleteFile (path);
658                         FileInfo info = new FileInfo (path);
659                         info.OpenText ();
660                 }
661
662                 [Test]
663                 [ExpectedException(typeof (UnauthorizedAccessException))]
664                 public void OpenTextUnauthorizedAccessException ()
665                 {
666                         string path = TempFolder;
667                         
668                         FileInfo info = new FileInfo (path);
669                         info.OpenText ();
670                 }
671
672                 [Test]
673                 public void OpenWrite ()
674                 {
675                         string path = TempFolder + DSC + "FIT.OpenWrite.Test";
676                         DeleteFile (path);
677                         FileStream stream = null;
678                         try {
679                                 File.Create (path).Close ();
680                                 FileInfo info = new FileInfo (path);
681                                 stream = info.OpenWrite ();
682                                 AssertEquals ("test#01", false, stream.CanRead);
683                                 AssertEquals ("test#02", true, stream.CanSeek);
684                                 AssertEquals ("test#03", true, stream.CanWrite);
685                         } finally {
686                                 
687                                 if (stream != null)
688                                         stream.Close ();
689                                 DeleteFile (path);
690                         }
691                 }
692                 
693                 [Test]
694                 [ExpectedException(typeof (UnauthorizedAccessException))]
695                 public void OpenWriteUnauthorizedAccessException ()
696                 {
697                         string path = TempFolder;
698                         
699                         FileInfo info = new FileInfo (path);
700                         info.OpenWrite ();
701                 }
702
703                 private void DeleteFile (string path)
704                 {
705                         if (File.Exists (path))
706                                 File.Delete (path);
707                 }
708
709                 [Test]
710                 public void ToStringVariety ()
711                 {
712                         AssertEquals ("foo", new FileInfo ("foo").ToString ());
713                         AssertEquals ("c:/foo", new FileInfo ("c:/foo").ToString ());
714                         AssertEquals ("/usr/local/foo", new FileInfo ("/usr/local/foo").ToString ());
715                         AssertEquals ("c:\\foo", new FileInfo ("c:\\foo").ToString ());
716                         AssertEquals ("/usr/local\\foo", new FileInfo ("/usr/local\\foo").ToString ());
717                         AssertEquals ("foo/BAR/baz", new FileInfo ("foo/BAR/baz").ToString ());
718                         AssertEquals ("c:/documents and settings", new FileInfo ("c:/documents and settings").ToString ());
719                         AssertEquals ("c:/docUme~1", new FileInfo ("c:/docUme~1").ToString ());
720                 }
721         }
722 }
723