2 // NamedPermissionSetTest.cs - NUnit Test Cases for NamedPermissionSet
5 // Sebastien Pouliot <sebastien@ximian.com>
7 // (C) 2003 Motus Technologies Inc. (http://www.motus.com)
8 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
10 // Permission is hereby granted, free of charge, to any person obtaining
11 // a copy of this software and associated documentation files (the
12 // "Software"), to deal in the Software without restriction, including
13 // without limitation the rights to use, copy, modify, merge, publish,
14 // distribute, sublicense, and/or sell copies of the Software, and to
15 // permit persons to whom the Software is furnished to do so, subject to
16 // the following conditions:
18 // The above copyright notice and this permission notice shall be
19 // included in all copies or substantial portions of the Software.
21 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
22 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
23 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
24 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
25 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
26 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
27 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
30 using NUnit
.Framework
;
32 using System
.Security
;
33 using System
.Security
.Permissions
;
35 namespace MonoTests
.System
.Security
{
38 public class NamedPermissionSetTest
: Assertion
{
40 private static string name
= "mono";
41 private static string sentinel
= "go mono!";
44 [ExpectedException (typeof (ArgumentException
))]
45 public void ConstructorNameNull ()
47 string s
= null; // we don't want to confuse the compiler
48 NamedPermissionSet nps
= new NamedPermissionSet (s
);
52 [ExpectedException (typeof (ArgumentException
))]
53 public void ConstructorNameEmpty ()
55 NamedPermissionSet nps
= new NamedPermissionSet ("");
59 public void Description ()
61 NamedPermissionSet nps
= new NamedPermissionSet (name
);
62 // null by default (not empty)
63 AssertNull ("Description", nps
.Description
);
64 // is null-able (without exception)
65 nps
.Description
= null;
66 AssertNull ("Description(null)", nps
.Description
);
67 nps
.Description
= sentinel
;
68 AssertEquals ("Description", sentinel
, nps
.Description
);
72 [ExpectedException (typeof (ArgumentException
))]
73 public void NameNull ()
75 NamedPermissionSet nps
= new NamedPermissionSet (name
);
77 // strangely this isn't a ArgumentNullException (but so says the doc)
81 [ExpectedException (typeof (ArgumentException
))]
82 public void NameEmpty ()
84 NamedPermissionSet nps
= new NamedPermissionSet (name
);
91 NamedPermissionSet nps
= new NamedPermissionSet (name
);
93 AssertEquals ("Name", sentinel
, nps
.Name
);
99 NamedPermissionSet nps
= new NamedPermissionSet (name
);
100 nps
.Description
= sentinel
;
101 nps
.AddPermission (new SecurityPermission (SecurityPermissionFlag
.Assertion
));
102 NamedPermissionSet copy
= (NamedPermissionSet
)nps
.Copy ();
103 AssertEquals ("Name", nps
.Name
, copy
.Name
);
104 AssertEquals ("Description", nps
.Description
, copy
.Description
);
105 AssertEquals ("Count", nps
.Count
, copy
.Count
);
109 public void Copy_Name ()
111 NamedPermissionSet nps
= new NamedPermissionSet (name
);
112 nps
.Description
= sentinel
;
113 nps
.AddPermission (new SecurityPermission (SecurityPermissionFlag
.Assertion
));
114 NamedPermissionSet copy
= (NamedPermissionSet
)nps
.Copy ("Copy");
115 AssertEquals ("Name", "Copy", copy
.Name
);
116 AssertEquals ("Description", nps
.Description
, copy
.Description
);
117 AssertEquals ("Count", nps
.Count
, copy
.Count
);
121 [ExpectedException (typeof (ArgumentException
))]
122 public void Copy_Name_Null ()
124 NamedPermissionSet nps
= new NamedPermissionSet (name
);
125 NamedPermissionSet copy
= (NamedPermissionSet
)nps
.Copy (null);
129 [ExpectedException (typeof (ArgumentException
))]
130 public void Copy_Name_Empty ()
132 NamedPermissionSet nps
= new NamedPermissionSet (name
);
133 NamedPermissionSet copy
= (NamedPermissionSet
)nps
.Copy (String
.Empty
);
137 [ExpectedException (typeof (ArgumentNullException
))]
138 public void FromXml_Null ()
140 NamedPermissionSet nps
= new NamedPermissionSet (name
, PermissionState
.None
);
145 [ExpectedException (typeof (ArgumentException
))]
146 public void FromXml_InvalidPermission ()
148 NamedPermissionSet nps
= new NamedPermissionSet (name
, PermissionState
.None
);
149 SecurityElement se
= nps
.ToXml ();
150 // can't modify - so we create our own
151 SecurityElement se2
= new SecurityElement ("InvalidPermissionSet", se
.Text
);
152 se2
.AddAttribute ("class", se
.Attribute ("class"));
153 se2
.AddAttribute ("version", se
.Attribute ("version"));
154 se2
.AddAttribute ("Name", se
.Attribute ("Name"));
159 [ExpectedException (typeof (ArgumentException
))]
160 public void FromXml_WrongTagCase ()
162 NamedPermissionSet nps
= new NamedPermissionSet (name
, PermissionState
.None
);
163 SecurityElement se
= nps
.ToXml ();
164 se
.Tag
= se
.Tag
.ToUpper (); // instead of PermissionSet
169 public void FromXml_WrongClass ()
171 NamedPermissionSet nps
= new NamedPermissionSet (name
, PermissionState
.None
);
172 SecurityElement se
= nps
.ToXml ();
174 SecurityElement w
= new SecurityElement (se
.Tag
);
175 w
.AddAttribute ("class", "Wrong" + se
.Attribute ("class"));
176 w
.AddAttribute ("version", se
.Attribute ("version"));
177 w
.AddAttribute ("Name", se
.Attribute ("Name"));
179 // doesn't care of the class name at that stage
180 // anyway the class has already be created so...
184 public void FromXml_NoClass ()
186 NamedPermissionSet nps
= new NamedPermissionSet (name
, PermissionState
.None
);
187 SecurityElement se
= nps
.ToXml ();
189 SecurityElement w
= new SecurityElement (se
.Tag
);
190 w
.AddAttribute ("version", se
.Attribute ("version"));
192 // doesn't even care of the class attribute presence
196 // [ExpectedException (typeof (ArgumentException))]
197 public void FromXml_WrongVersion ()
199 NamedPermissionSet nps
= new NamedPermissionSet (name
, PermissionState
.None
);
200 SecurityElement se
= nps
.ToXml ();
201 // can't modify - so we create our own
202 SecurityElement se2
= new SecurityElement (se
.Tag
, se
.Text
);
203 se2
.AddAttribute ("class", se
.Attribute ("class"));
204 se2
.AddAttribute ("version", "2");
205 se2
.AddAttribute ("Name", se
.Attribute ("Name"));
207 // wow - here we accept a version 2 !!!
211 public void FromXml_NoVersion ()
213 NamedPermissionSet nps
= new NamedPermissionSet (name
, PermissionState
.None
);
214 SecurityElement se
= nps
.ToXml ();
216 SecurityElement w
= new SecurityElement (se
.Tag
);
217 w
.AddAttribute ("class", se
.Attribute ("class"));
218 w
.AddAttribute ("Name", se
.Attribute ("Name"));
223 public void FromXml_NoName ()
225 NamedPermissionSet nps
= new NamedPermissionSet (name
, PermissionState
.None
);
226 SecurityElement se
= nps
.ToXml ();
228 SecurityElement w
= new SecurityElement (se
.Tag
);
229 w
.AddAttribute ("class", se
.Attribute ("class"));
230 w
.AddAttribute ("version", "1");
233 // having a null name can badly influence the rest of the class code
234 AssertNull ("Name", nps
.Name
);
235 NamedPermissionSet copy
= (NamedPermissionSet
) nps
.Copy ();
236 AssertNull ("Copy.Name", copy
.Name
);
238 copy
= nps
.Copy ("name");
239 AssertEquals ("Copy(Name).Name", "name", copy
.Name
);
242 AssertNull ("Name attribute", se
.Attribute ("Name"));
244 AssertEquals ("GetHashCode", 0, nps
.GetHashCode ());
245 Assert ("Equals-self", nps
.Equals (nps
));
250 public void FromXml ()
252 NamedPermissionSet nps
= new NamedPermissionSet (name
, PermissionState
.None
);
253 SecurityElement se
= nps
.ToXml ();
254 AssertNotNull ("ToXml()", se
);
256 NamedPermissionSet nps2
= (NamedPermissionSet
) nps
.Copy ();
258 AssertEquals ("FromXml-Copy.Name", name
, nps2
.Name
);
259 // strangely it's empty when converted from XML (but null when created)
260 AssertEquals ("FromXml-Copy.Description", "", nps2
.Description
);
261 Assert ("FromXml-Copy.IsUnrestricted", !nps2
.IsUnrestricted ());
263 se
.AddAttribute ("Description", sentinel
);
265 AssertEquals ("FromXml-Add1.Name", name
, nps2
.Name
);
266 AssertEquals ("FromXml-Add1.Description", sentinel
, nps2
.Description
);
267 Assert ("FromXml-Add1.IsUnrestricted", !nps2
.IsUnrestricted ());
269 se
.AddAttribute ("Unrestricted", "true");
271 AssertEquals ("FromXml-Add2.Name", name
, nps2
.Name
);
272 AssertEquals ("FromXml-Add2.Description", sentinel
, nps2
.Description
);
273 Assert ("FromXml-Add2.IsUnrestricted", nps2
.IsUnrestricted ());
277 public void ToXml_None ()
279 NamedPermissionSet ps
= new NamedPermissionSet (name
, PermissionState
.None
);
280 ps
.Description
= sentinel
;
281 SecurityElement se
= ps
.ToXml ();
282 Assert ("None.ToString().StartsWith", ps
.ToString().StartsWith ("<PermissionSet"));
283 AssertEquals ("None.class", "System.Security.NamedPermissionSet", (se
.Attributes
["class"] as string));
284 AssertEquals ("None.version", "1", (se
.Attributes
["version"] as string));
285 AssertEquals ("None.Name", name
, (se
.Attributes
["Name"] as string));
286 AssertEquals ("None.Description", sentinel
, (se
.Attributes
["Description"] as string));
287 AssertNull ("None.Unrestricted", (se
.Attributes
["Unrestricted"] as string));
291 public void ToXml_Unrestricted ()
293 NamedPermissionSet ps
= new NamedPermissionSet (name
, PermissionState
.Unrestricted
);
294 SecurityElement se
= ps
.ToXml ();
295 Assert ("Unrestricted.ToString().StartsWith", ps
.ToString().StartsWith ("<PermissionSet"));
296 AssertEquals ("Unrestricted.class", "System.Security.NamedPermissionSet", (se
.Attributes
["class"] as string));
297 AssertEquals ("Unrestricted.version", "1", (se
.Attributes
["version"] as string));
298 AssertEquals ("Unrestricted.Name", name
, (se
.Attributes
["Name"] as string));
299 AssertNull ("Unrestricted.Description", (se
.Attributes
["Description"] as string));
300 AssertEquals ("Unrestricted.Unrestricted", "true", (se
.Attributes
["Unrestricted"] as string));
304 public void Equals ()
306 NamedPermissionSet psn
= new NamedPermissionSet (name
, PermissionState
.None
);
307 NamedPermissionSet psu
= new NamedPermissionSet (name
, PermissionState
.Unrestricted
);
308 Assert ("psn!=psu", !psn
.Equals (psu
));
309 Assert ("psu!=psn", !psu
.Equals (psn
));
310 NamedPermissionSet cpsn
= (NamedPermissionSet
) psn
.Copy ();
311 Assert ("cpsn==psn", cpsn
.Equals (psn
));
312 Assert ("psn==cpsn", psn
.Equals (cpsn
));
313 NamedPermissionSet cpsu
= (NamedPermissionSet
) psu
.Copy ();
314 Assert ("cpsu==psu", cpsu
.Equals (psu
));
315 Assert ("psu==cpsu", psu
.Equals (cpsu
));
316 cpsn
.Description
= sentinel
;
317 Assert ("cpsn+desc==psn", cpsn
.Equals (psn
));
318 Assert ("psn==cpsn+desc", psn
.Equals (cpsn
));
319 cpsn
.Description
= sentinel
;
320 Assert ("cpsu+desc==psu", cpsu
.Equals (psu
));
321 Assert ("psu==cpsu+desc", psu
.Equals (cpsu
));
325 public void GetHashCode_ ()
327 NamedPermissionSet psn
= new NamedPermissionSet (name
, PermissionState
.None
);
328 int nhc
= psn
.GetHashCode ();
329 NamedPermissionSet psu
= new NamedPermissionSet (name
, PermissionState
.Unrestricted
);
330 int uhc
= psu
.GetHashCode ();
331 Assert ("GetHashCode-1", nhc
!= uhc
);
332 psn
.Description
= sentinel
;
333 Assert ("GetHashCode-2", psn
.GetHashCode () == nhc
);
334 psu
.Description
= sentinel
;
335 Assert ("GetHashCode-3", psu
.GetHashCode () == uhc
);