Skip to content

Commit

Permalink
tests: make test coverage consistent (#790)
Browse files Browse the repository at this point in the history
  • Loading branch information
piksel authored Oct 20, 2022
1 parent 75d1cf8 commit fe2b04b
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 104 deletions.
23 changes: 7 additions & 16 deletions test/ICSharpCode.SharpZipLib.Tests/Tar/TarBufferTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand Down Expand Up @@ -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);
Expand Down
8 changes: 2 additions & 6 deletions test/ICSharpCode.SharpZipLib.Tests/Tar/TarInputStreamTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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 })
{
Expand Down Expand Up @@ -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 })
{
Expand Down
5 changes: 1 addition & 4 deletions test/ICSharpCode.SharpZipLib.Tests/Tar/TarTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down
4 changes: 2 additions & 2 deletions test/ICSharpCode.SharpZipLib.Tests/TestSupport/RingBuffer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
80 changes: 9 additions & 71 deletions test/ICSharpCode.SharpZipLib.Tests/Zip/GeneralHandling.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down Expand Up @@ -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();

Expand All @@ -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;
Expand Down Expand Up @@ -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();
}
Expand Down
7 changes: 2 additions & 5 deletions test/ICSharpCode.SharpZipLib.Tests/Zip/ZipTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit fe2b04b

Please sign in to comment.