PageRenderTime 26ms CodeModel.GetById 14ms app.highlight 9ms RepoModel.GetById 1ms 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
 1using System;
 2using System.Collections.Generic;
 3using System.Security;
 4using System.Security.Policy;
 5using System.Security.Permissions;
 6using System.Reflection;
 7using System.Reflection.Emit;
 8using System.Runtime.Remoting;
 9
10using SecurityTests;
11
12namespace SecurityTests {
13    public partial class SecurityTests {
14        public static List<string> Log = new List<string>();
15
16        public static void Main() {
17            try {
18                var s = new System.Diagnostics.Stopwatch();
19                s.Start();
20
21                // trust level irrelevant for this test
22                AppDomainDynamicTest(Utils.GetAD(false, true), false); // 4.0 policy, FT
23                AppDomainDynamicTest(Utils.GetAD(true, true), true);  // 3.5 policy, FT
24
25                // homogenous irrelevant for this test, no dynamic calls
26                AppDomainTrustTest(Utils.GetAD(true, true), false); // 3.5 policy, FT
27
28                AppDomainTrustTest(Utils.GetAD(false, false), true); // 4.0 policy, PT
29                AppDomainTrustTest(Utils.GetAD(true, false), true); // 3.5 policy, PT
30                AppDomainTrustTest(Utils.GetAD(false, false, new ReflectionPermission(ReflectionPermissionFlag.MemberAccess)), false); // PT with necessary permissions
31
32                // trust level irrelevant for this test
33                CrossAppDomainDynamicTest(Utils.GetAD(false, true), Utils.GetAD(false, true), false); // Both homogenous
34                CrossAppDomainDynamicTest(Utils.GetAD(true, true), Utils.GetAD(false, true), true); // Caller heterogeneous, both FT
35                CrossAppDomainDynamicTest(Utils.GetAD(false, true), Utils.GetAD(true, false), false); // Caller homogenous, callee heterogeneous
36
37                // homogenous irrelevant for this test, no dynamic calls
38                CrossAppDomainTrustTest(Utils.GetAD(false, true), Utils.GetAD(false, true), false); // Both FT
39                CrossAppDomainTrustTest(Utils.GetAD(true, true), Utils.GetAD(true, false), true); // Caller FT but Callee PT
40                CrossAppDomainTrustTest(Utils.GetAD(false, false), Utils.GetAD(true, true), true); // Caller PT but Callee FT
41
42                PartialTrustLINQTest(Utils.GetAD(false, true), false); // FT, homogenous
43                PartialTrustLINQTest(Utils.GetAD(false, false), true); // PT
44                PartialTrustLINQTest(Utils.GetAD(false, false, new System.Data.SqlClient.SqlClientPermission(PermissionState.Unrestricted)), false); // PT with necessary permissions
45
46                BadNodeLINQTest(Utils.GetAD(false, true), true); // Both FT, homogenous
47
48                IDMOPTest(Utils.GetAD(false, true), false); // homogenous
49                IDMOPTest(Utils.GetAD(true, true), true); // heterogeneous
50
51                CrossAppDomainIDMOPTest(Utils.GetAD(false, true), Utils.GetAD(false, true), false); // Both homogenous
52                CrossAppDomainIDMOPTest(Utils.GetAD(true, true), Utils.GetAD(false, true), true); // Caller heterogeneous, both FT
53                CrossAppDomainIDMOPTest(Utils.GetAD(false, true), Utils.GetAD(true, false, new ReflectionPermission(PermissionState.Unrestricted)), false); // Caller homogenous, callee heterogeneous
54
55                // hetergeneous causes failures here but that's redundant with previous dynamic tests
56                ExecuteAssemblyTest(Utils.GetAD(false, true), false); // FT
57                ExecuteAssemblyTest(Utils.GetAD(false, false), true); // PT
58
59                COMTrustTest(Utils.GetAD(false, true), false); // FT
60                COMTrustTest(Utils.GetAD(false, false), true); // PT
61                COMTrustTest(Utils.GetAD(false, false, new SecurityPermission(SecurityPermissionFlag.UnmanagedCode)), false); // PT with necessary permissions
62
63                // Partial Trust AppDomain with evidence from signed assembly
64                PermissionSet permissions = new PermissionSet(PermissionState.None);
65                permissions.AddPermission(new SecurityPermission(SecurityPermissionFlag.Execution));
66                AppDomain a = AppDomain.CreateDomain("Medium Trust Sandbox", null, new AppDomainSetup { ApplicationBase = Environment.CurrentDirectory }, permissions, typeof(InternalTypeTest.InternalTest).Assembly.Evidence.GetHostEvidence<StrongName>());
67
68                TreeWithInternalTypeTest(a, true); // PT
69                TreeWithInternalTypeTest(Utils.GetAD(false, true), false); // FT
70
71                int TestCount = 0;
72                foreach (var r in Log) {
73                    Console.WriteLine(r);
74                    TestCount++;
75                }
76
77                foreach (AppDomain d in Utils.AppDomainCache) {
78                    AppDomain.Unload(d);
79                }
80
81                s.Stop();
82                Console.WriteLine("{0} tests ran in {1} seconds.", TestCount, s.Elapsed.TotalSeconds);
83            } catch (DivideByZeroException e) {
84                Console.WriteLine(e.GetType());
85                Console.WriteLine(e.Message);
86                Console.WriteLine(e.StackTrace);
87            }
88        }
89    }
90}