New test.
[mono.git] / mcs / class / System / System.Net / DnsPermission.cs
1 //\r
2 // System.Net.DnsPermission.cs\r
3 //\r
4 // Authors:\r
5 //      Lawrence Pit (loz@cable.a2000.nl)\r
6 //      Sebastien Pouliot  <sebastien@ximian.com>
7 //\r
8 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
9 //
10 // Permission is hereby granted, free of charge, to any person obtaining
11 // a copy of this software and associated documentation files (the
12 // "Software"), to deal in the Software without restriction, including
13 // without limitation the rights to use, copy, modify, merge, publish,
14 // distribute, sublicense, and/or sell copies of the Software, and to
15 // permit persons to whom the Software is furnished to do so, subject to
16 // the following conditions:
17 // 
18 // The above copyright notice and this permission notice shall be
19 // included in all copies or substantial portions of the Software.
20 // 
21 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
22 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
23 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
24 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
25 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
26 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
27 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28 //
29 \r
30 using System.Collections;\r
31 using System.Security;\r
32 using System.Security.Permissions;\r
33 \r
34 namespace System.Net {
35 \r
36         [Serializable]\r
37         public sealed class DnsPermission : CodeAccessPermission, IUnrestrictedPermission {
38
39                 private const int version = 1;
40 \r
41                 // Fields\r
42                 bool m_noRestriction;\r
43                 \r
44                 // Constructors\r
45                 public DnsPermission (PermissionState state)
46                         : base () \r
47                 {                                               \r
48                         m_noRestriction = (state == PermissionState.Unrestricted);\r
49                 }\r
50                 \r
51                 // Methods\r
52                                 \r
53                 public override IPermission Copy ()\r
54                 {\r
55                         return new DnsPermission (m_noRestriction ? PermissionState.Unrestricted : PermissionState.None);               \r
56                 }\r
57                 \r
58                 public override IPermission Intersect (IPermission target)\r
59                 {\r
60                         DnsPermission dp = Cast (target);
61                         if (dp == null)
62                                 return null;
63                         if (IsUnrestricted () && dp.IsUnrestricted ())
64                                 return new DnsPermission (PermissionState.Unrestricted);
65                         return null;\r
66                 }\r
67                 \r
68                 public override bool IsSubsetOf (IPermission target) \r
69                 {\r
70                         DnsPermission dp = Cast (target);
71                         if (dp == null)
72                                 return IsEmpty ();
73
74                         return (dp.IsUnrestricted () || (m_noRestriction == dp.m_noRestriction));
75                 }\r
76 \r
77                 public bool IsUnrestricted () \r
78                 {\r
79                         return this.m_noRestriction;\r
80                 }\r
81 \r
82                 public override SecurityElement ToXml ()\r
83                 {\r
84                         SecurityElement se = PermissionHelper.Element (typeof (DnsPermission), version);
85                         if (m_noRestriction)\r
86                                 se.AddAttribute ("Unrestricted", "true");                               \r
87                         return se;\r
88                 }\r
89                 \r
90                 public override void FromXml (SecurityElement securityElement)\r
91                 {
92                         PermissionHelper.CheckSecurityElement (securityElement, "securityElement", version, version);
93                 \r
94                         // LAMESPEC: it says to throw an ArgumentNullException in this case                             \r
95                         if (securityElement.Tag != "IPermission")\r
96                                 throw new ArgumentException ("securityElement");\r
97                                 \r
98                         this.m_noRestriction = PermissionHelper.IsUnrestricted (securityElement);\r
99                 }               \r
100                 \r
101                 public override IPermission Union (IPermission target) \r
102                 {
103                         DnsPermission dp = Cast (target);
104                         if (dp == null)
105                                 return Copy ();
106                         if (IsUnrestricted () || dp.IsUnrestricted ())
107                                 return new DnsPermission (PermissionState.Unrestricted);
108                         else
109                                 return new DnsPermission (PermissionState.None);
110                 }\r
111
112                 // Internal helpers methods
113
114                 private bool IsEmpty ()
115                 {
116                         return !m_noRestriction;
117                 }
118
119                 private DnsPermission Cast (IPermission target)
120                 {
121                         if (target == null)
122                                 return null;
123
124                         DnsPermission dp = (target as DnsPermission);
125                         if (dp == null) {
126                                 PermissionHelper.ThrowInvalidPermission (target, typeof (DnsPermission));
127                         }
128
129                         return dp;
130                 }
131         }\r
132 }\r