2 // System.Net.WebUtilityTest.cs
4 // copied and edited from System.Web.HttpUtilityTest.cs
7 // Sebastien Pouliot <sebastien@ximian.com>
8 // Mike Kestner <mkestner@novell.com>
10 // Copyright (C) 2005, 2010 Novell, Inc (http://www.novell.com)
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:
20 // The above copyright notice and this permission notice shall be
21 // included in all copies or substantial portions of the Software.
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.
32 #if NET_4_0 && !MOBILE
38 using System.Collections.Specialized;
40 using NUnit.Framework;
42 namespace MonoTests.System.Net {
45 public class WebUtilityTest {
48 public void HtmlEncode_LtGt ()
50 Assert.AreEqual ("<script>", WebUtility.HtmlEncode ("<script>"));
54 // * this is to avoid a regression that would cause Mono to
55 // fail item #3 of the XSS vulnerabilities listed at:
56 // http://it-project.ru/andir/docs/aspxvuln/aspxvuln.en.xml
57 // we didn't fall the first time so let's ensure we never will
58 // * The author notes that Microsoft has decided not to fix
59 // this issue (hence the NotDotNet category).
62 [Category ("NotDotNet")]
66 public void HtmlEncode_XSS ()
68 string problem = "\xff1cscript\xff1e"; // unicode looks alike <script>
69 byte[] utf8data = Encoding.UTF8.GetBytes (problem);
70 Encoding win1251 = Encoding.GetEncoding ("windows-1251");
71 byte[] windata = Encoding.Convert (Encoding.UTF8, win1251, utf8data);
72 // now it's a real problem
73 Assert.AreEqual ("<script>", Encoding.ASCII.GetString (windata), "<script>");
75 string encoded = WebUtility.HtmlEncode (problem);
76 Assert.AreEqual ("<script>", encoded, "<script>");
78 utf8data = Encoding.UTF8.GetBytes (encoded);
79 windata = Encoding.Convert (Encoding.UTF8, win1251, utf8data);
80 Assert.AreEqual ("<script>", Encoding.ASCII.GetString (windata), "ok");
85 [Category ("NotWorking")]
87 public void HtmlEncode () {
88 for (char c = char.MinValue; c < char.MaxValue; c++) {
89 String exp = HtmlEncode (c.ToString ());
90 String act = WebUtility.HtmlEncode (c.ToString ());
91 Assert.AreEqual (exp, act, "HtmlEncode " + c.ToString () + " [" + (int) c + "]");
95 string HtmlEncode (string s) {
99 bool needEncode = false;
100 for (int i = 0; i < s.Length; i++) {
102 if (c == '&' || c == '"' || c == '<' || c == '>' || c > 159) {
111 StringBuilder output = new StringBuilder ();
114 for (int i = 0; i < len; i++)
117 output.Append ("&");
120 output.Append (">");
123 output.Append ("<");
126 output.Append (""");
129 // MS starts encoding with &# from 160 and stops at 255.
130 // We don't do that. One reason is the 65308/65310 unicode
131 // characters that look like '<' and '>'.
132 if (s [i] > 159 && s [i] < 256) {
133 output.Append ("&#");
134 output.Append (((int) s [i]).ToString ());
138 output.Append (s [i]);
142 return output.ToString ();
147 public void EscapedCharacters ()
149 for (int i = 0; i < 256; i++) {
150 string str = new string ((char) i, 1);
151 string encoded = WebUtility.HtmlEncode (str);
152 if ((i > 159 && i < 256 ) || i == '&' || i == '<' || i == '>' || i == '"') {
153 if (encoded [0] != '&' || encoded [encoded.Length - 1] != ';')
154 Assert.Fail ("Failed for i = " + i);
155 } else if (encoded.Length != 1) {
156 Assert.Fail ("Wrong length for i = " + i);
162 public void Decode1 ()
164 Assert.AreEqual ("\xE9", WebUtility.HtmlDecode ("é"));
168 public void RoundTrip ()
170 string x = "<html>& hello+= world!";
171 string y = WebUtility.HtmlEncode (x);
172 string z = WebUtility.HtmlDecode (y);
173 Assert.AreEqual (x, z);
177 public void LooksLikeEntity ()
179 string str = "<%# \"hola\" + \"/somepage.aspx?ItemID=\" + DataBinder.Eval(Container.DataItem,\"Country\")" +
180 " + \"&mid=\" + ModuleID + \"&pageindex=\" + Request.Params.Get(\"pageindex\") %>";
181 Assert.AreEqual (str, WebUtility.HtmlDecode (str));