From f817f31f2dfdacd9bd572ce8bedf900feec3be00 Mon Sep 17 00:00:00 2001 From: dbuechel Date: Tue, 19 Feb 2019 11:01:14 +0100 Subject: [PATCH] SEBWIN-296: Implemented unit tests for GZip compressor. --- .../DataCompression/GZipCompressorTests.cs | 90 +++++++++++++++++++ ...ExamBrowser.Configuration.UnitTests.csproj | 1 + .../DataCompression/GZipCompressor.cs | 2 +- 3 files changed, 92 insertions(+), 1 deletion(-) create mode 100644 SafeExamBrowser.Configuration.UnitTests/DataCompression/GZipCompressorTests.cs diff --git a/SafeExamBrowser.Configuration.UnitTests/DataCompression/GZipCompressorTests.cs b/SafeExamBrowser.Configuration.UnitTests/DataCompression/GZipCompressorTests.cs new file mode 100644 index 00000000..309e982d --- /dev/null +++ b/SafeExamBrowser.Configuration.UnitTests/DataCompression/GZipCompressorTests.cs @@ -0,0 +1,90 @@ +/* + * Copyright (c) 2019 ETH Zürich, Educational Development and Technology (LET) + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + */ + +using System; +using System.IO; +using System.Linq; +using System.Text; +using Microsoft.VisualStudio.TestTools.UnitTesting; +using Moq; +using SafeExamBrowser.Configuration.DataCompression; +using SafeExamBrowser.Contracts.Logging; + +namespace SafeExamBrowser.Configuration.UnitTests.DataCompression +{ + [TestClass] + public class GZipCompressorTests + { + private const int ID1 = 0x1F; + private const int ID2 = 0x8B; + private const int CM = 8; + private const int FOOTER_LENGTH = 8; + private const int HEADER_LENGTH = 10; + + private Mock logger; + private GZipCompressor sut; + + [TestInitialize] + public void Initialize() + { + logger = new Mock(); + sut = new GZipCompressor(logger.Object); + } + + [TestMethod] + public void MustCorrectlyDetectGZipStream() + { + var randomBytes = new byte[123]; + + new Random().NextBytes(randomBytes); + + Assert.IsFalse(sut.IsCompressed(null)); + Assert.IsFalse(sut.IsCompressed(new MemoryStream())); + Assert.IsFalse(sut.IsCompressed(new MemoryStream(randomBytes))); + + Assert.IsTrue(sut.IsCompressed(new MemoryStream(new byte[] { ID1, ID2, CM }.Concat(randomBytes).ToArray()))); + } + + [TestMethod] + public void MustPerformCorrectly() + { + var data = Encoding.UTF8.GetBytes(String.Join(" ", Enumerable.Repeat("A comparatively easy text to compress.", 100))); + var compressed = sut.Compress(new MemoryStream(data)); + + compressed.Seek(0, SeekOrigin.Begin); + + Assert.AreEqual(ID1, compressed.ReadByte()); + Assert.AreEqual(ID2, compressed.ReadByte()); + Assert.AreEqual(CM, compressed.ReadByte()); + Assert.IsTrue(compressed.Length < data.Length); + + var decompressed = sut.Decompress(compressed); + + decompressed.Seek(0, SeekOrigin.Begin); + + foreach (var item in data) + { + Assert.AreEqual(item, decompressed.ReadByte()); + } + + Assert.IsTrue(decompressed.Length == data.Length); + } + + [TestMethod] + public void MustPreviewDataCorrectly() + { + var data = Encoding.UTF8.GetBytes("A comparatively easy text to compress."); + var compressed = sut.Compress(new MemoryStream(data)); + + var preview = sut.Peek(compressed, 5); + + Assert.AreEqual(5, preview.Length); + Assert.IsTrue(Encoding.UTF8.GetBytes("A com").SequenceEqual(preview)); + } + } +} diff --git a/SafeExamBrowser.Configuration.UnitTests/SafeExamBrowser.Configuration.UnitTests.csproj b/SafeExamBrowser.Configuration.UnitTests/SafeExamBrowser.Configuration.UnitTests.csproj index dd7df7c7..3e84475d 100644 --- a/SafeExamBrowser.Configuration.UnitTests/SafeExamBrowser.Configuration.UnitTests.csproj +++ b/SafeExamBrowser.Configuration.UnitTests/SafeExamBrowser.Configuration.UnitTests.csproj @@ -88,6 +88,7 @@ + diff --git a/SafeExamBrowser.Configuration/DataCompression/GZipCompressor.cs b/SafeExamBrowser.Configuration/DataCompression/GZipCompressor.cs index 02554eea..3c8aac99 100644 --- a/SafeExamBrowser.Configuration/DataCompression/GZipCompressor.cs +++ b/SafeExamBrowser.Configuration/DataCompression/GZipCompressor.cs @@ -95,7 +95,7 @@ namespace SafeExamBrowser.Configuration.DataCompression } catch (Exception e) { - logger.Error($"Failed to check whether '{data}' with {data.Length / 1000.0} KB data is a gzip-compressed stream!", e); + logger.Error($"Failed to check whether '{data}' with {data?.Length / 1000.0} KB data is a gzip-compressed stream!", e); } return false;