From fe2b04bca3ee9f21de016c7685776e93d6d9f909 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?nils=20m=C3=A5s=C3=A9n?= Date: Thu, 20 Oct 2022 13:17:25 +0200 Subject: [PATCH] tests: make test coverage consistent (#790) --- .../Tar/TarBufferTests.cs | 23 ++---- .../Tar/TarInputStreamTests.cs | 8 +- .../Tar/TarTests.cs | 5 +- .../TestSupport/RingBuffer.cs | 4 +- .../Zip/GeneralHandling.cs | 80 +++---------------- .../Zip/ZipTests.cs | 7 +- 6 files changed, 23 insertions(+), 104 deletions(-) diff --git a/test/ICSharpCode.SharpZipLib.Tests/Tar/TarBufferTests.cs b/test/ICSharpCode.SharpZipLib.Tests/Tar/TarBufferTests.cs index 3974ffb5b..6f7ffedca 100644 --- a/test/ICSharpCode.SharpZipLib.Tests/Tar/TarBufferTests.cs +++ b/test/ICSharpCode.SharpZipLib.Tests/Tar/TarBufferTests.cs @@ -3,6 +3,7 @@ using System.Threading; using System.Threading.Tasks; using ICSharpCode.SharpZipLib.Tar; +using ICSharpCode.SharpZipLib.Tests.TestSupport; using NUnit.Framework; namespace ICSharpCode.SharpZipLib.Tests.Tar @@ -18,9 +19,7 @@ public void TestSimpleReadWrite() var writer = TarBuffer.CreateOutputTarBuffer(ms, 1); writer.IsStreamOwner = false; - var block = new byte[TarBuffer.BlockSize]; - var r = new Random(); - r.NextBytes(block); + var block = Utils.GetDummyBytes(TarBuffer.BlockSize); writer.WriteBlock(block); writer.WriteBlock(block); @@ -46,11 +45,8 @@ public void TestSkipBlock() var writer = TarBuffer.CreateOutputTarBuffer(ms, 1); writer.IsStreamOwner = false; - var block0 = new byte[TarBuffer.BlockSize]; - var block1 = new byte[TarBuffer.BlockSize]; - var r = new Random(); - r.NextBytes(block0); - r.NextBytes(block1); + var block0 = Utils.GetDummyBytes(TarBuffer.BlockSize); + var block1 = Utils.GetDummyBytes(TarBuffer.BlockSize); writer.WriteBlock(block0); writer.WriteBlock(block1); @@ -72,9 +68,7 @@ public async Task TestSimpleReadWriteAsync() var writer = TarBuffer.CreateOutputTarBuffer(ms, 1); writer.IsStreamOwner = false; - var block = new byte[TarBuffer.BlockSize]; - var r = new Random(); - r.NextBytes(block); + var block = Utils.GetDummyBytes(TarBuffer.BlockSize); await writer.WriteBlockAsync(block, CancellationToken.None); await writer.WriteBlockAsync(block, CancellationToken.None); @@ -103,11 +97,8 @@ public async Task TestSkipBlockAsync() var writer = TarBuffer.CreateOutputTarBuffer(ms, 1); writer.IsStreamOwner = false; - var block0 = new byte[TarBuffer.BlockSize]; - var block1 = new byte[TarBuffer.BlockSize]; - var r = new Random(); - r.NextBytes(block0); - r.NextBytes(block1); + var block0 = Utils.GetDummyBytes(TarBuffer.BlockSize); + var block1 = Utils.GetDummyBytes(TarBuffer.BlockSize); await writer.WriteBlockAsync(block0, CancellationToken.None); await writer.WriteBlockAsync(block1, CancellationToken.None); diff --git a/test/ICSharpCode.SharpZipLib.Tests/Tar/TarInputStreamTests.cs b/test/ICSharpCode.SharpZipLib.Tests/Tar/TarInputStreamTests.cs index fb5a26714..a69cfdf6d 100644 --- a/test/ICSharpCode.SharpZipLib.Tests/Tar/TarInputStreamTests.cs +++ b/test/ICSharpCode.SharpZipLib.Tests/Tar/TarInputStreamTests.cs @@ -15,9 +15,7 @@ public class TarInputStreamTests [Test] public void TestRead() { - var entryBytes = new byte[2000]; - var r = new Random(); - r.NextBytes(entryBytes); + var entryBytes = Utils.GetDummyBytes(2000); using var ms = new MemoryStream(); using (var tos = new TarOutputStream(ms, Encoding.UTF8) { IsStreamOwner = false }) { @@ -54,9 +52,7 @@ public void TestRead() [Test] public async Task TestReadAsync() { - var entryBytes = new byte[2000]; - var r = new Random(); - r.NextBytes(entryBytes); + var entryBytes = Utils.GetDummyBytes(2000); using var ms = new MemoryStream(); using (var tos = new TarOutputStream(ms, Encoding.UTF8) { IsStreamOwner = false }) { diff --git a/test/ICSharpCode.SharpZipLib.Tests/Tar/TarTests.cs b/test/ICSharpCode.SharpZipLib.Tests/Tar/TarTests.cs index c6a35ff08..e49035afa 100644 --- a/test/ICSharpCode.SharpZipLib.Tests/Tar/TarTests.cs +++ b/test/ICSharpCode.SharpZipLib.Tests/Tar/TarTests.cs @@ -140,10 +140,7 @@ public void TrailerContainsNulls() } tarOut.PutNextEntry(entry); - byte[] buffer = new byte[TarBuffer.BlockSize]; - - var r = new Random(); - r.NextBytes(buffer); + byte[] buffer = Utils.GetDummyBytes(TarBuffer.BlockSize); if (iteration > 0) { diff --git a/test/ICSharpCode.SharpZipLib.Tests/TestSupport/RingBuffer.cs b/test/ICSharpCode.SharpZipLib.Tests/TestSupport/RingBuffer.cs index d4b75e3cf..c8ee11881 100644 --- a/test/ICSharpCode.SharpZipLib.Tests/TestSupport/RingBuffer.cs +++ b/test/ICSharpCode.SharpZipLib.Tests/TestSupport/RingBuffer.cs @@ -510,7 +510,7 @@ public void Threaded() private void Reader() { - var r = new Random(); + var r = new Random(Utils.DefaultSeed); byte nextValue = 0; while (readTarget_ > 0) @@ -541,7 +541,7 @@ private void Reader() private void Writer() { - var r = new Random(); + var r = new Random(Utils.DefaultSeed); byte nextValue = 0; while (writeTarget_ > 0) diff --git a/test/ICSharpCode.SharpZipLib.Tests/Zip/GeneralHandling.cs b/test/ICSharpCode.SharpZipLib.Tests/Zip/GeneralHandling.cs index d45eedf3c..8026668ff 100644 --- a/test/ICSharpCode.SharpZipLib.Tests/Zip/GeneralHandling.cs +++ b/test/ICSharpCode.SharpZipLib.Tests/Zip/GeneralHandling.cs @@ -19,18 +19,6 @@ namespace ICSharpCode.SharpZipLib.Tests.Zip [TestFixture] public class GeneralHandling : ZipBase { - private void AddRandomDataToEntry(ZipOutputStream zipStream, int size) - { - if (size > 0) - { - byte[] data = new byte[size]; - var rnd = new Random(); - rnd.NextBytes(data); - - zipStream.Write(data, 0, data.Length); - } - } - private void ExerciseZip(CompressionMethod method, int compressionLevel, int size, string password, bool canSeek) { @@ -343,57 +331,11 @@ public void BasicStoredNonSeekable() [Test] [Category("Zip")] - public void StoredNonSeekableKnownSizeNoCrc() - { - // This cannot be stored directly as the crc is not be known. - const int TargetSize = 21348; - const string Password = null; - - MemoryStream ms = new MemoryStreamWithoutSeek(); - - using (ZipOutputStream outStream = new ZipOutputStream(ms)) - { - outStream.Password = Password; - outStream.IsStreamOwner = false; - var entry = new ZipEntry("dummyfile.tst"); - entry.CompressionMethod = CompressionMethod.Stored; - - // The bit thats in question is setting the size before its added to the archive. - entry.Size = TargetSize; - - outStream.PutNextEntry(entry); - - Assert.AreEqual(CompressionMethod.Deflated, entry.CompressionMethod, "Entry should be deflated"); - Assert.AreEqual(-1, entry.CompressedSize, "Compressed size should be known"); - - var rnd = new Random(); - - int size = TargetSize; - byte[] original = new byte[size]; - rnd.NextBytes(original); - - // Although this could be written in one chunk doing it in lumps - // throws up buffering problems including with encryption the original - // source for this change. - int index = 0; - while (size > 0) - { - int count = (size > 0x200) ? 0x200 : size; - outStream.Write(original, index, count); - size -= 0x200; - index += count; - } - } - Assert.That(ms.ToArray(), Does.PassTestArchive()); - } - - [Test] - [Category("Zip")] - public void StoredNonSeekableKnownSizeNoCrcEncrypted() + [TestCase(21348, null)] + [TestCase(24692, "Mabutu")] + public void StoredNonSeekableKnownSizeNoCrc(int targetSize, string password) { - // This cant be stored directly as the crc is not known - const int targetSize = 24692; - const string password = "Mabutu"; + // This cannot be stored directly as the crc is not known. MemoryStream ms = new MemoryStreamWithoutSeek(); @@ -409,19 +351,15 @@ public void StoredNonSeekableKnownSizeNoCrcEncrypted() outStream.PutNextEntry(entry); - Assert.AreEqual(CompressionMethod.Deflated, entry.CompressionMethod, "Entry should be stored"); + Assert.AreEqual(CompressionMethod.Deflated, entry.CompressionMethod, "Entry should be deflated"); Assert.AreEqual(-1, entry.CompressedSize, "Compressed size should be known"); - var rnd = new Random(); - - int size = targetSize; - byte[] original = new byte[size]; - rnd.NextBytes(original); + byte[] original = Utils.GetDummyBytes(targetSize); // Although this could be written in one chunk doing it in lumps // throws up buffering problems including with encryption the original // source for this change. - int index = 0; + int index = 0, size = targetSize; while (size > 0) { int count = (size > 0x200) ? 0x200 : size; @@ -565,12 +503,12 @@ public void StoredNonSeekableConvertToDeflate() outStream.PutNextEntry(entry); Assert.AreEqual(0, outStream.GetLevel(), "Compression level invalid"); - AddRandomDataToEntry(outStream, 100); + Utils.WriteDummyData(outStream, 100); entry = new ZipEntry("2.tst"); entry.CompressionMethod = CompressionMethod.Deflated; outStream.PutNextEntry(entry); Assert.AreEqual(8, outStream.GetLevel(), "Compression level invalid"); - AddRandomDataToEntry(outStream, 100); + Utils.WriteDummyData(outStream, 100); outStream.Close(); } diff --git a/test/ICSharpCode.SharpZipLib.Tests/Zip/ZipTests.cs b/test/ICSharpCode.SharpZipLib.Tests/Zip/ZipTests.cs index 4a0c9954f..dc398000e 100644 --- a/test/ICSharpCode.SharpZipLib.Tests/Zip/ZipTests.cs +++ b/test/ICSharpCode.SharpZipLib.Tests/Zip/ZipTests.cs @@ -25,8 +25,7 @@ public RuntimeInfo(CompressionMethod method, int compressionLevel, original = new byte[Size]; if (random) { - var rnd = new Random(); - rnd.NextBytes(original); + original = Utils.GetDummyBytes(Size); } else { @@ -251,9 +250,7 @@ protected byte[] MakeInMemoryZip(ref byte[] original, CompressionMethod method, if (size > 0) { - var rnd = new Random(); - original = new byte[size]; - rnd.NextBytes(original); + original = Utils.GetDummyBytes(size); // Although this could be written in one chunk doing it in lumps // throws up buffering problems including with encryption the original