mirror of
https://github.com/microsoft/PowerToys.git
synced 2024-12-24 00:48:00 +08:00
13fd6bd6e1
* Adjusting namespace * Getting FxCop working e2e
103 lines
3.9 KiB
C#
103 lines
3.9 KiB
C#
// Copyright (c) Microsoft Corporation
|
|
// The Microsoft Corporation licenses this file to you under the MIT license.
|
|
// See the LICENSE file in the project root for more information.
|
|
|
|
using System.Text;
|
|
using Microsoft.PowerToys.PreviewHandler.Svg.Utilities;
|
|
using Microsoft.VisualStudio.TestTools.UnitTesting;
|
|
|
|
namespace SvgPreviewHandlerUnitTests
|
|
{
|
|
[TestClass]
|
|
public class SvgPreviewHandlerHelperTests
|
|
{
|
|
[TestMethod]
|
|
public void CheckBlockedElementsShouldReturnTrueIfABlockedElementIsPresent()
|
|
{
|
|
// Arrange
|
|
var svgBuilder = new StringBuilder();
|
|
svgBuilder.AppendLine("<svg width =\"200\" height=\"200\" xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\">");
|
|
svgBuilder.AppendLine("\t<script>alert(\"hello\")</script>");
|
|
svgBuilder.AppendLine("</svg>");
|
|
bool foundFilteredElement;
|
|
|
|
// Act
|
|
foundFilteredElement = SvgPreviewHandlerHelper.CheckBlockedElements(svgBuilder.ToString());
|
|
|
|
// Assert
|
|
Assert.IsTrue(foundFilteredElement);
|
|
}
|
|
|
|
[TestMethod]
|
|
public void CheckBlockedElementsShouldReturnTrueIfBlockedElementsIsPresentInNestedLevel()
|
|
{
|
|
// Arrange
|
|
var svgBuilder = new StringBuilder();
|
|
svgBuilder.AppendLine("<svg viewBox=\"0 0 100 100\" xmlns=\"http://www.w3.org/2000/svg\">");
|
|
svgBuilder.AppendLine("\t<circle cx=\"50\" cy=\"50\" r=\"50\">");
|
|
svgBuilder.AppendLine("\t\t<script>alert(\"valid-message\")</script>");
|
|
svgBuilder.AppendLine("\t</circle>");
|
|
svgBuilder.AppendLine("</svg>");
|
|
bool foundFilteredElement;
|
|
|
|
// Act
|
|
foundFilteredElement = SvgPreviewHandlerHelper.CheckBlockedElements(svgBuilder.ToString());
|
|
|
|
// Assert
|
|
Assert.IsTrue(foundFilteredElement);
|
|
}
|
|
|
|
[TestMethod]
|
|
public void CheckBlockedElementsShouldReturnTrueIfMultipleBlockedElementsArePresent()
|
|
{
|
|
// Arrange
|
|
var svgBuilder = new StringBuilder();
|
|
svgBuilder.AppendLine("<svg width =\"200\" height=\"200\" xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\">");
|
|
svgBuilder.AppendLine("\t<script>alert(\"valid-message\")</script>");
|
|
svgBuilder.AppendLine("\t<image href=\"valid-url\" height=\"200\" width=\"200\"/>");
|
|
svgBuilder.AppendLine("</svg>");
|
|
bool foundFilteredElement;
|
|
|
|
// Act
|
|
foundFilteredElement = SvgPreviewHandlerHelper.CheckBlockedElements(svgBuilder.ToString());
|
|
|
|
// Assert
|
|
Assert.IsTrue(foundFilteredElement);
|
|
}
|
|
|
|
[TestMethod]
|
|
public void CheckBlockedElementsShouldReturnFalseIfNoBlockedElementsArePresent()
|
|
{
|
|
// Arrange
|
|
var svgBuilder = new StringBuilder();
|
|
svgBuilder.AppendLine("<svg viewBox=\"0 0 100 100\" xmlns=\"http://www.w3.org/2000/svg\">");
|
|
svgBuilder.AppendLine("\t<circle cx=\"50\" cy=\"50\" r=\"50\">");
|
|
svgBuilder.AppendLine("\t</circle>");
|
|
svgBuilder.AppendLine("</svg>");
|
|
bool foundFilteredElement;
|
|
|
|
// Act
|
|
foundFilteredElement = SvgPreviewHandlerHelper.CheckBlockedElements(svgBuilder.ToString());
|
|
|
|
// Assert
|
|
Assert.IsFalse(foundFilteredElement);
|
|
}
|
|
|
|
[DataTestMethod]
|
|
[DataRow("")]
|
|
[DataRow(" ")]
|
|
[DataRow(null)]
|
|
public void CheckBlockedElementsShouldReturnFalseIfSvgDataIsNullOrWhiteSpaces(string svgData)
|
|
{
|
|
// Arrange
|
|
bool foundFilteredElement;
|
|
|
|
// Act
|
|
foundFilteredElement = SvgPreviewHandlerHelper.CheckBlockedElements(svgData);
|
|
|
|
// Assert
|
|
Assert.IsFalse(foundFilteredElement);
|
|
}
|
|
}
|
|
}
|