//\r
// System.Net.DnsPermission.cs\r
//\r
-// Author:\r
-// Lawrence Pit (loz@cable.a2000.nl)\r
+// Authors:\r
+// Lawrence Pit (loz@cable.a2000.nl)\r
+// Sebastien Pouliot <sebastien@ximian.com>
//\r
-
+// Copyright (C) 2004 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
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
\r
-using System;\r
using System.Collections;\r
using System.Security;\r
using System.Security.Permissions;\r
\r
-namespace System.Net\r
-{\r
+namespace System.Net {
+\r
[Serializable]\r
- public sealed class DnsPermission : CodeAccessPermission, IUnrestrictedPermission\r
- {\r
+ public sealed class DnsPermission : CodeAccessPermission, IUnrestrictedPermission {
+
+ private const int version = 1;
+\r
// Fields\r
bool m_noRestriction;\r
\r
// Constructors\r
- public DnsPermission (PermissionState state) : base () \r
+ public DnsPermission (PermissionState state)
+ : base () \r
{ \r
m_noRestriction = (state == PermissionState.Unrestricted);\r
}\r
\r
public override IPermission Copy ()\r
{\r
- // this is immutable.\r
- return this; \r
+ return new DnsPermission (m_noRestriction ? PermissionState.Unrestricted : PermissionState.None); \r
}\r
\r
public override IPermission Intersect (IPermission target)\r
{\r
- // LAMESPEC: says to throw an exception when null\r
- // but at same time it says to return null. We'll\r
- // follow MS behaviour.\r
- if (target == null) \r
- return null;\r
- \r
- DnsPermission perm = target as DnsPermission;\r
- \r
- if (perm == null)\r
- throw new ArgumentException ("Argument not of type DnsPermission");\r
- \r
- if (this.m_noRestriction && perm.m_noRestriction)\r
- return this;\r
- \r
- return this.m_noRestriction ? perm : this;\r
+ DnsPermission dp = Cast (target);
+ if (dp == null)
+ return null;
+ if (IsUnrestricted () && dp.IsUnrestricted ())
+ return new DnsPermission (PermissionState.Unrestricted);
+ return null;\r
}\r
\r
public override bool IsSubsetOf (IPermission target) \r
{\r
- if (target == null)\r
- return !m_noRestriction;\r
- \r
- DnsPermission perm = target as DnsPermission;\r
- \r
- if (perm == null)\r
- throw new ArgumentException ("Argument not of type DnsPermission");\r
- \r
- return !this.m_noRestriction || perm.m_noRestriction;\r
+ DnsPermission dp = Cast (target);
+ if (dp == null)
+ return IsEmpty ();
+
+ return (dp.IsUnrestricted () || (m_noRestriction == dp.m_noRestriction));
}\r
\r
public bool IsUnrestricted () \r
return this.m_noRestriction;\r
}\r
\r
- /*\r
- \r
- DnsPermission dns1 = new DnsPermission (PermissionState.None);\r
- Console.WriteLine (dns1.ToXml ().ToString ());\r
-\r
- DnsPermission dns2 = new DnsPermission (PermissionState.Unrestricted);\r
- Console.WriteLine (dns2.ToXml ().ToString ());\r
- \r
- This is the sample xml output:\r
-\r
- <IPermission class="System.Net.DnsPermission, System, Version=1.0.3300.0, Cultur\r
- e=neutral, PublicKeyToken=b77a5c561934e089"\r
- version="1"/>\r
-\r
- <IPermission class="System.Net.DnsPermission, System, Version=1.0.3300.0, Cultur\r
- e=neutral, PublicKeyToken=b77a5c561934e089"\r
- version="1"\r
- Unrestricted="true"/>\r
- */\r
public override SecurityElement ToXml ()\r
{\r
- \r
- SecurityElement root = new SecurityElement ("IPermission");\r
- root.AddAttribute ("class", this.GetType ().AssemblyQualifiedName);\r
- root.AddAttribute ("version", "1");\r
+ SecurityElement se = PermissionHelper.Element (typeof (DnsPermission), version);
if (m_noRestriction)\r
- root.AddAttribute ("Unrestricted", "true"); \r
-\r
- return root;\r
+ se.AddAttribute ("Unrestricted", "true"); \r
+ return se;\r
}\r
\r
public override void FromXml (SecurityElement securityElement)\r
- {\r
- if (securityElement == null)\r
- throw new ArgumentNullException ("securityElement");\r
- \r
+ {
+ PermissionHelper.CheckSecurityElement (securityElement, "securityElement", version, version);
+ \r
// LAMESPEC: it says to throw an ArgumentNullException in this case \r
if (securityElement.Tag != "IPermission")\r
throw new ArgumentException ("securityElement");\r
\r
- string classStr = securityElement.Attribute ("class");\r
- if (classStr == null || !classStr.StartsWith (this.GetType ().FullName + ","))\r
- throw new ArgumentException ("securityElement");\r
- \r
- string unrestricted = securityElement.Attribute ("Unrestricted");\r
- if (unrestricted != null) \r
- this.m_noRestriction = (String.Compare (unrestricted, "true", true) == 0);\r
+ this.m_noRestriction = PermissionHelper.IsUnrestricted (securityElement);\r
} \r
\r
public override IPermission Union (IPermission target) \r
- {\r
- // LAMESPEC: according to spec we should throw an \r
- // exception when target is null. We'll follow the\r
- // behaviour of MS.Net instead of the spec.\r
- if (target == null)\r
- return this;\r
- // throw new ArgumentNullException ("target");\r
- \r
- DnsPermission perm = target as DnsPermission;\r
- \r
- if (perm == null)\r
- throw new ArgumentException ("Argument not of type DnsPermission");\r
- \r
- return this.m_noRestriction ? this : perm;\r
+ {
+ DnsPermission dp = Cast (target);
+ if (dp == null)
+ return Copy ();
+ if (IsUnrestricted () || dp.IsUnrestricted ())
+ return new DnsPermission (PermissionState.Unrestricted);
+ else
+ return new DnsPermission (PermissionState.None);
}\r
-\r
+
+ // Internal helpers methods
+
+ private bool IsEmpty ()
+ {
+ return !m_noRestriction;
+ }
+
+ private DnsPermission Cast (IPermission target)
+ {
+ if (target == null)
+ return null;
+
+ DnsPermission dp = (target as DnsPermission);
+ if (dp == null) {
+ PermissionHelper.ThrowInvalidPermission (target, typeof (DnsPermission));
+ }
+
+ return dp;
+ }
}\r
}\r