New tests.
[mono.git] / mcs / class / corlib / System.Security / SecurityManager.cs
index 41546c13e97c10d0a666d75cc170d8c5a2618939..26da6e305bb0493d4810c97efc59dbb71cc59185 100644 (file)
 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 //
 
-#if !NET_2_1
+#if !MOONLIGHT
 
 using System.Collections;
+using System.Diagnostics;
 using System.Globalization;
 using System.IO;
 using System.Reflection;
@@ -52,16 +53,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;
@@ -86,9 +79,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,9 +89,19 @@ namespace System.Security {
                        set;
                }
 
+               internal static bool CheckElevatedPermissions ()
+               {
+                       return true; // always true outside Moonlight
+               }
+
+               [Conditional ("MOONLIGHT")]
+               internal static void EnsureElevatedPermissions ()
+               {
+                       // do nothing outside of Moonlight
+               }
+
                // 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!
@@ -112,7 +113,6 @@ namespace System.Security {
                        zone = new ArrayList ();
                        origin = new ArrayList ();
                }
-#endif
 
                public static bool IsGranted (IPermission perm)
                {
@@ -126,23 +126,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;
@@ -150,35 +142,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)
                {
@@ -188,18 +160,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 {
@@ -222,13 +184,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);
 
@@ -318,7 +275,6 @@ namespace System.Security {
                        return ps;
                }
 
-#if NET_2_0
                [MonoTODO ("(2.0) more tests are needed")]
                public static PermissionSet ResolvePolicy (Evidence[] evidences)
                {
@@ -355,7 +311,6 @@ namespace System.Security {
                        ResolveIdentityPermissions (ps, evidence);
                        return ps;
                }
-#endif
 
                static private SecurityPermission _execution = new SecurityPermission (SecurityPermissionFlag.Execution);
 
@@ -488,11 +443,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 ()) {
@@ -736,14 +690,11 @@ namespace System.Security {
                        throw new SecurityException (message, an, granted, refused, method, SecurityAction.InheritanceDemand, null, null, null);
                }
 
-               private static void FieldAccessException (IntPtr caller, IntPtr field)
-               {
-                       throw new FieldAccessException (Locale.GetText ("Field access not allowed."));
-               }
+               // called by the runtime when CoreCLR is enabled
 
-               private static void MethodAccessException (IntPtr caller, IntPtr callee)
+               private static void ThrowException (Exception ex)
                {
-                       throw new MethodAccessException (Locale.GetText ("Method call not allowed."));
+                       throw ex;
                }
 
                // internal - get called by the class loader