//\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
+//\r
+// Copyright (C) 2004 Novell, Inc (http://www.novell.com)\r
+//\r
+// Permission is hereby granted, free of charge, to any person obtaining\r
+// a copy of this software and associated documentation files (the\r
+// "Software"), to deal in the Software without restriction, including\r
+// without limitation the rights to use, copy, modify, merge, publish,\r
+// distribute, sublicense, and/or sell copies of the Software, and to\r
+// permit persons to whom the Software is furnished to do so, subject to\r
+// the following conditions:\r
+// \r
+// The above copyright notice and this permission notice shall be\r
+// included in all copies or substantial portions of the Software.\r
+// \r
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,\r
+// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF\r
+// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND\r
+// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE\r
+// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION\r
+// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION\r
+// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.\r
//\r
-
-//
-// 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.
-//
\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
+\r
[Serializable]\r
- public sealed class DnsPermission : CodeAccessPermission, IUnrestrictedPermission\r
- {\r
+ public sealed class DnsPermission : CodeAccessPermission, IUnrestrictedPermission {\r
+\r
+ private const int version = 1;\r
+\r
// Fields\r
bool m_noRestriction;\r
\r
// Constructors\r
- public DnsPermission (PermissionState state) : base () \r
+ public DnsPermission (PermissionState state)\r
+ : 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
+ DnsPermission dp = Cast (target);\r
+ if (dp == 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
+ if (IsUnrestricted () && dp.IsUnrestricted ())\r
+ return new DnsPermission (PermissionState.Unrestricted);\r
+ 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);\r
+ if (dp == null)\r
+ return IsEmpty ();\r
+\r
+ return (dp.IsUnrestricted () || (m_noRestriction == dp.m_noRestriction));\r
}\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);\r
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
+ \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);\r
+ if (dp == null)\r
+ return Copy ();\r
+ if (IsUnrestricted () || dp.IsUnrestricted ())\r
+ return new DnsPermission (PermissionState.Unrestricted);\r
+ else\r
+ return new DnsPermission (PermissionState.None);\r
}\r
\r
+ // Internal helpers methods\r
+\r
+ private bool IsEmpty ()\r
+ {\r
+ return !m_noRestriction;\r
+ }\r
+\r
+ private DnsPermission Cast (IPermission target)\r
+ {\r
+ if (target == null)\r
+ return null;\r
+\r
+ DnsPermission dp = (target as DnsPermission);\r
+ if (dp == null) {\r
+ PermissionHelper.ThrowInvalidPermission (target, typeof (DnsPermission));\r
+ }\r
+\r
+ return dp;\r
+ }\r
}\r
}\r