SEBWIN-352: Extended unit tests for XML parser.

This commit is contained in:
dbuechel 2020-01-30 16:07:24 +01:00
parent de00dbc13c
commit f89b0d8a2a

View file

@ -119,6 +119,31 @@ namespace SafeExamBrowser.Configuration.UnitTests.DataFormats
Assert.AreEqual(LoadStatus.InvalidData, result.Status); Assert.AreEqual(LoadStatus.InvalidData, result.Status);
} }
[TestMethod]
public void MustDecompressDataIfCompressed()
{
var bytes = Encoding.UTF8.GetBytes("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
var data = LoadTestData();
var stream = new MemoryStream(Encoding.UTF8.GetBytes("some incorrect data here"));
compressor.Setup(c => c.Peek(It.IsAny<Stream>(), It.IsAny<int>())).Returns<Stream, int>((d, c) => bytes.Take(c).ToArray());
compressor.Setup(c => c.IsCompressed(It.IsAny<Stream>())).Returns(true);
Assert.IsTrue(sut.CanParse(stream));
compressor.Verify(c => c.IsCompressed(It.IsAny<Stream>()), Times.Once);
compressor.Verify(c => c.Peek(It.IsAny<Stream>(), It.IsAny<int>()), Times.Once);
compressor.Setup(c => c.Decompress(It.IsAny<Stream>())).Returns(data);
var result = sut.TryParse(stream);
compressor.Verify(c => c.IsCompressed(It.IsAny<Stream>()), Times.AtLeast(2));
compressor.Verify(c => c.Decompress(It.IsAny<Stream>()), Times.Once);
Assert.AreEqual(LoadStatus.Success, result.Status);
}
[TestMethod] [TestMethod]
public void MustParseEmptyXml() public void MustParseEmptyXml()
{ {
@ -187,6 +212,14 @@ namespace SafeExamBrowser.Configuration.UnitTests.DataFormats
Assert.IsNull(result.RawData["value"]); Assert.IsNull(result.RawData["value"]);
} }
[TestMethod]
public void MustNotFailWithWrongData()
{
var result = sut.TryParse(new MemoryStream(Encoding.UTF8.GetBytes("some random text")));
Assert.AreEqual(LoadStatus.InvalidData, result.Status);
}
private Stream LoadTestData() private Stream LoadTestData()
{ {
var path = $"{nameof(SafeExamBrowser)}.{nameof(Configuration)}.{nameof(UnitTests)}.{nameof(DataFormats)}.XmlTestData.xml"; var path = $"{nameof(SafeExamBrowser)}.{nameof(Configuration)}.{nameof(UnitTests)}.{nameof(DataFormats)}.XmlTestData.xml";