2009-06-30 Zoltan Varga <vargaz@gmail.com>
[mono.git] / mcs / class / corlib / System.Security / SecurityException.cs
index af9fed9fda1175304716e83db5a9e35dc6609d36..e87fad90e5e7399022024afed7a676ba47569765 100644 (file)
@@ -41,25 +41,33 @@ namespace System.Security {
 
        [Serializable]
 #if NET_2_0
-       public class SecurityException : SystemException, _Exception {
-#else
-       public class SecurityException : SystemException {
+       [ComVisible (true)]
 #endif
+       public class SecurityException : SystemException {
                // Fields
                string permissionState;
                Type permissionType;
                private string _granted;
                private string _refused;
-               private SecurityAction _action;
                private object _demanded;
+               private IPermission _firstperm;
+//             private IPermission _permfailed;
+               private MethodInfo _method;
+               private Evidence _evidence;
+
+#if !NET_2_0
+       #pragma warning disable 169
+#endif
+               private SecurityAction _action;
                private object _denyset;
                private object _permitset;
                private AssemblyName _assembly;
-               private IPermission _firstperm;
-               private IPermission _permfailed;
-               private MethodInfo _method;
                private string _url;
                private SecurityZone _zone;
+               
+#if !NET_2_0
+       #pragma warning restore 169
+#endif 
 
                // Properties
 
@@ -70,13 +78,6 @@ namespace System.Security {
                        set { _action = value; }
                }
 
-               [ComVisible (false)]
-               public object Demanded {
-                       [SecurityPermission (SecurityAction.Demand, ControlEvidence=true, ControlPolicy=true)]
-                       get { return _demanded; }
-                       set { _demanded = value; }
-               }
-
                [ComVisible (false)]
                public object DenySetInstance {
                        [SecurityPermission (SecurityAction.Demand, ControlEvidence=true, ControlPolicy=true)]
@@ -91,12 +92,6 @@ namespace System.Security {
                        set { _assembly = value; }
                }
 
-               public IPermission FirstPermissionThatFailed {
-                       [SecurityPermission (SecurityAction.Demand, ControlEvidence=true, ControlPolicy=true)]
-                       get { return _firstperm; }
-                       set { _firstperm = value; }
-               }
-
                [ComVisible (false)]
                public MethodInfo Method {
                        [SecurityPermission (SecurityAction.Demand, ControlEvidence=true, ControlPolicy=true)]
@@ -104,13 +99,6 @@ namespace System.Security {
                        set { _method = value; }
                }
 
-               [Obsolete]
-               [ComVisible (false)]
-               public IPermission PermissionThatFailed {
-                       get { return _permfailed; }
-                       set { _permfailed = value; }
-               }
-
                [ComVisible (false)]
                public object PermitOnlySetInstance {
                        [SecurityPermission (SecurityAction.Demand, ControlEvidence=true, ControlPolicy=true)]
@@ -130,6 +118,29 @@ namespace System.Security {
                }
 #endif
 
+#if NET_2_0
+               [ComVisible (false)]
+               public 
+#else
+               internal
+#endif
+               object Demanded {
+                       [SecurityPermission (SecurityAction.Demand, ControlEvidence=true, ControlPolicy=true)]
+                       get { return _demanded; }
+                       set { _demanded = value; }
+               }
+
+#if NET_2_0
+               public 
+#else
+               internal
+#endif
+               IPermission FirstPermissionThatFailed {
+                       [SecurityPermission (SecurityAction.Demand, ControlEvidence=true, ControlPolicy=true)]
+                       get { return _firstperm; }
+                       set { _firstperm = value; }
+               }
+
                public string PermissionState {
                        [SecurityPermission (SecurityAction.Demand, ControlEvidence=true, ControlPolicy=true)]
                        get { return permissionState; }
@@ -180,7 +191,15 @@ namespace System.Security {
                        : base (info, context)
                {
                        base.HResult = unchecked ((int)0x8013150A);
-                       permissionState = info.GetString ("PermissionState");
+                       // depending on the security policy the info about PermissionState may
+                       // not be available (but the serializable must work)
+                       SerializationInfoEnumerator e = info.GetEnumerator ();
+                       while (e.MoveNext ()) {
+                               if (e.Name == "PermissionState") {
+                                       permissionState = (string) e.Value;
+                                       break;
+                               }
+                       }
                }
                
                public SecurityException (string message, Exception inner) 
@@ -226,7 +245,7 @@ namespace System.Security {
                        _permitset = permitOnly;
                        _method = method;
                        _demanded = demanded;
-                       _permfailed = permThatFailed;
+                       _firstperm = permThatFailed;
                }
 
 #if NET_2_0
@@ -246,48 +265,70 @@ namespace System.Security {
                        _method = method;
                        _action = action;
                        _demanded = demanded;
-                       _permfailed = permThatFailed;
-                       if (_permfailed != null)
-                               permissionType = _permfailed.GetType ();
-                       // FIXME ? evidence ?
+                       _firstperm = permThatFailed;
+                       if (_firstperm != null)
+                               permissionType = _firstperm.GetType ();
+                       _evidence = evidence;
                }
 
                // Methods
                public override void GetObjectData (SerializationInfo info, StreamingContext context)
                {
                        base.GetObjectData (info, context);
-                       info.AddValue ("PermissionState", permissionState);
+                       try {
+                               info.AddValue ("PermissionState", PermissionState);
+                       }
+                       catch (SecurityException) {
+                               // serialize only if permitted to do so
+                       }
                }
 
                public override string ToString ()
                {
+#if NET_2_1
+                       return base.ToString ();
+#else
                        StringBuilder sb = new StringBuilder (base.ToString ());
                        try {
-                               if (permissionState != null) {
-                                       sb.AppendFormat ("{0}State: {1}", Environment.NewLine, PermissionState);
-                               }
                                if (permissionType != null) {
                                        sb.AppendFormat ("{0}Type: {1}", Environment.NewLine, PermissionType);
                                }
+                               if (_method != null) {
+                                       // method string representation doesn't include the type
+                                       string m = _method.ToString ();
+                                       int ret = m.IndexOf (" ") + 1;
+                                       sb.AppendFormat ("{0}Method: {1} {2}.{3}", Environment.NewLine, 
+                                               _method.ReturnType.Name, _method.ReflectedType, m.Substring (ret));
+                               }
+                               if (permissionState != null) {
+                                       sb.AppendFormat ("{0}State: {1}", Environment.NewLine, PermissionState);
+                               }
                                if ((_granted != null) && (_granted.Length > 0)) {
                                        sb.AppendFormat ("{0}Granted: {1}", Environment.NewLine, GrantedSet);
                                }
                                if ((_refused != null) && (_refused.Length > 0)) {
                                        sb.AppendFormat ("{0}Refused: {1}", Environment.NewLine, RefusedSet);
                                }
-#if NET_2_0
                                if (_demanded != null) {
                                        sb.AppendFormat ("{0}Demanded: {1}", Environment.NewLine, Demanded);
                                }
-                               if (_permfailed != null) {
+                               if (_firstperm != null) {
                                        sb.AppendFormat ("{0}Failed Permission: {1}", Environment.NewLine, FirstPermissionThatFailed);
                                }
-#endif
+                               if (_evidence != null) {
+                                       sb.AppendFormat ("{0}Evidences:", Environment.NewLine);
+                                       foreach (object o in _evidence) {
+                                               // Hash evidence is way much too verbose to be useful to anyone
+                                               if (!(o is Hash))
+                                                       sb.AppendFormat ("{0}\t{1}", Environment.NewLine, o);
+                                       }
+                               }
                        }
                        catch (SecurityException) {
                                // some informations can't be displayed
                        }
                        return sb.ToString ();
+#endif
                }
        }
 }