SecurityElementTest.cs 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754
  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 System;
  31. using System.Collections;
  32. using System.Globalization;
  33. using System.Security;
  34. using NUnit.Framework;
  35. namespace MonoTests.System.Security {
  36. [TestFixture]
  37. public class SecurityElementTest {
  38. SecurityElement elem;
  39. [SetUp]
  40. public void SetUp ()
  41. {
  42. elem = CreateElement ();
  43. }
  44. private SecurityElement CreateElement ()
  45. {
  46. SecurityElement elem = new SecurityElement ("IPermission");
  47. elem.AddAttribute ("class", "System");
  48. elem.AddAttribute ("version", "1");
  49. SecurityElement child = new SecurityElement ("ConnectAccess");
  50. elem.AddChild (child);
  51. SecurityElement grandchild = new SecurityElement ("ENDPOINT", "some text");
  52. grandchild.AddAttribute ("transport", "All");
  53. grandchild.AddAttribute ("host", "localhost");
  54. grandchild.AddAttribute ("port", "8080");
  55. child.AddChild (grandchild);
  56. SecurityElement grandchild2 = new SecurityElement ("ENDPOINT");
  57. grandchild2.AddAttribute ("transport", "Tcp");
  58. grandchild2.AddAttribute ("host", "www.ximian.com");
  59. grandchild2.AddAttribute ("port", "All");
  60. child.AddChild (grandchild2);
  61. return elem;
  62. }
  63. [Test]
  64. public void Constructor1 ()
  65. {
  66. SecurityElement se = new SecurityElement ("tag");
  67. Assert.IsNull (se.Attributes, "#A1");
  68. Assert.IsNull (se.Children, "#A2");
  69. Assert.AreEqual ("tag", se.Tag, "#A3");
  70. Assert.IsNull (se.Text, "#A4");
  71. se = new SecurityElement (string.Empty);
  72. Assert.IsNull (se.Attributes, "#B1");
  73. Assert.IsNull (se.Children, "#B2");
  74. Assert.AreEqual (string.Empty, se.Tag, "#B3");
  75. Assert.IsNull (se.Text, "#B4");
  76. }
  77. [Test]
  78. public void Constructor1_Tag_Invalid ()
  79. {
  80. try {
  81. new SecurityElement ("Na<me");
  82. Assert.Fail ("#A1");
  83. } catch (ArgumentException ex) {
  84. // Invalid element tag Nam<e
  85. Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#A2");
  86. Assert.IsNull (ex.InnerException, "#A3");
  87. Assert.IsNotNull (ex.Message, "#A4");
  88. Assert.IsTrue (ex.Message.IndexOf ("Na<me") != -1, "#A5");
  89. Assert.IsNull (ex.ParamName, "#A6");
  90. }
  91. try {
  92. new SecurityElement ("Nam>e");
  93. Assert.Fail ("#B1");
  94. } catch (ArgumentException ex) {
  95. // Invalid element tag Nam>e
  96. Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#B2");
  97. Assert.IsNull (ex.InnerException, "#B3");
  98. Assert.IsNotNull (ex.Message, "#B4");
  99. Assert.IsTrue (ex.Message.IndexOf ("Nam>e") != -1, "#B5");
  100. Assert.IsNull (ex.ParamName, "#B6");
  101. }
  102. }
  103. [Test]
  104. public void Constructor1_Tag_Null ()
  105. {
  106. try {
  107. new SecurityElement (null);
  108. Assert.Fail ("#1");
  109. } catch (ArgumentNullException ex) {
  110. Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
  111. Assert.IsNull (ex.InnerException, "#3");
  112. Assert.IsNotNull (ex.Message, "#4");
  113. Assert.IsNotNull (ex.ParamName, "#5");
  114. Assert.AreEqual ("tag", ex.ParamName, "#6");
  115. }
  116. }
  117. [Test]
  118. public void Constructor2 ()
  119. {
  120. SecurityElement se = new SecurityElement ("tag", "text");
  121. Assert.IsNull (se.Attributes, "EmptyAttributes");
  122. Assert.IsNull (se.Children, "EmptyChildren");
  123. Assert.AreEqual ("tag", se.Tag, "Tag");
  124. Assert.AreEqual ("text", se.Text, "Text");
  125. }
  126. [Test]
  127. public void Constructor2_Tag_Invalid ()
  128. {
  129. try {
  130. new SecurityElement ("Na<me", "text");
  131. Assert.Fail ("#A1");
  132. } catch (ArgumentException ex) {
  133. // Invalid element tag Nam<e
  134. Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#A2");
  135. Assert.IsNull (ex.InnerException, "#A3");
  136. Assert.IsNotNull (ex.Message, "#A4");
  137. Assert.IsTrue (ex.Message.IndexOf ("Na<me") != -1, "#A5");
  138. Assert.IsNull (ex.ParamName, "#A6");
  139. }
  140. try {
  141. new SecurityElement ("Nam>e", "text");
  142. Assert.Fail ("#B1");
  143. } catch (ArgumentException ex) {
  144. // Invalid element tag Nam>e
  145. Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#B2");
  146. Assert.IsNull (ex.InnerException, "#B3");
  147. Assert.IsNotNull (ex.Message, "#B4");
  148. Assert.IsTrue (ex.Message.IndexOf ("Nam>e") != -1, "#B5");
  149. Assert.IsNull (ex.ParamName, "#B6");
  150. }
  151. }
  152. [Test]
  153. public void Constructor2_Tag_Null ()
  154. {
  155. try {
  156. new SecurityElement (null, "text");
  157. Assert.Fail ("#1");
  158. } catch (ArgumentNullException ex) {
  159. Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
  160. Assert.IsNull (ex.InnerException, "#3");
  161. Assert.IsNotNull (ex.Message, "#4");
  162. Assert.IsNotNull (ex.ParamName, "#5");
  163. Assert.AreEqual ("tag", ex.ParamName, "#6");
  164. }
  165. }
  166. [Test]
  167. public void Constructor2_Text_Null ()
  168. {
  169. SecurityElement se = new SecurityElement ("tag", null);
  170. Assert.IsNull (se.Attributes, "EmptyAttributes");
  171. Assert.IsNull (se.Children, "EmptyChildren");
  172. Assert.AreEqual ("tag", se.Tag, "Tag");
  173. Assert.IsNull (se.Text, "Text");
  174. }
  175. [Test]
  176. public void AddAttribute_Name_Null ()
  177. {
  178. try {
  179. elem.AddAttribute (null, "valid");
  180. Assert.Fail ("#1");
  181. } catch (ArgumentNullException ex) {
  182. Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
  183. Assert.IsNull (ex.InnerException, "#3");
  184. Assert.IsNotNull (ex.Message, "#4");
  185. Assert.IsNotNull (ex.ParamName, "#5");
  186. Assert.AreEqual ("name", ex.ParamName, "#6");
  187. }
  188. }
  189. [Test]
  190. public void AddAttribute_Value_Null ()
  191. {
  192. try {
  193. elem.AddAttribute ("valid", null);
  194. Assert.Fail ("#1");
  195. } catch (ArgumentNullException ex) {
  196. Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
  197. Assert.IsNull (ex.InnerException, "#3");
  198. Assert.IsNotNull (ex.Message, "#4");
  199. Assert.IsNotNull (ex.ParamName, "#5");
  200. Assert.AreEqual ("value", ex.ParamName, "#6");
  201. }
  202. }
  203. [Test]
  204. [ExpectedException (typeof (ArgumentException))]
  205. public void AddAttribute_Name_Invalid ()
  206. {
  207. elem.AddAttribute ("<invalid>", "valid");
  208. }
  209. [Test]
  210. [ExpectedException (typeof (ArgumentException))]
  211. public void AddAttribute_Value_Invalid ()
  212. {
  213. elem.AddAttribute ("valid", "invalid\"");
  214. }
  215. [Test]
  216. public void AddAttribute_InvalidValue2 ()
  217. {
  218. elem.AddAttribute ("valid", "valid&");
  219. // in xml world this is actually not considered valid
  220. // but it is by MS.Net
  221. }
  222. [Test]
  223. [ExpectedException (typeof (ArgumentException))]
  224. public void AddAttribute_InvalidValue3 ()
  225. {
  226. elem.AddAttribute ("valid", "<invalid>");
  227. }
  228. [Test]
  229. [ExpectedException (typeof (ArgumentException))]
  230. public void AddAttribute_Duplicate ()
  231. {
  232. elem.AddAttribute ("valid", "first time");
  233. elem.AddAttribute ("valid", "second time");
  234. }
  235. [Test]
  236. public void AddAttribute ()
  237. {
  238. elem.AddAttribute ("valid", "valid\'");
  239. }
  240. [Test]
  241. public void AddChild_Null ()
  242. {
  243. try {
  244. elem.AddChild (null);
  245. Assert.Fail ("#1");
  246. } catch (ArgumentNullException ex) {
  247. Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
  248. Assert.IsNull (ex.InnerException, "#3");
  249. Assert.IsNotNull (ex.Message, "#4");
  250. Assert.IsNotNull (ex.ParamName, "#5");
  251. Assert.AreEqual ("child", ex.ParamName, "#6");
  252. }
  253. }
  254. [Test]
  255. public void AddChild ()
  256. {
  257. int n = elem.Children.Count;
  258. // add itself
  259. elem.AddChild (elem);
  260. Assert.AreEqual ((n + 1), elem.Children.Count, "Count");
  261. }
  262. [Test]
  263. [Category ("NotDotNet")] // MS bug: https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=304549
  264. public void Attributes_Name_Invalid ()
  265. {
  266. Hashtable h = elem.Attributes;
  267. h.Add ("<invalid>", "valid");
  268. try {
  269. elem.Attributes = h;
  270. Assert.Fail ("#1");
  271. } catch (ArgumentException ex) {
  272. // Invalid attribute name '<invalid>'
  273. Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
  274. Assert.IsNull (ex.InnerException, "#3");
  275. Assert.IsNotNull (ex.Message, "#4");
  276. Assert.IsTrue (ex.Message.IndexOf ("<invalid>") != -1, "#5");
  277. Assert.IsNull (ex.ParamName, "#6");
  278. }
  279. }
  280. [Test]
  281. [Category ("NotWorking")] // MS bug: https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=304549
  282. public void Attributes_Name_Invalid_MS ()
  283. {
  284. Hashtable h = elem.Attributes;
  285. h.Add ("<invalid>", "valid");
  286. try {
  287. elem.Attributes = h;
  288. Assert.Fail ();
  289. } catch (InvalidCastException) {
  290. }
  291. }
  292. [Test]
  293. public void Attributes_Value_Invalid ()
  294. {
  295. Hashtable h = elem.Attributes;
  296. h.Add ("valid", "\"invalid\"");
  297. try {
  298. elem.Attributes = h;
  299. Assert.Fail ("#1");
  300. } catch (ArgumentException ex) {
  301. // Invalid attribute value '"invalid"'
  302. Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
  303. Assert.IsNull (ex.InnerException, "#3");
  304. Assert.IsNotNull (ex.Message, "#4");
  305. Assert.IsTrue (ex.Message.IndexOf ("\"invalid\"") != -1, "#5");
  306. Assert.IsNull (ex.ParamName, "#6");
  307. }
  308. }
  309. [Test]
  310. public void Attributes ()
  311. {
  312. Hashtable h = elem.Attributes;
  313. h = elem.Attributes;
  314. h.Add ("foo", "bar");
  315. Assert.IsTrue (elem.Attributes.Count != h.Count, "#1");
  316. elem.Attributes = h;
  317. Assert.IsNotNull (elem.Attribute ("foo"), "#2");
  318. }
  319. [Test]
  320. public void Equal ()
  321. {
  322. int iTest = 0;
  323. SecurityElement elem2 = CreateElement ();
  324. iTest++;
  325. Assert.IsTrue (elem.Equal (elem2), "#1");
  326. iTest++;
  327. SecurityElement child = (SecurityElement) elem2.Children [0];
  328. iTest++;
  329. child = (SecurityElement) child.Children [1];
  330. iTest++;
  331. child.Text = "some text";
  332. iTest++;
  333. Assert.IsFalse (elem.Equal (elem2), "#2");
  334. }
  335. [Test]
  336. public void Escape ()
  337. {
  338. Assert.AreEqual ("foo&lt;&gt;&quot;&apos;&amp; bar",
  339. SecurityElement.Escape ("foo<>\"'& bar"), "#1");
  340. Assert.IsNull (SecurityElement.Escape (null), "#2");
  341. }
  342. [Test]
  343. public void IsValidAttributeName ()
  344. {
  345. Assert.IsFalse (SecurityElement.IsValidAttributeName ("x x"), "#1");
  346. Assert.IsFalse (SecurityElement.IsValidAttributeName ("x<x"), "#2");
  347. Assert.IsFalse (SecurityElement.IsValidAttributeName ("x>x"), "#3");
  348. Assert.IsTrue (SecurityElement.IsValidAttributeName ("x\"x"), "#4");
  349. Assert.IsTrue (SecurityElement.IsValidAttributeName ("x'x"), "#5");
  350. Assert.IsTrue (SecurityElement.IsValidAttributeName ("x&x"), "#6");
  351. Assert.IsFalse (SecurityElement.IsValidAttributeName (null), "#7");
  352. Assert.IsTrue (SecurityElement.IsValidAttributeName (string.Empty), "#8");
  353. }
  354. [Test]
  355. public void IsValidAttributeValue ()
  356. {
  357. Assert.IsTrue (SecurityElement.IsValidAttributeValue ("x x"), "#1");
  358. Assert.IsFalse (SecurityElement.IsValidAttributeValue ("x<x"), "#2");
  359. Assert.IsFalse (SecurityElement.IsValidAttributeValue ("x>x"), "#3");
  360. Assert.IsFalse (SecurityElement.IsValidAttributeValue ("x\"x"), "#4");
  361. Assert.IsTrue (SecurityElement.IsValidAttributeValue ("x'x"), "#5");
  362. Assert.IsTrue (SecurityElement.IsValidAttributeValue ("x&x"), "#6");
  363. Assert.IsFalse (SecurityElement.IsValidAttributeValue (null), "#7");
  364. Assert.IsTrue (SecurityElement.IsValidAttributeValue (string.Empty), "#8");
  365. }
  366. [Test]
  367. public void IsValidTag ()
  368. {
  369. Assert.IsFalse (SecurityElement.IsValidTag ("x x"), "#1");
  370. Assert.IsFalse (SecurityElement.IsValidTag ("x<x"), "#2");
  371. Assert.IsFalse (SecurityElement.IsValidTag ("x>x"), "#3");
  372. Assert.IsTrue (SecurityElement.IsValidTag ("x\"x"), "#4");
  373. Assert.IsTrue (SecurityElement.IsValidTag ("x'x"), "#5");
  374. Assert.IsTrue (SecurityElement.IsValidTag ("x&x"), "#6");
  375. Assert.IsFalse (SecurityElement.IsValidTag (null), "#7");
  376. Assert.IsTrue (SecurityElement.IsValidTag (string.Empty), "#8");
  377. }
  378. [Test]
  379. public void IsValidText ()
  380. {
  381. Assert.IsTrue (SecurityElement.IsValidText ("x x"), "#1");
  382. Assert.IsFalse (SecurityElement.IsValidText ("x<x"), "#2");
  383. Assert.IsFalse (SecurityElement.IsValidText ("x>x"), "#3");
  384. Assert.IsTrue (SecurityElement.IsValidText ("x\"x"), "#4");
  385. Assert.IsTrue (SecurityElement.IsValidText ("x'x"), "#5");
  386. Assert.IsTrue (SecurityElement.IsValidText ("x&x"), "#6");
  387. Assert.IsFalse (SecurityElement.IsValidText (null), "#7");
  388. Assert.IsTrue (SecurityElement.IsValidText (string.Empty), "#8");
  389. }
  390. [Test]
  391. public void SearchForChildByTag_Null ()
  392. {
  393. try {
  394. elem.SearchForChildByTag (null);
  395. Assert.Fail ("#1");
  396. } catch (ArgumentNullException ex) {
  397. Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
  398. Assert.IsNull (ex.InnerException, "#3");
  399. Assert.IsNotNull (ex.Message, "#4");
  400. Assert.IsNotNull (ex.ParamName, "#5");
  401. Assert.AreEqual ("tag", ex.ParamName, "#6");
  402. }
  403. }
  404. [Test]
  405. public void SearchForChildByTag ()
  406. {
  407. SecurityElement child = elem.SearchForChildByTag ("doesnotexist");
  408. Assert.IsNull (child, "#1");
  409. child = elem.SearchForChildByTag ("ENDPOINT");
  410. Assert.IsNull (child, "#2");
  411. child = (SecurityElement) elem.Children [0];
  412. child = child.SearchForChildByTag ("ENDPOINT");
  413. Assert.AreEqual ("All", child.Attribute ("transport"), "#3");
  414. }
  415. [Test]
  416. public void SearchForTextOfTag_Tag_Null ()
  417. {
  418. try {
  419. elem.SearchForTextOfTag (null);
  420. Assert.Fail ("#1");
  421. } catch (ArgumentNullException ex) {
  422. Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
  423. Assert.IsNull (ex.InnerException, "#3");
  424. Assert.IsNotNull (ex.Message, "#4");
  425. Assert.IsNotNull (ex.ParamName, "#5");
  426. Assert.AreEqual ("tag", ex.ParamName, "#6");
  427. }
  428. }
  429. [Test]
  430. public void SearchForTextOfTag ()
  431. {
  432. string s = elem.SearchForTextOfTag ("ENDPOINT");
  433. Assert.AreEqual ("some text", s);
  434. }
  435. [Test]
  436. public void Tag ()
  437. {
  438. SecurityElement se = new SecurityElement ("Values");
  439. Assert.AreEqual ("Values", se.Tag, "#A1");
  440. Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
  441. "<Values/>{0}", Environment.NewLine),
  442. se.ToString (), "#A2");
  443. se.Tag = "abc:Name";
  444. Assert.AreEqual ("abc:Name", se.Tag, "#B1");
  445. Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
  446. "<abc:Name/>{0}", Environment.NewLine),
  447. se.ToString (), "#B2");
  448. se.Tag = "Name&Address";
  449. Assert.AreEqual ("Name&Address", se.Tag, "#C1");
  450. Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
  451. "<Name&Address/>{0}", Environment.NewLine),
  452. se.ToString (), "#C2");
  453. se.Tag = string.Empty;
  454. Assert.AreEqual (string.Empty, se.Tag, "#D1");
  455. Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
  456. "</>{0}", Environment.NewLine),
  457. se.ToString (), "#D2");
  458. }
  459. [Test]
  460. public void Tag_Invalid ()
  461. {
  462. SecurityElement se = new SecurityElement ("Values");
  463. try {
  464. se.Tag = "Na<me";
  465. Assert.Fail ("#A1");
  466. } catch (ArgumentException ex) {
  467. // Invalid element tag Nam<e
  468. Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#A2");
  469. Assert.IsNull (ex.InnerException, "#A3");
  470. Assert.IsNotNull (ex.Message, "#A4");
  471. Assert.IsTrue (ex.Message.IndexOf ("Na<me") != -1, "#A5");
  472. Assert.IsNull (ex.ParamName, "#A6");
  473. }
  474. try {
  475. se.Tag = "Nam>e";
  476. Assert.Fail ("#B1");
  477. } catch (ArgumentException ex) {
  478. // Invalid element tag Nam>e
  479. Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#B2");
  480. Assert.IsNull (ex.InnerException, "#B3");
  481. Assert.IsNotNull (ex.Message, "#B4");
  482. Assert.IsTrue (ex.Message.IndexOf ("Nam>e") != -1, "#B5");
  483. Assert.IsNull (ex.ParamName, "#B6");
  484. }
  485. }
  486. [Test]
  487. public void Tag_Null ()
  488. {
  489. try {
  490. elem.Tag = null;
  491. Assert.Fail ("#1");
  492. } catch (ArgumentNullException ex) {
  493. Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
  494. Assert.IsNull (ex.InnerException, "#3");
  495. Assert.IsNotNull (ex.Message, "#4");
  496. Assert.IsNotNull (ex.ParamName, "#5");
  497. Assert.AreEqual ("Tag", ex.ParamName, "#6");
  498. }
  499. }
  500. [Test]
  501. public void Text ()
  502. {
  503. elem.Text = "Miguel&Sébastien";
  504. Assert.AreEqual ("Miguel&Sébastien", elem.Text, "#1");
  505. elem.Text = null;
  506. Assert.IsNull (elem.Text, "#2");
  507. elem.Text = "Sébastien\"Miguel";
  508. Assert.AreEqual ("Sébastien\"Miguel", elem.Text, "#3");
  509. elem.Text = string.Empty;
  510. Assert.AreEqual (string.Empty, elem.Text, "#4");
  511. elem.Text = "&lt;sample&amp;practice&unresolved;&gt;";
  512. Assert.AreEqual ("<sample&practice&unresolved;>", elem.Text, "#5");
  513. }
  514. [Test]
  515. public void Text_Invalid ()
  516. {
  517. try {
  518. elem.Text = "Mig<uelSébastien";
  519. Assert.Fail ("#A1");
  520. } catch (ArgumentException ex) {
  521. // Invalid element tag Mig<uelSébastien
  522. Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#A2");
  523. Assert.IsNull (ex.InnerException, "#A3");
  524. Assert.IsNotNull (ex.Message, "#A4");
  525. Assert.IsTrue (ex.Message.IndexOf ("Mig<uelSébastien") != -1, "#A5");
  526. Assert.IsNull (ex.ParamName, "#A6");
  527. }
  528. try {
  529. elem.Text = "Mig>uelSébastien";
  530. Assert.Fail ("#B1");
  531. } catch (ArgumentException ex) {
  532. // Invalid element tag Mig>uelSébastien
  533. Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#B2");
  534. Assert.IsNull (ex.InnerException, "#B3");
  535. Assert.IsNotNull (ex.Message, "#B4");
  536. Assert.IsTrue (ex.Message.IndexOf ("Mig>uelSébastien") != -1, "#B5");
  537. Assert.IsNull (ex.ParamName, "#B6");
  538. }
  539. }
  540. [Test]
  541. public void MultipleAttributes ()
  542. {
  543. SecurityElement se = new SecurityElement ("Multiple");
  544. se.AddAttribute ("Attribute1", "One");
  545. se.AddAttribute ("Attribute2", "Two");
  546. string expected = String.Format ("<Multiple Attribute1=\"One\"{0}Attribute2=\"Two\"/>{0}", Environment.NewLine);
  547. Assert.AreEqual (expected, se.ToString (), "ToString()");
  548. }
  549. [Test]
  550. public void Copy ()
  551. {
  552. SecurityElement se = SecurityElement.FromString ("<tag attribute=\"value\"><child attr=\"1\">mono</child><child/></tag>");
  553. SecurityElement copy = se.Copy ();
  554. Assert.IsFalse (Object.ReferenceEquals (se, copy), "se!ReferenceEquals");
  555. Assert.IsTrue (Object.ReferenceEquals (se.Children [0], copy.Children [0]), "c1=ReferenceEquals");
  556. Assert.IsTrue (Object.ReferenceEquals (se.Children [1], copy.Children [1]), "c2=ReferenceEquals");
  557. }
  558. [Test]
  559. public void FromString_Null ()
  560. {
  561. try {
  562. SecurityElement.FromString (null);
  563. Assert.Fail ("#1");
  564. } catch (ArgumentNullException ex) {
  565. Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
  566. Assert.IsNull (ex.InnerException, "#3");
  567. Assert.IsNotNull (ex.Message, "#4");
  568. Assert.IsNotNull (ex.ParamName, "#5");
  569. Assert.AreEqual ("xml", ex.ParamName, "#6");
  570. }
  571. }
  572. [Test]
  573. [ExpectedException (typeof (XmlSyntaxException))]
  574. public void FromString_Empty ()
  575. {
  576. SecurityElement.FromString (String.Empty);
  577. }
  578. [Test]
  579. [ExpectedException (typeof (XmlSyntaxException))]
  580. public void FromString_NonXml ()
  581. {
  582. SecurityElement.FromString ("mono");
  583. }
  584. [Test]
  585. public void FromString ()
  586. {
  587. SecurityElement se = SecurityElement.FromString ("<tag attribute=\"value\"><x:child attr=\"1\">mono</x:child><child/></tag>");
  588. Assert.AreEqual ("tag", se.Tag, "#A1");
  589. Assert.IsNull (se.Text, "#A2");
  590. Assert.AreEqual (1, se.Attributes.Count, "#A3");
  591. Assert.AreEqual ("value", se.Attribute ("attribute"), "#A4");
  592. Assert.AreEqual (2, se.Children.Count, "#A5");
  593. SecurityElement child = (SecurityElement) se.Children [0];
  594. Assert.AreEqual ("x:child", child.Tag, "#B1");
  595. Assert.AreEqual ("mono", child.Text, "#B2");
  596. Assert.AreEqual (1, child.Attributes.Count, "#B3");
  597. Assert.AreEqual ("1", child.Attribute ("attr"), "#B4");
  598. child = (SecurityElement) se.Children [1];
  599. Assert.AreEqual ("child", child.Tag, "#C1");
  600. Assert.IsNull (child.Text, "#C2");
  601. Assert.IsNull (child.Attributes, "#C3");
  602. }
  603. [Test]
  604. [Category ("NotDotNet")] // MS bug: https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=304583
  605. public void FromString_Quote_Delimiter ()
  606. {
  607. const string xml = "<value name='Company'>Novell</value>";
  608. SecurityElement se = SecurityElement.FromString (xml);
  609. Assert.AreEqual ("Company", se.Attribute ("name"), "#1");
  610. Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
  611. "<value name=\"Company\">Novell</value>{0}",
  612. Environment.NewLine), se.ToString (), "#2");
  613. }
  614. [Test]
  615. [Category ("NotWorking")] // MS bug: https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=304583
  616. public void FromString_Quote_Delimiter_MS ()
  617. {
  618. const string xml = "<value name='Company'>Novell</value>";
  619. SecurityElement se = SecurityElement.FromString (xml);
  620. Assert.AreEqual ("'Company'", se.Attribute ("name"), "#1");
  621. Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
  622. "<value name=\"'Company'\">Novell</value>{0}",
  623. Environment.NewLine), se.ToString (), "#2");
  624. }
  625. [Test] // bug #333699
  626. public void FromString_EntityReferences ()
  627. {
  628. const string xml = @"
  629. <values>
  630. <value name=""&quot;name&quot;&amp;&lt;address&gt;"">&lt;&apos;Suds&apos; &amp; &quot;Soda&quot;&gt;!</value>
  631. </values>";
  632. SecurityElement se = SecurityElement.FromString (xml);
  633. Assert.IsNotNull (se, "#A1");
  634. Assert.IsNull (se.Attributes, "#A2");
  635. Assert.IsNotNull (se.Children, "#A3");
  636. Assert.AreEqual (1, se.Children.Count, "#A4");
  637. Assert.AreEqual ("values", se.Tag, "#A5");
  638. Assert.IsNull (se.Text, "#A6");
  639. SecurityElement child = se.Children [0] as SecurityElement;
  640. Assert.IsNotNull (child, "#B1");
  641. Assert.IsNotNull (child.Attributes, "#B2");
  642. Assert.AreEqual ("\"name\"&<address>", child.Attribute ("name"), "#B3");
  643. Assert.AreEqual ("value", child.Tag, "#B4");
  644. Assert.AreEqual ("<'Suds' & \"Soda\">!", child.Text, "#B5");
  645. Assert.IsNull (child.Children, "#B6");
  646. }
  647. [Test] // bug #
  648. [Category ("NotWorking")]
  649. public void FromString_CharacterReferences ()
  650. {
  651. const string xml = @"
  652. <value name=""name&#38;address"">Suds&#x26;Soda&#38;</value>";
  653. SecurityElement se = SecurityElement.FromString (xml);
  654. Assert.IsNotNull (se, "#1");
  655. Assert.IsNotNull (se.Attributes, "#2");
  656. Assert.AreEqual ("name&#38;address", se.Attribute ("name"), "#3");
  657. Assert.AreEqual ("value", se.Tag, "#4");
  658. Assert.AreEqual ("Suds&#x26;Soda&#38;", se.Text, "#5");
  659. Assert.IsNull (se.Children, "#6");
  660. }
  661. [Test] // bug #333699 (ugh, mostly a dup)
  662. public void TestToString ()
  663. {
  664. SecurityElement values = new SecurityElement ("values");
  665. SecurityElement infoValue = new SecurityElement ("value");
  666. infoValue.AddAttribute ("name", "string");
  667. infoValue.Text = SecurityElement.Escape ("<'Suds' & \"Soda\">!");
  668. values.AddChild (infoValue);
  669. Assert.AreEqual ("<value name=\"string\">&lt;&apos;Suds&apos; &amp; &quot;Soda&quot;&gt;!</value>" + Environment.NewLine, infoValue.ToString (), "#1");
  670. Assert.AreEqual ("<'Suds' & \"Soda\">!", infoValue.Text, "#2");
  671. Assert.IsNull (values.Text, "#3");
  672. Assert.AreEqual (String.Format ("<values>{0}<value name=\"string\">&lt;&apos;Suds&apos; &amp; &quot;Soda&quot;&gt;!</value>{0}</values>{0}", Environment.NewLine), values.ToString (), "#4");
  673. SecurityElement sec = SecurityElement.FromString (values.ToString ());
  674. Assert.AreEqual (1, sec.Children.Count, "#5");
  675. Assert.AreEqual ("<'Suds' & \"Soda\">!", ((SecurityElement) sec.Children [0]).Text, "#6");
  676. }
  677. }
  678. }