New test.
[mono.git] / mcs / class / System / System.Security.Permissions / PermissionHelper.cs
1 //
2 // System.Security.Permissions.PermissionHelper.cs
3 //
4 // Author:
5 //      Sebastien Pouliot  <sebastien@ximian.com>
6 //
7 // Copyright (C) 2004 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 using System.Globalization;
30
31 namespace System.Security.Permissions {
32
33         internal sealed class PermissionHelper {
34
35                 // snippet moved from FileIOPermission (nickd) to be reused in all derived classes
36                 internal static SecurityElement Element (Type type, int version) 
37                 {
38                         SecurityElement se = new SecurityElement ("IPermission");
39                         se.AddAttribute ("class", type.FullName + ", " + type.Assembly.ToString ().Replace ('\"', '\''));
40                         se.AddAttribute ("version", version.ToString ());
41                         return se;
42                 }
43
44                 internal static PermissionState CheckPermissionState (PermissionState state, bool allowUnrestricted)
45                 {
46                         string msg;
47                         switch (state) {
48                         case PermissionState.None:
49                                 break;
50                         case PermissionState.Unrestricted:
51 #if !NET_2_0
52                                 if (!allowUnrestricted) {
53                                         msg = Locale.GetText ("Unrestricted isn't not allowed for identity permissions.");
54                                         throw new ArgumentException (msg, "state");
55                                 }
56 #endif
57                                 break;
58                         default:
59                                 msg = String.Format (Locale.GetText ("Invalid enum {0}"), state);
60                                 throw new ArgumentException (msg, "state");
61                         }
62                         return state;
63                 }
64
65                 internal static int CheckSecurityElement (SecurityElement se, string parameterName, int minimumVersion, int maximumVersion) 
66                 {
67                         if (se == null)
68                                 throw new ArgumentNullException (parameterName);
69
70                         if (se.Attribute ("class") == null) {
71                                 string msg = Locale.GetText ("Missing 'class' attribute.");
72                                 throw new ArgumentException (msg, parameterName);
73                         }
74
75                         // we assume minimum version if no version number is supplied
76                         int version = minimumVersion;
77                         string v = se.Attribute ("version");
78                         if (v != null) {
79                                 try {
80                                         version = Int32.Parse (v);
81                                 }
82                                 catch (Exception e) {
83                                         string msg = Locale.GetText ("Couldn't parse version from '{0}'.");
84                                         msg = String.Format (msg, v);
85                                         throw new ArgumentException (msg, parameterName, e);
86                                 }
87                         }
88
89                         if ((version < minimumVersion) || (version > maximumVersion)) {
90                                 string msg = Locale.GetText ("Unknown version '{0}', expected versions between ['{1}','{2}'].");
91                                 msg = String.Format (msg, version, minimumVersion, maximumVersion);
92                                 throw new ArgumentException (msg, parameterName);
93                         }
94                         return version;
95                 }
96
97                 // must be called after CheckSecurityElement (i.e. se != null)
98                 internal static bool IsUnrestricted (SecurityElement se) 
99                 {
100                         string value = se.Attribute ("Unrestricted");
101                         if (value == null)
102                                 return false;
103                         return (String.Compare (value, Boolean.TrueString, true, CultureInfo.InvariantCulture) == 0);
104                 }
105
106                 internal static void ThrowInvalidPermission (IPermission target, Type expected) 
107                 {
108                         string msg = Locale.GetText ("Invalid permission type '{0}', expected type '{1}'.");
109                         msg = String.Format (msg, target.GetType (), expected);
110                         throw new ArgumentException (msg, "target");
111                 }
112         }
113 }