New test.
[mono.git] / mcs / class / System / Test / System.CodeDom.Compiler / CompilerParametersCas.cs
1 //
2 // CompilerParametersCas.cs 
3 //      - CAS unit tests for System.CodeDom.Compiler.CompilerParameters
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.Compiler;
34 using System.Reflection;
35 using System.Security;
36 using System.Security.Permissions;
37 using System.Security.Policy;
38
39 namespace MonoCasTests.System.CodeDom.Compiler {
40
41         [TestFixture]
42         [Category ("CAS")]
43         public class CompilerParametersCas {
44
45                 [SetUp]
46                 public void SetUp ()
47                 {
48                         if (!SecurityManager.SecurityEnabled)
49                                 Assert.Ignore ("SecurityManager.SecurityEnabled is OFF");
50                 }
51
52                 [Test]
53                 [PermissionSet (SecurityAction.Deny, Unrestricted = true)]
54                 public void Constructor0_Deny_Unrestricted ()
55                 {
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");
76                         cp.WarningLevel = 0;
77                         Assert.IsNull (cp.Win32Resource, "Win32Resource");
78                         cp.Win32Resource = "*";
79 #if NET_2_0
80                         Assert.AreEqual (0, cp.EmbeddedResources.Count, "EmbeddedResources");
81                         Assert.AreEqual (0, cp.LinkedResources.Count, "LinkedResources");
82 #endif
83                 }
84
85                 [Test]
86                 [PermissionSet (SecurityAction.Deny, Unrestricted = true)]
87                 public void Constructor1_Deny_Unrestricted ()
88                 {
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");
109                         cp.WarningLevel = 0;
110                         Assert.IsNull (cp.Win32Resource, "Win32Resource");
111                         cp.Win32Resource = "*";
112 #if NET_2_0
113                         Assert.AreEqual (0, cp.EmbeddedResources.Count, "EmbeddedResources");
114                         Assert.AreEqual (0, cp.LinkedResources.Count, "LinkedResources");
115 #endif
116                 }
117
118                 [Test]
119                 [PermissionSet (SecurityAction.Deny, Unrestricted = true)]
120                 public void Constructor2_Deny_Unrestricted ()
121                 {
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");
142                         cp.WarningLevel = 0;
143                         Assert.IsNull (cp.Win32Resource, "Win32Resource");
144                         cp.Win32Resource = "*";
145 #if NET_2_0
146                         Assert.AreEqual (0, cp.EmbeddedResources.Count, "EmbeddedResources");
147                         Assert.AreEqual (0, cp.LinkedResources.Count, "LinkedResources");
148 #endif
149                 }
150
151                 [Test]
152                 [PermissionSet (SecurityAction.Deny, Unrestricted = true)]
153                 public void Constructor3_Deny_Unrestricted ()
154                 {
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");
175                         cp.WarningLevel = 0;
176                         Assert.IsNull (cp.Win32Resource, "Win32Resource");
177                         cp.Win32Resource = "*";
178 #if NET_2_0
179                         Assert.AreEqual (0, cp.EmbeddedResources.Count, "EmbeddedResources");
180                         Assert.AreEqual (0, cp.LinkedResources.Count, "LinkedResources");
181 #endif
182                 }
183
184                 [Test]
185                 [SecurityPermission (SecurityAction.PermitOnly, ControlEvidence = true)]
186                 public void Evidence_PermitOnly_Unrestricted ()
187                 {
188                         CompilerParameters cp = new CompilerParameters ();
189                         cp.Evidence = new Evidence ();
190                 }
191
192                 [Test]
193                 [SecurityPermission (SecurityAction.Deny, ControlEvidence = true)]
194                 [ExpectedException (typeof (SecurityException))]
195                 public void Evidence_Deny_Unrestricted ()
196                 {
197                         CompilerParameters cp = new CompilerParameters ();
198                         cp.Evidence = new Evidence ();
199                 }
200
201                 [Test]
202                 public void LinkDemand_No_Restriction ()
203                 {
204                         ConstructorInfo ci = typeof (CompilerParameters).GetConstructor (new Type[0]);
205                         Assert.IsNotNull (ci, "default .ctor()");
206                         Assert.IsNotNull (ci.Invoke (null), "invoke");
207                 }
208
209                 [Test]
210                 [EnvironmentPermission (SecurityAction.Deny, Read = "Mono")]
211                 [ExpectedException (typeof (SecurityException))]
212                 public void LinkDemand_Deny_Anything ()
213                 {
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");
218                 }
219         }
220 }