New tests.
[mono.git] / mcs / class / corlib / System.Security.Permissions / ZoneIdentityPermission.cs
1 //
2 // System.Security.Permissions.ZoneIdentityPermission
3 //
4 // Authors:
5 //      Gonzalo Paniagua Javier (gonzalo@ximian.com)
6 //      Sebastien Pouliot  <sebastien@ximian.com>
7 //
8 // (C) 2002 Ximian, Inc (http://www.ximian.com)
9 // Copyright (C) 2004-2005 Novell, Inc (http://www.novell.com)
10 //
11 // Permission is hereby granted, free of charge, to any person obtaining
12 // a copy of this software and associated documentation files (the
13 // "Software"), to deal in the Software without restriction, including
14 // without limitation the rights to use, copy, modify, merge, publish,
15 // distribute, sublicense, and/or sell copies of the Software, and to
16 // permit persons to whom the Software is furnished to do so, subject to
17 // the following conditions:
18 // 
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
21 // 
22 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
23 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
25 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
26 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
27 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
28 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29 //
30
31 using System.Globalization;
32 using System.Runtime.InteropServices;
33
34 namespace System.Security.Permissions {
35
36         [ComVisible (true)]
37         [Serializable]
38         public sealed class ZoneIdentityPermission : CodeAccessPermission, IBuiltInPermission {
39
40                 private const int version = 1;
41
42                 private SecurityZone zone;
43
44                 public ZoneIdentityPermission (PermissionState state)
45                 {
46                         // false == do not allow Unrestricted for Identity Permissions
47                         CheckPermissionState (state, false);
48                         // default values
49                         zone = SecurityZone.NoZone;
50                 }
51
52                 public ZoneIdentityPermission (SecurityZone zone)
53                 {
54                         // also needs the validations
55                         SecurityZone = zone;
56                 }
57
58                 public override IPermission Copy ()
59                 {
60                         return new ZoneIdentityPermission (zone);
61                 }
62
63                 public override bool IsSubsetOf (IPermission target)
64                 {
65                         ZoneIdentityPermission zip = Cast (target);
66                         if (zip == null)
67                                 return (zone == SecurityZone.NoZone);
68
69                         return ((zone == SecurityZone.NoZone) || (zone == zip.zone));
70                 }
71
72                 public override IPermission Union (IPermission target)
73                 {
74                         ZoneIdentityPermission zip = Cast (target);
75                         if (zip == null)
76                                 return (zone == SecurityZone.NoZone) ? null : Copy ();
77
78                         if (zone == zip.zone || zip.zone == SecurityZone.NoZone)
79                                 return Copy ();
80
81                         if (zone == SecurityZone.NoZone)
82                                 return zip.Copy ();
83                         throw new ArgumentException (Locale.GetText (
84                                 "Union impossible"));
85                 }
86
87                 public override IPermission Intersect (IPermission target)
88                 {
89                         ZoneIdentityPermission zip = Cast (target);
90                         if (zip == null || zone == SecurityZone.NoZone)
91                                 return null;
92
93                         if (zone == zip.zone)
94                                 return Copy ();
95
96                         return null;
97                 }
98
99                 public override void FromXml (SecurityElement esd)
100                 {
101                         // General validation in CodeAccessPermission
102                         CheckSecurityElement (esd, "esd", version, version);
103                         // Note: we do not (yet) care about the return value 
104                         // as we only accept version 1 (min/max values)
105
106                         string zoneName = esd.Attribute ("Zone");
107                         if (zoneName == null)
108                                 zone = SecurityZone.NoZone;
109                         else
110                                 zone = (SecurityZone) Enum.Parse (typeof (SecurityZone), zoneName);
111                 }
112
113                 public override SecurityElement ToXml ()
114                 {
115                         SecurityElement se = Element (version);
116                         if (zone != SecurityZone.NoZone)
117                                 se.AddAttribute ("Zone", zone.ToString ());
118                         return se;
119                 }
120
121                 public SecurityZone SecurityZone {
122                         get { return zone; }
123                         set {
124                                 if (!Enum.IsDefined (typeof (SecurityZone), value)) {
125                                         string msg = String.Format (Locale.GetText ("Invalid enum {0}"), value);
126                                         throw new ArgumentException (msg, "SecurityZone");
127                                 }
128                                 zone = value;
129                         }
130                 }
131
132                 // IBuiltInPermission
133                 int IBuiltInPermission.GetTokenIndex ()
134                 {
135                         return (int) BuiltInToken.ZoneIdentity;
136                 }
137
138                 // helpers
139
140                 private ZoneIdentityPermission Cast (IPermission target)
141                 {
142                         if (target == null)
143                                 return null;
144
145                         ZoneIdentityPermission zip = (target as ZoneIdentityPermission);
146                         if (zip == null) {
147                                 ThrowInvalidPermission (target, typeof (ZoneIdentityPermission));
148                         }
149
150                         return zip;
151                 }
152         }
153 }
154