New test.
[mono.git] / mcs / class / corlib / System.Security.Permissions / StrongNamePermissionAttribute.cs
1 //
2 // System.Security.Permissions.StrongNameIdentityPermissionAttribute.cs
3 //
4 // Authors:
5 //      Duncan Mak <duncan@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 #if NET_2_0
37         [ComVisible (true)]
38 #endif
39         [AttributeUsage (AttributeTargets.Assembly | AttributeTargets.Class |
40                          AttributeTargets.Struct | AttributeTargets.Constructor |
41                          AttributeTargets.Method, AllowMultiple=true, Inherited=false)]
42         [Serializable]
43         public sealed class StrongNameIdentityPermissionAttribute : CodeAccessSecurityAttribute {
44
45                 // Fields
46                 private string name;
47                 private string key;
48                 private string version;
49                 
50                 // Constructor
51                 public StrongNameIdentityPermissionAttribute (SecurityAction action) 
52                         : base (action)
53                 {
54                 }
55                 
56                 // Properties
57                 public string Name {
58                         get { return name; }
59                         set { name = value; }
60                 }
61
62                 public string PublicKey {
63                         get { return key; }
64                         set { key = value; }
65                 }
66
67                 public string Version {
68                         get { return version; }
69                         set { version = value; }
70                 }
71                          
72                 // Methods
73                 public override IPermission CreatePermission ()
74                 {
75                         if (this.Unrestricted)
76                                 return new StrongNameIdentityPermission (PermissionState.Unrestricted);
77
78                         if ((name == null) && (key == null) && (version == null))
79                                 return new StrongNameIdentityPermission (PermissionState.None);
80
81                         if (key == null) {
82                                 throw new ArgumentException (Locale.GetText (
83                                         "PublicKey is required"));
84                         }
85                         StrongNamePublicKeyBlob blob = StrongNamePublicKeyBlob.FromString (key);
86                                 
87                         Version v = null;
88                         if (version != null)
89                                 v = new Version (version);
90
91                         return new StrongNameIdentityPermission (blob, name, v);
92                 }
93         }
94 }