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