public void Ctor_WithUnicodeRanges() { // Arrange HtmlEncoder encoder = HtmlEncoder.Create(UnicodeRanges.Latin1Supplement, UnicodeRanges.MiscellaneousSymbols); // Act & assert Assert.Equal("a", encoder.Encode("a")); Assert.Equal("\u00E9", encoder.Encode("\u00E9" /* LATIN SMALL LETTER E WITH ACUTE */)); Assert.Equal("\u2601", encoder.Encode("\u2601" /* CLOUD */)); }
public void HtmlEncode_AllRangesAllowed_StillEncodesForbiddenChars_Simple(string input, string expected) { // Arrange HtmlEncoder encoder = HtmlEncoder.Create(UnicodeRanges.All); // Act string retVal = encoder.Encode(input); // Assert Assert.Equal(expected, retVal); }
public void HtmlEncode_BadSurrogates_ReturnsUnicodeReplacementChar() { // Arrange HtmlEncoder encoder = HtmlEncoder.Create(UnicodeRanges.All); // allow all codepoints // "a<unpaired leading>b<unpaired trailing>c<trailing before leading>d<unpaired trailing><valid>e<high at end of string>" const string input = "a\uD800b\uDFFFc\uDFFF\uD800d\uDFFF\uD800\uDFFFe\uD800"; const string expected = "a�b�c��d�𐏿e�"; // Act string retVal = encoder.Encode(input); // Assert Assert.Equal(expected, retVal); }
public void Default_EquivalentToBasicLatin() { // Arrange HtmlEncoder controlEncoder = HtmlEncoder.Create(UnicodeRanges.BasicLatin); HtmlEncoder testEncoder = HtmlEncoder.Default; // Act & assert for (int i = 0; i <= char.MaxValue; i++) { if (!IsSurrogateCodePoint(i)) { string input = new string((char)i, 1); Assert.Equal(controlEncoder.Encode(input), testEncoder.Encode(input)); } } }
public void Ctor_WithTextEncoderSettings() { // Arrange var filter = new TextEncoderSettings(); filter.AllowCharacters('a', 'b'); filter.AllowCharacters('\0', '&', '\uFFFF', 'd'); HtmlEncoder encoder = HtmlEncoder.Create(filter); // Act & assert Assert.Equal("a", encoder.Encode("a")); Assert.Equal("b", encoder.Encode("b")); Assert.Equal("c", encoder.Encode("c")); Assert.Equal("d", encoder.Encode("d")); Assert.Equal("�", encoder.Encode("\0")); // we still always encode control chars Assert.Equal("&", encoder.Encode("&")); // we still always encode HTML-special chars Assert.Equal("", encoder.Encode("\uFFFF")); // we still always encode non-chars and other forbidden chars }
public void UrlEncode_DoesNotOutputHtmlSensitiveCharacters() { // Per the design document, we provide additional defense-in-depth // by never emitting HTML-sensitive characters unescaped. // Arrange UrlEncoder urlEncoder = UrlEncoder.Create(UnicodeRanges.All); HtmlEncoder htmlEncoder = HtmlEncoder.Create(UnicodeRanges.All); // Act & assert for (int i = 0; i <= 0x10FFFF; i++) { if (IsSurrogateCodePoint(i)) { continue; // surrogates don't matter here } string urlEncoded = urlEncoder.Encode(char.ConvertFromUtf32(i)); string thenHtmlEncoded = htmlEncoder.Encode(urlEncoded); Assert.Equal(urlEncoded, thenHtmlEncoded); // should have contained no HTML-sensitive characters } }
public void HtmlEncode_AllRangesAllowed_StillEncodesForbiddenChars_Extended() { // Arrange HtmlEncoder encoder = HtmlEncoder.Create(UnicodeRanges.All); // Act & assert - BMP chars for (int i = 0; i <= 0xFFFF; i++) { string input = new string((char)i, 1); string expected; if (IsSurrogateCodePoint(i)) { expected = "�"; // unpaired surrogate -> Unicode replacement char (escaped) } else { if (input == "<") { expected = "<"; } else if (input == ">") { expected = ">"; } else if (input == "&") { expected = "&"; } else if (input == "\"") { expected = """; } else { bool mustEncode = false; if (i == '\'' || i == '+') { mustEncode = true; // apostrophe, plus } else if (i <= 0x001F || (0x007F <= i && i <= 0x9F)) { mustEncode = true; // control char } else if (!UnicodeTestHelpers.IsCharacterDefined((char)i)) { mustEncode = true; // undefined (or otherwise disallowed) char } if (mustEncode) { expected = string.Format(CultureInfo.InvariantCulture, "&#x{0:X};", i); } else { expected = input; // no encoding } } } string retVal = encoder.Encode(input); Assert.Equal(expected, retVal); } // Act & assert - astral chars for (int i = 0x10000; i <= 0x10FFFF; i++) { string input = char.ConvertFromUtf32(i); string expected = string.Format(CultureInfo.InvariantCulture, "&#x{0:X};", i); string retVal = encoder.Encode(input); Assert.Equal(expected, retVal); } }