[System] Implement a helper function to create unique temporary directories and use...
[mono-project.git] / mcs / class / System / Test / System.CodeDom.Compiler / TempFileCollectionCas.cs
blob4fb1639aa760a3a3cd6771b8e96ecabb10fd30ab
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.
30 using NUnit.Framework;
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;
40 using MonoTests.Helpers;
42 namespace MonoCasTests.System.CodeDom.Compiler {
44 [TestFixture]
45 [Category ("CAS")]
46 public class TempFileCollectionCas {
48 private TempDirectory _temp;
49 private string temp;
50 private string[] array;
52 [TestFixtureSetUp]
53 public void FixtureSetUp ()
55 // at full trust
56 _temp = new TempDirectory ();
57 temp = _temp.Path;
58 array = new string[1];
61 [SetUp]
62 public void SetUp ()
64 if (!SecurityManager.SecurityEnabled)
65 Assert.Ignore ("SecurityManager.SecurityEnabled is OFF");
68 [Test]
69 [PermissionSet (SecurityAction.Deny, Unrestricted = true)]
70 public void Constructor0_Deny_Unrestricted ()
72 TempFileCollection tfc = new TempFileCollection ();
73 Assert.AreEqual (0, tfc.Count, "Count");
74 Assert.IsFalse (tfc.KeepFiles, "KeepFiles");
75 Assert.AreEqual (String.Empty, tfc.TempDir, "TempDir");
76 tfc.AddFile ("main.cs", false);
77 tfc.CopyTo (array, 0);
78 tfc.Delete ();
79 (tfc as IDisposable).Dispose ();
82 [Test]
83 [PermissionSet (SecurityAction.Deny, Unrestricted = true)]
84 public void Constructor1_Deny_Unrestricted ()
86 TempFileCollection tfc = new TempFileCollection (temp);
87 Assert.AreEqual (0, tfc.Count, "Count");
88 Assert.IsFalse (tfc.KeepFiles, "KeepFiles");
89 Assert.AreEqual (temp, tfc.TempDir, "TempDir");
90 tfc.AddFile ("main.cs", false);
91 tfc.CopyTo (array, 0);
92 tfc.Delete ();
93 (tfc as IDisposable).Dispose ();
96 [Test]
97 [PermissionSet (SecurityAction.Deny, Unrestricted = true)]
98 public void Constructor2_Deny_Unrestricted ()
100 TempFileCollection tfc = new TempFileCollection (temp, true);
101 Assert.AreEqual (0, tfc.Count, "Count");
102 Assert.IsTrue (tfc.KeepFiles, "KeepFiles");
103 Assert.AreEqual (temp, tfc.TempDir, "TempDir");
104 tfc.AddFile ("main.cs", false);
105 tfc.CopyTo (array, 0);
106 tfc.Delete ();
107 (tfc as IDisposable).Dispose ();
111 [EnvironmentPermission (SecurityAction.PermitOnly, Unrestricted = true)]
112 private void Cache (TempFileCollection tfc)
114 Assert.IsNotNull (tfc.BasePath, "BasePath-Restricted");
115 // no failure because the BasePath was calculated when
116 // unrestricted and cached for further calls. This is
117 // design tradeoff between security and performance but
118 // shouldn't occurs much in real life (but it's worth
119 // keeping in mind ;-).
122 [Test]
123 public void BasePath_Caching ()
125 TempFileCollection tfc = new TempFileCollection ();
126 Assert.IsNotNull (tfc.BasePath, "BasePath-Unrestricted");
127 Cache (tfc);
130 [Test]
131 [EnvironmentPermission (SecurityAction.PermitOnly, Unrestricted = true)]
132 [ExpectedException (typeof (SecurityException))]
133 public void BasePath_PermitOnly_EnvironmentPermission ()
135 TempFileCollection tfc = new TempFileCollection ();
136 // good but not enough
137 Assert.IsNotNull (tfc.BasePath, "BasePath");
140 [Test]
141 [EnvironmentPermission (SecurityAction.Deny, Read = "Mono")]
142 [ExpectedException (typeof (SecurityException))]
143 public void BasePath_Deny_EnvironmentPermission ()
145 TempFileCollection tfc = new TempFileCollection ();
146 // requires Unrestricted Environment access
147 Assert.IsNotNull (tfc.BasePath, "BasePath");
150 [Test]
151 [FileIOPermission (SecurityAction.PermitOnly, Unrestricted = true)]
152 [ExpectedException (typeof (SecurityException))]
153 public void BasePath_PermitOnly_FileIOPermission ()
155 TempFileCollection tfc = new TempFileCollection ();
156 // good but not enough
157 Assert.IsNotNull (tfc.BasePath, "BasePath");
160 [Test]
161 [FileIOPermission (SecurityAction.Deny, Unrestricted = true)]
162 [ExpectedException (typeof (SecurityException))]
163 public void BasePath_Deny_FileIOPermission ()
165 TempFileCollection tfc = new TempFileCollection ();
166 // requires path access
167 Assert.IsNotNull (tfc.BasePath, "BasePath");
170 [Test]
171 [EnvironmentPermission (SecurityAction.PermitOnly, Unrestricted = true)]
172 [ExpectedException (typeof (SecurityException))]
173 public void AddExtension_PermitOnly_EnvironmentPermission ()
175 TempFileCollection tfc = new TempFileCollection ();
176 // good but not enough
177 tfc.AddExtension (".cs");
180 [Test]
181 [EnvironmentPermission (SecurityAction.Deny, Read = "Mono")]
182 [ExpectedException (typeof (SecurityException))]
183 public void AddExtension_Deny_EnvironmentPermission ()
185 TempFileCollection tfc = new TempFileCollection ();
186 // requires Unrestricted Environment access
187 tfc.AddExtension (".cs");
190 [Test]
191 [FileIOPermission (SecurityAction.PermitOnly, Unrestricted = true)]
192 [ExpectedException (typeof (SecurityException))]
193 public void AddExtension_PermitOnly_FileIOPermission ()
195 TempFileCollection tfc = new TempFileCollection ();
196 // good but not enough
197 tfc.AddExtension (".cs");
200 [Test]
201 [FileIOPermission (SecurityAction.Deny, Unrestricted = true)]
202 [ExpectedException (typeof (SecurityException))]
203 public void AddExtension_Deny_FileIOPermission ()
205 TempFileCollection tfc = new TempFileCollection ();
206 // requires path access
207 tfc.AddExtension (".cs");
210 [Test]
211 [EnvironmentPermission (SecurityAction.PermitOnly, Unrestricted = true)]
212 [ExpectedException (typeof (SecurityException))]
213 public void AddExtension2_PermitOnly_EnvironmentPermission ()
215 TempFileCollection tfc = new TempFileCollection ();
216 // good but not enough
217 tfc.AddExtension (".cx", true);
220 [Test]
221 [EnvironmentPermission (SecurityAction.Deny, Read = "Mono")]
222 [ExpectedException (typeof (SecurityException))]
223 public void AddExtension2_Deny_EnvironmentPermission ()
225 TempFileCollection tfc = new TempFileCollection ();
226 // requires Unrestricted Environment access
227 tfc.AddExtension (".cx", true);
230 [Test]
231 [FileIOPermission (SecurityAction.PermitOnly, Unrestricted = true)]
232 [ExpectedException (typeof (SecurityException))]
233 public void AddExtension2_PermitOnly_FileIOPermission ()
235 TempFileCollection tfc = new TempFileCollection ();
236 // good but not enough
237 tfc.AddExtension (".cx", true);
240 [Test]
241 [FileIOPermission (SecurityAction.Deny, Unrestricted = true)]
242 [ExpectedException (typeof (SecurityException))]
243 public void AddExtension2_Deny_FileIOPermission ()
245 TempFileCollection tfc = new TempFileCollection ();
246 // requires path access
247 tfc.AddExtension (".cx", true);
250 [Test]
251 [FileIOPermission (SecurityAction.PermitOnly, Unrestricted = true)]
252 [EnvironmentPermission (SecurityAction.PermitOnly, Unrestricted = true)]
253 public void PermitOnly_FileIOPermission_EnvironmentPermission ()
255 TempFileCollection tfc = new TempFileCollection ();
256 // ok
257 Assert.IsNotNull (tfc.BasePath, "BasePath");
258 tfc.AddExtension (".cs");
259 tfc.AddExtension (".cx", true);
260 // both AddExtension methods depends on BasePath
261 Assert.AreEqual (String.Empty, tfc.TempDir, "TempDir");
262 // and that last one is *important*
265 [Test]
266 [PermissionSet (SecurityAction.Deny, Unrestricted = true)]
267 public void ICollection_Deny_Unrestricted ()
269 ICollection coll = (ICollection) new TempFileCollection ();
270 Assert.AreEqual (0, coll.Count, "Count");
271 Assert.IsNull (coll.SyncRoot, "SyncRoot");
272 Assert.IsFalse (coll.IsSynchronized, "IsSynchronized");
273 coll.CopyTo (array, 0);
276 [Test]
277 [PermissionSet (SecurityAction.Deny, Unrestricted = true)]
278 public void IEnumerable_Deny_Unrestricted ()
280 IEnumerable e = (IEnumerable) new TempFileCollection ();
281 Assert.IsNotNull (e.GetEnumerator (), "GetEnumerator");
284 [Test]
285 public void LinkDemand_No_Restriction ()
287 ConstructorInfo ci = typeof (TempFileCollection).GetConstructor (new Type[0]);
288 Assert.IsNotNull (ci, "default .ctor");
289 Assert.IsNotNull (ci.Invoke (null), "invoke");
292 [Test]
293 [EnvironmentPermission (SecurityAction.Deny, Read = "Mono")]
294 [ExpectedException (typeof (SecurityException))]
295 public void LinkDemand_Deny_Unrestricted ()
297 // denying anything results in a non unrestricted permission set
298 ConstructorInfo ci = typeof (TempFileCollection).GetConstructor (new Type[0]);
299 Assert.IsNotNull (ci, "default .ctor");
300 Assert.IsNotNull (ci.Invoke (null), "invoke");