CompilerParametersCas.cs 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. //
  2. // CompilerParametersCas.cs
  3. // - CAS unit tests for System.CodeDom.Compiler.CompilerParameters
  4. //
  5. // Author:
  6. // Sebastien Pouliot <[email protected]>
  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. using NUnit.Framework;
  30. using System;
  31. using System.CodeDom.Compiler;
  32. using System.Reflection;
  33. using System.Security;
  34. using System.Security.Permissions;
  35. using System.Security.Policy;
  36. namespace MonoCasTests.System.CodeDom.Compiler {
  37. [TestFixture]
  38. [Category ("CAS")]
  39. public class CompilerParametersCas {
  40. [SetUp]
  41. public void SetUp ()
  42. {
  43. if (!SecurityManager.SecurityEnabled)
  44. Assert.Ignore ("SecurityManager.SecurityEnabled is OFF");
  45. }
  46. [Test]
  47. [PermissionSet (SecurityAction.Deny, Unrestricted = true)]
  48. public void Constructor0_Deny_Unrestricted ()
  49. {
  50. CompilerParameters cp = new CompilerParameters ();
  51. Assert.IsNull (cp.CompilerOptions, "CompilerOptions");
  52. cp.CompilerOptions = "-debug";
  53. Assert.IsNull (cp.Evidence, "Evidence");
  54. Assert.IsFalse (cp.GenerateExecutable, "GenerateExecutable");
  55. cp.GenerateExecutable = true;
  56. Assert.IsFalse (cp.GenerateInMemory, "GenerateInMemory");
  57. cp.GenerateInMemory = true;
  58. Assert.IsFalse (cp.IncludeDebugInformation, "IncludeDebugInformation");
  59. cp.IncludeDebugInformation = true;
  60. Assert.IsNull (cp.MainClass, "MainClass");
  61. cp.MainClass = "Program";
  62. Assert.IsNull (cp.OutputAssembly, "OutputAssembly");
  63. cp.OutputAssembly = "mono.dll";
  64. Assert.AreEqual (0, cp.ReferencedAssemblies.Count, "ReferencedAssemblies");
  65. Assert.AreEqual (0, cp.TempFiles.Count, "TempFiles");
  66. cp.TempFiles = new TempFileCollection ();
  67. Assert.AreEqual (IntPtr.Zero, cp.UserToken, "UserToken");
  68. cp.UserToken = (IntPtr) 1;
  69. Assert.AreEqual (-1, cp.WarningLevel, "WarningLevel");
  70. cp.WarningLevel = 0;
  71. Assert.IsNull (cp.Win32Resource, "Win32Resource");
  72. cp.Win32Resource = "*";
  73. #if NET_2_0
  74. Assert.AreEqual (0, cp.EmbeddedResources.Count, "EmbeddedResources");
  75. Assert.AreEqual (0, cp.LinkedResources.Count, "LinkedResources");
  76. #endif
  77. }
  78. [Test]
  79. [PermissionSet (SecurityAction.Deny, Unrestricted = true)]
  80. public void Constructor1_Deny_Unrestricted ()
  81. {
  82. CompilerParameters cp = new CompilerParameters (new string[1] { "mono.exe" });
  83. Assert.IsNull (cp.CompilerOptions, "CompilerOptions");
  84. cp.CompilerOptions = "-debug";
  85. Assert.IsNull (cp.Evidence, "Evidence");
  86. Assert.IsFalse (cp.GenerateExecutable, "GenerateExecutable");
  87. cp.GenerateExecutable = true;
  88. Assert.IsFalse (cp.GenerateInMemory, "GenerateInMemory");
  89. cp.GenerateInMemory = true;
  90. Assert.IsFalse (cp.IncludeDebugInformation, "IncludeDebugInformation");
  91. cp.IncludeDebugInformation = true;
  92. Assert.IsNull (cp.MainClass, "MainClass");
  93. cp.MainClass = "Program";
  94. Assert.IsNull (cp.OutputAssembly, "OutputAssembly");
  95. cp.OutputAssembly = "mono.dll";
  96. Assert.AreEqual (1, cp.ReferencedAssemblies.Count, "ReferencedAssemblies");
  97. Assert.AreEqual (0, cp.TempFiles.Count, "TempFiles");
  98. cp.TempFiles = new TempFileCollection ();
  99. Assert.AreEqual (IntPtr.Zero, cp.UserToken, "UserToken");
  100. cp.UserToken = (IntPtr) 1;
  101. Assert.AreEqual (-1, cp.WarningLevel, "WarningLevel");
  102. cp.WarningLevel = 0;
  103. Assert.IsNull (cp.Win32Resource, "Win32Resource");
  104. cp.Win32Resource = "*";
  105. #if NET_2_0
  106. Assert.AreEqual (0, cp.EmbeddedResources.Count, "EmbeddedResources");
  107. Assert.AreEqual (0, cp.LinkedResources.Count, "LinkedResources");
  108. #endif
  109. }
  110. [Test]
  111. [PermissionSet (SecurityAction.Deny, Unrestricted = true)]
  112. public void Constructor2_Deny_Unrestricted ()
  113. {
  114. CompilerParameters cp = new CompilerParameters (new string[1] { "mono.exe" }, "mono.dll");
  115. Assert.IsNull (cp.CompilerOptions, "CompilerOptions");
  116. cp.CompilerOptions = "-debug";
  117. Assert.IsNull (cp.Evidence, "Evidence");
  118. Assert.IsFalse (cp.GenerateExecutable, "GenerateExecutable");
  119. cp.GenerateExecutable = true;
  120. Assert.IsFalse (cp.GenerateInMemory, "GenerateInMemory");
  121. cp.GenerateInMemory = true;
  122. Assert.IsFalse (cp.IncludeDebugInformation, "IncludeDebugInformation");
  123. cp.IncludeDebugInformation = true;
  124. Assert.IsNull (cp.MainClass, "MainClass");
  125. cp.MainClass = "Program";
  126. Assert.AreEqual ("mono.dll", cp.OutputAssembly, "OutputAssembly");
  127. cp.OutputAssembly = null;
  128. Assert.AreEqual (1, cp.ReferencedAssemblies.Count, "ReferencedAssemblies");
  129. Assert.AreEqual (0, cp.TempFiles.Count, "TempFiles");
  130. cp.TempFiles = new TempFileCollection ();
  131. Assert.AreEqual (IntPtr.Zero, cp.UserToken, "UserToken");
  132. cp.UserToken = (IntPtr) 1;
  133. Assert.AreEqual (-1, cp.WarningLevel, "WarningLevel");
  134. cp.WarningLevel = 0;
  135. Assert.IsNull (cp.Win32Resource, "Win32Resource");
  136. cp.Win32Resource = "*";
  137. #if NET_2_0
  138. Assert.AreEqual (0, cp.EmbeddedResources.Count, "EmbeddedResources");
  139. Assert.AreEqual (0, cp.LinkedResources.Count, "LinkedResources");
  140. #endif
  141. }
  142. [Test]
  143. [PermissionSet (SecurityAction.Deny, Unrestricted = true)]
  144. public void Constructor3_Deny_Unrestricted ()
  145. {
  146. CompilerParameters cp = new CompilerParameters (new string[1] { "mono.exe" }, "mono.dll", true);
  147. Assert.IsNull (cp.CompilerOptions, "CompilerOptions");
  148. cp.CompilerOptions = "-debug";
  149. Assert.IsNull (cp.Evidence, "Evidence");
  150. Assert.IsFalse (cp.GenerateExecutable, "GenerateExecutable");
  151. cp.GenerateExecutable = true;
  152. Assert.IsFalse (cp.GenerateInMemory, "GenerateInMemory");
  153. cp.GenerateInMemory = true;
  154. Assert.IsTrue (cp.IncludeDebugInformation, "IncludeDebugInformation");
  155. cp.IncludeDebugInformation = false;
  156. Assert.IsNull (cp.MainClass, "MainClass");
  157. cp.MainClass = "Program";
  158. Assert.AreEqual ("mono.dll", cp.OutputAssembly, "OutputAssembly");
  159. cp.OutputAssembly = null;
  160. Assert.AreEqual (1, cp.ReferencedAssemblies.Count, "ReferencedAssemblies");
  161. Assert.AreEqual (0, cp.TempFiles.Count, "TempFiles");
  162. cp.TempFiles = new TempFileCollection ();
  163. Assert.AreEqual (IntPtr.Zero, cp.UserToken, "UserToken");
  164. cp.UserToken = (IntPtr) 1;
  165. Assert.AreEqual (-1, cp.WarningLevel, "WarningLevel");
  166. cp.WarningLevel = 0;
  167. Assert.IsNull (cp.Win32Resource, "Win32Resource");
  168. cp.Win32Resource = "*";
  169. #if NET_2_0
  170. Assert.AreEqual (0, cp.EmbeddedResources.Count, "EmbeddedResources");
  171. Assert.AreEqual (0, cp.LinkedResources.Count, "LinkedResources");
  172. #endif
  173. }
  174. [Test]
  175. [SecurityPermission (SecurityAction.PermitOnly, ControlEvidence = true)]
  176. public void Evidence_PermitOnly_Unrestricted ()
  177. {
  178. CompilerParameters cp = new CompilerParameters ();
  179. cp.Evidence = new Evidence ();
  180. }
  181. [Test]
  182. [SecurityPermission (SecurityAction.Deny, ControlEvidence = true)]
  183. [ExpectedException (typeof (SecurityException))]
  184. public void Evidence_Deny_Unrestricted ()
  185. {
  186. CompilerParameters cp = new CompilerParameters ();
  187. cp.Evidence = new Evidence ();
  188. }
  189. [Test]
  190. public void LinkDemand_No_Restriction ()
  191. {
  192. ConstructorInfo ci = typeof (CompilerParameters).GetConstructor (new Type[0]);
  193. Assert.IsNotNull (ci, "default .ctor()");
  194. Assert.IsNotNull (ci.Invoke (null), "invoke");
  195. }
  196. [Test]
  197. [EnvironmentPermission (SecurityAction.Deny, Read = "Mono")]
  198. [ExpectedException (typeof (SecurityException))]
  199. public void LinkDemand_Deny_Anything ()
  200. {
  201. // denying anything results in a non unrestricted permission set
  202. ConstructorInfo ci = typeof (CompilerParameters).GetConstructor (new Type[0]);
  203. Assert.IsNotNull (ci, "default .ctor()");
  204. Assert.IsNotNull (ci.Invoke (null), "invoke");
  205. }
  206. }
  207. }