ExecutorCas.cs 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. //
  2. // ExecutorCas.cs
  3. // - CAS unit tests for System.CodeDom.Compiler.Executor
  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.IO;
  33. using System.Reflection;
  34. using System.Security;
  35. using System.Security.Permissions;
  36. using System.Security.Policy;
  37. using MonoTests.System.CodeDom.Compiler;
  38. namespace MonoCasTests.System.CodeDom.Compiler {
  39. [TestFixture]
  40. [Category ("CAS")]
  41. public class ExecutorCas {
  42. [SetUp]
  43. public void SetUp ()
  44. {
  45. if (!SecurityManager.SecurityEnabled)
  46. Assert.Ignore ("SecurityManager.SecurityEnabled is OFF");
  47. }
  48. private string cmd;
  49. private TempFileCollection tfc;
  50. private ExecutorTest unit;
  51. private MethodInfo execWaitWithCapture;
  52. [TestFixtureSetUp]
  53. public void FixtureSetUp ()
  54. {
  55. // at fulltrust
  56. tfc = new TempFileCollection ();
  57. cmd = "ping"; // available everywhere
  58. unit = new ExecutorTest ();
  59. unit.FixtureSetUp ();
  60. // for linkdemands tests
  61. MethodInfo[] methods = typeof (Executor).GetMethods ();
  62. foreach (MethodInfo mi in methods) {
  63. if ((mi.Name == "ExecWaitWithCapture") && (mi.GetParameters ().Length == 4)) {
  64. execWaitWithCapture = mi;
  65. break;
  66. }
  67. }
  68. }
  69. [Test]
  70. [FileIOPermission (SecurityAction.PermitOnly, Unrestricted = true)]
  71. [SecurityPermission (SecurityAction.PermitOnly, UnmanagedCode = true)]
  72. [EnvironmentPermission (SecurityAction.PermitOnly, Unrestricted = true)]
  73. public void ReuseUnitTests_PermitOnly ()
  74. {
  75. unit.ExecWaitWithCapture ();
  76. unit.ExecWaitWithCapture_CurrentDir ();
  77. unit.ExecWaitWithCapture_Token ();
  78. unit.ExecWaitWithCapture_Token_CurrentDir ();
  79. }
  80. [Test]
  81. [FileIOPermission (SecurityAction.Deny, Unrestricted = true)]
  82. [ExpectedException (typeof (SecurityException))]
  83. public void ExecWaitWithCapture_Deny_FileIO ()
  84. {
  85. unit.ExecWaitWithCapture ();
  86. }
  87. [Test]
  88. [SecurityPermission (SecurityAction.Deny, UnmanagedCode = true)]
  89. [ExpectedException (typeof (SecurityException))]
  90. public void ExecWaitWithCapture_Deny_UnmanagedCode ()
  91. {
  92. unit.ExecWaitWithCapture ();
  93. }
  94. [Test]
  95. [EnvironmentPermission (SecurityAction.Deny, Unrestricted = true)]
  96. [ExpectedException (typeof (SecurityException))]
  97. public void ExecWaitWithCapture_Deny_Environment ()
  98. {
  99. unit.ExecWaitWithCapture ();
  100. }
  101. [Test]
  102. public void LinkDemand_No_Restriction ()
  103. {
  104. Assert.IsNotNull (execWaitWithCapture, "ExecWaitWithCapture");
  105. string output = null;
  106. string error = null;
  107. Assert.IsNotNull (execWaitWithCapture.Invoke (null, new object[4] { cmd, tfc, output, error }), "invoke");
  108. }
  109. [Test]
  110. [FileIOPermission (SecurityAction.PermitOnly, Unrestricted = true)]
  111. [SecurityPermission (SecurityAction.PermitOnly, UnmanagedCode = true)]
  112. [EnvironmentPermission (SecurityAction.PermitOnly, Unrestricted = true)]
  113. [ExpectedException (typeof (SecurityException))]
  114. public void LinkDemand_PermitOnly ()
  115. {
  116. Assert.IsNotNull (execWaitWithCapture, "ExecWaitWithCapture");
  117. string output = null;
  118. string error = null;
  119. Assert.IsNotNull (execWaitWithCapture.Invoke (null, new object[4] { cmd, tfc, output, error }), "invoke");
  120. // it's not enough, so there's a LinkDemand on the class
  121. }
  122. [Test]
  123. [SecurityPermission (SecurityAction.Deny, UnmanagedCode = true)]
  124. [ExpectedException (typeof (SecurityException))]
  125. public void LinkDemand_Deny_UnmanagedCode ()
  126. {
  127. // denying anything results in a non unrestricted permission set
  128. Assert.IsNotNull (execWaitWithCapture, "ExecWaitWithCapture");
  129. string output = null;
  130. string error = null;
  131. Assert.IsNotNull (execWaitWithCapture.Invoke (null, new object[4] { cmd, tfc, output, error }), "invoke");
  132. }
  133. [Test]
  134. [FileIOPermission (SecurityAction.Deny, Unrestricted = true)]
  135. [ExpectedException (typeof (SecurityException))]
  136. public void LinkDemand_Deny_FileIO ()
  137. {
  138. Assert.IsNotNull (execWaitWithCapture, "ExecWaitWithCapture");
  139. string output = null;
  140. string error = null;
  141. Assert.IsNotNull (execWaitWithCapture.Invoke (null, new object[4] { cmd, tfc, output, error }), "invoke");
  142. }
  143. [Test]
  144. [EnvironmentPermission (SecurityAction.Deny, Read = "Mono")]
  145. [ExpectedException (typeof (SecurityException))]
  146. public void LinkDemand_Deny_Environment ()
  147. {
  148. // denying anything results in a non unrestricted permission set
  149. Assert.IsNotNull (execWaitWithCapture, "ExecWaitWithCapture");
  150. string output = null;
  151. string error = null;
  152. Assert.IsNotNull (execWaitWithCapture.Invoke (null, new object[4] { cmd, tfc, output, error }), "invoke");
  153. }
  154. }
  155. }