Merge pull request #3622 from rolfbjarne/remove-stray-file
[mono.git] / mcs / class / corlib / System.Security.Permissions / ReflectionPermission.cs
index 5fca73d668913f0cf8b6ad6f71d8aecbd8ae0198..b26c566a3b9f5610b08507d493f9b0e775b4e735 100644 (file)
@@ -3,59 +3,75 @@
 //
 // Authors:
 //     Tim Coleman <tim@timcoleman.com>
-//     Sebastien Pouliot (spouliot@motus.com)
+//     Sebastien Pouliot  <sebastien@ximian.com>
 //
 // Copyright (C) 2002, Tim Coleman
 // Portions (C) 2003 Motus Technologies Inc. (http://www.motus.com)
+// Copyright (C) 2004-2005 Novell, Inc (http://www.novell.com)
+//
+// Permission is hereby granted, free of charge, to any person obtaining
+// a copy of this software and associated documentation files (the
+// "Software"), to deal in the Software without restriction, including
+// without limitation the rights to use, copy, modify, merge, publish,
+// distribute, sublicense, and/or sell copies of the Software, and to
+// permit persons to whom the Software is furnished to do so, subject to
+// the following conditions:
+// 
+// The above copyright notice and this permission notice shall be
+// included in all copies or substantial portions of the Software.
+// 
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
+// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
+// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
+// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 //
 
