Merge pull request #1326 from BrzVlad/master
[mono.git] / mcs / class / System / Test / System.CodeDom.Compiler / CodeDomProviderCas.cs
1 //
2 // CodeDomProviderCas.cs 
3 //      - CAS unit tests for System.CodeDom.Compiler.CodeDomProvider
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;
34 using System.CodeDom.Compiler;
35 using System.Configuration;
36 using System.IO;
37 using System.Reflection;
38 using System.Security;
39 using System.Security.Permissions;
40
41 namespace MonoCasTests.System.CodeDom.Compiler {
42
43         class CodeDomProviderTest: CodeDomProvider {
44
45                 public CodeDomProviderTest ()
46                 {
47                 }
48
49                 public override ICodeCompiler CreateCompiler ()
50                 {
51                         return null;
52                 }
53
54                 public override ICodeGenerator CreateGenerator ()
55                 {
56                         return null;
57                 }
58         }
59
60         [TestFixture]
61         [Category ("CAS")]
62         public class CodeDomProviderCas {
63
64                 private StringWriter writer;
65                 private CodeDomProviderTest cdp;
66
67                 [TestFixtureSetUp]
68                 public void FixtureSetUp ()
69                 {
70                         // at full trust
71                         writer = new StringWriter ();
72                         cdp = new CodeDomProviderTest ();
73                 }
74
75                 [SetUp]
76                 public void SetUp ()
77                 {
78                         if (!SecurityManager.SecurityEnabled)
79                                 Assert.Ignore ("SecurityManager.SecurityEnabled is OFF");
80                 }
81
82                 [Test]
83                 [PermissionSet (SecurityAction.Deny, Unrestricted = true)]
84                 public void Defaults ()
85                 {
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");
95 #if NET_2_0
96                         Assert.IsNotNull (CodeDomProvider.GetAllCompilerInfo (), "GetAllCompilerInfo");
97
98                         // mono returns null (missing config?)
99                         CodeDomProvider.GetCompilerInfo ("cs"); 
100                         CodeDomProvider.GetLanguageFromExtension ("cs");
101
102                         Assert.IsFalse (CodeDomProvider.IsDefinedExtension (String.Empty), "String.Empty");
103                         Assert.IsFalse (CodeDomProvider.IsDefinedLanguage (String.Empty), "String.Empty");
104 #endif
105                 }
106
107 #if NET_2_0
108                 [Test]
109                 [PermissionSet (SecurityAction.Deny, Unrestricted = true)]
110                 [ExpectedException (typeof (NotImplementedException))]
111                 public void CompileAssemblyFromDom_Deny_Unrestricted ()
112                 {
113                         cdp.CompileAssemblyFromDom (null, null);
114                 }
115
116                 [Test]
117                 [PermissionSet (SecurityAction.Deny, Unrestricted = true)]
118                 [ExpectedException (typeof (NotImplementedException))]
119                 public void CompileAssemblyFromFile_Deny_Unrestricted ()
120                 {
121                         cdp.CompileAssemblyFromFile (null, null);
122                 }
123
124                 [Test]
125                 [PermissionSet (SecurityAction.Deny, Unrestricted = true)]
126                 [ExpectedException (typeof (NotImplementedException))]
127                 public void CompileAssemblyFromSource_Deny_Unrestricted ()
128                 {
129                         cdp.CompileAssemblyFromSource (null, null);
130                 }
131
132                 [Test]
133                 [PermissionSet (SecurityAction.Deny, Unrestricted = true)]
134                 [ExpectedException (typeof (NotImplementedException))]
135                 public void CreateEscapedIdentifier_Deny_Unrestricted ()
136                 {
137                         cdp.CreateEscapedIdentifier (null);
138                 }
139
140                 [Test]
141                 [PermissionSet (SecurityAction.Deny, Unrestricted = true)]
142                 [ExpectedException (typeof (NotImplementedException))]
143                 public void CreateValidIdentifier_Deny_Unrestricted ()
144                 {
145                         cdp.CreateValidIdentifier (null);
146                 }
147
148                 [Test]
149                 [PermissionSet (SecurityAction.Deny, Unrestricted = true)]
150                 [ExpectedException (typeof (NotImplementedException))]
151                 public void GenerateCodeFromCompileUnit_Deny_Unrestricted ()
152                 {
153                         cdp.GenerateCodeFromCompileUnit (null, writer, null);
154                 }
155
156                 [Test]
157                 [PermissionSet (SecurityAction.Deny, Unrestricted = true)]
158                 [ExpectedException (typeof (NotImplementedException))]
159                 public void GenerateCodeFromExpression_Deny_Unrestricted ()
160                 {
161                         cdp.GenerateCodeFromExpression (null, writer, null);
162                 }
163
164                 [Test]
165                 [PermissionSet (SecurityAction.Deny, Unrestricted = true)]
166                 [ExpectedException (typeof (NotImplementedException))]
167                 public void GenerateCodeFromMember_Deny_Unrestricted ()
168                 {
169                         cdp.GenerateCodeFromMember (null, writer, null);
170                 }
171
172                 [Test]
173                 [PermissionSet (SecurityAction.Deny, Unrestricted = true)]
174                 [ExpectedException (typeof (NotImplementedException))]
175                 public void GenerateCodeFromNamespace_Deny_Unrestricted ()
176                 {
177                         cdp.GenerateCodeFromNamespace (null, writer, null);
178                 }
179
180                 [Test]
181                 [PermissionSet (SecurityAction.Deny, Unrestricted = true)]
182                 [ExpectedException (typeof (NotImplementedException))]
183                 public void GenerateCodeFromStatement_Deny_Unrestricted ()
184                 {
185                         cdp.GenerateCodeFromStatement (null, writer, null);
186                 }
187
188                 [Test]
189                 [PermissionSet (SecurityAction.Deny, Unrestricted = true)]
190                 [ExpectedException (typeof (NotImplementedException))]
191                 public void GenerateCodeFromType_Deny_Unrestricted ()
192                 {
193                         cdp.GenerateCodeFromType (null, writer, null);
194                 }
195
196                 [Test]
197                 [PermissionSet (SecurityAction.Deny, Unrestricted = true)]
198                 [ExpectedException (typeof (NotImplementedException))]
199                 public void GetTypeOutput_Deny_Unrestricted ()
200                 {
201                         cdp.GetTypeOutput (new CodeTypeReference ());
202                 }
203
204                 [Test]
205                 [PermissionSet (SecurityAction.Deny, Unrestricted = true)]
206                 [ExpectedException (typeof (NotImplementedException))]
207                 public void IsValidIdentifier_Deny_Unrestricted ()
208                 {
209                         cdp.IsValidIdentifier (null);
210                 }
211
212                 [Test]
213                 [PermissionSet (SecurityAction.Deny, Unrestricted = true)]
214                 [ExpectedException (typeof (NotImplementedException))]
215                 public void Parse_Deny_Unrestricted ()
216                 {
217                         cdp.Parse (null);
218                 }
219
220                 [Test]
221                 [PermissionSet (SecurityAction.Deny, Unrestricted = true)]
222                 [ExpectedException (typeof (NotImplementedException))]
223                 public void Supports_Deny_Unrestricted ()
224                 {
225                         cdp.Supports (GeneratorSupport.ArraysOfArrays);
226                 }
227
228                 // static methods
229
230                 [Test]
231                 public void CreateProvider_Allow_Everything ()
232                 {
233                         CodeDomProvider.CreateProvider ("cs");
234                         // returns null on mono (missing config?)
235                 }
236
237                 [Test]
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 ()
242                 {
243                         CodeDomProvider cdp = CodeDomProvider.CreateProvider ("cs");
244                         // requires full trust (i.e. unrestricted permission set)
245                 }
246
247                 [Test]
248                 public void LinkDemand_StaticMethods_Allow_Everything ()
249                 {
250                         object[] language = new object[1] { "cs" };
251                         object[] empty = new object[1] { String.Empty };
252
253                         MethodInfo mi = typeof (CodeDomProvider).GetMethod ("CreateProvider");
254                         mi.Invoke (null, language); // returns null on mono (missing config?)
255                         
256                         mi = typeof (CodeDomProvider).GetMethod ("GetAllCompilerInfo");
257                         Assert.IsNotNull (mi.Invoke (null, null), "GetAllCompilerInfo()");
258
259                         mi = typeof (CodeDomProvider).GetMethod ("GetCompilerInfo");
260                         mi.Invoke (null, language); // returns null on mono (missing config?)
261
262                         mi = typeof (CodeDomProvider).GetMethod ("GetLanguageFromExtension");
263                         mi.Invoke (null, language); // returns null on mono (missing config?)
264
265                         mi = typeof (CodeDomProvider).GetMethod ("IsDefinedExtension");
266                         Assert.IsFalse ((bool)mi.Invoke (null, empty), "IsDefinedExtension('')");
267
268                         mi = typeof (CodeDomProvider).GetMethod ("IsDefinedLanguage");
269                         Assert.IsFalse ((bool)mi.Invoke (null, empty), "IsDefinedLanguage('')");
270                 }
271
272                 [Test]
273                 [EnvironmentPermission (SecurityAction.Deny, Read = "MONO")]
274                 [ExpectedException (typeof (SecurityException))]
275                 public void LinkDemand_CreateProvider_Deny_Anything ()
276                 {
277                         MethodInfo mi = typeof (CodeDomProvider).GetMethod ("CreateProvider");
278                         Assert.IsNotNull (mi, "CreateProvider");
279                         Assert.IsNotNull (mi.Invoke (null, new object[1] { "cs" }), "CreateProvider(cs)");
280                 }
281
282                 [Test]
283                 [EnvironmentPermission (SecurityAction.Deny, Read = "MONO")]
284                 [ExpectedException (typeof (SecurityException))]
285                 public void LinkDemand_GetAllCompilerInfo_Deny_Anything ()
286                 {
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)
291                 }
292
293                 [Test]
294                 [EnvironmentPermission (SecurityAction.Deny, Read = "MONO")]
295                 [ExpectedException (typeof (SecurityException))]
296                 public void LinkDemand_GetCompilerInfo_Deny_Anything ()
297                 {
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)
302                 }
303
304                 [Test]
305                 [EnvironmentPermission (SecurityAction.Deny, Read = "MONO")]
306                 [ExpectedException (typeof (SecurityException))]
307                 public void LinkDemand_GetLanguageFromExtension_Deny_Anything ()
308                 {
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)
313                 }
314
315                 [Test]
316                 [EnvironmentPermission (SecurityAction.Deny, Read = "MONO")]
317                 [ExpectedException (typeof (SecurityException))]
318                 public void LinkDemand_IsDefinedExtension_Deny_Anything ()
319                 {
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)
324                 }
325
326                 [Test]
327                 [EnvironmentPermission (SecurityAction.Deny, Read = "MONO")]
328                 [ExpectedException (typeof (SecurityException))]
329                 public void LinkDemand_IsDefinedLanguage_Deny_Anything ()
330                 {
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)
335                 }
336 #endif
337                 [Test]
338                 [PermissionSet (SecurityAction.Deny, Unrestricted = true)]
339                 public void LinkDemand_Deny_Unrestricted ()
340                 {
341                         ConstructorInfo ci = typeof (CodeDomProviderTest).GetConstructor (new Type[0]);
342                         Assert.IsNotNull (ci, "default .ctor()");
343                         Assert.IsNotNull (ci.Invoke (null), "invoke");
344                 }
345         }
346 }