New test.
[mono.git] / mcs / class / corlib / System.Security.Policy / UrlMembershipCondition.cs
1 //
2 // System.Security.Policy.UrlMembershipCondition.cs
3 //
4 // Authors:
5 //      Duncan Mak (duncan@ximian.com)
6 //      Sebastien Pouliot  <sebastien@ximian.com>
7 //
8 // (C) 2003, Ximian Inc.
9 // (C) 2004 Motus Technologies Inc. (http://www.motus.com)
10 // Copyright (C) 2004-2005 Novell, Inc (http://www.novell.com)
11 //
12 // Permission is hereby granted, free of charge, to any person obtaining
13 // a copy of this software and associated documentation files (the
14 // "Software"), to deal in the Software without restriction, including
15 // without limitation the rights to use, copy, modify, merge, publish,
16 // distribute, sublicense, and/or sell copies of the Software, and to
17 // permit persons to whom the Software is furnished to do so, subject to
18 // the following conditions:
19 // 
20 // The above copyright notice and this permission notice shall be
21 // included in all copies or substantial portions of the Software.
22 // 
23 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
27 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
28 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
29 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
30 //
31
32 using System.Collections;
33 using System.Globalization;
34 using System.Runtime.InteropServices;
35
36 using Mono.Security;
37
38 namespace System.Security.Policy {
39
40         [Serializable]
41 #if NET_2_0
42         [ComVisible (true)]
43 #endif
44         public sealed class UrlMembershipCondition : IMembershipCondition, IConstantMembershipCondition {
45
46                 private readonly int version = 1;
47
48                 private Url url;
49                 private string userUrl;
50                 
51                 public UrlMembershipCondition (string url)
52                 {
53                         if (url == null)
54                                 throw new ArgumentNullException ("url");
55 #if NET_2_0
56                         CheckUrl (url);
57                         userUrl = url;
58                         this.url = new Url (url);
59 #else
60                         this.url = new Url (url);
61                         userUrl = this.url.Value;
62 #endif
63                 }
64
65                 internal UrlMembershipCondition (Url url, string userUrl)
66                 {
67                         // as the Url object has already been validated there's no
68                         // need to restart the whole process by converting to string
69                         this.url = (Url) url.Copy ();
70                         this.userUrl = userUrl;
71                 }
72
73                 // properties
74
75                 public string Url {
76                         get {
77                                 if (userUrl == null)
78                                         userUrl = url.Value;
79                                 return userUrl;
80                         }
81                         set { url = new Url (value); }
82                 }
83
84                 // methods
85
86                 public bool Check (Evidence evidence)
87                 {
88                         if (evidence == null)
89                                 return false;
90
91                         string u = url.Value;
92                         int wildcard = u.LastIndexOf ("*");     // partial match with a wildcard at the end
93                         if (wildcard == -1)
94                                 wildcard = u.Length;            // exact match
95
96                         IEnumerator e = evidence.GetHostEnumerator ();
97                         while (e.MoveNext ()) {
98                                 if (e.Current is Url) {
99                                         // note: there shouldn't be more than one Url evidence
100                                         if (String.Compare (u, 0, (e.Current as Url).Value, 0, wildcard,
101                                                 true, CultureInfo.InvariantCulture) == 0) {
102                                                 return true;
103                                         }
104                                         // but we must check for all of them!
105                                 }
106                         }
107                         return false;
108                 }
109
110                 public IMembershipCondition Copy ()
111                 {
112                         return new UrlMembershipCondition (url, userUrl);
113                 }
114
115                 public override bool Equals (object o)
116                 {
117                         UrlMembershipCondition umc = (o as UrlMembershipCondition);
118                         if (o == null)
119                                 return false;
120
121                         string u = url.Value;
122                         int length = u.Length; // exact match
123
124                         // partial match with a wildcard at the end
125                         if (u [length - 1] == '*') {
126                                 length--;
127                                 // in this case the last / could be ommited
128                                 if (u [length - 1] == '/')
129                                         length--;
130                         }
131
132                         return (String.Compare (u, 0, umc.Url, 0, length, true, CultureInfo.InvariantCulture) == 0);
133                 }
134
135                 public void FromXml (SecurityElement element)
136                 {
137                         FromXml (element, null);
138                 }
139
140                 public void FromXml (SecurityElement element, PolicyLevel level)
141                 {
142                         MembershipConditionHelper.CheckSecurityElement (element, "element", version, version);
143                         
144                         string u = element.Attribute ("Url");
145 #if NET_2_0
146                         if (u != null) {
147                                 CheckUrl (u);
148                                 url = new Url (u);
149                         } else {
150                                 url = null;
151                         }
152 #else
153                         url = (u == null) ? null : new Url (u);
154 #endif
155                         userUrl = u;
156                 }
157
158                 public override int GetHashCode ()
159                 {
160                         return url.GetHashCode ();
161                 }
162
163                 public override string ToString ()
164                 {
165                         return "Url - " + Url;
166                 }
167
168                 public SecurityElement ToXml ()
169                 {
170                         return ToXml (null);
171                 }
172
173                 public SecurityElement ToXml (PolicyLevel level)
174                 {
175                         // PolicyLevel isn't used as there's no need to resolve NamedPermissionSet references
176                         SecurityElement se = MembershipConditionHelper.Element (typeof (UrlMembershipCondition), version);
177                         se.AddAttribute ("Url", userUrl);
178                         return se;
179                 }
180
181                 // internal stuff
182
183 #if NET_2_0
184                 internal void CheckUrl (string url)
185                 {
186                         // In .NET 1.x Url class checked the validity of the 
187                         // URL but that's no more the case in 2.x - but we 
188                         // still need the check done here
189                         int protocolPos = url.IndexOf (Uri.SchemeDelimiter);
190                         string u = (protocolPos < 0) ? "file://" + url : url;
191
192                         Uri uri = new Uri (u, false, false);
193                         // no * except for the "lone star" case
194                         if (uri.Host.IndexOf ('*') >= 1) {
195                                 string msg = Locale.GetText ("Invalid * character in url");
196                                 throw new ArgumentException (msg, "name");
197                         }
198                 }
199 #endif
200         }
201 }