Switch to compiler-tester
[mono.git] / mcs / class / corlib / System.Security.Permissions / SecurityPermission.cs
1 //
2 // System.Security.Permissions.SecurityPermission.cs
3 //
4 // Authors:
5 //      Dan Lewis (dihlewis@yahoo.co.uk)
6 //      Sebastien Pouliot  <sebastien@ximian.com>
7 //
8 // (C) 2002
9 // Portions (C) 2003 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.Runtime.InteropServices;
34
35 namespace System.Security.Permissions {
36
37 #if NET_2_0
38         [ComVisible (true)]
39 #endif
40
41         [Serializable]
42         public sealed class SecurityPermission :
43                 CodeAccessPermission, IUnrestrictedPermission, IBuiltInPermission {
44
45                 private const int version = 1;
46
47                 private SecurityPermissionFlag flags;
48
49                 // constructors
50
51                 public SecurityPermission (PermissionState state)
52                 {
53                         if (CheckPermissionState (state, true) == PermissionState.Unrestricted)
54                                 flags = SecurityPermissionFlag.AllFlags;
55                         else
56                                 flags = SecurityPermissionFlag.NoFlags;
57                 }
58
59                 public SecurityPermission (SecurityPermissionFlag flags) 
60                 {
61                         // reuse validation by the Flags property
62                         Flags = flags;
63                 }
64
65                 public SecurityPermissionFlag Flags {
66                         get { return flags; }
67                         set {
68                                 if ((value & SecurityPermissionFlag.AllFlags) != value) {
69                                         string msg = String.Format (Locale.GetText ("Invalid flags {0}"), value);
70                                         throw new ArgumentException (msg, "SecurityPermissionFlag");
71                                 }
72                                 flags = value;
73                         }
74                 }
75
76                 // IUnrestrictedPermission
77                 public bool IsUnrestricted () 
78                 {
79                         return (flags == SecurityPermissionFlag.AllFlags);
80                 }
81
82                 public override IPermission Copy () 
83                 {
84                         return new SecurityPermission (flags);
85                 }
86
87                 public override IPermission Intersect (IPermission target) 
88                 {
89                         SecurityPermission sp = Cast (target);
90                         if (sp == null)
91                                 return null;
92                         if (IsEmpty () || sp.IsEmpty ())
93                                 return null;
94
95                         if (this.IsUnrestricted () && sp.IsUnrestricted ())
96                                 return new SecurityPermission (PermissionState.Unrestricted);
97                         if (this.IsUnrestricted ())
98                                 return sp.Copy ();
99                         if (sp.IsUnrestricted ())
100                                 return this.Copy ();
101
102                         SecurityPermissionFlag f = flags & sp.flags;
103                         if (f == SecurityPermissionFlag.NoFlags)
104                                 return null;
105                         else
106                                 return new SecurityPermission (f);
107                 }
108
109                 public override IPermission Union (IPermission target) 
110                 {
111                         SecurityPermission sp = Cast (target);
112                         if (sp == null)
113                                 return this.Copy ();
114
115                         if (this.IsUnrestricted () || sp.IsUnrestricted ())
116                                 return new SecurityPermission (PermissionState.Unrestricted);
117                         
118                         return new SecurityPermission (flags | sp.flags);
119                 }
120
121                 public override bool IsSubsetOf (IPermission target) 
122                 {
123                         SecurityPermission sp = Cast (target);
124                         if (sp == null) 
125                                 return IsEmpty ();
126
127                         if (sp.IsUnrestricted ())
128                                 return true;
129                         if (this.IsUnrestricted ())
130                                 return false;
131
132                         return ((flags & ~sp.flags) == 0);
133                 }
134
135                 public override void FromXml (SecurityElement e) 
136                 {
137                         // General validation in CodeAccessPermission
138                         CheckSecurityElement (e, "e", version, version);
139                         // Note: we do not (yet) care about the return value 
140                         // as we only accept version 1 (min/max values)
141
142                         if (IsUnrestricted (e)) {
143                                 flags = SecurityPermissionFlag.AllFlags;
144                         }
145                         else {
146                                 string f = e.Attribute ("Flags");
147                                 if (f == null) {
148                                         flags = SecurityPermissionFlag.NoFlags;
149                                 }
150                                 else {
151                                         flags = (SecurityPermissionFlag) Enum.Parse (
152                                                 typeof (SecurityPermissionFlag), f);
153                                 }
154                         }
155                 }
156
157                 public override SecurityElement ToXml () 
158                 {
159                         SecurityElement e = Element (version);
160                         if (IsUnrestricted ())
161                                 e.AddAttribute ("Unrestricted", "true");
162                         else
163                                 e.AddAttribute ("Flags", flags.ToString ());
164                         return e;
165                 }
166
167                 // IBuiltInPermission
168                 int IBuiltInPermission.GetTokenIndex ()
169                 {
170                         return (int) BuiltInToken.Security;
171                 }
172
173                 // helpers
174
175                 private bool IsEmpty ()
176                 {
177                         return (flags == SecurityPermissionFlag.NoFlags);
178                 }
179
180                 private SecurityPermission Cast (IPermission target)
181                 {
182                         if (target == null)
183                                 return null;
184
185                         SecurityPermission sp = (target as SecurityPermission);
186                         if (sp == null) {
187                                 ThrowInvalidPermission (target, typeof (SecurityPermission));
188                         }
189
190                         return sp;
191                 }
192         }
193 }