SecurityElementTest.cs 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. //
  2. // SecurityElementTest.cs - NUnit Test Cases for System.Security.SecurityElement
  3. //
  4. // Author:
  5. // Lawrence Pit ([email protected])
  6. //
  7. using NUnit.Framework;
  8. using System;
  9. using System.Collections;
  10. using System.Security;
  11. namespace MonoTests.System.Security
  12. {
  13. public class SecurityElementTest : TestCase
  14. {
  15. SecurityElement elem;
  16. protected override void SetUp ()
  17. {
  18. elem = CreateElement ();
  19. }
  20. protected override void TearDown () {}
  21. private SecurityElement CreateElement ()
  22. {
  23. SecurityElement elem = new SecurityElement ("IPermission");
  24. elem.AddAttribute ("class", "System");
  25. elem.AddAttribute ("version", "1");
  26. SecurityElement child = new SecurityElement ("ConnectAccess");
  27. elem.AddChild (child);
  28. SecurityElement grandchild = new SecurityElement ("ENDPOINT", "some text");
  29. grandchild.AddAttribute ("transport", "All");
  30. grandchild.AddAttribute ("host", "localhost");
  31. grandchild.AddAttribute ("port", "8080");
  32. child.AddChild (grandchild);
  33. SecurityElement grandchild2 = new SecurityElement ("ENDPOINT");
  34. grandchild2.AddAttribute ("transport", "Tcp");
  35. grandchild2.AddAttribute ("host", "www.ximian.com");
  36. grandchild2.AddAttribute ("port", "All");
  37. child.AddChild (grandchild2);
  38. return elem;
  39. }
  40. public void TestConstructors ()
  41. {
  42. }
  43. public void TestAddAttribute ()
  44. {
  45. try {
  46. elem.AddAttribute (null, "valid");
  47. Fail ("#1");
  48. } catch (ArgumentNullException) { }
  49. try {
  50. elem.AddAttribute ("valid", null);
  51. Fail ("#2");
  52. } catch (ArgumentNullException) { }
  53. try {
  54. elem.AddAttribute ("<invalid>", "valid");
  55. Fail ("#3");
  56. } catch (ArgumentException) { }
  57. try {
  58. elem.AddAttribute ("valid", "invalid\"");
  59. Fail ("#4");
  60. } catch (ArgumentException) { }
  61. try {
  62. elem.AddAttribute ("valid", "valid\'");
  63. } catch (ArgumentException) { Fail ("#5"); }
  64. try {
  65. elem.AddAttribute ("valid", "valid&");
  66. Fail ("#6");
  67. // in xml world this is actually not considered valid
  68. // but it is by MS.Net
  69. } catch (ArgumentException) { }
  70. try {
  71. elem.AddAttribute ("valid", "<invalid>");
  72. Fail ("#7");
  73. } catch (ArgumentException) { }
  74. }
  75. public void TestAttributes ()
  76. {
  77. Hashtable h = elem.Attributes;
  78. /*
  79. // this will result in an InvalidCastException on MS.Net
  80. // I have no clue why
  81. h.Add ("<invalid>", "valid");
  82. try {
  83. elem.Attributes = h;
  84. Fail ("#1");
  85. } catch (ArgumentException) { }
  86. */
  87. h = elem.Attributes;
  88. h.Add ("valid", "\"invalid\"");
  89. try {
  90. elem.Attributes = h;
  91. Fail ("#2");
  92. } catch (ArgumentException) { }
  93. h = elem.Attributes;
  94. h.Add ("foo", "bar");
  95. Assert ("#3", elem.Attributes.Count != h.Count);
  96. elem.Attributes = h;
  97. Assert ("#4", elem.Attribute ("foo") != null);
  98. }
  99. public void TestEqual ()
  100. {
  101. int iTest = 0;
  102. try {
  103. SecurityElement elem2 = CreateElement ();
  104. iTest++;
  105. Assert ("#1", elem.Equal (elem2));
  106. iTest++;
  107. SecurityElement child = (SecurityElement) elem2.Children [0];
  108. iTest++;
  109. child = (SecurityElement) child.Children [1];
  110. iTest++;
  111. child.Text = "some text";
  112. iTest++;
  113. Assert ("#2", !elem.Equal (elem2));
  114. } catch (Exception e) {
  115. Fail ("Unexpected Exception at iTest = " + iTest + ". e = " + e);
  116. }
  117. }
  118. public void TestEscape ()
  119. {
  120. AssertEquals ("#1", SecurityElement.Escape ("foo<>\"'& bar"), "foo&lt;&gt;&quot;&apos;&amp; bar");
  121. }
  122. public void TestIsValidAttributeName ()
  123. {
  124. Assert ("#1", !SecurityElement.IsValidAttributeName ("x x"));
  125. Assert ("#2", !SecurityElement.IsValidAttributeName ("x<x"));
  126. Assert ("#3", !SecurityElement.IsValidAttributeName ("x>x"));
  127. Assert ("#4", SecurityElement.IsValidAttributeName ("x\"x"));
  128. Assert ("#5", SecurityElement.IsValidAttributeName ("x'x"));
  129. Assert ("#6", SecurityElement.IsValidAttributeName ("x&x"));
  130. }
  131. public void TestIsValidAttributeValue ()
  132. {
  133. Assert ("#1", SecurityElement.IsValidAttributeValue ("x x"));
  134. Assert ("#2", !SecurityElement.IsValidAttributeValue ("x<x"));
  135. Assert ("#3", !SecurityElement.IsValidAttributeValue ("x>x"));
  136. Assert ("#4", !SecurityElement.IsValidAttributeValue ("x\"x"));
  137. Assert ("#5", SecurityElement.IsValidAttributeValue ("x'x"));
  138. Assert ("#6", SecurityElement.IsValidAttributeValue ("x&x"));
  139. }
  140. public void TestIsValidTag ()
  141. {
  142. Assert ("#1", !SecurityElement.IsValidTag ("x x"));
  143. Assert ("#2", !SecurityElement.IsValidTag ("x<x"));
  144. Assert ("#3", !SecurityElement.IsValidTag ("x>x"));
  145. Assert ("#4", SecurityElement.IsValidTag ("x\"x"));
  146. Assert ("#5", SecurityElement.IsValidTag ("x'x"));
  147. Assert ("#6", SecurityElement.IsValidTag ("x&x"));
  148. }
  149. public void TestIsValidText ()
  150. {
  151. Assert ("#1", SecurityElement.IsValidText ("x x"));
  152. Assert ("#2", !SecurityElement.IsValidText ("x<x"));
  153. Assert ("#3", !SecurityElement.IsValidText ("x>x"));
  154. Assert ("#4", SecurityElement.IsValidText ("x\"x"));
  155. Assert ("#5", SecurityElement.IsValidText ("x'x"));
  156. Assert ("#6", SecurityElement.IsValidText ("x&x"));
  157. }
  158. public void TestSearchForChildByTag ()
  159. {
  160. SecurityElement child = null;
  161. try {
  162. child = elem.SearchForChildByTag (null);
  163. Fail ("#1 should have thrown an ArgumentNullException");
  164. } catch (ArgumentNullException) { }
  165. child = elem.SearchForChildByTag ("doesnotexist");
  166. AssertEquals ("#2", child, null);
  167. child = elem.SearchForChildByTag ("ENDPOINT");
  168. AssertEquals ("#3", child, null);
  169. child = (SecurityElement) elem.Children [0];
  170. child = child.SearchForChildByTag ("ENDPOINT");
  171. AssertEquals ("#4", child.Attribute ("transport"), "All");
  172. }
  173. public void TestSearchForTextOfTag ()
  174. {
  175. try {
  176. string t1 = elem.SearchForTextOfTag (null);
  177. Fail ("#1 should have thrown an ArgumentNullException");
  178. } catch (ArgumentNullException) { }
  179. string t2 = elem.SearchForTextOfTag ("ENDPOINT");
  180. AssertEquals ("#2", t2, "some text");
  181. }
  182. }
  183. }