WebUtilityTest.cs 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. //
  2. // System.Net.WebUtilityTest.cs
  3. //
  4. // copied and edited from System.Web.HttpUtilityTest.cs
  5. //
  6. // Author:
  7. // Sebastien Pouliot <[email protected]>
  8. // Mike Kestner <[email protected]>
  9. //
  10. // Copyright (C) 2005, 2010 Novell, Inc (http://www.novell.com)
  11. //
  12. // Permission is hereby granted, free of charge, to any person obtaining
  13. // a copy of this software and associated documentation files (the
  14. // "Software"), to deal in the Software without restriction, including
  15. // without limitation the rights to use, copy, modify, merge, publish,
  16. // distribute, sublicense, and/or sell copies of the Software, and to
  17. // permit persons to whom the Software is furnished to do so, subject to
  18. // the following conditions:
  19. //
  20. // The above copyright notice and this permission notice shall be
  21. // included in all copies or substantial portions of the Software.
  22. //
  23. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  24. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  25. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  26. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  27. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  28. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  29. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  30. //
  31. #if !MOBILE
  32. using System;
  33. using System.Text;
  34. using System.Net;
  35. using System.IO;
  36. using System.Collections.Specialized;
  37. using NUnit.Framework;
  38. namespace MonoTests.System.Net {
  39. [TestFixture]
  40. public class WebUtilityTest {
  41. [Test]
  42. public void HtmlEncode_LtGt ()
  43. {
  44. Assert.AreEqual ("&lt;script&gt;", WebUtility.HtmlEncode ("<script>"));
  45. }
  46. // Notes:
  47. // * this is to avoid a regression that would cause Mono to
  48. // fail item #3 of the XSS vulnerabilities listed at:
  49. // http://it-project.ru/andir/docs/aspxvuln/aspxvuln.en.xml
  50. // we didn't fall the first time so let's ensure we never will
  51. // * The author notes that Microsoft has decided not to fix
  52. // this issue (hence the NotDotNet category).
  53. [Test]
  54. [Category ("NotDotNet")]
  55. public void HtmlEncode_XSS ()
  56. {
  57. string problem = "\xff1cscript\xff1e"; // unicode looks alike <script>
  58. byte[] utf8data = Encoding.UTF8.GetBytes (problem);
  59. Encoding win1251 = Encoding.GetEncoding ("windows-1251");
  60. byte[] windata = Encoding.Convert (Encoding.UTF8, win1251, utf8data);
  61. // now it's a real problem
  62. Assert.AreEqual ("<script>", Encoding.ASCII.GetString (windata), "<script>");
  63. string encoded = WebUtility.HtmlEncode (problem);
  64. Assert.AreEqual ("&#65308;script&#65310;", encoded, "&#65308;script&#65310;");
  65. utf8data = Encoding.UTF8.GetBytes (encoded);
  66. windata = Encoding.Convert (Encoding.UTF8, win1251, utf8data);
  67. Assert.AreEqual ("&#65308;script&#65310;", Encoding.ASCII.GetString (windata), "ok");
  68. }
  69. string HtmlEncode (string s) {
  70. if (s == null)
  71. return null;
  72. bool needEncode = false;
  73. for (int i = 0; i < s.Length; i++) {
  74. char c = s [i];
  75. if (c == '&' || c == '"' || c == '<' || c == '>' || c > 159) {
  76. needEncode = true;
  77. break;
  78. }
  79. }
  80. if (!needEncode)
  81. return s;
  82. StringBuilder output = new StringBuilder ();
  83. int len = s.Length;
  84. for (int i = 0; i < len; i++)
  85. switch (s [i]) {
  86. case '&':
  87. output.Append ("&amp;");
  88. break;
  89. case '>':
  90. output.Append ("&gt;");
  91. break;
  92. case '<':
  93. output.Append ("&lt;");
  94. break;
  95. case '"':
  96. output.Append ("&quot;");
  97. break;
  98. default:
  99. // MS starts encoding with &# from 160 and stops at 255.
  100. // We don't do that. One reason is the 65308/65310 unicode
  101. // characters that look like '<' and '>'.
  102. if (s [i] > 159 && s [i] < 256) {
  103. output.Append ("&#");
  104. output.Append (((int) s [i]).ToString ());
  105. output.Append (";");
  106. }
  107. else {
  108. output.Append (s [i]);
  109. }
  110. break;
  111. }
  112. return output.ToString ();
  113. }
  114. [Test]
  115. public void EncodeEscapedCharacters ()
  116. {
  117. for (int i = 0; i < 256; i++) {
  118. string str = new string ((char) i, 1);
  119. string encoded = WebUtility.HtmlEncode (str);
  120. if ((i > 159 && i < 256 ) || i == '&' || i == '<' || i == '>' || i == '"' || i == '\'') {
  121. if (encoded [0] != '&' || encoded [encoded.Length - 1] != ';')
  122. Assert.Fail ("Failed for i = " + i);
  123. } else if (encoded.Length != 1) {
  124. Assert.Fail ("Wrong length for i = " + i);
  125. }
  126. }
  127. }
  128. [Test]
  129. public void Decode ()
  130. {
  131. Assert.AreEqual ("\xE9", WebUtility.HtmlDecode ("&#233;"), "#1");
  132. Assert.IsNull (WebUtility.HtmlDecode (null), "#2");
  133. Assert.AreEqual ("", WebUtility.HtmlDecode (""), "#3");
  134. Assert.AreEqual ("U", WebUtility.HtmlDecode ("&#x55;"), "#4");
  135. Assert.AreEqual ("&#x55G;", WebUtility.HtmlDecode ("&#x55G;"), "#5");
  136. Assert.AreEqual (char.ConvertFromUtf32 (144308), WebUtility.HtmlDecode ("&#144308;"), "#6");
  137. }
  138. [Test]
  139. public void RoundTrip ()
  140. {
  141. string x = "<html>& hello+= world!";
  142. string y = WebUtility.HtmlEncode (x);
  143. string z = WebUtility.HtmlDecode (y);
  144. Assert.AreEqual (x, z);
  145. }
  146. [Test]
  147. public void LooksLikeEntity ()
  148. {
  149. string str = "<%# \"hola\" + \"/somepage.aspx?ItemID=\" + DataBinder.Eval(Container.DataItem,\"Country\")" +
  150. " + \"&mid=\" + ModuleID + \"&pageindex=\" + Request.Params.Get(\"pageindex\") %>";
  151. Assert.AreEqual (str, WebUtility.HtmlDecode (str));
  152. }
  153. [Test]
  154. public void EntityEncoding ()
  155. {
  156. var expected = "\u00A0\u00A1\u00A2\u00A3\u00A4\u00A5\u00A6\u00A7\u00A8\u00A9\u00AA\u00AB\u00AC\u00AD\u00AE\u00AF\u00B0\u00B1\u00B2\u00B3\u00B4\u00B5\u00B6\u00B7\u00B8\u00B9\u00BA\u00BB\u00BC\u00BD\u00BE\u00BF\u00C0\u00C1\u00C2\u00C3\u00C4\u00C5\u00C6\u00C7\u00C8\u00C9\u00CA\u00CB\u00CC\u00CD\u00CE\u00CF\u00D0\u00D1\u00D2\u00D3\u00D4\u00D5\u00D6\u00D7\u00D8\u00D9\u00DA\u00DB\u00DC\u00DD\u00DE\u00DF\u00E0\u00E1\u00E2\u00E3\u00E4\u00E5\u00E6\u00E7\u00E8\u00E9\u00EA\u00EB\u00EC\u00ED\u00EE\u00EF\u00F0\u00F1\u00F2\u00F3\u00F4\u00F5\u00F6\u00F7\u00F8\u00F9\u00FA\u00FB\u00FC\u00FD\u00FE\u00FF\u0192\u0391\u0392\u0393\u0394\u0395\u0396\u0397\u0398\u0399\u039A\u039B\u039C\u039D\u039E\u039F\u03A0\u03A1\u03A3\u03A4\u03A5\u03A6\u03A7\u03A8\u03A9\u03B1\u03B2\u03B3\u03B4\u03B5\u03B6\u03B7\u03B8\u03B9\u03BA\u03BB\u03BC\u03BD\u03BE\u03BF\u03C0\u03C1\u03C2\u03C3\u03C4\u03C5\u03C6\u03C7\u03C8\u03C9\u03D1\u03D2\u03D6\u2022\u2026\u2032\u2033\u203E\u2044\u2118\u2111\u211C\u2122\u2135\u2190\u2191\u2192\u2193\u2194\u21B5\u21D0\u21D1\u21D2\u21D3\u21D4\u2200\u2202\u2203\u2205\u2207\u2208\u2209\u220B\u220F\u2211\u2212\u2217\u221A\u221D\u221E\u2220\u2227\u2228\u2229\u222A\u222B\u2234\u223C\u2245\u2248\u2260\u2261\u2264\u2265\u2282\u2283\u2284\u2286\u2287\u2295\u2297\u22A5\u22C5\u2308\u2309\u230A\u230B\u2329\u232A\u25CA\u2660\u2663\u2665\u2666\u0022\u0026\u003C\u003E\u0152\u0153\u0160\u0161\u0178\u02C6\u02DC\u2002\u2003\u2009\u200C\u200D\u200E\u200F\u2013\u2014\u2018\u2019\u201A\u201C\u201D\u201E\u2020\u2021\u2030\u2039\u203A\u20AC\'";
  157. var htmlDecoded = WebUtility.HtmlDecode ("&nbsp;&iexcl;&cent;&pound;&curren;&yen;&brvbar;&sect;&uml;&copy;&ordf;&laquo;&not;&shy;&reg;&macr;&deg;&plusmn;&sup2;&sup3;&acute;&micro;&para;&middot;&cedil;&sup1;&ordm;&raquo;&frac14;&frac12;&frac34;&iquest;&Agrave;&Aacute;&Acirc;&Atilde;&Auml;&Aring;&AElig;&Ccedil;&Egrave;&Eacute;&Ecirc;&Euml;&Igrave;&Iacute;&Icirc;&Iuml;&ETH;&Ntilde;&Ograve;&Oacute;&Ocirc;&Otilde;&Ouml;&times;&Oslash;&Ugrave;&Uacute;&Ucirc;&Uuml;&Yacute;&THORN;&szlig;&agrave;&aacute;&acirc;&atilde;&auml;&aring;&aelig;&ccedil;&egrave;&eacute;&ecirc;&euml;&igrave;&iacute;&icirc;&iuml;&eth;&ntilde;&ograve;&oacute;&ocirc;&otilde;&ouml;&divide;&oslash;&ugrave;&uacute;&ucirc;&uuml;&yacute;&thorn;&yuml;&fnof;&Alpha;&Beta;&Gamma;&Delta;&Epsilon;&Zeta;&Eta;&Theta;&Iota;&Kappa;&Lambda;&Mu;&Nu;&Xi;&Omicron;&Pi;&Rho;&Sigma;&Tau;&Upsilon;&Phi;&Chi;&Psi;&Omega;&alpha;&beta;&gamma;&delta;&epsilon;&zeta;&eta;&theta;&iota;&kappa;&lambda;&mu;&nu;&xi;&omicron;&pi;&rho;&sigmaf;&sigma;&tau;&upsilon;&phi;&chi;&psi;&omega;&thetasym;&upsih;&piv;&bull;&hellip;&prime;&Prime;&oline;&frasl;&weierp;&image;&real;&trade;&alefsym;&larr;&uarr;&rarr;&darr;&harr;&crarr;&lArr;&uArr;&rArr;&dArr;&hArr;&forall;&part;&exist;&empty;&nabla;&isin;&notin;&ni;&prod;&sum;&minus;&lowast;&radic;&prop;&infin;&ang;&and;&or;&cap;&cup;&int;&there4;&sim;&cong;&asymp;&ne;&equiv;&le;&ge;&sub;&sup;&nsub;&sube;&supe;&oplus;&otimes;&perp;&sdot;&lceil;&rceil;&lfloor;&rfloor;&lang;&rang;&loz;&spades;&clubs;&hearts;&diams;&quot;&amp;&lt;&gt;&OElig;&oelig;&Scaron;&scaron;&Yuml;&circ;&tilde;&ensp;&emsp;&thinsp;&zwnj;&zwj;&lrm;&rlm;&ndash;&mdash;&lsquo;&rsquo;&sbquo;&ldquo;&rdquo;&bdquo;&dagger;&Dagger;&permil;&lsaquo;&rsaquo;&euro;&apos;");
  158. Assert.AreEqual (expected, htmlDecoded);
  159. }
  160. [Test]
  161. public void UrlEncoding ()
  162. {
  163. Assert.AreEqual ("%27", WebUtility.UrlEncode("'"), "#1");
  164. }
  165. }
  166. }
  167. #endif