2 // CompilerParametersCas.cs
3 // - CAS unit tests for System.CodeDom.Compiler.CompilerParameters
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.Reflection;
35 using System.Security;
36 using System.Security.Permissions;
37 using System.Security.Policy;
39 namespace MonoCasTests.System.CodeDom.Compiler {
43 public class CompilerParametersCas {
48 if (!SecurityManager.SecurityEnabled)
49 Assert.Ignore ("SecurityManager.SecurityEnabled is OFF");
53 [PermissionSet (SecurityAction.Deny, Unrestricted = true)]
54 public void Constructor0_Deny_Unrestricted ()
56 CompilerParameters cp = new CompilerParameters ();
57 Assert.IsNull (cp.CompilerOptions, "CompilerOptions");
58 cp.CompilerOptions = "-debug";
59 Assert.IsNull (cp.Evidence, "Evidence");
60 Assert.IsFalse (cp.GenerateExecutable, "GenerateExecutable");
61 cp.GenerateExecutable = true;
62 Assert.IsFalse (cp.GenerateInMemory, "GenerateInMemory");
63 cp.GenerateInMemory = true;
64 Assert.IsFalse (cp.IncludeDebugInformation, "IncludeDebugInformation");
65 cp.IncludeDebugInformation = true;
66 Assert.IsNull (cp.MainClass, "MainClass");
67 cp.MainClass = "Program";
68 Assert.IsNull (cp.OutputAssembly, "OutputAssembly");
69 cp.OutputAssembly = "mono.dll";
70 Assert.AreEqual (0, cp.ReferencedAssemblies.Count, "ReferencedAssemblies");
71 Assert.AreEqual (0, cp.TempFiles.Count, "TempFiles");
72 cp.TempFiles = new TempFileCollection ();
73 Assert.AreEqual (IntPtr.Zero, cp.UserToken, "UserToken");
74 cp.UserToken = (IntPtr) 1;
75 Assert.AreEqual (-1, cp.WarningLevel, "WarningLevel");
77 Assert.IsNull (cp.Win32Resource, "Win32Resource");
78 cp.Win32Resource = "*";
80 Assert.AreEqual (0, cp.EmbeddedResources.Count, "EmbeddedResources");
81 Assert.AreEqual (0, cp.LinkedResources.Count, "LinkedResources");
86 [PermissionSet (SecurityAction.Deny, Unrestricted = true)]
87 public void Constructor1_Deny_Unrestricted ()
89 CompilerParameters cp = new CompilerParameters (new string[1] { "mono.exe" });
90 Assert.IsNull (cp.CompilerOptions, "CompilerOptions");
91 cp.CompilerOptions = "-debug";
92 Assert.IsNull (cp.Evidence, "Evidence");
93 Assert.IsFalse (cp.GenerateExecutable, "GenerateExecutable");
94 cp.GenerateExecutable = true;
95 Assert.IsFalse (cp.GenerateInMemory, "GenerateInMemory");
96 cp.GenerateInMemory = true;
97 Assert.IsFalse (cp.IncludeDebugInformation, "IncludeDebugInformation");
98 cp.IncludeDebugInformation = true;
99 Assert.IsNull (cp.MainClass, "MainClass");
100 cp.MainClass = "Program";
101 Assert.IsNull (cp.OutputAssembly, "OutputAssembly");
102 cp.OutputAssembly = "mono.dll";
103 Assert.AreEqual (1, cp.ReferencedAssemblies.Count, "ReferencedAssemblies");
104 Assert.AreEqual (0, cp.TempFiles.Count, "TempFiles");
105 cp.TempFiles = new TempFileCollection ();
106 Assert.AreEqual (IntPtr.Zero, cp.UserToken, "UserToken");
107 cp.UserToken = (IntPtr) 1;
108 Assert.AreEqual (-1, cp.WarningLevel, "WarningLevel");
110 Assert.IsNull (cp.Win32Resource, "Win32Resource");
111 cp.Win32Resource = "*";
113 Assert.AreEqual (0, cp.EmbeddedResources.Count, "EmbeddedResources");
114 Assert.AreEqual (0, cp.LinkedResources.Count, "LinkedResources");
119 [PermissionSet (SecurityAction.Deny, Unrestricted = true)]
120 public void Constructor2_Deny_Unrestricted ()
122 CompilerParameters cp = new CompilerParameters (new string[1] { "mono.exe" }, "mono.dll");
123 Assert.IsNull (cp.CompilerOptions, "CompilerOptions");
124 cp.CompilerOptions = "-debug";
125 Assert.IsNull (cp.Evidence, "Evidence");
126 Assert.IsFalse (cp.GenerateExecutable, "GenerateExecutable");
127 cp.GenerateExecutable = true;
128 Assert.IsFalse (cp.GenerateInMemory, "GenerateInMemory");
129 cp.GenerateInMemory = true;
130 Assert.IsFalse (cp.IncludeDebugInformation, "IncludeDebugInformation");
131 cp.IncludeDebugInformation = true;
132 Assert.IsNull (cp.MainClass, "MainClass");
133 cp.MainClass = "Program";
134 Assert.AreEqual ("mono.dll", cp.OutputAssembly, "OutputAssembly");
135 cp.OutputAssembly = null;
136 Assert.AreEqual (1, cp.ReferencedAssemblies.Count, "ReferencedAssemblies");
137 Assert.AreEqual (0, cp.TempFiles.Count, "TempFiles");
138 cp.TempFiles = new TempFileCollection ();
139 Assert.AreEqual (IntPtr.Zero, cp.UserToken, "UserToken");
140 cp.UserToken = (IntPtr) 1;
141 Assert.AreEqual (-1, cp.WarningLevel, "WarningLevel");
143 Assert.IsNull (cp.Win32Resource, "Win32Resource");
144 cp.Win32Resource = "*";
146 Assert.AreEqual (0, cp.EmbeddedResources.Count, "EmbeddedResources");
147 Assert.AreEqual (0, cp.LinkedResources.Count, "LinkedResources");
152 [PermissionSet (SecurityAction.Deny, Unrestricted = true)]
153 public void Constructor3_Deny_Unrestricted ()
155 CompilerParameters cp = new CompilerParameters (new string[1] { "mono.exe" }, "mono.dll", true);
156 Assert.IsNull (cp.CompilerOptions, "CompilerOptions");
157 cp.CompilerOptions = "-debug";
158 Assert.IsNull (cp.Evidence, "Evidence");
159 Assert.IsFalse (cp.GenerateExecutable, "GenerateExecutable");
160 cp.GenerateExecutable = true;
161 Assert.IsFalse (cp.GenerateInMemory, "GenerateInMemory");
162 cp.GenerateInMemory = true;
163 Assert.IsTrue (cp.IncludeDebugInformation, "IncludeDebugInformation");
164 cp.IncludeDebugInformation = false;
165 Assert.IsNull (cp.MainClass, "MainClass");
166 cp.MainClass = "Program";
167 Assert.AreEqual ("mono.dll", cp.OutputAssembly, "OutputAssembly");
168 cp.OutputAssembly = null;
169 Assert.AreEqual (1, cp.ReferencedAssemblies.Count, "ReferencedAssemblies");
170 Assert.AreEqual (0, cp.TempFiles.Count, "TempFiles");
171 cp.TempFiles = new TempFileCollection ();
172 Assert.AreEqual (IntPtr.Zero, cp.UserToken, "UserToken");
173 cp.UserToken = (IntPtr) 1;
174 Assert.AreEqual (-1, cp.WarningLevel, "WarningLevel");
176 Assert.IsNull (cp.Win32Resource, "Win32Resource");
177 cp.Win32Resource = "*";
179 Assert.AreEqual (0, cp.EmbeddedResources.Count, "EmbeddedResources");
180 Assert.AreEqual (0, cp.LinkedResources.Count, "LinkedResources");
185 [SecurityPermission (SecurityAction.PermitOnly, ControlEvidence = true)]
186 public void Evidence_PermitOnly_Unrestricted ()
188 CompilerParameters cp = new CompilerParameters ();
189 cp.Evidence = new Evidence ();
193 [SecurityPermission (SecurityAction.Deny, ControlEvidence = true)]
194 [ExpectedException (typeof (SecurityException))]
195 public void Evidence_Deny_Unrestricted ()
197 CompilerParameters cp = new CompilerParameters ();
198 cp.Evidence = new Evidence ();
202 public void LinkDemand_No_Restriction ()
204 ConstructorInfo ci = typeof (CompilerParameters).GetConstructor (new Type[0]);
205 Assert.IsNotNull (ci, "default .ctor()");
206 Assert.IsNotNull (ci.Invoke (null), "invoke");
210 [EnvironmentPermission (SecurityAction.Deny, Read = "Mono")]
211 [ExpectedException (typeof (SecurityException))]
212 public void LinkDemand_Deny_Anything ()
214 // denying anything results in a non unrestricted permission set
215 ConstructorInfo ci = typeof (CompilerParameters).GetConstructor (new Type[0]);
216 Assert.IsNotNull (ci, "default .ctor()");
217 Assert.IsNotNull (ci.Invoke (null), "invoke");