AspNetHostingPermissionTest.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419
  1. //
  2. // AspNetHostingPermissionTest.cs -
  3. // NUnit Test Cases for AspNetHostingPermission
  4. //
  5. // Author:
  6. // Sebastien Pouliot <[email protected]>
  7. //
  8. // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
  9. //
  10. // Permission is hereby granted, free of charge, to any person obtaining
  11. // a copy of this software and associated documentation files (the
  12. // "Software"), to deal in the Software without restriction, including
  13. // without limitation the rights to use, copy, modify, merge, publish,
  14. // distribute, sublicense, and/or sell copies of the Software, and to
  15. // permit persons to whom the Software is furnished to do so, subject to
  16. // the following conditions:
  17. //
  18. // The above copyright notice and this permission notice shall be
  19. // included in all copies or substantial portions of the Software.
  20. //
  21. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  22. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  23. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  24. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  25. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  26. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  27. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  28. //
  29. using NUnit.Framework;
  30. using System;
  31. using System.Security;
  32. using System.Security.Permissions;
  33. using System.Web;
  34. //using System.Diagnostics;
  35. namespace MonoTests.System.Web {
  36. [TestFixture]
  37. public class AspNetHostingPermissionTest {
  38. static AspNetHostingPermissionLevel[] AllLevel = {
  39. AspNetHostingPermissionLevel.None,
  40. AspNetHostingPermissionLevel.Minimal,
  41. AspNetHostingPermissionLevel.Low,
  42. AspNetHostingPermissionLevel.Medium,
  43. AspNetHostingPermissionLevel.High,
  44. AspNetHostingPermissionLevel.Unrestricted,
  45. };
  46. static AspNetHostingPermissionLevel[] AllLevelExceptNone = {
  47. AspNetHostingPermissionLevel.Minimal,
  48. AspNetHostingPermissionLevel.Low,
  49. AspNetHostingPermissionLevel.Medium,
  50. AspNetHostingPermissionLevel.High,
  51. AspNetHostingPermissionLevel.Unrestricted,
  52. };
  53. static AspNetHostingPermissionLevel[] AllLevelExceptUnrestricted = {
  54. AspNetHostingPermissionLevel.None,
  55. AspNetHostingPermissionLevel.Minimal,
  56. AspNetHostingPermissionLevel.Low,
  57. AspNetHostingPermissionLevel.Medium,
  58. AspNetHostingPermissionLevel.High,
  59. };
  60. [Test]
  61. public void PermissionState_None ()
  62. {
  63. PermissionState ps = PermissionState.None;
  64. AspNetHostingPermission anhp = new AspNetHostingPermission (ps);
  65. Assert.AreEqual (AspNetHostingPermissionLevel.None, anhp.Level, "Level");
  66. Assert.IsFalse (anhp.IsUnrestricted (), "IsUnrestricted");
  67. SecurityElement se = anhp.ToXml ();
  68. // only class and version are present
  69. Assert.AreEqual ("None", se.Attribute ("Level"), "Xml-Level");
  70. Assert.IsNull (se.Children, "Xml-Children");
  71. AspNetHostingPermission copy = (AspNetHostingPermission)anhp.Copy ();
  72. Assert.IsFalse (Object.ReferenceEquals (anhp, copy), "ReferenceEquals");
  73. Assert.AreEqual (anhp.Level, copy.Level, "Level");
  74. Assert.AreEqual (anhp.IsUnrestricted (), copy.IsUnrestricted (), "IsUnrestricted ()");
  75. }
  76. [Test]
  77. public void PermissionState_Unrestricted ()
  78. {
  79. PermissionState ps = PermissionState.Unrestricted;
  80. AspNetHostingPermission anhp = new AspNetHostingPermission (ps);
  81. Assert.AreEqual (AspNetHostingPermissionLevel.Unrestricted, anhp.Level, "Level");
  82. Assert.IsTrue (anhp.IsUnrestricted (), "IsUnrestricted");
  83. SecurityElement se = anhp.ToXml ();
  84. Assert.IsNull (se.Attribute ("Unrestricted"), "Xml-Unrestricted");
  85. Assert.AreEqual ("Unrestricted", se.Attribute ("Level"), "Xml-Level");
  86. Assert.IsNull (se.Children, "Xml-Children");
  87. AspNetHostingPermission copy = (AspNetHostingPermission)anhp.Copy ();
  88. Assert.IsFalse (Object.ReferenceEquals (anhp, copy), "ReferenceEquals");
  89. Assert.AreEqual (anhp.Level, copy.Level, "Level");
  90. Assert.AreEqual (anhp.IsUnrestricted (), copy.IsUnrestricted (), "IsUnrestricted ()");
  91. }
  92. [Test]
  93. [ExpectedException (typeof (ArgumentException))]
  94. public void PermissionState_Bad ()
  95. {
  96. PermissionState ps = (PermissionState) Int32.MinValue;
  97. AspNetHostingPermission anhp = new AspNetHostingPermission (ps);
  98. }
  99. [Test]
  100. [ExpectedException (typeof (ArgumentException))]
  101. public void AspNetHostingPermissionLevels_Bad ()
  102. {
  103. AspNetHostingPermissionLevel ppl = (AspNetHostingPermissionLevel) Int32.MinValue;
  104. AspNetHostingPermission anhp = new AspNetHostingPermission (ppl);
  105. }
  106. [Test]
  107. [ExpectedException (typeof (ArgumentException))]
  108. public void Level_AspNetHostingPermissionLevels_Bad ()
  109. {
  110. AspNetHostingPermission anhp = new AspNetHostingPermission (PermissionState.None);
  111. anhp.Level = (AspNetHostingPermissionLevel) Int32.MinValue;
  112. }
  113. [Test]
  114. public void Copy ()
  115. {
  116. AspNetHostingPermission anhp = new AspNetHostingPermission (PermissionState.None);
  117. foreach (AspNetHostingPermissionLevel ppl in AllLevel) {
  118. anhp.Level = ppl;
  119. AspNetHostingPermission copy = (AspNetHostingPermission)anhp.Copy ();
  120. Assert.AreEqual (ppl, copy.Level, ppl.ToString ());
  121. }
  122. }
  123. [Test]
  124. public void Intersect_Null ()
  125. {
  126. AspNetHostingPermission anhp = new AspNetHostingPermission (PermissionState.None);
  127. // No intersection with null
  128. foreach (AspNetHostingPermissionLevel ppl in AllLevel) {
  129. anhp.Level = ppl;
  130. Assert.IsNull (anhp.Intersect (null), ppl.ToString ());
  131. }
  132. }
  133. [Test]
  134. public void Intersect_None ()
  135. {
  136. AspNetHostingPermission sp1 = new AspNetHostingPermission (PermissionState.None);
  137. AspNetHostingPermission sp2 = new AspNetHostingPermission (PermissionState.None);
  138. foreach (AspNetHostingPermissionLevel ppl in AllLevel) {
  139. sp2.Level = ppl;
  140. // 1. Intersect None with ppl
  141. AspNetHostingPermission result = (AspNetHostingPermission)sp1.Intersect (sp2);
  142. Assert.AreEqual (AspNetHostingPermissionLevel.None, result.Level, "None N " + ppl.ToString ());
  143. // 2. Intersect ppl with None
  144. result = (AspNetHostingPermission)sp2.Intersect (sp1);
  145. Assert.AreEqual (AspNetHostingPermissionLevel.None, result.Level, ppl.ToString () + "N None");
  146. }
  147. }
  148. [Test]
  149. public void Intersect_Self ()
  150. {
  151. AspNetHostingPermission anhp = new AspNetHostingPermission (PermissionState.None);
  152. foreach (AspNetHostingPermissionLevel ppl in AllLevel) {
  153. anhp.Level = ppl;
  154. AspNetHostingPermission result = (AspNetHostingPermission)anhp.Intersect (anhp);
  155. Assert.AreEqual (ppl, result.Level, ppl.ToString ());
  156. }
  157. }
  158. [Test]
  159. public void Intersect_Unrestricted ()
  160. {
  161. // Intersection with unrestricted == Copy
  162. // a. source (this) is unrestricted
  163. AspNetHostingPermission sp1 = new AspNetHostingPermission (PermissionState.Unrestricted);
  164. AspNetHostingPermission sp2 = new AspNetHostingPermission (PermissionState.None);
  165. foreach (AspNetHostingPermissionLevel ppl in AllLevel) {
  166. sp2.Level = ppl;
  167. AspNetHostingPermission result = (AspNetHostingPermission)sp1.Intersect (sp2);
  168. Assert.AreEqual (sp2.Level, result.Level, "target " + ppl.ToString ());
  169. }
  170. // b. destination (target) is unrestricted
  171. foreach (AspNetHostingPermissionLevel ppl in AllLevel) {
  172. sp2.Level = ppl;
  173. AspNetHostingPermission result = (AspNetHostingPermission)sp2.Intersect (sp1);
  174. Assert.AreEqual (sp2.Level, result.Level, "source " + ppl.ToString ());
  175. }
  176. }
  177. [Test]
  178. public void IsSubset_Null ()
  179. {
  180. AspNetHostingPermission anhp = new AspNetHostingPermission (PermissionState.None);
  181. Assert.IsTrue (anhp.IsSubsetOf (null), "NoLevel");
  182. foreach (AspNetHostingPermissionLevel ppl in AllLevelExceptNone) {
  183. anhp.Level = ppl;
  184. Assert.IsFalse (anhp.IsSubsetOf (null), ppl.ToString ());
  185. }
  186. }
  187. [Test]
  188. public void IsSubset_None ()
  189. {
  190. // IsSubset with none
  191. // a. source (this) is none -> target is never a subset
  192. AspNetHostingPermission sp1 = new AspNetHostingPermission (PermissionState.None);
  193. AspNetHostingPermission sp2 = new AspNetHostingPermission (PermissionState.None);
  194. foreach (AspNetHostingPermissionLevel ppl in AllLevel) {
  195. sp2.Level = ppl;
  196. Assert.IsTrue (sp1.IsSubsetOf (sp2), "target " + ppl.ToString ());
  197. }
  198. // b. destination (target) is none -> target is always a subset
  199. foreach (AspNetHostingPermissionLevel ppl in AllLevelExceptNone) {
  200. sp2.Level = ppl;
  201. Assert.IsFalse (sp2.IsSubsetOf (sp1), "source " + ppl.ToString ());
  202. }
  203. // exception of NoLevel
  204. sp2.Level = AspNetHostingPermissionLevel.None;
  205. Assert.IsTrue (sp2.IsSubsetOf (sp1), "source NoLevel");
  206. }
  207. [Test]
  208. public void IsSubset_Self ()
  209. {
  210. AspNetHostingPermission anhp = new AspNetHostingPermission (PermissionState.None);
  211. foreach (AspNetHostingPermissionLevel ppl in AllLevel) {
  212. anhp.Level = ppl;
  213. AspNetHostingPermission result = (AspNetHostingPermission)anhp.Intersect (anhp);
  214. Assert.IsTrue (anhp.IsSubsetOf (anhp), ppl.ToString ());
  215. }
  216. }
  217. [Test]
  218. public void IsSubset_Unrestricted ()
  219. {
  220. // IsSubset with unrestricted
  221. // a. source (this) is unrestricted -> target is never a subset
  222. AspNetHostingPermission sp1 = new AspNetHostingPermission (PermissionState.Unrestricted);
  223. AspNetHostingPermission sp2 = new AspNetHostingPermission (PermissionState.None);
  224. foreach (AspNetHostingPermissionLevel ppl in AllLevelExceptUnrestricted) {
  225. sp2.Level = ppl;
  226. Assert.IsFalse (sp1.IsSubsetOf (sp2), "target " + ppl.ToString ());
  227. }
  228. // exception of AllLevel
  229. sp2.Level = AspNetHostingPermissionLevel.Unrestricted;
  230. Assert.IsTrue (sp1.IsSubsetOf (sp2), "target AllLevel");
  231. // b. destination (target) is unrestricted -> target is always a subset
  232. foreach (AspNetHostingPermissionLevel ppl in AllLevel) {
  233. sp2.Level = ppl;
  234. Assert.IsTrue (sp2.IsSubsetOf (sp1), "source " + ppl.ToString ());
  235. }
  236. }
  237. [Test]
  238. public void Union_Null ()
  239. {
  240. AspNetHostingPermission anhp = new AspNetHostingPermission (PermissionState.None);
  241. // Union with null is a simple copy
  242. foreach (AspNetHostingPermissionLevel ppl in AllLevel) {
  243. anhp.Level = ppl;
  244. AspNetHostingPermission union = (AspNetHostingPermission)anhp.Union (null);
  245. Assert.AreEqual (ppl, union.Level, ppl.ToString ());
  246. }
  247. }
  248. [Test]
  249. public void Union_None ()
  250. {
  251. // Union with none is same
  252. AspNetHostingPermission pp1 = new AspNetHostingPermission (PermissionState.None);
  253. AspNetHostingPermission pp2 = new AspNetHostingPermission (PermissionState.None);
  254. AspNetHostingPermission union = null;
  255. foreach (AspNetHostingPermissionLevel ppl in AllLevelExceptUnrestricted) {
  256. pp2.Level = ppl;
  257. union = (AspNetHostingPermission)pp1.Union (pp2);
  258. Assert.IsFalse (union.IsUnrestricted (), "target.Unrestricted " + ppl.ToString ());
  259. Assert.AreEqual (ppl, union.Level, "target.Level " + ppl.ToString ());
  260. union = (AspNetHostingPermission)pp2.Union (pp1);
  261. Assert.IsFalse (union.IsUnrestricted (), "source.Unrestricted " + ppl.ToString ());
  262. Assert.AreEqual (ppl, union.Level, "source.Level " + ppl.ToString ());
  263. }
  264. pp2.Level = AspNetHostingPermissionLevel.Unrestricted;
  265. union = (AspNetHostingPermission)pp1.Union (pp2);
  266. Assert.IsTrue (union.IsUnrestricted (), "target.Unrestricted Unrestricted");
  267. Assert.AreEqual (AspNetHostingPermissionLevel.Unrestricted, union.Level, "target.Level Unrestricted");
  268. union = (AspNetHostingPermission)pp2.Union (pp1);
  269. Assert.IsTrue (union.IsUnrestricted (), "source.Unrestricted Unrestricted");
  270. Assert.AreEqual (AspNetHostingPermissionLevel.Unrestricted, union.Level, "source.Level Unrestricted");
  271. }
  272. [Test]
  273. public void Union_Self ()
  274. {
  275. AspNetHostingPermission anhp = new AspNetHostingPermission (PermissionState.None);
  276. foreach (AspNetHostingPermissionLevel ppl in AllLevel) {
  277. anhp.Level = ppl;
  278. AspNetHostingPermission result = (AspNetHostingPermission)anhp.Union (anhp);
  279. Assert.AreEqual (ppl, result.Level, ppl.ToString ());
  280. }
  281. }
  282. [Test]
  283. public void Union_Unrestricted ()
  284. {
  285. // Union with unrestricted is unrestricted
  286. AspNetHostingPermission sp1 = new AspNetHostingPermission (PermissionState.Unrestricted);
  287. AspNetHostingPermission sp2 = new AspNetHostingPermission (PermissionState.None);
  288. // a. source (this) is unrestricted
  289. foreach (AspNetHostingPermissionLevel ppl in AllLevel) {
  290. sp2.Level = ppl;
  291. AspNetHostingPermission union = (AspNetHostingPermission)sp1.Union (sp2);
  292. Assert.IsTrue (union.IsUnrestricted (), "target " + ppl.ToString ());
  293. }
  294. // b. destination (target) is unrestricted
  295. foreach (AspNetHostingPermissionLevel ppl in AllLevel) {
  296. sp2.Level = ppl;
  297. AspNetHostingPermission union = (AspNetHostingPermission)sp2.Union (sp1);
  298. Assert.IsTrue (union.IsUnrestricted (), "source " + ppl.ToString ());
  299. }
  300. }
  301. [Test]
  302. [ExpectedException (typeof (ArgumentNullException))]
  303. public void FromXml_Null ()
  304. {
  305. AspNetHostingPermission anhp = new AspNetHostingPermission (PermissionState.None);
  306. anhp.FromXml (null);
  307. }
  308. [Test]
  309. [ExpectedException (typeof (ArgumentException))]
  310. public void FromXml_WrongTag ()
  311. {
  312. AspNetHostingPermission anhp = new AspNetHostingPermission (PermissionState.None);
  313. SecurityElement se = anhp.ToXml ();
  314. se.Tag = "IMono";
  315. anhp.FromXml (se);
  316. // note: normally IPermission classes (in corlib) DO care about the
  317. // IPermission tag
  318. }
  319. [Test]
  320. [ExpectedException (typeof (ArgumentException))]
  321. public void FromXml_WrongTagCase ()
  322. {
  323. AspNetHostingPermission anhp = new AspNetHostingPermission (PermissionState.None);
  324. SecurityElement se = anhp.ToXml ();
  325. se.Tag = "IPERMISSION"; // instead of IPermission
  326. anhp.FromXml (se);
  327. // note: normally IPermission classes (in corlib) DO care about the
  328. // IPermission tag
  329. }
  330. [Test]
  331. public void FromXml_WrongClass ()
  332. {
  333. AspNetHostingPermission anhp = new AspNetHostingPermission (PermissionState.None);
  334. SecurityElement se = anhp.ToXml ();
  335. SecurityElement w = new SecurityElement (se.Tag);
  336. w.AddAttribute ("class", "Wrong" + se.Attribute ("class"));
  337. w.AddAttribute ("version", se.Attribute ("version"));
  338. anhp.FromXml (w);
  339. // doesn't care of the class name at that stage
  340. // anyway the class has already be created so...
  341. }
  342. [Test]
  343. [ExpectedException (typeof (ArgumentException))]
  344. public void FromXml_NoClass ()
  345. {
  346. AspNetHostingPermission anhp = new AspNetHostingPermission (PermissionState.None);
  347. SecurityElement se = anhp.ToXml ();
  348. SecurityElement w = new SecurityElement (se.Tag);
  349. w.AddAttribute ("version", se.Attribute ("version"));
  350. anhp.FromXml (w);
  351. // note: normally IPermission classes (in corlib) DO NOT care about
  352. // attribute "class" name presence in the XML
  353. }
  354. [Test]
  355. [ExpectedException (typeof (ArgumentException))]
  356. public void FromXml_WrongVersion ()
  357. {
  358. AspNetHostingPermission anhp = new AspNetHostingPermission (PermissionState.None);
  359. SecurityElement se = anhp.ToXml ();
  360. se.Attributes.Remove ("version");
  361. se.Attributes.Add ("version", "2");
  362. anhp.FromXml (se);
  363. }
  364. [Test]
  365. [ExpectedException (typeof (ArgumentException))]
  366. public void FromXml_NoVersion ()
  367. {
  368. AspNetHostingPermission anhp = new AspNetHostingPermission (PermissionState.None);
  369. SecurityElement se = anhp.ToXml ();
  370. SecurityElement w = new SecurityElement (se.Tag);
  371. w.AddAttribute ("class", se.Attribute ("class"));
  372. anhp.FromXml (w);
  373. }
  374. }
  375. }