backport this one too
[mono.git] / mcs / class / System.Drawing / Test / System.Drawing.Printing / PrintingPermissionTest.cs
1 //
2 // PrintingPermissionTest.cs - NUnit Test Cases for PrintingPermission
3 //
4 // Author:
5 //      Sebastien Pouliot  <sebastien@ximian.com>
6 //
7 // Copyright (C) 2004-2005 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 using NUnit.Framework;
30 using System;
31 using System.IO;
32 using System.Drawing.Printing;
33 using System.Security;
34 using System.Security.Permissions;
35
36 using System.Diagnostics;
37
38 namespace MonoTests.System.Drawing.Printing {
39
40         [TestFixture]
41         public class PrintingPermissionTest {
42
43                 static PrintingPermissionLevel[] AllLevel = {
44                         PrintingPermissionLevel.NoPrinting,
45                         PrintingPermissionLevel.SafePrinting,
46                         PrintingPermissionLevel.DefaultPrinting,
47                         PrintingPermissionLevel.AllPrinting,
48                 };
49
50                 static PrintingPermissionLevel[] AllLevelExceptNoLevel = {
51                         PrintingPermissionLevel.SafePrinting,
52                         PrintingPermissionLevel.DefaultPrinting,
53                         PrintingPermissionLevel.AllPrinting,
54                 };
55
56                 static PrintingPermissionLevel[] AllLevelExceptAllLevel = {
57                         PrintingPermissionLevel.NoPrinting,
58                         PrintingPermissionLevel.SafePrinting,
59                         PrintingPermissionLevel.DefaultPrinting,
60                 };
61
62                 static PrintingPermissionLevel[] AllLevelExceptNoAndAllLevel = {
63                         PrintingPermissionLevel.SafePrinting,
64                         PrintingPermissionLevel.DefaultPrinting,
65                 };
66
67                 [Test]
68                 public void PermissionState_None ()
69                 {
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");
74
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");
79
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 ()");
84                 }
85
86                 [Test]
87                 public void PermissionState_Unrestricted ()
88                 {
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");
93
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");
98
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 ()");
103                 }
104
105                 [Test]
106                 [ExpectedException (typeof (ArgumentException))]
107                 public void PermissionState_Bad ()
108                 {
109                         PermissionState ps = (PermissionState)77;
110                         PrintingPermission pp = new PrintingPermission (ps);
111                 }
112
113                 [Test]
114                 [ExpectedException (typeof (ArgumentException))]
115                 public void PrintingPermissionLevels_Bad ()
116                 {
117                         PrintingPermissionLevel ppl = (PrintingPermissionLevel)(PrintingPermissionLevel.AllPrinting + 1);
118                         PrintingPermission pp = new PrintingPermission (ppl);
119                 }
120
121                 [Test]
122                 [ExpectedException (typeof (ArgumentException))]
123                 public void Level_PrintingPermissionLevels_Bad ()
124                 {
125                         PrintingPermissionLevel ppl = (PrintingPermissionLevel)(PrintingPermissionLevel.AllPrinting + 1);
126                         PrintingPermission pp = new PrintingPermission (PermissionState.None);
127                         pp.Level = ppl;
128                 }
129
130                 [Test]
131                 public void Copy ()
132                 {
133                         PrintingPermission pp = new PrintingPermission (PermissionState.None);
134                         foreach (PrintingPermissionLevel ppl in AllLevel) {
135                                 pp.Level = ppl;
136                                 PrintingPermission copy = (PrintingPermission)pp.Copy ();
137                                 Assert.AreEqual (ppl, copy.Level, ppl.ToString ());
138                         }
139                 }
140
141                 [Test]
142                 public void Intersect_Null ()
143                 {
144                         PrintingPermission pp = new PrintingPermission (PermissionState.None);
145                         // No intersection with null
146                         foreach (PrintingPermissionLevel ppl in AllLevel) {
147                                 pp.Level = ppl;
148                                 Assert.IsNull (pp.Intersect (null), ppl.ToString ());
149                         }
150                 }
151
152                 [Test]
153                 public void Intersect_None ()
154                 {
155                         PrintingPermission sp1 = new PrintingPermission (PermissionState.None);
156                         PrintingPermission sp2 = new PrintingPermission (PermissionState.None);
157                         foreach (PrintingPermissionLevel ppl in AllLevelExceptNoLevel) {
158                                 sp2.Level = ppl;
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 ());
165                         }
166                 }
167
168                 [Test]
169                 public void Intersect_Self ()
170                 {
171                         PrintingPermission pp = new PrintingPermission (PermissionState.None);
172                         foreach (PrintingPermissionLevel ppl in AllLevelExceptNoLevel) {
173                                 pp.Level = ppl;
174                                 PrintingPermission result = (PrintingPermission)pp.Intersect (pp);
175                                 Assert.AreEqual (ppl, result.Level, ppl.ToString ());
176                         }
177                 }
178
179                 [Test]
180                 public void Intersect_Unrestricted ()
181                 {
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) {
187                                 sp2.Level = ppl;
188                                 PrintingPermission result = (PrintingPermission)sp1.Intersect (sp2);
189                                 Assert.AreEqual (sp2.Level, result.Level, "target " + ppl.ToString ());
190                         }
191                         // b. destination (target) is unrestricted
192                         foreach (PrintingPermissionLevel ppl in AllLevelExceptNoLevel) {
193                                 sp2.Level = ppl;
194                                 PrintingPermission result = (PrintingPermission)sp2.Intersect (sp1);
195                                 Assert.AreEqual (sp2.Level, result.Level, "source " + ppl.ToString ());
196                         }
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");
201                 }
202
203                 [Test]
204                 public void IsSubset_Null ()
205                 {
206                         PrintingPermission pp = new PrintingPermission (PermissionState.None);
207                         Assert.IsTrue (pp.IsSubsetOf (null), "NoLevel");
208                         foreach (PrintingPermissionLevel ppl in AllLevelExceptNoLevel) {
209                                 pp.Level = ppl;
210                                 Assert.IsFalse (pp.IsSubsetOf (null), ppl.ToString ());
211                         }
212                 }
213
214                 [Test]
215                 public void IsSubset_None ()
216                 {
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) {
222                                 sp2.Level = ppl;
223                                 Assert.IsTrue (sp1.IsSubsetOf (sp2), "target " + ppl.ToString ());
224                         }
225                         // b. destination (target) is none -> target is always a subset
226                         foreach (PrintingPermissionLevel ppl in AllLevelExceptNoLevel) {
227                                 sp2.Level = ppl;
228                                 Assert.IsFalse (sp2.IsSubsetOf (sp1), "source " + ppl.ToString ());
229                         }
230                         // exception of NoLevel
231                         sp2.Level = PrintingPermissionLevel.NoPrinting;
232                         Assert.IsTrue (sp2.IsSubsetOf (sp1), "source NoLevel");
233                 }
234
235                 [Test]
236                 public void IsSubset_Self ()
237                 {
238                         PrintingPermission pp = new PrintingPermission (PermissionState.None);
239                         foreach (PrintingPermissionLevel ppl in AllLevel) {
240                                 pp.Level = ppl;
241                                 PrintingPermission result = (PrintingPermission)pp.Intersect (pp);
242                                 Assert.IsTrue (pp.IsSubsetOf (pp), ppl.ToString ());
243                         }
244                 }
245
246                 [Test]
247                 public void IsSubset_Unrestricted ()
248                 {
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) {
254                                 sp2.Level = ppl;
255                                 Assert.IsFalse (sp1.IsSubsetOf (sp2), "target " + ppl.ToString ());
256                         }
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) {
262                                 sp2.Level = ppl;
263                                 Assert.IsTrue (sp2.IsSubsetOf (sp1), "source " + ppl.ToString ());
264                         }
265                 }
266
267                 [Test]
268                 public void Union_Null ()
269                 {
270                         PrintingPermission pp = new PrintingPermission (PermissionState.None);
271                         // Union with null is a simple copy
272                         foreach (PrintingPermissionLevel ppl in AllLevel) {
273                                 pp.Level = ppl;
274                                 PrintingPermission union = (PrintingPermission)pp.Union (null);
275                                 Assert.AreEqual (ppl, union.Level, ppl.ToString ());
276                         }
277                 }
278
279                 [Test]
280                 public void Union_None ()
281                 {
282                         // Union with none is same
283                         PrintingPermission pp1 = new PrintingPermission (PermissionState.None);
284                         PrintingPermission pp2 = new PrintingPermission (PermissionState.None);
285                         PrintingPermission union = null;
286
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) {
292                                 pp2.Level = ppl;
293                                 union = (PrintingPermission)pp1.Union (pp2);
294                                 Assert.IsFalse (union.IsUnrestricted (), "target " + ppl.ToString ());
295                         }
296                         pp2.Level = PrintingPermissionLevel.AllPrinting;
297                         union = (PrintingPermission)pp1.Union (pp2);
298                         Assert.IsTrue (union.IsUnrestricted (), "target AllPrinting");
299
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) {
305                                 pp2.Level = ppl;
306                                 union = (PrintingPermission)pp2.Union (pp1);
307                                 Assert.IsFalse (union.IsUnrestricted (), "source " + ppl.ToString ());
308                         }
309                         pp2.Level = PrintingPermissionLevel.AllPrinting;
310                         union = (PrintingPermission)pp2.Union (pp1);
311                         Assert.IsTrue (union.IsUnrestricted (), "source AllPrinting");
312                 }
313
314                 [Test]
315                 public void Union_Self ()
316                 {
317                         PrintingPermission pp = new PrintingPermission (PermissionState.None);
318                         foreach (PrintingPermissionLevel ppl in AllLevelExceptNoLevel) {
319                                 pp.Level = ppl;
320                                 PrintingPermission result = (PrintingPermission)pp.Union (pp);
321                                 Assert.AreEqual (ppl, result.Level, ppl.ToString ());
322                         }
323                         // union of NoPrinting with NoPrinting == null
324                         pp.Level = PrintingPermissionLevel.NoPrinting;
325                         Assert.IsNull (pp.Union (pp), "NoPrinting");
326                 }
327
328                 [Test]
329                 public void Union_Unrestricted ()
330                 {
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) {
336                                 sp2.Level = ppl;
337                                 PrintingPermission union = (PrintingPermission)sp1.Union (sp2);
338                                 Assert.IsTrue (union.IsUnrestricted (), "target " + ppl.ToString ());
339                         }
340                         // b. destination (target) is unrestricted
341                         foreach (PrintingPermissionLevel ppl in AllLevel) {
342                                 sp2.Level = ppl;
343                                 PrintingPermission union = (PrintingPermission)sp2.Union (sp1);
344                                 Assert.IsTrue (union.IsUnrestricted (), "source " + ppl.ToString ());
345                         }
346                 }
347
348                 [Test]
349                 [ExpectedException (typeof (ArgumentNullException))]
350                 public void FromXml_Null ()
351                 {
352                         PrintingPermission pp = new PrintingPermission (PermissionState.None);
353                         pp.FromXml (null);
354                 }
355
356                 [Test]
357                 public void FromXml_WrongTag ()
358                 {
359                         PrintingPermission pp = new PrintingPermission (PermissionState.None);
360                         SecurityElement se = pp.ToXml ();
361                         se.Tag = "IMono";
362                         pp.FromXml (se);
363                         // note: normally IPermission classes (in corlib) DO care about the
364                         // IPermission tag
365                 }
366
367                 [Test]
368                 public void FromXml_WrongTagCase ()
369                 {
370                         PrintingPermission pp = new PrintingPermission (PermissionState.None);
371                         SecurityElement se = pp.ToXml ();
372                         se.Tag = "IPERMISSION"; // instead of IPermission
373                         pp.FromXml (se);
374                         // note: normally IPermission classes (in corlib) DO care about the
375                         // IPermission tag
376                 }
377
378                 [Test]
379                 public void FromXml_WrongClass ()
380                 {
381                         PrintingPermission pp = new PrintingPermission (PermissionState.None);
382                         SecurityElement se = pp.ToXml ();
383
384                         SecurityElement w = new SecurityElement (se.Tag);
385                         w.AddAttribute ("class", "Wrong" + se.Attribute ("class"));
386                         w.AddAttribute ("version", se.Attribute ("version"));
387                         pp.FromXml (w);
388                         // doesn't care of the class name at that stage
389                         // anyway the class has already be created so...
390                 }
391
392                 [Test]
393                 [ExpectedException (typeof (ArgumentException))]
394                 public void FromXml_NoClass ()
395                 {
396                         PrintingPermission pp = new PrintingPermission (PermissionState.None);
397                         SecurityElement se = pp.ToXml ();
398
399                         SecurityElement w = new SecurityElement (se.Tag);
400                         w.AddAttribute ("version", se.Attribute ("version"));
401                         pp.FromXml (w);
402                         // note: normally IPermission classes (in corlib) DO NOT care about
403                         // attribute "class" name presence in the XML
404                 }
405
406                 [Test]
407                 [ExpectedException (typeof (ArgumentException))]
408                 public void FromXml_WrongVersion ()
409                 {
410                         PrintingPermission pp = new PrintingPermission (PermissionState.None);
411                         SecurityElement se = pp.ToXml ();
412                         se.Attributes.Remove ("version");
413                         se.Attributes.Add ("version", "2");
414                         pp.FromXml (se);
415                 }
416
417                 [Test]
418                 public void FromXml_NoVersion ()
419                 {
420                         PrintingPermission pp = new PrintingPermission (PermissionState.None);
421                         SecurityElement se = pp.ToXml ();
422
423                         SecurityElement w = new SecurityElement (se.Tag);
424                         w.AddAttribute ("class", se.Attribute ("class"));
425                         pp.FromXml (w);
426                 }
427
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";
434
435                 private void Unification (string xml)
436                 {
437                         PermissionSetAttribute psa = new PermissionSetAttribute (SecurityAction.Assert);
438                         psa.XML = xml;
439                         string pset = psa.CreatePermissionSet ().ToString ();
440                         string currentVersion = typeof (string).Assembly.GetName ().Version.ToString ();
441                         Assert.IsTrue ((pset.IndexOf (currentVersion) > 0), currentVersion);
442                 }
443
444                 [Test]
445                 public void Unification_FromFx10 ()
446                 {
447                         Unification (String.Format (PermissionPattern, fx10version));
448                 }
449
450                 [Test]
451                 public void Unification_FromFx11 ()
452                 {
453                         Unification (String.Format (PermissionPattern, fx11version));
454                 }
455
456                 [Test]
457                 public void Unification_FromFx20 ()
458                 {
459                         Unification (String.Format (PermissionPattern, fx20version));
460                 }
461
462 #if NET_2_0
463                 [Test]
464                 [Category ("NotWorking")]
465                 [ExpectedException (typeof (FileLoadException))]
466                 public void Unification_FromFx99 ()
467                 {
468                         Type.GetType (String.Format (PermissionPattern, "9.99.999.9999"));
469                 }
470 #else
471                 [Test]
472                 public void Unification_FromFx99 ()
473                 {
474                         Unification (String.Format (PermissionPattern, "9.99.999.9999"));
475                 }
476 #endif
477         }
478 }