2 // GacIdentityPermissionTest.cs - NUnit Test Cases for GacIdentityPermission
5 // Sebastien Pouliot <sebastien@ximian.com>
7 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
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:
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
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.
31 using NUnit.Framework;
33 using System.Security;
34 using System.Security.Permissions;
36 namespace MonoTests.System.Security.Permissions {
39 public class GacIdentityPermissionTest {
42 public void PermissionStateNone ()
44 GacIdentityPermission gip = new GacIdentityPermission (PermissionState.None);
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");
51 GacIdentityPermission copy = (GacIdentityPermission)gip.Copy ();
52 Assert.IsFalse (Object.ReferenceEquals (gip, copy), "ReferenceEquals");
56 public void PermissionStateUnrestricted ()
58 GacIdentityPermission gip = new GacIdentityPermission (PermissionState.Unrestricted);
60 // FX 2.0 now supports Unrestricted for Identity Permissions
61 // However the XML doesn't show the Unrestricted status...
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");
68 GacIdentityPermission copy = (GacIdentityPermission)gip.Copy ();
69 Assert.IsFalse (Object.ReferenceEquals (gip, copy), "ReferenceEquals");
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 ;-)
78 [ExpectedException (typeof (ArgumentException))]
79 public void PermissionStateInvalid ()
81 GacIdentityPermission gip = new GacIdentityPermission ((PermissionState)2);
85 public void GacIdentityPermission_Empty ()
87 GacIdentityPermission gip = new GacIdentityPermission ();
88 Assert.IsNotNull (gip);
92 public void Intersect ()
94 GacIdentityPermission gip = new GacIdentityPermission ();
96 GacIdentityPermission intersect = (GacIdentityPermission)gip.Intersect (null);
97 Assert.IsNull (intersect, "gip N null");
99 GacIdentityPermission empty = new GacIdentityPermission (PermissionState.None);
100 intersect = (GacIdentityPermission)gip.Intersect (empty);
101 Assert.IsNotNull (intersect, "gip N null");
103 intersect = (GacIdentityPermission)gip.Intersect (gip);
104 Assert.IsNotNull (intersect, "gip N gip");
108 [ExpectedException (typeof (ArgumentException))]
109 public void Intersect_DifferentPermissions ()
111 GacIdentityPermission a = new GacIdentityPermission (PermissionState.None);
112 SecurityPermission b = new SecurityPermission (PermissionState.None);
117 public void IsSubsetOf ()
119 GacIdentityPermission gip = new GacIdentityPermission ();
120 Assert.IsFalse (gip.IsSubsetOf (null), "gip.IsSubsetOf (null)");
122 GacIdentityPermission empty = new GacIdentityPermission (PermissionState.None);
123 Assert.IsFalse (empty.IsSubsetOf (null), "empty.IsSubsetOf (null)");
127 [ExpectedException (typeof (ArgumentException))]
128 public void IsSubsetOf_DifferentPermissions ()
130 GacIdentityPermission a = new GacIdentityPermission (PermissionState.None);
131 SecurityPermission b = new SecurityPermission (PermissionState.None);
138 GacIdentityPermission gip = new GacIdentityPermission ();
140 GacIdentityPermission union = (GacIdentityPermission)gip.Union (null);
141 Assert.IsNotNull (union, "gip U null");
143 GacIdentityPermission empty = new GacIdentityPermission (PermissionState.None);
144 union = (GacIdentityPermission)gip.Union (empty);
145 Assert.IsNotNull (union, "gip U empty");
147 union = (GacIdentityPermission)gip.Union (gip);
148 Assert.IsNotNull (union, "gip U gip");
150 // note: can't be tested with PermissionState.Unrestricted
154 [ExpectedException (typeof (ArgumentException))]
155 public void Union_DifferentPermissions ()
157 GacIdentityPermission a = new GacIdentityPermission (PermissionState.None);
158 SecurityPermission b = new SecurityPermission (PermissionState.None);
163 [ExpectedException (typeof (ArgumentNullException))]
164 public void FromXml_Null ()
166 GacIdentityPermission gip = new GacIdentityPermission ();
171 [ExpectedException (typeof (ArgumentException))]
172 public void FromXml_WrongTag ()
174 GacIdentityPermission gip = new GacIdentityPermission ();
175 SecurityElement se = gip.ToXml ();
181 [ExpectedException (typeof (ArgumentException))]
182 public void FromXml_WrongTagCase ()
184 GacIdentityPermission gip = new GacIdentityPermission ();
185 SecurityElement se = gip.ToXml ();
186 se.Tag = "IPERMISSION"; // instead of IPermission
191 public void FromXml_WrongClass ()
193 GacIdentityPermission gip = new GacIdentityPermission ();
194 SecurityElement se = gip.ToXml ();
196 SecurityElement w = new SecurityElement (se.Tag);
197 w.AddAttribute ("class", "Wrong" + se.Attribute ("class"));
198 w.AddAttribute ("version", se.Attribute ("version"));
200 // doesn't care of the class name at that stage
201 // anyway the class has already be created so...
205 public void FromXml_NoClass ()
207 GacIdentityPermission gip = new GacIdentityPermission ();
208 SecurityElement se = gip.ToXml ();
210 SecurityElement w = new SecurityElement (se.Tag);
211 w.AddAttribute ("version", se.Attribute ("version"));
213 // doesn't even care of the class attribute presence
217 [ExpectedException (typeof (ArgumentException))]
218 public void FromXml_WrongVersion ()
220 GacIdentityPermission gip = new GacIdentityPermission ();
221 SecurityElement se = gip.ToXml ();
222 se.Attributes.Remove ("version");
223 se.Attributes.Add ("version", "2");
228 public void FromXml_NoVersion ()
230 GacIdentityPermission gip = new GacIdentityPermission ();
231 SecurityElement se = gip.ToXml ();
233 SecurityElement w = new SecurityElement (se.Tag);
234 w.AddAttribute ("class", se.Attribute ("class"));