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 using System.Diagnostics;
38 namespace MonoTests.System.Drawing.Printing {
41 public class PrintingPermissionTest {
43 static PrintingPermissionLevel[] AllLevel = {
44 PrintingPermissionLevel.NoPrinting,
45 PrintingPermissionLevel.SafePrinting,
46 PrintingPermissionLevel.DefaultPrinting,
47 PrintingPermissionLevel.AllPrinting,
50 static PrintingPermissionLevel[] AllLevelExceptNoLevel = {
51 PrintingPermissionLevel.SafePrinting,
52 PrintingPermissionLevel.DefaultPrinting,
53 PrintingPermissionLevel.AllPrinting,
56 static PrintingPermissionLevel[] AllLevelExceptAllLevel = {
57 PrintingPermissionLevel.NoPrinting,
58 PrintingPermissionLevel.SafePrinting,
59 PrintingPermissionLevel.DefaultPrinting,
62 static PrintingPermissionLevel[] AllLevelExceptNoAndAllLevel = {
63 PrintingPermissionLevel.SafePrinting,
64 PrintingPermissionLevel.DefaultPrinting,
68 public void PermissionState_None ()
70 PermissionState ps = PermissionState.None;
71 PrintingPermission pp = new PrintingPermission (ps);
72 Assert.AreEqual (PrintingPermissionLevel.NoPrinting, pp.Level, "Level");
73 Assert.IsFalse (pp.IsUnrestricted (), "IsUnrestricted");
75 SecurityElement se = pp.ToXml ();
76 // only class and version are present
77 Assert.AreEqual ("NoPrinting", se.Attribute ("Level"), "Xml-Level");
78 Assert.IsNull (se.Children, "Xml-Children");
80 PrintingPermission copy = (PrintingPermission)pp.Copy ();
81 Assert.IsFalse (Object.ReferenceEquals (pp, copy), "ReferenceEquals");
82 Assert.AreEqual (pp.Level, copy.Level, "Level");
83 Assert.AreEqual (pp.IsUnrestricted (), copy.IsUnrestricted (), "IsUnrestricted ()");
87 public void PermissionState_Unrestricted ()
89 PermissionState ps = PermissionState.Unrestricted;
90 PrintingPermission pp = new PrintingPermission (ps);
91 Assert.AreEqual (PrintingPermissionLevel.AllPrinting, pp.Level, "Level");
92 Assert.IsTrue (pp.IsUnrestricted (), "IsUnrestricted");
94 SecurityElement se = pp.ToXml ();
95 // only class and version are present
96 Assert.AreEqual ("true", se.Attribute ("Unrestricted"), "Xml-Unrestricted");
97 Assert.IsNull (se.Children, "Xml-Children");
99 PrintingPermission copy = (PrintingPermission)pp.Copy ();
100 Assert.IsFalse (Object.ReferenceEquals (pp, copy), "ReferenceEquals");
101 Assert.AreEqual (pp.Level, copy.Level, "Level");
102 Assert.AreEqual (pp.IsUnrestricted (), copy.IsUnrestricted (), "IsUnrestricted ()");
106 [ExpectedException (typeof (ArgumentException))]
107 public void PermissionState_Bad ()
109 PermissionState ps = (PermissionState)77;
110 PrintingPermission pp = new PrintingPermission (ps);
114 [ExpectedException (typeof (ArgumentException))]
115 public void PrintingPermissionLevels_Bad ()
117 PrintingPermissionLevel ppl = (PrintingPermissionLevel)(PrintingPermissionLevel.AllPrinting + 1);
118 PrintingPermission pp = new PrintingPermission (ppl);
122 [ExpectedException (typeof (ArgumentException))]
123 public void Level_PrintingPermissionLevels_Bad ()
125 PrintingPermissionLevel ppl = (PrintingPermissionLevel)(PrintingPermissionLevel.AllPrinting + 1);
126 PrintingPermission pp = new PrintingPermission (PermissionState.None);
133 PrintingPermission pp = new PrintingPermission (PermissionState.None);
134 foreach (PrintingPermissionLevel ppl in AllLevel) {
136 PrintingPermission copy = (PrintingPermission)pp.Copy ();
137 Assert.AreEqual (ppl, copy.Level, ppl.ToString ());
142 public void Intersect_Null ()
144 PrintingPermission pp = new PrintingPermission (PermissionState.None);
145 // No intersection with null
146 foreach (PrintingPermissionLevel ppl in AllLevel) {
148 Assert.IsNull (pp.Intersect (null), ppl.ToString ());
153 public void Intersect_None ()
155 PrintingPermission sp1 = new PrintingPermission (PermissionState.None);
156 PrintingPermission sp2 = new PrintingPermission (PermissionState.None);
157 foreach (PrintingPermissionLevel ppl in AllLevelExceptNoLevel) {
159 // 1. Intersect None with ppl
160 PrintingPermission result = (PrintingPermission)sp1.Intersect (sp2);
161 Assert.IsNull (result, "None N " + ppl.ToString ());
162 // 2. Intersect ppl with None
163 result = (PrintingPermission)sp2.Intersect (sp1);
164 Assert.IsNull (result, "None N " + ppl.ToString ());
169 public void Intersect_Self ()
171 PrintingPermission pp = new PrintingPermission (PermissionState.None);
172 foreach (PrintingPermissionLevel ppl in AllLevelExceptNoLevel) {
174 PrintingPermission result = (PrintingPermission)pp.Intersect (pp);
175 Assert.AreEqual (ppl, result.Level, ppl.ToString ());
180 public void Intersect_Unrestricted ()
182 // Intersection with unrestricted == Copy
183 // a. source (this) is unrestricted
184 PrintingPermission sp1 = new PrintingPermission (PermissionState.Unrestricted);
185 PrintingPermission sp2 = new PrintingPermission (PermissionState.None);
186 foreach (PrintingPermissionLevel ppl in AllLevelExceptNoLevel) {
188 PrintingPermission result = (PrintingPermission)sp1.Intersect (sp2);
189 Assert.AreEqual (sp2.Level, result.Level, "target " + ppl.ToString ());
191 // b. destination (target) is unrestricted
192 foreach (PrintingPermissionLevel ppl in AllLevelExceptNoLevel) {
194 PrintingPermission result = (PrintingPermission)sp2.Intersect (sp1);
195 Assert.AreEqual (sp2.Level, result.Level, "source " + ppl.ToString ());
197 // exceptions for NoLevel
198 sp2.Level = PrintingPermissionLevel.NoPrinting;
199 Assert.IsNull (sp1.Intersect (sp2), "target NoLevel");
200 Assert.IsNull (sp2.Intersect (sp1), "source NoLevel");
204 public void IsSubset_Null ()
206 PrintingPermission pp = new PrintingPermission (PermissionState.None);
207 Assert.IsTrue (pp.IsSubsetOf (null), "NoLevel");
208 foreach (PrintingPermissionLevel ppl in AllLevelExceptNoLevel) {
210 Assert.IsFalse (pp.IsSubsetOf (null), ppl.ToString ());
215 public void IsSubset_None ()
217 // IsSubset with none
218 // a. source (this) is none -> target is never a subset
219 PrintingPermission sp1 = new PrintingPermission (PermissionState.None);
220 PrintingPermission sp2 = new PrintingPermission (PermissionState.None);
221 foreach (PrintingPermissionLevel ppl in AllLevel) {
223 Assert.IsTrue (sp1.IsSubsetOf (sp2), "target " + ppl.ToString ());
225 // b. destination (target) is none -> target is always a subset
226 foreach (PrintingPermissionLevel ppl in AllLevelExceptNoLevel) {
228 Assert.IsFalse (sp2.IsSubsetOf (sp1), "source " + ppl.ToString ());
230 // exception of NoLevel
231 sp2.Level = PrintingPermissionLevel.NoPrinting;
232 Assert.IsTrue (sp2.IsSubsetOf (sp1), "source NoLevel");
236 public void IsSubset_Self ()
238 PrintingPermission pp = new PrintingPermission (PermissionState.None);
239 foreach (PrintingPermissionLevel ppl in AllLevel) {
241 PrintingPermission result = (PrintingPermission)pp.Intersect (pp);
242 Assert.IsTrue (pp.IsSubsetOf (pp), ppl.ToString ());
247 public void IsSubset_Unrestricted ()
249 // IsSubset with unrestricted
250 // a. source (this) is unrestricted -> target is never a subset
251 PrintingPermission sp1 = new PrintingPermission (PermissionState.Unrestricted);
252 PrintingPermission sp2 = new PrintingPermission (PermissionState.None);
253 foreach (PrintingPermissionLevel ppl in AllLevelExceptAllLevel) {
255 Assert.IsFalse (sp1.IsSubsetOf (sp2), "target " + ppl.ToString ());
257 // exception of AllLevel
258 sp2.Level = PrintingPermissionLevel.AllPrinting;
259 Assert.IsTrue (sp1.IsSubsetOf (sp2), "target AllLevel");
260 // b. destination (target) is unrestricted -> target is always a subset
261 foreach (PrintingPermissionLevel ppl in AllLevel) {
263 Assert.IsTrue (sp2.IsSubsetOf (sp1), "source " + ppl.ToString ());
268 public void Union_Null ()
270 PrintingPermission pp = new PrintingPermission (PermissionState.None);
271 // Union with null is a simple copy
272 foreach (PrintingPermissionLevel ppl in AllLevel) {
274 PrintingPermission union = (PrintingPermission)pp.Union (null);
275 Assert.AreEqual (ppl, union.Level, ppl.ToString ());
280 public void Union_None ()
282 // Union with none is same
283 PrintingPermission pp1 = new PrintingPermission (PermissionState.None);
284 PrintingPermission pp2 = new PrintingPermission (PermissionState.None);
285 PrintingPermission union = null;
287 // a. source (this) is none
288 pp2.Level = PrintingPermissionLevel.NoPrinting;
289 union = (PrintingPermission)pp1.Union (pp2);
290 Assert.IsNull (union, "target NoPrinting");
291 foreach (PrintingPermissionLevel ppl in AllLevelExceptNoAndAllLevel) {
293 union = (PrintingPermission)pp1.Union (pp2);
294 Assert.IsFalse (union.IsUnrestricted (), "target " + ppl.ToString ());
296 pp2.Level = PrintingPermissionLevel.AllPrinting;
297 union = (PrintingPermission)pp1.Union (pp2);
298 Assert.IsTrue (union.IsUnrestricted (), "target AllPrinting");
300 // b. destination (target) is none
301 pp2.Level = PrintingPermissionLevel.NoPrinting;
302 union = (PrintingPermission)pp2.Union (pp1);
303 Assert.IsNull (union, "source NoPrinting");
304 foreach (PrintingPermissionLevel ppl in AllLevelExceptNoAndAllLevel) {
306 union = (PrintingPermission)pp2.Union (pp1);
307 Assert.IsFalse (union.IsUnrestricted (), "source " + ppl.ToString ());
309 pp2.Level = PrintingPermissionLevel.AllPrinting;
310 union = (PrintingPermission)pp2.Union (pp1);
311 Assert.IsTrue (union.IsUnrestricted (), "source AllPrinting");
315 public void Union_Self ()
317 PrintingPermission pp = new PrintingPermission (PermissionState.None);
318 foreach (PrintingPermissionLevel ppl in AllLevelExceptNoLevel) {
320 PrintingPermission result = (PrintingPermission)pp.Union (pp);
321 Assert.AreEqual (ppl, result.Level, ppl.ToString ());
323 // union of NoPrinting with NoPrinting == null
324 pp.Level = PrintingPermissionLevel.NoPrinting;
325 Assert.IsNull (pp.Union (pp), "NoPrinting");
329 public void Union_Unrestricted ()
331 // Union with unrestricted is unrestricted
332 PrintingPermission sp1 = new PrintingPermission (PermissionState.Unrestricted);
333 PrintingPermission sp2 = new PrintingPermission (PermissionState.None);
334 // a. source (this) is unrestricted
335 foreach (PrintingPermissionLevel ppl in AllLevel) {
337 PrintingPermission union = (PrintingPermission)sp1.Union (sp2);
338 Assert.IsTrue (union.IsUnrestricted (), "target " + ppl.ToString ());
340 // b. destination (target) is unrestricted
341 foreach (PrintingPermissionLevel ppl in AllLevel) {
343 PrintingPermission union = (PrintingPermission)sp2.Union (sp1);
344 Assert.IsTrue (union.IsUnrestricted (), "source " + ppl.ToString ());
349 [ExpectedException (typeof (ArgumentNullException))]
350 public void FromXml_Null ()
352 PrintingPermission pp = new PrintingPermission (PermissionState.None);
357 public void FromXml_WrongTag ()
359 PrintingPermission pp = new PrintingPermission (PermissionState.None);
360 SecurityElement se = pp.ToXml ();
363 // note: normally IPermission classes (in corlib) DO care about the
368 public void FromXml_WrongTagCase ()
370 PrintingPermission pp = new PrintingPermission (PermissionState.None);
371 SecurityElement se = pp.ToXml ();
372 se.Tag = "IPERMISSION"; // instead of IPermission
374 // note: normally IPermission classes (in corlib) DO care about the
379 public void FromXml_WrongClass ()
381 PrintingPermission pp = new PrintingPermission (PermissionState.None);
382 SecurityElement se = pp.ToXml ();
384 SecurityElement w = new SecurityElement (se.Tag);
385 w.AddAttribute ("class", "Wrong" + se.Attribute ("class"));
386 w.AddAttribute ("version", se.Attribute ("version"));
388 // doesn't care of the class name at that stage
389 // anyway the class has already be created so...
393 [ExpectedException (typeof (ArgumentException))]
394 public void FromXml_NoClass ()
396 PrintingPermission pp = new PrintingPermission (PermissionState.None);
397 SecurityElement se = pp.ToXml ();
399 SecurityElement w = new SecurityElement (se.Tag);
400 w.AddAttribute ("version", se.Attribute ("version"));
402 // note: normally IPermission classes (in corlib) DO NOT care about
403 // attribute "class" name presence in the XML
407 [ExpectedException (typeof (ArgumentException))]
408 public void FromXml_WrongVersion ()
410 PrintingPermission pp = new PrintingPermission (PermissionState.None);
411 SecurityElement se = pp.ToXml ();
412 se.Attributes.Remove ("version");
413 se.Attributes.Add ("version", "2");
418 public void FromXml_NoVersion ()
420 PrintingPermission pp = new PrintingPermission (PermissionState.None);
421 SecurityElement se = pp.ToXml ();
423 SecurityElement w = new SecurityElement (se.Tag);
424 w.AddAttribute ("class", se.Attribute ("class"));
428 // Unification tests (with the MS final key)
429 // note: corlib already test the ECMA key support for unification
430 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>";
431 private const string fx10version = "1.0.3300.0";
432 private const string fx11version = "1.0.5000.0";
433 private const string fx20version = "2.0.0.0";
435 private void Unification (string xml)
437 PermissionSetAttribute psa = new PermissionSetAttribute (SecurityAction.Assert);
439 string pset = psa.CreatePermissionSet ().ToString ();
440 string currentVersion = typeof (string).Assembly.GetName ().Version.ToString ();
441 Assert.IsTrue ((pset.IndexOf (currentVersion) > 0), currentVersion);
445 public void Unification_FromFx10 ()
447 Unification (String.Format (PermissionPattern, fx10version));
451 public void Unification_FromFx11 ()
453 Unification (String.Format (PermissionPattern, fx11version));
457 public void Unification_FromFx20 ()
459 Unification (String.Format (PermissionPattern, fx20version));
464 [Category ("NotWorking")]
465 [ExpectedException (typeof (FileLoadException))]
466 public void Unification_FromFx99 ()
468 Type.GetType (String.Format (PermissionPattern, "9.99.999.9999"));
472 public void Unification_FromFx99 ()
474 Unification (String.Format (PermissionPattern, "9.99.999.9999"));