AspNetHostingPermissionTest.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427
  1. //
  2. // AspNetHostingPermissionTest.cs -
  3. // NUnit Test Cases for AspNetHostingPermission
  4. //
  5. // Author:
  6. // Sebastien Pouliot <[email protected]>
  7. //
  8. // Copyright (C) 2004-2005 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. namespace MonoTests.System.Web {
  35. [TestFixture]
  36. public class AspNetHostingPermissionTest {
  37. static AspNetHostingPermissionLevel[] AllLevel = {
  38. AspNetHostingPermissionLevel.None,
  39. AspNetHostingPermissionLevel.Minimal,
  40. AspNetHostingPermissionLevel.Low,
  41. AspNetHostingPermissionLevel.Medium,
  42. AspNetHostingPermissionLevel.High,
  43. AspNetHostingPermissionLevel.Unrestricted,
  44. };
  45. static AspNetHostingPermissionLevel[] AllLevelExceptNone = {
  46. AspNetHostingPermissionLevel.Minimal,
  47. AspNetHostingPermissionLevel.Low,
  48. AspNetHostingPermissionLevel.Medium,
  49. AspNetHostingPermissionLevel.High,
  50. AspNetHostingPermissionLevel.Unrestricted,
  51. };
  52. static AspNetHostingPermissionLevel[] AllLevelExceptUnrestricted = {
  53. AspNetHostingPermissionLevel.None,
  54. AspNetHostingPermissionLevel.Minimal,
  55. AspNetHostingPermissionLevel.Low,
  56. AspNetHostingPermissionLevel.Medium,
  57. AspNetHostingPermissionLevel.High,
  58. };
  59. [Test]
  60. public void PermissionState_None ()
  61. {
  62. PermissionState ps = PermissionState.None;
  63. AspNetHostingPermission anhp = new AspNetHostingPermission (ps);
  64. Assert.AreEqual (AspNetHostingPermissionLevel.None, anhp.Level, "Level");
  65. Assert.IsFalse (anhp.IsUnrestricted (), "IsUnrestricted");
  66. SecurityElement se = anhp.ToXml ();
  67. // only class and version are present
  68. Assert.AreEqual ("None", se.Attribute ("Level"), "Xml-Level");
  69. Assert.IsNull (se.Children, "Xml-Children");
  70. AspNetHostingPermission copy = (AspNetHostingPermission)anhp.Copy ();
  71. Assert.IsFalse (Object.ReferenceEquals (anhp, copy), "ReferenceEquals");
  72. Assert.AreEqual (anhp.Level, copy.Level, "Level");
  73. Assert.AreEqual (anhp.IsUnrestricted (), copy.IsUnrestricted (), "IsUnrestricted ()");
  74. }
  75. [Test]
  76. public void PermissionState_Unrestricted ()
  77. {
  78. PermissionState ps = PermissionState.Unrestricted;
  79. AspNetHostingPermission anhp = new AspNetHostingPermission (ps);
  80. Assert.AreEqual (AspNetHostingPermissionLevel.Unrestricted, anhp.Level, "Level");
  81. Assert.IsTrue (anhp.IsUnrestricted (), "IsUnrestricted");
  82. SecurityElement se = anhp.ToXml ();
  83. #if NET_2_0
  84. // fixed in 2.0 RC
  85. Assert.IsNotNull (se.Attribute ("Unrestricted"), "Xml-Unrestricted");
  86. #else
  87. Assert.IsNull (se.Attribute ("Unrestricted"), "Xml-Unrestricted");
  88. #endif
  89. Assert.AreEqual ("Unrestricted", se.Attribute ("Level"), "Xml-Level");
  90. Assert.IsNull (se.Children, "Xml-Children");
  91. AspNetHostingPermission copy = (AspNetHostingPermission)anhp.Copy ();
  92. Assert.IsFalse (Object.ReferenceEquals (anhp, copy), "ReferenceEquals");
  93. Assert.AreEqual (anhp.Level, copy.Level, "Level");
  94. Assert.AreEqual (anhp.IsUnrestricted (), copy.IsUnrestricted (), "IsUnrestricted ()");
  95. }
  96. [Test]
  97. [ExpectedException (typeof (ArgumentException))]
  98. public void PermissionState_Bad ()
  99. {
  100. PermissionState ps = (PermissionState) Int32.MinValue;
  101. AspNetHostingPermission anhp = new AspNetHostingPermission (ps);
  102. }
  103. [Test]
  104. [ExpectedException (typeof (ArgumentException))]
  105. public void AspNetHostingPermissionLevels_Bad ()
  106. {
  107. AspNetHostingPermissionLevel ppl = (AspNetHostingPermissionLevel) Int32.MinValue;
  108. AspNetHostingPermission anhp = new AspNetHostingPermission (ppl);
  109. }
  110. [Test]
  111. [ExpectedException (typeof (ArgumentException))]
  112. public void Level_AspNetHostingPermissionLevels_Bad ()
  113. {
  114. AspNetHostingPermission anhp = new AspNetHostingPermission (PermissionState.None);
  115. anhp.Level = (AspNetHostingPermissionLevel) Int32.MinValue;
  116. }
  117. [Test]
  118. public void Copy ()
  119. {
  120. AspNetHostingPermission anhp = new AspNetHostingPermission (PermissionState.None);
  121. foreach (AspNetHostingPermissionLevel ppl in AllLevel) {
  122. anhp.Level = ppl;
  123. AspNetHostingPermission copy = (AspNetHostingPermission)anhp.Copy ();
  124. Assert.AreEqual (ppl, copy.Level, ppl.ToString ());
  125. }
  126. }
  127. [Test]
  128. public void Intersect_Null ()
  129. {
  130. AspNetHostingPermission anhp = new AspNetHostingPermission (PermissionState.None);
  131. // No intersection with null
  132. foreach (AspNetHostingPermissionLevel ppl in AllLevel) {
  133. anhp.Level = ppl;
  134. IPermission p = anhp.Intersect (null);
  135. #if ! NET_2_0
  136. if (p != null)
  137. Assert.Ignore ("Behaviour changed in FX 1.1 SP1");
  138. #endif
  139. Assert.IsNull (p, ppl.ToString ());
  140. }
  141. }
  142. [Test]
  143. public void Intersect_None ()
  144. {
  145. AspNetHostingPermission sp1 = new AspNetHostingPermission (PermissionState.None);
  146. AspNetHostingPermission sp2 = new AspNetHostingPermission (PermissionState.None);
  147. foreach (AspNetHostingPermissionLevel ppl in AllLevel) {
  148. sp2.Level = ppl;
  149. // 1. Intersect None with ppl
  150. AspNetHostingPermission result = (AspNetHostingPermission)sp1.Intersect (sp2);
  151. Assert.AreEqual (AspNetHostingPermissionLevel.None, result.Level, "None N " + ppl.ToString ());
  152. // 2. Intersect ppl with None
  153. result = (AspNetHostingPermission)sp2.Intersect (sp1);
  154. Assert.AreEqual (AspNetHostingPermissionLevel.None, result.Level, ppl.ToString () + "N None");
  155. }
  156. }
  157. [Test]
  158. public void Intersect_Self ()
  159. {
  160. AspNetHostingPermission anhp = new AspNetHostingPermission (PermissionState.None);
  161. foreach (AspNetHostingPermissionLevel ppl in AllLevel) {
  162. anhp.Level = ppl;
  163. AspNetHostingPermission result = (AspNetHostingPermission)anhp.Intersect (anhp);
  164. Assert.AreEqual (ppl, result.Level, ppl.ToString ());
  165. }
  166. }
  167. [Test]
  168. public void Intersect_Unrestricted ()
  169. {
  170. // Intersection with unrestricted == Copy
  171. // a. source (this) is unrestricted
  172. AspNetHostingPermission sp1 = new AspNetHostingPermission (PermissionState.Unrestricted);
  173. AspNetHostingPermission sp2 = new AspNetHostingPermission (PermissionState.None);
  174. foreach (AspNetHostingPermissionLevel ppl in AllLevel) {
  175. sp2.Level = ppl;
  176. AspNetHostingPermission result = (AspNetHostingPermission)sp1.Intersect (sp2);
  177. Assert.AreEqual (sp2.Level, result.Level, "target " + ppl.ToString ());
  178. }
  179. // b. destination (target) is unrestricted
  180. foreach (AspNetHostingPermissionLevel ppl in AllLevel) {
  181. sp2.Level = ppl;
  182. AspNetHostingPermission result = (AspNetHostingPermission)sp2.Intersect (sp1);
  183. Assert.AreEqual (sp2.Level, result.Level, "source " + ppl.ToString ());
  184. }
  185. }
  186. [Test]
  187. public void IsSubset_Null ()
  188. {
  189. AspNetHostingPermission anhp = new AspNetHostingPermission (PermissionState.None);
  190. Assert.IsTrue (anhp.IsSubsetOf (null), "NoLevel");
  191. foreach (AspNetHostingPermissionLevel ppl in AllLevelExceptNone) {
  192. anhp.Level = ppl;
  193. Assert.IsFalse (anhp.IsSubsetOf (null), ppl.ToString ());
  194. }
  195. }
  196. [Test]
  197. public void IsSubset_None ()
  198. {
  199. // IsSubset with none
  200. // a. source (this) is none -> target is never a subset
  201. AspNetHostingPermission sp1 = new AspNetHostingPermission (PermissionState.None);
  202. AspNetHostingPermission sp2 = new AspNetHostingPermission (PermissionState.None);
  203. foreach (AspNetHostingPermissionLevel ppl in AllLevel) {
  204. sp2.Level = ppl;
  205. Assert.IsTrue (sp1.IsSubsetOf (sp2), "target " + ppl.ToString ());
  206. }
  207. // b. destination (target) is none -> target is always a subset
  208. foreach (AspNetHostingPermissionLevel ppl in AllLevelExceptNone) {
  209. sp2.Level = ppl;
  210. Assert.IsFalse (sp2.IsSubsetOf (sp1), "source " + ppl.ToString ());
  211. }
  212. // exception of NoLevel
  213. sp2.Level = AspNetHostingPermissionLevel.None;
  214. Assert.IsTrue (sp2.IsSubsetOf (sp1), "source NoLevel");
  215. }
  216. [Test]
  217. public void IsSubset_Self ()
  218. {
  219. AspNetHostingPermission anhp = new AspNetHostingPermission (PermissionState.None);
  220. foreach (AspNetHostingPermissionLevel ppl in AllLevel) {
  221. anhp.Level = ppl;
  222. AspNetHostingPermission result = (AspNetHostingPermission)anhp.Intersect (anhp);
  223. Assert.IsTrue (anhp.IsSubsetOf (anhp), ppl.ToString ());
  224. }
  225. }
  226. [Test]
  227. public void IsSubset_Unrestricted ()
  228. {
  229. // IsSubset with unrestricted
  230. // a. source (this) is unrestricted -> target is never a subset
  231. AspNetHostingPermission sp1 = new AspNetHostingPermission (PermissionState.Unrestricted);
  232. AspNetHostingPermission sp2 = new AspNetHostingPermission (PermissionState.None);
  233. foreach (AspNetHostingPermissionLevel ppl in AllLevelExceptUnrestricted) {
  234. sp2.Level = ppl;
  235. Assert.IsFalse (sp1.IsSubsetOf (sp2), "target " + ppl.ToString ());
  236. }
  237. // exception of AllLevel
  238. sp2.Level = AspNetHostingPermissionLevel.Unrestricted;
  239. Assert.IsTrue (sp1.IsSubsetOf (sp2), "target AllLevel");
  240. // b. destination (target) is unrestricted -> target is always a subset
  241. foreach (AspNetHostingPermissionLevel ppl in AllLevel) {
  242. sp2.Level = ppl;
  243. Assert.IsTrue (sp2.IsSubsetOf (sp1), "source " + ppl.ToString ());
  244. }
  245. }
  246. [Test]
  247. public void Union_Null ()
  248. {
  249. AspNetHostingPermission anhp = new AspNetHostingPermission (PermissionState.None);
  250. // Union with null is a simple copy
  251. foreach (AspNetHostingPermissionLevel ppl in AllLevel) {
  252. anhp.Level = ppl;
  253. AspNetHostingPermission union = (AspNetHostingPermission)anhp.Union (null);
  254. Assert.AreEqual (ppl, union.Level, ppl.ToString ());
  255. }
  256. }
  257. [Test]
  258. public void Union_None ()
  259. {
  260. // Union with none is same
  261. AspNetHostingPermission pp1 = new AspNetHostingPermission (PermissionState.None);
  262. AspNetHostingPermission pp2 = new AspNetHostingPermission (PermissionState.None);
  263. AspNetHostingPermission union = null;
  264. foreach (AspNetHostingPermissionLevel ppl in AllLevelExceptUnrestricted) {
  265. pp2.Level = ppl;
  266. union = (AspNetHostingPermission)pp1.Union (pp2);
  267. Assert.IsFalse (union.IsUnrestricted (), "target.Unrestricted " + ppl.ToString ());
  268. Assert.AreEqual (ppl, union.Level, "target.Level " + ppl.ToString ());
  269. union = (AspNetHostingPermission)pp2.Union (pp1);
  270. Assert.IsFalse (union.IsUnrestricted (), "source.Unrestricted " + ppl.ToString ());
  271. Assert.AreEqual (ppl, union.Level, "source.Level " + ppl.ToString ());
  272. }
  273. pp2.Level = AspNetHostingPermissionLevel.Unrestricted;
  274. union = (AspNetHostingPermission)pp1.Union (pp2);
  275. Assert.IsTrue (union.IsUnrestricted (), "target.Unrestricted Unrestricted");
  276. Assert.AreEqual (AspNetHostingPermissionLevel.Unrestricted, union.Level, "target.Level Unrestricted");
  277. union = (AspNetHostingPermission)pp2.Union (pp1);
  278. Assert.IsTrue (union.IsUnrestricted (), "source.Unrestricted Unrestricted");
  279. Assert.AreEqual (AspNetHostingPermissionLevel.Unrestricted, union.Level, "source.Level Unrestricted");
  280. }
  281. [Test]
  282. public void Union_Self ()
  283. {
  284. AspNetHostingPermission anhp = new AspNetHostingPermission (PermissionState.None);
  285. foreach (AspNetHostingPermissionLevel ppl in AllLevel) {
  286. anhp.Level = ppl;
  287. AspNetHostingPermission result = (AspNetHostingPermission)anhp.Union (anhp);
  288. Assert.AreEqual (ppl, result.Level, ppl.ToString ());
  289. }
  290. }
  291. [Test]
  292. public void Union_Unrestricted ()
  293. {
  294. // Union with unrestricted is unrestricted
  295. AspNetHostingPermission sp1 = new AspNetHostingPermission (PermissionState.Unrestricted);
  296. AspNetHostingPermission sp2 = new AspNetHostingPermission (PermissionState.None);
  297. // a. source (this) is unrestricted
  298. foreach (AspNetHostingPermissionLevel ppl in AllLevel) {
  299. sp2.Level = ppl;
  300. AspNetHostingPermission union = (AspNetHostingPermission)sp1.Union (sp2);
  301. Assert.IsTrue (union.IsUnrestricted (), "target " + ppl.ToString ());
  302. }
  303. // b. destination (target) is unrestricted
  304. foreach (AspNetHostingPermissionLevel ppl in AllLevel) {
  305. sp2.Level = ppl;
  306. AspNetHostingPermission union = (AspNetHostingPermission)sp2.Union (sp1);
  307. Assert.IsTrue (union.IsUnrestricted (), "source " + ppl.ToString ());
  308. }
  309. }
  310. [Test]
  311. [ExpectedException (typeof (ArgumentNullException))]
  312. public void FromXml_Null ()
  313. {
  314. AspNetHostingPermission anhp = new AspNetHostingPermission (PermissionState.None);
  315. anhp.FromXml (null);
  316. }
  317. [Test]
  318. [ExpectedException (typeof (ArgumentException))]
  319. public void FromXml_WrongTag ()
  320. {
  321. AspNetHostingPermission anhp = new AspNetHostingPermission (PermissionState.None);
  322. SecurityElement se = anhp.ToXml ();
  323. se.Tag = "IMono";
  324. anhp.FromXml (se);
  325. // note: normally IPermission classes (in corlib) DO care about the
  326. // IPermission tag
  327. }
  328. [Test]
  329. [ExpectedException (typeof (ArgumentException))]
  330. public void FromXml_WrongTagCase ()
  331. {
  332. AspNetHostingPermission anhp = new AspNetHostingPermission (PermissionState.None);
  333. SecurityElement se = anhp.ToXml ();
  334. se.Tag = "IPERMISSION"; // instead of IPermission
  335. anhp.FromXml (se);
  336. // note: normally IPermission classes (in corlib) DO care about the
  337. // IPermission tag
  338. }
  339. [Test]
  340. public void FromXml_WrongClass ()
  341. {
  342. AspNetHostingPermission anhp = new AspNetHostingPermission (PermissionState.None);
  343. SecurityElement se = anhp.ToXml ();
  344. SecurityElement w = new SecurityElement (se.Tag);
  345. w.AddAttribute ("class", "Wrong" + se.Attribute ("class"));
  346. w.AddAttribute ("version", se.Attribute ("version"));
  347. anhp.FromXml (w);
  348. // doesn't care of the class name at that stage
  349. // anyway the class has already be created so...
  350. }
  351. [Test]
  352. [ExpectedException (typeof (ArgumentException))]
  353. public void FromXml_NoClass ()
  354. {
  355. AspNetHostingPermission anhp = new AspNetHostingPermission (PermissionState.None);
  356. SecurityElement se = anhp.ToXml ();
  357. SecurityElement w = new SecurityElement (se.Tag);
  358. w.AddAttribute ("version", se.Attribute ("version"));
  359. anhp.FromXml (w);
  360. // note: normally IPermission classes (in corlib) DO NOT care about
  361. // attribute "class" name presence in the XML
  362. }
  363. [Test]
  364. [ExpectedException (typeof (ArgumentException))]
  365. public void FromXml_WrongVersion ()
  366. {
  367. AspNetHostingPermission anhp = new AspNetHostingPermission (PermissionState.None);
  368. SecurityElement se = anhp.ToXml ();
  369. se.Attributes.Remove ("version");
  370. se.Attributes.Add ("version", "2");
  371. anhp.FromXml (se);
  372. }
  373. [Test]
  374. [ExpectedException (typeof (ArgumentException))]
  375. public void FromXml_NoVersion ()
  376. {
  377. AspNetHostingPermission anhp = new AspNetHostingPermission (PermissionState.None);
  378. SecurityElement se = anhp.ToXml ();
  379. SecurityElement w = new SecurityElement (se.Tag);
  380. w.AddAttribute ("class", se.Attribute ("class"));
  381. anhp.FromXml (w);
  382. }
  383. }
  384. }