SecurityElementTest.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408
  1. //
  2. // SecurityElementTest.cs - NUnit Test Cases for System.Security.SecurityElement
  3. //
  4. // Authors:
  5. // Lawrence Pit ([email protected])
  6. // Sebastien Pouliot <[email protected]>
  7. //
  8. // Portions (C) 2004 Motus Technologies Inc. (http://www.motus.com)
  9. // Copyright (C) 2005 Novell, Inc (http://www.novell.com)
  10. //
  11. // Permission is hereby granted, free of charge, to any person obtaining
  12. // a copy of this software and associated documentation files (the
  13. // "Software"), to deal in the Software without restriction, including
  14. // without limitation the rights to use, copy, modify, merge, publish,
  15. // distribute, sublicense, and/or sell copies of the Software, and to
  16. // permit persons to whom the Software is furnished to do so, subject to
  17. // the following conditions:
  18. //
  19. // The above copyright notice and this permission notice shall be
  20. // included in all copies or substantial portions of the Software.
  21. //
  22. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  23. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  24. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  25. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  26. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  27. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  28. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  29. //
  30. using NUnit.Framework;
  31. using System;
  32. using System.Collections;
  33. using System.Security;
  34. namespace MonoTests.System.Security {
  35. [TestFixture]
  36. public class SecurityElementTest : Assertion {
  37. SecurityElement elem;
  38. [SetUp]
  39. public void SetUp ()
  40. {
  41. elem = CreateElement ();
  42. }
  43. private SecurityElement CreateElement ()
  44. {
  45. SecurityElement elem = new SecurityElement ("IPermission");
  46. elem.AddAttribute ("class", "System");
  47. elem.AddAttribute ("version", "1");
  48. SecurityElement child = new SecurityElement ("ConnectAccess");
  49. elem.AddChild (child);
  50. SecurityElement grandchild = new SecurityElement ("ENDPOINT", "some text");
  51. grandchild.AddAttribute ("transport", "All");
  52. grandchild.AddAttribute ("host", "localhost");
  53. grandchild.AddAttribute ("port", "8080");
  54. child.AddChild (grandchild);
  55. SecurityElement grandchild2 = new SecurityElement ("ENDPOINT");
  56. grandchild2.AddAttribute ("transport", "Tcp");
  57. grandchild2.AddAttribute ("host", "www.ximian.com");
  58. grandchild2.AddAttribute ("port", "All");
  59. child.AddChild (grandchild2);
  60. return elem;
  61. }
  62. [Test]
  63. public void ConstructorsTagTest ()
  64. {
  65. SecurityElement se = new SecurityElement ("tag", "text");
  66. AssertNull ("EmptyAttributes", se.Attributes);
  67. AssertNull ("EmptyChildren", se.Children);
  68. AssertEquals ("Tag", "tag", se.Tag);
  69. AssertEquals ("Text", "text", se.Text);
  70. }
  71. [Test]
  72. [ExpectedException (typeof (ArgumentNullException))]
  73. public void ConstructorsTagNullText ()
  74. {
  75. SecurityElement se = new SecurityElement (null, "text");
  76. }
  77. [Test]
  78. public void ConstructorsTagTextNull ()
  79. {
  80. SecurityElement se = new SecurityElement ("tag", null);
  81. AssertNull ("EmptyAttributes", se.Attributes);
  82. AssertNull ("EmptyChildren", se.Children);
  83. AssertEquals ("Tag", "tag", se.Tag);
  84. AssertNull ("Text", se.Text);
  85. }
  86. [Test]
  87. public void ConstructorsTag ()
  88. {
  89. SecurityElement se = new SecurityElement ("tag");
  90. AssertNull ("EmptyAttributes", se.Attributes);
  91. AssertNull ("EmptyChildren", se.Children);
  92. AssertEquals ("Tag", "tag", se.Tag);
  93. AssertNull ("Text", se.Text);
  94. }
  95. [Test]
  96. [ExpectedException (typeof (ArgumentNullException))]
  97. public void ConstructorsTagNull ()
  98. {
  99. SecurityElement se = new SecurityElement (null);
  100. }
  101. [Test]
  102. [ExpectedException (typeof (ArgumentNullException))]
  103. public void AddAttribute_NameNullValue ()
  104. {
  105. elem.AddAttribute (null, "valid");
  106. }
  107. [Test]
  108. [ExpectedException (typeof (ArgumentNullException))]
  109. public void AddAttribute_NameValueNull ()
  110. {
  111. elem.AddAttribute ("valid", null);
  112. }
  113. [Test]
  114. [ExpectedException (typeof (ArgumentException))]
  115. public void AddAttribute_InvalidName ()
  116. {
  117. elem.AddAttribute ("<invalid>", "valid");
  118. }
  119. [Test]
  120. [ExpectedException (typeof (ArgumentException))]
  121. public void AddAttribute_InvalidValue ()
  122. {
  123. elem.AddAttribute ("valid", "invalid\"");
  124. }
  125. [Test]
  126. public void AddAttribute_InvalidValue2 ()
  127. {
  128. elem.AddAttribute ("valid", "valid&");
  129. // in xml world this is actually not considered valid
  130. // but it is by MS.Net
  131. }
  132. [Test]
  133. [ExpectedException (typeof (ArgumentException))]
  134. public void AddAttribute_InvalidValue3 ()
  135. {
  136. elem.AddAttribute ("valid", "<invalid>");
  137. }
  138. [Test]
  139. [ExpectedException (typeof (ArgumentException))]
  140. public void AddAttribute_Duplicate ()
  141. {
  142. elem.AddAttribute ("valid", "first time");
  143. elem.AddAttribute ("valid", "second time");
  144. }
  145. [Test]
  146. public void AddAttribute ()
  147. {
  148. elem.AddAttribute ("valid", "valid\'");
  149. }
  150. [Test]
  151. [ExpectedException (typeof (ArgumentNullException))]
  152. public void AddChild_Null ()
  153. {
  154. elem.AddChild (null);
  155. }
  156. [Test]
  157. public void AddChild ()
  158. {
  159. int n = elem.Children.Count;
  160. // add itself
  161. elem.AddChild (elem);
  162. AssertEquals ("Count", (n+1), elem.Children.Count);
  163. }
  164. [Test]
  165. [ExpectedException (typeof (ArgumentException))]
  166. [Category ("NotDotNet")] // this will result in an InvalidCastException on MS.Net - I have no clue why
  167. public void Attributes_StrangeCase ()
  168. {
  169. Hashtable h = elem.Attributes;
  170. h.Add ("<invalid>", "valid");
  171. elem.Attributes = h;
  172. }
  173. [Test]
  174. [ExpectedException (typeof (ArgumentException))]
  175. public void Attributes_ArgumentException ()
  176. {
  177. Hashtable h = elem.Attributes;
  178. h.Add ("valid", "\"invalid\"");
  179. elem.Attributes = h;
  180. }
  181. [Test]
  182. public void Attributes ()
  183. {
  184. Hashtable h = elem.Attributes;
  185. h = elem.Attributes;
  186. h.Add ("foo", "bar");
  187. Assert ("#1", elem.Attributes.Count != h.Count);
  188. elem.Attributes = h;
  189. AssertNotNull ("#2", elem.Attribute ("foo"));
  190. }
  191. [Test]
  192. public void Equal ()
  193. {
  194. int iTest = 0;
  195. SecurityElement elem2 = CreateElement ();
  196. iTest++;
  197. Assert ("#1", elem.Equal (elem2));
  198. iTest++;
  199. SecurityElement child = (SecurityElement) elem2.Children [0];
  200. iTest++;
  201. child = (SecurityElement) child.Children [1];
  202. iTest++;
  203. child.Text = "some text";
  204. iTest++;
  205. Assert ("#2", !elem.Equal (elem2));
  206. }
  207. [Test]
  208. public void Escape ()
  209. {
  210. AssertEquals ("#1", "foo&lt;&gt;&quot;&apos;&amp; bar", SecurityElement.Escape ("foo<>\"'& bar"));
  211. }
  212. [Test]
  213. public void IsValidAttributeName ()
  214. {
  215. Assert ("#1", !SecurityElement.IsValidAttributeName ("x x"));
  216. Assert ("#2", !SecurityElement.IsValidAttributeName ("x<x"));
  217. Assert ("#3", !SecurityElement.IsValidAttributeName ("x>x"));
  218. Assert ("#4", SecurityElement.IsValidAttributeName ("x\"x"));
  219. Assert ("#5", SecurityElement.IsValidAttributeName ("x'x"));
  220. Assert ("#6", SecurityElement.IsValidAttributeName ("x&x"));
  221. }
  222. [Test]
  223. public void IsValidAttributeValue ()
  224. {
  225. Assert ("#1", SecurityElement.IsValidAttributeValue ("x x"));
  226. Assert ("#2", !SecurityElement.IsValidAttributeValue ("x<x"));
  227. Assert ("#3", !SecurityElement.IsValidAttributeValue ("x>x"));
  228. Assert ("#4", !SecurityElement.IsValidAttributeValue ("x\"x"));
  229. Assert ("#5", SecurityElement.IsValidAttributeValue ("x'x"));
  230. Assert ("#6", SecurityElement.IsValidAttributeValue ("x&x"));
  231. }
  232. [Test]
  233. public void IsValidTag ()
  234. {
  235. Assert ("#1", !SecurityElement.IsValidTag ("x x"));
  236. Assert ("#2", !SecurityElement.IsValidTag ("x<x"));
  237. Assert ("#3", !SecurityElement.IsValidTag ("x>x"));
  238. Assert ("#4", SecurityElement.IsValidTag ("x\"x"));
  239. Assert ("#5", SecurityElement.IsValidTag ("x'x"));
  240. Assert ("#6", SecurityElement.IsValidTag ("x&x"));
  241. }
  242. [Test]
  243. public void IsValidText ()
  244. {
  245. Assert ("#1", SecurityElement.IsValidText ("x x"));
  246. Assert ("#2", !SecurityElement.IsValidText ("x<x"));
  247. Assert ("#3", !SecurityElement.IsValidText ("x>x"));
  248. Assert ("#4", SecurityElement.IsValidText ("x\"x"));
  249. Assert ("#5", SecurityElement.IsValidText ("x'x"));
  250. Assert ("#6", SecurityElement.IsValidText ("x&x"));
  251. }
  252. [Test]
  253. [ExpectedException (typeof (ArgumentNullException))]
  254. public void SearchForChildByTag_Null ()
  255. {
  256. SecurityElement child = elem.SearchForChildByTag (null);
  257. }
  258. [Test]
  259. public void SearchForChildByTag ()
  260. {
  261. SecurityElement child = elem.SearchForChildByTag ("doesnotexist");
  262. AssertNull ("#1", child);
  263. child = elem.SearchForChildByTag ("ENDPOINT");
  264. AssertNull ("#2", child);
  265. child = (SecurityElement) elem.Children [0];
  266. child = child.SearchForChildByTag ("ENDPOINT");
  267. AssertEquals ("#3", "All", child.Attribute ("transport"));
  268. }
  269. [Test]
  270. [ExpectedException (typeof (ArgumentNullException))]
  271. public void SearchForTextOfTag_Null ()
  272. {
  273. string s = elem.SearchForTextOfTag (null);
  274. }
  275. [Test]
  276. public void SearchForTextOfTag ()
  277. {
  278. string s = elem.SearchForTextOfTag ("ENDPOINT");
  279. AssertEquals ("SearchForTextOfTag", "some text", s);
  280. }
  281. [Test]
  282. [ExpectedException (typeof (ArgumentNullException))]
  283. public void Tag_Null ()
  284. {
  285. elem.Tag = null;
  286. AssertNull ("Tag", elem.Tag);
  287. }
  288. [Test]
  289. public void Text_Null ()
  290. {
  291. elem.Text = null;
  292. AssertNull ("Text", elem.Text);
  293. }
  294. [Test]
  295. public void MultipleAttributes ()
  296. {
  297. SecurityElement se = new SecurityElement ("Multiple");
  298. se.AddAttribute ("Attribute1", "One");
  299. se.AddAttribute ("Attribute2", "Two");
  300. #if NET_2_0
  301. string expected = String.Format ("<Multiple Attribute1=\"One\"{0}Attribute2=\"Two\"/>{0}", Environment.NewLine);
  302. #else
  303. string expected = String.Format ("<Multiple Attribute1=\"One\"{0} Attribute2=\"Two\"/>{0}", Environment.NewLine);
  304. #endif
  305. AssertEquals ("ToString()", expected, se.ToString ());
  306. }
  307. #if NET_2_0
  308. [Test]
  309. public void Copy ()
  310. {
  311. SecurityElement se = SecurityElement.FromString ("<tag attribute=\"value\"><child attr=\"1\">mono</child><child/></tag>");
  312. SecurityElement copy = se.Copy ();
  313. Assert ("se!ReferenceEquals", !Object.ReferenceEquals (se, copy));
  314. Assert ("c1=ReferenceEquals", Object.ReferenceEquals (se.Children [0], copy.Children [0]));
  315. Assert ("c2=ReferenceEquals", Object.ReferenceEquals (se.Children [1], copy.Children [1]));
  316. }
  317. [Test]
  318. [ExpectedException (typeof (ArgumentNullException))]
  319. public void FromString_Null ()
  320. {
  321. SecurityElement.FromString (null);
  322. }
  323. [Test]
  324. [ExpectedException (typeof (XmlSyntaxException))]
  325. public void FromString_Empty ()
  326. {
  327. SecurityElement.FromString (String.Empty);
  328. }
  329. [Test]
  330. [ExpectedException (typeof (XmlSyntaxException))]
  331. public void FromString_NonXml ()
  332. {
  333. SecurityElement.FromString ("mono");
  334. }
  335. [Test]
  336. public void FromString ()
  337. {
  338. SecurityElement se = SecurityElement.FromString ("<tag attribute=\"value\"><child attr=\"1\">mono</child><child/></tag>");
  339. AssertEquals ("Tag", "tag", se.Tag);
  340. AssertNull ("Text", se.Text);
  341. AssertEquals ("Attribute.Count", 1, se.Attributes.Count);
  342. AssertEquals ("Attribute", "value", se.Attribute ("attribute"));
  343. AssertEquals ("Children.Count", 2, se.Children.Count);
  344. SecurityElement child = (SecurityElement) se.Children [0];
  345. AssertEquals ("Child1.Tag", "child", child.Tag);
  346. AssertEquals ("Child1.Text", "mono", child.Text);
  347. AssertEquals ("Child1.Attribute.Count", 1, child.Attributes.Count);
  348. AssertEquals ("Child1.Attribute", "1", child.Attribute ("attr"));
  349. child = (SecurityElement) se.Children [1];
  350. AssertEquals ("Child2.Tag", "child", child.Tag);
  351. AssertNull ("Child2.Text", child.Text);
  352. AssertNull ("Child2.Attribute", child.Attributes);
  353. }
  354. #endif
  355. }
  356. }