-using System;
+using System.Globalization;
+using System.Runtime.InteropServices;
 
 namespace System.Security.Permissions {
 
+       [ComVisible (true)]
        [Serializable]
        public sealed class ReflectionPermission : CodeAccessPermission, IUnrestrictedPermission, IBuiltInPermission {
 
-               #region Fields
+               private const int version = 1;
 
                ReflectionPermissionFlag flags;
-               PermissionState state;
-
-               #endregion // Fields
 
-               #region Constructors
 
                public ReflectionPermission (PermissionState state)
                {
-                       switch (state) {
-                               case PermissionState.None:
-                                       flags = ReflectionPermissionFlag.NoFlags;
-                                       break;
-                               case PermissionState.Unrestricted:
-                                       flags = ReflectionPermissionFlag.AllFlags;
-                                       break;
-                               default:
-                                       throw new ArgumentException ("Invalid PermissionState");
-                       }
+                       if (CheckPermissionState (state, true) == PermissionState.Unrestricted)
+                               flags = ReflectionPermissionFlag.AllFlags;
+                       else
+                               flags = ReflectionPermissionFlag.NoFlags;
                }
 
                public ReflectionPermission (ReflectionPermissionFlag flag)
                {
-                       flags = flag;
+                       // reuse validation by the Flags property
+                       Flags = flag;
                }
 
-               #endregion // Constructors
-
-               #region Properties
 
                public ReflectionPermissionFlag Flags {
                        get { return flags; }
-                       set { flags = value; }
-               }
+                       set {
+                               const ReflectionPermissionFlag all_flags = ReflectionPermissionFlag.AllFlags | ReflectionPermissionFlag.RestrictedMemberAccess;
+
+                               if ((value & all_flags) != value) {
+                                       string msg = String.Format (Locale.GetText ("Invalid flags {0}"), value);
+                                       throw new ArgumentException (msg, "ReflectionPermissionFlag");
+                               }
 
-               #endregion // Properties
+                               flags = value;
+                       }
+               }
 
-               #region Methods
 
                public override IPermission Copy ()
                {
@@ -64,17 +80,14 @@ namespace System.Security.Permissions {
 
                public override void FromXml (SecurityElement esd)
                {
-                       if (esd == null)
-                               throw new ArgumentNullException ("esd");
-                       if (esd.Tag != "IPermission")
-                               throw new ArgumentException ("not IPermission");
-                       if (!(esd.Attributes ["class"] as string).StartsWith ("System.Security.Permissions.ReflectionPermission"))
-                               throw new ArgumentException ("not ReflectionPermission");
-                       if ((esd.Attributes ["version"] as string) != "1")
-                               throw new ArgumentException ("wrong version");
-
-                       if ((esd.Attributes ["Unrestricted"] as string) == "true")
+                       // General validation in CodeAccessPermission
+                       CheckSecurityElement (esd, "esd", version, version);
+                       // Note: we do not (yet) care about the return value 
+                       // as we only accept version 1 (min/max values)
+
+                       if (IsUnrestricted (esd)) {
                                flags = ReflectionPermissionFlag.AllFlags;
+                       }
                        else {
                                flags = ReflectionPermissionFlag.NoFlags;
                                string xmlFlags = (esd.Attributes ["Flags"] as string);
@@ -89,45 +102,40 @@ namespace System.Security.Permissions {
 
                public override IPermission Intersect (IPermission target)
                {
-                       if (target == null)
+                       ReflectionPermission rp = Cast (target);
+                       if (rp == null)
                                return null;
-                       if (! (target is ReflectionPermission))
-                               throw new ArgumentException ("wrong type");
 
-                       ReflectionPermission o = (ReflectionPermission) target;
                        if (IsUnrestricted ()) {
-                               if (o.Flags == ReflectionPermissionFlag.NoFlags)
+                               if (rp.Flags == ReflectionPermissionFlag.NoFlags)
                                        return null;
                                else
-                                       return o.Copy ();
+                                       return rp.Copy ();
                        }
-                       if (o.IsUnrestricted ()) {
+                       if (rp.IsUnrestricted ()) {
                                if (flags == ReflectionPermissionFlag.NoFlags)
                                        return null;
                                else
                                        return Copy ();
                        }
 
-                       ReflectionPermission p = (ReflectionPermission) o.Copy ();
+                       ReflectionPermission p = (ReflectionPermission) rp.Copy ();
                        p.Flags &= flags;
                        return ((p.Flags == ReflectionPermissionFlag.NoFlags) ? null : p);
                }
 
                public override bool IsSubsetOf (IPermission target)
                {
-                       if (target == null)
+                       ReflectionPermission rp = Cast (target);
+                       if (rp == null)
                                return (flags == ReflectionPermissionFlag.NoFlags);
 
-                       if (! (target is ReflectionPermission))
-                               throw new ArgumentException ("wrong type");
-
-                       ReflectionPermission o = (ReflectionPermission) target;
                        if (IsUnrestricted ())
-                               return o.IsUnrestricted ();
-                       else if (o.IsUnrestricted ())
+                               return rp.IsUnrestricted ();
+                       else if (rp.IsUnrestricted ())
                                return true;
 
-                       return ((flags & o.Flags) == flags);
+                       return ((flags & rp.Flags) == flags);
                }
 
                public bool IsUnrestricted ()
@@ -137,7 +145,7 @@ namespace System.Security.Permissions {
 
                public override SecurityElement ToXml ()
                {
-                       SecurityElement se = Element (this, 1);
+                       SecurityElement se = Element (version);
                        if (IsUnrestricted ()) {
                                se.AddAttribute ("Unrestricted", "true");
                        }
@@ -168,16 +176,14 @@ namespace System.Security.Permissions {
 
                public override IPermission Union (IPermission other)
                {
+                       ReflectionPermission rp = Cast (other);
                        if (other == null)
                                return Copy ();
-                       if (! (other is ReflectionPermission))
-                               throw new ArgumentException ("wrong type");
 
-                       ReflectionPermission o = (ReflectionPermission) other;
-                       if (IsUnrestricted () || o.IsUnrestricted ())
+                       if (IsUnrestricted () || rp.IsUnrestricted ())
                                return new ReflectionPermission (PermissionState.Unrestricted);
 
-                       ReflectionPermission p = (ReflectionPermission) o.Copy ();
+                       ReflectionPermission p = (ReflectionPermission) rp.Copy ();
                        p.Flags |= flags;
                        return p;
                }
@@ -185,9 +191,22 @@ namespace System.Security.Permissions {
                // IBuiltInPermission
                int IBuiltInPermission.GetTokenIndex ()
                {
-                       return 4;
+                       return (int) BuiltInToken.Reflection;
                }
 
-               #endregion // Methods
+               // helpers
+
+               private ReflectionPermission Cast (IPermission target)
+               {
+                       if (target == null)
+                               return null;
+
+                       ReflectionPermission rp = (target as ReflectionPermission);
+                       if (rp == null) {
+                               ThrowInvalidPermission (target, typeof (ReflectionPermission));
+                       }
+
+                       return rp;
+               }
        }
 }