1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
|
//
// CodeDomProviderCas.cs
// - CAS unit tests for System.CodeDom.Compiler.CodeDomProvider
//
// Author:
// Sebastien Pouliot <sebastien@ximian.com>
//
// Copyright (C) 2005 Novell, Inc (http://www.novell.com)
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
using NUnit.Framework;
using System;
using System.CodeDom;
using System.CodeDom.Compiler;
using System.Configuration;
using System.IO;
using System.Reflection;
using System.Security;
using System.Security.Permissions;
namespace MonoCasTests.System.CodeDom.Compiler {
class CodeDomProviderTest: CodeDomProvider {
public CodeDomProviderTest ()
{
}
public override ICodeCompiler CreateCompiler ()
{
return null;
}
public override ICodeGenerator CreateGenerator ()
{
return null;
}
}
[TestFixture]
[Category ("CAS")]
public class CodeDomProviderCas {
private StringWriter writer;
private CodeDomProviderTest cdp;
[TestFixtureSetUp]
public void FixtureSetUp ()
{
// at full trust
writer = new StringWriter ();
cdp = new CodeDomProviderTest ();
}
[SetUp]
public void SetUp ()
{
if (!SecurityManager.SecurityEnabled)
Assert.Ignore ("SecurityManager.SecurityEnabled is OFF");
}
[Test]
[PermissionSet (SecurityAction.Deny, Unrestricted = true)]
public void Defaults ()
{
cdp = new CodeDomProviderTest (); // execute ctor not a full trust
Assert.AreEqual (String.Empty, cdp.FileExtension, "FileExtension");
Assert.AreEqual (LanguageOptions.None, cdp.LanguageOptions, "LanguageOptions");
Assert.IsNull (cdp.CreateCompiler (), "CreateCompiler");
Assert.IsNull (cdp.CreateGenerator (), "CreateGenerator");
Assert.IsNull (cdp.CreateGenerator (String.Empty), "CreateGenerator(string)");
Assert.IsNull (cdp.CreateGenerator (writer), "CreateGenerator(TextWriter)");
Assert.IsNull (cdp.CreateParser (), "CreateParser()");
Assert.IsNotNull (cdp.GetConverter (typeof (string)), "GetConverter");
#if NET_2_0
Assert.IsNotNull (CodeDomProvider.GetAllCompilerInfo (), "GetAllCompilerInfo");
// mono returns null (missing config?)
CodeDomProvider.GetCompilerInfo ("cs");
CodeDomProvider.GetLanguageFromExtension ("cs");
Assert.IsFalse (CodeDomProvider.IsDefinedExtension (String.Empty), "String.Empty");
Assert.IsFalse (CodeDomProvider.IsDefinedLanguage (String.Empty), "String.Empty");
#endif
}
#if NET_2_0
[Test]
[PermissionSet (SecurityAction.Deny, Unrestricted = true)]
[ExpectedException (typeof (NotImplementedException))]
public void CompileAssemblyFromDom_Deny_Unrestricted ()
{
cdp.CompileAssemblyFromDom (null, null);
}
[Test]
[PermissionSet (SecurityAction.Deny, Unrestricted = true)]
[ExpectedException (typeof (NotImplementedException))]
public void CompileAssemblyFromFile_Deny_Unrestricted ()
{
cdp.CompileAssemblyFromFile (null, null);
}
[Test]
[PermissionSet (SecurityAction.Deny, Unrestricted = true)]
[ExpectedException (typeof (NotImplementedException))]
public void CompileAssemblyFromSource_Deny_Unrestricted ()
{
cdp.CompileAssemblyFromSource (null, null);
}
[Test]
[PermissionSet (SecurityAction.Deny, Unrestricted = true)]
[ExpectedException (typeof (NotImplementedException))]
public void CreateEscapedIdentifier_Deny_Unrestricted ()
{
cdp.CreateEscapedIdentifier (null);
}
[Test]
[PermissionSet (SecurityAction.Deny, Unrestricted = true)]
[ExpectedException (typeof (NotImplementedException))]
public void CreateValidIdentifier_Deny_Unrestricted ()
{
cdp.CreateValidIdentifier (null);
}
[Test]
[PermissionSet (SecurityAction.Deny, Unrestricted = true)]
[ExpectedException (typeof (NotImplementedException))]
public void GenerateCodeFromCompileUnit_Deny_Unrestricted ()
{
cdp.GenerateCodeFromCompileUnit (null, writer, null);
}
[Test]
[PermissionSet (SecurityAction.Deny, Unrestricted = true)]
[ExpectedException (typeof (NotImplementedException))]
public void GenerateCodeFromExpression_Deny_Unrestricted ()
{
cdp.GenerateCodeFromExpression (null, writer, null);
}
[Test]
[PermissionSet (SecurityAction.Deny, Unrestricted = true)]
[ExpectedException (typeof (NotImplementedException))]
public void GenerateCodeFromMember_Deny_Unrestricted ()
{
cdp.GenerateCodeFromMember (null, writer, null);
}
[Test]
[PermissionSet (SecurityAction.Deny, Unrestricted = true)]
[ExpectedException (typeof (NotImplementedException))]
public void GenerateCodeFromNamespace_Deny_Unrestricted ()
{
cdp.GenerateCodeFromNamespace (null, writer, null);
}
[Test]
[PermissionSet (SecurityAction.Deny, Unrestricted = true)]
[ExpectedException (typeof (NotImplementedException))]
public void GenerateCodeFromStatement_Deny_Unrestricted ()
{
cdp.GenerateCodeFromStatement (null, writer, null);
}
[Test]
[PermissionSet (SecurityAction.Deny, Unrestricted = true)]
[ExpectedException (typeof (NotImplementedException))]
public void GenerateCodeFromType_Deny_Unrestricted ()
{
cdp.GenerateCodeFromType (null, writer, null);
}
[Test]
[PermissionSet (SecurityAction.Deny, Unrestricted = true)]
[ExpectedException (typeof (NotImplementedException))]
public void GetTypeOutput_Deny_Unrestricted ()
{
cdp.GetTypeOutput (new CodeTypeReference ());
}
[Test]
[PermissionSet (SecurityAction.Deny, Unrestricted = true)]
[ExpectedException (typeof (NotImplementedException))]
public void IsValidIdentifier_Deny_Unrestricted ()
{
cdp.IsValidIdentifier (null);
}
[Test]
[PermissionSet (SecurityAction.Deny, Unrestricted = true)]
[ExpectedException (typeof (NotImplementedException))]
public void Parse_Deny_Unrestricted ()
{
cdp.Parse (null);
}
[Test]
[PermissionSet (SecurityAction.Deny, Unrestricted = true)]
[ExpectedException (typeof (NotImplementedException))]
public void Supports_Deny_Unrestricted ()
{
cdp.Supports (GeneratorSupport.ArraysOfArrays);
}
// static methods
[Test]
public void CreateProvider_Allow_Everything ()
{
CodeDomProvider.CreateProvider ("cs");
// returns null on mono (missing config?)
}
[Test]
[EnvironmentPermission (SecurityAction.Deny, Read = "MONO")]
[ExpectedException (typeof (SecurityException))]
[Category ("NotWorking")] // mono returns null not an instance
public void CreateProvider_Deny_Anything ()
{
CodeDomProvider cdp = CodeDomProvider.CreateProvider ("cs");
// requires full trust (i.e. unrestricted permission set)
}
[Test]
public void LinkDemand_StaticMethods_Allow_Everything ()
{
object[] language = new object[1] { "cs" };
object[] empty = new object[1] { String.Empty };
MethodInfo mi = typeof (CodeDomProvider).GetMethod ("CreateProvider");
mi.Invoke (null, language); // returns null on mono (missing config?)
mi = typeof (CodeDomProvider).GetMethod ("GetAllCompilerInfo");
Assert.IsNotNull (mi.Invoke (null, null), "GetAllCompilerInfo()");
mi = typeof (CodeDomProvider).GetMethod ("GetCompilerInfo");
mi.Invoke (null, language); // returns null on mono (missing config?)
mi = typeof (CodeDomProvider).GetMethod ("GetLanguageFromExtension");
mi.Invoke (null, language); // returns null on mono (missing config?)
mi = typeof (CodeDomProvider).GetMethod ("IsDefinedExtension");
Assert.IsFalse ((bool)mi.Invoke (null, empty), "IsDefinedExtension('')");
mi = typeof (CodeDomProvider).GetMethod ("IsDefinedLanguage");
Assert.IsFalse ((bool)mi.Invoke (null, empty), "IsDefinedLanguage('')");
}
[Test]
[EnvironmentPermission (SecurityAction.Deny, Read = "MONO")]
[ExpectedException (typeof (SecurityException))]
public void LinkDemand_CreateProvider_Deny_Anything ()
{
MethodInfo mi = typeof (CodeDomProvider).GetMethod ("CreateProvider");
Assert.IsNotNull (mi, "CreateProvider");
Assert.IsNotNull (mi.Invoke (null, new object[1] { "cs" }), "CreateProvider(cs)");
}
[Test]
[EnvironmentPermission (SecurityAction.Deny, Read = "MONO")]
[ExpectedException (typeof (SecurityException))]
public void LinkDemand_GetAllCompilerInfo_Deny_Anything ()
{
MethodInfo mi = typeof (CodeDomProvider).GetMethod ("GetAllCompilerInfo");
Assert.IsNotNull (mi, "GetAllCompilerInfo");
Assert.IsNotNull (mi.Invoke (null, null), "GetAllCompilerInfo()");
// requires full trust (i.e. unrestricted permission set)
}
[Test]
[EnvironmentPermission (SecurityAction.Deny, Read = "MONO")]
[ExpectedException (typeof (SecurityException))]
public void LinkDemand_GetCompilerInfo_Deny_Anything ()
{
MethodInfo mi = typeof (CodeDomProvider).GetMethod ("GetCompilerInfo");
Assert.IsNotNull (mi, "GetCompilerInfo");
Assert.IsNotNull (mi.Invoke (null, new object[1] { "cs" }), "GetCompilerInfo(cs)");
// requires full trust (i.e. unrestricted permission set)
}
[Test]
[EnvironmentPermission (SecurityAction.Deny, Read = "MONO")]
[ExpectedException (typeof (SecurityException))]
public void LinkDemand_GetLanguageFromExtension_Deny_Anything ()
{
MethodInfo mi = typeof (CodeDomProvider).GetMethod ("GetLanguageFromExtension");
Assert.IsNotNull (mi, "GetLanguageFromExtension");
Assert.IsNotNull (mi.Invoke (null, new object[1] { null }), "invoke (null)");
// requires full trust (i.e. unrestricted permission set)
}
[Test]
[EnvironmentPermission (SecurityAction.Deny, Read = "MONO")]
[ExpectedException (typeof (SecurityException))]
public void LinkDemand_IsDefinedExtension_Deny_Anything ()
{
MethodInfo mi = typeof (CodeDomProvider).GetMethod ("IsDefinedExtension");
Assert.IsNotNull (mi, "IsDefinedExtension");
Assert.IsFalse ((bool) mi.Invoke (null, new object[1] { String.Empty }), "IsDefinedExtension('')");
// requires full trust (i.e. unrestricted permission set)
}
[Test]
[EnvironmentPermission (SecurityAction.Deny, Read = "MONO")]
[ExpectedException (typeof (SecurityException))]
public void LinkDemand_IsDefinedLanguage_Deny_Anything ()
{
MethodInfo mi = typeof (CodeDomProvider).GetMethod ("IsDefinedLanguage");
Assert.IsNotNull (mi, "IsDefinedLanguage");
Assert.IsFalse ((bool) mi.Invoke (null, new object[1] { String.Empty }), "IsDefinedLanguage('')");
// requires full trust (i.e. unrestricted permission set)
}
#endif
[Test]
[PermissionSet (SecurityAction.Deny, Unrestricted = true)]
public void LinkDemand_Deny_Unrestricted ()
{
ConstructorInfo ci = typeof (CodeDomProviderTest).GetConstructor (new Type[0]);
Assert.IsNotNull (ci, "default .ctor()");
Assert.IsNotNull (ci.Invoke (null), "invoke");
}
}
}
|