[bcl] Remove more NET_2_0 checks from class libs
[mono.git] / mcs / class / corlib / Test / System.Security.Permissions / GacIdentityPermissionTest.cs
1 //
2 // GacIdentityPermissionTest.cs - NUnit Test Cases for GacIdentityPermission
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
30 using NUnit.Framework;
31 using System;
32 using System.Security;
33 using System.Security.Permissions;
34
35 namespace MonoTests.System.Security.Permissions {
36
37         [TestFixture]
38         public class GacIdentityPermissionTest {
39
40                 [Test]
41                 public void PermissionStateNone ()
42                 {
43                         GacIdentityPermission gip = new GacIdentityPermission (PermissionState.None);
44
45                         SecurityElement se = gip.ToXml ();
46                         // only class and version are present
47                         Assert.AreEqual (2, se.Attributes.Count, "Xml-Attributes");
48                         Assert.IsNull (se.Children, "Xml-Children");
49
50                         GacIdentityPermission copy = (GacIdentityPermission)gip.Copy ();
51                         Assert.IsFalse (Object.ReferenceEquals (gip, copy), "ReferenceEquals");
52                 }
53
54                 [Category ("NotWorking")]
55                 [Test]
56                 public void PermissionStateUnrestricted ()
57                 {
58                         GacIdentityPermission gip = new GacIdentityPermission (PermissionState.Unrestricted);
59
60                         // FX 2.0 now supports Unrestricted for Identity Permissions
61                         // However the XML doesn't show the Unrestricted status...
62
63                         SecurityElement se = gip.ToXml ();
64                         // only class and version are present
65                         Assert.AreEqual (2, se.Attributes.Count, "Xml-Attributes");
66                         Assert.IsNull (se.Children, "Xml-Children");
67
68                         GacIdentityPermission copy = (GacIdentityPermission)gip.Copy ();
69                         Assert.IsFalse (Object.ReferenceEquals (gip, copy), "ReferenceEquals");
70
71                         // ... and because it doesn't implement IUnrestrictedPermission
72                         // there is not way to know if it's unrestricted so...
73                         Assert.IsTrue (gip.Equals (new GacIdentityPermission (PermissionState.None)), "Unrestricted==None");
74                         // there is not much difference after all ;-)
75                 }
76
77                 [Test]
78                 [ExpectedException (typeof (ArgumentException))]
79                 public void PermissionStateInvalid ()
80                 {
81                         GacIdentityPermission gip = new GacIdentityPermission ((PermissionState)2);
82                 }
83
84                 [Test]
85                 public void GacIdentityPermission_Empty ()
86                 {
87                         GacIdentityPermission gip = new GacIdentityPermission ();
88                         Assert.IsNotNull (gip);
89                 }
90
91                 [Test]
92                 public void Intersect ()
93                 {
94                         GacIdentityPermission gip = new GacIdentityPermission ();
95
96                         GacIdentityPermission intersect = (GacIdentityPermission)gip.Intersect (null);
97                         Assert.IsNull (intersect, "gip N null");
98
99                         GacIdentityPermission empty = new GacIdentityPermission (PermissionState.None);
100                         intersect = (GacIdentityPermission)gip.Intersect (empty);
101                         Assert.IsNotNull (intersect, "gip N null");
102
103                         intersect = (GacIdentityPermission)gip.Intersect (gip);
104                         Assert.IsNotNull (intersect, "gip N gip");
105                 }
106
107                 [Test]
108                 [ExpectedException (typeof (ArgumentException))]
109                 public void Intersect_DifferentPermissions ()
110                 {
111                         GacIdentityPermission a = new GacIdentityPermission (PermissionState.None);
112                         SecurityPermission b = new SecurityPermission (PermissionState.None);
113                         a.Intersect (b);
114                 }
115
116                 [Test]
117                 public void IsSubsetOf ()
118                 {
119                         GacIdentityPermission gip = new GacIdentityPermission ();
120                         Assert.IsFalse (gip.IsSubsetOf (null), "gip.IsSubsetOf (null)");
121
122                         GacIdentityPermission empty = new GacIdentityPermission (PermissionState.None);
123                         Assert.IsFalse (empty.IsSubsetOf (null), "empty.IsSubsetOf (null)");
124                 }
125
126                 [Test]
127                 [ExpectedException (typeof (ArgumentException))]
128                 public void IsSubsetOf_DifferentPermissions ()
129                 {
130                         GacIdentityPermission a = new GacIdentityPermission (PermissionState.None);
131                         SecurityPermission b = new SecurityPermission (PermissionState.None);
132                         a.IsSubsetOf (b);
133                 }
134
135                 [Test]
136                 public void Union ()
137                 {
138                         GacIdentityPermission gip = new GacIdentityPermission ();
139
140                         GacIdentityPermission union = (GacIdentityPermission)gip.Union (null);
141                         Assert.IsNotNull (union, "gip U null");
142
143                         GacIdentityPermission empty = new GacIdentityPermission (PermissionState.None);
144                         union = (GacIdentityPermission)gip.Union (empty);
145                         Assert.IsNotNull (union, "gip U empty");
146
147                         union = (GacIdentityPermission)gip.Union (gip);
148                         Assert.IsNotNull (union, "gip U gip");
149
150                         // note: can't be tested with PermissionState.Unrestricted
151                 }
152
153                 [Test]
154                 [ExpectedException (typeof (ArgumentException))]
155                 public void Union_DifferentPermissions ()
156                 {
157                         GacIdentityPermission a = new GacIdentityPermission (PermissionState.None);
158                         SecurityPermission b = new SecurityPermission (PermissionState.None);
159                         a.Union (b);
160                 }
161
162                 [Test]
163                 [ExpectedException (typeof (ArgumentNullException))]
164                 public void FromXml_Null ()
165                 {
166                         GacIdentityPermission gip = new GacIdentityPermission ();
167                         gip.FromXml (null);
168                 }
169
170                 [Test]
171                 [ExpectedException (typeof (ArgumentException))]
172                 public void FromXml_WrongTag ()
173                 {
174                         GacIdentityPermission gip = new GacIdentityPermission ();
175                         SecurityElement se = gip.ToXml ();
176                         se.Tag = "IMono";
177                         gip.FromXml (se);
178                 }
179
180                 [Test]
181                 [ExpectedException (typeof (ArgumentException))]
182                 public void FromXml_WrongTagCase ()
183                 {
184                         GacIdentityPermission gip = new GacIdentityPermission ();
185                         SecurityElement se = gip.ToXml ();
186                         se.Tag = "IPERMISSION"; // instead of IPermission
187                         gip.FromXml (se);
188                 }
189
190                 [Test]
191                 public void FromXml_WrongClass ()
192                 {
193                         GacIdentityPermission gip = new GacIdentityPermission ();
194                         SecurityElement se = gip.ToXml ();
195
196                         SecurityElement w = new SecurityElement (se.Tag);
197                         w.AddAttribute ("class", "Wrong" + se.Attribute ("class"));
198                         w.AddAttribute ("version", se.Attribute ("version"));
199                         gip.FromXml (w);
200                         // doesn't care of the class name at that stage
201                         // anyway the class has already be created so...
202                 }
203
204                 [Test]
205                 public void FromXml_NoClass ()
206                 {
207                         GacIdentityPermission gip = new GacIdentityPermission ();
208                         SecurityElement se = gip.ToXml ();
209
210                         SecurityElement w = new SecurityElement (se.Tag);
211                         w.AddAttribute ("version", se.Attribute ("version"));
212                         gip.FromXml (w);
213                         // doesn't even care of the class attribute presence
214                 }
215
216                 [Test]
217                 [ExpectedException (typeof (ArgumentException))]
218                 public void FromXml_WrongVersion ()
219                 {
220                         GacIdentityPermission gip = new GacIdentityPermission ();
221                         SecurityElement se = gip.ToXml ();
222                         se.Attributes.Remove ("version");
223                         se.Attributes.Add ("version", "2");
224                         gip.FromXml (se);
225                 }
226
227                 [Test]
228                 public void FromXml_NoVersion ()
229                 {
230                         GacIdentityPermission gip = new GacIdentityPermission ();
231                         SecurityElement se = gip.ToXml ();
232
233                         SecurityElement w = new SecurityElement (se.Tag);
234                         w.AddAttribute ("class", se.Attribute ("class"));
235                         gip.FromXml (w);
236                 }
237         }
238 }
239