PrintingPermissionTest.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482
  1. //
  2. // PrintingPermissionTest.cs - NUnit Test Cases for PrintingPermission
  3. //
  4. // Author:
  5. // Sebastien Pouliot <[email protected]>
  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. using NUnit.Framework;
  29. using System;
  30. using System.IO;
  31. using System.Drawing.Printing;
  32. using System.Security;
  33. using System.Security.Permissions;
  34. namespace MonoTests.System.Drawing.Printing {
  35. [TestFixture]
  36. public class PrintingPermissionTest {
  37. static PrintingPermissionLevel[] AllLevel = {
  38. PrintingPermissionLevel.NoPrinting,
  39. PrintingPermissionLevel.SafePrinting,
  40. PrintingPermissionLevel.DefaultPrinting,
  41. PrintingPermissionLevel.AllPrinting,
  42. };
  43. static PrintingPermissionLevel[] AllLevelExceptNoLevel = {
  44. PrintingPermissionLevel.SafePrinting,
  45. PrintingPermissionLevel.DefaultPrinting,
  46. PrintingPermissionLevel.AllPrinting,
  47. };
  48. static PrintingPermissionLevel[] AllLevelExceptAllLevel = {
  49. PrintingPermissionLevel.NoPrinting,
  50. PrintingPermissionLevel.SafePrinting,
  51. PrintingPermissionLevel.DefaultPrinting,
  52. };
  53. static PrintingPermissionLevel[] AllLevelExceptNoAndAllLevel = {
  54. PrintingPermissionLevel.SafePrinting,
  55. PrintingPermissionLevel.DefaultPrinting,
  56. };
  57. [Test]
  58. public void PermissionState_None ()
  59. {
  60. PermissionState ps = PermissionState.None;
  61. PrintingPermission pp = new PrintingPermission (ps);
  62. Assert.AreEqual (PrintingPermissionLevel.NoPrinting, pp.Level, "Level");
  63. Assert.IsFalse (pp.IsUnrestricted (), "IsUnrestricted");
  64. SecurityElement se = pp.ToXml ();
  65. // only class and version are present
  66. Assert.AreEqual ("NoPrinting", se.Attribute ("Level"), "Xml-Level");
  67. Assert.IsNull (se.Children, "Xml-Children");
  68. PrintingPermission copy = (PrintingPermission)pp.Copy ();
  69. Assert.IsFalse (Object.ReferenceEquals (pp, copy), "ReferenceEquals");
  70. Assert.AreEqual (pp.Level, copy.Level, "Level");
  71. Assert.AreEqual (pp.IsUnrestricted (), copy.IsUnrestricted (), "IsUnrestricted ()");
  72. }
  73. [Test]
  74. public void PermissionState_Unrestricted ()
  75. {
  76. PermissionState ps = PermissionState.Unrestricted;
  77. PrintingPermission pp = new PrintingPermission (ps);
  78. Assert.AreEqual (PrintingPermissionLevel.AllPrinting, pp.Level, "Level");
  79. Assert.IsTrue (pp.IsUnrestricted (), "IsUnrestricted");
  80. SecurityElement se = pp.ToXml ();
  81. // only class and version are present
  82. Assert.AreEqual ("true", se.Attribute ("Unrestricted"), "Xml-Unrestricted");
  83. Assert.IsNull (se.Children, "Xml-Children");
  84. PrintingPermission copy = (PrintingPermission)pp.Copy ();
  85. Assert.IsFalse (Object.ReferenceEquals (pp, copy), "ReferenceEquals");
  86. Assert.AreEqual (pp.Level, copy.Level, "Level");
  87. Assert.AreEqual (pp.IsUnrestricted (), copy.IsUnrestricted (), "IsUnrestricted ()");
  88. }
  89. [Test]
  90. [ExpectedException (typeof (ArgumentException))]
  91. public void PermissionState_Bad ()
  92. {
  93. PermissionState ps = (PermissionState)77;
  94. PrintingPermission pp = new PrintingPermission (ps);
  95. }
  96. [Test]
  97. [ExpectedException (typeof (ArgumentException))]
  98. public void PrintingPermissionLevels_Bad ()
  99. {
  100. PrintingPermissionLevel ppl = (PrintingPermissionLevel)(PrintingPermissionLevel.AllPrinting + 1);
  101. PrintingPermission pp = new PrintingPermission (ppl);
  102. }
  103. [Test]
  104. [ExpectedException (typeof (ArgumentException))]
  105. public void Level_PrintingPermissionLevels_Bad ()
  106. {
  107. PrintingPermissionLevel ppl = (PrintingPermissionLevel)(PrintingPermissionLevel.AllPrinting + 1);
  108. PrintingPermission pp = new PrintingPermission (PermissionState.None);
  109. pp.Level = ppl;
  110. }
  111. [Test]
  112. public void Copy ()
  113. {
  114. PrintingPermission pp = new PrintingPermission (PermissionState.None);
  115. foreach (PrintingPermissionLevel ppl in AllLevel) {
  116. pp.Level = ppl;
  117. PrintingPermission copy = (PrintingPermission)pp.Copy ();
  118. Assert.AreEqual (ppl, copy.Level, ppl.ToString ());
  119. }
  120. }
  121. [Test]
  122. public void Intersect_Null ()
  123. {
  124. PrintingPermission pp = new PrintingPermission (PermissionState.None);
  125. // No intersection with null
  126. foreach (PrintingPermissionLevel ppl in AllLevel) {
  127. pp.Level = ppl;
  128. Assert.IsNull (pp.Intersect (null), ppl.ToString ());
  129. }
  130. }
  131. [Test]
  132. public void Intersect_None ()
  133. {
  134. PrintingPermission sp1 = new PrintingPermission (PermissionState.None);
  135. PrintingPermission sp2 = new PrintingPermission (PermissionState.None);
  136. foreach (PrintingPermissionLevel ppl in AllLevelExceptNoLevel) {
  137. sp2.Level = ppl;
  138. // 1. Intersect None with ppl
  139. PrintingPermission result = (PrintingPermission)sp1.Intersect (sp2);
  140. Assert.IsNull (result, "None N " + ppl.ToString ());
  141. // 2. Intersect ppl with None
  142. result = (PrintingPermission)sp2.Intersect (sp1);
  143. Assert.IsNull (result, "None N " + ppl.ToString ());
  144. }
  145. }
  146. [Test]
  147. public void Intersect_Self ()
  148. {
  149. PrintingPermission pp = new PrintingPermission (PermissionState.None);
  150. foreach (PrintingPermissionLevel ppl in AllLevelExceptNoLevel) {
  151. pp.Level = ppl;
  152. PrintingPermission result = (PrintingPermission)pp.Intersect (pp);
  153. Assert.AreEqual (ppl, result.Level, ppl.ToString ());
  154. }
  155. }
  156. [Test]
  157. public void Intersect_Unrestricted ()
  158. {
  159. // Intersection with unrestricted == Copy
  160. // a. source (this) is unrestricted
  161. PrintingPermission sp1 = new PrintingPermission (PermissionState.Unrestricted);
  162. PrintingPermission sp2 = new PrintingPermission (PermissionState.None);
  163. foreach (PrintingPermissionLevel ppl in AllLevelExceptNoLevel) {
  164. sp2.Level = ppl;
  165. PrintingPermission result = (PrintingPermission)sp1.Intersect (sp2);
  166. Assert.AreEqual (sp2.Level, result.Level, "target " + ppl.ToString ());
  167. }
  168. // b. destination (target) is unrestricted
  169. foreach (PrintingPermissionLevel ppl in AllLevelExceptNoLevel) {
  170. sp2.Level = ppl;
  171. PrintingPermission result = (PrintingPermission)sp2.Intersect (sp1);
  172. Assert.AreEqual (sp2.Level, result.Level, "source " + ppl.ToString ());
  173. }
  174. // exceptions for NoLevel
  175. sp2.Level = PrintingPermissionLevel.NoPrinting;
  176. Assert.IsNull (sp1.Intersect (sp2), "target NoLevel");
  177. Assert.IsNull (sp2.Intersect (sp1), "source NoLevel");
  178. }
  179. [Test]
  180. public void IsSubset_Null ()
  181. {
  182. PrintingPermission pp = new PrintingPermission (PermissionState.None);
  183. Assert.IsTrue (pp.IsSubsetOf (null), "NoLevel");
  184. foreach (PrintingPermissionLevel ppl in AllLevelExceptNoLevel) {
  185. pp.Level = ppl;
  186. Assert.IsFalse (pp.IsSubsetOf (null), ppl.ToString ());
  187. }
  188. }
  189. [Test]
  190. public void IsSubset_None ()
  191. {
  192. // IsSubset with none
  193. // a. source (this) is none -> target is never a subset
  194. PrintingPermission sp1 = new PrintingPermission (PermissionState.None);
  195. PrintingPermission sp2 = new PrintingPermission (PermissionState.None);
  196. foreach (PrintingPermissionLevel ppl in AllLevel) {
  197. sp2.Level = ppl;
  198. Assert.IsTrue (sp1.IsSubsetOf (sp2), "target " + ppl.ToString ());
  199. }
  200. // b. destination (target) is none -> target is always a subset
  201. foreach (PrintingPermissionLevel ppl in AllLevelExceptNoLevel) {
  202. sp2.Level = ppl;
  203. Assert.IsFalse (sp2.IsSubsetOf (sp1), "source " + ppl.ToString ());
  204. }
  205. // exception of NoLevel
  206. sp2.Level = PrintingPermissionLevel.NoPrinting;
  207. Assert.IsTrue (sp2.IsSubsetOf (sp1), "source NoLevel");
  208. }
  209. [Test]
  210. public void IsSubset_Self ()
  211. {
  212. PrintingPermission pp = new PrintingPermission (PermissionState.None);
  213. foreach (PrintingPermissionLevel ppl in AllLevel) {
  214. pp.Level = ppl;
  215. PrintingPermission result = (PrintingPermission)pp.Intersect (pp);
  216. Assert.IsTrue (pp.IsSubsetOf (pp), ppl.ToString ());
  217. }
  218. }
  219. [Test]
  220. public void IsSubset_Unrestricted ()
  221. {
  222. // IsSubset with unrestricted
  223. // a. source (this) is unrestricted -> target is never a subset
  224. PrintingPermission sp1 = new PrintingPermission (PermissionState.Unrestricted);
  225. PrintingPermission sp2 = new PrintingPermission (PermissionState.None);
  226. foreach (PrintingPermissionLevel ppl in AllLevelExceptAllLevel) {
  227. sp2.Level = ppl;
  228. Assert.IsFalse (sp1.IsSubsetOf (sp2), "target " + ppl.ToString ());
  229. }
  230. // exception of AllLevel
  231. sp2.Level = PrintingPermissionLevel.AllPrinting;
  232. Assert.IsTrue (sp1.IsSubsetOf (sp2), "target AllLevel");
  233. // b. destination (target) is unrestricted -> target is always a subset
  234. foreach (PrintingPermissionLevel ppl in AllLevel) {
  235. sp2.Level = ppl;
  236. Assert.IsTrue (sp2.IsSubsetOf (sp1), "source " + ppl.ToString ());
  237. }
  238. }
  239. [Test]
  240. public void Union_Null ()
  241. {
  242. PrintingPermission pp = new PrintingPermission (PermissionState.None);
  243. // Union with null is a simple copy
  244. foreach (PrintingPermissionLevel ppl in AllLevel) {
  245. pp.Level = ppl;
  246. PrintingPermission union = (PrintingPermission)pp.Union (null);
  247. Assert.AreEqual (ppl, union.Level, ppl.ToString ());
  248. }
  249. }
  250. [Test]
  251. public void Union_None ()
  252. {
  253. // Union with none is same
  254. PrintingPermission pp1 = new PrintingPermission (PermissionState.None);
  255. PrintingPermission pp2 = new PrintingPermission (PermissionState.None);
  256. PrintingPermission union = null;
  257. // a. source (this) is none
  258. pp2.Level = PrintingPermissionLevel.NoPrinting;
  259. union = (PrintingPermission)pp1.Union (pp2);
  260. Assert.IsNull (union, "target NoPrinting");
  261. foreach (PrintingPermissionLevel ppl in AllLevelExceptNoAndAllLevel) {
  262. pp2.Level = ppl;
  263. union = (PrintingPermission)pp1.Union (pp2);
  264. Assert.IsFalse (union.IsUnrestricted (), "target " + ppl.ToString ());
  265. }
  266. pp2.Level = PrintingPermissionLevel.AllPrinting;
  267. union = (PrintingPermission)pp1.Union (pp2);
  268. Assert.IsTrue (union.IsUnrestricted (), "target AllPrinting");
  269. // b. destination (target) is none
  270. pp2.Level = PrintingPermissionLevel.NoPrinting;
  271. union = (PrintingPermission)pp2.Union (pp1);
  272. Assert.IsNull (union, "source NoPrinting");
  273. foreach (PrintingPermissionLevel ppl in AllLevelExceptNoAndAllLevel) {
  274. pp2.Level = ppl;
  275. union = (PrintingPermission)pp2.Union (pp1);
  276. Assert.IsFalse (union.IsUnrestricted (), "source " + ppl.ToString ());
  277. }
  278. pp2.Level = PrintingPermissionLevel.AllPrinting;
  279. union = (PrintingPermission)pp2.Union (pp1);
  280. Assert.IsTrue (union.IsUnrestricted (), "source AllPrinting");
  281. }
  282. [Test]
  283. public void Union_Self ()
  284. {
  285. PrintingPermission pp = new PrintingPermission (PermissionState.None);
  286. foreach (PrintingPermissionLevel ppl in AllLevelExceptNoLevel) {
  287. pp.Level = ppl;
  288. PrintingPermission result = (PrintingPermission)pp.Union (pp);
  289. Assert.AreEqual (ppl, result.Level, ppl.ToString ());
  290. }
  291. // union of NoPrinting with NoPrinting == null
  292. pp.Level = PrintingPermissionLevel.NoPrinting;
  293. Assert.IsNull (pp.Union (pp), "NoPrinting");
  294. }
  295. [Test]
  296. public void Union_Unrestricted ()
  297. {
  298. // Union with unrestricted is unrestricted
  299. PrintingPermission sp1 = new PrintingPermission (PermissionState.Unrestricted);
  300. PrintingPermission sp2 = new PrintingPermission (PermissionState.None);
  301. // a. source (this) is unrestricted
  302. foreach (PrintingPermissionLevel ppl in AllLevel) {
  303. sp2.Level = ppl;
  304. PrintingPermission union = (PrintingPermission)sp1.Union (sp2);
  305. Assert.IsTrue (union.IsUnrestricted (), "target " + ppl.ToString ());
  306. }
  307. // b. destination (target) is unrestricted
  308. foreach (PrintingPermissionLevel ppl in AllLevel) {
  309. sp2.Level = ppl;
  310. PrintingPermission union = (PrintingPermission)sp2.Union (sp1);
  311. Assert.IsTrue (union.IsUnrestricted (), "source " + ppl.ToString ());
  312. }
  313. }
  314. [Test]
  315. [ExpectedException (typeof (ArgumentNullException))]
  316. public void FromXml_Null ()
  317. {
  318. PrintingPermission pp = new PrintingPermission (PermissionState.None);
  319. pp.FromXml (null);
  320. }
  321. [Test]
  322. public void FromXml_WrongTag ()
  323. {
  324. PrintingPermission pp = new PrintingPermission (PermissionState.None);
  325. SecurityElement se = pp.ToXml ();
  326. se.Tag = "IMono";
  327. pp.FromXml (se);
  328. // note: normally IPermission classes (in corlib) DO care about the
  329. // IPermission tag
  330. }
  331. [Test]
  332. public void FromXml_WrongTagCase ()
  333. {
  334. PrintingPermission pp = new PrintingPermission (PermissionState.None);
  335. SecurityElement se = pp.ToXml ();
  336. se.Tag = "IPERMISSION"; // instead of IPermission
  337. pp.FromXml (se);
  338. // note: normally IPermission classes (in corlib) DO care about the
  339. // IPermission tag
  340. }
  341. [Test]
  342. #if !NET_2_0
  343. [ExpectedException (typeof (ArgumentException))]
  344. #endif
  345. public void FromXml_WrongClass ()
  346. {
  347. PrintingPermission pp = new PrintingPermission (PermissionState.None);
  348. SecurityElement se = pp.ToXml ();
  349. SecurityElement w = new SecurityElement (se.Tag);
  350. w.AddAttribute ("class", "Wrong" + se.Attribute ("class"));
  351. w.AddAttribute ("version", se.Attribute ("version"));
  352. pp.FromXml (w);
  353. // doesn't care of the class name at that stage
  354. // anyway the class has already be created so...
  355. }
  356. [Test]
  357. [ExpectedException (typeof (ArgumentException))]
  358. public void FromXml_NoClass ()
  359. {
  360. PrintingPermission pp = new PrintingPermission (PermissionState.None);
  361. SecurityElement se = pp.ToXml ();
  362. SecurityElement w = new SecurityElement (se.Tag);
  363. w.AddAttribute ("version", se.Attribute ("version"));
  364. pp.FromXml (w);
  365. // note: normally IPermission classes (in corlib) DO NOT care about
  366. // attribute "class" name presence in the XML
  367. }
  368. [Test]
  369. [ExpectedException (typeof (ArgumentException))]
  370. public void FromXml_WrongVersion ()
  371. {
  372. PrintingPermission pp = new PrintingPermission (PermissionState.None);
  373. SecurityElement se = pp.ToXml ();
  374. se.Attributes.Remove ("version");
  375. se.Attributes.Add ("version", "2");
  376. pp.FromXml (se);
  377. }
  378. [Test]
  379. #if !NET_2_0
  380. [ExpectedException (typeof (ArgumentException))]
  381. #endif
  382. public void FromXml_NoVersion ()
  383. {
  384. PrintingPermission pp = new PrintingPermission (PermissionState.None);
  385. SecurityElement se = pp.ToXml ();
  386. SecurityElement w = new SecurityElement (se.Tag);
  387. w.AddAttribute ("class", se.Attribute ("class"));
  388. pp.FromXml (w);
  389. }
  390. // Unification tests (with the MS final key)
  391. // note: corlib already test the ECMA key support for unification
  392. 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>";
  393. private const string fx10version = "1.0.3300.0";
  394. private const string fx11version = "1.0.5000.0";
  395. private const string fx20version = "2.0.0.0";
  396. private void Unification (string xml)
  397. {
  398. PermissionSetAttribute psa = new PermissionSetAttribute (SecurityAction.Assert);
  399. psa.XML = xml;
  400. string pset = psa.CreatePermissionSet ().ToString ();
  401. string currentVersion = typeof (string).Assembly.GetName ().Version.ToString ();
  402. Assert.IsTrue ((pset.IndexOf (currentVersion) > 0), currentVersion);
  403. }
  404. [Test]
  405. public void Unification_FromFx10 ()
  406. {
  407. Unification (String.Format (PermissionPattern, fx10version));
  408. }
  409. [Test]
  410. public void Unification_FromFx11 ()
  411. {
  412. Unification (String.Format (PermissionPattern, fx11version));
  413. }
  414. [Test]
  415. public void Unification_FromFx20 ()
  416. {
  417. Unification (String.Format (PermissionPattern, fx20version));
  418. }
  419. #if NET_2_0
  420. [Test]
  421. [Category ("NotWorking")]
  422. [ExpectedException (typeof (FileLoadException))]
  423. public void Unification_FromFx99 ()
  424. {
  425. Type.GetType (String.Format (PermissionPattern, "9.99.999.9999"));
  426. }
  427. #else
  428. [Test]
  429. public void Unification_FromFx99 ()
  430. {
  431. Unification (String.Format (PermissionPattern, "9.99.999.9999"));
  432. }
  433. #endif
  434. }
  435. }