New test.
[mono.git] / mcs / class / corlib / System.Security.Policy / ApplicationDirectoryMembershipCondition.cs
1 //
2 // System.Security.Policy.ApplicationDirectoryMembershipCondition\r
3 //\r
4 // Authors:\r
5 //      Nick Drochak (ndrochak@gol.com)\r
6 //      Jackson Harper (Jackson@LatitudeGeo.com)\r
7 //      Sebastien Pouliot  <sebastien@ximian.com>
8 //\r
9 // (C) 2002 Nick Drochak, All rights reserved.\r
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.IO;
35 using System.Reflection;
36 using System.Runtime.InteropServices;
37
38 using Mono.Security;
39 \r
40 namespace System.Security.Policy {\r
41 \r
42         [Serializable]\r
43 #if NET_2_0
44         [ComVisible (true)]
45 #endif
46         public sealed class ApplicationDirectoryMembershipCondition : IConstantMembershipCondition, IMembershipCondition {\r
47
48                 private readonly int version = 1;
49
50                 public ApplicationDirectoryMembershipCondition ()
51                 {
52                 }
53
54                 // Methods\r
55                 public bool Check (Evidence evidence)\r
56                 {\r
57                         if (evidence == null)\r
58                                 return false;\r
59
60                         string codebase = Assembly.GetCallingAssembly ().CodeBase;
61                         Uri local = new Uri (codebase);
62                         Url ucode = new Url (codebase);
63
64                         // *both* ApplicationDirectory and Url must be in *Host* evidences
65                         bool adir = false;
66                         bool url = false;
67                         IEnumerator e = evidence.GetHostEnumerator ();
68                         while (e.MoveNext ()) {
69                                 object o = e.Current;
70 \r
71                                 if (!adir && (o is ApplicationDirectory)) {\r
72                                         ApplicationDirectory ad = (o as ApplicationDirectory);
73                                         string s = ad.Directory;
74                                         adir = (String.Compare (s, 0, local.ToString (), 0, s.Length, true, CultureInfo.InvariantCulture) == 0);
75                                 }
76                                 else if (!url && (o is Url)) {
77                                         url = ucode.Equals (o);
78                                 }
79
80                                 // got both ?
81                                 if (adir && url)
82                                         return true;\r
83                         }\r
84                         return false;\r
85                 }\r
86 \r
87                 public IMembershipCondition Copy () \r
88                 { \r
89                         return new ApplicationDirectoryMembershipCondition ();\r
90                 }\r
91                 \r
92                 public override bool Equals (object o) \r
93                 { \r
94                         return (o is ApplicationDirectoryMembershipCondition); \r
95                 }\r
96                 \r
97                 public void FromXml (SecurityElement e)\r
98                 {\r
99                         FromXml (e, null);\r
100                 }\r
101                 \r
102                 public void FromXml (SecurityElement e, PolicyLevel level)\r
103                 {\r
104                         MembershipConditionHelper.CheckSecurityElement (e, "e", version, version);
105                 }\r
106                 \r
107                 // All instances of ApplicationDirectoryMembershipCondition are equal so they should\r
108                 // have the same hashcode\r
109                 public override int GetHashCode () \r
110                 { \r
111                         return typeof (ApplicationDirectoryMembershipCondition).GetHashCode ();\r
112                 }\r
113                 \r
114                 public override string ToString () \r
115                 { \r
116                         return "ApplicationDirectory";\r
117                 }\r
118                 \r
119                 public SecurityElement ToXml () \r
120                 { \r
121                         return ToXml (null);\r
122                 }\r
123                 \r
124                 public SecurityElement ToXml (PolicyLevel level) \r
125                 {\r
126                         SecurityElement se = MembershipConditionHelper.Element (typeof (ApplicationDirectoryMembershipCondition), version);
127                         // nothing to add
128                         return se;
129                 }
130         }\r
131 }\r