[bcl] Remove NET_4_0 defines from class libs
[mono.git] / mcs / class / corlib / Test / System.IO.IsolatedStorage / IsolatedStorageTest.cs
1 //
2 // IsolatedStorageTest.cs - Unit Tests for abstract IsolatedStorage class
3 //
4 // Author:
5 //      Sebastien Pouliot  <sebastien@ximian.com>
6 //
7 // Copyright (C) 2005 Novell Inc. (http://www.novell.com)
8 //
9 // Permission is hereby granted, free of charge, to any person obtaining
10 // a copy of this software and associated documentation files (the
11 // "Software"), to deal in the Software without restriction, including
12 // without limitation the rights to use, copy, modify, merge, publish,
13 // distribute, sublicense, and/or sell copies of the Software, and to
14 // permit persons to whom the Software is furnished to do so, subject to
15 // the following conditions:
16 // 
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
19 // 
20 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
24 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27 //
28
29 using System;
30 using System.IO;
31 using System.Security;
32 using System.Security.Permissions;
33 using System.IO.IsolatedStorage;
34
35 using NUnit.Framework;
36
37 namespace MonoTests.System.IO.IsolatedStorageTest {
38
39         // note: IsolatedStorage is abstract so we create a
40         // non-abstract class to test it
41
42         // naming a class with the same name as a namespace is a BAD idea
43         public class NonAbstractIsolatedStorage : global::System.IO.IsolatedStorage.IsolatedStorage
44         {
45                 public NonAbstractIsolatedStorage ()
46                 {
47                         // no InitStore here
48                 }
49
50                 public NonAbstractIsolatedStorage (IsolatedStorageScope scope, Type domain, Type assembly)
51                 {
52                         InitStore (scope, domain, assembly);
53                 }
54
55                 public NonAbstractIsolatedStorage(IsolatedStorageScope scope, Type application)
56                 {
57                         InitStore (scope, application);
58                 }
59
60                 protected override IsolatedStoragePermission GetPermission (PermissionSet ps)
61                 {
62                         throw new NotImplementedException();
63                 }
64
65                 public override void Remove ()
66                 {
67                         throw new NotImplementedException();
68                 }
69
70                 public char PublicSeparatorExternal {
71                         get { return base.SeparatorExternal; }
72                 }
73
74                 public char PublicSeparatorInternal {
75                         get { return base.SeparatorInternal; }
76                 }
77         }
78
79         [TestFixture]
80         public class IsolatedStorageTest {
81
82                 [Test]
83                 [ExpectedException (typeof (ArgumentException))]
84                 public void IsolatedStorage_Bad_Null ()
85                 {
86                         IsolatedStorageScope bad = (IsolatedStorageScope)Int32.MinValue;
87                         NonAbstractIsolatedStorage nais = new NonAbstractIsolatedStorage (bad, null, null);
88                 }
89
90                 [Test]
91                 [ExpectedException (typeof (ArgumentException))]
92                 public void IsolatedStorage_None_Null ()
93                 {
94                         NonAbstractIsolatedStorage nais = new NonAbstractIsolatedStorage (IsolatedStorageScope.None, null, null);
95                 }
96
97                 [Test]
98                 [ExpectedException (typeof (ArgumentException))]
99                 public void IsolatedStorage_None ()
100                 {
101                         Type t = typeof (object);
102                         NonAbstractIsolatedStorage nais = new NonAbstractIsolatedStorage (IsolatedStorageScope.None, t, t);
103                 }
104
105                 [Test]
106                 [ExpectedException (typeof (ArgumentException))]
107                 public void IsolatedStorage_User ()
108                 {
109                         NonAbstractIsolatedStorage nais = new NonAbstractIsolatedStorage (IsolatedStorageScope.User, null, null);
110                 }
111
112                 [Test]
113                 [ExpectedException (typeof (ArgumentException))]
114                 public void IsolatedStorage_Domain ()
115                 {
116                         NonAbstractIsolatedStorage nais = new NonAbstractIsolatedStorage (IsolatedStorageScope.Domain, null, null);
117                 }
118
119                 [Test]
120                 [ExpectedException (typeof (ArgumentException))]
121                 public void IsolatedStorage_Assembly ()
122                 {
123                         NonAbstractIsolatedStorage nais = new NonAbstractIsolatedStorage (IsolatedStorageScope.Assembly, null, null);
124                 }
125
126                 [Test]
127                 [ExpectedException (typeof (ArgumentException))]
128                 public void IsolatedStorage_Roaming ()
129                 {
130                         NonAbstractIsolatedStorage nais = new NonAbstractIsolatedStorage (IsolatedStorageScope.Roaming, null, null);
131                 }
132
133                 [Test]
134                 [ExpectedException (typeof (ArgumentException))]
135                 public void IsolatedStorage_Machine ()
136                 {
137                         NonAbstractIsolatedStorage nais = new NonAbstractIsolatedStorage (IsolatedStorageScope.Machine, null, null);
138                 }
139
140                 [Test]
141 #if !MOBILE
142                 [ExpectedException (typeof (IsolatedStorageException))]
143 #endif
144                 public void IsolatedStorage_Application ()
145                 {
146                         NonAbstractIsolatedStorage nais = new NonAbstractIsolatedStorage (IsolatedStorageScope.Application, null);
147                 }
148
149                 [Test]
150                 [ExpectedException (typeof (NotImplementedException))]
151                 public void IsolatedStorage_AssemblyUser ()
152                 {
153                         NonAbstractIsolatedStorage nais = new NonAbstractIsolatedStorage (IsolatedStorageScope.Assembly | IsolatedStorageScope.User, null, null);
154                 }
155
156                 [Test]
157                 [ExpectedException (typeof (NotImplementedException))]
158                 public void IsolatedStorage_AssemblyUserDomain ()
159                 {
160                         NonAbstractIsolatedStorage nais = new NonAbstractIsolatedStorage (IsolatedStorageScope.Assembly | IsolatedStorageScope.User | IsolatedStorageScope.Domain, null, null);
161                 }
162
163                 [Test]
164                 public void IsolatedStorage ()
165                 {
166                         NonAbstractIsolatedStorage nais = new NonAbstractIsolatedStorage ();
167                         Assert.AreEqual (IsolatedStorageScope.None, nais.Scope, "Scope");
168                         Assert.AreEqual (Path.DirectorySeparatorChar, nais.PublicSeparatorExternal, "SeparatorExternal");
169                         Assert.AreEqual ('.', nais.PublicSeparatorInternal, "SeparatorInternal");
170                 }
171                 [Test]
172                 [ExpectedException (typeof (InvalidOperationException))]
173                 public void IsolatedStorage_ApplicationIdentity ()
174                 {
175                         NonAbstractIsolatedStorage nais = new NonAbstractIsolatedStorage ();
176                         object o = nais.ApplicationIdentity;
177                 }
178
179                 [Test]
180                 [ExpectedException (typeof (InvalidOperationException))]
181                 public void IsolatedStorage_AssemblyIdentity ()
182                 {
183                         NonAbstractIsolatedStorage nais = new NonAbstractIsolatedStorage ();
184                         object o = nais.AssemblyIdentity;
185                 }
186
187                 [Test]
188                 [ExpectedException (typeof (InvalidOperationException))]
189                 public void IsolatedStorage_CurrentSize ()
190                 {
191                         NonAbstractIsolatedStorage nais = new NonAbstractIsolatedStorage ();
192                         ulong ul = nais.CurrentSize;
193                 }
194
195                 [Test]
196                 [ExpectedException (typeof (InvalidOperationException))]
197                 public void IsolatedStorage_DomainIdentity ()
198                 {
199                         NonAbstractIsolatedStorage nais = new NonAbstractIsolatedStorage ();
200                         object o = nais.DomainIdentity;
201                 }
202
203                 [Test]
204                 [ExpectedException (typeof (InvalidOperationException))]
205                 public void IsolatedStorage_MaximumSize ()
206                 {
207                         NonAbstractIsolatedStorage nais = new NonAbstractIsolatedStorage ();
208                         ulong ul = nais.MaximumSize;
209                 }
210                 
211                 [Test]
212                 public void MultiLevel ()
213                 {
214                         // see bug #4101
215                         IsolatedStorageFile isf;
216 #if MOBILE
217                         isf = IsolatedStorageFile.GetUserStoreForApplication ();
218 #else
219                         isf = IsolatedStorageFile.GetStore (IsolatedStorageScope.User |  IsolatedStorageScope.Assembly | IsolatedStorageScope.Domain,
220                                                 typeof (global::System.Security.Policy.Url), typeof (global::System.Security.Policy.Url));
221 #endif
222
223                         try {
224                                 isf.CreateDirectory ("dir1");
225                                 string [] dirs = isf.GetDirectoryNames ("*");
226                                 Assert.AreEqual (1, dirs.Length, "1a");
227                                 Assert.AreEqual ("dir1", dirs [0], "1b");
228         
229                                 isf.CreateDirectory ("dir1/test");
230                                 dirs = isf.GetDirectoryNames ("dir1/*");
231                                 Assert.AreEqual (1, dirs.Length, "2a");
232                                 Assert.AreEqual ("test", dirs [0], "2b");
233         
234                                 isf.CreateDirectory ("dir1/test/test2a");
235                                 isf.CreateDirectory ("dir1/test/test2b");
236                                 dirs = isf.GetDirectoryNames ("dir1/test/*");
237                                 Assert.AreEqual (2, dirs.Length, "3a");
238                                 Assert.AreEqual ("test2a", dirs [0], "3b");
239                                 Assert.AreEqual ("test2b", dirs [1], "3c");
240                         } finally {
241                                 isf.DeleteDirectory ("dir1/test/test2a");
242                                 isf.DeleteDirectory ("dir1/test/test2b");
243                                 isf.DeleteDirectory ("dir1/test");
244                                 isf.DeleteDirectory ("dir1");
245                         }
246                 }
247
248                 [Test]
249                 [ExpectedException (typeof (InvalidOperationException))]
250                 public void IsolatedStorage_UsedSize ()
251                 {
252                         NonAbstractIsolatedStorage nais = new NonAbstractIsolatedStorage ();
253                         Console.WriteLine (nais.UsedSize);
254                 }
255
256                 [Test]
257                 [ExpectedException (typeof (InvalidOperationException))]
258                 public void IsolatedStorage_AvailableFreeSpace ()
259                 {
260                         NonAbstractIsolatedStorage nais = new NonAbstractIsolatedStorage ();
261                         Console.WriteLine (nais.AvailableFreeSpace);
262                 }
263
264                 [Test]
265                 [ExpectedException (typeof (InvalidOperationException))]
266                 public void IsolatedStorage_Quota ()
267                 {
268                         NonAbstractIsolatedStorage nais = new NonAbstractIsolatedStorage ();
269                         Console.WriteLine (nais.Quota);
270                 }
271
272                 [Test]
273                 public void IsolatedStorage_IncreaseQuotaTo ()
274                 {
275                         NonAbstractIsolatedStorage nais = new NonAbstractIsolatedStorage ();
276                         Assert.AreEqual (false, nais.IncreaseQuotaTo (-10), "#A0");
277                         Assert.AreEqual (false, nais.IncreaseQuotaTo (0), "#A1");
278                         Assert.AreEqual (false, nais.IncreaseQuotaTo (100), "#A2");
279                 }
280         }
281 }