2
0

CompilerResultsCas.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. //
  2. // CompilerResultsCas.cs
  3. // - CAS unit tests for System.CodeDom.Compiler.CompilerResults
  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 CompilerResultsCas {
  40. [SetUp]
  41. public void SetUp ()
  42. {
  43. if (!SecurityManager.SecurityEnabled)
  44. Assert.Ignore ("SecurityManager.SecurityEnabled is OFF");
  45. }
  46. private TempFileCollection temps;
  47. private Assembly assembly;
  48. private string path;
  49. [TestFixtureSetUp]
  50. public void FixtureSetUp ()
  51. {
  52. // at full trust
  53. temps = new TempFileCollection ();
  54. assembly = Assembly.GetExecutingAssembly ();
  55. path = assembly.Location;
  56. }
  57. [Test]
  58. [PermissionSet (SecurityAction.Deny, Unrestricted = true)]
  59. public void Deny_Unrestricted ()
  60. {
  61. CompilerResults cr = new CompilerResults (temps);
  62. Assert.IsNull (cr.CompiledAssembly, "CompiledAssembly");
  63. cr.CompiledAssembly = assembly;
  64. Assert.AreEqual (0, cr.Errors.Count, "Errors");
  65. Assert.IsNull (cr.Evidence, "Evidence");
  66. Assert.AreEqual (0, cr.NativeCompilerReturnValue, "NativeCompilerReturnValue");
  67. cr.NativeCompilerReturnValue = 1;
  68. Assert.AreEqual (0, cr.Output.Count, "Output");
  69. Assert.IsNull (cr.PathToAssembly, "PathToAssembly");
  70. cr.PathToAssembly = path;
  71. Assert.AreEqual (0, cr.TempFiles.Count, "TempFiles");
  72. cr.TempFiles = new TempFileCollection ();
  73. }
  74. [Test]
  75. [SecurityPermission (SecurityAction.PermitOnly, ControlEvidence = true)]
  76. public void Evidence_PermitOnly_Unrestricted ()
  77. {
  78. CompilerResults cr = new CompilerResults (temps);
  79. cr.Evidence = new Evidence ();
  80. }
  81. [Test]
  82. [SecurityPermission (SecurityAction.Deny, ControlEvidence = true)]
  83. [ExpectedException (typeof (SecurityException))]
  84. public void Evidence_Deny_Unrestricted ()
  85. {
  86. CompilerResults cr = new CompilerResults (temps);
  87. cr.Evidence = new Evidence ();
  88. }
  89. [Test]
  90. public void LinkDemand_No_Restriction ()
  91. {
  92. Type[] types = new Type[1] { typeof (TempFileCollection) };
  93. ConstructorInfo ci = typeof (CompilerResults).GetConstructor (types);
  94. Assert.IsNotNull (ci, ".ctor(TempFileCollection)");
  95. Assert.IsNotNull (ci.Invoke (new object[1] { temps }), "invoke");
  96. }
  97. [Test]
  98. [EnvironmentPermission (SecurityAction.Deny, Read = "Mono")]
  99. [ExpectedException (typeof (SecurityException))]
  100. public void LinkDemand_Deny_Anything ()
  101. {
  102. // denying anything results in a non unrestricted permission set
  103. Type[] types = new Type[1] { typeof (TempFileCollection) };
  104. ConstructorInfo ci = typeof (CompilerResults).GetConstructor (types);
  105. Assert.IsNotNull (ci, ".ctor(TempFileCollection)");
  106. Assert.IsNotNull (ci.Invoke (new object[1] { temps }), "invoke");
  107. }
  108. }
  109. }