PageRenderTime 35ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/IronPython_Main/Runtime/Tests/SecurityTests/SecurityTests/SecurityTests.cs

#
C# | 90 lines | 65 code | 19 blank | 6 comment | 0 complexity | 216acc796cf5c6d644e1ab0450a6770c MD5 | raw file
Possible License(s): GPL-2.0, MPL-2.0-no-copyleft-exception, CPL-1.0, CC-BY-SA-3.0, BSD-3-Clause, ISC, AGPL-3.0, LGPL-2.1, Apache-2.0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Security;
  4. using System.Security.Policy;
  5. using System.Security.Permissions;
  6. using System.Reflection;
  7. using System.Reflection.Emit;
  8. using System.Runtime.Remoting;
  9. using SecurityTests;
  10. namespace SecurityTests {
  11. public partial class SecurityTests {
  12. public static List<string> Log = new List<string>();
  13. public static void Main() {
  14. try {
  15. var s = new System.Diagnostics.Stopwatch();
  16. s.Start();
  17. // trust level irrelevant for this test
  18. AppDomainDynamicTest(Utils.GetAD(false, true), false); // 4.0 policy, FT
  19. AppDomainDynamicTest(Utils.GetAD(true, true), true); // 3.5 policy, FT
  20. // homogenous irrelevant for this test, no dynamic calls
  21. AppDomainTrustTest(Utils.GetAD(true, true), false); // 3.5 policy, FT
  22. AppDomainTrustTest(Utils.GetAD(false, false), true); // 4.0 policy, PT
  23. AppDomainTrustTest(Utils.GetAD(true, false), true); // 3.5 policy, PT
  24. AppDomainTrustTest(Utils.GetAD(false, false, new ReflectionPermission(ReflectionPermissionFlag.MemberAccess)), false); // PT with necessary permissions
  25. // trust level irrelevant for this test
  26. CrossAppDomainDynamicTest(Utils.GetAD(false, true), Utils.GetAD(false, true), false); // Both homogenous
  27. CrossAppDomainDynamicTest(Utils.GetAD(true, true), Utils.GetAD(false, true), true); // Caller heterogeneous, both FT
  28. CrossAppDomainDynamicTest(Utils.GetAD(false, true), Utils.GetAD(true, false), false); // Caller homogenous, callee heterogeneous
  29. // homogenous irrelevant for this test, no dynamic calls
  30. CrossAppDomainTrustTest(Utils.GetAD(false, true), Utils.GetAD(false, true), false); // Both FT
  31. CrossAppDomainTrustTest(Utils.GetAD(true, true), Utils.GetAD(true, false), true); // Caller FT but Callee PT
  32. CrossAppDomainTrustTest(Utils.GetAD(false, false), Utils.GetAD(true, true), true); // Caller PT but Callee FT
  33. PartialTrustLINQTest(Utils.GetAD(false, true), false); // FT, homogenous
  34. PartialTrustLINQTest(Utils.GetAD(false, false), true); // PT
  35. PartialTrustLINQTest(Utils.GetAD(false, false, new System.Data.SqlClient.SqlClientPermission(PermissionState.Unrestricted)), false); // PT with necessary permissions
  36. BadNodeLINQTest(Utils.GetAD(false, true), true); // Both FT, homogenous
  37. IDMOPTest(Utils.GetAD(false, true), false); // homogenous
  38. IDMOPTest(Utils.GetAD(true, true), true); // heterogeneous
  39. CrossAppDomainIDMOPTest(Utils.GetAD(false, true), Utils.GetAD(false, true), false); // Both homogenous
  40. CrossAppDomainIDMOPTest(Utils.GetAD(true, true), Utils.GetAD(false, true), true); // Caller heterogeneous, both FT
  41. CrossAppDomainIDMOPTest(Utils.GetAD(false, true), Utils.GetAD(true, false, new ReflectionPermission(PermissionState.Unrestricted)), false); // Caller homogenous, callee heterogeneous
  42. // hetergeneous causes failures here but that's redundant with previous dynamic tests
  43. ExecuteAssemblyTest(Utils.GetAD(false, true), false); // FT
  44. ExecuteAssemblyTest(Utils.GetAD(false, false), true); // PT
  45. COMTrustTest(Utils.GetAD(false, true), false); // FT
  46. COMTrustTest(Utils.GetAD(false, false), true); // PT
  47. COMTrustTest(Utils.GetAD(false, false, new SecurityPermission(SecurityPermissionFlag.UnmanagedCode)), false); // PT with necessary permissions
  48. // Partial Trust AppDomain with evidence from signed assembly
  49. PermissionSet permissions = new PermissionSet(PermissionState.None);
  50. permissions.AddPermission(new SecurityPermission(SecurityPermissionFlag.Execution));
  51. AppDomain a = AppDomain.CreateDomain("Medium Trust Sandbox", null, new AppDomainSetup { ApplicationBase = Environment.CurrentDirectory }, permissions, typeof(InternalTypeTest.InternalTest).Assembly.Evidence.GetHostEvidence<StrongName>());
  52. TreeWithInternalTypeTest(a, true); // PT
  53. TreeWithInternalTypeTest(Utils.GetAD(false, true), false); // FT
  54. int TestCount = 0;
  55. foreach (var r in Log) {
  56. Console.WriteLine(r);
  57. TestCount++;
  58. }
  59. foreach (AppDomain d in Utils.AppDomainCache) {
  60. AppDomain.Unload(d);
  61. }
  62. s.Stop();
  63. Console.WriteLine("{0} tests ran in {1} seconds.", TestCount, s.Elapsed.TotalSeconds);
  64. } catch (DivideByZeroException e) {
  65. Console.WriteLine(e.GetType());
  66. Console.WriteLine(e.Message);
  67. Console.WriteLine(e.StackTrace);
  68. }
  69. }
  70. }
  71. }