New tests.
[mono.git] / mcs / class / corlib / System.Security.Policy / SiteMembershipCondition.cs
1 //
2 // SiteMembershipCondition.cs: Site MembershipCondition
3 //
4 // Author:
5 //      Sebastien Pouliot  <sebastien@ximian.com>
6 //
7 // (C) 2004 Motus Technologies Inc. (http://www.motus.com)
8 // Copyright (C) 2004-2005 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
30 using System.Collections;
31 using System.Globalization;
32 using System.Runtime.InteropServices;
33
34 namespace System.Security.Policy {
35
36         [Serializable]
37         [ComVisible (true)]
38         public sealed class SiteMembershipCondition : IMembershipCondition, IConstantMembershipCondition {
39
40                 private readonly int version = 1;
41
42                 private string _site;
43
44                 // constructors
45
46                 // so System.Activator.CreateInstance can create an instance...
47                 internal SiteMembershipCondition ()
48                 {
49                 }
50
51                 public SiteMembershipCondition (string site)
52                 {
53                         Site = site;
54                 }
55
56                 // properties
57
58                 public string Site { 
59                         get { return _site; }
60                         set {
61                                 if (value == null)
62                                         throw new ArgumentNullException ("site");
63                                 if (!System.Security.Policy.Site.IsValid (value))
64                                         throw new ArgumentException ("invalid site");
65                                 _site = value;
66                         }
67                 }
68
69                 // methods
70
71                 public bool Check (Evidence evidence) 
72                 {
73                         if (evidence == null)
74                                 return false;
75
76                         IEnumerator e = evidence.GetHostEnumerator ();
77                         while (e.MoveNext ()) {
78                                 if (e.Current is Site) {
79                                         string[] s1 = _site.Split ('.');
80                                         string[] s2 = (e.Current as Site).origin_site.Split ('.');
81                                         for (int i = s1.Length - 1, j = s2.Length - 1; i>=0; i--, j--) {
82                                                 if (i == 0) {
83                                                         // special * case
84                                                         return (String.Compare (s1 [0], "*", true, CultureInfo.InvariantCulture) == 0);
85                                                 }
86                                                 if (String.Compare (s1 [i], s2 [j], true, CultureInfo.InvariantCulture) != 0)
87                                                         return false;
88                                         }
89                                         return true;
90                                 }
91                         }
92                         return false;
93                 }
94                 
95                 public IMembershipCondition Copy () 
96                 {
97                         return new SiteMembershipCondition (_site);
98                 }
99
100                 public override bool Equals (object o) 
101                 {
102                         if (o == null)
103                                 return false;
104                         if (o is SiteMembershipCondition) {
105                                 Site s = new Site ((o as SiteMembershipCondition)._site);
106                                 return s.Equals (new Site (_site));
107                         }
108                         return false;
109                 }
110
111                 public void FromXml (SecurityElement e) 
112                 {
113                         FromXml (e, null);
114                 }
115
116                 public void FromXml (SecurityElement e, PolicyLevel level) 
117                 {
118                         MembershipConditionHelper.CheckSecurityElement (e, "e", version, version);
119                         _site = e.Attribute ("Site");
120                         // PolicyLevel isn't used as there's no need to resolve NamedPermissionSet references
121                 }
122
123                 public override int GetHashCode () 
124                 {
125                         return _site.GetHashCode ();
126                 }
127
128                 public override string ToString () 
129                 {
130                         return "Site - " + _site;
131                 }
132
133                 public SecurityElement ToXml () 
134                 {
135                         return ToXml (null);
136                 }
137
138                 public SecurityElement ToXml (PolicyLevel level) 
139                 {
140                         // PolicyLevel isn't used as there's no need to resolve NamedPermissionSet references
141                         SecurityElement se = MembershipConditionHelper.Element (typeof (SiteMembershipCondition), version);
142                         se.AddAttribute ("Site", _site);
143                         return se;
144                 }
145         }
146 }