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