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 using MonoTests
.Helpers
;
42 namespace MonoCasTests
.System
.CodeDom
.Compiler
{
46 public class TempFileCollectionCas
{
48 private TempDirectory _temp
;
50 private string[] array
;
53 public void FixtureSetUp ()
56 _temp
= new TempDirectory ();
58 array
= new string[1];
64 if (!SecurityManager
.SecurityEnabled
)
65 Assert
.Ignore ("SecurityManager.SecurityEnabled is OFF");
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);
79 (tfc
as IDisposable
).Dispose ();
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);
93 (tfc
as IDisposable
).Dispose ();
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);
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 ;-).
123 public void BasePath_Caching ()
125 TempFileCollection tfc
= new TempFileCollection ();
126 Assert
.IsNotNull (tfc
.BasePath
, "BasePath-Unrestricted");
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");
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");
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");
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");
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");
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");
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");
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");
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);
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);
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);
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);
251 [FileIOPermission (SecurityAction
.PermitOnly
, Unrestricted
= true)]
252 [EnvironmentPermission (SecurityAction
.PermitOnly
, Unrestricted
= true)]
253 public void PermitOnly_FileIOPermission_EnvironmentPermission ()
255 TempFileCollection tfc
= new TempFileCollection ();
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*
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);
277 [PermissionSet (SecurityAction
.Deny
, Unrestricted
= true)]
278 public void IEnumerable_Deny_Unrestricted ()
280 IEnumerable e
= (IEnumerable
) new TempFileCollection ();
281 Assert
.IsNotNull (e
.GetEnumerator (), "GetEnumerator");
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");
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");