New test.
[mono.git] / mcs / class / System / System.Net / DnsPermission.cs
index 42b5e917645f30003f535892b7b839e71dac9e42..6fae96b801a9aaef33cac30f11c6858a806b7815 100644 (file)
@@ -1,25 +1,49 @@
 //\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
+// "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
        [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
@@ -28,40 +52,26 @@ namespace System.Net
                                \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
@@ -69,81 +79,54 @@ namespace System.Net
                        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
-\r
-                       root.AddAttribute ("class", this.GetType ().FullName + ", " + \r
-                                                   "System, Version=1.0.3300.0, Culture=neutral, PublicKeyToken=b77a5c561934e089");\r
-\r
-/*                     \r
-GetType ().Module doesn't work yet with Mono.. (2002-05-01)\r
-We need to do it as below though, because this class can be extended\r
-\r
-                       root.AddAttribute ("class", this.GetType ().FullName + ", " + \r
-                                                   this.GetType ().Module.Assembly.FullName);\r
-*/                                                 \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