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