New test.
[mono.git] / mcs / class / corlib / System.Security / SecurityManager.cs
index a0193de0e20e70a41c8c83dd8086610abc7c1b72..531b5439a410f1a87bf772c47752c13c442eb1e6 100644 (file)
@@ -29,6 +29,8 @@
 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 //
 
+#if !MOONLIGHT
+
 using System.Collections;
 using System.Globalization;
 using System.IO;
@@ -50,16 +52,8 @@ namespace System.Security {
                public RuntimeDeclSecurityEntry choice;
        }
 
-#if NET_2_0
        [ComVisible (true)]
        public static class SecurityManager {
-#else
-       public sealed class SecurityManager {
-
-               private SecurityManager ()
-               {
-               }
-#endif
                private static object _lockObject;
                private static ArrayList _hierarchy;
                private static IPermission _unmanagedCode;
@@ -84,9 +78,7 @@ namespace System.Security {
                        set;
                }
 
-#if NET_2_0
                [Obsolete ("The security manager cannot be turned off on MS runtime")]
-#endif
                extern public static bool SecurityEnabled {
                        [MethodImplAttribute (MethodImplOptions.InternalCall)]
                        get;
@@ -98,18 +90,17 @@ namespace System.Security {
 
                // methods
 
-#if NET_1_1
                // NOTE: This method doesn't show in the class library status page because
                // it cannot be "found" with the StrongNameIdentityPermission for ECMA key.
                // But it's there!
-               [MonoTODO ("works for fulltrust (empty), documentation doesn't really make sense, type wise")]
+               // FIXME works for fulltrust (empty), documentation doesn't really make sense, type wise
+               [MonoTODO ("CAS support is experimental (and unsupported). This method only works in FullTrust.")]
                [StrongNameIdentityPermission (SecurityAction.LinkDemand, PublicKey = "0x00000000000000000400000000000000")]
                public static void GetZoneAndOrigin (out ArrayList zone, out ArrayList origin) 
                {
                        zone = new ArrayList ();
                        origin = new ArrayList ();
                }
-#endif
 
                public static bool IsGranted (IPermission perm)
                {
@@ -123,23 +114,15 @@ namespace System.Security {
                        // - Not affected by overrides (like Assert, Deny and PermitOnly)
                        // - calls IsSubsetOf even for non CAS permissions
                        //   (i.e. it does call Demand so any code there won't be executed)
-#if NET_2_0
                        // with 2.0 identity permission are unrestrictable
                        return IsGranted (Assembly.GetCallingAssembly (), perm);
-#else
-                       if (perm is IUnrestrictedPermission)
-                               return IsGranted (Assembly.GetCallingAssembly (), perm);
-                       else
-                               return IsGrantedRestricted (Assembly.GetCallingAssembly (), perm);
-#endif
                }
 
-#if !NET_2_0
-               // only for permissions that do not implement IUnrestrictedPermission
-               internal static bool IsGrantedRestricted (Assembly a, IPermission perm)
+               // note: in 2.0 *all* permissions (including identity permissions) support unrestricted
+               internal static bool IsGranted (Assembly a, IPermission perm)
                {
-                       PermissionSet granted = a.GrantedPermissionSet;\r
-                       if (granted != null) {\r
+                       PermissionSet granted = a.GrantedPermissionSet;
+                       if ((granted != null) && !granted.IsUnrestricted ()) {
                                CodeAccessPermission grant = (CodeAccessPermission) granted.GetPermission (perm.GetType ());
                                if (!perm.IsSubsetOf (grant)) {
                                        return false;
@@ -147,35 +130,15 @@ namespace System.Security {
                        }
 
                        PermissionSet denied = a.DeniedPermissionSet;
-                       if (denied != null) {\r
+                       if ((denied != null) && !denied.IsEmpty ()) {
+                               if (denied.IsUnrestricted ())
+                                       return false;
                                CodeAccessPermission refuse = (CodeAccessPermission) a.DeniedPermissionSet.GetPermission (perm.GetType ());
                                if ((refuse != null) && perm.IsSubsetOf (refuse))
                                        return false;
                        }
                        return true;
                }
-#endif
-               // note: in 2.0 *all* permissions (including identity permissions) support unrestricted
-               internal static bool IsGranted (Assembly a, IPermission perm)
-               {
-                       PermissionSet granted = a.GrantedPermissionSet;\r
-                       if ((granted != null) && !granted.IsUnrestricted ()) {\r
-                               CodeAccessPermission grant = (CodeAccessPermission) granted.GetPermission (perm.GetType ());\r
-                               if (!perm.IsSubsetOf (grant)) {\r
-                                       return false;\r
-                               }\r
-                       }\r
-\r
-                       PermissionSet denied = a.DeniedPermissionSet;\r
-                       if ((denied != null) && !denied.IsEmpty ()) {\r
-                               if (denied.IsUnrestricted ())\r
-                                       return false;\r
-                               CodeAccessPermission refuse = (CodeAccessPermission) a.DeniedPermissionSet.GetPermission (perm.GetType ());\r
-                               if ((refuse != null) && perm.IsSubsetOf (refuse))\r
-                                       return false;\r
-                       }\r
-                       return true;\r
-               }
 
                internal static IPermission CheckPermissionSet (Assembly a, PermissionSet ps, bool noncas)
                {
@@ -185,18 +148,8 @@ namespace System.Security {
                        foreach (IPermission p in ps) {
                                // note: this may contains non CAS permissions
                                if ((!noncas) && (p is CodeAccessPermission)) {
-#if NET_2_0
                                        if (!IsGranted (a, p))
                                                return p;
-#else
-                                       if (p is IUnrestrictedPermission) {
-                                               if (!IsGranted (a, p))
-                                                       return p;
-                                       } else {
-                                               if (!IsGrantedRestricted (a, p))
-                                                       return p;
-                                       }
-#endif
                                } else {
                                        // but non-CAS will throw on failure...
                                        try {
@@ -219,13 +172,8 @@ namespace System.Security {
                        PermissionSet granted = ad.GrantedPermissionSet;
                        if (granted == null)
                                return null;
-#if NET_2_0
                        if (granted.IsUnrestricted ())
                                return null;
-#else
-                       if ((granted.Count == 0) && granted.IsUnrestricted ())
-                               return null;
-#endif
                        if (ps.IsUnrestricted ())
                                return new SecurityPermission (SecurityPermissionFlag.NoFlags);
 
@@ -315,7 +263,6 @@ namespace System.Security {
                        return ps;
                }
 
-#if NET_2_0
                [MonoTODO ("(2.0) more tests are needed")]
                public static PermissionSet ResolvePolicy (Evidence[] evidences)
                {
@@ -352,7 +299,6 @@ namespace System.Security {
                        ResolveIdentityPermissions (ps, evidence);
                        return ps;
                }
-#endif
 
                static private SecurityPermission _execution = new SecurityPermission (SecurityPermissionFlag.Execution);
 
@@ -485,11 +431,10 @@ namespace System.Security {
 
                internal static void ResolveIdentityPermissions (PermissionSet ps, Evidence evidence)
                {
-#if NET_2_0
                        // in 2.0 identity permissions can now be unrestricted
                        if (ps.IsUnrestricted ())
                                return;
-#endif
+
                        // Only host evidence are used for policy resolution
                        IEnumerator ee = evidence.GetHostEnumerator ();
                        while (ee.MoveNext ()) {
@@ -610,13 +555,6 @@ namespace System.Security {
                        return LinkDemand (mb.ReflectedType.Assembly, &klass, &method);
                }
 
-               // internal - get called at JIT time
-
-               private static void DemandUnmanaged ()
-               {
-                       UnmanagedCode.Demand ();
-               }
-
                private unsafe static bool LinkDemand (Assembly a, RuntimeDeclSecurityActions *klass, RuntimeDeclSecurityActions *method)
                {
                        try {
@@ -646,6 +584,7 @@ namespace System.Security {
                        }
                }
 
+#pragma warning disable 169
                private static bool LinkDemandFullTrust (Assembly a)
                {
                        // FullTrust is immutable (and means Unrestricted) 
@@ -668,8 +607,12 @@ namespace System.Security {
                }
 
                // we try to provide as much details as possible to help debugging
-               private static void LinkDemandSecurityException (int securityViolation, Assembly a, MethodInfo method)
+               private static void LinkDemandSecurityException (int securityViolation, IntPtr methodHandle)
                {
+                       RuntimeMethodHandle runtimeHandle = new RuntimeMethodHandle (methodHandle);
+                       MethodInfo method = (MethodInfo)(MethodBase.GetMethodFromHandle (runtimeHandle));
+                       Assembly a = method.DeclaringType.Assembly;
+
                        string message = null;
                        AssemblyName an = null;
                        PermissionSet granted = null;
@@ -735,6 +678,13 @@ namespace System.Security {
                        throw new SecurityException (message, an, granted, refused, method, SecurityAction.InheritanceDemand, null, null, null);
                }
 
+               // called by the runtime when CoreCLR is enabled
+
+               private static void ThrowException (Exception ex)
+               {
+                       throw ex;
+               }
+
                // internal - get called by the class loader
 
                // Called when
@@ -768,6 +718,12 @@ namespace System.Security {
                        }
                }
 
+               // internal - get called at JIT time
+
+               private static void DemandUnmanaged ()
+               {
+                       UnmanagedCode.Demand ();
+               }
 
                // internal - get called by JIT generated code
 
@@ -781,5 +737,9 @@ namespace System.Security {
                {
                        throw new SecurityException ("SecurityAction.DemandChoice was removed from 2.0");
                }
+#pragma warning restore 169            
        }
 }
+
+#endif
+