[LoongArch64] Part-5:add loongarch support in some files for LoongArch64. (#21769)
[mono-project.git] / mcs / class / corlib / Test / System.IO / FileInfoTest.cs
blob68618e44c401a34d9c4a6fd18a0437e75d3dfe72
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 //
8 using System;
9 using System.IO;
10 using System.Runtime.Serialization;
11 using System.Runtime.Serialization.Formatters.Binary;
13 using NUnit.Framework;
15 namespace MonoTests.System.IO
17 [TestFixture]
18 public class FileInfoTest
20 string TempFolder = Path.Combine (Path.GetTempPath (), "MonoTests.System.IO.Tests");
21 static readonly char DSC = Path.DirectorySeparatorChar;
23 [SetUp]
24 public void SetUp ()
26 DeleteDirectory (TempFolder);
27 Directory.CreateDirectory (TempFolder);
30 [TearDown]
31 public void TearDown ()
33 DeleteDirectory (TempFolder);
36 [Test] // ctor (String)
37 public void Constructor1 ()
39 string path = TempFolder + DSC + "FIT.Ctr.Test";
40 DeleteFile (path);
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");
49 [Test] // ctor (String)
50 public void Constructor1_FileName_Null ()
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");
63 [Test] // ctor (String)
64 public void Constructor1_FileName_Empty ()
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");
78 [Test] // ctor (String)
79 public void Constructor1_FileName_InvalidPathChars ()
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");
95 [Test] // ctor (String)
96 public void Constructor1_FileName_Whitespace ()
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");
110 [Test]
111 public void DirectoryTest ()
113 string path = TempFolder + DSC + "FIT.Directory.Test";
114 DeleteFile (path);
116 FileInfo info = new FileInfo (path);
117 DirectoryInfo dir = info.Directory;
118 Assert.AreEqual ("MonoTests.System.IO.Tests", dir.Name);
121 [Test]
122 public void Exists ()
124 string path = TempFolder + DSC + "FIT.Exists.Test";
125 DeleteFile (path);
127 try {
128 FileInfo info = new FileInfo (path);
129 Assert.IsFalse (info.Exists, "#1");
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);
142 #if !MOBILE
143 [Test]
144 public void IsReadOnly ()
146 string path = TempFolder + DSC + "FIT.IsReadOnly.Test";
147 DeleteFile (path);
149 try {
150 using (FileStream stream = File.Create (path)) {
151 stream.WriteByte (12);
152 stream.Close ();
155 FileInfo info1 = new FileInfo (path);
156 Assert.IsFalse (info1.IsReadOnly, "#1");
158 FileInfo info2 = new FileInfo (path);
159 info2.IsReadOnly = true;
160 Assert.IsFalse (info1.IsReadOnly, "#2");
161 Assert.IsTrue (info2.IsReadOnly, "#3");
163 FileInfo info3 = new FileInfo (path);
164 Assert.IsTrue (info3.IsReadOnly, "#4");
165 info3.IsReadOnly = false;
166 Assert.IsFalse (info1.IsReadOnly, "#4");
167 Assert.IsTrue (info2.IsReadOnly, "#5");
168 Assert.IsFalse (info3.IsReadOnly, "#6");
169 } finally {
170 File.SetAttributes (path, FileAttributes.Normal);
171 DeleteFile (path);
174 #endif
176 [Test]
177 public void Length ()
179 string path = TempFolder + DSC + "FIT.Length.Test";
180 DeleteFile (path);
182 try {
183 FileStream stream = File.Create (path);
184 FileInfo info = new FileInfo (path);
185 Assert.AreEqual (0, info.Length, "#1");
186 stream.WriteByte (12);
187 stream.Flush ();
188 Assert.AreEqual (0, info.Length, "#2");
189 info = new FileInfo (path);
190 Assert.AreEqual (1, info.Length, "#3");
191 stream.Close ();
192 } finally {
193 DeleteFile (path);
197 [Test]
198 public void Length_FileDoesNotExist ()
200 string path = TempFolder + DSC + "FIT.LengthException.Test";
201 DeleteFile (path);
202 FileInfo info = new FileInfo (path);
203 try {
204 long l = info.Length;
205 Assert.Fail ("#1:" + l);
206 } catch (FileNotFoundException ex) {
207 Assert.AreEqual (typeof (FileNotFoundException), ex.GetType (), "#2");
208 Assert.AreEqual (path, ex.FileName, "#3");
209 Assert.IsNull (ex.InnerException, "#4");
210 Assert.IsNotNull (ex.Message, "#5");
214 [Test]
215 public void AppendText ()
217 string path = TempFolder + DSC + "FIT.AppendText.Test";
218 DeleteFile (path);
220 try {
221 FileInfo info = new FileInfo (path);
222 Assert.IsFalse (info.Exists, "#1");
224 StreamWriter writer = info.AppendText ();
225 info = new FileInfo (path);
226 Assert.IsTrue (info.Exists, "#2");
228 writer.Write ("aaa");
229 writer.Flush ();
230 writer.Close ();
232 Assert.AreEqual (0, info.Length, "#3");
233 info = new FileInfo (path);
234 Assert.AreEqual (3, info.Length, "#4");
235 } finally {
236 DeleteFile (path);
240 [Test] // CopyTo (String)
241 public void CopyTo1 ()
243 string path1 = TempFolder + DSC + "FIT.CopyTo.Source.Test";
244 string path2 = TempFolder + DSC + "FIT.CopyTo.Dest.Test";
245 DeleteFile (path1);
246 DeleteFile (path2);
247 try {
248 File.Create (path1).Close ();
250 FileInfo info = new FileInfo (path1);
251 Assert.IsTrue (info.Exists, "#1");
253 FileInfo info2 = info.CopyTo (path2);
254 info = new FileInfo (path1);
255 Assert.IsTrue (info2.Exists, "#2");
256 } finally {
257 DeleteFile (path1);
258 DeleteFile (path2);
262 [Test] // CopyTo (String)
263 public void CopyTo1_DestFileName_AlreadyExists ()
265 string path1 = TempFolder + DSC + "FIT.CopyToException.Source.Test";
266 string path2 = TempFolder + DSC + "FIT.CopyToException.Dest.Test";
268 try {
269 DeleteFile (path1);
270 DeleteFile (path2);
271 File.Create (path1).Close ();
272 File.Create (path2).Close ();
273 FileInfo info = new FileInfo (path1);
274 try {
275 info.CopyTo (path2);
276 Assert.Fail ("#1");
277 } catch (IOException ex) {
278 // The file '...' already exists.
279 Assert.AreEqual (typeof (IOException), ex.GetType (), "#2");
280 Assert.IsNull (ex.InnerException, "#3");
281 Assert.IsNotNull (ex.Message, "#4");
282 Assert.IsTrue (ex.Message.IndexOf (path2) != -1, "#5");
284 } finally {
285 DeleteFile (path1);
286 DeleteFile (path2);
290 [Test] // CopyTo (String)
291 public void CopyTo1_DestFileName_Null ()
293 string path = TempFolder + DSC + "FIT.CopyToArgumentNullException.Test";
294 DeleteFile (path);
295 try {
296 File.Create (path).Close ();
297 FileInfo info = new FileInfo (path);
298 try {
299 info.CopyTo (null);
300 Assert.Fail ("#1");
301 } catch (ArgumentNullException ex) {
302 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
303 Assert.IsNull (ex.InnerException, "#3");
304 Assert.IsNotNull (ex.Message, "#4");
305 Assert.AreEqual ("destFileName", ex.ParamName, "#5");
307 } finally {
308 DeleteFile (path);
312 [Test] // CopyTo (String)
313 public void CopyTo1_DestFileName_Empty ()
315 string path = TempFolder + DSC + "FIT.CopyToArgument1Exception.Test";
316 DeleteFile (path);
318 try {
319 File.Create (path).Close ();
320 FileInfo info = new FileInfo (path);
321 try {
322 info.CopyTo (string.Empty);
323 Assert.Fail ("#1");
324 } catch (ArgumentException ex) {
325 // Empty file name is not legal
326 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
327 Assert.IsNull (ex.InnerException, "#3");
328 Assert.IsNotNull (ex.Message, "#4");
329 Assert.AreEqual ("destFileName", ex.ParamName, "#5");
331 } finally {
332 DeleteFile (path);
336 [Test] // CopyTo (String)
337 public void CopyTo1_DestFileName_Whitespace ()
339 string path = TempFolder + DSC + "FIT.CopyToArgument2Exception.Test";
340 DeleteFile (path);
342 try {
343 File.Create (path).Close ();
344 FileInfo info = new FileInfo (path);
345 try {
346 info.CopyTo (" ");
347 Assert.Fail ("#1");
348 } catch (ArgumentException ex) {
349 // The path is not of a legal form
350 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
351 Assert.IsNotNull (ex.Message, "#4");
352 Assert.IsNull (ex.ParamName, "#5");
354 } finally {
355 DeleteFile (path);
359 [Test] // CopyTo (String)
360 public void CopyTo1_DestFileName_InvalidPathChars ()
362 string path = TempFolder + DSC + "FIT.CopyToArgument4Exception.Test";
363 string path2 = string.Empty;
364 DeleteFile (path);
366 try {
367 File.Create (path).Close ();
368 FileInfo info = new FileInfo (path);
369 foreach (char c in Path.InvalidPathChars)
370 path2 += c;
371 try {
372 info.CopyTo (path2);
373 Assert.Fail ("#1");
374 } catch (ArgumentException ex) {
375 // Illegal characters in path
376 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
377 Assert.IsNull (ex.InnerException, "#3");
378 Assert.IsNotNull (ex.Message, "#4");
379 Assert.IsNull (ex.ParamName, "#5");
381 } finally {
382 DeleteFile (path);
386 [Test] // CopyTo (String, Boolean)
387 public void CopyTo2 ()
389 string path1 = TempFolder + DSC + "FIT.CopyTo2.Source.Test";
390 string path2 = TempFolder + DSC + "FIT.CopyTo2.Dest.Test";
391 DeleteFile (path1);
392 DeleteFile (path2);
393 try {
394 File.Create (path1).Close ();
395 File.Create (path2).Close ();
396 FileInfo info = new FileInfo (path1);
398 FileInfo info2 = info.CopyTo (path2, true);
399 info = new FileInfo (path1);
400 Assert.IsTrue (info.Exists, "#1");
401 Assert.IsTrue (info2.Exists, "#2");
402 } finally {
403 DeleteFile (path1);
404 DeleteFile (path2);
408 [Test] // CopyTo (String, Boolean)
409 public void CopyTo2_DestFileName_AlreadyExists ()
411 string path1 = TempFolder + DSC + "FIT.CopyToException.Source.Test";
412 string path2 = TempFolder + DSC + "FIT.CopyToException.Dest.Test";
414 try {
415 DeleteFile (path1);
416 DeleteFile (path2);
417 File.Create (path1).Close ();
418 File.Create (path2).Close ();
419 FileInfo info = new FileInfo (path1);
420 try {
421 info.CopyTo (path2, false);
422 Assert.Fail ("#1");
423 } catch (IOException ex) {
424 // The file '...' already exists.
425 Assert.AreEqual (typeof (IOException), ex.GetType (), "#2");
426 Assert.IsNull (ex.InnerException, "#3");
427 Assert.IsNotNull (ex.Message, "#4");
428 Assert.IsTrue (ex.Message.IndexOf (path2) != -1, "#5");
430 } finally {
431 DeleteFile (path1);
432 DeleteFile (path2);
436 [Test] // CopyTo (String, Boolean)
437 public void CopyTo2_DestFileName_Null ()
439 string path = TempFolder + DSC + "FIT.CopyToArgumentNullException.Test";
440 DeleteFile (path);
441 try {
442 File.Create (path).Close ();
443 FileInfo info = new FileInfo (path);
444 try {
445 info.CopyTo (null, false);
446 Assert.Fail ("#1");
447 } catch (ArgumentNullException ex) {
448 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
449 Assert.IsNull (ex.InnerException, "#3");
450 Assert.IsNotNull (ex.Message, "#4");
451 Assert.AreEqual ("destFileName", ex.ParamName, "#5");
453 } finally {
454 DeleteFile (path);
458 [Test] // CopyTo (String, Boolean)
459 public void CopyTo2_DestFileName_Empty ()
461 string path = TempFolder + DSC + "FIT.CopyToArgument1Exception.Test";
462 DeleteFile (path);
464 try {
465 File.Create (path).Close ();
466 FileInfo info = new FileInfo (path);
467 try {
468 info.CopyTo (string.Empty, false);
469 Assert.Fail ("#1");
470 } catch (ArgumentException ex) {
471 // Empty file name is not legal
472 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
473 Assert.IsNull (ex.InnerException, "#3");
474 Assert.IsNotNull (ex.Message, "#4");
475 Assert.AreEqual ("destFileName", ex.ParamName, "#5");
477 } finally {
478 DeleteFile (path);
482 [Test] // CopyTo (String, Boolean)
483 public void CopyTo2_DestFileName_Whitespace ()
485 string path = TempFolder + DSC + "FIT.CopyToArgument2Exception.Test";
486 DeleteFile (path);
488 try {
489 File.Create (path).Close ();
490 FileInfo info = new FileInfo (path);
491 try {
492 info.CopyTo (" ", false);
493 Assert.Fail ("#1");
494 } catch (ArgumentException ex) {
495 // The path is not of a legal form
496 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
497 Assert.IsNull (ex.InnerException, "#3");
498 Assert.IsNotNull (ex.Message, "#4");
499 Assert.IsNull (ex.ParamName, "#5");
501 } finally {
502 DeleteFile (path);
506 [Test] // CopyTo (String, Boolean)
507 public void CopyTo2_DestFileName_InvalidPathChars ()
509 string path = TempFolder + DSC + "FIT.CopyToArgument4Exception.Test";
510 string path2 = string.Empty;
511 DeleteFile (path);
513 try {
514 File.Create (path).Close ();
515 FileInfo info = new FileInfo (path);
516 foreach (char c in Path.InvalidPathChars)
517 path2 += c;
518 try {
519 info.CopyTo (path2, false);
520 Assert.Fail ("#1");
521 } catch (ArgumentException ex) {
522 // Illegal characters in path
523 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
524 Assert.IsNull (ex.InnerException, "#3");
525 Assert.IsNotNull (ex.Message, "#4");
526 Assert.IsNull (ex.ParamName, "#5");
528 } finally {
529 DeleteFile (path);
533 [Test]
534 public void Create ()
536 string path = TempFolder + DSC + "FIT.Create.Test";
537 DeleteFile (path);
539 try {
540 FileInfo info = new FileInfo (path);
541 Assert.IsFalse (info.Exists, "#1");
542 FileStream stream = info.Create ();
543 Assert.IsFalse (info.Exists, "#2");
544 info = new FileInfo (path);
545 Assert.IsTrue (info.Exists, "#3");
546 Assert.IsTrue (stream.CanRead, "#4");
547 Assert.IsTrue (stream.CanWrite, "#5");
548 Assert.IsTrue (stream.CanSeek, "#6");
549 stream.Close ();
550 } finally {
551 DeleteFile (path);
555 [Test]
556 public void CreateText ()
558 string path = TempFolder + DSC + "FIT.CreateText.Test";
559 DeleteFile (path);
561 try {
562 FileInfo info = new FileInfo (path);
563 Assert.IsFalse (info.Exists, "#1");
564 StreamWriter writer = info.CreateText ();
565 writer.WriteLine ("test");
566 writer.Close ();
567 info = new FileInfo (path);
568 Assert.IsTrue (info.Exists, "#2");
569 } finally {
570 DeleteFile (path);
574 [Test]
575 public void CreateText_Directory ()
577 FileInfo info = new FileInfo (TempFolder);
578 try {
579 info.CreateText ();
580 Assert.Fail ("#1");
581 } catch (UnauthorizedAccessException ex) {
582 Assert.AreEqual (typeof (UnauthorizedAccessException), ex.GetType (), "#2");
583 Assert.IsNull (ex.InnerException, "#3");
584 Assert.IsNotNull (ex.Message, "#4");
588 [Test]
589 public void Delete ()
591 string path = TempFolder + DSC + "FIT.Delete.Test";
592 DeleteFile (path);
594 try {
595 FileInfo info = new FileInfo (path);
596 Assert.IsFalse (info.Exists, "#1");
597 info.Delete ();
598 Assert.IsFalse (info.Exists, "#1a");
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);
611 [Test]
612 public void Delete_Directory ()
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.IsNotNull (ex.Message, "#4");
624 [Test]
625 [Category("AndroidSdksNotWorking")]
626 public void MoveTo ()
628 string path1 = TempFolder + DSC + "FIT.MoveTo.Source.Test";
629 string path2 = TempFolder + DSC + "FIT.MoveTo.Dest.Test";
630 DeleteFile (path1);
631 DeleteFile (path2);
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");
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);
654 [Test] //Covers #18361
655 [Category("AndroidSdksNotWorking")]
656 public void MoveTo_SameName ()
658 string name = "FIT.MoveTo.SameName.Test";
659 string path1 = TempFolder + DSC + name;
660 string path2 = TempFolder + DSC + "same";
661 Directory.CreateDirectory (path2);
662 path2 += DSC + name;
663 DeleteFile (path1);
664 DeleteFile (path2);
666 try {
667 File.Create (path1).Close ();
668 FileInfo info1 = new FileInfo (path1);
669 FileInfo info2 = new FileInfo (path2);
670 Assert.IsTrue (info1.Exists, "#A1");
671 Assert.IsFalse (info2.Exists, "#A2");
673 info1.MoveTo (path2);
674 info1 = new FileInfo (path1);
675 info2 = new FileInfo (path2);
676 Assert.IsFalse (info1.Exists, "#B1");
677 Assert.IsTrue (info2.Exists, "#B2");
678 } finally {
679 DeleteFile (path1);
680 DeleteFile (path2);
684 [Test]
685 [Category ("NotWasm")]
686 public void MoveTo_DestFileName_AlreadyExists ()
688 string sourceFile = TempFolder + DSC + "FIT.MoveTo.Source.Test";
689 string destFile;
690 FileInfo info;
692 // move to same directory
693 File.Create (sourceFile).Close ();
694 info = new FileInfo (sourceFile);
695 try {
696 info.MoveTo (TempFolder);
697 Assert.Fail ("#A1");
698 } catch (IOException ex) {
699 // Cannot create a file when that file already exists
700 Assert.AreEqual (typeof (IOException), ex.GetType (), "#A2");
701 Assert.IsNull (ex.InnerException, "#A3");
702 Assert.IsNotNull (ex.Message, "#A4");
703 Assert.IsFalse (ex.Message.IndexOf (sourceFile) != -1, "#A5");
704 } finally {
705 DeleteFile (sourceFile);
708 // move to exist file
709 File.Create (sourceFile).Close ();
710 destFile = TempFolder + DSC + "FIT.MoveTo.Dest.Test";
711 File.Create (destFile).Close ();
712 info = new FileInfo (sourceFile);
713 try {
714 info.MoveTo (destFile);
715 Assert.Fail ("#B1");
716 } catch (IOException ex) {
717 // Cannot create a file when that file already exists
718 Assert.AreEqual (typeof (IOException), ex.GetType (), "#B2");
719 Assert.IsNull (ex.InnerException, "#B3");
720 Assert.IsNotNull (ex.Message, "#B4");
721 Assert.IsFalse (ex.Message.IndexOf (sourceFile) != -1, "#B5");
722 } finally {
723 DeleteFile (sourceFile);
724 DeleteFile (destFile);
727 // move to existing directory
728 File.Create (sourceFile).Close ();
729 destFile = TempFolder + Path.DirectorySeparatorChar + "bar";
730 Directory.CreateDirectory (destFile);
731 info = new FileInfo (sourceFile);
732 try {
733 info.MoveTo (destFile);
734 Assert.Fail ("#C1");
735 } catch (IOException ex) {
736 // Cannot create a file when that file already exists
737 Assert.AreEqual (typeof (IOException), ex.GetType (), "#C2");
738 Assert.IsNull (ex.InnerException, "#C3");
739 Assert.IsNotNull (ex.Message, "#C4");
740 Assert.IsFalse (ex.Message.IndexOf (sourceFile) != -1, "#C5");
741 } finally {
742 DeleteFile (sourceFile);
743 DeleteDirectory (destFile);
747 [Test]
748 [Category ("NotWasm")]
749 public void MoveTo_DestFileName_DirectoryDoesNotExist ()
751 string sourceFile = TempFolder + Path.DirectorySeparatorChar + "foo";
752 string destFile = Path.Combine (Path.Combine (TempFolder, "doesnotexist"), "b");
753 DeleteFile (sourceFile);
754 try {
755 File.Create (sourceFile).Close ();
756 FileInfo info = new FileInfo (sourceFile);
757 try {
758 info.MoveTo (destFile);
759 Assert.Fail ("#1");
760 } catch (DirectoryNotFoundException ex) {
761 // Could not find a part of the path
762 Assert.AreEqual (typeof (DirectoryNotFoundException), ex.GetType (), "#2");
763 Assert.IsNull (ex.InnerException, "#3");
764 Assert.IsNotNull (ex.Message, "#4");
766 } finally {
767 DeleteFile (sourceFile);
771 [Test]
772 public void MoveTo_DestFileName_Null ()
774 string path = TempFolder + DSC + "FIT.MoveToArgumentNullException.Test";
775 DeleteFile (path);
777 try {
778 File.Create (path).Close ();
779 FileInfo info = new FileInfo (path);
780 try {
781 info.MoveTo (null);
782 Assert.Fail ("#1");
783 } catch (ArgumentNullException ex) {
784 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
785 Assert.IsNull (ex.InnerException, "#3");
786 Assert.IsNotNull (ex.Message, "#4");
787 Assert.AreEqual ("destFileName", ex.ParamName, "#5");
789 } finally {
790 DeleteFile (path);
794 [Test]
795 public void MoveTo_DestFileName_Empty ()
797 string path = TempFolder + DSC + "FIT.MoveToArgumentException.Test";
798 DeleteFile (path);
800 try {
801 File.Create (path).Close ();
802 FileInfo info = new FileInfo (path);
803 try {
804 info.MoveTo (string.Empty);
805 Assert.Fail ("#1");
806 } catch (ArgumentException ex) {
807 // Empty file name is not legal
808 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
809 Assert.IsNull (ex.InnerException, "#3");
810 Assert.IsNotNull (ex.Message, "#4");
811 Assert.AreEqual ("destFileName", ex.ParamName, "#5");
813 } finally {
814 DeleteFile (path);
818 [Test]
819 public void MoveTo_DestFileName_Whitespace ()
821 string path = TempFolder + DSC + "FIT.MoveToArgumentException.Test";
822 DeleteFile (path);
824 try {
825 File.Create (path).Close ();
826 FileInfo info = new FileInfo (path);
827 try {
828 info.MoveTo (" ");
829 Assert.Fail ("#1");
830 } catch (ArgumentException ex) {
831 // The path is not of a legal form
832 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
833 Assert.IsNull (ex.InnerException, "#3");
834 Assert.IsNotNull (ex.Message, "#4");
835 Assert.IsNull (ex.ParamName, "#5");
837 } finally {
838 DeleteFile (path);
842 [Test]
843 public void MoveTo_FileDoesNotExist ()
845 string path1 = TempFolder + DSC + "FIT.MoveToFileNotFoundException.Src";
846 string path2 = TempFolder + DSC + "FIT.MoveToFileNotFoundException.Dst";
847 DeleteFile (path1);
848 DeleteFile (path2);
850 try {
851 FileInfo info = new FileInfo (path1);
852 try {
853 info.MoveTo (path2);
854 Assert.Fail ("#1");
855 } catch (FileNotFoundException ex) {
856 // Unable to find the specified file
857 Assert.AreEqual (typeof (FileNotFoundException), ex.GetType (), "#2");
858 Assert.IsNull (ex.InnerException, "#3");
859 Assert.IsNotNull (ex.Message, "#4");
861 } finally {
862 DeleteFile (path1);
863 DeleteFile (path2);
867 [Test]
868 public void MoveTo_Same ()
870 string path = TempFolder + DSC + "FIT.MoveToSame.Test";
871 DeleteFile (path);
873 try {
874 File.Create (path).Close ();
875 FileInfo info = new FileInfo (path);
876 info.MoveTo (path);
877 Assert.IsTrue (info.Exists, "#1");
878 Assert.IsTrue (File.Exists (path), "#2");
879 } finally {
880 DeleteFile (path);
884 [Test] //Covers #38796
885 [Category("AndroidSdksNotWorking")]
886 public void ToStringAfterMoveTo ()
888 string name1 = "FIT.ToStringAfterMoveTo.Test";
889 string name2 = "FIT.ToStringAfterMoveTo.Test.Alt";
890 string path1 = TempFolder + DSC + name1;
891 string path2 = TempFolder + DSC + name2;
892 DeleteFile (path1);
893 DeleteFile (path2);
895 try {
896 File.Create (path1).Close ();
897 FileInfo info = new FileInfo (path1);
898 Assert.AreEqual (path1, info.ToString (), "#A");
900 info.MoveTo (path2);
901 Assert.AreEqual (path2, info.ToString (), "#B");
902 } finally {
903 DeleteFile (path1);
904 DeleteFile (path2);
908 #if !MOBILE
909 [Test]
910 public void Replace1 ()
912 string path1 = TempFolder + DSC + "FIT.Replace.Source.Test";
913 string path2 = TempFolder + DSC + "FIT.Replace.Dest.Test";
914 string path3 = TempFolder + DSC + "FIT.Replace.Back.Test";
916 DeleteFile (path1);
917 DeleteFile (path2);
918 DeleteFile (path3);
919 try {
920 File.Create (path1).Close ();
921 File.Create (path2).Close ();
922 File.Create (path3).Close ();
923 FileInfo info = new FileInfo (path1);
924 Assert.IsTrue (info.Exists, "#1");
925 FileInfo info2 = info.Replace (path2, path3);
926 Assert.IsTrue (info2.Exists, "#2");
927 FileInfo info3 = new FileInfo (path3);
928 Assert.IsTrue (info3.Exists, "#3");
929 } finally {
930 DeleteFile (path2);
931 DeleteFile (path3);
935 [Test]
936 public void Replace1_Backup_Null ()
938 string path1 = TempFolder + DSC + "FIT.Replace.Source.Test";
939 string path2 = TempFolder + DSC + "FIT.Replace.Dest.Test";
941 DeleteFile (path1);
942 DeleteFile (path2);
943 try {
944 File.Create (path1).Close ();
945 File.Create (path2).Close ();
946 FileInfo info = new FileInfo (path1);
947 Assert.IsTrue (info.Exists, "#1");
948 FileInfo info2 = info.Replace (path2, null);
949 Assert.IsTrue (info2.Exists, "#2");
950 info = new FileInfo (path1);
951 Assert.IsFalse (info.Exists, "#3");
952 } finally {
953 DeleteFile (path2);
957 [Test]
958 public void Replace1_DestFileName_Null ()
960 string path1 = TempFolder + DSC + "FIT.Replace.Source.Test";
961 DeleteFile (path1);
962 try {
963 try {
964 File.Create (path1).Close ();
965 FileInfo info = new FileInfo (path1);
966 info.Replace (null, null);
967 Assert.Fail ("#1");
968 } catch (ArgumentNullException ex) {
969 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
970 Assert.IsNull (ex.InnerException, "#3");
971 Assert.IsNotNull (ex.Message, "#4");
973 } finally {
974 DeleteFile (path1);
978 [Test]
979 public void Replace1_DestFileName_Empty ()
981 string path1 = TempFolder + DSC + "FIT.Replace.Source.Test";
982 DeleteFile (path1);
983 try {
984 try {
985 File.Create (path1).Close ();
986 FileInfo info = new FileInfo (path1);
987 info.Replace (string.Empty, null);
988 Assert.Fail ("#1");
989 } catch (ArgumentException ex) {
990 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
991 Assert.IsNull (ex.InnerException, "#3");
992 Assert.IsNotNull (ex.Message, "#4");
994 } finally {
995 DeleteFile (path1);
999 [Test]
1000 public void Replace1_DestFileName_WhiteSpace ()
1002 string path1 = TempFolder + DSC + "FIT.Replace.Source.Test";
1003 DeleteFile (path1);
1004 try {
1005 try {
1006 File.Create (path1).Close ();
1007 FileInfo info = new FileInfo (path1);
1008 info.Replace (" ", null);
1009 Assert.Fail ("#1");
1010 } catch (ArgumentException ex) {
1011 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
1012 Assert.IsNull (ex.InnerException, "#3");
1013 Assert.IsNotNull (ex.Message, "#4");
1015 } finally {
1016 DeleteFile (path1);
1020 [Test]
1021 public void Replace1_DestFileName_InvalidPathChars ()
1023 string path1 = TempFolder + DSC + "FIT.Replace.Source.Test";
1024 string path2 = string.Empty;
1025 DeleteFile (path1);
1027 try {
1028 File.Create (path1).Close ();
1029 FileInfo info = new FileInfo (path1);
1030 foreach (char c in Path.InvalidPathChars)
1031 path2 += c;
1032 try {
1033 info.Replace (path2, null);
1034 Assert.Fail ("#1");
1035 } catch (ArgumentException ex) {
1036 // Illegal characters in path
1037 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
1038 Assert.IsNull (ex.InnerException, "#3");
1039 Assert.IsNotNull (ex.Message, "#4");
1040 Assert.IsNull (ex.ParamName, "#5");
1042 } finally {
1043 DeleteFile (path1);
1047 [Test]
1048 public void Replace1_DestFileName_FileNotFound ()
1050 string path1 = TempFolder + DSC + "FIT.Replace.Source.Test";
1051 string path2 = TempFolder + DSC + "FIT.Replace.Dest.Test";
1052 DeleteFile (path1);
1053 DeleteFile (path2);
1055 try {
1056 try {
1057 File.Create (path1).Close ();
1058 FileInfo info = new FileInfo (path1);
1059 info.Replace (path2, null);
1060 Assert.Fail ("#1");
1061 } catch (FileNotFoundException ex) {
1062 Assert.AreEqual (typeof (FileNotFoundException), ex.GetType (), "#2");
1063 Assert.IsNull (ex.InnerException, "#3");
1064 Assert.IsNotNull (ex.Message, "#4");
1066 } finally {
1067 DeleteFile (path1);
1071 [Test]
1072 public void Replace1_Source_FileNotFound ()
1074 string path1 = TempFolder + DSC + "FIT.Replace.Source.Test";
1075 string path2 = TempFolder + DSC + "FIT.Replace.Dest.Test";
1076 DeleteFile (path2);
1078 try {
1079 try {
1080 File.Create (path2).Close ();
1081 FileInfo info = new FileInfo (path1);
1082 info.Replace (path2, null);
1083 Assert.Fail ("#1");
1084 } catch (FileNotFoundException ex) {
1085 Assert.AreEqual (typeof (FileNotFoundException), ex.GetType (), "#2");
1086 Assert.IsNull (ex.InnerException, "#3");
1087 Assert.IsNotNull (ex.Message, "#4");
1089 } finally {
1090 DeleteFile (path2);
1093 #endif
1095 [Test]
1096 public void Open ()
1098 string path = TempFolder + DSC + "FIT.Open.Test";
1099 DeleteFile (path);
1100 FileStream stream = null;
1101 try {
1102 FileInfo info = new FileInfo (path);
1103 stream = info.Open (FileMode.CreateNew);
1104 Assert.IsTrue (stream.CanRead, "#A1");
1105 Assert.IsTrue (stream.CanSeek, "#A2");
1106 Assert.IsTrue (stream.CanWrite, "#A3");
1107 stream.Close ();
1109 stream = info.Open (FileMode.Open);
1110 Assert.IsTrue (stream.CanRead, "#B1");
1111 Assert.IsTrue (stream.CanSeek, "#B2");
1112 Assert.IsTrue (stream.CanWrite, "#B3");
1113 stream.Close ();
1115 stream = info.Open (FileMode.Append, FileAccess.Write);
1116 Assert.IsFalse (stream.CanRead, "#C1");
1117 Assert.IsTrue (stream.CanSeek, "#C2");
1118 Assert.IsTrue (stream.CanWrite, "#C3");
1119 stream.Close ();
1121 stream = info.Open (FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite);
1122 Assert.IsTrue (stream.CanRead, "#D1");
1123 Assert.IsTrue (stream.CanSeek, "#D2");
1124 Assert.IsTrue (stream.CanWrite, "#D3");
1125 stream.Close ();
1126 } finally {
1127 if (stream != null)
1128 stream.Close ();
1129 DeleteFile (path);
1133 [Test]
1134 public void Open_FileDoesNotExist ()
1136 string path = TempFolder + DSC + "FIT.OpenFileNotFoundException.Test";
1137 DeleteFile (path);
1139 FileInfo info = new FileInfo (path);
1140 try {
1141 info.Open (FileMode.Open);
1142 Assert.Fail ("#1");
1143 } catch (FileNotFoundException ex) {
1144 Assert.AreEqual (typeof (FileNotFoundException), ex.GetType (), "#2");
1145 Assert.AreEqual (path, ex.FileName, "#3");
1146 Assert.IsNull (ex.InnerException, "#4");
1147 Assert.IsNotNull (ex.Message, "#5");
1151 [Test]
1152 public void OpenRead ()
1154 string path = TempFolder + DSC + "FIT.OpenRead.Test";
1155 DeleteFile (path);
1156 FileStream stream = null;
1157 try {
1158 File.Create (path).Close ();
1159 FileInfo info = new FileInfo (path);
1160 stream = info.OpenRead ();
1161 Assert.IsTrue (stream.CanRead, "#1");
1162 Assert.IsTrue (stream.CanSeek, "#2");
1163 Assert.IsFalse (stream.CanWrite, "#3");
1164 stream.Close ();
1166 } finally {
1167 if (stream != null)
1168 stream.Close ();
1169 DeleteFile (path);
1173 [Test]
1174 public void OpenRead_FileLock ()
1176 string path = TempFolder + DSC + "FIT.OpenReadIOException.Test";
1177 DeleteFile (path);
1178 FileStream stream = null;
1180 try {
1181 stream = File.Create (path);
1182 FileInfo info = new FileInfo (path);
1183 try {
1184 info.OpenRead ();
1185 Assert.Fail ("#1");
1186 } catch (IOException ex) {
1187 // The process cannot access the file because
1188 // it is being used by another process
1189 Assert.AreEqual (typeof (IOException), ex.GetType (), "#2");
1190 Assert.IsNull (ex.InnerException, "#3");
1191 Assert.IsNotNull (ex.Message, "#4");
1193 } finally {
1194 if (stream != null)
1195 stream.Close ();
1196 DeleteFile (path);
1200 [Test]
1201 public void OpenRead_Directory ()
1203 FileInfo info = new FileInfo (TempFolder);
1204 try {
1205 info.OpenRead ();
1206 Assert.Fail ("#1");
1207 } catch (UnauthorizedAccessException ex) {
1208 Assert.AreEqual (typeof (UnauthorizedAccessException), ex.GetType (), "#2");
1209 Assert.IsNull (ex.InnerException, "#3");
1210 Assert.IsNotNull (ex.Message, "#4");
1214 [Test]
1215 public void OpenText ()
1217 string path = TempFolder + DSC + "FIT.OpenText.Test";
1218 DeleteFile (path);
1219 StreamReader reader = null;
1220 try {
1221 File.Create (path).Close ();
1222 FileInfo info = new FileInfo (path);
1223 reader = info.OpenText ();
1224 Assert.AreEqual (-1, reader.Peek ());
1225 } finally {
1226 if (reader != null)
1227 reader.Close ();
1228 DeleteFile (path);
1232 [Test]
1233 public void OpenText_FileDoesNotExist ()
1235 string path = TempFolder + DSC + "FIT.OpenTextFileNotFoundException.Test";
1236 DeleteFile (path);
1237 FileInfo info = new FileInfo (path);
1238 try {
1239 info.OpenText ();
1240 Assert.Fail ("#1");
1241 } catch (FileNotFoundException ex) {
1242 Assert.AreEqual (typeof (FileNotFoundException), ex.GetType (), "#2");
1243 Assert.AreEqual (path, ex.FileName, "#3");
1244 Assert.IsNull (ex.InnerException, "#4");
1245 Assert.IsNotNull (ex.Message, "#5");
1249 [Test]
1250 public void OpenText_Directory ()
1252 FileInfo info = new FileInfo (TempFolder);
1253 try {
1254 info.OpenText ();
1255 Assert.Fail ("#1");
1256 } catch (UnauthorizedAccessException ex) {
1257 Assert.AreEqual (typeof (UnauthorizedAccessException), ex.GetType (), "#2");
1258 Assert.IsNull (ex.InnerException, "#3");
1259 Assert.IsNotNull (ex.Message, "#4");
1263 [Test]
1264 public void OpenWrite ()
1266 string path = TempFolder + DSC + "FIT.OpenWrite.Test";
1267 DeleteFile (path);
1268 FileStream stream = null;
1269 try {
1270 File.Create (path).Close ();
1271 FileInfo info = new FileInfo (path);
1272 stream = info.OpenWrite ();
1273 Assert.IsFalse (stream.CanRead, "#1");
1274 Assert.IsTrue (stream.CanSeek, "#2");
1275 Assert.IsTrue (stream.CanWrite, "#3");
1276 } finally {
1277 if (stream != null)
1278 stream.Close ();
1279 DeleteFile (path);
1283 [Test]
1284 public void OpenWrite_Directory ()
1286 FileInfo info = new FileInfo (TempFolder);
1287 try {
1288 info.OpenWrite ();
1289 Assert.Fail ("#1");
1290 } catch (UnauthorizedAccessException ex) {
1291 Assert.AreEqual (typeof (UnauthorizedAccessException), ex.GetType (), "#2");
1292 Assert.IsNull (ex.InnerException, "#3");
1293 Assert.IsNotNull (ex.Message, "#4");
1296 #if !MOBILE
1297 [Test]
1298 public void Serialization ()
1300 FileInfo info;
1301 SerializationInfo si;
1303 info = new FileInfo ("Test");
1304 si = new SerializationInfo (typeof (FileInfo), new FormatterConverter ());
1305 info.GetObjectData (si, new StreamingContext ());
1307 Assert.AreEqual (3, si.MemberCount, "#A1");
1308 Assert.AreEqual ("Test", si.GetString ("OriginalPath"), "#A2");
1309 Assert.AreEqual (Path.Combine (Directory.GetCurrentDirectory (), "Test"), si.GetString ("FullPath"), "#A3");
1311 info = new FileInfo (TempFolder);
1312 si = new SerializationInfo (typeof (FileInfo), new FormatterConverter ());
1313 info.GetObjectData (si, new StreamingContext ());
1315 Assert.AreEqual (3, si.MemberCount, "#B1");
1316 Assert.AreEqual (TempFolder, si.GetString ("OriginalPath"), "#B2");
1317 Assert.AreEqual (TempFolder, si.GetString ("FullPath"), "#B3");
1320 [Test]
1321 public void Deserialization ()
1323 FileInfo info = new FileInfo ("Test");
1325 MemoryStream ms = new MemoryStream ();
1326 BinaryFormatter bf = new BinaryFormatter ();
1327 bf.Serialize (ms, info);
1328 ms.Position = 0;
1330 FileInfo clone = (FileInfo) bf.Deserialize (ms);
1331 Assert.AreEqual (info.Name, clone.Name, "#1");
1332 Assert.AreEqual (info.FullName, clone.FullName, "#2");
1334 #endif
1336 [Test]
1337 [Category ("MobileNotWorking")]
1338 public void ToStringVariety ()
1340 Assert.AreEqual ("foo", new FileInfo ("foo").ToString ());
1341 Assert.AreEqual ("c:/foo", new FileInfo ("c:/foo").ToString ());
1342 Assert.AreEqual ("/usr/local/foo", new FileInfo ("/usr/local/foo").ToString ());
1343 Assert.AreEqual ("c:\\foo", new FileInfo ("c:\\foo").ToString ());
1344 Assert.AreEqual ("/usr/local\\foo", new FileInfo ("/usr/local\\foo").ToString ());
1345 Assert.AreEqual ("foo/BAR/baz", new FileInfo ("foo/BAR/baz").ToString ());
1346 Assert.AreEqual ("c:/documents and settings", new FileInfo ("c:/documents and settings").ToString ());
1347 Assert.AreEqual ("c:/docUme~1", new FileInfo ("c:/docUme~1").ToString ());
1350 void DeleteFile (string path)
1352 if (File.Exists (path))
1353 File.Delete (path);
1356 void DeleteDirectory (string path)
1358 if (Directory.Exists (path))
1359 Directory.Delete (path, true);