2 // CodeDomProviderCas.cs
3 // - CAS unit tests for System.CodeDom.Compiler.CodeDomProvider
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;
34 using System.CodeDom.Compiler;
35 using System.Configuration;
37 using System.Reflection;
38 using System.Security;
39 using System.Security.Permissions;
41 namespace MonoCasTests.System.CodeDom.Compiler {
43 class CodeDomProviderTest: CodeDomProvider {
45 public CodeDomProviderTest ()
49 public override ICodeCompiler CreateCompiler ()
54 public override ICodeGenerator CreateGenerator ()
62 public class CodeDomProviderCas {
64 private StringWriter writer;
65 private CodeDomProviderTest cdp;
68 public void FixtureSetUp ()
71 writer = new StringWriter ();
72 cdp = new CodeDomProviderTest ();
78 if (!SecurityManager.SecurityEnabled)
79 Assert.Ignore ("SecurityManager.SecurityEnabled is OFF");
83 [PermissionSet (SecurityAction.Deny, Unrestricted = true)]
84 public void Defaults ()
86 cdp = new CodeDomProviderTest (); // execute ctor not a full trust
87 Assert.AreEqual (String.Empty, cdp.FileExtension, "FileExtension");
88 Assert.AreEqual (LanguageOptions.None, cdp.LanguageOptions, "LanguageOptions");
89 Assert.IsNull (cdp.CreateCompiler (), "CreateCompiler");
90 Assert.IsNull (cdp.CreateGenerator (), "CreateGenerator");
91 Assert.IsNull (cdp.CreateGenerator (String.Empty), "CreateGenerator(string)");
92 Assert.IsNull (cdp.CreateGenerator (writer), "CreateGenerator(TextWriter)");
93 Assert.IsNull (cdp.CreateParser (), "CreateParser()");
94 Assert.IsNotNull (cdp.GetConverter (typeof (string)), "GetConverter");
96 Assert.IsNotNull (CodeDomProvider.GetAllCompilerInfo (), "GetAllCompilerInfo");
98 // mono returns null (missing config?)
99 CodeDomProvider.GetCompilerInfo ("cs");
100 CodeDomProvider.GetLanguageFromExtension ("cs");
102 Assert.IsFalse (CodeDomProvider.IsDefinedExtension (String.Empty), "String.Empty");
103 Assert.IsFalse (CodeDomProvider.IsDefinedLanguage (String.Empty), "String.Empty");
109 [PermissionSet (SecurityAction.Deny, Unrestricted = true)]
110 [ExpectedException (typeof (NotImplementedException))]
111 public void CompileAssemblyFromDom_Deny_Unrestricted ()
113 cdp.CompileAssemblyFromDom (null, null);
117 [PermissionSet (SecurityAction.Deny, Unrestricted = true)]
118 [ExpectedException (typeof (NotImplementedException))]
119 public void CompileAssemblyFromFile_Deny_Unrestricted ()
121 cdp.CompileAssemblyFromFile (null, null);
125 [PermissionSet (SecurityAction.Deny, Unrestricted = true)]
126 [ExpectedException (typeof (NotImplementedException))]
127 public void CompileAssemblyFromSource_Deny_Unrestricted ()
129 cdp.CompileAssemblyFromSource (null, null);
133 [PermissionSet (SecurityAction.Deny, Unrestricted = true)]
134 [ExpectedException (typeof (NotImplementedException))]
135 public void CreateEscapedIdentifier_Deny_Unrestricted ()
137 cdp.CreateEscapedIdentifier (null);
141 [PermissionSet (SecurityAction.Deny, Unrestricted = true)]
142 [ExpectedException (typeof (NotImplementedException))]
143 public void CreateValidIdentifier_Deny_Unrestricted ()
145 cdp.CreateValidIdentifier (null);
149 [PermissionSet (SecurityAction.Deny, Unrestricted = true)]
150 [ExpectedException (typeof (NotImplementedException))]
151 public void GenerateCodeFromCompileUnit_Deny_Unrestricted ()
153 cdp.GenerateCodeFromCompileUnit (null, writer, null);
157 [PermissionSet (SecurityAction.Deny, Unrestricted = true)]
158 [ExpectedException (typeof (NotImplementedException))]
159 public void GenerateCodeFromExpression_Deny_Unrestricted ()
161 cdp.GenerateCodeFromExpression (null, writer, null);
165 [PermissionSet (SecurityAction.Deny, Unrestricted = true)]
166 [ExpectedException (typeof (NotImplementedException))]
167 public void GenerateCodeFromMember_Deny_Unrestricted ()
169 cdp.GenerateCodeFromMember (null, writer, null);
173 [PermissionSet (SecurityAction.Deny, Unrestricted = true)]
174 [ExpectedException (typeof (NotImplementedException))]
175 public void GenerateCodeFromNamespace_Deny_Unrestricted ()
177 cdp.GenerateCodeFromNamespace (null, writer, null);
181 [PermissionSet (SecurityAction.Deny, Unrestricted = true)]
182 [ExpectedException (typeof (NotImplementedException))]
183 public void GenerateCodeFromStatement_Deny_Unrestricted ()
185 cdp.GenerateCodeFromStatement (null, writer, null);
189 [PermissionSet (SecurityAction.Deny, Unrestricted = true)]
190 [ExpectedException (typeof (NotImplementedException))]
191 public void GenerateCodeFromType_Deny_Unrestricted ()
193 cdp.GenerateCodeFromType (null, writer, null);
197 [PermissionSet (SecurityAction.Deny, Unrestricted = true)]
198 [ExpectedException (typeof (NotImplementedException))]
199 public void GetTypeOutput_Deny_Unrestricted ()
201 cdp.GetTypeOutput (new CodeTypeReference ());
205 [PermissionSet (SecurityAction.Deny, Unrestricted = true)]
206 [ExpectedException (typeof (NotImplementedException))]
207 public void IsValidIdentifier_Deny_Unrestricted ()
209 cdp.IsValidIdentifier (null);
213 [PermissionSet (SecurityAction.Deny, Unrestricted = true)]
214 [ExpectedException (typeof (NotImplementedException))]
215 public void Parse_Deny_Unrestricted ()
221 [PermissionSet (SecurityAction.Deny, Unrestricted = true)]
222 [ExpectedException (typeof (NotImplementedException))]
223 public void Supports_Deny_Unrestricted ()
225 cdp.Supports (GeneratorSupport.ArraysOfArrays);
231 public void CreateProvider_Allow_Everything ()
233 CodeDomProvider.CreateProvider ("cs");
234 // returns null on mono (missing config?)
238 [EnvironmentPermission (SecurityAction.Deny, Read = "MONO")]
239 [ExpectedException (typeof (SecurityException))]
240 [Category ("NotWorking")] // mono returns null not an instance
241 public void CreateProvider_Deny_Anything ()
243 CodeDomProvider cdp = CodeDomProvider.CreateProvider ("cs");
244 // requires full trust (i.e. unrestricted permission set)
248 public void LinkDemand_StaticMethods_Allow_Everything ()
250 object[] language = new object[1] { "cs" };
251 object[] empty = new object[1] { String.Empty };
253 MethodInfo mi = typeof (CodeDomProvider).GetMethod ("CreateProvider");
254 mi.Invoke (null, language); // returns null on mono (missing config?)
256 mi = typeof (CodeDomProvider).GetMethod ("GetAllCompilerInfo");
257 Assert.IsNotNull (mi.Invoke (null, null), "GetAllCompilerInfo()");
259 mi = typeof (CodeDomProvider).GetMethod ("GetCompilerInfo");
260 mi.Invoke (null, language); // returns null on mono (missing config?)
262 mi = typeof (CodeDomProvider).GetMethod ("GetLanguageFromExtension");
263 mi.Invoke (null, language); // returns null on mono (missing config?)
265 mi = typeof (CodeDomProvider).GetMethod ("IsDefinedExtension");
266 Assert.IsFalse ((bool)mi.Invoke (null, empty), "IsDefinedExtension('')");
268 mi = typeof (CodeDomProvider).GetMethod ("IsDefinedLanguage");
269 Assert.IsFalse ((bool)mi.Invoke (null, empty), "IsDefinedLanguage('')");
273 [EnvironmentPermission (SecurityAction.Deny, Read = "MONO")]
274 [ExpectedException (typeof (SecurityException))]
275 public void LinkDemand_CreateProvider_Deny_Anything ()
277 MethodInfo mi = typeof (CodeDomProvider).GetMethod ("CreateProvider");
278 Assert.IsNotNull (mi, "CreateProvider");
279 Assert.IsNotNull (mi.Invoke (null, new object[1] { "cs" }), "CreateProvider(cs)");
283 [EnvironmentPermission (SecurityAction.Deny, Read = "MONO")]
284 [ExpectedException (typeof (SecurityException))]
285 public void LinkDemand_GetAllCompilerInfo_Deny_Anything ()
287 MethodInfo mi = typeof (CodeDomProvider).GetMethod ("GetAllCompilerInfo");
288 Assert.IsNotNull (mi, "GetAllCompilerInfo");
289 Assert.IsNotNull (mi.Invoke (null, null), "GetAllCompilerInfo()");
290 // requires full trust (i.e. unrestricted permission set)
294 [EnvironmentPermission (SecurityAction.Deny, Read = "MONO")]
295 [ExpectedException (typeof (SecurityException))]
296 public void LinkDemand_GetCompilerInfo_Deny_Anything ()
298 MethodInfo mi = typeof (CodeDomProvider).GetMethod ("GetCompilerInfo");
299 Assert.IsNotNull (mi, "GetCompilerInfo");
300 Assert.IsNotNull (mi.Invoke (null, new object[1] { "cs" }), "GetCompilerInfo(cs)");
301 // requires full trust (i.e. unrestricted permission set)
305 [EnvironmentPermission (SecurityAction.Deny, Read = "MONO")]
306 [ExpectedException (typeof (SecurityException))]
307 public void LinkDemand_GetLanguageFromExtension_Deny_Anything ()
309 MethodInfo mi = typeof (CodeDomProvider).GetMethod ("GetLanguageFromExtension");
310 Assert.IsNotNull (mi, "GetLanguageFromExtension");
311 Assert.IsNotNull (mi.Invoke (null, new object[1] { null }), "invoke (null)");
312 // requires full trust (i.e. unrestricted permission set)
316 [EnvironmentPermission (SecurityAction.Deny, Read = "MONO")]
317 [ExpectedException (typeof (SecurityException))]
318 public void LinkDemand_IsDefinedExtension_Deny_Anything ()
320 MethodInfo mi = typeof (CodeDomProvider).GetMethod ("IsDefinedExtension");
321 Assert.IsNotNull (mi, "IsDefinedExtension");
322 Assert.IsFalse ((bool) mi.Invoke (null, new object[1] { String.Empty }), "IsDefinedExtension('')");
323 // requires full trust (i.e. unrestricted permission set)
327 [EnvironmentPermission (SecurityAction.Deny, Read = "MONO")]
328 [ExpectedException (typeof (SecurityException))]
329 public void LinkDemand_IsDefinedLanguage_Deny_Anything ()
331 MethodInfo mi = typeof (CodeDomProvider).GetMethod ("IsDefinedLanguage");
332 Assert.IsNotNull (mi, "IsDefinedLanguage");
333 Assert.IsFalse ((bool) mi.Invoke (null, new object[1] { String.Empty }), "IsDefinedLanguage('')");
334 // requires full trust (i.e. unrestricted permission set)
338 [PermissionSet (SecurityAction.Deny, Unrestricted = true)]
339 public void LinkDemand_Deny_Unrestricted ()
341 ConstructorInfo ci = typeof (CodeDomProviderTest).GetConstructor (new Type[0]);
342 Assert.IsNotNull (ci, "default .ctor()");
343 Assert.IsNotNull (ci.Invoke (null), "invoke");