Merge pull request #194 from QuickJack/master
[mono.git] / mcs / class / System / Test / System.CodeDom.Compiler / TempFileCollectionCas.cs
1 //
2 // TempFileCollectionCas.cs 
3 //      - CAS unit tests for System.CodeDom.Compiler.TempFileCollection
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 NUnit.Framework;
31
32 using System;
33 using System.CodeDom.Compiler;
34 using System.Collections;
35 using System.IO;
36 using System.Reflection;
37 using System.Security;
38 using System.Security.Permissions;
39
40 namespace MonoCasTests.System.CodeDom.Compiler {
41
42         [TestFixture]
43         [Category ("CAS")]
44         public class TempFileCollectionCas {
45
46                 private string temp;
47                 private string[] array;
48
49                 [TestFixtureSetUp]
50                 public void FixtureSetUp ()
51                 {
52                         // at full trust
53                         temp = Path.GetTempPath ();
54                         array = new string[1];
55                 }
56
57                 [SetUp]
58                 public void SetUp ()
59                 {
60                         if (!SecurityManager.SecurityEnabled)
61                                 Assert.Ignore ("SecurityManager.SecurityEnabled is OFF");
62                 }
63
64                 [Test]
65                 [PermissionSet (SecurityAction.Deny, Unrestricted = true)]
66                 public void Constructor0_Deny_Unrestricted ()
67                 {
68                         TempFileCollection tfc = new TempFileCollection ();
69                         Assert.AreEqual (0, tfc.Count, "Count");
70                         Assert.IsFalse (tfc.KeepFiles, "KeepFiles");
71                         Assert.AreEqual (String.Empty, tfc.TempDir, "TempDir");
72                         tfc.AddFile ("main.cs", false);
73                         tfc.CopyTo (array, 0);
74                         tfc.Delete ();
75                         (tfc as IDisposable).Dispose ();
76                 }
77
78                 [Test]
79                 [PermissionSet (SecurityAction.Deny, Unrestricted = true)]
80                 public void Constructor1_Deny_Unrestricted ()
81                 {
82                         TempFileCollection tfc = new TempFileCollection (temp);
83                         Assert.AreEqual (0, tfc.Count, "Count");
84                         Assert.IsFalse (tfc.KeepFiles, "KeepFiles");
85                         Assert.AreEqual (temp, tfc.TempDir, "TempDir");
86                         tfc.AddFile ("main.cs", false);
87                         tfc.CopyTo (array, 0);
88                         tfc.Delete ();
89                         (tfc as IDisposable).Dispose ();
90                 }
91
92                 [Test]
93                 [PermissionSet (SecurityAction.Deny, Unrestricted = true)]
94                 public void Constructor2_Deny_Unrestricted ()
95                 {
96                         TempFileCollection tfc = new TempFileCollection (temp, true);
97                         Assert.AreEqual (0, tfc.Count, "Count");
98                         Assert.IsTrue (tfc.KeepFiles, "KeepFiles");
99                         Assert.AreEqual (temp, tfc.TempDir, "TempDir");
100                         tfc.AddFile ("main.cs", false);
101                         tfc.CopyTo (array, 0);
102                         tfc.Delete ();
103                         (tfc as IDisposable).Dispose ();
104                 }
105
106
107                 [EnvironmentPermission (SecurityAction.PermitOnly, Unrestricted = true)]
108                 private void Cache (TempFileCollection tfc)
109                 {
110                         Assert.IsNotNull (tfc.BasePath, "BasePath-Restricted");
111                         // no failure because the BasePath was calculated when 
112                         // unrestricted and cached for further calls. This is
113                         // design tradeoff between security and performance but
114                         // shouldn't occurs much in real life (but it's worth
115                         // keeping in mind ;-).
116                 }
117
118                 [Test]
119                 public void BasePath_Caching ()
120                 {
121                         TempFileCollection tfc = new TempFileCollection ();
122                         Assert.IsNotNull (tfc.BasePath, "BasePath-Unrestricted");
123                         Cache (tfc);
124                 }
125
126                 [Test]
127                 [EnvironmentPermission (SecurityAction.PermitOnly, Unrestricted = true)]
128                 [ExpectedException (typeof (SecurityException))]
129                 public void BasePath_PermitOnly_EnvironmentPermission ()
130                 {
131                         TempFileCollection tfc = new TempFileCollection ();
132                         // good but not enough
133                         Assert.IsNotNull (tfc.BasePath, "BasePath");
134                 }
135
136                 [Test]
137                 [EnvironmentPermission (SecurityAction.Deny, Read = "Mono")]
138                 [ExpectedException (typeof (SecurityException))]
139 #if ONLY_1_1
140                 [Category ("NotDotNet")] // MS doesn't check for Environment under 1.x
141 #endif
142                 public void BasePath_Deny_EnvironmentPermission ()
143                 {
144                         TempFileCollection tfc = new TempFileCollection ();
145                         // requires Unrestricted Environment access
146                         Assert.IsNotNull (tfc.BasePath, "BasePath");
147                 }
148
149                 [Test]
150                 [FileIOPermission (SecurityAction.PermitOnly, Unrestricted = true)]
151                 [ExpectedException (typeof (SecurityException))]
152 #if ONLY_1_1
153                 [Category ("NotDotNet")] // MS doesn't check for Environment under 1.x
154 #endif
155                 public void BasePath_PermitOnly_FileIOPermission ()
156                 {
157                         TempFileCollection tfc = new TempFileCollection ();
158                         // good but not enough
159                         Assert.IsNotNull (tfc.BasePath, "BasePath");
160                 }
161
162                 [Test]
163                 [FileIOPermission (SecurityAction.Deny, Unrestricted = true)]
164                 [ExpectedException (typeof (SecurityException))]
165                 public void BasePath_Deny_FileIOPermission ()
166                 {
167                         TempFileCollection tfc = new TempFileCollection ();
168                         // requires path access
169                         Assert.IsNotNull (tfc.BasePath, "BasePath");
170                 }
171
172                 [Test]
173                 [EnvironmentPermission (SecurityAction.PermitOnly, Unrestricted = true)]
174                 [ExpectedException (typeof (SecurityException))]
175                 public void AddExtension_PermitOnly_EnvironmentPermission ()
176                 {
177                         TempFileCollection tfc = new TempFileCollection ();
178                         // good but not enough
179                         tfc.AddExtension (".cs");
180                 }
181
182                 [Test]
183                 [EnvironmentPermission (SecurityAction.Deny, Read = "Mono")]
184                 [ExpectedException (typeof (SecurityException))]
185 #if ONLY_1_1
186                 [Category ("NotDotNet")] // MS doesn't check for Environment under 1.x
187 #endif
188                 public void AddExtension_Deny_EnvironmentPermission ()
189                 {
190                         TempFileCollection tfc = new TempFileCollection ();
191                         // requires Unrestricted Environment access
192                         tfc.AddExtension (".cs");
193                 }
194
195                 [Test]
196                 [FileIOPermission (SecurityAction.PermitOnly, Unrestricted = true)]
197                 [ExpectedException (typeof (SecurityException))]
198 #if ONLY_1_1
199                 [Category ("NotDotNet")] // MS doesn't check for Environment under 1.x
200 #endif
201                 public void AddExtension_PermitOnly_FileIOPermission ()
202                 {
203                         TempFileCollection tfc = new TempFileCollection ();
204                         // good but not enough
205                         tfc.AddExtension (".cs");
206                 }
207
208                 [Test]
209                 [FileIOPermission (SecurityAction.Deny, Unrestricted = true)]
210                 [ExpectedException (typeof (SecurityException))]
211                 public void AddExtension_Deny_FileIOPermission ()
212                 {
213                         TempFileCollection tfc = new TempFileCollection ();
214                         // requires path access
215                         tfc.AddExtension (".cs");
216                 }
217
218                 [Test]
219                 [EnvironmentPermission (SecurityAction.PermitOnly, Unrestricted = true)]
220                 [ExpectedException (typeof (SecurityException))]
221                 public void AddExtension2_PermitOnly_EnvironmentPermission ()
222                 {
223                         TempFileCollection tfc = new TempFileCollection ();
224                         // good but not enough
225                         tfc.AddExtension (".cx", true);
226                 }
227
228                 [Test]
229                 [EnvironmentPermission (SecurityAction.Deny, Read = "Mono")]
230                 [ExpectedException (typeof (SecurityException))]
231 #if ONLY_1_1
232                 [Category ("NotDotNet")] // MS doesn't check for Environment under 1.x
233 #endif
234                 public void AddExtension2_Deny_EnvironmentPermission ()
235                 {
236                         TempFileCollection tfc = new TempFileCollection ();
237                         // requires Unrestricted Environment access
238                         tfc.AddExtension (".cx", true);
239                 }
240
241                 [Test]
242                 [FileIOPermission (SecurityAction.PermitOnly, Unrestricted = true)]
243                 [ExpectedException (typeof (SecurityException))]
244 #if ONLY_1_1
245                 [Category ("NotDotNet")] // MS doesn't check for Environment under 1.x
246 #endif
247                 public void AddExtension2_PermitOnly_FileIOPermission ()
248                 {
249                         TempFileCollection tfc = new TempFileCollection ();
250                         // good but not enough
251                         tfc.AddExtension (".cx", true);
252                 }
253
254                 [Test]
255                 [FileIOPermission (SecurityAction.Deny, Unrestricted = true)]
256                 [ExpectedException (typeof (SecurityException))]
257                 public void AddExtension2_Deny_FileIOPermission ()
258                 {
259                         TempFileCollection tfc = new TempFileCollection ();
260                         // requires path access
261                         tfc.AddExtension (".cx", true);
262                 }
263
264                 [Test]
265                 [FileIOPermission (SecurityAction.PermitOnly, Unrestricted = true)]
266                 [EnvironmentPermission (SecurityAction.PermitOnly, Unrestricted = true)]
267                 public void PermitOnly_FileIOPermission_EnvironmentPermission ()
268                 {
269                         TempFileCollection tfc = new TempFileCollection ();
270                         // ok
271                         Assert.IsNotNull (tfc.BasePath, "BasePath");
272                         tfc.AddExtension (".cs");
273                         tfc.AddExtension (".cx", true);
274                         // both AddExtension methods depends on BasePath
275                         Assert.AreEqual (String.Empty, tfc.TempDir, "TempDir");
276                         // and that last one is *important*
277                 }
278
279                 [Test]
280                 [PermissionSet (SecurityAction.Deny, Unrestricted = true)]
281                 public void ICollection_Deny_Unrestricted ()
282                 {
283                         ICollection coll = (ICollection) new TempFileCollection ();
284                         Assert.AreEqual (0, coll.Count, "Count");
285                         Assert.IsNull (coll.SyncRoot, "SyncRoot");
286                         Assert.IsFalse (coll.IsSynchronized, "IsSynchronized");
287                         coll.CopyTo (array, 0);
288                 }
289
290                 [Test]
291                 [PermissionSet (SecurityAction.Deny, Unrestricted = true)]
292                 public void IEnumerable_Deny_Unrestricted ()
293                 {
294                         IEnumerable e = (IEnumerable) new TempFileCollection ();
295                         Assert.IsNotNull (e.GetEnumerator (), "GetEnumerator");
296                 }
297
298                 [Test]
299                 public void LinkDemand_No_Restriction ()
300                 {
301                         ConstructorInfo ci = typeof (TempFileCollection).GetConstructor (new Type[0]);
302                         Assert.IsNotNull (ci, "default .ctor");
303                         Assert.IsNotNull (ci.Invoke (null), "invoke");
304                 }
305
306                 [Test]
307                 [EnvironmentPermission (SecurityAction.Deny, Read = "Mono")]
308                 [ExpectedException (typeof (SecurityException))]
309                 public void LinkDemand_Deny_Unrestricted ()
310                 {
311                         // denying anything results in a non unrestricted permission set
312                         ConstructorInfo ci = typeof (TempFileCollection).GetConstructor (new Type[0]);
313                         Assert.IsNotNull (ci, "default .ctor");
314                         Assert.IsNotNull (ci.Invoke (null), "invoke");
315                 }
316         }
317 }