Adjust corlib unit tests to reduce failures when executed on the NET_2_1 profile
[mono.git] / mcs / class / corlib / Test / System.IO.IsolatedStorage / IsolatedStorageFileTest.cs
1 //
2 // IsolatedStorageFileTest.cs 
3 //      - Unit Tests for abstract IsolatedStorageFile class
4 //
5 // Author:
6 //      Sebastien Pouliot  <sebastien@ximian.com>
7 //
8 // Copyright (C) 2005 Novell Inc. (http://www.novell.com)
9 //
10 // Permission is hereby granted, free of charge, to any person obtaining
11 // a copy of this software and associated documentation files (the
12 // "Software"), to deal in the Software without restriction, including
13 // without limitation the rights to use, copy, modify, merge, publish,
14 // distribute, sublicense, and/or sell copies of the Software, and to
15 // permit persons to whom the Software is furnished to do so, subject to
16 // the following conditions:
17 // 
18 // The above copyright notice and this permission notice shall be
19 // included in all copies or substantial portions of the Software.
20 // 
21 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
22 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
23 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
24 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
25 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
26 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
27 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28 //
29
30 using System;
31 using System.Collections;
32 using System.IO;
33 using System.IO.IsolatedStorage;
34 using System.Reflection;
35 using System.Security;
36 using System.Security.Permissions;
37 using System.Security.Policy;
38
39 using NUnit.Framework;
40
41 namespace MonoTests.System.IO.IsolatedStorageTest {
42
43         [TestFixture]
44         public class IsolatedStorageFileTest {
45
46                 private const string dirname = "mono-unit-test";
47
48                 private void CheckEnumerated (int n, IsolatedStorageScope scope, IsolatedStorageFile isf)
49                 {
50                         string prefix = n.ToString () + " - " + scope.ToString () + " - ";
51                         Assert.IsNotNull (isf, prefix + "IsolatedStorageFile");
52                         Assert.IsTrue (((scope & isf.Scope) != 0), prefix + "Scope");
53
54                         if ((isf.Scope & IsolatedStorageScope.Assembly) != 0)
55                                 Assert.IsNotNull (isf.AssemblyIdentity, prefix + "AssemblyIdentity");
56                         if ((isf.Scope & IsolatedStorageScope.Domain) != 0)
57                                 Assert.IsNotNull (isf.DomainIdentity, prefix + "DomainIdentity");
58 #if NET_2_0
59                         if ((isf.Scope & IsolatedStorageScope.Application) != 0)
60                                 Assert.IsNotNull (isf.ApplicationIdentity, prefix + "ApplicationIdentity");
61 #endif
62                 }
63
64                 private void GetEnumerator (IsolatedStorageScope scope)
65                 {
66                         IEnumerator e = IsolatedStorageFile.GetEnumerator (scope);
67                         int n = 0;
68                         while (e.MoveNext ())
69                         {
70                                 IsolatedStorageFile isf = (IsolatedStorageFile)e.Current;
71                                 CheckEnumerated (++n, scope, isf);
72                         }
73                 }
74
75                 [Test]
76                 public void GetEnumerator_User ()
77                 {
78                         GetEnumerator (IsolatedStorageScope.User);
79                 }
80
81                 [Test]
82                 [ExpectedException (typeof (ArgumentException))]
83                 public void GetEnumerator_User_Details ()
84                 {
85                         // giving more details is bad
86                         GetEnumerator (IsolatedStorageScope.User | IsolatedStorageScope.Assembly | IsolatedStorageScope.Domain);
87                 }
88
89                 [Test]
90                 public void GetEnumerator_UserRoaming ()
91                 {
92                         GetEnumerator (IsolatedStorageScope.User | IsolatedStorageScope.Roaming);
93                 }
94
95                 [Test]
96                 [ExpectedException (typeof (ArgumentException))]
97                 public void GetEnumerator_UserRoaming_Details ()
98                 {
99                         // giving more details is bad
100                         GetEnumerator (IsolatedStorageScope.User | IsolatedStorageScope.Assembly | IsolatedStorageScope.Domain | IsolatedStorageScope.Roaming);
101                 }
102 #if NET_2_0
103                 [Test]
104                 public void GetEnumerator_Machine ()
105                 {
106                         GetEnumerator (IsolatedStorageScope.Machine);
107                 }
108
109                 [Test]
110                 [ExpectedException (typeof (ArgumentException))]
111                 public void GetEnumerator_Machine_Details ()
112                 {
113                         GetEnumerator (IsolatedStorageScope.Machine | IsolatedStorageScope.Assembly);
114                 }
115
116                 [Test]
117                 [ExpectedException (typeof (ArgumentException))]
118                 public void GetEnumerator_Application ()
119                 {
120                         // we can't enum application
121                         GetEnumerator (IsolatedStorageScope.Application);
122                 }
123 #endif
124                 [Test]
125                 public void GetUserStoreForAssembly ()
126                 {
127                         IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForAssembly ();
128                         Assert.AreEqual (Int64.MaxValue, isf.MaximumSize, "MaximumSize");
129                         Assert.AreEqual (IsolatedStorageScope.User | IsolatedStorageScope.Assembly, isf.Scope, "Scope");
130 #if !NET_2_1
131                         Assert.IsTrue ((isf.AssemblyIdentity is Url), "AssemblyIdentity");
132                         // note: mono transforms the CodeBase into uppercase
133                         // for net 1.1 which uses file:// and not file:///
134                         string codebase = Assembly.GetExecutingAssembly ().CodeBase.ToUpper ().Substring (8);
135                         Assert.IsTrue ((isf.AssemblyIdentity.ToString ().ToUpper ().IndexOf (codebase) > 0), "Url");
136                         Assert.IsTrue ((isf.AssemblyIdentity.ToString ().ToUpper ().IndexOf (codebase) > 0), "Url");
137 #endif
138                         Assert.IsTrue ((isf.CurrentSize >= 0), "CurrentSize");
139                 }
140
141                 [Test]
142                 [ExpectedException (typeof (InvalidOperationException))]
143                 public void GetUserStoreForAssembly_DomainIdentity ()
144                 {
145                         IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForAssembly ();
146                         object o = isf.DomainIdentity;
147                 }
148
149 #if NET_2_0
150                 [Test]
151                 [ExpectedException (typeof (InvalidOperationException))]
152                 public void GetUserStoreForAssembly_ApplicationIdentity ()
153                 {
154                         IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForAssembly ();
155                         object o = isf.ApplicationIdentity;
156                 }
157 #endif
158
159                 [Test]
160                 public void GetUserStoreForDomain ()
161                 {
162                         IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForDomain ();
163                         Assert.AreEqual (Int64.MaxValue, isf.MaximumSize, "MaximumSize");
164                         Assert.AreEqual (IsolatedStorageScope.User | IsolatedStorageScope.Domain | IsolatedStorageScope.Assembly, isf.Scope, "Scope");
165 #if !NET_2_1
166                         Assert.IsTrue ((isf.AssemblyIdentity is Url), "AssemblyIdentity");
167                         // note: mono transforms the CodeBase into uppercase
168                         // for net 1.1 which uses file:// and not file:///
169                         string codebase = Assembly.GetExecutingAssembly ().CodeBase.ToUpper ().Substring (8);
170                         Assert.IsTrue ((isf.AssemblyIdentity.ToString ().ToUpper ().IndexOf (codebase) > 0), "Url");
171                         Assert.IsTrue ((isf.DomainIdentity is Url), "DomainIdentity");
172                         // note: with MS Assembly.GetEntryAssembly () only works in the default (first) AppDomain
173                         // so we're using the first parameter to GetCommandLineArgs
174                         string exe = Environment.GetCommandLineArgs ()[0].Replace ("\\", "/").ToUpper ();
175                         Assert.IsTrue ((isf.DomainIdentity.ToString ().ToUpper ().IndexOf (exe) > 0), exe + "\n" + isf.DomainIdentity.ToString ().ToUpper ()); //"Url - Domain");
176 #endif
177                         Assert.IsTrue ((isf.CurrentSize >= 0), "CurrentSize");
178                 }
179
180 #if NET_2_0
181                 [Test]
182                 [ExpectedException (typeof (InvalidOperationException))]
183                 public void GetUserStoreForDomain_ApplicationIdentity ()
184                 {
185                         IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForDomain ();
186                         object o = isf.ApplicationIdentity;
187                 }
188
189                 [Test]
190                 [ExpectedException (typeof (IsolatedStorageException))]
191                 public void GetUserStoreForApplication_WithoutApplicationIdentity ()
192                 {
193                         // note: a manifest is required
194                         IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForApplication ();
195                 }
196
197                 [Test]
198                 [ExpectedException (typeof (IsolatedStorageException))]
199                 public void GetUserStoreForApplication ()
200                 {
201                         IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForApplication ();
202                         Assert.AreEqual (Int64.MaxValue, isf.MaximumSize, "MaximumSize");
203 #if !NET_2_1
204                         Assert.AreEqual (IsolatedStorageScope.User | IsolatedStorageScope.Assembly, isf.Scope, "Scope");
205                         Assert.IsTrue ((isf.AssemblyIdentity is Url), "AssemblyIdentity");
206                         Assert.IsTrue ((isf.AssemblyIdentity.ToString ().IndexOf (Assembly.GetExecutingAssembly ().CodeBase) > 0), "Url");
207 #endif
208                         Assert.IsTrue ((isf.CurrentSize >= 0), "CurrentSize");
209                 }
210                 
211 #if !NET_2_1
212                 [Test]
213                 [ExpectedException (typeof (IsolatedStorageException))]
214                 public void GetUserStoreForApplication_AssemblyIdentity ()
215                 {
216                         IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForApplication ();
217                         object o = isf.AssemblyIdentity;
218                 }
219
220                 [Test]
221                 [ExpectedException (typeof (IsolatedStorageException))]
222                 public void GetUserStoreForApplication_DomainIdentity ()
223                 {
224                         IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForApplication ();
225                         object o = isf.DomainIdentity;
226                 }
227 #endif
228 #endif
229
230 #if NET_4_0
231                 // This is supposed to be working only in SL.
232                 [Test]
233                 [ExpectedException (typeof (NotSupportedException))]
234                 public void GetUserStoreForSite ()
235                 {
236                         IsolatedStorageFile.GetUserStoreForSite ();
237                 }
238 #endif
239
240                 [Test]
241                 public void GetStore_Domain_Zone ()
242                 {
243                         IsolatedStorageScope scope = IsolatedStorageScope.User | IsolatedStorageScope.Domain | IsolatedStorageScope.Assembly;
244                         IsolatedStorageFile isf = IsolatedStorageFile.GetStore (scope, typeof (Zone), typeof (Zone));
245                         Assert.AreEqual (Int64.MaxValue, isf.MaximumSize, "MaximumSize");
246 #if !NET_2_1
247                         Assert.AreEqual (IsolatedStorageScope.User | IsolatedStorageScope.Domain | IsolatedStorageScope.Assembly, isf.Scope, "Scope");
248                         Assert.IsTrue ((isf.AssemblyIdentity is Zone), "AssemblyIdentity");
249                         Assert.IsTrue ((isf.AssemblyIdentity.ToString ().IndexOf ("MyComputer") > 0), "Zone - Assembly");
250                         Assert.IsTrue ((isf.DomainIdentity is Zone), "DomainIdentity");
251                         Assert.IsTrue ((isf.DomainIdentity.ToString ().IndexOf ("MyComputer") > 0), "Zone - Domain");
252 #endif
253                         Assert.IsTrue ((isf.CurrentSize >= 0), "CurrentSize");
254                 }
255
256                 [Test]
257                 [ExpectedException (typeof (IsolatedStorageException))]
258                 public void GetStore_Domain_NonPresentEvidences ()
259                 {
260                         IsolatedStorageScope scope = IsolatedStorageScope.User | IsolatedStorageScope.Domain | IsolatedStorageScope.Assembly;
261                         IsolatedStorageFile isf = IsolatedStorageFile.GetStore (scope, typeof (StrongName), typeof (Publisher));
262                 }
263
264                 [Test]
265                 public void GetStore_Assembly_NonPresentDomainEvidences ()
266                 {
267                         IsolatedStorageScope scope = IsolatedStorageScope.User | IsolatedStorageScope.Assembly;
268                         IsolatedStorageFile isf = IsolatedStorageFile.GetStore (scope, typeof (StrongName), typeof (Url));
269                         Assert.AreEqual (Int64.MaxValue, isf.MaximumSize, "MaximumSize");
270                         Assert.AreEqual (scope, isf.Scope, "Scope");
271 #if !NET_2_1
272                         Assert.IsTrue ((isf.AssemblyIdentity is Url), "AssemblyIdentity");
273                         // note: mono transforms the CodeBase into uppercase
274                         // for net 1.1 which uses file:// and not file:///
275                         string codebase = Assembly.GetExecutingAssembly ().CodeBase.ToUpper ().Substring (8);
276                         Assert.IsTrue ((isf.AssemblyIdentity.ToString ().ToUpper ().IndexOf (codebase) > 0), "Url");
277                         // DomainIdentity throws a InvalidOperationException
278 #endif
279                         Assert.IsTrue ((isf.CurrentSize >= 0), "CurrentSize");
280                 }
281
282                 [Test]
283                 [ExpectedException (typeof (ArgumentNullException))]
284                 public void GetStore_Domain_DomainNullObject ()
285                 {
286                         IsolatedStorageScope scope = IsolatedStorageScope.User | IsolatedStorageScope.Domain | IsolatedStorageScope.Assembly;
287                         IsolatedStorageFile isf = IsolatedStorageFile.GetStore (scope, (object)null, new Zone (SecurityZone.MyComputer));
288                 }
289
290                 [Test]
291                 [ExpectedException (typeof (ArgumentNullException))]
292                 public void GetStore_Domain_AssemblyNullObject ()
293                 {
294                         IsolatedStorageScope scope = IsolatedStorageScope.User | IsolatedStorageScope.Domain | IsolatedStorageScope.Assembly;
295                         IsolatedStorageFile isf = IsolatedStorageFile.GetStore (scope, new Zone (SecurityZone.MyComputer), (object)null);
296                 }
297
298                 [Test]
299                 public void GetStore_Assembly_DomainNullObject ()
300                 {
301                         IsolatedStorageScope scope = IsolatedStorageScope.User | IsolatedStorageScope.Assembly;
302                         IsolatedStorageFile isf = IsolatedStorageFile.GetStore (scope, (object)null, new Zone (SecurityZone.Internet));
303                         Assert.AreEqual (Int64.MaxValue, isf.MaximumSize, "MaximumSize");
304                         Assert.AreEqual (scope, isf.Scope, "Scope");
305                         Assert.IsTrue ((isf.AssemblyIdentity is Zone), "AssemblyIdentity");
306                         Assert.IsTrue ((isf.AssemblyIdentity.ToString ().IndexOf ("Internet") > 0), "Zone - Assembly");
307                         Assert.IsTrue ((isf.CurrentSize >= 0), "CurrentSize");
308                 }
309
310                 [Test]
311                 [ExpectedException (typeof (ArgumentNullException))]
312                 public void GetStore_Assembly_AssemblyNullObject ()
313                 {
314                         IsolatedStorageScope scope = IsolatedStorageScope.User | IsolatedStorageScope.Assembly;
315                         IsolatedStorageFile isf = IsolatedStorageFile.GetStore (scope, new Zone (SecurityZone.MyComputer), (object)null);
316                 }
317
318                 [Test]
319                 public void GetStore_Domain_ZoneObjectZoneObject ()
320                 {
321                         IsolatedStorageScope scope = IsolatedStorageScope.User | IsolatedStorageScope.Domain | IsolatedStorageScope.Assembly;
322                         IsolatedStorageFile isf = IsolatedStorageFile.GetStore (scope, new Zone (SecurityZone.Internet), new Zone (SecurityZone.Internet));
323                         Assert.AreEqual (Int64.MaxValue, isf.MaximumSize, "MaximumSize");
324                         Assert.AreEqual (scope, isf.Scope, "Scope");
325                         Assert.IsTrue ((isf.AssemblyIdentity is Zone), "AssemblyIdentity");
326                         Assert.IsTrue ((isf.AssemblyIdentity.ToString ().IndexOf ("Internet") > 0), "Zone - Assembly");
327                         Assert.IsTrue ((isf.DomainIdentity is Zone), "DomainIdentity");
328                         Assert.IsTrue ((isf.DomainIdentity.ToString ().IndexOf ("Internet") > 0), "Zone - Domain");
329                         Assert.IsTrue ((isf.CurrentSize >= 0), "CurrentSize");
330                 }
331 #if NET_2_0
332                 [Test]
333                 [ExpectedException (typeof (ArgumentNullException))]
334                 public void GetStore_Application_NullObject ()
335                 {
336                         IsolatedStorageScope scope = IsolatedStorageScope.User | IsolatedStorageScope.Application;
337                         IsolatedStorageFile isf = IsolatedStorageFile.GetStore (scope, (object)null);
338                 }
339
340                 [Test]
341                 [ExpectedException (typeof (IsolatedStorageException))]
342                 public void GetStore_Application_NullType ()
343                 {
344                         IsolatedStorageScope scope = IsolatedStorageScope.User | IsolatedStorageScope.Application;
345                         IsolatedStorageFile isf = IsolatedStorageFile.GetStore (scope, (Type)null);
346                         // again it's the lack of a manifest
347                 }
348 #endif
349
350                 [Test]
351                 public void GetStore_DomainScope_Evidences ()
352                 {
353                         IsolatedStorageScope scope = IsolatedStorageScope.User | IsolatedStorageScope.Domain | IsolatedStorageScope.Assembly;
354
355                         Evidence de = new Evidence ();
356                         de.AddHost (new Zone (SecurityZone.Internet));
357                         Evidence ae = new Evidence ();
358                         ae.AddHost (new Zone (SecurityZone.Intranet));
359                         IsolatedStorageFile isf = IsolatedStorageFile.GetStore (scope, de, typeof (Zone), ae, typeof (Zone));
360
361                         // Maximum size for Internet isn't (by default) Int64.MaxValue
362                         Assert.AreEqual (scope, isf.Scope, "Scope");
363 #if !NET_2_1
364                         Assert.IsTrue ((isf.AssemblyIdentity is Zone), "AssemblyIdentity");
365                         Assert.IsTrue ((isf.AssemblyIdentity.ToString ().IndexOf ("Intranet") > 0), "Zone - Assembly");
366                         Assert.IsTrue ((isf.DomainIdentity is Zone), "DomainIdentity");
367                         Assert.IsTrue ((isf.DomainIdentity.ToString ().IndexOf ("Internet") > 0), isf.DomainIdentity.ToString ()); //"Zone - Domain");
368 #endif
369                         Assert.IsTrue ((isf.CurrentSize >= 0), "CurrentSize");
370                 }
371
372                 [Test]
373                 [ExpectedException (typeof (ArgumentNullException))]
374                 public void GetStore_DomainScope_Evidence_NullAssemblyEvidence ()
375                 {
376                         IsolatedStorageScope scope = IsolatedStorageScope.User | IsolatedStorageScope.Domain | IsolatedStorageScope.Assembly;
377
378                         Evidence de = new Evidence ();
379                         de.AddHost (new Zone (SecurityZone.Internet));
380                         IsolatedStorageFile isf = IsolatedStorageFile.GetStore (scope, de, typeof (Zone), null, null);
381                 }
382
383                 [Test]
384                 [ExpectedException (typeof (ArgumentNullException))]
385                 public void GetStore_DomainScope_Evidence_NullDomainEvidence ()
386                 {
387                         IsolatedStorageScope scope = IsolatedStorageScope.User | IsolatedStorageScope.Domain | IsolatedStorageScope.Assembly;
388
389                         Evidence ae = new Evidence ();
390                         ae.AddHost (new Zone (SecurityZone.Internet));
391                         IsolatedStorageFile isf = IsolatedStorageFile.GetStore (scope, null, null, ae, typeof (Zone));
392                 }
393
394                 [Test]
395                 [ExpectedException (typeof (ArgumentNullException))]
396                 public void GetStore_AssemblyScope_Evidence_NullAssemblyEvidence ()
397                 {
398                         IsolatedStorageScope scope = IsolatedStorageScope.User | IsolatedStorageScope.Assembly;
399
400                         Evidence de = new Evidence ();
401                         de.AddHost (new Zone (SecurityZone.Internet));
402                         IsolatedStorageFile isf = IsolatedStorageFile.GetStore (scope, de, typeof (Zone), null, null);
403                 }
404
405                 [Test]
406                 public void GetStore_AssemblyScope_Evidence_NullDomainEvidence ()
407                 {
408                         IsolatedStorageScope scope = IsolatedStorageScope.User | IsolatedStorageScope.Assembly;
409
410                         Evidence ae = new Evidence ();
411                         ae.AddHost (new Zone (SecurityZone.Internet));
412                         IsolatedStorageFile isf = IsolatedStorageFile.GetStore (scope, null, null, ae, typeof (Zone));
413                 }
414
415                 [Test]
416                 public void RegressionBNC354539 ()
417                 {
418                         string filename = "test-bnc-354539";
419                         byte[] expected = new byte[] { 0x01, 0x42, 0x00 };
420                         byte[] actual = new byte [expected.Length];
421
422                         using (IsolatedStorageFile file = IsolatedStorageFile.GetStore (IsolatedStorageScope.User | IsolatedStorageScope.Assembly, null, null)) {
423                                 using (IsolatedStorageFileStream stream = new IsolatedStorageFileStream (filename, FileMode.Create, FileAccess.Write, FileShare.None, file)) {
424                                         stream.Write (expected, 0, expected.Length);
425                                 }
426                         }
427
428                         using (IsolatedStorageFile file = IsolatedStorageFile.GetUserStoreForAssembly ()) {
429                                 using (IsolatedStorageFileStream stream = new IsolatedStorageFileStream (filename, FileMode.Open, FileAccess.Read, FileShare.Read, file)) {
430                                         stream.Read (actual, 0, actual.Length);
431                                 }
432
433                                 file.DeleteFile (filename);
434                         }
435                         
436                         Assert.AreEqual (expected, actual);
437                 }
438
439                 [Test]
440                 [ExpectedException (typeof (ArgumentNullException))]
441                 public void CreateDirectory_Null ()
442                 {
443                         IsolatedStorageFile.GetUserStoreForAssembly ().CreateDirectory (null);
444                 }
445
446                 [Test]
447                 public void CreateDirectory_FileWithSameNameExists ()
448                 {
449                         string path = "bug374377";
450                         using (IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForDomain ()) {
451                                 using (IsolatedStorageFileStream fs = new IsolatedStorageFileStream (path, FileMode.OpenOrCreate, isf)) {
452                                 }
453                                 try {
454                                         isf.CreateDirectory (path);
455                                 }
456 #if NET_4_0 || NET_2_1
457                                 catch (IsolatedStorageException ex) {
458                                         Assert.IsFalse (ex.Message.IndexOf (path) >= 0, "Message");
459                                         Assert.IsNull (ex.InnerException, "InnerException");
460                                 }
461 #else
462                                 catch (IOException ex) {
463                                         Assert.AreEqual (typeof (IOException), ex.GetType (), "Type");
464                                         // don't leak path information
465                                         Assert.IsFalse (ex.Message.IndexOf (path) >= 0, "Message");
466                                         Assert.IsNull (ex.InnerException, "InnerException");
467                                 }
468 #endif
469                         }
470                 }
471
472                 [Test]
473                 public void CreateDirectory_DirectoryWithSameNameExists ()
474                 {
475                         string dir = "new-dir";
476                         string file = Path.Combine (dir, "new-file");
477                         IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForAssembly ();
478                         try {
479                                 isf.CreateDirectory (dir);
480                                 using (IsolatedStorageFileStream isfs = new IsolatedStorageFileStream (file, FileMode.OpenOrCreate, isf)) {
481                                         isfs.WriteByte (0);
482                                 }
483                                 string pattern = Path.Combine (dir, "*");
484                                 Assert.AreEqual (1, isf.GetFileNames (file).Length, "file exists");
485
486                                 // create again directory
487                                 isf.CreateDirectory (dir);
488                                 Assert.AreEqual (1, isf.GetFileNames (file).Length, "file still exists");
489                         }
490                         finally {
491                                 isf.DeleteFile (file);
492                                 isf.DeleteDirectory (dir);
493                         }
494                 }
495
496                 [Test]
497 #if NET_4_0 || NET_2_1
498                 [ExpectedException (typeof (ArgumentException))]
499 #else
500                 [ExpectedException (typeof (SecurityException))]
501 #endif
502                 public void GetFilesInSubdirs ()
503                 {
504                         IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForAssembly ();
505                         string pattern = Path.Combine ("..", "*");
506                         isf.GetFileNames (pattern);
507                 }
508
509         
510 #if NET_4_0
511                 [Test]
512                 [ExpectedException (typeof (ArgumentException))]
513                 public void GetDirsInSubDirs ()
514                 {
515                         IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForAssembly ();
516                         isf.CreateDirectory ("subdir");
517                         string [] dir_names = isf.GetDirectoryNames ("subdir/../*");
518                 }
519 #endif
520
521                 [Test] // https://bugzilla.novell.com/show_bug.cgi?id=376188
522                 public void CreateSubDirectory ()
523                 {
524                         IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForAssembly ();
525                         isf.CreateDirectory ("subdir");
526                         isf.CreateDirectory ("subdir/subdir2");
527                         Assert.AreEqual (1, isf.GetDirectoryNames ("*").Length, "subdir");
528                         Assert.AreEqual (1, isf.GetDirectoryNames ("subdir/*").Length, "subdir/subdir2");
529                         isf.DeleteDirectory ("subdir/subdir2");
530                         isf.DeleteDirectory ("subdir");
531                 }
532
533                 [Test]
534                 [ExpectedException (typeof (IsolatedStorageException))]
535                 public void DeleteDirectory_NonEmpty ()
536                 {
537                         IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForAssembly ();
538                         isf.CreateDirectory ("subdir");
539                         isf.CreateDirectory ("subdir/subdir2");
540                         isf.DeleteDirectory ("subdir");
541                 }
542
543                 [Test]
544                 public void DeleteFile ()
545                 {
546                         IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForAssembly ();
547
548                         try {
549                                 isf.DeleteFile (null);
550                                 Assert.Fail ("#Exc0");
551                         } catch (ArgumentNullException) {
552                         }
553
554 #if NET_4_0
555                         // We are getting an internal IndexOutOfRangeException in 2.0
556                         // Not sure we want to mimic that one.
557                         try {
558                                 isf.DeleteFile (String.Empty);
559                                 Assert.Fail ("#Exc1");
560                         } catch (IsolatedStorageException) {
561                         }
562 #endif
563
564                         try {
565                                 isf.DeleteFile ("idontexist");
566                                 Assert.Fail ("#Exc2");
567                         } catch (IsolatedStorageException) {
568                         }
569
570 #if NET_4_0
571                         try {
572                                 isf.DeleteFile ("../../file");
573                                 Assert.Fail ("#Exc3");
574                         } catch (IsolatedStorageException) {
575                         }
576 #endif
577                 
578                         try {
579                                 isf.DeleteFile ("subdir/file");
580                                 Assert.Fail ("#Exc4");
581                         } catch (IsolatedStorageException) {
582                         }
583
584                         isf.CreateDirectory ("subdir");
585                         try {
586                                 isf.DeleteFile ("subdir");
587                                 Assert.Fail ("#Exc5");
588                         } catch (IsolatedStorageException) {
589                         }
590                 }
591
592                 [Test]
593                 public void GetStore_NullTypes ()
594                 {
595                         IsolatedStorageScope scope = IsolatedStorageScope.User | IsolatedStorageScope.Roaming | IsolatedStorageScope.Assembly | IsolatedStorageScope.Domain;
596                         IsolatedStorageFile isf = IsolatedStorageFile.GetStore (scope, null, null);
597 #if !NET_2_1
598                         Assert.AreEqual (typeof (Url), isf.AssemblyIdentity.GetType (), "AssemblyIdentity");
599                         Assert.AreEqual (typeof (Url), isf.DomainIdentity.GetType (), "DomainIdentity");
600 #endif
601                 }
602
603                 [Test]
604                 public void RemoveFromOtherInstance ()
605                 {
606                         IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForAssembly ();
607                         IsolatedStorageFile isf2 = IsolatedStorageFile.GetUserStoreForAssembly ();
608
609                         isf.Remove ();
610                         try {
611                                 isf2.Remove ();
612                                 Assert.Fail ("#Exc1");
613                         } catch (IsolatedStorageException) {
614                         }
615                 }
616
617 #if NET_4_0
618                 [Test]
619                 public void Remove ()
620                 {
621                         // Test that we can call Remove several times
622                         IsolatedStorageFile.Remove (IsolatedStorageScope.User);
623                         IsolatedStorageFile.Remove (IsolatedStorageScope.User);
624
625                         IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForAssembly ();
626                         isf.Remove ();
627
628                         // The second call to Remove should cause an InvalidOperationException, due to
629                         // marking itself as closed.
630                         try {
631                                 isf.Remove ();
632                                 Assert.Fail ("#Exc1");
633                         } catch (InvalidOperationException) {
634                         }
635
636                         // Open, Close and try to Remove
637                         isf = IsolatedStorageFile.GetUserStoreForAssembly ();
638                         isf.Close ();
639                         try {
640                                 isf.Remove ();
641                                 Assert.Fail ("#Exc2");
642                         } catch (InvalidOperationException) {
643                         }
644                 }
645
646                 [Test]
647                 public void UsedSize ()
648                 {
649                         IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForAssembly ();
650                         IsolatedStorageFileStream isfs = isf.CreateFile ("file");
651                         StreamWriter writer = new StreamWriter (isfs);
652                         writer.WriteLine ("hello mono");
653                         writer.Close ();
654
655                         Assert.AreEqual (true, isf.UsedSize > 0, "#A0");
656
657                         isf.Close ();
658                         try {
659                                 Console.WriteLine (isf.UsedSize);
660                                 Assert.Fail ("#Exc1");
661                         } catch (InvalidOperationException) {
662                         }
663
664                         isf.Dispose ();
665                         try {
666                                 Console.WriteLine (isf.UsedSize);
667                                 Assert.Fail ("#Exc2");
668                         } catch (ObjectDisposedException) {
669                         }
670                 }
671
672                 [Test]
673                 public void IncreateQuotaTo ()
674                 {
675                         IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForAssembly ();
676
677                         try {
678                                 isf.IncreaseQuotaTo (-2);
679                                 Assert.Fail ("#Exc1");
680                         } catch (ArgumentException) {
681                         }
682
683                         // I wonder how this behaves on some systems
684                         try {
685                                 isf.IncreaseQuotaTo (100);
686                                 Assert.Fail ("#Exc2");
687                         } catch (ArgumentException) {
688                         }
689
690                         // Since 'Quota' seems to be returning Int64.MaxValue, we cannot truly test against a value
691                         // larger than that.
692                 }
693
694                 [Test]
695                 public void DirectoryExists ()
696                 {
697                         IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForAssembly ();
698                         isf.CreateDirectory ("subdir");
699                         isf.CreateDirectory ("subdir/subdir2");
700                         isf.CreateDirectory ("subdir3");
701
702                         Assert.AreEqual (true, isf.DirectoryExists ("subdir/"), "#A0");
703                         Assert.AreEqual (true, isf.DirectoryExists ("subdir/subdir2/"), "#A1");
704                         Assert.AreEqual (true, isf.DirectoryExists ("subdir3"), "#A2");
705                         Assert.AreEqual (true, isf.DirectoryExists (String.Empty), "#A3"); // Weird
706                         Assert.AreEqual (false, isf.DirectoryExists ("subdir99"), "#A4");
707                         Assert.AreEqual (false, isf.DirectoryExists ("../../subdir"), "#A5");
708                         Assert.AreEqual (false, isf.DirectoryExists ("*"), "#A5");
709                         Assert.AreEqual (false, isf.DirectoryExists ("subdir*"), "#A6");
710
711                         isf.DeleteDirectory ("subdir3");
712                         Assert.AreEqual (false, isf.DirectoryExists ("subdir3"), "#B0");
713
714                         isf.DeleteDirectory ("subdir/subdir2");
715                         isf.DeleteDirectory ("subdir");
716
717                         try {
718                                 isf.DirectoryExists (null);
719                                 Assert.Fail ("#Exc1");
720                         } catch (ArgumentNullException) {
721                         }
722
723                         isf.Close ();
724                         try {
725                                 isf.DirectoryExists ("subdir");
726                                 Assert.Fail ("#Exc2");
727                         } catch (InvalidOperationException) {
728                         }
729
730                         isf.Dispose ();
731                         try {
732                                 isf.DirectoryExists ("subdir");
733                                 Assert.Fail ("#Exc3");
734                         } catch (ObjectDisposedException) {
735                         }
736
737                         // We want to be sure that if not closing but disposing
738                         // should fire ObjectDisposedException instead of InvalidOperationException
739                         isf = IsolatedStorageFile.GetUserStoreForAssembly ();
740                         isf.Dispose ();
741
742                         try {
743                                 isf.DirectoryExists ("subdir");
744                                 Assert.Fail ("#Exc4");
745                         } catch (ObjectDisposedException) {
746                         }
747                 }
748
749                 [Test]
750                 public void FileExists ()
751                 {
752                         IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForAssembly ();
753                         IsolatedStorageFileStream file_a = new IsolatedStorageFileStream ("file-a", FileMode.Create, isf);
754                         IsolatedStorageFileStream file_b = new IsolatedStorageFileStream ("file-b", FileMode.Create, isf);
755                         file_a.Close ();
756                         file_b.Close ();
757
758                         Assert.AreEqual (true, isf.FileExists ("file-a"), "#A0");
759                         Assert.AreEqual (true, isf.FileExists ("file-b"), "#A1");
760                         Assert.AreEqual (false, isf.FileExists (String.Empty), "#A2");
761                         Assert.AreEqual (false, isf.FileExists ("file-"), "#A3");
762                         Assert.AreEqual (false, isf.FileExists ("file-*"), "#A4");
763                         Assert.AreEqual (false, isf.FileExists ("../../file-a"), "#A5");
764
765                         isf.CreateDirectory ("subdir");
766                         Assert.AreEqual (false, isf.FileExists ("subdir"), "#B0");
767
768                         try {
769                                 isf.FileExists (null);
770                                 Assert.Fail ("#Exc1");
771                         } catch (ArgumentNullException) {
772                         }
773
774                         isf.Close ();
775                         try {
776                                 isf.FileExists ("file-a");
777                                 Assert.Fail ("#Exc2");
778                         } catch (InvalidOperationException) {
779                         }
780
781                         isf.Dispose ();
782                         try {
783                                 isf.FileExists ("file-a");
784                                 Assert.Fail ("#Exc3");
785                         } catch (ObjectDisposedException) {
786                         }
787                 }
788
789                 [Test]
790                 public void CreateFile ()
791                 {
792                         IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForAssembly ();
793                         // Make sure we are actually creating it, by first removing it in case it already exists
794                         if (isf.FileExists ("file-a"))
795                                 isf.DeleteFile ("file-a");
796
797                         IsolatedStorageFileStream isf_stream = isf.CreateFile ("file-a");
798                         isf_stream.Close ();
799                         Assert.AreEqual (true, isf.FileExists ("file-a"), "#A0");
800
801                         // Re-open the file that is already created, so we make sure we are passing
802                         // the proper FileOpen
803                         isf_stream = isf.CreateFile ("file-a");
804                         isf_stream.Close ();
805
806                         try {
807                                 isf.CreateFile (null);
808                                 Assert.Fail ("#Exc1");
809                         } catch (ArgumentNullException) {
810                         }
811
812                         try {
813                                 isf.CreateFile ("random-dir/fileb");
814                                 Assert.Fail ("#Exc2");
815                         } catch (DirectoryNotFoundException) {
816                         }
817
818                         isf.Close ();
819                         try {
820                                 isf.CreateFile ("file-b");
821                                 Assert.Fail ("#Exc3");
822                         } catch (InvalidOperationException) {
823                         }
824
825                         isf.Dispose ();
826                         try {
827                                 isf.CreateFile ("file-a");
828                                 Assert.Fail ("#Exc4");
829                         } catch (ObjectDisposedException) {
830                         }
831                 }
832
833                 [Test]
834                 public void GetCreationTime ()
835                 {
836                         IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForAssembly ();
837
838                         // This is not causing an exception
839                         isf.GetCreationTime ("doesntexist");
840                         isf.GetCreationTime ("dir/doesntexist");
841
842                         try {
843                                 isf.GetCreationTime (String.Empty);
844                                 Assert.Fail ("#Exc1");
845                         } catch (ArgumentException) {
846                         }
847
848                         try {
849                                 isf.GetCreationTime ("   ");
850                                 Assert.Fail ("#Exc2");
851                         } catch (ArgumentException) {
852                         }
853
854                         isf.Close ();
855                         try {
856                                 isf.GetCreationTime ("doesntexist");
857                                 Assert.Fail ("#Exc3");
858                         } catch (InvalidOperationException) {
859                         }
860
861                         isf.Dispose ();
862                         try {
863                                 isf.GetCreationTime ("doesntexist");
864                                 Assert.Fail ("#Exc4");
865                         } catch (ObjectDisposedException) {
866                         }
867                 }
868
869                 [Test]
870                 public void MoveDirectory ()
871                 {
872                         IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForAssembly ();
873                         // Mare sure to remove them if they exist already
874                         if (isf.DirectoryExists ("subdir"))
875                                 isf.DeleteDirectory ("subdir");
876                         if (isf.DirectoryExists ("subdir-new"))
877                                 isf.DeleteDirectory ("subdir-new");
878
879                         isf.CreateDirectory ("subdir");
880                         Assert.AreEqual (true, isf.DirectoryExists ("subdir"), "#A0");
881
882                         isf.MoveDirectory ("subdir", "subdir-new");
883                         Assert.AreEqual (false, isf.DirectoryExists ("subdir"), "#A1");
884                         Assert.AreEqual (true, isf.DirectoryExists ("subdir-new"), "#A2");
885
886                         try {
887                                 isf.MoveDirectory (String.Empty, "subdir-new-new");
888                                 Assert.Fail ("#Exc1");
889                         } catch (ArgumentException) {
890                         }
891
892                         try {
893                                 isf.MoveDirectory ("  ", "subdir-new-new");
894                                 Assert.Fail ("#Exc2");
895                         } catch (ArgumentException) {
896                         }
897
898                         try {
899                                 isf.MoveDirectory ("doesntexist", "subdir-new-new");
900                                 Assert.Fail ("#Exc3");
901                         } catch (DirectoryNotFoundException) {
902                         }
903
904                         try {
905                                 isf.MoveDirectory ("doesnexist/doesntexist", "subdir-new-new");
906                                 Assert.Fail ("#Exc4");
907                         } catch (DirectoryNotFoundException) {
908                         }
909
910                         try {
911                                 isf.MoveDirectory ("subdir-new", "doesntexist/doesntexist");
912                                 Assert.Fail ("#Exc5");
913                         } catch (DirectoryNotFoundException) {
914                         }
915
916                         // Out of storage dir
917                         try {
918                                 isf.MoveDirectory ("subdir-new", "../../subdir-new");
919                                 Assert.Fail ("#Exc6");
920                         } catch (IsolatedStorageException) {
921                         }
922
923                         isf.Remove ();
924                         isf.Close ();
925                         isf.Dispose ();
926                 }
927
928                 [Test]
929                 public void CopyFile ()
930                 {
931                         IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForAssembly ();
932                         if (isf.FileExists ("file"))
933                                 isf.DeleteFile ("file");
934                         if (isf.FileExists ("file-new"))
935                                 isf.DeleteFile ("file-new");
936
937                         isf.CreateFile ("file").Close ();
938                         isf.CopyFile ("file", "file-new");
939                         Assert.AreEqual (true, isf.FileExists ("file"), "#A0");
940                         Assert.AreEqual (true, isf.FileExists ("file-new"), "#A1");
941
942                         // At this point 'file-exists' already exists.
943                         isf.CopyFile ("file", "file-new", true);
944                         Assert.AreEqual (true, isf.FileExists ("file"), "#B0");
945                         Assert.AreEqual (true, isf.FileExists ("file-new"), "#B1");
946
947                         isf.CreateDirectory ("subdir");
948                         isf.CreateFile ("subdir/subfile").Close ();
949                         isf.CopyFile ("subdir/subfile", "subdir/subfile-new");
950                         Assert.AreEqual (true, isf.FileExists ("subdir/subfile"), "#C0");
951                         Assert.AreEqual (true, isf.FileExists ("subdir/subfile-new"), "#C1");
952
953                         try {
954                                 isf.CopyFile ("file", "file-new");
955                                 Assert.Fail ("#Exc0");
956                         } catch (IsolatedStorageException) {
957                         }
958
959                         // Using the same file name is failing for even when passing override=true.
960                         try {
961                                 isf.CopyFile ("file-new", "file-new", true);
962                                 Assert.Fail ("#Exc1");
963                         } catch (IsolatedStorageException) {
964                         }
965
966                         try {
967                                 isf.CopyFile ("file-new", "file-new", false);
968                                 Assert.Fail ("#Exc2");
969                         } catch (IsolatedStorageException) {
970                         }
971
972                         // Remove 'file-new' for cleaness purposes.
973                         isf.DeleteFile ("file-new");
974
975                         try {
976                                 isf.CopyFile ("doesntexist", "file-new", false);
977                                 Assert.Fail ("#Exc3");
978                         } catch (FileNotFoundException) {
979                         }
980
981                         try {
982                                 isf.CopyFile ("doesnexist/doesntexist", "file-new", false);
983                                 Assert.Fail ("#Exc4");
984                         } catch (DirectoryNotFoundException) {
985                         }
986
987                         // I'd have expected a DirectoryNotFoundException here.
988                         try {
989                                 isf.CopyFile ("file", "doesntexist/doesntexist");
990                                 Assert.Fail ("#Exc5");
991                         } catch (IsolatedStorageException) {
992                         }
993
994                         // Out of storage dir
995                         try {
996                                 isf.CopyFile ("file", "../../file");
997                                 Assert.Fail ("#Exc6");
998                         } catch (IsolatedStorageException) {
999                         }
1000
1001                         try {
1002                                 isf.CopyFile ("../file", "file-new");
1003                                 Assert.Fail ("#Exc7");
1004                         } catch (IsolatedStorageException) {
1005                         }
1006
1007                         // We are creating a subdirectory and files within it, so remove it just in case.
1008                         isf.Remove ();
1009
1010                         isf.Close ();
1011                         isf.Dispose ();
1012                 }
1013
1014                 [Test]
1015                 public void MoveFile ()
1016                 {
1017                         IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForAssembly ();
1018                         // Mare sure to remove them if they exist already
1019                         if (isf.FileExists ("file"))
1020                                 isf.DeleteFile ("file");
1021                         if (isf.FileExists ("file-new"))
1022                                 isf.DeleteFile ("file-new");
1023                         if (isf.FileExists ("subdir/subfile"))
1024                                 isf.DeleteFile ("subdir/subfile");
1025                         if (isf.FileExists ("subdir/subfile-new"))
1026                                 isf.DeleteFile ("subdir/subfile-new");
1027
1028                         isf.CreateFile ("file").Close ();
1029                         Assert.AreEqual (true, isf.FileExists ("file"), "#A0");
1030
1031                         // Same file
1032                         isf.MoveFile ("file", "file");
1033                         Assert.AreEqual (true, isf.FileExists ("file"), "#A0-1");
1034
1035                         isf.MoveFile ("file", "file-new");
1036                         Assert.AreEqual (false, isf.FileExists ("file"), "#A1");
1037                         Assert.AreEqual (true, isf.FileExists ("file-new"), "#A2");
1038
1039                         isf.CreateDirectory ("subdir");
1040                         isf.CreateFile ("subdir/subfile").Close ();
1041                         isf.MoveFile ("subdir/subfile", "subdir/subfile-new");
1042                         Assert.AreEqual (false, isf.FileExists ("subdir/subfile"), "#B0");
1043                         Assert.AreEqual (true, isf.FileExists ("subdir/subfile-new"), "#B1");
1044
1045                         try {
1046                                 isf.MoveFile (String.Empty, "file-new-new");
1047                                 Assert.Fail ("#Exc1");
1048                         } catch (ArgumentException) {
1049                         }
1050
1051                         try {
1052                                 isf.MoveFile ("  ", "file-new-new");
1053                                 Assert.Fail ("#Exc2");
1054                         } catch (ArgumentException e) {
1055                                 Console.WriteLine (e);
1056                         }
1057
1058                         try {
1059                                 isf.MoveFile ("doesntexist", "file-new-new");
1060                                 Assert.Fail ("#Exc3");
1061                         } catch (FileNotFoundException) {
1062                         }
1063
1064                         // CopyFile is throwing a DirectoryNotFoundException here.
1065                         try {
1066                                 isf.MoveFile ("doesnexist/doesntexist", "file-new-new");
1067                                 Assert.Fail ("#Exc4");
1068                         } catch (FileNotFoundException) {
1069                         }
1070
1071                         // I'd have expected a DirectoryNotFoundException here.
1072                         try {
1073                                 isf.MoveFile ("file-new", "doesntexist/doesntexist");
1074                                 Assert.Fail ("#Exc5");
1075                         } catch (IsolatedStorageException) {
1076                         }
1077
1078                         // Out of storage dir
1079                         try {
1080                                 isf.MoveFile ("file-new", "../../file-new");
1081                                 Assert.Fail ("#Exc6");
1082                         } catch (IsolatedStorageException) {
1083                         }
1084
1085                         isf.Remove ();
1086                         isf.Close ();
1087                         isf.Dispose ();
1088                 }
1089 #endif
1090         }
1091 }