2 // TempFileCollectionCas.cs
3 // - CAS unit tests for System.CodeDom.Compiler.TempFileCollection
6 // Sebastien Pouliot <sebastien@ximian.com>
8 // Copyright (C) 2005 Novell, Inc (http://www.novell.com)
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:
18 // The above copyright notice and this permission notice shall be
19 // included in all copies or substantial portions of the Software.
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;
33 using System.CodeDom.Compiler;
34 using System.Collections;
36 using System.Reflection;
37 using System.Security;
38 using System.Security.Permissions;
40 namespace MonoCasTests.System.CodeDom.Compiler {
44 public class TempFileCollectionCas {
47 private string[] array;
50 public void FixtureSetUp ()
53 temp = Path.GetTempPath ();
54 array = new string[1];
60 if (!SecurityManager.SecurityEnabled)
61 Assert.Ignore ("SecurityManager.SecurityEnabled is OFF");
65 [PermissionSet (SecurityAction.Deny, Unrestricted = true)]
66 public void Constructor0_Deny_Unrestricted ()
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);
75 (tfc as IDisposable).Dispose ();
79 [PermissionSet (SecurityAction.Deny, Unrestricted = true)]
80 public void Constructor1_Deny_Unrestricted ()
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);
89 (tfc as IDisposable).Dispose ();
93 [PermissionSet (SecurityAction.Deny, Unrestricted = true)]
94 public void Constructor2_Deny_Unrestricted ()
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);
103 (tfc as IDisposable).Dispose ();
107 [EnvironmentPermission (SecurityAction.PermitOnly, Unrestricted = true)]
108 private void Cache (TempFileCollection tfc)
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 ;-).
119 public void BasePath_Caching ()
121 TempFileCollection tfc = new TempFileCollection ();
122 Assert.IsNotNull (tfc.BasePath, "BasePath-Unrestricted");
127 [EnvironmentPermission (SecurityAction.PermitOnly, Unrestricted = true)]
128 [ExpectedException (typeof (SecurityException))]
130 [Category ("NotDotNet")] // MS doesn't check for Environment under 1.x
132 public void BasePath_PermitOnly_EnvironmentPermission ()
134 TempFileCollection tfc = new TempFileCollection ();
135 // good but not enough
136 Assert.IsNotNull (tfc.BasePath, "BasePath");
140 [EnvironmentPermission (SecurityAction.Deny, Read = "Mono")]
141 [ExpectedException (typeof (SecurityException))]
142 public void BasePath_Deny_EnvironmentPermission ()
144 TempFileCollection tfc = new TempFileCollection ();
145 // requires Unrestricted Environment access
146 Assert.IsNotNull (tfc.BasePath, "BasePath");
150 [FileIOPermission (SecurityAction.PermitOnly, Unrestricted = true)]
151 [ExpectedException (typeof (SecurityException))]
153 [Category ("NotDotNet")] // MS doesn't check for Environment under 1.x
155 public void BasePath_PermitOnly_FileIOPermission ()
157 TempFileCollection tfc = new TempFileCollection ();
158 // good but not enough
159 Assert.IsNotNull (tfc.BasePath, "BasePath");
163 [FileIOPermission (SecurityAction.Deny, Unrestricted = true)]
164 [ExpectedException (typeof (SecurityException))]
165 public void BasePath_Deny_FileIOPermission ()
167 TempFileCollection tfc = new TempFileCollection ();
168 // requires path access
169 Assert.IsNotNull (tfc.BasePath, "BasePath");
173 [EnvironmentPermission (SecurityAction.PermitOnly, Unrestricted = true)]
174 [ExpectedException (typeof (SecurityException))]
175 public void AddExtension_PermitOnly_EnvironmentPermission ()
177 TempFileCollection tfc = new TempFileCollection ();
178 // good but not enough
179 tfc.AddExtension (".cs");
183 [EnvironmentPermission (SecurityAction.Deny, Read = "Mono")]
184 [ExpectedException (typeof (SecurityException))]
186 [Category ("NotDotNet")] // MS doesn't check for Environment under 1.x
188 public void AddExtension_Deny_EnvironmentPermission ()
190 TempFileCollection tfc = new TempFileCollection ();
191 // requires Unrestricted Environment access
192 tfc.AddExtension (".cs");
196 [FileIOPermission (SecurityAction.PermitOnly, Unrestricted = true)]
197 [ExpectedException (typeof (SecurityException))]
199 [Category ("NotDotNet")] // MS doesn't check for Environment under 1.x
201 public void AddExtension_PermitOnly_FileIOPermission ()
203 TempFileCollection tfc = new TempFileCollection ();
204 // good but not enough
205 tfc.AddExtension (".cs");
209 [FileIOPermission (SecurityAction.Deny, Unrestricted = true)]
210 [ExpectedException (typeof (SecurityException))]
211 public void AddExtension_Deny_FileIOPermission ()
213 TempFileCollection tfc = new TempFileCollection ();
214 // requires path access
215 tfc.AddExtension (".cs");
219 [EnvironmentPermission (SecurityAction.PermitOnly, Unrestricted = true)]
220 [ExpectedException (typeof (SecurityException))]
221 public void AddExtension2_PermitOnly_EnvironmentPermission ()
223 TempFileCollection tfc = new TempFileCollection ();
224 // good but not enough
225 tfc.AddExtension (".cx", true);
229 [EnvironmentPermission (SecurityAction.Deny, Read = "Mono")]
230 [ExpectedException (typeof (SecurityException))]
232 [Category ("NotDotNet")] // MS doesn't check for Environment under 1.x
234 public void AddExtension2_Deny_EnvironmentPermission ()
236 TempFileCollection tfc = new TempFileCollection ();
237 // requires Unrestricted Environment access
238 tfc.AddExtension (".cx", true);
242 [FileIOPermission (SecurityAction.PermitOnly, Unrestricted = true)]
243 [ExpectedException (typeof (SecurityException))]
245 [Category ("NotDotNet")] // MS doesn't check for Environment under 1.x
247 public void AddExtension2_PermitOnly_FileIOPermission ()
249 TempFileCollection tfc = new TempFileCollection ();
250 // good but not enough
251 tfc.AddExtension (".cx", true);
255 [FileIOPermission (SecurityAction.Deny, Unrestricted = true)]
256 [ExpectedException (typeof (SecurityException))]
257 public void AddExtension2_Deny_FileIOPermission ()
259 TempFileCollection tfc = new TempFileCollection ();
260 // requires path access
261 tfc.AddExtension (".cx", true);
265 [FileIOPermission (SecurityAction.PermitOnly, Unrestricted = true)]
266 [EnvironmentPermission (SecurityAction.PermitOnly, Unrestricted = true)]
267 public void PermitOnly_FileIOPermission_EnvironmentPermission ()
269 TempFileCollection tfc = new TempFileCollection ();
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*
280 [PermissionSet (SecurityAction.Deny, Unrestricted = true)]
281 public void ICollection_Deny_Unrestricted ()
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);
291 [PermissionSet (SecurityAction.Deny, Unrestricted = true)]
292 public void IEnumerable_Deny_Unrestricted ()
294 IEnumerable e = (IEnumerable) new TempFileCollection ();
295 Assert.IsNotNull (e.GetEnumerator (), "GetEnumerator");
299 public void LinkDemand_No_Restriction ()
301 ConstructorInfo ci = typeof (TempFileCollection).GetConstructor (new Type[0]);
302 Assert.IsNotNull (ci, "default .ctor");
303 Assert.IsNotNull (ci.Invoke (null), "invoke");
307 [EnvironmentPermission (SecurityAction.Deny, Read = "Mono")]
308 [ExpectedException (typeof (SecurityException))]
309 public void LinkDemand_Deny_Unrestricted ()
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");