2 // PrintingPermissionTest.cs - NUnit Test Cases for PrintingPermission
5 // Sebastien Pouliot <sebastien@ximian.com>
7 // Copyright (C) 2004-2005 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.
29 using NUnit.Framework;
32 using System.Drawing.Printing;
33 using System.Security;
34 using System.Security.Permissions;
36 namespace MonoTests.System.Drawing.Printing {
39 public class PrintingPermissionTest {
41 static PrintingPermissionLevel[] AllLevel = {
42 PrintingPermissionLevel.NoPrinting,
43 PrintingPermissionLevel.SafePrinting,
44 PrintingPermissionLevel.DefaultPrinting,
45 PrintingPermissionLevel.AllPrinting,
48 static PrintingPermissionLevel[] AllLevelExceptNoLevel = {
49 PrintingPermissionLevel.SafePrinting,
50 PrintingPermissionLevel.DefaultPrinting,
51 PrintingPermissionLevel.AllPrinting,
54 static PrintingPermissionLevel[] AllLevelExceptAllLevel = {
55 PrintingPermissionLevel.NoPrinting,
56 PrintingPermissionLevel.SafePrinting,
57 PrintingPermissionLevel.DefaultPrinting,
60 static PrintingPermissionLevel[] AllLevelExceptNoAndAllLevel = {
61 PrintingPermissionLevel.SafePrinting,
62 PrintingPermissionLevel.DefaultPrinting,
66 public void PermissionState_None ()
68 PermissionState ps = PermissionState.None;
69 PrintingPermission pp = new PrintingPermission (ps);
70 Assert.AreEqual (PrintingPermissionLevel.NoPrinting, pp.Level, "Level");
71 Assert.IsFalse (pp.IsUnrestricted (), "IsUnrestricted");
73 SecurityElement se = pp.ToXml ();
74 // only class and version are present
75 Assert.AreEqual ("NoPrinting", se.Attribute ("Level"), "Xml-Level");
76 Assert.IsNull (se.Children, "Xml-Children");
78 PrintingPermission copy = (PrintingPermission)pp.Copy ();
79 Assert.IsFalse (Object.ReferenceEquals (pp, copy), "ReferenceEquals");
80 Assert.AreEqual (pp.Level, copy.Level, "Level");
81 Assert.AreEqual (pp.IsUnrestricted (), copy.IsUnrestricted (), "IsUnrestricted ()");
85 public void PermissionState_Unrestricted ()
87 PermissionState ps = PermissionState.Unrestricted;
88 PrintingPermission pp = new PrintingPermission (ps);
89 Assert.AreEqual (PrintingPermissionLevel.AllPrinting, pp.Level, "Level");
90 Assert.IsTrue (pp.IsUnrestricted (), "IsUnrestricted");
92 SecurityElement se = pp.ToXml ();
93 // only class and version are present
94 Assert.AreEqual ("true", se.Attribute ("Unrestricted"), "Xml-Unrestricted");
95 Assert.IsNull (se.Children, "Xml-Children");
97 PrintingPermission copy = (PrintingPermission)pp.Copy ();
98 Assert.IsFalse (Object.ReferenceEquals (pp, copy), "ReferenceEquals");
99 Assert.AreEqual (pp.Level, copy.Level, "Level");
100 Assert.AreEqual (pp.IsUnrestricted (), copy.IsUnrestricted (), "IsUnrestricted ()");
104 [ExpectedException (typeof (ArgumentException))]
105 public void PermissionState_Bad ()
107 PermissionState ps = (PermissionState)77;
108 PrintingPermission pp = new PrintingPermission (ps);
112 [ExpectedException (typeof (ArgumentException))]
113 public void PrintingPermissionLevels_Bad ()
115 PrintingPermissionLevel ppl = (PrintingPermissionLevel)(PrintingPermissionLevel.AllPrinting + 1);
116 PrintingPermission pp = new PrintingPermission (ppl);
120 [ExpectedException (typeof (ArgumentException))]
121 public void Level_PrintingPermissionLevels_Bad ()
123 PrintingPermissionLevel ppl = (PrintingPermissionLevel)(PrintingPermissionLevel.AllPrinting + 1);
124 PrintingPermission pp = new PrintingPermission (PermissionState.None);
131 PrintingPermission pp = new PrintingPermission (PermissionState.None);
132 foreach (PrintingPermissionLevel ppl in AllLevel) {
134 PrintingPermission copy = (PrintingPermission)pp.Copy ();
135 Assert.AreEqual (ppl, copy.Level, ppl.ToString ());
140 public void Intersect_Null ()
142 PrintingPermission pp = new PrintingPermission (PermissionState.None);
143 // No intersection with null
144 foreach (PrintingPermissionLevel ppl in AllLevel) {
146 Assert.IsNull (pp.Intersect (null), ppl.ToString ());
151 public void Intersect_None ()
153 PrintingPermission sp1 = new PrintingPermission (PermissionState.None);
154 PrintingPermission sp2 = new PrintingPermission (PermissionState.None);
155 foreach (PrintingPermissionLevel ppl in AllLevelExceptNoLevel) {
157 // 1. Intersect None with ppl
158 PrintingPermission result = (PrintingPermission)sp1.Intersect (sp2);
159 Assert.IsNull (result, "None N " + ppl.ToString ());
160 // 2. Intersect ppl with None
161 result = (PrintingPermission)sp2.Intersect (sp1);
162 Assert.IsNull (result, "None N " + ppl.ToString ());
167 public void Intersect_Self ()
169 PrintingPermission pp = new PrintingPermission (PermissionState.None);
170 foreach (PrintingPermissionLevel ppl in AllLevelExceptNoLevel) {
172 PrintingPermission result = (PrintingPermission)pp.Intersect (pp);
173 Assert.AreEqual (ppl, result.Level, ppl.ToString ());
178 public void Intersect_Unrestricted ()
180 // Intersection with unrestricted == Copy
181 // a. source (this) is unrestricted
182 PrintingPermission sp1 = new PrintingPermission (PermissionState.Unrestricted);
183 PrintingPermission sp2 = new PrintingPermission (PermissionState.None);
184 foreach (PrintingPermissionLevel ppl in AllLevelExceptNoLevel) {
186 PrintingPermission result = (PrintingPermission)sp1.Intersect (sp2);
187 Assert.AreEqual (sp2.Level, result.Level, "target " + ppl.ToString ());
189 // b. destination (target) is unrestricted
190 foreach (PrintingPermissionLevel ppl in AllLevelExceptNoLevel) {
192 PrintingPermission result = (PrintingPermission)sp2.Intersect (sp1);
193 Assert.AreEqual (sp2.Level, result.Level, "source " + ppl.ToString ());
195 // exceptions for NoLevel
196 sp2.Level = PrintingPermissionLevel.NoPrinting;
197 Assert.IsNull (sp1.Intersect (sp2), "target NoLevel");
198 Assert.IsNull (sp2.Intersect (sp1), "source NoLevel");
202 public void IsSubset_Null ()
204 PrintingPermission pp = new PrintingPermission (PermissionState.None);
205 Assert.IsTrue (pp.IsSubsetOf (null), "NoLevel");
206 foreach (PrintingPermissionLevel ppl in AllLevelExceptNoLevel) {
208 Assert.IsFalse (pp.IsSubsetOf (null), ppl.ToString ());
213 public void IsSubset_None ()
215 // IsSubset with none
216 // a. source (this) is none -> target is never a subset
217 PrintingPermission sp1 = new PrintingPermission (PermissionState.None);
218 PrintingPermission sp2 = new PrintingPermission (PermissionState.None);
219 foreach (PrintingPermissionLevel ppl in AllLevel) {
221 Assert.IsTrue (sp1.IsSubsetOf (sp2), "target " + ppl.ToString ());
223 // b. destination (target) is none -> target is always a subset
224 foreach (PrintingPermissionLevel ppl in AllLevelExceptNoLevel) {
226 Assert.IsFalse (sp2.IsSubsetOf (sp1), "source " + ppl.ToString ());
228 // exception of NoLevel
229 sp2.Level = PrintingPermissionLevel.NoPrinting;
230 Assert.IsTrue (sp2.IsSubsetOf (sp1), "source NoLevel");
234 public void IsSubset_Self ()
236 PrintingPermission pp = new PrintingPermission (PermissionState.None);
237 foreach (PrintingPermissionLevel ppl in AllLevel) {
239 PrintingPermission result = (PrintingPermission)pp.Intersect (pp);
240 Assert.IsTrue (pp.IsSubsetOf (pp), ppl.ToString ());
245 public void IsSubset_Unrestricted ()
247 // IsSubset with unrestricted
248 // a. source (this) is unrestricted -> target is never a subset
249 PrintingPermission sp1 = new PrintingPermission (PermissionState.Unrestricted);
250 PrintingPermission sp2 = new PrintingPermission (PermissionState.None);
251 foreach (PrintingPermissionLevel ppl in AllLevelExceptAllLevel) {
253 Assert.IsFalse (sp1.IsSubsetOf (sp2), "target " + ppl.ToString ());
255 // exception of AllLevel
256 sp2.Level = PrintingPermissionLevel.AllPrinting;
257 Assert.IsTrue (sp1.IsSubsetOf (sp2), "target AllLevel");
258 // b. destination (target) is unrestricted -> target is always a subset
259 foreach (PrintingPermissionLevel ppl in AllLevel) {
261 Assert.IsTrue (sp2.IsSubsetOf (sp1), "source " + ppl.ToString ());
266 public void Union_Null ()
268 PrintingPermission pp = new PrintingPermission (PermissionState.None);
269 // Union with null is a simple copy
270 foreach (PrintingPermissionLevel ppl in AllLevel) {
272 PrintingPermission union = (PrintingPermission)pp.Union (null);
273 Assert.AreEqual (ppl, union.Level, ppl.ToString ());
278 public void Union_None ()
280 // Union with none is same
281 PrintingPermission pp1 = new PrintingPermission (PermissionState.None);
282 PrintingPermission pp2 = new PrintingPermission (PermissionState.None);
283 PrintingPermission union = null;
285 // a. source (this) is none
286 pp2.Level = PrintingPermissionLevel.NoPrinting;
287 union = (PrintingPermission)pp1.Union (pp2);
288 Assert.IsNull (union, "target NoPrinting");
289 foreach (PrintingPermissionLevel ppl in AllLevelExceptNoAndAllLevel) {
291 union = (PrintingPermission)pp1.Union (pp2);
292 Assert.IsFalse (union.IsUnrestricted (), "target " + ppl.ToString ());
294 pp2.Level = PrintingPermissionLevel.AllPrinting;
295 union = (PrintingPermission)pp1.Union (pp2);
296 Assert.IsTrue (union.IsUnrestricted (), "target AllPrinting");
298 // b. destination (target) is none
299 pp2.Level = PrintingPermissionLevel.NoPrinting;
300 union = (PrintingPermission)pp2.Union (pp1);
301 Assert.IsNull (union, "source NoPrinting");
302 foreach (PrintingPermissionLevel ppl in AllLevelExceptNoAndAllLevel) {
304 union = (PrintingPermission)pp2.Union (pp1);
305 Assert.IsFalse (union.IsUnrestricted (), "source " + ppl.ToString ());
307 pp2.Level = PrintingPermissionLevel.AllPrinting;
308 union = (PrintingPermission)pp2.Union (pp1);
309 Assert.IsTrue (union.IsUnrestricted (), "source AllPrinting");
313 public void Union_Self ()
315 PrintingPermission pp = new PrintingPermission (PermissionState.None);
316 foreach (PrintingPermissionLevel ppl in AllLevelExceptNoLevel) {
318 PrintingPermission result = (PrintingPermission)pp.Union (pp);
319 Assert.AreEqual (ppl, result.Level, ppl.ToString ());
321 // union of NoPrinting with NoPrinting == null
322 pp.Level = PrintingPermissionLevel.NoPrinting;
323 Assert.IsNull (pp.Union (pp), "NoPrinting");
327 public void Union_Unrestricted ()
329 // Union with unrestricted is unrestricted
330 PrintingPermission sp1 = new PrintingPermission (PermissionState.Unrestricted);
331 PrintingPermission sp2 = new PrintingPermission (PermissionState.None);
332 // a. source (this) is unrestricted
333 foreach (PrintingPermissionLevel ppl in AllLevel) {
335 PrintingPermission union = (PrintingPermission)sp1.Union (sp2);
336 Assert.IsTrue (union.IsUnrestricted (), "target " + ppl.ToString ());
338 // b. destination (target) is unrestricted
339 foreach (PrintingPermissionLevel ppl in AllLevel) {
341 PrintingPermission union = (PrintingPermission)sp2.Union (sp1);
342 Assert.IsTrue (union.IsUnrestricted (), "source " + ppl.ToString ());
347 [ExpectedException (typeof (ArgumentNullException))]
348 public void FromXml_Null ()
350 PrintingPermission pp = new PrintingPermission (PermissionState.None);
355 public void FromXml_WrongTag ()
357 PrintingPermission pp = new PrintingPermission (PermissionState.None);
358 SecurityElement se = pp.ToXml ();
361 // note: normally IPermission classes (in corlib) DO care about the
366 public void FromXml_WrongTagCase ()
368 PrintingPermission pp = new PrintingPermission (PermissionState.None);
369 SecurityElement se = pp.ToXml ();
370 se.Tag = "IPERMISSION"; // instead of IPermission
372 // note: normally IPermission classes (in corlib) DO care about the
378 [ExpectedException (typeof (ArgumentException))]
380 public void FromXml_WrongClass ()
382 PrintingPermission pp = new PrintingPermission (PermissionState.None);
383 SecurityElement se = pp.ToXml ();
385 SecurityElement w = new SecurityElement (se.Tag);
386 w.AddAttribute ("class", "Wrong" + se.Attribute ("class"));
387 w.AddAttribute ("version", se.Attribute ("version"));
389 // doesn't care of the class name at that stage
390 // anyway the class has already be created so...
394 [ExpectedException (typeof (ArgumentException))]
395 public void FromXml_NoClass ()
397 PrintingPermission pp = new PrintingPermission (PermissionState.None);
398 SecurityElement se = pp.ToXml ();
400 SecurityElement w = new SecurityElement (se.Tag);
401 w.AddAttribute ("version", se.Attribute ("version"));
403 // note: normally IPermission classes (in corlib) DO NOT care about
404 // attribute "class" name presence in the XML
408 [ExpectedException (typeof (ArgumentException))]
409 public void FromXml_WrongVersion ()
411 PrintingPermission pp = new PrintingPermission (PermissionState.None);
412 SecurityElement se = pp.ToXml ();
413 se.Attributes.Remove ("version");
414 se.Attributes.Add ("version", "2");
420 [ExpectedException (typeof (ArgumentException))]
422 public void FromXml_NoVersion ()
424 PrintingPermission pp = new PrintingPermission (PermissionState.None);
425 SecurityElement se = pp.ToXml ();
427 SecurityElement w = new SecurityElement (se.Tag);
428 w.AddAttribute ("class", se.Attribute ("class"));
432 // Unification tests (with the MS final key)
433 // note: corlib already test the ECMA key support for unification
434 private const string PermissionPattern = "<PermissionSet class=\"System.Security.PermissionSet\" version=\"1\"><IPermission class=\"System.Drawing.Printing.PrintingPermission, System.Drawing, Version={0}, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a\" version=\"1\" Level=\"NoPrinting\"/></PermissionSet>";
435 private const string fx10version = "1.0.3300.0";
436 private const string fx11version = "1.0.5000.0";
437 private const string fx20version = "2.0.0.0";
439 private void Unification (string xml)
441 PermissionSetAttribute psa = new PermissionSetAttribute (SecurityAction.Assert);
443 string pset = psa.CreatePermissionSet ().ToString ();
444 string currentVersion = typeof (string).Assembly.GetName ().Version.ToString ();
445 Assert.IsTrue ((pset.IndexOf (currentVersion) > 0), currentVersion);
449 public void Unification_FromFx10 ()
451 Unification (String.Format (PermissionPattern, fx10version));
455 public void Unification_FromFx11 ()
457 Unification (String.Format (PermissionPattern, fx11version));
461 public void Unification_FromFx20 ()
463 Unification (String.Format (PermissionPattern, fx20version));
468 [Category ("NotWorking")]
469 [ExpectedException (typeof (FileLoadException))]
470 public void Unification_FromFx99 ()
472 Type.GetType (String.Format (PermissionPattern, "9.99.999.9999"));
476 public void Unification_FromFx99 ()
478 Unification (String.Format (PermissionPattern, "9.99.999.9999"));