Merge pull request #4872 from kumpera/add_test_for_eloop
[mono.git] / mcs / class / corlib / Test / System.IO / FileTest.cs
1 //
2 // FileTest.cs: Test cases for System.IO.File
3 //
4 // Author: 
5 //     Duncan Mak (duncan@ximian.com)
6 //     Ville Palo (vi64pa@kolumbus.fi)
7 //
8 // (C) 2002 Ximian, Inc. http://www.ximian.com
9 //
10 // TODO: Find out why ArgumentOutOfRangeExceptions does not manage to close streams properly
11 //
12
13 using System;
14 using System.IO;
15 using System.Globalization;
16 using System.Threading;
17 using System.Runtime.InteropServices;
18
19
20 using NUnit.Framework;
21
22 namespace MonoTests.System.IO
23 {
24         [TestFixture]
25         public class FileTest
26         {
27                 CultureInfo old_culture;
28                 static string TempFolder = Path.Combine (Path.GetTempPath (), "MonoTests.System.IO.Tests");
29
30                 [SetUp]
31                 public void SetUp ()
32                 {
33                         DeleteDirectory (TempFolder);
34                         Directory.CreateDirectory (TempFolder);
35                         old_culture = Thread.CurrentThread.CurrentCulture;
36                         Thread.CurrentThread.CurrentCulture = new CultureInfo ("en-US", false);
37                 }
38
39                 [TearDown]
40                 public void TearDown ()
41                 {
42                         DeleteDirectory (TempFolder);
43                         Thread.CurrentThread.CurrentCulture = old_culture;
44                 }
45
46                 string path;
47                 string testfile;
48
49                 [TestFixtureSetUp]
50                 public void FixtureSetUp ()
51                 {
52                         path = Environment.GetFolderPath (Environment.SpecialFolder.Personal);
53                         testfile = Path.Combine (path, "FileStreamTest.dat");
54                         File.WriteAllText (testfile, "1");
55                 }
56
57                 [TestFixtureTearDown]
58                 public void FixtureTearDown ()
59                 {
60                         if (File.Exists (testfile))
61                                 File.Delete (testfile);                 
62                 }
63
64                 [Test]
65                 public void TestExists ()
66                 {
67                         FileStream s = null;
68                         string path = TempFolder + Path.DirectorySeparatorChar + "AFile.txt";
69                         try {
70                                 Assert.IsFalse (File.Exists (null), "#1");
71                                 Assert.IsFalse (File.Exists (string.Empty), "#2");
72                                 Assert.IsFalse (File.Exists ("  \t\t  \t \n\t\n \n"), "#3");
73                                 DeleteFile (path);
74                                 s = File.Create (path);
75                                 s.Close ();
76                                 Assert.IsTrue (File.Exists (path), "#4");
77                                 Assert.IsFalse (File.Exists (TempFolder + Path.DirectorySeparatorChar + "doesnotexist"), "#5");
78                         } finally {
79                                 if (s != null)
80                                         s.Close ();
81                                 DeleteFile (path);
82                         }
83                 }
84
85                 [Test]
86                 public void Exists_InvalidFileName () 
87                 {
88                         Assert.IsFalse (File.Exists ("><|"), "#1");
89                         Assert.IsFalse (File.Exists ("?*"), "#2");
90                 }
91
92                 [Test]
93                 public void Exists_InvalidDirectory () 
94                 {
95                         Assert.IsFalse (File.Exists (Path.Combine ("does not exist", "file.txt")));
96                 }
97
98                 [Test]
99                 public void Create_Path_Null ()
100                 {
101                         try {
102                                 File.Create (null);
103                                 Assert.Fail ("#1");
104                         } catch (ArgumentNullException ex) {
105                                 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
106                                 Assert.IsNull (ex.InnerException, "#3");
107                                 Assert.IsNotNull (ex.Message, "#4");
108                                 Assert.AreEqual ("path", ex.ParamName, "#5");
109                         }
110                 }
111
112                 [Test]
113                 public void Create_Path_Directory ()
114                 {
115                         string path = Path.Combine (TempFolder, "foo");
116                         Directory.CreateDirectory (path);
117                         try {
118                                 File.Create (path);
119                                 Assert.Fail ("#1");
120                         } catch (UnauthorizedAccessException ex) {
121                                 // Access to the path '...' is denied
122                                 Assert.AreEqual (typeof (UnauthorizedAccessException), ex.GetType (), "#2");
123                                 Assert.IsNull (ex.InnerException, "#3");
124                                 Assert.IsNotNull (ex.Message, "#4");
125                                 Assert.IsTrue (ex.Message.IndexOf (path) != -1, "#5");
126                         } finally {
127                                 DeleteDirectory (path);
128                         }
129                 }
130
131                 [Test]
132                 public void Create_Path_Empty ()
133                 {
134                         try {
135                                 File.Create (string.Empty);
136                                 Assert.Fail ("#1");
137                         } catch (ArgumentException ex) {
138                                 // Empty file name is not legal
139                                 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
140                                 Assert.IsNull (ex.InnerException, "#3");
141                                 Assert.IsNotNull (ex.Message, "#4");
142                                 Assert.IsNull (ex.ParamName, "#5");
143                         }
144                 }
145
146                 [Test]
147                 public void Create_Path_ReadOnly ()
148                 {
149                         string path = Path.Combine (TempFolder, "foo");
150                         File.Create (path).Close ();
151                         File.SetAttributes (path, FileAttributes.ReadOnly);
152                         try {
153                                 File.Create (path);
154                                 Assert.Fail ("#1");
155                         } catch (UnauthorizedAccessException ex) {
156                                 // Access to the path '...' is denied
157                                 Assert.AreEqual (typeof (UnauthorizedAccessException), ex.GetType (), "#2");
158                                 Assert.IsNull (ex.InnerException, "#3");
159                                 Assert.IsNotNull (ex.Message, "#4");
160                                 Assert.IsTrue (ex.Message.IndexOf (path) != -1, "#5");
161                         } finally {
162                                 File.SetAttributes (path, FileAttributes.Normal);
163                         }
164                 }
165
166                 [Test]
167                 public void Create_Path_Whitespace ()
168                 {
169                         try {
170                                 File.Create (" ");
171                                 Assert.Fail ("#1");
172                         } catch (ArgumentException ex) {
173                                 // The path is not of a legal form
174                                 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
175                                 Assert.IsNull (ex.InnerException, "#3");
176                                 Assert.IsNotNull (ex.Message, "#4");
177                                 Assert.IsNull (ex.ParamName, "#5");
178                         }
179                 }
180
181                 [Test]
182                 public void Create_Directory_DoesNotExist ()
183                 {
184                         FileStream stream = null;
185                         string path = TempFolder + Path.DirectorySeparatorChar + "directory_does_not_exist" + Path.DirectorySeparatorChar + "foo";
186                         
187                         try {
188                                 stream = File.Create (path);
189                                 Assert.Fail ("#1");
190                         } catch (DirectoryNotFoundException ex) {
191                                 // Could not find a part of the path "..."
192                                 Assert.AreEqual (typeof (DirectoryNotFoundException), ex.GetType (), "#2");
193                                 Assert.IsNull (ex.InnerException, "#3");
194                                 Assert.IsNotNull (ex.Message, "#4");
195                                 Assert.IsTrue (ex.Message.IndexOf (path) != -1, "#5");
196                         } finally {
197                                 if (stream != null)
198                                         stream.Close ();
199                                 DeleteFile (path);
200                         }
201                 }
202
203                 [Test]
204                 public void Create ()
205                 {
206                         FileStream stream = null;
207                         string path = null;
208
209                         /* positive test: create resources/foo */
210                         path = TempFolder + Path.DirectorySeparatorChar + "foo";
211                         try {
212
213                                 stream = File.Create (path);
214                                 Assert.IsTrue (File.Exists (path), "#1");
215                                 stream.Close ();
216                         } finally {
217                                 if (stream != null)
218                                         stream.Close ();
219                                 DeleteFile (path);
220                         }
221
222                         stream = null;
223
224                         /* positive test: repeat test above again to test for overwriting file */
225                         path = TempFolder + Path.DirectorySeparatorChar + "foo";
226                         try {
227                                 stream = File.Create (path);
228                                 Assert.IsTrue (File.Exists (path), "#2");
229                                 stream.Close ();
230                         } finally {
231                                 if (stream != null)
232                                         stream.Close ();
233                                 DeleteFile (path);
234                         }
235                 }
236
237                 [Test]
238                 public void Copy_SourceFileName_Null ()
239                 {
240                         try {
241                                 File.Copy (null, "b");
242                                 Assert.Fail ("#1");
243                         } catch (ArgumentNullException ex) {
244                                 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
245                                 Assert.IsNull (ex.InnerException, "#3");
246                                 Assert.IsNotNull (ex.Message, "#4");
247                                 Assert.AreEqual ("sourceFileName", ex.ParamName, "#5");
248                         }
249                 }
250
251                 [Test]
252                 public void Copy_DestFileName_Null ()
253                 {
254                         try {
255                                 File.Copy ("a", null);
256                                 Assert.Fail ("#1");
257                         } catch (ArgumentNullException ex) {
258                                 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
259                                 Assert.IsNull (ex.InnerException, "#3");
260                                 Assert.IsNotNull (ex.Message, "#4");
261                                 Assert.AreEqual ("destFileName", ex.ParamName, "#5");
262                         }
263                 }
264
265                 [Test]
266                 public void Copy_SourceFileName_Empty ()
267                 {
268                         try {
269                                 File.Copy (string.Empty, "b");
270                                 Assert.Fail ("#1");
271                         } catch (ArgumentException ex) {
272                                 // Empty file name is not legal
273                                 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
274                                 Assert.IsNull (ex.InnerException, "#3");
275                                 Assert.IsNotNull (ex.Message, "#4");
276                                 Assert.AreEqual ("sourceFileName", ex.ParamName, "#5");
277                         }
278                 }
279
280                 [Test]
281                 public void Copy_DestFileName_Empty ()
282                 {
283                         try {
284                                 File.Copy ("a", string.Empty);
285                                 Assert.Fail ("#1");
286                         } catch (ArgumentException ex) {
287                                 // Empty file name is not legal
288                                 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
289                                 Assert.IsNull (ex.InnerException, "#3");
290                                 Assert.IsNotNull (ex.Message, "#4");
291                                 Assert.AreEqual ("destFileName", ex.ParamName, "#5");
292                         }
293                 }
294
295                 [Test]
296                 public void Copy_SourceFileName_Whitespace ()
297                 {
298                         try {
299                                 File.Copy (" ", "b");
300                                 Assert.Fail ("#1");
301                         } catch (ArgumentException ex) {
302                                 // The path is not of a legal form
303                                 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
304                                 Assert.IsNull (ex.InnerException, "#3");
305                                 Assert.IsNotNull (ex.Message, "#4");
306                                 Assert.IsNull (ex.ParamName, "#5");
307                         }
308                 }
309
310                 [Test]
311                 public void Copy_DestFileName_Whitespace ()
312                 {
313                         try {
314                                 File.Copy ("a", " ");
315                                 Assert.Fail ("#1");
316                         } catch (ArgumentException ex) {
317                                 // The path is not of a legal form
318                                 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
319                                 Assert.IsNull (ex.InnerException, "#3");
320                                 Assert.IsNotNull (ex.Message, "#4");
321                                 Assert.IsNull (ex.ParamName, "#5");
322                         }
323                 }
324
325                 [Test]
326                 public void Copy_SourceFileName_DoesNotExist ()
327                 {
328                         try {
329                                 File.Copy ("doesnotexist", "b");
330                                 Assert.Fail ("#1");
331                         } catch (FileNotFoundException ex) {
332                                 Assert.AreEqual (typeof (FileNotFoundException), ex.GetType (), "#2");
333                                 Assert.AreEqual ("doesnotexist", ex.FileName, "#3");
334                                 Assert.IsNull (ex.InnerException, "#4");
335                                 Assert.IsNotNull (ex.Message, "#5");
336                         }
337                 }
338
339                 [Test]
340                 public void Copy_DestFileName_AlreadyExists ()
341                 {
342                         string source = TempFolder + Path.DirectorySeparatorChar + "AFile.txt";
343                         string dest = TempFolder + Path.DirectorySeparatorChar + "bar";
344                         DeleteFile (source);
345                         DeleteFile (dest);
346                         try {
347                                 File.Create (source).Close ();
348                                 File.Copy (source, dest);
349                                 try {
350                                         File.Copy (source, dest);
351                                         Assert.Fail ("#1");
352                                 } catch (IOException ex) {
353                                         // The file '...' already exists.
354                                         Assert.AreEqual (typeof (IOException), ex.GetType (), "#2");
355                                         Assert.IsNull (ex.InnerException, "#3");
356                                         Assert.IsNotNull (ex.Message, "#4");
357                                         Assert.IsTrue (ex.Message.IndexOf (dest) != -1, "#5");
358                                 }
359                         } finally {
360                                 DeleteFile (dest);
361                                 DeleteFile (source);
362                         }
363                 }
364
365                 [Test]
366                 public void Copy_SourceFileName_DestFileName_Same ()
367                 {
368                         string source = TempFolder + Path.DirectorySeparatorChar + "SameFile.txt";
369                         DeleteFile (source);
370                         try {
371                                 // new empty file
372                                 File.Create (source).Close ();
373                                 try {
374                                         File.Copy (source, source, true);
375                                         Assert.Fail ("#1");
376                                 } catch (IOException ex) {
377                                         // process cannot access file ... because it is being used by another process
378                                         Assert.IsNull (ex.InnerException, "#2");
379                                         Assert.IsTrue (ex.Message.IndexOf (source) != -1, "#3");
380                                 }
381                         } finally {
382                                 DeleteFile (source);
383                         }
384                 }
385
386                 [Test]
387                 public void Copy ()
388                 {
389                         string path1 = TempFolder + Path.DirectorySeparatorChar + "bar";
390                         string path2 = TempFolder + Path.DirectorySeparatorChar + "AFile.txt";
391                         /* positive test: copy resources/AFile.txt to resources/bar */
392                         try {
393                                 DeleteFile (path1);
394                                 DeleteFile (path2);
395
396                                 File.Create (path2).Close ();
397                                 File.Copy (path2, path1);
398                                 Assert.IsTrue (File.Exists (path2), "#A1");
399                                 Assert.IsTrue (File.Exists (path1), "#A2");
400
401                                 Assert.IsTrue (File.Exists (path1), "#B1");
402                                 File.Copy (path2, path1, true);
403                                 Assert.IsTrue (File.Exists (path2), "#B2");
404                                 Assert.IsTrue (File.Exists (path1), "#B3");
405                         } finally {
406                                 DeleteFile (path1);
407                                 DeleteFile (path2);
408                         }
409                 }
410
411                 [Test]
412                 public void Delete_Path_Null ()
413                 {
414                         try {
415                                 File.Delete (null);
416                                 Assert.Fail ("#1");
417                         } catch (ArgumentNullException ex) {
418                                 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
419                                 Assert.IsNull (ex.InnerException, "#3");
420                                 Assert.IsNotNull (ex.Message, "#4");
421                                 Assert.AreEqual ("path", ex.ParamName, "#5");
422                         }
423                 }
424
425                 [Test]
426                 public void Delete_Path_Empty ()
427                 {
428                         try {
429                                 File.Delete (string.Empty);
430                                 Assert.Fail ("#1");
431                         } catch (ArgumentException ex) {
432                                 // Empty file name is not legal
433                                 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
434                                 Assert.IsNull (ex.InnerException, "#3");
435                                 Assert.IsNotNull (ex.Message, "#4");
436                                 Assert.IsNull (ex.ParamName, "#5");
437                         }
438                 }
439
440                 [Test]
441                 public void Delete_Path_Whitespace ()
442                 {
443                         try {
444                                 File.Delete (" ");
445                                 Assert.Fail ("#1");
446                         } catch (ArgumentException ex) {
447                                 // The path is not of a legal form
448                                 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
449                                 Assert.IsNull (ex.InnerException, "#3");
450                                 Assert.IsNotNull (ex.Message, "#4");
451                                 Assert.IsNull (ex.ParamName, "#5");
452                         }
453                 }
454
455                 [Test]
456                 public void Delete_Directory_DoesNotExist ()
457                 {
458                         string path = TempFolder + Path.DirectorySeparatorChar + "directory_does_not_exist" + Path.DirectorySeparatorChar + "foo";
459                         if (Directory.Exists (path))
460                                 Directory.Delete (path, true);
461
462                         try {
463                                 File.Delete (path);
464                                 Assert.Fail ("#1");
465                         } catch (DirectoryNotFoundException ex) {
466                                 // Could not find a part of the path "..."
467                                 Assert.AreEqual (typeof (DirectoryNotFoundException), ex.GetType (), "#2");
468                                 Assert.IsNull (ex.InnerException, "#3");
469                                 Assert.IsNotNull (ex.Message, "#4");
470                                 Assert.IsTrue (ex.Message.IndexOf (path) != -1, "#5");
471                         }
472                 }
473
474                 [Test]
475                 public void Delete ()
476                 {
477                         string foopath = TempFolder + Path.DirectorySeparatorChar + "foo";
478                         DeleteFile (foopath);
479                         try {
480                                 File.Create (foopath).Close ();
481                                 File.Delete (foopath);
482                                 Assert.IsFalse (File.Exists (foopath));
483                                 File.Delete (foopath);
484                         } finally {
485                                 DeleteFile (foopath);
486                         }
487                 }
488
489                 [Test] // bug #323389
490                 [Category ("NotWorking")]
491                 public void Delete_FileLock ()
492                 {
493                         string path = TempFolder + Path.DirectorySeparatorChar + "DeleteOpenStreamException";
494                         DeleteFile (path);
495                         FileStream stream = null;
496                         try {
497                                 stream = new FileStream (path, FileMode.OpenOrCreate, FileAccess.ReadWrite);
498                                 try {
499                                         File.Delete (path);
500                                         Assert.Fail ("#1");
501                                 } catch (IOException ex) {
502                                         // The process cannot access the file '...'
503                                         // because it is being used by another process
504                                         Assert.AreEqual (typeof (IOException), ex.GetType (), "#2");
505                                         Assert.IsNull (ex.InnerException, "#3");
506                                         Assert.IsNotNull (ex.Message, "#4");
507                                         Assert.IsTrue (ex.Message.IndexOf (path) != -1, "#5");
508                                 }
509                         } finally {
510                                 if (stream != null)
511                                         stream.Close ();
512                                 DeleteFile (path);
513                         }
514                 }
515
516                 [Test]
517                 [ExpectedException (typeof(UnauthorizedAccessException))]
518                 public void Delete_File_ReadOnly ()
519                 {
520                         if (RunningOnUnix)
521                                 Assert.Ignore ("ReadOnly files can be deleted on unix since fdef50957f508627928c7876a905d5584da45748.");
522
523                         string path = TempFolder + Path.DirectorySeparatorChar + "DeleteReadOnly";
524                         DeleteFile (path);
525                         try {
526                                 File.Create (path).Close ();
527                                 File.SetAttributes (path, FileAttributes.ReadOnly);
528                                 File.Delete (path);
529                         } finally {
530                                 File.SetAttributes (path, FileAttributes.Normal);
531                                 DeleteFile (path);
532                         }
533                 }
534
535                 [Test]
536                 public void GetAttributes_Archive ()
537                 {
538                         if (RunningOnUnix)
539                                 Assert.Ignore ("bug #325181: FileAttributes.Archive has no effect on Unix.");
540
541                         FileAttributes attrs;
542
543                         string path = Path.Combine (TempFolder, "GetAttributes.tmp");
544                         File.Create (path).Close ();
545
546                         attrs = File.GetAttributes (path);
547                         Assert.IsTrue ((attrs & FileAttributes.Archive) != 0, "#1");
548
549                         attrs &= ~FileAttributes.Archive;
550                         File.SetAttributes (path, attrs);
551
552                         attrs = File.GetAttributes (path);
553                         Assert.IsFalse ((attrs & FileAttributes.Archive) != 0, "#2");
554                 }
555
556                 [Test]
557                 public void GetAttributes_Default_File ()
558                 {
559                         if (RunningOnUnix)
560                                 Assert.Ignore ("bug #325181: FileAttributes.Archive has no effect on Unix.");
561
562                         string path = Path.Combine (TempFolder, "GetAttributes.tmp");
563                         File.Create (path).Close ();
564
565                         FileAttributes attrs = File.GetAttributes (path);
566
567                         Assert.IsTrue ((attrs & FileAttributes.Archive) != 0, "#1");
568                         Assert.IsFalse ((attrs & FileAttributes.Directory) != 0, "#2");
569                         Assert.IsFalse ((attrs & FileAttributes.Hidden) != 0, "#3");
570                         Assert.IsFalse ((attrs & FileAttributes.Normal) != 0, "#4");
571                         Assert.IsFalse ((attrs & FileAttributes.ReadOnly) != 0, "#5");
572                         Assert.IsFalse ((attrs & FileAttributes.System) != 0, "#6");
573                 }
574
575                 [Test]
576                 public void GetAttributes_Default_Directory ()
577                 {
578                         FileAttributes attrs = File.GetAttributes (TempFolder);
579
580                         Assert.IsFalse ((attrs & FileAttributes.Archive) != 0, "#1");
581                         Assert.IsTrue ((attrs & FileAttributes.Directory) != 0, "#2");
582                         Assert.IsFalse ((attrs & FileAttributes.Hidden) != 0, "#3");
583                         Assert.IsFalse ((attrs & FileAttributes.Normal) != 0, "#4");
584                         Assert.IsFalse ((attrs & FileAttributes.ReadOnly) != 0, "#5");
585                         Assert.IsFalse ((attrs & FileAttributes.System) != 0, "#6");
586                 }
587
588                 [Test]
589                 public void GetAttributes_Directory ()
590                 {
591                         FileAttributes attrs = File.GetAttributes (TempFolder);
592
593                         Assert.IsTrue ((attrs & FileAttributes.Directory) != 0, "#1");
594
595                         attrs &= ~FileAttributes.Directory;
596                         File.SetAttributes (TempFolder, attrs);
597
598                         Assert.IsFalse ((attrs & FileAttributes.Directory) != 0, "#2");
599
600                         string path = Path.Combine (TempFolder, "GetAttributes.tmp");
601                         File.Create (path).Close ();
602
603                         attrs = File.GetAttributes (path);
604                         attrs |= FileAttributes.Directory;
605                         File.SetAttributes (path, attrs);
606
607                         Assert.IsTrue ((attrs & FileAttributes.Directory) != 0, "#3");
608                 }
609
610                 [Test]
611                 public void GetAttributes_ReadOnly ()
612                 {
613                         FileAttributes attrs;
614
615                         string path = Path.Combine (TempFolder, "GetAttributes.tmp");
616                         File.Create (path).Close ();
617
618                         attrs = File.GetAttributes (path);
619                         Assert.IsFalse ((attrs & FileAttributes.ReadOnly) != 0, "#1");
620
621                         try {
622                                 attrs |= FileAttributes.ReadOnly;
623                                 File.SetAttributes (path, attrs);
624
625                                 attrs = File.GetAttributes (path);
626                                 Assert.IsTrue ((attrs & FileAttributes.ReadOnly) != 0, "#2");
627                         } finally {
628                                 File.SetAttributes (path, FileAttributes.Normal);
629                         }
630                 }
631
632                 [Test]
633                 public void GetAttributes_System ()
634                 {
635                         if (RunningOnUnix)
636                                 Assert.Ignore ("FileAttributes.System is not supported on Unix.");
637
638                         FileAttributes attrs;
639
640                         string path = Path.Combine (TempFolder, "GetAttributes.tmp");
641                         File.Create (path).Close ();
642
643                         attrs = File.GetAttributes (path);
644                         Assert.IsFalse ((attrs & FileAttributes.System) != 0, "#1");
645
646                         attrs |= FileAttributes.System;
647                         File.SetAttributes (path, FileAttributes.System);
648
649                         attrs = File.GetAttributes (path);
650                         Assert.IsTrue ((attrs & FileAttributes.System) != 0, "#2");
651                 }
652
653                 [Test]
654                 public void GetAttributes_Path_DoesNotExist ()
655                 {
656                         string path = Path.Combine (TempFolder, "GetAttributes.tmp");
657                         try {
658                                 File.GetAttributes (path);
659                                 Assert.Fail ("#1");
660                         } catch (FileNotFoundException ex) {
661                                 Assert.AreEqual (typeof (FileNotFoundException), ex.GetType (), "#2");
662                                 Assert.AreEqual (path, ex.FileName, "#3");
663                                 Assert.IsNull (ex.InnerException, "#4");
664                                 Assert.IsNotNull (ex.Message, "#5");
665                         }
666                 }
667
668                 [Test]
669                 public void GetAttributes_Path_Empty ()
670                 {
671                         try {
672                                 File.GetAttributes (string.Empty);
673                                 Assert.Fail ("#1");
674                         } catch (ArgumentException ex) {
675                                 // Empty file name is not legal
676                                 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
677                                 Assert.IsNull (ex.InnerException, "#3");
678                                 Assert.IsNotNull (ex.Message, "#4");
679                                 Assert.IsNull (ex.ParamName, "#5");
680                         }
681                 }
682
683                 [Test]
684                 public void GetAttributes_Path_Null ()
685                 {
686                         try {
687                                 File.GetAttributes (null);
688                                 Assert.Fail ("#1");
689                         } catch (ArgumentNullException ex) {
690                                 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
691                                 Assert.IsNull (ex.InnerException, "#3");
692                                 Assert.IsNotNull (ex.Message, "#4");
693                                 Assert.AreEqual ("path", ex.ParamName, "#5");
694                         }
695                 }
696
697                 [Test]
698                 public void Move_SourceFileName_Null ()
699                 {
700                         try {
701                                 File.Move (null, "b");
702                                 Assert.Fail ("#1");
703                         } catch (ArgumentNullException ex) {
704                                 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
705                                 Assert.IsNull (ex.InnerException, "#3");
706                                 Assert.IsNotNull (ex.Message, "#4");
707                                 Assert.AreEqual ("sourceFileName", ex.ParamName, "#5");
708                         }
709                 }
710
711                 [Test]
712                 public void Move_DestFileName_Null ()
713                 {
714                         try {
715                                 File.Move ("a", null);
716                                 Assert.Fail ("#1");
717                         } catch (ArgumentNullException ex) {
718                                 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
719                                 Assert.IsNull (ex.InnerException, "#3");
720                                 Assert.IsNotNull (ex.Message, "#4");
721                                 Assert.AreEqual ("destFileName", ex.ParamName, "#5");
722                         }
723                 }
724
725                 [Test]
726                 public void Move_SourceFileName_Empty ()
727                 {
728                         try {
729                                 File.Move (string.Empty, "b");
730                                 Assert.Fail ("#1");
731                         } catch (ArgumentException ex) {
732                                 // Empty file name is not legal
733                                 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
734                                 Assert.IsNull (ex.InnerException, "#3");
735                                 Assert.IsNotNull (ex.Message, "#4");
736                                 Assert.AreEqual ("sourceFileName", ex.ParamName, "#5");
737                         }
738                 }
739
740                 [Test]
741                 public void Move_DestFileName_Empty ()
742                 {
743                         try {
744                                 File.Move ("a", string.Empty);
745                                 Assert.Fail ("#1");
746                         } catch (ArgumentException ex) {
747                                 // Empty file name is not legal
748                                 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
749                                 Assert.IsNull (ex.InnerException, "#3");
750                                 Assert.IsNotNull (ex.Message, "#4");
751                                 Assert.AreEqual ("destFileName", ex.ParamName, "#5");
752                         }
753                 }
754
755                 [Test]
756                 public void Move_SourceFileName_Whitespace ()
757                 {
758                         try {
759                                 File.Move (" ", "b");
760                                 Assert.Fail ("#1");
761                         } catch (ArgumentException ex) {
762                                 // The path is not of a legal form
763                                 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
764                                 Assert.IsNull (ex.InnerException, "#3");
765                                 Assert.IsNotNull (ex.Message, "#4");
766                                 Assert.IsNull (ex.ParamName, "#5");
767                         }
768                 }
769
770                 [Test]
771                 public void Move_DestFileName_Whitespace ()
772                 {
773                         try {
774                                 File.Move ("a", " ");
775                                 Assert.Fail ("#1");
776                         } catch (ArgumentException ex) {
777                                 // The path is not of a legal form
778                                 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
779                                 Assert.IsNull (ex.InnerException, "#3");
780                                 Assert.IsNotNull (ex.Message, "#4");
781                                 Assert.IsNull (ex.ParamName, "#5");
782                         }
783                 }
784
785                 [Test]
786                 public void Move_SourceFileName_DoesNotExist ()
787                 {
788                         string file = TempFolder + Path.DirectorySeparatorChar + "doesnotexist";
789                         DeleteFile (file);
790                         try {
791                                 File.Move (file, "b");
792                                 Assert.Fail ("#1");
793                         } catch (FileNotFoundException ex) {
794                                 Assert.AreEqual (typeof (FileNotFoundException), ex.GetType (), "#2");
795                                 Assert.AreEqual (file, ex.FileName, "#3");
796                                 Assert.IsNull (ex.InnerException, "#4");
797                                 Assert.IsNotNull (ex.Message, "#5");
798                         }
799                 }
800
801                 [Test]
802                 public void Move_DestFileName_DirectoryDoesNotExist ()
803                 {
804                         string sourceFile = TempFolder + Path.DirectorySeparatorChar + "foo";
805                         string destFile = Path.Combine (Path.Combine (TempFolder, "doesnotexist"), "b");
806                         DeleteFile (sourceFile);
807                         try {
808                                 File.Create (sourceFile).Close ();
809                                 try {
810                                         File.Move (sourceFile, destFile);
811                                         Assert.Fail ("#1");
812                                 } catch (DirectoryNotFoundException ex) {
813                                         // Could not find a part of the path
814                                         Assert.AreEqual (typeof (DirectoryNotFoundException), ex.GetType (), "#2");
815                                         Assert.IsNull (ex.InnerException, "#3");
816                                         Assert.IsNotNull (ex.Message, "#4");
817                                         Assert.IsFalse (ex.Message.IndexOf (destFile) != -1, "#5");
818                                 }
819                         } finally {
820                                 DeleteFile (sourceFile);
821                         }
822                 }
823
824                 [Test]
825                 public void Move_DestFileName_AlreadyExists ()
826                 {
827                         string sourceFile = TempFolder + Path.DirectorySeparatorChar + "foo";
828                         string destFile;
829
830                         // move to same directory
831                         File.Create (sourceFile).Close ();
832                         try {
833                                 File.Move (sourceFile, TempFolder);
834                                 Assert.Fail ("#A1");
835                         } catch (IOException ex) {
836                                 // Cannot create a file when that file already exists
837                                 Assert.AreEqual (typeof (IOException), ex.GetType (), "#A2");
838                                 Assert.IsNull (ex.InnerException, "#A3");
839                                 Assert.IsNotNull (ex.Message, "#A4");
840                                 Assert.IsFalse (ex.Message.IndexOf (sourceFile) != -1, "#A5");
841                                 Assert.IsFalse (ex.Message.IndexOf (TempFolder) != -1, "#A6");
842                         } finally {
843                                 DeleteFile (sourceFile);
844                         }
845
846                         // move to exist file
847                         File.Create (sourceFile).Close ();
848                         destFile = TempFolder + Path.DirectorySeparatorChar + "bar";
849                         File.Create (destFile).Close ();
850                         try {
851                                 File.Move (sourceFile, destFile);
852                                 Assert.Fail ("#B1");
853                         } catch (IOException ex) {
854                                 // Cannot create a file when that file already exists
855                                 Assert.AreEqual (typeof (IOException), ex.GetType (), "#B2");
856                                 Assert.IsNull (ex.InnerException, "#B3");
857                                 Assert.IsNotNull (ex.Message, "#B4");
858                                 Assert.IsFalse (ex.Message.IndexOf (sourceFile) != -1, "#B5");
859                                 Assert.IsFalse (ex.Message.IndexOf (destFile) != -1, "#B6");
860                         } finally {
861                                 DeleteFile (sourceFile);
862                                 DeleteFile (destFile);
863                         }
864
865                         // move to existing directory
866                         File.Create (sourceFile).Close ();
867                         destFile = TempFolder + Path.DirectorySeparatorChar + "bar";
868                         Directory.CreateDirectory (destFile);
869                         try {
870                                 File.Move (sourceFile, destFile);
871                                 Assert.Fail ("#C1");
872                         } catch (IOException ex) {
873                                 // Cannot create a file when that file already exists
874                                 Assert.AreEqual (typeof (IOException), ex.GetType (), "#C2");
875                                 Assert.IsNull (ex.InnerException, "#C3");
876                                 Assert.IsNotNull (ex.Message, "#C4");
877                                 Assert.IsFalse (ex.Message.IndexOf (sourceFile) != -1, "#C5");
878                                 Assert.IsFalse (ex.Message.IndexOf (destFile) != -1, "#C6");
879                         } finally {
880                                 DeleteFile (sourceFile);
881                                 DeleteDirectory (destFile);
882                         }
883                 }
884
885                 [Test]
886                 public void Move ()
887                 {
888                         string bar = TempFolder + Path.DirectorySeparatorChar + "bar";
889                         string baz = TempFolder + Path.DirectorySeparatorChar + "baz";
890                         if (!File.Exists (bar)) {
891                                 FileStream f = File.Create(bar);
892                                 f.Close();
893                         }
894                         
895                         Assert.IsTrue (File.Exists (bar), "#1");
896                         File.Move (bar, baz);
897                         Assert.IsFalse (File.Exists (bar), "#2");
898                         Assert.IsTrue (File.Exists (baz), "#3");
899
900                         // Test moving of directories
901                         string dir = Path.Combine (TempFolder, "dir");
902                         string dir2 = Path.Combine (TempFolder, "dir2");
903                         string dir_foo = Path.Combine (dir, "foo");
904                         string dir2_foo = Path.Combine (dir2, "foo");
905
906                         if (Directory.Exists (dir))
907                                 Directory.Delete (dir, true);
908
909                         Directory.CreateDirectory (dir);
910                         Directory.CreateDirectory (dir2);
911                         File.Create (dir_foo).Close ();
912                         File.Move (dir_foo, dir2_foo);
913                         Assert.IsTrue (File.Exists (dir2_foo), "#4");
914                         
915                         Directory.Delete (dir, true);
916                         Directory.Delete (dir2, true);
917                         DeleteFile (dir_foo);
918                         DeleteFile (dir2_foo);
919                 }
920
921                 [Test]
922                 public void Move_FileLock ()
923                 {
924                         string sourceFile = Path.GetTempFileName ();
925                         string destFile = Path.GetTempFileName ();
926
927                         // source file locked
928                         using (File.Open (sourceFile, FileMode.Open, FileAccess.ReadWrite, FileShare.None)) {
929                                 try {
930                                         File.Move (sourceFile, destFile);
931                                         Assert.Fail ("#A1");
932                                 } catch (IOException ex) {
933                                         // The process cannot access the file because
934                                         // it is being used by another process
935                                         Assert.AreEqual (typeof (IOException), ex.GetType (), "#A2");
936                                         Assert.IsNull (ex.InnerException, "#A3");
937                                         Assert.IsNotNull (ex.Message, "#A4");
938                                 }
939                         }
940
941                         // destination file locked
942                         using (File.Open (destFile, FileMode.Open, FileAccess.ReadWrite, FileShare.None)) {
943                                 try {
944                                         File.Move (sourceFile, destFile);
945                                         Assert.Fail ("#B1");
946                                 } catch (IOException ex) {
947                                         // The process cannot access the file because
948                                         // it is being used by another process
949                                         Assert.AreEqual (typeof (IOException), ex.GetType (), "#B2");
950                                         Assert.IsNull (ex.InnerException, "#B3");
951                                         Assert.IsNotNull (ex.Message, "#B4");
952                                 }
953                         }
954                 }
955
956                 [Test]
957                 public void Open ()
958                 {
959                         string path = null;
960                         FileStream stream = null;
961
962                         path = TempFolder + Path.DirectorySeparatorChar + "AFile.txt";
963                         try {
964                                 if (!File.Exists (path))
965                                         stream = File.Create (path);
966                                 stream.Close ();
967                                 stream = File.Open (path, FileMode.Open);
968                                 stream.Close ();
969                         } finally {
970                                 if (stream != null)
971                                         stream.Close ();
972                                 DeleteFile (path);
973                         }
974
975                         stream = null;
976
977                         if (!File.Exists (path))
978                                 File.Create (path).Close ();
979                         try {
980                                 stream = File.Open (path, FileMode.Open);
981                                 Assert.IsTrue (stream.CanRead, "#A1");
982                                 Assert.IsTrue (stream.CanSeek, "#A2");
983                                 Assert.IsTrue (stream.CanWrite, "#A3");
984                                 stream.Close ();
985
986                                 stream = File.Open (path, FileMode.Open, FileAccess.Write);
987                                 Assert.IsFalse (stream.CanRead, "#B1");
988                                 Assert.IsTrue (stream.CanSeek, "#B2");
989                                 Assert.IsTrue (stream.CanWrite, "#B3");
990                                 stream.Close ();
991
992                                 stream = File.Open (path, FileMode.Open, FileAccess.Read);
993                                 Assert.IsTrue (stream.CanRead, "#C1");
994                                 Assert.IsTrue (stream.CanSeek, "#C2");
995                                 Assert.IsFalse (stream.CanWrite, "#C3");
996                                 stream.Close ();
997                         } finally {
998                                 if (stream != null)
999                                         stream.Close ();
1000                                 DeleteFile (path);
1001                         }
1002
1003                         stream = null;
1004
1005                         /* Exception tests */
1006                         path = TempFolder + Path.DirectorySeparatorChar + "filedoesnotexist";
1007                         try {
1008                                 stream = File.Open (path, FileMode.Open);
1009                                 Assert.Fail ("#D1");
1010                         } catch (FileNotFoundException ex) {
1011                                 Assert.AreEqual (typeof (FileNotFoundException), ex.GetType (), "#D2");
1012                                 Assert.AreEqual (path, ex.FileName, "#D3");
1013                                 Assert.IsNull (ex.InnerException, "#D4");
1014                                 Assert.IsNotNull (ex.Message, "#D5");
1015                         } finally {
1016                                 if (stream != null)
1017                                         stream.Close ();
1018                                 DeleteFile (path);
1019                         }
1020                 }
1021
1022                 [Test]
1023                 public void Open_CreateNewMode_ReadAccess ()
1024                 {
1025                         string path = TempFolder + Path.DirectorySeparatorChar + "AFile.txt";
1026                         FileStream stream = null;
1027                         try {
1028                                 stream = File.Open (TempFolder + Path.DirectorySeparatorChar + "AFile.txt", FileMode.CreateNew, FileAccess.Read);
1029                                 Assert.Fail ("#1");
1030                         } catch (ArgumentException ex) {
1031                                 // Combining FileMode: CreateNew with FileAccess: Read is invalid
1032                                 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
1033                                 Assert.IsNull (ex.InnerException, "#3");
1034                                 Assert.IsNotNull (ex.Message, "#4");
1035                                 Assert.IsNull (ex.ParamName, "#5");
1036                         } finally {
1037                                 if (stream != null)
1038                                         stream.Close ();
1039                                 DeleteFile (path);
1040                         }
1041                 }
1042
1043                 [Test]
1044                 public void Open_AppendMode_ReadAccess ()
1045                 {
1046                         string path = TempFolder + Path.DirectorySeparatorChar + "AFile.txt";
1047                         FileStream s = null;
1048                         if (!File.Exists (path))
1049                                 File.Create (path).Close ();
1050                         try {
1051                                 s = File.Open (path, FileMode.Append, FileAccess.Read);
1052                                 Assert.Fail ("#1");
1053                         } catch (ArgumentException ex) {
1054                                 // Combining FileMode: Append with FileAccess: Read is invalid
1055                                 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
1056                                 Assert.IsNull (ex.InnerException, "#3");
1057                                 Assert.IsNotNull (ex.Message, "#4");
1058                                 Assert.IsNull (ex.ParamName, "#5");
1059                         } finally {
1060                                 if (s != null)
1061                                         s.Close ();
1062                                 DeleteFile (path);
1063                         }
1064                 }
1065
1066                 [Test]
1067                 public void OpenRead ()
1068                 {
1069                         string path = TempFolder + Path.DirectorySeparatorChar + "AFile.txt";
1070                         if (!File.Exists (path))
1071                                 File.Create (path).Close ();
1072                         FileStream stream = null;
1073                         
1074                         try {
1075                                 stream = File.OpenRead (path);
1076                                 Assert.IsTrue (stream.CanRead, "#1");
1077                                 Assert.IsTrue (stream.CanSeek, "#2");
1078                                 Assert.IsFalse (stream.CanWrite, "#3");
1079                         } finally {
1080                                 if (stream != null)
1081                                         stream.Close ();
1082                                 DeleteFile (path);
1083                         }
1084                 }
1085
1086                 [Test]
1087                 public void OpenWrite ()
1088                 {
1089                         string path = TempFolder + Path.DirectorySeparatorChar + "AFile.txt";
1090                         if (!File.Exists (path))
1091                                 File.Create (path).Close ();
1092                         FileStream stream = null;
1093
1094                         try {
1095                                 stream = File.OpenWrite (path);
1096                                 Assert.IsFalse (stream.CanRead, "#1");
1097                                 Assert.IsTrue (stream.CanSeek, "#2");
1098                                 Assert.IsTrue (stream.CanWrite, "#3");
1099                                 stream.Close ();
1100                         } finally {
1101                                 if (stream != null)
1102                                         stream.Close ();
1103                                 DeleteFile (path);
1104                         }
1105                 }
1106
1107                 [Test]
1108                 public void TestGetCreationTime ()
1109                 {
1110                         string path = TempFolder + Path.DirectorySeparatorChar + "baz";
1111                         DeleteFile (path);
1112
1113                         try {
1114                                 File.Create (path).Close();
1115                                 DateTime time = File.GetCreationTime (path);
1116                                 Assert.IsTrue ((DateTime.Now - time).TotalSeconds < 10);
1117                         } finally {
1118                                 DeleteFile (path);
1119                         }
1120                 }
1121
1122                 [Test]
1123                 public void CreationTime ()
1124                 {
1125                         if (RunningOnUnix)
1126                                 Assert.Ignore ("Setting the creation time on Unix is not possible.");
1127
1128                         string path = Path.GetTempFileName ();
1129                         try {
1130                                 File.SetCreationTime (path, new DateTime (2002, 4, 6, 4, 6, 4));
1131                                 DateTime time = File.GetCreationTime (path);
1132                                 Assert.AreEqual (2002, time.Year, "#A1");
1133                                 Assert.AreEqual (4, time.Month, "#A2");
1134                                 Assert.AreEqual (6, time.Day, "#A3");
1135                                 Assert.AreEqual (4, time.Hour, "#A4");
1136                                 Assert.AreEqual (4, time.Second, "#A5");
1137
1138                                 time = TimeZone.CurrentTimeZone.ToLocalTime (File.GetCreationTimeUtc (path));
1139                                 Assert.AreEqual (2002, time.Year, "#B1");
1140                                 Assert.AreEqual (4, time.Month, "#B2");
1141                                 Assert.AreEqual (6, time.Day, "#B3");
1142                                 Assert.AreEqual (4, time.Hour, "#B4");
1143                                 Assert.AreEqual (4, time.Second, "#B5");
1144
1145                                 File.SetCreationTimeUtc (path, new DateTime (2002, 4, 6, 4, 6, 4));
1146                                 time = File.GetCreationTimeUtc (path);
1147                                 Assert.AreEqual (2002, time.Year, "#C1");
1148                                 Assert.AreEqual (4, time.Month, "#C2");
1149                                 Assert.AreEqual (6, time.Day, "#C3");
1150                                 Assert.AreEqual (4, time.Hour, "#C4");
1151                                 Assert.AreEqual (4, time.Second, "#C5");
1152
1153                                 time = TimeZone.CurrentTimeZone.ToUniversalTime (File.GetCreationTime (path));
1154                                 Assert.AreEqual (2002, time.Year, "#D1");
1155                                 Assert.AreEqual (4, time.Month, "#D2");
1156                                 Assert.AreEqual (6, time.Day, "#D3");
1157                                 Assert.AreEqual (4, time.Hour, "#D4");
1158                                 Assert.AreEqual (4, time.Second, "#D5");
1159                         } finally {
1160                                 DeleteFile (path);
1161                         }
1162                 }
1163
1164                 [Test]
1165                 public void LastAccessTime ()
1166                 {
1167                         string path = TempFolder + Path.DirectorySeparatorChar + "lastAccessTime";
1168                         if (File.Exists (path))
1169                                 File.Delete (path);
1170                         FileStream stream = null;
1171                         try {
1172                                 stream = File.Create (path);
1173                                 stream.Close ();
1174
1175                                 File.SetLastAccessTime (path, new DateTime (2002, 4, 6, 4, 6, 4));
1176                                 DateTime time = File.GetLastAccessTime (path);
1177                                 Assert.AreEqual (2002, time.Year, "#A1");
1178                                 Assert.AreEqual (4, time.Month, "#A2");
1179                                 Assert.AreEqual (6, time.Day, "#A3");
1180                                 Assert.AreEqual (4, time.Hour, "#A4");
1181                                 Assert.AreEqual (4, time.Second, "#A5");
1182
1183                                 time = TimeZone.CurrentTimeZone.ToLocalTime (File.GetLastAccessTimeUtc (path));
1184                                 Assert.AreEqual (2002, time.Year, "#B1");
1185                                 Assert.AreEqual (4, time.Month, "#B2");
1186                                 Assert.AreEqual (6, time.Day, "#B3");
1187                                 Assert.AreEqual (4, time.Hour, "#B4");
1188                                 Assert.AreEqual (4, time.Second, "#B5");
1189
1190                                 File.SetLastAccessTimeUtc (path, new DateTime (2002, 4, 6, 4, 6, 4));
1191                                 time = File.GetLastAccessTimeUtc (path);
1192                                 Assert.AreEqual (2002, time.Year, "#C1");
1193                                 Assert.AreEqual (4, time.Month, "#C2");
1194                                 Assert.AreEqual (6, time.Day, "#C3");
1195                                 Assert.AreEqual (4, time.Hour, "#C4");
1196                                 Assert.AreEqual (4, time.Second, "#C5");
1197
1198                                 time = TimeZone.CurrentTimeZone.ToUniversalTime (File.GetLastAccessTime (path));
1199                                 Assert.AreEqual (2002, time.Year, "#D1");
1200                                 Assert.AreEqual (4, time.Month, "#D2");
1201                                 Assert.AreEqual (6, time.Day, "#D3");
1202                                 Assert.AreEqual (4, time.Hour, "#D4");
1203                                 Assert.AreEqual (4, time.Second, "#D5");
1204                         } finally {
1205                                 if (stream != null)
1206                                         stream.Close ();
1207                                 DeleteFile (path);
1208                         }
1209                 }
1210
1211                 [Test]
1212                 public void LastWriteTime ()
1213                 {
1214                         string path = TempFolder + Path.DirectorySeparatorChar + "lastWriteTime";
1215                         if (File.Exists (path))
1216                                 File.Delete (path);
1217                         FileStream stream = null;
1218                         try {
1219                                 stream = File.Create (path);
1220                                 stream.Close ();
1221
1222                                 File.SetLastWriteTime (path, new DateTime (2002, 4, 6, 4, 6, 4));
1223                                 DateTime time = File.GetLastWriteTime (path);
1224                                 Assert.AreEqual (2002, time.Year, "#A1");
1225                                 Assert.AreEqual (4, time.Month, "#A2");
1226                                 Assert.AreEqual (6, time.Day, "#A3");
1227                                 Assert.AreEqual (4, time.Hour, "#A4");
1228                                 Assert.AreEqual (4, time.Second, "#A5");
1229
1230                                 time = TimeZone.CurrentTimeZone.ToLocalTime (File.GetLastWriteTimeUtc (path));
1231                                 Assert.AreEqual (2002, time.Year, "#B1");
1232                                 Assert.AreEqual (4, time.Month, "#B2");
1233                                 Assert.AreEqual (6, time.Day, "#B3");
1234                                 Assert.AreEqual (4, time.Hour, "#B4");
1235                                 Assert.AreEqual (4, time.Second, "#B5");
1236
1237                                 File.SetLastWriteTimeUtc (path, new DateTime (2002, 4, 6, 4, 6, 4));
1238                                 time = File.GetLastWriteTimeUtc (path);
1239                                 Assert.AreEqual (2002, time.Year, "#C1");
1240                                 Assert.AreEqual (4, time.Month, "#C2");
1241                                 Assert.AreEqual (6, time.Day, "#C3");
1242                                 Assert.AreEqual (4, time.Hour, "#C4");
1243                                 Assert.AreEqual (4, time.Second, "#C5");
1244
1245                                 time = TimeZone.CurrentTimeZone.ToUniversalTime (File.GetLastWriteTime (path));
1246                                 Assert.AreEqual (2002, time.Year, "#D1");
1247                                 Assert.AreEqual (4, time.Month, "#D2");
1248                                 Assert.AreEqual (6, time.Day, "#D3");
1249                                 Assert.AreEqual (4, time.Hour, "#D4");
1250                                 Assert.AreEqual (4, time.Second, "#D5");
1251                         } finally {
1252                                 if (stream != null)
1253                                         stream.Close ();
1254                                 DeleteFile (path);
1255                         }
1256                 }
1257
1258                 [Test]
1259                 public void GetCreationTime_Path_Null ()
1260                 {
1261                         try {
1262                                 File.GetCreationTime (null as string);
1263                                 Assert.Fail ("#1");
1264                         } catch (ArgumentNullException ex) {
1265                                 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
1266                                 Assert.IsNull (ex.InnerException, "#3");
1267                                 Assert.IsNotNull (ex.Message, "#4");
1268                                 Assert.AreEqual ("path", ex.ParamName, "#5");
1269                         }
1270                 }
1271
1272                 [Test]
1273                 public void GetCreationTime_Path_Empty ()
1274                 {
1275                         try {
1276                                 File.GetCreationTime (string.Empty);
1277                                 Assert.Fail ("#1");
1278                         } catch (ArgumentException ex) {
1279                                 // Empty file name is not legal
1280                                 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
1281                                 Assert.IsNull (ex.InnerException, "#3");
1282                                 Assert.IsNotNull (ex.Message, "#4");
1283                                 Assert.IsNull (ex.ParamName, "#5");
1284                         }
1285                 }
1286         
1287                 [Test]
1288                 public void GetCreationTime_Path_DoesNotExist ()
1289                 {
1290                         string path = TempFolder + Path.DirectorySeparatorChar + "GetCreationTimeException3";
1291                         DeleteFile (path);
1292
1293                         DateTime time = File.GetCreationTime (path);
1294                         DateTime expectedTime = (new DateTime (1601, 1, 1)).ToLocalTime ();
1295                         Assert.AreEqual (expectedTime.Year, time.Year, "#1");
1296                         Assert.AreEqual (expectedTime.Month, time.Month, "#2");
1297                         Assert.AreEqual (expectedTime.Day, time.Day, "#3");
1298                         Assert.AreEqual (expectedTime.Hour, time.Hour, "#4");
1299                         Assert.AreEqual (expectedTime.Second, time.Second, "#5");
1300                         Assert.AreEqual (expectedTime.Millisecond, time.Millisecond, "#6");
1301                 }
1302
1303                 [Test]
1304                 public void GetCreationTime_Path_Whitespace ()
1305                 {
1306                         try {
1307                                 File.GetCreationTime ("    ");
1308                                 Assert.Fail ("#1");
1309                         } catch (ArgumentException ex) {
1310                                 // The path is not of a legal form
1311                                 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
1312                                 Assert.IsNull (ex.InnerException, "#3");
1313                                 Assert.IsNotNull (ex.Message, "#4");
1314                                 Assert.IsNull (ex.ParamName, "#5");
1315                         }
1316                 }
1317
1318                 [Test]
1319                 public void GetCreationTime_Path_InvalidPathChars ()
1320                 {
1321                         try {
1322                                 File.GetCreationTime (Path.InvalidPathChars [0].ToString ());
1323                                 Assert.Fail ("#1");
1324                         } catch (ArgumentException ex) {
1325                                 // Illegal characters in path
1326                                 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
1327                                 Assert.IsNull (ex.InnerException, "#3");
1328                                 Assert.IsNotNull (ex.Message, "#4");
1329                                 Assert.IsNull (ex.ParamName, "#5");
1330                         }
1331                 }
1332
1333                 [Test]
1334                 public void GetCreationTimeUtc_Path_Null ()
1335                 {
1336                         try {
1337                                 File.GetCreationTimeUtc (null as string);
1338                                 Assert.Fail ("#1");
1339                         } catch (ArgumentNullException ex) {
1340                                 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
1341                                 Assert.IsNull (ex.InnerException, "#3");
1342                                 Assert.IsNotNull (ex.Message, "#4");
1343                                 Assert.AreEqual ("path", ex.ParamName, "#5");
1344                         }
1345                 }
1346
1347                 [Test]
1348                 public void GetCreationTimeUtc_Path_Empty ()
1349                 {
1350                         try {
1351                                 File.GetCreationTimeUtc (string.Empty);
1352                                 Assert.Fail ("#1");
1353                         } catch (ArgumentException ex) {
1354                                 // Empty file name is not legal
1355                                 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
1356                                 Assert.IsNull (ex.InnerException, "#3");
1357                                 Assert.IsNotNull (ex.Message, "#4");
1358                                 Assert.IsNull (ex.ParamName, "#5");
1359                         }
1360                 }
1361         
1362                 [Test]
1363                 public void GetCreationTimeUtc_Path_DoesNotExist ()
1364                 {
1365                         string path = TempFolder + Path.DirectorySeparatorChar + "GetCreationTimeUtcException3";
1366                         DeleteFile (path);
1367
1368                         DateTime time = File.GetCreationTimeUtc (path);
1369                         Assert.AreEqual (1601, time.Year, "#1");
1370                         Assert.AreEqual (1, time.Month, "#2");
1371                         Assert.AreEqual (1, time.Day, "#3");
1372                         Assert.AreEqual (0, time.Hour, "#4");
1373                         Assert.AreEqual (0, time.Second, "#5");
1374                         Assert.AreEqual (0, time.Millisecond, "#6");
1375                 }
1376
1377                 [Test]
1378                 public void GetCreationTimeUtc_Path_Whitespace ()
1379                 {
1380                         try {
1381                                 File.GetCreationTimeUtc ("    ");
1382                                 Assert.Fail ("#1");
1383                         } catch (ArgumentException ex) {
1384                                 // The path is not of a legal form
1385                                 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
1386                                 Assert.IsNull (ex.InnerException, "#3");
1387                                 Assert.IsNotNull (ex.Message, "#4");
1388                                 Assert.IsNull (ex.ParamName, "#5");
1389                         }
1390                 }
1391
1392                 [Test]
1393                 public void GetCreationTimeUtc_Path_InvalidPathChars ()
1394                 {
1395                         try {
1396                                 File.GetCreationTimeUtc (Path.InvalidPathChars [0].ToString ());
1397                                 Assert.Fail ("#1");
1398                         } catch (ArgumentException ex) {
1399                                 // Illegal characters in path
1400                                 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
1401                                 Assert.IsNull (ex.InnerException, "#3");
1402                                 Assert.IsNotNull (ex.Message, "#4");
1403                                 Assert.IsNull (ex.ParamName, "#5");
1404                         }
1405                 }
1406
1407                 [Test]
1408                 public void GetLastAccessTime_Path_Null ()
1409                 {
1410                         try {
1411                                 File.GetLastAccessTime (null as string);
1412                                 Assert.Fail ("#1");
1413                         } catch (ArgumentNullException ex) {
1414                                 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
1415                                 Assert.IsNull (ex.InnerException, "#3");
1416                                 Assert.IsNotNull (ex.Message, "#4");
1417                                 Assert.AreEqual ("path", ex.ParamName, "#5");
1418                         }
1419                 }
1420
1421                 [Test]
1422                 public void GetLastAccessTime_Path_Empty ()
1423                 {
1424                         try {
1425                                 File.GetLastAccessTime (string.Empty);
1426                                 Assert.Fail ("#1");
1427                         } catch (ArgumentException ex) {
1428                                 // Empty file name is not legal
1429                                 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
1430                                 Assert.IsNull (ex.InnerException, "#3");
1431                                 Assert.IsNotNull (ex.Message, "#4");
1432                                 Assert.IsNull (ex.ParamName, "#5");
1433                         }
1434                 }
1435         
1436                 [Test]
1437                 public void GetLastAccessTime_Path_DoesNotExist ()
1438                 {
1439                         string path = TempFolder + Path.DirectorySeparatorChar + "GetLastAccessTimeException3";
1440                         DeleteFile (path);
1441
1442                         DateTime time = File.GetLastAccessTime (path);
1443                         DateTime expectedTime = (new DateTime (1601, 1, 1)).ToLocalTime ();
1444                         Assert.AreEqual (expectedTime.Year, time.Year, "#1");
1445                         Assert.AreEqual (expectedTime.Month, time.Month, "#2");
1446                         Assert.AreEqual (expectedTime.Day, time.Day, "#3");
1447                         Assert.AreEqual (expectedTime.Hour, time.Hour, "#4");
1448                         Assert.AreEqual (expectedTime.Second, time.Second, "#5");
1449                         Assert.AreEqual (expectedTime.Millisecond, time.Millisecond, "#6");
1450                 }
1451
1452                 [Test]
1453                 public void GetLastAccessTime_Path_Whitespace ()
1454                 {
1455                         try {
1456                                 File.GetLastAccessTime ("    ");
1457                                 Assert.Fail ("#1");
1458                         } catch (ArgumentException ex) {
1459                                 // The path is not of a legal form
1460                                 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
1461                                 Assert.IsNull (ex.InnerException, "#3");
1462                                 Assert.IsNotNull (ex.Message, "#4");
1463                                 Assert.IsNull (ex.ParamName, "#5");
1464                         }
1465                 }
1466
1467                 [Test]
1468                 public void GetLastAccessTime_Path_InvalidPathChars ()
1469                 {
1470                         try {
1471                                 File.GetLastAccessTime (Path.InvalidPathChars [0].ToString ());
1472                                 Assert.Fail ("#1");
1473                         } catch (ArgumentException ex) {
1474                                 // Illegal characters in path
1475                                 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
1476                                 Assert.IsNull (ex.InnerException, "#3");
1477                                 Assert.IsNotNull (ex.Message, "#4");
1478                                 Assert.IsNull (ex.ParamName, "#5");
1479                         }
1480                 }
1481
1482                 [Test]
1483                 public void GetLastAccessTimeUtc_Path_Null ()
1484                 {
1485                         try {
1486                                 File.GetLastAccessTimeUtc (null as string);
1487                                 Assert.Fail ("#1");
1488                         } catch (ArgumentNullException ex) {
1489                                 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
1490                                 Assert.IsNull (ex.InnerException, "#3");
1491                                 Assert.IsNotNull (ex.Message, "#4");
1492                                 Assert.AreEqual ("path", ex.ParamName, "#5");
1493                         }
1494                 }
1495
1496                 [Test]
1497                 public void GetLastAccessTimeUtc_Path_Empty ()
1498                 {
1499                         try {
1500                                 File.GetLastAccessTimeUtc (string.Empty);
1501                                 Assert.Fail ("#1");
1502                         } catch (ArgumentException ex) {
1503                                 // Empty file name is not legal
1504                                 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
1505                                 Assert.IsNull (ex.InnerException, "#3");
1506                                 Assert.IsNotNull (ex.Message, "#4");
1507                                 Assert.IsNull (ex.ParamName, "#5");
1508                         }
1509                 }
1510         
1511                 [Test]
1512                 public void GetLastAccessTimeUtc_Path_DoesNotExist ()
1513                 {
1514                         string path = TempFolder + Path.DirectorySeparatorChar + "GetLastAccessTimeUtcException3";
1515                         DeleteFile (path);
1516
1517                         DateTime time = File.GetLastAccessTimeUtc (path);
1518                         Assert.AreEqual (1601, time.Year, "#1");
1519                         Assert.AreEqual (1, time.Month, "#2");
1520                         Assert.AreEqual (1, time.Day, "#3");
1521                         Assert.AreEqual (0, time.Hour, "#4");
1522                         Assert.AreEqual (0, time.Second, "#5");
1523                         Assert.AreEqual (0, time.Millisecond, "#6");
1524                 }
1525
1526                 [Test]
1527                 public void GetLastAccessTimeUtc_Path_Whitespace ()
1528                 {
1529                         try {
1530                                 File.GetLastAccessTimeUtc ("    ");
1531                                 Assert.Fail ("#1");
1532                         } catch (ArgumentException ex) {
1533                                 // The path is not of a legal form
1534                                 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
1535                                 Assert.IsNull (ex.InnerException, "#3");
1536                                 Assert.IsNotNull (ex.Message, "#4");
1537                                 Assert.IsNull (ex.ParamName, "#5");
1538                         }
1539                 }
1540
1541                 [Test]
1542                 public void GetLastAccessTimeUtc_Path_InvalidPathChars ()
1543                 {
1544                         try {
1545                                 File.GetLastAccessTimeUtc (Path.InvalidPathChars [0].ToString ());
1546                                 Assert.Fail ("#1");
1547                         } catch (ArgumentException ex) {
1548                                 // Illegal characters in path
1549                                 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
1550                                 Assert.IsNull (ex.InnerException, "#3");
1551                                 Assert.IsNotNull (ex.Message, "#4");
1552                                 Assert.IsNull (ex.ParamName, "#5");
1553                         }
1554                 }
1555
1556                 [Test]
1557                 public void GetLastWriteTime_Path_Null ()
1558                 {
1559                         try {
1560                                 File.GetLastWriteTime (null as string);
1561                                 Assert.Fail ("#1");
1562                         } catch (ArgumentNullException ex) {
1563                                 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
1564                                 Assert.IsNull (ex.InnerException, "#3");
1565                                 Assert.IsNotNull (ex.Message, "#4");
1566                                 Assert.AreEqual ("path", ex.ParamName, "#5");
1567                         }
1568                 }
1569
1570                 [Test]
1571                 public void GetLastWriteTime_Path_Empty ()
1572                 {
1573                         try {
1574                                 File.GetLastWriteTime (string.Empty);
1575                                 Assert.Fail ("#1");
1576                         } catch (ArgumentException ex) {
1577                                 // Empty file name is not legal
1578                                 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
1579                                 Assert.IsNull (ex.InnerException, "#3");
1580                                 Assert.IsNotNull (ex.Message, "#4");
1581                                 Assert.IsNull (ex.ParamName, "#5");
1582                         }
1583                 }
1584         
1585                 [Test]
1586                 public void GetLastWriteTime_Path_DoesNotExist ()
1587                 {
1588                         string path = TempFolder + Path.DirectorySeparatorChar + "GetLastAccessTimeUtcException3";
1589                         DeleteFile (path);
1590
1591                         DateTime time = File.GetLastWriteTime (path);
1592                         DateTime expectedTime = (new DateTime (1601, 1, 1)).ToLocalTime ();
1593                         Assert.AreEqual (expectedTime.Year, time.Year, "#1");
1594                         Assert.AreEqual (expectedTime.Month, time.Month, "#2");
1595                         Assert.AreEqual (expectedTime.Day, time.Day, "#3");
1596                         Assert.AreEqual (expectedTime.Hour, time.Hour, "#4");
1597                         Assert.AreEqual (expectedTime.Second, time.Second, "#5");
1598                         Assert.AreEqual (expectedTime.Millisecond, time.Millisecond, "#6");
1599                 }
1600
1601                 [Test]
1602                 public void GetLastWriteTime_Path_Whitespace ()
1603                 {
1604                         try {
1605                                 File.GetLastWriteTime ("    ");
1606                                 Assert.Fail ("#1");
1607                         } catch (ArgumentException ex) {
1608                                 // The path is not of a legal form
1609                                 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
1610                                 Assert.IsNull (ex.InnerException, "#3");
1611                                 Assert.IsNotNull (ex.Message, "#4");
1612                                 Assert.IsNull (ex.ParamName, "#5");
1613                         }
1614                 }
1615
1616                 [Test]
1617                 public void GetLastWriteTime_Path_InvalidPathChars ()
1618                 {
1619                         try {
1620                                 File.GetLastWriteTime (Path.InvalidPathChars [0].ToString ());
1621                                 Assert.Fail ("#1");
1622                         } catch (ArgumentException ex) {
1623                                 // Illegal characters in path
1624                                 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
1625                                 Assert.IsNull (ex.InnerException, "#3");
1626                                 Assert.IsNotNull (ex.Message, "#4");
1627                                 Assert.IsNull (ex.ParamName, "#5");
1628                         }
1629                 }
1630
1631                 [Test]
1632                 public void GetLastWriteTimeUtc_Path_Null ()
1633                 {
1634                         try {
1635                                 File.GetLastWriteTimeUtc (null as string);
1636                                 Assert.Fail ("#1");
1637                         } catch (ArgumentNullException ex) {
1638                                 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
1639                                 Assert.IsNull (ex.InnerException, "#3");
1640                                 Assert.IsNotNull (ex.Message, "#4");
1641                                 Assert.AreEqual ("path", ex.ParamName, "#5");
1642                         }
1643                 }
1644
1645                 [Test]
1646                 public void GetLastWriteTimeUtc_Path_Empty ()
1647                 {
1648                         try {
1649                                 File.GetLastWriteTimeUtc (string.Empty);
1650                                 Assert.Fail ("#1");
1651                         } catch (ArgumentException ex) {
1652                                 // Empty file name is not legal
1653                                 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
1654                                 Assert.IsNull (ex.InnerException, "#3");
1655                                 Assert.IsNotNull (ex.Message, "#4");
1656                                 Assert.IsNull (ex.ParamName, "#5");
1657                         }
1658                 }
1659         
1660                 [Test]
1661                 public void GetLastWriteTimeUtc_Path_DoesNotExist ()
1662                 {
1663                         string path = TempFolder + Path.DirectorySeparatorChar + "GetLastWriteTimeUtcException3";
1664                         DeleteFile (path);
1665
1666                         DateTime time = File.GetLastWriteTimeUtc (path);
1667                         Assert.AreEqual (1601, time.Year, "#1");
1668                         Assert.AreEqual (1, time.Month, "#2");
1669                         Assert.AreEqual (1, time.Day, "#3");
1670                         Assert.AreEqual (0, time.Hour, "#4");
1671                         Assert.AreEqual (0, time.Second, "#5");
1672                         Assert.AreEqual (0, time.Millisecond, "#6");
1673                 }
1674
1675                 [Test]
1676                 public void GetLastWriteTimeUtc_Path_Whitespace ()
1677                 {
1678                         try {
1679                                 File.GetLastWriteTimeUtc ("    ");
1680                                 Assert.Fail ("#1");
1681                         } catch (ArgumentException ex) {
1682                                 // The path is not of a legal form
1683                                 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
1684                                 Assert.IsNull (ex.InnerException, "#3");
1685                                 Assert.IsNotNull (ex.Message, "#4");
1686                                 Assert.IsNull (ex.ParamName, "#5");
1687                         }
1688                 }
1689
1690                 [Test]
1691                 public void GetLastWriteTimeUtc_Path_InvalidPathChars ()
1692                 {
1693                         try {
1694                                 File.GetLastWriteTimeUtc (Path.InvalidPathChars [0].ToString ());
1695                                 Assert.Fail ("#1");
1696                         } catch (ArgumentException ex) {
1697                                 // Illegal characters in path
1698                                 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
1699                                 Assert.IsNull (ex.InnerException, "#3");
1700                                 Assert.IsNotNull (ex.Message, "#4");
1701                                 Assert.IsNull (ex.ParamName, "#5");
1702                         }
1703                 }
1704
1705                 [Test]
1706                 public void FileStreamClose ()
1707                 {
1708                         string path = TempFolder + Path.DirectorySeparatorChar + "FileStreamClose";
1709                         FileStream stream = null;
1710                         try {
1711                                 stream = File.Create (path);
1712                                 stream.Close ();
1713                                 File.Delete (path);
1714                         } finally {
1715                                 if (stream != null)
1716                                         stream.Close ();
1717                                 DeleteFile (path);
1718                         }
1719                 }
1720                 
1721                 // SetCreationTime and SetCreationTimeUtc exceptions
1722
1723                 [Test]
1724                 public void SetCreationTime_Path_Null ()
1725                 {
1726                         try {
1727                                 File.SetCreationTime (null as string, new DateTime (2000, 12, 12, 11, 59, 59));
1728                                 Assert.Fail ("#1");
1729                         } catch (ArgumentNullException ex) {
1730                                 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
1731                                 Assert.IsNull (ex.InnerException, "#3");
1732                                 Assert.IsNotNull (ex.Message, "#4");
1733                                 Assert.AreEqual ("path", ex.ParamName, "#5");
1734                         }
1735                 }
1736
1737                 [Test]
1738                 public void SetCreationTime_Path_Empty ()
1739                 {
1740                         try {
1741                                 File.SetCreationTime (string.Empty, new DateTime (2000, 12, 12, 11, 59, 59));
1742                                 Assert.Fail ("#1");
1743                         } catch (ArgumentException ex) {
1744                                 // Empty file name is not legal
1745                                 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
1746                                 Assert.IsNull (ex.InnerException, "#3");
1747                                 Assert.IsNotNull (ex.Message, "#4");
1748                                 Assert.IsNull (ex.ParamName, "#5");
1749                         }
1750                 }
1751
1752                 [Test]
1753                 public void SetCreationTime_Path_Whitespace ()
1754                 {
1755                         try {
1756                                 File.SetCreationTime ("     ", new DateTime (2000, 12, 12, 11, 59, 59));
1757                                 Assert.Fail ("#1");
1758                         } catch (ArgumentException ex) {
1759                                 // The path is not of a legal form
1760                                 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
1761                                 Assert.IsNull (ex.InnerException, "#3");
1762                                 Assert.IsNotNull (ex.Message, "#4");
1763                                 Assert.IsNull (ex.ParamName, "#5");
1764                         }
1765                 }
1766
1767                 [Test]
1768                 public void SetCreationTime_Path_InvalidPathChars ()
1769                 {
1770                         // On Unix there are no invalid path chars.
1771                         if (Path.InvalidPathChars.Length > 1) {
1772                                 try {
1773                                         File.SetCreationTime (Path.InvalidPathChars [1].ToString (),
1774                                                 new DateTime (2000, 12, 12, 11, 59, 59));
1775                                         Assert.Fail ("#1");
1776                                 } catch (ArgumentException ex) {
1777                                         // Illegal characters in path
1778                                         Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
1779                                         Assert.IsNull (ex.InnerException, "#3");
1780                                         Assert.IsNotNull (ex.Message, "#4");
1781                                         Assert.IsNull (ex.ParamName, "#5");
1782                                 }
1783                         }
1784                 }
1785
1786                 [Test]
1787                 public void SetCreationTime_Path_DoesNotExist ()
1788                 {
1789                         string path = TempFolder + Path.DirectorySeparatorChar + "SetCreationTimeFileNotFoundException1";
1790                         DeleteFile (path);
1791                         
1792                         try {
1793                                 File.SetCreationTime (path, new DateTime (2000, 12, 12, 11, 59, 59));
1794                                 Assert.Fail ("#1");
1795                         } catch (FileNotFoundException ex) {
1796                                 Assert.AreEqual (typeof (FileNotFoundException), ex.GetType (), "#2");
1797                                 Assert.AreEqual (path, ex.FileName, "#3");
1798                                 Assert.IsNull (ex.InnerException, "#4");
1799                                 Assert.IsNotNull (ex.Message, "#5");
1800                         }
1801                 }
1802
1803 //              [Test]
1804 //              [ExpectedException(typeof (ArgumentOutOfRangeException))]
1805 //              public void SetCreationTimeArgumentOutOfRangeException1 ()
1806 //              {
1807 //                      string path = TempFolder + Path.DirectorySeparatorChar + "SetCreationTimeArgumentOutOfRangeException1";
1808 //                      FileStream stream = null;
1809 //                      DeleteFile (path);
1810 //                      try {
1811 //                              stream = File.Create (path);
1812 //                              stream.Close ();
1813 //                              File.SetCreationTime (path, new DateTime (1000, 12, 12, 11, 59, 59));
1814 //                      } finally {
1815 //                              if (stream != null)
1816 //                                      stream.Close ();
1817 //                              DeleteFile (path);
1818 //                      }
1819 //              }
1820
1821                 [Test]
1822                 public void SetCreationTime_FileLock ()
1823                 {
1824                         string path = TempFolder + Path.DirectorySeparatorChar + "CreationTimeIOException1";
1825                         DeleteFile (path);
1826                         FileStream stream = null;
1827                         try {
1828                                 stream = File.Create (path);
1829                                 try {
1830                                         File.SetCreationTime (path, new DateTime (1000, 12, 12, 11, 59, 59));
1831                                         Assert.Fail ("#1");
1832                                 } catch (IOException ex) {
1833                                         // The process cannot access the file '...'
1834                                         // because it is being used by another process
1835                                         Assert.AreEqual (typeof (IOException), ex.GetType (), "#2");
1836                                         Assert.IsNull (ex.InnerException, "#3");
1837                                         Assert.IsNotNull (ex.Message, "#4");
1838                                         Assert.IsTrue (ex.Message.IndexOf (path) != -1, "#5");
1839                                 }
1840                         } finally {
1841                                 if (stream != null)
1842                                         stream.Close ();
1843                                 DeleteFile (path);
1844                         }
1845                 }
1846
1847                 [Test]
1848                 public void SetCreationTimeUtc_Path_Null ()
1849                 { 
1850                         try {
1851                                 File.SetCreationTimeUtc (null as string, new DateTime (2000, 12, 12, 11, 59, 59));
1852                                 Assert.Fail ("#1");
1853                         } catch (ArgumentNullException ex) {
1854                                 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
1855                                 Assert.IsNull (ex.InnerException, "#3");
1856                                 Assert.IsNotNull (ex.Message, "#4");
1857                                 Assert.AreEqual ("path", ex.ParamName, "#5");
1858                         }
1859                 }
1860
1861                 [Test]
1862                 public void SetCreationTimeUtc_Path_Empty ()
1863                 {
1864                         try {
1865                                 File.SetCreationTimeUtc (string.Empty, new DateTime (2000, 12, 12, 11, 59, 59));
1866                                 Assert.Fail ("#1");
1867                         } catch (ArgumentException ex) {
1868                                 // Empty file name is not legal
1869                                 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
1870                                 Assert.IsNull (ex.InnerException, "#3");
1871                                 Assert.IsNotNull (ex.Message, "#4");
1872                                 Assert.IsNull (ex.ParamName, "#5");
1873                         }
1874                 }
1875
1876                 [Test]
1877                 public void SetCreationTimeUtc_Path_Whitespace ()
1878                 {
1879                         try {
1880                                 File.SetCreationTimeUtc ("     ", new DateTime (2000, 12, 12, 11, 59, 59));
1881                                 Assert.Fail ("#1");
1882                         } catch (ArgumentException ex) {
1883                                 // The path is not of a legal form
1884                                 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
1885                                 Assert.IsNull (ex.InnerException, "#3");
1886                                 Assert.IsNotNull (ex.Message, "#4");
1887                                 Assert.IsNull (ex.ParamName, "#5");
1888                         }
1889                 }
1890
1891                 [Test]
1892                 public void SetCreationTimeUtc_Path_InvalidPathChars ()
1893                 {
1894                         // On Unix there are no invalid path chars.
1895                         if (Path.InvalidPathChars.Length > 1) {
1896                                 try {
1897                                         File.SetCreationTimeUtc (Path.InvalidPathChars [1].ToString (),
1898                                                 new DateTime (2000, 12, 12, 11, 59, 59));
1899                                         Assert.Fail ("#1");
1900                                 } catch (ArgumentException ex) {
1901                                         // Illegal characters in path
1902                                         Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
1903                                         Assert.IsNull (ex.InnerException, "#3");
1904                                         Assert.IsNotNull (ex.Message, "#4");
1905                                         Assert.IsNull (ex.ParamName, "#5");
1906                                 }
1907                         }
1908                 }
1909
1910                 [Test]
1911                 public void SetCreationTimeUtc_Path_DoesNotExist ()
1912                 {
1913                         string path = TempFolder + Path.DirectorySeparatorChar + "SetCreationTimeUtcFileNotFoundException1";
1914                         DeleteFile (path);
1915
1916                         try {
1917                                 File.SetCreationTimeUtc (path, new DateTime (2000, 12, 12, 11, 59, 59));
1918                                 Assert.Fail ("#1");
1919                         } catch (FileNotFoundException ex) {
1920                                 Assert.AreEqual (typeof (FileNotFoundException), ex.GetType (), "#2");
1921                                 Assert.AreEqual (path, ex.FileName, "#3");
1922                                 Assert.IsNull (ex.InnerException, "#3");
1923                                 Assert.IsNotNull (ex.Message, "#4");
1924                         }
1925                 }
1926
1927 //              [Test]
1928 //              [ExpectedException(typeof (ArgumentOutOfRangeException))]
1929 //              public void SetCreationTimeUtcArgumentOutOfRangeException1 ()
1930 //              {
1931 //                      string path = TempFolder + Path.DirectorySeparatorChar + "SetCreationTimeUtcArgumentOutOfRangeException1";
1932 //                      DeleteFile (path);
1933 //                      FileStream stream = null;
1934 //                      try {
1935 //                              stream = File.Create (path);
1936 //                              stream.Close ();
1937 //                              File.SetCreationTimeUtc (path, new DateTime (1000, 12, 12, 11, 59, 59));
1938 //                      } finally {
1939 //                              if (stream != null)
1940 //                                      stream.Close();
1941 //                              DeleteFile (path);
1942 //                      }
1943 //              }
1944
1945                 [Test]
1946                 public void SetCreationTimeUtc_FileLock ()
1947                 {
1948                         string path = TempFolder + Path.DirectorySeparatorChar + "SetCreationTimeUtcIOException1";
1949                         DeleteFile (path);
1950                         FileStream stream = null;
1951                         try {
1952                                 stream = File.Create (path);
1953                                 try {
1954                                         File.SetCreationTimeUtc (path, new DateTime (1000, 12, 12, 11, 59, 59));
1955                                         Assert.Fail ("#1");
1956                                 } catch (IOException ex) {
1957                                         // The process cannot access the file "..."
1958                                         // because it is being used by another process
1959                                         Assert.AreEqual (typeof (IOException), ex.GetType (), "#2");
1960                                         Assert.IsNull (ex.InnerException, "#3");
1961                                         Assert.IsNotNull (ex.Message, "#4");
1962                                         Assert.IsTrue (ex.Message.IndexOf (path) != -1, "#5");
1963                                 }
1964                         } finally {
1965                                 if (stream != null)
1966                                         stream.Close ();
1967                                 DeleteFile (path);
1968                         }
1969                 }
1970
1971                 // SetLastAccessTime and SetLastAccessTimeUtc exceptions
1972
1973                 [Test]
1974                 public void SetLastAccessTime_Path_Null ()
1975                 {
1976                         try {
1977                                 File.SetLastAccessTime (null as string, new DateTime (2000, 12, 12, 11, 59, 59));
1978                                 Assert.Fail ("#1");
1979                         } catch (ArgumentNullException ex) {
1980                                 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
1981                                 Assert.IsNull (ex.InnerException, "#3");
1982                                 Assert.IsNotNull (ex.Message, "#4");
1983                                 Assert.AreEqual ("path", ex.ParamName, "#5");
1984                         }
1985                 }
1986
1987                 [Test]
1988                 public void SetLastAccessTime_Path_Empty ()
1989                 {
1990                         try {
1991                                 File.SetLastAccessTime (string.Empty, new DateTime (2000, 12, 12, 11, 59, 59));
1992                                 Assert.Fail ("#1");
1993                         } catch (ArgumentException ex) {
1994                                 // Empty file name is not legal
1995                                 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
1996                                 Assert.IsNull (ex.InnerException, "#3");
1997                                 Assert.IsNotNull (ex.Message, "#4");
1998                                 Assert.IsNull (ex.ParamName, "#5");
1999                         }
2000                 }
2001
2002                 [Test]
2003                 public void SetLastAccessTime_Path_Whitespace ()
2004                 {
2005                         try {
2006                                 File.SetLastAccessTime ("     ", new DateTime (2000, 12, 12, 11, 59, 59));
2007                                 Assert.Fail ("#1");
2008                         } catch (ArgumentException ex) {
2009                                 // The path is not of a legal form
2010                                 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
2011                                 Assert.IsNull (ex.InnerException, "#3");
2012                                 Assert.IsNotNull (ex.Message, "#4");
2013                                 Assert.IsNull (ex.ParamName, "#5");
2014                         }
2015                 }
2016
2017                 [Test]
2018                 public void SetLastAccessTime_Path_InvalidPathChars ()
2019                 {
2020                         // On Unix there are no invalid path chars.
2021                         if (Path.InvalidPathChars.Length > 1) {
2022                                 try {
2023                                         File.SetLastAccessTime (Path.InvalidPathChars [1].ToString (),
2024                                                 new DateTime (2000, 12, 12, 11, 59, 59));
2025                                         Assert.Fail ("#1");
2026                                 } catch (ArgumentException ex) {
2027                                         // Illegal characters in path
2028                                         Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
2029                                         Assert.IsNull (ex.InnerException, "#3");
2030                                         Assert.IsNotNull (ex.Message, "#4");
2031                                         Assert.IsNull (ex.ParamName, "#5");
2032                                 }
2033                         }
2034                 }
2035
2036                 [Test]
2037                 public void SetLastAccessTime_Path_DoesNotExist ()
2038                 {
2039                         string path = TempFolder + Path.DirectorySeparatorChar + "SetLastAccessTimeFileNotFoundException1";
2040                         DeleteFile (path);
2041
2042                         try {
2043                                 File.SetLastAccessTime (path, new DateTime (2000, 12, 12, 11, 59, 59));
2044                                 Assert.Fail ("#1");
2045                         } catch (FileNotFoundException ex) {
2046                                 Assert.AreEqual (typeof (FileNotFoundException), ex.GetType (), "#2");
2047                                 Assert.AreEqual (path, ex.FileName, "#3");
2048                                 Assert.IsNull (ex.InnerException, "#4");
2049                                 Assert.IsNotNull (ex.Message, "#5");
2050                         }
2051                 }
2052
2053 //              [Test]
2054 //              [ExpectedException(typeof (ArgumentOutOfRangeException))]
2055 //              public void SetLastAccessTimeArgumentOutOfRangeException1 ()
2056 //              {
2057 //                      string path = TempFolder + Path.DirectorySeparatorChar + "SetLastTimeArgumentOutOfRangeException1";
2058 //                      DeleteFile (path);
2059 //                      FileStream stream = null;
2060 //                      try {
2061 //                              stream = File.Create (path);
2062 //                              stream.Close ();
2063 //                              File.SetLastAccessTime (path, new DateTime (1000, 12, 12, 11, 59, 59));
2064 //                      } finally {
2065 //                              if (stream != null)
2066 //                                      stream.Close ();
2067 //                              DeleteFile (path);
2068 //                      }
2069 //              }
2070
2071                 [Test]
2072                 public void SetLastAccessTime_FileLock ()
2073                 {
2074                         string path = TempFolder + Path.DirectorySeparatorChar + "LastAccessIOException1";
2075                         DeleteFile (path);
2076                         FileStream stream = null;
2077                         try {
2078                                 stream = File.Create (path);
2079                                 try {
2080                                         File.SetLastAccessTime (path, new DateTime (1000, 12, 12, 11, 59, 59));
2081                                         Assert.Fail ("#1");
2082                                 } catch (IOException ex) {
2083                                         // The process cannot access the file "..."
2084                                         // because it is being used by another process
2085                                         Assert.AreEqual (typeof (IOException), ex.GetType (), "#2");
2086                                         Assert.IsNull (ex.InnerException, "#3");
2087                                         Assert.IsNotNull (ex.Message, "#4");
2088                                         Assert.IsTrue (ex.Message.IndexOf (path) != -1, "#5");
2089                                 }
2090                         } finally {
2091                                 if (stream != null)
2092                                         stream.Close ();
2093                                 DeleteFile (path);
2094                         }
2095                 }
2096
2097                 [Test]
2098                 public void SetLastAccessTimeUtc_Path_Null ()
2099                 {
2100                         try {
2101                                 File.SetLastAccessTimeUtc (null as string, new DateTime (2000, 12, 12, 11, 59, 59));
2102                                 Assert.Fail ("#1");
2103                         } catch (ArgumentNullException ex) {
2104                                 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
2105                                 Assert.IsNull (ex.InnerException, "#3");
2106                                 Assert.IsNotNull (ex.Message, "#4");
2107                                 Assert.AreEqual ("path", ex.ParamName, "#5");
2108                         }
2109                 }
2110
2111                 [Test]
2112                 public void SetCLastAccessTimeUtc_Path_Empty ()
2113                 {
2114                         try {
2115                                 File.SetLastAccessTimeUtc (string.Empty, new DateTime (2000, 12, 12, 11, 59, 59));
2116                                 Assert.Fail ("#1");
2117                         } catch (ArgumentException ex) {
2118                                 // Empty file name is not legal
2119                                 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
2120                                 Assert.IsNull (ex.InnerException, "#3");
2121                                 Assert.IsNotNull (ex.Message, "#4");
2122                                 Assert.IsNull (ex.ParamName, "#5");
2123                         }
2124                 }
2125
2126                 [Test]
2127                 public void SetLastAccessTimeUtc_Path_Whitespace ()
2128                 {
2129                         try {
2130                                 File.SetLastAccessTimeUtc ("     ", new DateTime (2000, 12, 12, 11, 59, 59));
2131                                 Assert.Fail ("#1");
2132                         } catch (ArgumentException ex) {
2133                                 // The path is not of a legal form
2134                                 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
2135                                 Assert.IsNull (ex.InnerException, "#3");
2136                                 Assert.IsNotNull (ex.Message, "#4");
2137                                 Assert.IsNull (ex.ParamName, "#5");
2138                         }
2139                 }
2140
2141                 [Test]
2142                 public void SetLastAccessTimeUtc_Path_InvalidPathChars ()
2143                 {
2144                         // On Unix there are no invalid path chars.
2145                         if (Path.InvalidPathChars.Length > 1) {
2146                                 try {
2147                                         File.SetLastAccessTimeUtc (Path.InvalidPathChars [1].ToString (),
2148                                                 new DateTime (2000, 12, 12, 11, 59, 59));
2149                                         Assert.Fail ("#1");
2150                                 } catch (ArgumentException ex) {
2151                                         // Illegal characters in path
2152                                         Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
2153                                         Assert.IsNull (ex.InnerException, "#3");
2154                                         Assert.IsNotNull (ex.Message, "#4");
2155                                         Assert.IsNull (ex.ParamName, "#5");
2156                                 }
2157                         }
2158                 }
2159
2160                 [Test]
2161                 public void SetLastAccessTimeUtc_Path_DoesNotExist ()
2162                 {
2163                         string path = TempFolder + Path.DirectorySeparatorChar + "SetLastAccessTimeUtcFileNotFoundException1";
2164                         DeleteFile (path);
2165
2166                         try {
2167                                 File.SetLastAccessTimeUtc (path, new DateTime (2000, 12, 12, 11, 59, 59));
2168                                 Assert.Fail ("#1");
2169                         } catch (FileNotFoundException ex) {
2170                                 Assert.AreEqual (typeof (FileNotFoundException), ex.GetType (), "#2");
2171                                 Assert.AreEqual (path, ex.FileName, "#3");
2172                                 Assert.IsNull (ex.InnerException, "#4");
2173                                 Assert.IsNotNull (ex.Message, "#5");
2174                         }
2175                 }
2176
2177 //              [Test]
2178 //              [ExpectedException(typeof (ArgumentOutOfRangeException))]
2179 //              public void SetLastAccessTimeUtcArgumentOutOfRangeException1 ()
2180 //              {
2181 //                      string path = TempFolder + Path.DirectorySeparatorChar + "SetLastAccessTimeUtcArgumentOutOfRangeException1";
2182 //                      DeleteFile (path);
2183 //                      FileStream stream = null;
2184 //                      try {
2185 //                              stream = File.Create (path);
2186 //                              stream.Close ();
2187 //                              File.SetLastAccessTimeUtc (path, new DateTime (1000, 12, 12, 11, 59, 59));
2188 //                      } finally {
2189 //                              if (stream != null)
2190 //                                      stream.Close ();
2191 //                              DeleteFile (path);
2192 //                      }
2193 //              }
2194
2195                 [Test]
2196                 public void SetLastAccessTimeUtc_FileLock ()
2197                 {
2198                         string path = TempFolder + Path.DirectorySeparatorChar + "SetLastAccessTimeUtcIOException1";
2199                         DeleteFile (path);
2200                         FileStream stream = null;
2201                         try {
2202                                 stream = File.Create (path);
2203                                 try {
2204                                         File.SetLastAccessTimeUtc (path, new DateTime (1000, 12, 12, 11, 59, 59));
2205                                         Assert.Fail ("#1");
2206                                 } catch (IOException ex) {
2207                                         // The process cannot access the file "..."
2208                                         // because it is being used by another process
2209                                         Assert.AreEqual (typeof (IOException), ex.GetType (), "#2");
2210                                         Assert.IsNull (ex.InnerException, "#3");
2211                                         Assert.IsNotNull (ex.Message, "#4");
2212                                         Assert.IsTrue (ex.Message.IndexOf (path) != -1, "#5");
2213                                 }
2214                         } finally {
2215                                 if (stream != null)
2216                                         stream.Close ();
2217                                 DeleteFile (path);
2218                         }
2219                 }
2220
2221                 // SetLastWriteTime and SetLastWriteTimeUtc exceptions
2222
2223                 [Test]
2224                 public void SetLastWriteTime_Path_Null ()
2225                 {
2226                         try {
2227                                 File.SetLastWriteTime (null as string, new DateTime (2000, 12, 12, 11, 59, 59));
2228                                 Assert.Fail ("#1");
2229                         } catch (ArgumentNullException ex) {
2230                                 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
2231                                 Assert.IsNull (ex.InnerException, "#3");
2232                                 Assert.IsNotNull (ex.Message, "#4");
2233                                 Assert.AreEqual ("path", ex.ParamName, "#5");
2234                         }
2235                 }
2236
2237                 [Test]
2238                 public void SetLastWriteTime_Path_Empty ()
2239                 {
2240                         try {
2241                                 File.SetLastWriteTime (string.Empty, new DateTime (2000, 12, 12, 11, 59, 59));
2242                                 Assert.Fail ("#1");
2243                         } catch (ArgumentException ex) {
2244                                 // Empty file name is not legal
2245                                 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
2246                                 Assert.IsNull (ex.InnerException, "#3");
2247                                 Assert.IsNotNull (ex.Message, "#4");
2248                                 Assert.IsNull (ex.ParamName, "#5");
2249                         }
2250                 }
2251
2252                 [Test]
2253                 public void SetLastWriteTime_Path_Whitespace ()
2254                 {
2255                         try {
2256                                 File.SetLastWriteTime ("     ", new DateTime (2000, 12, 12, 11, 59, 59));
2257                                 Assert.Fail ("#1");
2258                         } catch (ArgumentException ex) {
2259                                 // The path is not of a legal form
2260                                 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
2261                                 Assert.IsNull (ex.InnerException, "#3");
2262                                 Assert.IsNotNull (ex.Message, "#4");
2263                                 Assert.IsNull (ex.ParamName, "#5");
2264                         }
2265                 }
2266
2267                 [Test]
2268                 public void SetLastWriteTime_Path_InvalidPathChars ()
2269                 {
2270                         // On Unix there are no invalid path chars.
2271                         if (Path.InvalidPathChars.Length > 1) {
2272                                 try {
2273                                         File.SetLastWriteTime (Path.InvalidPathChars [1].ToString (),
2274                                                 new DateTime (2000, 12, 12, 11, 59, 59));
2275                                         Assert.Fail ("#1");
2276                                 } catch (ArgumentException ex) {
2277                                         // Illegal characters in path
2278                                         Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
2279                                         Assert.IsNull (ex.InnerException, "#3");
2280                                         Assert.IsNotNull (ex.Message, "#4");
2281                                         Assert.IsNull (ex.ParamName, "#5");
2282                                 }
2283                         }
2284                 }
2285
2286                 [Test]
2287                 public void SetLastWriteTime_Path_DoesNotExist ()
2288                 {
2289                         string path = TempFolder + Path.DirectorySeparatorChar + "SetLastWriteTimeFileNotFoundException1";
2290                         DeleteFile (path);
2291
2292                         try {
2293                                 File.SetLastWriteTime (path, new DateTime (2000, 12, 12, 11, 59, 59));
2294                                 Assert.Fail ("#1");
2295                         } catch (FileNotFoundException ex) {
2296                                 Assert.AreEqual (typeof (FileNotFoundException), ex.GetType (), "#2");
2297                                 Assert.AreEqual (path, ex.FileName, "#3");
2298                                 Assert.IsNull (ex.InnerException, "#4");
2299                                 Assert.IsNotNull (ex.Message, "#5");
2300                         }
2301                 }
2302
2303 //              [Test]
2304 //              [ExpectedException(typeof (ArgumentOutOfRangeException))]
2305 //              public void SetLastWriteTimeArgumentOutOfRangeException1 ()
2306 //              {
2307 //                      string path = TempFolder + Path.DirectorySeparatorChar + "SetLastWriteTimeArgumentOutOfRangeException1";
2308 //                      DeleteFile (path);
2309 //                      FileStream stream = null;
2310 //                      try {
2311 //                              stream = File.Create (path);
2312 //                              stream.Close ();
2313 //                              File.SetLastWriteTime (path, new DateTime (1000, 12, 12, 11, 59, 59));
2314 //                      } finally {
2315 //                              if (stream != null)
2316 //                                      stream.Close ();
2317 //                              DeleteFile (path);
2318 //                      }
2319 //              }
2320
2321                 [Test]
2322                 public void SetLastWriteTime_FileLock ()
2323                 {
2324                         string path = TempFolder + Path.DirectorySeparatorChar + "LastWriteTimeIOException1";
2325                         DeleteFile (path);
2326                         FileStream stream = null;
2327                         try {
2328                                 stream = File.Create (path);
2329                                 try {
2330                                         File.SetLastWriteTime (path, new DateTime (1000, 12, 12, 11, 59, 59));
2331                                         Assert.Fail ("#1");
2332                                 } catch (IOException ex) {
2333                                         // The process cannot access the file '...'
2334                                         // because it is being used by another process
2335                                         Assert.AreEqual (typeof (IOException), ex.GetType (), "#2");
2336                                         Assert.IsNull (ex.InnerException, "#3");
2337                                         Assert.IsNotNull (ex.Message, "#4");
2338                                         Assert.IsTrue (ex.Message.IndexOf (path) != -1, "#5");
2339                                 }
2340                         } finally {
2341                                 if (stream != null)
2342                                         stream.Close ();
2343                                 DeleteFile (path);
2344                         }
2345                 }
2346
2347                 [Test]
2348                 public void SetLastWriteTimeUtc_Path_Null ()
2349                 {
2350                         try {
2351                                 File.SetLastWriteTimeUtc (null as string, new DateTime (2000, 12, 12, 11, 59, 59));
2352                                 Assert.Fail ("#1");
2353                         } catch (ArgumentNullException ex) {
2354                                 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
2355                                 Assert.IsNull (ex.InnerException, "#3");
2356                                 Assert.IsNotNull (ex.Message, "#4");
2357                                 Assert.AreEqual ("path", ex.ParamName, "#5");
2358                         }
2359                 }
2360
2361                 [Test]
2362                 public void SetLastWriteTimeUtc_Path_Empty ()
2363                 {
2364                         try {
2365                                 File.SetLastWriteTimeUtc (string.Empty, new DateTime (2000, 12, 12, 11, 59, 59));
2366                                 Assert.Fail ("#1");
2367                         } catch (ArgumentException ex) {
2368                                 // Empty file name is not legal
2369                                 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
2370                                 Assert.IsNull (ex.InnerException, "#3");
2371                                 Assert.IsNotNull (ex.Message, "#4");
2372                                 Assert.IsNull (ex.ParamName, "#5");
2373                         }
2374                 }
2375
2376                 [Test]
2377                 public void SetLastWriteTimeUtc_Path_Whitespace ()
2378                 {
2379                         try {
2380                                 File.SetLastWriteTimeUtc ("     ", new DateTime (2000, 12, 12, 11, 59, 59));
2381                                 Assert.Fail ("#1");
2382                         } catch (ArgumentException ex) {
2383                                 // The path is not of a legal form
2384                                 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
2385                                 Assert.IsNull (ex.InnerException, "#3");
2386                                 Assert.IsNotNull (ex.Message, "#4");
2387                                 Assert.IsNull (ex.ParamName, "#5");
2388                         }
2389                 }
2390
2391                 [Test]
2392                 public void SetLastWriteTimeUtc_Path_InvalidPathChars ()
2393                 {
2394                         // On Unix there are no invalid path chars.
2395                         if (Path.InvalidPathChars.Length > 1) {
2396                                 try {
2397                                         File.SetLastWriteTimeUtc (Path.InvalidPathChars [1].ToString (),
2398                                                 new DateTime (2000, 12, 12, 11, 59, 59));
2399                                         Assert.Fail ("#1");
2400                                 } catch (ArgumentException ex) {
2401                                         // Illegal characters in path
2402                                         Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
2403                                         Assert.IsNull (ex.InnerException, "#3");
2404                                         Assert.IsNotNull (ex.Message, "#4");
2405                                         Assert.IsNull (ex.ParamName, "#5");
2406                                 }
2407                         }
2408                 }
2409
2410                 [Test]
2411                 public void SetLastWriteTimeUtc_Path_DoesNotExist ()
2412                 {
2413                         string path = TempFolder + Path.DirectorySeparatorChar + "SetLastWriteTimeUtcFileNotFoundException1";
2414                         DeleteFile (path);
2415
2416                         try {
2417                                 File.SetLastWriteTimeUtc (path, new DateTime (2000, 12, 12, 11, 59, 59));
2418                                 Assert.Fail ("#1");
2419                         } catch (FileNotFoundException ex) {
2420                                 Assert.AreEqual (typeof (FileNotFoundException), ex.GetType (), "#2");
2421                                 Assert.AreEqual (path, ex.FileName, "#3");
2422                                 Assert.IsNull (ex.InnerException, "#4");
2423                                 Assert.IsNotNull (ex.Message, "#5");
2424                         }
2425                 }
2426
2427 //              [Test]
2428 //              [ExpectedException(typeof (ArgumentOutOfRangeException))]
2429 //              public void SetLastWriteTimeUtcArgumentOutOfRangeException1 ()
2430 //              {
2431 //                      string path = TempFolder + Path.DirectorySeparatorChar + "SetLastWriteTimeUtcArgumentOutOfRangeException1";
2432 //                      DeleteFile (path);
2433 //                      FileStream stream = null;
2434 //                      try {
2435 //                              stream = File.Create (path);
2436 //                              stream.Close ();
2437 //                              File.SetLastWriteTimeUtc (path, new DateTime (1000, 12, 12, 11, 59, 59));
2438 //                      } finally {
2439 //                              if (stream != null)
2440 //                                      stream.Close ();
2441 //                              DeleteFile (path);
2442 //                      }
2443 //              }
2444
2445                 [Test]
2446                 public void SetLastWriteTimeUtc_FileLock ()
2447                 {
2448                         string path = TempFolder + Path.DirectorySeparatorChar + "SetLastWriteTimeUtcIOException1";
2449                         DeleteFile (path);
2450                         FileStream stream = null;
2451                         try {
2452                                 stream = File.Create (path);
2453                                 try {
2454                                         File.SetLastWriteTimeUtc (path, new DateTime (1000, 12, 12, 11, 59, 59));
2455                                         Assert.Fail ("#1");
2456                                 } catch (IOException ex) {
2457                                         // The process cannot access the file '...'
2458                                         // because it is being used by another process
2459                                         Assert.AreEqual (typeof (IOException), ex.GetType (), "#2");
2460                                         Assert.IsNull (ex.InnerException, "#3");
2461                                         Assert.IsNotNull (ex.Message, "#4");
2462                                         Assert.IsTrue (ex.Message.IndexOf (path) != -1, "#5");
2463                                 }
2464                         } finally {
2465                                 if (stream != null)
2466                                         stream.Close ();
2467                                 DeleteFile (path);
2468                         }
2469                 }
2470                 
2471                 [Test]
2472                 public void OpenAppend ()
2473                 {
2474                         string fn = Path.GetTempFileName ();
2475                         using (FileStream s = File.Open (fn, FileMode.Append)) {
2476                         }
2477                         DeleteFile (fn);
2478                 }
2479
2480                         void Position (long value)
2481                 {
2482                         using (FileStream fs = File.OpenRead (testfile)) {
2483                                 fs.Position = value;
2484                                 Assert.AreEqual (value, fs.Position, "Position");
2485                                 Assert.AreEqual (1, fs.Length, "Length");
2486                         }
2487                 }
2488                 
2489                 [Test]
2490                 public void Position_Small ()
2491                 {
2492                         Position (Int32.MaxValue);
2493                 }
2494
2495                 [Test]
2496                 [Category ("LargeFileSupport")]
2497                 public void Position_Large ()
2498                 {
2499                         // fails if HAVE_LARGE_FILE_SUPPORT is not enabled in device builds
2500                         Position ((long) Int32.MaxValue + 1);
2501                 }
2502                 
2503                 void Seek (long value)
2504                 {
2505                         using (FileStream fs = File.OpenRead (testfile)) {
2506                                 fs.Seek (value, SeekOrigin.Begin);
2507                                 Assert.AreEqual (value, fs.Position, "Position");
2508                                 Assert.AreEqual (1, fs.Length, "Length");
2509                         }
2510                 }
2511                 
2512                 [Test]
2513                 public void Seek_Small ()
2514                 {
2515                         Seek (Int32.MaxValue);
2516                 }
2517
2518                 [Test]
2519                 [Category ("LargeFileSupport")]
2520                 public void Seek_Large ()
2521                 {
2522                         // fails if HAVE_LARGE_FILE_SUPPORT is not enabled in device builds
2523                         Seek ((long) Int32.MaxValue + 1);
2524                 }
2525                 
2526                 void LockUnlock (long value)
2527                 {
2528                         using (FileStream fs = new FileStream (testfile, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.ReadWrite)) {
2529                                 fs.Lock (value - 1, 1);
2530                                 fs.Unlock (value - 1, 1);
2531                                 
2532                                 fs.Lock (0, value);
2533                                 fs.Unlock (0, value);
2534                         }
2535                 }
2536                 
2537                 [Test]
2538                 public void Lock_Small ()
2539                 {
2540                         LockUnlock ((long) Int32.MaxValue);
2541                 }
2542
2543                 [Test]
2544                 [Category ("LargeFileSupport")]
2545                 public void Lock_Large ()
2546                 {
2547                         // note: already worked without HAVE_LARGE_FILE_SUPPORT
2548                         LockUnlock ((long) Int32.MaxValue + 1);
2549                 }
2550         
2551                 [Test]
2552                 public void ReadWriteAllText ()
2553                 {
2554                         // The MSDN docs said something about
2555                         // not including a final new line. it looks
2556                         // like that was not true. I'm not sure what
2557                         // that was talking about
2558                         read_all (string.Empty);
2559                         read_all ("\r");
2560                         read_all ("\n");
2561                         read_all ("\r\n");
2562                         read_all ("a\r");
2563                         read_all ("a\n");
2564                         read_all ("a\r\n");
2565                         read_all ("a\ra");
2566                         read_all ("a\na");
2567                         read_all ("a\r\na");
2568                         read_all ("a");
2569                         read_all ("\r\r");
2570                         read_all ("\n\n");
2571                         read_all ("\r\n\r\n");
2572                 }
2573
2574                 [Test]
2575                 public void ReplaceTest ()
2576                 {
2577                         string tmp = Path.Combine (TempFolder, "ReplaceTest");
2578                         Directory.CreateDirectory (tmp);
2579                         string origFile = Path.Combine (tmp, "origFile");
2580                         string replaceFile = Path.Combine (tmp, "replaceFile");
2581                         string backupFile = Path.Combine (tmp, "backupFile");
2582
2583                         using (StreamWriter sw = File.CreateText (origFile)) {
2584                                 sw.WriteLine ("origFile");
2585                         }
2586                         using (StreamWriter sw = File.CreateText (replaceFile)) {
2587                                 sw.WriteLine ("replaceFile");
2588                         }
2589                         using (StreamWriter sw = File.CreateText (backupFile)) {
2590                                 sw.WriteLine ("backupFile");
2591                         }
2592
2593                         File.Replace (origFile, replaceFile, backupFile);
2594                         Assert.IsFalse (File.Exists (origFile), "#1");
2595                         using (StreamReader sr = File.OpenText (replaceFile)) {
2596                                 string txt = sr.ReadLine ();
2597                                 Assert.AreEqual ("origFile", txt, "#2");
2598                         }
2599                         using (StreamReader sr = File.OpenText (backupFile)) {
2600                                 string txt = sr.ReadLine ();
2601                                 Assert.AreEqual ("replaceFile", txt, "#3");
2602                         }
2603                 }
2604
2605                 static bool RunningOnUnix {
2606                         get {
2607                                 int p = (int) Environment.OSVersion.Platform;
2608                                 return ((p == 4) || (p == 128) || (p == 6));
2609                         }
2610                 }
2611
2612                 void DeleteFile (string path)
2613                 {
2614                         if (File.Exists (path))
2615                                 File.Delete (path);
2616                 }
2617
2618                 void DeleteDirectory (string path)
2619                 {
2620                         if (Directory.Exists (path))
2621                                 Directory.Delete (path, true);
2622                 }
2623
2624                 void read_all (string s)
2625                 {
2626                         string f = Path.GetTempFileName ();
2627                         try {
2628                                 File.WriteAllText (f, s);
2629                                 string r = File.ReadAllText (f);
2630                                 Assert.AreEqual (s, r);
2631                         } finally {
2632                                 DeleteFile (f);
2633                         }
2634                 }
2635
2636                 void MoveTest (FileAccess acc, FileShare share, bool works)
2637                 {
2638                         // use TEMP so since the default location (right along with the assemblies) 
2639                         // will get access denied when running under some environment (e.g. iOS devices)
2640                         var file = Path.Combine (Path.GetTempPath (), "kk597rfdnllh89");
2641
2642                         File.Delete (file + ".old");
2643                         using (var v = File.Create (file)) { }
2644
2645                         using (var stream = new FileStream(file, FileMode.Open, acc, share, 4096, FileOptions.SequentialScan)) {
2646                                 try {
2647                                         File.Move(file, file + ".old");
2648                                         if (!works)
2649                                                 Assert.Fail ("Move with ({0}) and  ({1}) did not fail", acc, share);
2650                                 } catch (IOException) {
2651                                         if (works)
2652                                                 Assert.Fail ("Move with ({0}) and  ({1}) did fail", acc, share);
2653                                 }
2654                         }
2655                 }
2656
2657                 [Test]
2658                 public void MoveTest ()
2659                 {
2660                         MoveTest (FileAccess.Read, FileShare.None, false);
2661                         MoveTest (FileAccess.Read, FileShare.Read, false);
2662                         MoveTest (FileAccess.Read, FileShare.Write, false);
2663                         MoveTest (FileAccess.Read, FileShare.ReadWrite, false);
2664                         MoveTest (FileAccess.Read, FileShare.Delete, true);
2665                         MoveTest (FileAccess.Read, FileShare.Read | FileShare.Delete, true);
2666                         MoveTest (FileAccess.Read, FileShare.Write | FileShare.Delete, true);
2667                         MoveTest (FileAccess.Read, FileShare.ReadWrite | FileShare.Delete, true);
2668                         MoveTest (FileAccess.Write, FileShare.None, false);
2669                         MoveTest (FileAccess.Write, FileShare.Read, false);
2670                         MoveTest (FileAccess.Write, FileShare.Write, false);
2671                         MoveTest (FileAccess.Write, FileShare.ReadWrite, false);
2672                         MoveTest (FileAccess.Write, FileShare.Delete, true);
2673                         MoveTest (FileAccess.Write, FileShare.Read | FileShare.Delete, true);
2674                         MoveTest (FileAccess.Write, FileShare.Write | FileShare.Delete, true);
2675                         MoveTest (FileAccess.Write, FileShare.ReadWrite | FileShare.Delete, true);
2676                         MoveTest (FileAccess.ReadWrite, FileShare.None, false);
2677                         MoveTest (FileAccess.ReadWrite, FileShare.Read, false);
2678                         MoveTest (FileAccess.ReadWrite, FileShare.Write, false);
2679                         MoveTest (FileAccess.ReadWrite, FileShare.ReadWrite, false);
2680                         MoveTest (FileAccess.ReadWrite, FileShare.Delete, true);
2681                         MoveTest (FileAccess.ReadWrite, FileShare.Read | FileShare.Delete, true);
2682                         MoveTest (FileAccess.ReadWrite, FileShare.Write | FileShare.Delete, true);
2683                         MoveTest (FileAccess.ReadWrite, FileShare.ReadWrite | FileShare.Delete, true);
2684                 }
2685
2686
2687                 [DllImport ("libc", SetLastError=true)]
2688                 public static extern int symlink (string oldpath, string newpath);
2689
2690                 [Test]
2691                 public void SymLinkLoop ()
2692                 {
2693                         if (!RunningOnUnix)
2694                                 Assert.Ignore ("Symlink are hard on windows");
2695
2696                         var name1 = Path.GetRandomFileName ();
2697                         var name2 = Path.GetRandomFileName ();
2698
2699                         var path1 = Path.Combine (Path.GetTempPath (), name1);
2700                         var path2 = Path.Combine (Path.GetTempPath (), name2);
2701
2702                         File.Delete (path1);
2703                         File.Delete (path2);
2704
2705                         try {
2706                                 symlink (path1, path2);
2707                                 symlink (path2, path1);
2708
2709                                 Assert.IsTrue (File.Exists (path1), "File.Exists must return true for path1 symlink loop");
2710                                 Assert.IsTrue (File.Exists (path2), "File.Exists must return true for path2 symlink loop");
2711
2712                                 try {
2713                                         using (var f = File.Open (path1, FileMode.Open, FileAccess.Read)) {
2714                                                 Assert.Fail ("File.Open must throw for symlink loops");
2715                                         }
2716                                 } catch (IOException ex) {
2717                                         Assert.AreEqual (0x80070781u, (uint)ex.HResult, "Ensure HRESULT is correct");
2718                                 }
2719
2720                                 File.Delete (path1); //Delete must not throw and must work
2721                                 Assert.IsFalse (File.Exists (path1), "File.Delete must delete symlink loops");
2722
2723                         } finally {
2724                                 try {
2725                                         File.Delete (path1);
2726                                         File.Delete (path2);
2727                                 } catch (IOException) {
2728                                         //Don't double fault any exception from the tests.
2729                                 }
2730
2731                         }
2732                 }
2733         }
2734 }