New tests.
[mono.git] / mcs / class / corlib / System.Security.Policy / FileCodeGroup.cs
1 //
2 // System.Security.Policy.FileCodeGroup
3 //
4 // Author(s):
5 //   Nick Drochak (ndrochak@gol.com)
6 //
7 // (C) 2001 Nick Drochak, All rights reserved.
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.Runtime.InteropServices;
32 using System.Security.Permissions;
33
34 namespace System.Security.Policy {
35
36         [Serializable]
37         [ComVisible (true)]
38         public sealed class FileCodeGroup : CodeGroup {
39
40                 private FileIOPermissionAccess m_access;
41
42                 public FileCodeGroup (IMembershipCondition membershipCondition, FileIOPermissionAccess access) 
43                         : base (membershipCondition, null)
44                 {
45                         // note: FileIOPermissionAccess is a [Flag]
46                         m_access = access;
47                 }
48
49                 // for PolicyLevel (to avoid validation duplication)
50                 internal FileCodeGroup (SecurityElement e, PolicyLevel level)
51                         : base (e, level)
52                 {
53                 }
54
55                 public override CodeGroup Copy ()
56                 {
57                         FileCodeGroup copy = new FileCodeGroup (MembershipCondition, m_access);
58                         copy.Name = this.Name;
59                         copy.Description = this.Description;
60                         foreach (CodeGroup child in Children) {
61                                 copy.AddChild (child.Copy ());  // deep copy
62                         }
63                         return copy;
64                 }
65                 
66                 public override string MergeLogic {
67                         get { return "Union";}
68                 }
69
70                 public override PolicyStatement Resolve (Evidence evidence)
71                 {
72                         if (null == evidence)
73                                 throw new ArgumentNullException("evidence");
74
75                         if (!MembershipCondition.Check (evidence))
76                                 return null;
77
78                         PermissionSet ps = null;
79                         if (this.PolicyStatement == null)
80                                 ps = new PermissionSet (PermissionState.None);
81                         else
82                                 ps = this.PolicyStatement.PermissionSet.Copy ();
83
84                         if (this.Children.Count > 0) {
85                                 foreach (CodeGroup child_cg in this.Children) {
86                                         PolicyStatement child_pst = child_cg.Resolve (evidence);
87                                         if (child_pst != null) {
88                                                 ps = ps.Union (child_pst.PermissionSet);
89                                         }
90                                 }
91                         }
92
93                         PolicyStatement pst = null;
94                         if (this.PolicyStatement != null)
95                                 pst = this.PolicyStatement.Copy ();
96                         else
97                                 pst = PolicyStatement.Empty ();
98                         pst.PermissionSet = ps;
99                         return pst;
100                 }
101
102                 public override CodeGroup ResolveMatchingCodeGroups(Evidence evidence)
103                 {
104                         if (null == evidence)
105                                 throw new ArgumentNullException("evidence");
106
107                         if (!MembershipCondition.Check (evidence))
108                                 return null;
109
110                         FileCodeGroup matchRoot = new FileCodeGroup (MembershipCondition, m_access);
111
112                         foreach (CodeGroup child in Children) {
113                                 CodeGroup childMatchingCodeGroup = child.ResolveMatchingCodeGroups (evidence);
114                                 if (childMatchingCodeGroup != null)
115                                         matchRoot.AddChild (childMatchingCodeGroup);
116                         }
117
118                         return matchRoot;
119                 }
120
121                 public override string AttributeString {
122                         get { return null; }
123                 }
124
125                 public override string PermissionSetName {
126                         get { return "Same directory FileIO - " + m_access.ToString (); }
127                 }
128
129                 public override bool Equals (object o)
130                 {
131                         if (!(o is FileCodeGroup))
132                                 return false;
133
134                         if (this.m_access != ((FileCodeGroup)o).m_access)
135                                 return false;
136
137                         return Equals((CodeGroup)o, false);
138                 }
139
140                 public override int GetHashCode ()
141                 {
142                         return m_access.GetHashCode ();
143                 }
144
145                 protected override void ParseXml (SecurityElement e, PolicyLevel level)
146                 {
147                         string a = e.Attribute ("Access");
148                         if (a != null)
149                                 m_access = (FileIOPermissionAccess) Enum.Parse (typeof (FileIOPermissionAccess), a, true);
150                         else
151                                 m_access = FileIOPermissionAccess.NoAccess;
152                 }
153                 
154                 protected override void CreateXml (SecurityElement element, PolicyLevel level)
155                 {
156                         element.AddAttribute ("Access", m_access.ToString ());
157                 }
158         }
159 }