New test.
[mono.git] / mcs / class / corlib / System.Security.Policy / Site.cs
1 //
2 // System.Security.Policy.Site.cs
3 //
4 // Authors
5 //      Duncan Mak (duncan@ximian.com)
6 //      Sebastien Pouliot  <sebastien@ximian.com>
7 //
8 // (C) 2003 Ximian, Inc (http://www.ximian.com)
9 // Portions (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.Globalization;
33 using System.Security.Permissions;
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 Site: IIdentityPermissionFactory, IBuiltInEvidence {
45
46                 internal string origin_site;
47
48                 public Site (string name)
49                 {
50                         if (name == null)
51                                 throw new ArgumentNullException ("url");
52                         if (!IsValid (name))
53                                 throw new ArgumentException (Locale.GetText ("name is not valid"));
54                         
55                         origin_site = name;
56                 }
57
58                 public static Site CreateFromUrl (string url)
59                 {
60                         if (url == null)
61                                 throw new ArgumentNullException ("url");
62                         if (url.Length == 0)
63                                 throw new FormatException (Locale.GetText ("Empty URL."));
64
65                         string site = UrlToSite (url);
66                         if (site == null) {
67                                 string msg = String.Format (Locale.GetText ("Invalid URL '{0}'."), url);
68                                 throw new ArgumentException (msg, "url");
69                         }
70
71                         return new Site (site);
72                 }
73
74                 public object Copy ()
75                 {
76                         return new Site (origin_site);
77                 }
78
79                 public IPermission CreateIdentityPermission (Evidence evidence)
80                 {
81                         return new SiteIdentityPermission (origin_site);
82                 }
83
84                 public override bool Equals (object o)
85                 {
86                         Site s = (o as System.Security.Policy.Site);
87                         if (s == null)
88                                 return false;
89                         return (String.Compare (s.Name, origin_site, true, CultureInfo.InvariantCulture) == 0);
90                 }
91
92                 public override int GetHashCode ()
93                 {
94                         return origin_site.GetHashCode ();
95                 }
96
97                 public override string ToString ()
98                 {
99                         SecurityElement element = new SecurityElement ("System.Security.Policy.Site");
100                         element.AddAttribute ("version", "1");
101                         element.AddChild (new SecurityElement ("Name", origin_site));
102                         return element.ToString ();
103                 }
104
105                 // properties
106
107                 public string Name {
108                         get { return origin_site; }
109                 }
110
111                 // interface IBuiltInEvidence
112
113                 int IBuiltInEvidence.GetRequiredSize (bool verbose) 
114                 {
115                         return (verbose ? 3 : 1) + origin_site.Length;
116                 }
117
118                 [MonoTODO ("IBuiltInEvidence")]
119                 int IBuiltInEvidence.InitFromBuffer (char [] buffer, int position) 
120                 {
121                         return 0;
122                 }
123
124                 [MonoTODO ("IBuiltInEvidence")]
125                 int IBuiltInEvidence.OutputToBuffer (char [] buffer, int position, bool verbose) 
126                 {
127                         return 0;
128                 }
129
130                 // internals
131
132                 internal static bool IsValid (string name)
133                 {
134                         if (name == String.Empty)
135                                 return false;
136                         if ((name.Length == 1) && (name == "."))                // split would remove .
137                                 return false;
138
139                         string [] parts = name.Split ('.');
140                         for (int i=0; i < parts.Length; i++) {
141                                 string part = parts [i];
142                                 if ((i == 0) && (part == "*"))                  // * (only in first part)
143                                         continue;
144                                 foreach (char c in part) {
145                                         int x = Convert.ToInt32 (c);
146 #if NET_2_0
147                                         bool result = ((x == 33) || (x == 45)   // !-
148                                                 || (x >= 35 && x <= 41)         // #$%&'()
149                                                 || (x >= 48 && x <= 57)         // 0-9
150                                                 || (x >= 64 && x <= 90)         // @,A-Z
151                                                 || (x >= 94 && x <= 95)         // ^_
152                                                 || (x >= 97 && x <= 123)        // a-z{
153                                                 || (x >= 125 && x <= 126));     // }~
154 #else
155                                         bool result = ((x == 45)                // -
156                                                 || (x >= 47 && x <= 57)         // /,0-9
157                                                 || (x >= 64 && x <= 90)         // @,A-Z
158                                                 || (x == 95)                    // _
159                                                 || (x >= 97 && x <= 122));      // a-z
160 #endif
161                                         if (!result)
162                                                 return false;
163                                 }
164                         }
165                         return true;
166                 }
167
168                 // no exception - we return null if a site couldn't be created
169                 // this is useful for creating the default evidence as the majority of URL will be local (file://)
170                 // and throw an unrequired exception
171                 internal static string UrlToSite (string url)
172                 {
173                         if (url == null)
174                                 return null;
175
176                         Uri uri = new Uri (url);
177 #if NET_2_0
178                         if (uri.Scheme == Uri.UriSchemeFile)
179                                 return null;
180                         string site = uri.Host;
181 #else
182                         string site = uri.Host;
183                         if (site == null)
184                                 site = uri.AbsoluteUri.ToUpper ();              // strange but true
185 #endif
186                         return IsValid (site) ? site : null;
187                 }
188         }
189 }