New test.
[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.IsolatedStorage;
33 using System.Reflection;
34 using System.Security;
35 using System.Security.Permissions;
36 using System.Security.Policy;
37
38 using NUnit.Framework;
39
40 namespace MonoTests.System.IO.IsolatedStorageTest {
41
42         [TestFixture]
43         public class IsolatedStorageFileTest {
44
45                 private const string dirname = "mono-unit-test";
46
47                 private void CheckEnumerated (int n, IsolatedStorageScope scope, IsolatedStorageFile isf)
48                 {
49                         string prefix = n.ToString () + " - " + scope.ToString () + " - ";
50                         Assert.IsNotNull (isf, prefix + "IsolatedStorageFile");
51                         Assert.IsTrue (((scope & isf.Scope) != 0), prefix + "Scope");
52
53                         if ((isf.Scope & IsolatedStorageScope.Assembly) != 0)
54                                 Assert.IsNotNull (isf.AssemblyIdentity, prefix + "AssemblyIdentity");
55                         if ((isf.Scope & IsolatedStorageScope.Domain) != 0)
56                                 Assert.IsNotNull (isf.DomainIdentity, prefix + "DomainIdentity");
57 #if NET_2_0
58                         if ((isf.Scope & IsolatedStorageScope.Application) != 0)
59                                 Assert.IsNotNull (isf.ApplicationIdentity, prefix + "ApplicationIdentity");
60 #endif
61                 }
62
63                 private void GetEnumerator (IsolatedStorageScope scope)
64                 {
65                         IEnumerator e = IsolatedStorageFile.GetEnumerator (scope);
66                         int n = 0;
67                         while (e.MoveNext ())
68                         {
69                                 IsolatedStorageFile isf = (IsolatedStorageFile)e.Current;
70                                 CheckEnumerated (++n, scope, isf);
71                         }
72                 }
73
74                 [Test]
75                 public void GetEnumerator_User ()
76                 {
77                         GetEnumerator (IsolatedStorageScope.User);
78                 }
79
80                 [Test]
81                 [ExpectedException (typeof (ArgumentException))]
82                 public void GetEnumerator_User_Details ()
83                 {
84                         // giving more details is bad
85                         GetEnumerator (IsolatedStorageScope.User | IsolatedStorageScope.Assembly | IsolatedStorageScope.Domain);
86                 }
87
88                 [Test]
89                 public void GetEnumerator_UserRoaming ()
90                 {
91                         GetEnumerator (IsolatedStorageScope.User | IsolatedStorageScope.Roaming);
92                 }
93
94                 [Test]
95                 [ExpectedException (typeof (ArgumentException))]
96                 public void GetEnumerator_UserRoaming_Details ()
97                 {
98                         // giving more details is bad
99                         GetEnumerator (IsolatedStorageScope.User | IsolatedStorageScope.Assembly | IsolatedStorageScope.Domain | IsolatedStorageScope.Roaming);
100                 }
101 #if NET_2_0
102                 [Test]
103                 public void GetEnumerator_Machine ()
104                 {
105                         GetEnumerator (IsolatedStorageScope.Machine);
106                 }
107
108                 [Test]
109                 [ExpectedException (typeof (ArgumentException))]
110                 public void GetEnumerator_Machine_Details ()
111                 {
112                         GetEnumerator (IsolatedStorageScope.Machine | IsolatedStorageScope.Assembly);
113                 }
114
115                 [Test]
116                 [ExpectedException (typeof (ArgumentException))]
117                 public void GetEnumerator_Application ()
118                 {
119                         // we can't enum application
120                         GetEnumerator (IsolatedStorageScope.Application);
121                 }
122 #endif
123                 [Test]
124                 public void GetUserStoreForAssembly ()
125                 {
126                         IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForAssembly ();
127                         Assert.AreEqual (Int64.MaxValue, isf.MaximumSize, "MaximumSize");
128                         Assert.AreEqual (IsolatedStorageScope.User | IsolatedStorageScope.Assembly, isf.Scope, "Scope");
129                         Assert.IsTrue ((isf.AssemblyIdentity is Url), "AssemblyIdentity");
130                         // note: mono transforms the CodeBase into uppercase
131                         // for net 1.1 which uses file:// and not file:///
132                         string codebase = Assembly.GetExecutingAssembly ().CodeBase.ToUpper ().Substring (8);
133                         Assert.IsTrue ((isf.AssemblyIdentity.ToString ().ToUpper ().IndexOf (codebase) > 0), "Url");
134                         Assert.IsTrue ((isf.AssemblyIdentity.ToString ().ToUpper ().IndexOf (codebase) > 0), "Url");
135                         Assert.IsTrue ((isf.CurrentSize >= 0), "CurrentSize");
136                 }
137
138                 [Test]
139                 [ExpectedException (typeof (InvalidOperationException))]
140                 public void GetUserStoreForAssembly_DomainIdentity ()
141                 {
142                         IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForAssembly ();
143                         object o = isf.DomainIdentity;
144                 }
145
146 #if NET_2_0
147                 [Test]
148                 [ExpectedException (typeof (InvalidOperationException))]
149                 public void GetUserStoreForAssembly_ApplicationIdentity ()
150                 {
151                         IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForAssembly ();
152                         object o = isf.ApplicationIdentity;
153                 }
154 #endif
155
156                 [Test]
157                 public void GetUserStoreForDomain ()
158                 {
159                         IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForDomain ();
160                         Assert.AreEqual (Int64.MaxValue, isf.MaximumSize, "MaximumSize");
161                         Assert.AreEqual (IsolatedStorageScope.User | IsolatedStorageScope.Domain | IsolatedStorageScope.Assembly, isf.Scope, "Scope");
162                         Assert.IsTrue ((isf.AssemblyIdentity is Url), "AssemblyIdentity");
163                         // note: mono transforms the CodeBase into uppercase
164                         // for net 1.1 which uses file:// and not file:///
165                         string codebase = Assembly.GetExecutingAssembly ().CodeBase.ToUpper ().Substring (8);
166                         Assert.IsTrue ((isf.AssemblyIdentity.ToString ().ToUpper ().IndexOf (codebase) > 0), "Url");
167                         Assert.IsTrue ((isf.DomainIdentity is Url), "DomainIdentity");
168                         // note: with MS Assembly.GetEntryAssembly () only works in the default (first) AppDomain
169                         // so we're using the first parameter to GetCommandLineArgs
170                         string exe = Environment.GetCommandLineArgs ()[0].Replace ("\\", "/").ToUpper ();
171                         Assert.IsTrue ((isf.DomainIdentity.ToString ().ToUpper ().IndexOf (exe) > 0), exe + "\n" + isf.DomainIdentity.ToString ().ToUpper ()); //"Url - Domain");
172                         Assert.IsTrue ((isf.CurrentSize >= 0), "CurrentSize");
173                 }
174
175 #if NET_2_0
176                 [Test]
177                 [ExpectedException (typeof (InvalidOperationException))]
178                 public void GetUserStoreForDomain_ApplicationIdentity ()
179                 {
180                         IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForDomain ();
181                         object o = isf.ApplicationIdentity;
182                 }
183
184                 [Test]
185                 [ExpectedException (typeof (IsolatedStorageException))]
186                 public void GetUserStoreForApplication_WithoutApplicationIdentity ()
187                 {
188                         // note: a manifest is required
189                         IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForApplication ();
190                 }
191
192                 [Test]
193                 [ExpectedException (typeof (IsolatedStorageException))]
194                 public void GetUserStoreForApplication ()
195                 {
196                         IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForApplication ();
197                         Assert.AreEqual (Int64.MaxValue, isf.MaximumSize, "MaximumSize");
198                         Assert.AreEqual (IsolatedStorageScope.User | IsolatedStorageScope.Assembly, isf.Scope, "Scope");
199                         Assert.IsTrue ((isf.AssemblyIdentity is Url), "AssemblyIdentity");
200                         Assert.IsTrue ((isf.AssemblyIdentity.ToString ().IndexOf (Assembly.GetExecutingAssembly ().CodeBase) > 0), "Url");
201                         Assert.IsTrue ((isf.CurrentSize >= 0), "CurrentSize");
202                 }
203
204                 [Test]
205                 [ExpectedException (typeof (IsolatedStorageException))]
206                 public void GetUserStoreForApplication_AssemblyIdentity ()
207                 {
208                         IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForApplication ();
209                         object o = isf.AssemblyIdentity;
210                 }
211
212                 [Test]
213                 [ExpectedException (typeof (IsolatedStorageException))]
214                 public void GetUserStoreForApplication_DomainIdentity ()
215                 {
216                         IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForApplication ();
217                         object o = isf.DomainIdentity;
218                 }
219 #endif
220
221                 [Test]
222                 public void GetStore_Domain_Zone ()
223                 {
224                         IsolatedStorageScope scope = IsolatedStorageScope.User | IsolatedStorageScope.Domain | IsolatedStorageScope.Assembly;
225                         IsolatedStorageFile isf = IsolatedStorageFile.GetStore (scope, typeof (Zone), typeof (Zone));
226                         Assert.AreEqual (Int64.MaxValue, isf.MaximumSize, "MaximumSize");
227                         Assert.AreEqual (IsolatedStorageScope.User | IsolatedStorageScope.Domain | IsolatedStorageScope.Assembly, isf.Scope, "Scope");
228                         Assert.IsTrue ((isf.AssemblyIdentity is Zone), "AssemblyIdentity");
229                         Assert.IsTrue ((isf.AssemblyIdentity.ToString ().IndexOf ("MyComputer") > 0), "Zone - Assembly");
230                         Assert.IsTrue ((isf.DomainIdentity is Zone), "DomainIdentity");
231                         Assert.IsTrue ((isf.DomainIdentity.ToString ().IndexOf ("MyComputer") > 0), "Zone - Domain");
232                         Assert.IsTrue ((isf.CurrentSize >= 0), "CurrentSize");
233                 }
234
235                 [Test]
236                 [ExpectedException (typeof (IsolatedStorageException))]
237                 public void GetStore_Domain_NonPresentEvidences ()
238                 {
239                         IsolatedStorageScope scope = IsolatedStorageScope.User | IsolatedStorageScope.Domain | IsolatedStorageScope.Assembly;
240                         IsolatedStorageFile isf = IsolatedStorageFile.GetStore (scope, typeof (StrongName), typeof (Publisher));
241                 }
242
243                 [Test]
244                 public void GetStore_Assembly_NonPresentDomainEvidences ()
245                 {
246                         IsolatedStorageScope scope = IsolatedStorageScope.User | IsolatedStorageScope.Assembly;
247                         IsolatedStorageFile isf = IsolatedStorageFile.GetStore (scope, typeof (StrongName), typeof (Url));
248                         Assert.AreEqual (Int64.MaxValue, isf.MaximumSize, "MaximumSize");
249                         Assert.AreEqual (scope, isf.Scope, "Scope");
250                         Assert.IsTrue ((isf.AssemblyIdentity is Url), "AssemblyIdentity");
251                         // note: mono transforms the CodeBase into uppercase
252                         // for net 1.1 which uses file:// and not file:///
253                         string codebase = Assembly.GetExecutingAssembly ().CodeBase.ToUpper ().Substring (8);
254                         Assert.IsTrue ((isf.AssemblyIdentity.ToString ().ToUpper ().IndexOf (codebase) > 0), "Url");
255                         // DomainIdentity throws a InvalidOperationException
256                         Assert.IsTrue ((isf.CurrentSize >= 0), "CurrentSize");
257                 }
258
259                 [Test]
260                 [ExpectedException (typeof (ArgumentNullException))]
261                 public void GetStore_Domain_DomainNullObject ()
262                 {
263                         IsolatedStorageScope scope = IsolatedStorageScope.User | IsolatedStorageScope.Domain | IsolatedStorageScope.Assembly;
264                         IsolatedStorageFile isf = IsolatedStorageFile.GetStore (scope, (object)null, new Zone (SecurityZone.MyComputer));
265                 }
266
267                 [Test]
268                 [ExpectedException (typeof (ArgumentNullException))]
269                 public void GetStore_Domain_AssemblyNullObject ()
270                 {
271                         IsolatedStorageScope scope = IsolatedStorageScope.User | IsolatedStorageScope.Domain | IsolatedStorageScope.Assembly;
272                         IsolatedStorageFile isf = IsolatedStorageFile.GetStore (scope, new Zone (SecurityZone.MyComputer), (object)null);
273                 }
274
275                 [Test]
276                 public void GetStore_Assembly_DomainNullObject ()
277                 {
278                         IsolatedStorageScope scope = IsolatedStorageScope.User | IsolatedStorageScope.Assembly;
279                         IsolatedStorageFile isf = IsolatedStorageFile.GetStore (scope, (object)null, new Zone (SecurityZone.Internet));
280                         Assert.AreEqual (Int64.MaxValue, isf.MaximumSize, "MaximumSize");
281                         Assert.AreEqual (scope, isf.Scope, "Scope");
282                         Assert.IsTrue ((isf.AssemblyIdentity is Zone), "AssemblyIdentity");
283                         Assert.IsTrue ((isf.AssemblyIdentity.ToString ().IndexOf ("Internet") > 0), "Zone - Assembly");
284                         Assert.IsTrue ((isf.CurrentSize >= 0), "CurrentSize");
285                 }
286
287                 [Test]
288                 [ExpectedException (typeof (ArgumentNullException))]
289                 public void GetStore_Assembly_AssemblyNullObject ()
290                 {
291                         IsolatedStorageScope scope = IsolatedStorageScope.User | IsolatedStorageScope.Assembly;
292                         IsolatedStorageFile isf = IsolatedStorageFile.GetStore (scope, new Zone (SecurityZone.MyComputer), (object)null);
293                 }
294
295                 [Test]
296                 public void GetStore_Domain_ZoneObjectZoneObject ()
297                 {
298                         IsolatedStorageScope scope = IsolatedStorageScope.User | IsolatedStorageScope.Domain | IsolatedStorageScope.Assembly;
299                         IsolatedStorageFile isf = IsolatedStorageFile.GetStore (scope, new Zone (SecurityZone.Internet), new Zone (SecurityZone.Internet));
300                         Assert.AreEqual (Int64.MaxValue, isf.MaximumSize, "MaximumSize");
301                         Assert.AreEqual (scope, isf.Scope, "Scope");
302                         Assert.IsTrue ((isf.AssemblyIdentity is Zone), "AssemblyIdentity");
303                         Assert.IsTrue ((isf.AssemblyIdentity.ToString ().IndexOf ("Internet") > 0), "Zone - Assembly");
304                         Assert.IsTrue ((isf.DomainIdentity is Zone), "DomainIdentity");
305                         Assert.IsTrue ((isf.DomainIdentity.ToString ().IndexOf ("Internet") > 0), "Zone - Domain");
306                         Assert.IsTrue ((isf.CurrentSize >= 0), "CurrentSize");
307                 }
308 #if NET_2_0
309                 [Test]
310                 [ExpectedException (typeof (ArgumentNullException))]
311                 public void GetStore_Application_NullObject ()
312                 {
313                         IsolatedStorageScope scope = IsolatedStorageScope.User | IsolatedStorageScope.Application;
314                         IsolatedStorageFile isf = IsolatedStorageFile.GetStore (scope, (object)null);
315                 }
316
317                 [Test]
318                 [ExpectedException (typeof (IsolatedStorageException))]
319                 public void GetStore_Application_NullType ()
320                 {
321                         IsolatedStorageScope scope = IsolatedStorageScope.User | IsolatedStorageScope.Application;
322                         IsolatedStorageFile isf = IsolatedStorageFile.GetStore (scope, (Type)null);
323                         // again it's the lack of a manifest
324                 }
325 #endif
326
327                 [Test]
328                 public void GetStore_DomainScope_Evidences ()
329                 {
330                         IsolatedStorageScope scope = IsolatedStorageScope.User | IsolatedStorageScope.Domain | IsolatedStorageScope.Assembly;
331
332                         Evidence de = new Evidence ();
333                         de.AddHost (new Zone (SecurityZone.Internet));
334                         Evidence ae = new Evidence ();
335                         ae.AddHost (new Zone (SecurityZone.Intranet));
336                         IsolatedStorageFile isf = IsolatedStorageFile.GetStore (scope, de, typeof (Zone), ae, typeof (Zone));
337
338                         // Maximum size for Internet isn't (by default) Int64.MaxValue
339                         Assert.AreEqual (scope, isf.Scope, "Scope");
340                         Assert.IsTrue ((isf.AssemblyIdentity is Zone), "AssemblyIdentity");
341                         Assert.IsTrue ((isf.AssemblyIdentity.ToString ().IndexOf ("Intranet") > 0), "Zone - Assembly");
342                         Assert.IsTrue ((isf.DomainIdentity is Zone), "DomainIdentity");
343                         Assert.IsTrue ((isf.DomainIdentity.ToString ().IndexOf ("Internet") > 0), isf.DomainIdentity.ToString ()); //"Zone - Domain");
344                         Assert.IsTrue ((isf.CurrentSize >= 0), "CurrentSize");
345                 }
346
347                 [Test]
348                 [ExpectedException (typeof (ArgumentNullException))]
349                 public void GetStore_DomainScope_Evidence_NullAssemblyEvidence ()
350                 {
351                         IsolatedStorageScope scope = IsolatedStorageScope.User | IsolatedStorageScope.Domain | IsolatedStorageScope.Assembly;
352
353                         Evidence de = new Evidence ();
354                         de.AddHost (new Zone (SecurityZone.Internet));
355                         IsolatedStorageFile isf = IsolatedStorageFile.GetStore (scope, de, typeof (Zone), null, null);
356                 }
357
358                 [Test]
359                 [ExpectedException (typeof (ArgumentNullException))]
360                 public void GetStore_DomainScope_Evidence_NullDomainEvidence ()
361                 {
362                         IsolatedStorageScope scope = IsolatedStorageScope.User | IsolatedStorageScope.Domain | IsolatedStorageScope.Assembly;
363
364                         Evidence ae = new Evidence ();
365                         ae.AddHost (new Zone (SecurityZone.Internet));
366                         IsolatedStorageFile isf = IsolatedStorageFile.GetStore (scope, null, null, ae, typeof (Zone));
367                 }
368
369                 [Test]
370                 [ExpectedException (typeof (ArgumentNullException))]
371                 public void GetStore_AssemblyScope_Evidence_NullAssemblyEvidence ()
372                 {
373                         IsolatedStorageScope scope = IsolatedStorageScope.User | IsolatedStorageScope.Assembly;
374
375                         Evidence de = new Evidence ();
376                         de.AddHost (new Zone (SecurityZone.Internet));
377                         IsolatedStorageFile isf = IsolatedStorageFile.GetStore (scope, de, typeof (Zone), null, null);
378                 }
379
380                 [Test]
381                 public void GetStore_AssemblyScope_Evidence_NullDomainEvidence ()
382                 {
383                         IsolatedStorageScope scope = IsolatedStorageScope.User | IsolatedStorageScope.Assembly;
384
385                         Evidence ae = new Evidence ();
386                         ae.AddHost (new Zone (SecurityZone.Internet));
387                         IsolatedStorageFile isf = IsolatedStorageFile.GetStore (scope, null, null, ae, typeof (Zone));
388                 }
389         }
390 }