Switch to compiler-tester
[mono.git] / mcs / class / corlib / System.Security.Permissions / KeyContainerPermission.cs
1 //
2 // System.Security.Permissions.KeyContainerPermission class
3 //
4 // Author
5 //      Sebastien Pouliot  <sebastien@ximian.com>
6 //
7 // Copyright (C) 2004-2005 Novell, Inc (http://www.novell.com)
8 //
9 // Permission is hereby granted, free of charge, to any person obtaining
10 // a copy of this software and associated documentation files (the
11 // "Software"), to deal in the Software without restriction, including
12 // without limitation the rights to use, copy, modify, merge, publish,
13 // distribute, sublicense, and/or sell copies of the Software, and to
14 // permit persons to whom the Software is furnished to do so, subject to
15 // the following conditions:
16 // 
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
19 // 
20 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
24 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27 //
28
29 #if NET_2_0
30
31 using System.Globalization;
32 using System.Runtime.InteropServices;
33
34 namespace System.Security.Permissions {
35
36         [Serializable]
37         [ComVisible (true)]
38         public sealed class KeyContainerPermission : CodeAccessPermission, IUnrestrictedPermission, IBuiltInPermission {
39
40                 private KeyContainerPermissionAccessEntryCollection _accessEntries;
41                 private KeyContainerPermissionFlags _flags;
42
43                 private const int version = 1;
44
45                 // Constructors
46
47                 public KeyContainerPermission (PermissionState state) 
48                 {
49                         if (CheckPermissionState (state, true) == PermissionState.Unrestricted) {
50                                 _flags = KeyContainerPermissionFlags.AllFlags;
51                         }
52                 }
53
54                 public KeyContainerPermission (KeyContainerPermissionFlags flags)
55                 {
56                         SetFlags (flags);
57                 }
58
59                 public KeyContainerPermission (KeyContainerPermissionFlags flags, KeyContainerPermissionAccessEntry[] accessList) 
60                 {
61                         SetFlags (flags);
62                         if (accessList != null) {
63                                 foreach (KeyContainerPermissionAccessEntry kcpae in accessList) {
64                                         _accessEntries.Add (kcpae);
65                                 }
66                         }
67                 }
68
69                 // Properties
70
71                 public KeyContainerPermissionAccessEntryCollection AccessEntries {
72                         get { return _accessEntries; }
73                 }
74
75                 public KeyContainerPermissionFlags Flags {
76                         get { return _flags; }
77                 }
78
79                 // Methods
80
81                 public override IPermission Copy () 
82                 {
83                         if (_accessEntries.Count == 0)
84                                 return new KeyContainerPermission (_flags);
85
86                         KeyContainerPermissionAccessEntry[] list = new KeyContainerPermissionAccessEntry [_accessEntries.Count];
87                         _accessEntries.CopyTo (list, 0);
88                         return new KeyContainerPermission (_flags, list);
89                 }
90
91                 public override void FromXml (SecurityElement esd) 
92                 {
93                         // General validation in CodeAccessPermission
94                         CheckSecurityElement (esd, "esd", version, version);
95                         // Note: we do not (yet) care about the return value 
96                         // as we only accept version 1 (min/max values)
97
98                         if (IsUnrestricted (esd)) {
99                                 _flags = KeyContainerPermissionFlags.AllFlags;
100                         }
101                         else {
102                                 // ???
103                                 _flags = (KeyContainerPermissionFlags) Enum.Parse (
104                                         typeof (KeyContainerPermissionFlags), esd.Attribute ("Flags"));
105                         }
106                 }
107
108                 [MonoTODO]
109                 public override IPermission Intersect (IPermission target) 
110                 {
111                         return null;
112                 }
113
114                 [MonoTODO]
115                 public override bool IsSubsetOf (IPermission target) 
116                 {
117                         return false;
118                 }
119
120                 public bool IsUnrestricted () 
121                 {
122                         return (_flags == KeyContainerPermissionFlags.AllFlags);
123                 }
124
125                 public override SecurityElement ToXml () 
126                 {
127                         SecurityElement e = Element (version);
128                         if (IsUnrestricted ()) {
129                                 e.AddAttribute ("Unrestricted", "true");
130                         }
131                         else {
132                                 // TODO
133                         }
134                         return e;
135                 }
136
137                 public override IPermission Union (IPermission target)
138                 {
139                         KeyContainerPermission kcp = Cast (target);
140                         if (kcp == null)
141                                 return Copy ();
142
143                         KeyContainerPermissionAccessEntryCollection kcpaec = new KeyContainerPermissionAccessEntryCollection ();
144                         // copy first group
145                         foreach (KeyContainerPermissionAccessEntry kcpae in _accessEntries) {
146                                 kcpaec.Add (kcpae);
147                         }
148                         // copy second group...
149                         foreach (KeyContainerPermissionAccessEntry kcpae in kcp._accessEntries) {
150                                 // ... but only if not present in first group
151                                 if (_accessEntries.IndexOf (kcpae) == -1)
152                                         kcpaec.Add (kcpae);
153                         }
154
155                         if (kcpaec.Count == 0)
156                                 return new KeyContainerPermission ((_flags | kcp._flags));
157
158                         KeyContainerPermissionAccessEntry[] list = new KeyContainerPermissionAccessEntry [kcpaec.Count];
159                         kcpaec.CopyTo (list, 0);
160                         return new KeyContainerPermission ((_flags | kcp._flags), list);
161                 }
162
163                 // IBuiltInPermission
164                 int IBuiltInPermission.GetTokenIndex ()
165                 {
166                         return (int) BuiltInToken.KeyContainer;
167                 }
168
169                 // helpers
170
171                 private void SetFlags (KeyContainerPermissionFlags flags)
172                 {
173                         if ((flags & KeyContainerPermissionFlags.AllFlags) != 0) {
174                                 string msg = String.Format (Locale.GetText ("Invalid enum {0}"), flags);
175                                 throw new ArgumentException (msg, "KeyContainerPermissionFlags");
176                         }
177                         _flags = flags;
178                 }
179
180                 private KeyContainerPermission Cast (IPermission target)
181                 {
182                         if (target == null)
183                                 return null;
184
185                         KeyContainerPermission kcp = (target as KeyContainerPermission);
186                         if (kcp == null) {
187                                 ThrowInvalidPermission (target, typeof (KeyContainerPermission));
188                         }
189
190                         return kcp;
191                 }
192         }
193 }
194
195 #endif