Skip to content

Commit fe2b04b

Browse files
authored
tests: make test coverage consistent (#790)
1 parent 75d1cf8 commit fe2b04b

File tree

6 files changed

+23
-104
lines changed

6 files changed

+23
-104
lines changed

test/ICSharpCode.SharpZipLib.Tests/Tar/TarBufferTests.cs

Lines changed: 7 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using System.Threading;
44
using System.Threading.Tasks;
55
using ICSharpCode.SharpZipLib.Tar;
6+
using ICSharpCode.SharpZipLib.Tests.TestSupport;
67
using NUnit.Framework;
78

89
namespace ICSharpCode.SharpZipLib.Tests.Tar
@@ -18,9 +19,7 @@ public void TestSimpleReadWrite()
1819
var writer = TarBuffer.CreateOutputTarBuffer(ms, 1);
1920
writer.IsStreamOwner = false;
2021

21-
var block = new byte[TarBuffer.BlockSize];
22-
var r = new Random();
23-
r.NextBytes(block);
22+
var block = Utils.GetDummyBytes(TarBuffer.BlockSize);
2423

2524
writer.WriteBlock(block);
2625
writer.WriteBlock(block);
@@ -46,11 +45,8 @@ public void TestSkipBlock()
4645
var writer = TarBuffer.CreateOutputTarBuffer(ms, 1);
4746
writer.IsStreamOwner = false;
4847

49-
var block0 = new byte[TarBuffer.BlockSize];
50-
var block1 = new byte[TarBuffer.BlockSize];
51-
var r = new Random();
52-
r.NextBytes(block0);
53-
r.NextBytes(block1);
48+
var block0 = Utils.GetDummyBytes(TarBuffer.BlockSize);
49+
var block1 = Utils.GetDummyBytes(TarBuffer.BlockSize);
5450

5551
writer.WriteBlock(block0);
5652
writer.WriteBlock(block1);
@@ -72,9 +68,7 @@ public async Task TestSimpleReadWriteAsync()
7268
var writer = TarBuffer.CreateOutputTarBuffer(ms, 1);
7369
writer.IsStreamOwner = false;
7470

75-
var block = new byte[TarBuffer.BlockSize];
76-
var r = new Random();
77-
r.NextBytes(block);
71+
var block = Utils.GetDummyBytes(TarBuffer.BlockSize);
7872

7973
await writer.WriteBlockAsync(block, CancellationToken.None);
8074
await writer.WriteBlockAsync(block, CancellationToken.None);
@@ -103,11 +97,8 @@ public async Task TestSkipBlockAsync()
10397
var writer = TarBuffer.CreateOutputTarBuffer(ms, 1);
10498
writer.IsStreamOwner = false;
10599

106-
var block0 = new byte[TarBuffer.BlockSize];
107-
var block1 = new byte[TarBuffer.BlockSize];
108-
var r = new Random();
109-
r.NextBytes(block0);
110-
r.NextBytes(block1);
100+
var block0 = Utils.GetDummyBytes(TarBuffer.BlockSize);
101+
var block1 = Utils.GetDummyBytes(TarBuffer.BlockSize);
111102

112103
await writer.WriteBlockAsync(block0, CancellationToken.None);
113104
await writer.WriteBlockAsync(block1, CancellationToken.None);

test/ICSharpCode.SharpZipLib.Tests/Tar/TarInputStreamTests.cs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,7 @@ public class TarInputStreamTests
1515
[Test]
1616
public void TestRead()
1717
{
18-
var entryBytes = new byte[2000];
19-
var r = new Random();
20-
r.NextBytes(entryBytes);
18+
var entryBytes = Utils.GetDummyBytes(2000);
2119
using var ms = new MemoryStream();
2220
using (var tos = new TarOutputStream(ms, Encoding.UTF8) { IsStreamOwner = false })
2321
{
@@ -54,9 +52,7 @@ public void TestRead()
5452
[Test]
5553
public async Task TestReadAsync()
5654
{
57-
var entryBytes = new byte[2000];
58-
var r = new Random();
59-
r.NextBytes(entryBytes);
55+
var entryBytes = Utils.GetDummyBytes(2000);
6056
using var ms = new MemoryStream();
6157
using (var tos = new TarOutputStream(ms, Encoding.UTF8) { IsStreamOwner = false })
6258
{

test/ICSharpCode.SharpZipLib.Tests/Tar/TarTests.cs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -140,10 +140,7 @@ public void TrailerContainsNulls()
140140
}
141141
tarOut.PutNextEntry(entry);
142142

143-
byte[] buffer = new byte[TarBuffer.BlockSize];
144-
145-
var r = new Random();
146-
r.NextBytes(buffer);
143+
byte[] buffer = Utils.GetDummyBytes(TarBuffer.BlockSize);
147144

148145
if (iteration > 0)
149146
{

test/ICSharpCode.SharpZipLib.Tests/TestSupport/RingBuffer.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -510,7 +510,7 @@ public void Threaded()
510510

511511
private void Reader()
512512
{
513-
var r = new Random();
513+
var r = new Random(Utils.DefaultSeed);
514514
byte nextValue = 0;
515515

516516
while (readTarget_ > 0)
@@ -541,7 +541,7 @@ private void Reader()
541541

542542
private void Writer()
543543
{
544-
var r = new Random();
544+
var r = new Random(Utils.DefaultSeed);
545545

546546
byte nextValue = 0;
547547
while (writeTarget_ > 0)

test/ICSharpCode.SharpZipLib.Tests/Zip/GeneralHandling.cs

Lines changed: 9 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -19,18 +19,6 @@ namespace ICSharpCode.SharpZipLib.Tests.Zip
1919
[TestFixture]
2020
public class GeneralHandling : ZipBase
2121
{
22-
private void AddRandomDataToEntry(ZipOutputStream zipStream, int size)
23-
{
24-
if (size > 0)
25-
{
26-
byte[] data = new byte[size];
27-
var rnd = new Random();
28-
rnd.NextBytes(data);
29-
30-
zipStream.Write(data, 0, data.Length);
31-
}
32-
}
33-
3422
private void ExerciseZip(CompressionMethod method, int compressionLevel,
3523
int size, string password, bool canSeek)
3624
{
@@ -343,57 +331,11 @@ public void BasicStoredNonSeekable()
343331

344332
[Test]
345333
[Category("Zip")]
346-
public void StoredNonSeekableKnownSizeNoCrc()
347-
{
348-
// This cannot be stored directly as the crc is not be known.
349-
const int TargetSize = 21348;
350-
const string Password = null;
351-
352-
MemoryStream ms = new MemoryStreamWithoutSeek();
353-
354-
using (ZipOutputStream outStream = new ZipOutputStream(ms))
355-
{
356-
outStream.Password = Password;
357-
outStream.IsStreamOwner = false;
358-
var entry = new ZipEntry("dummyfile.tst");
359-
entry.CompressionMethod = CompressionMethod.Stored;
360-
361-
// The bit thats in question is setting the size before its added to the archive.
362-
entry.Size = TargetSize;
363-
364-
outStream.PutNextEntry(entry);
365-
366-
Assert.AreEqual(CompressionMethod.Deflated, entry.CompressionMethod, "Entry should be deflated");
367-
Assert.AreEqual(-1, entry.CompressedSize, "Compressed size should be known");
368-
369-
var rnd = new Random();
370-
371-
int size = TargetSize;
372-
byte[] original = new byte[size];
373-
rnd.NextBytes(original);
374-
375-
// Although this could be written in one chunk doing it in lumps
376-
// throws up buffering problems including with encryption the original
377-
// source for this change.
378-
int index = 0;
379-
while (size > 0)
380-
{
381-
int count = (size > 0x200) ? 0x200 : size;
382-
outStream.Write(original, index, count);
383-
size -= 0x200;
384-
index += count;
385-
}
386-
}
387-
Assert.That(ms.ToArray(), Does.PassTestArchive());
388-
}
389-
390-
[Test]
391-
[Category("Zip")]
392-
public void StoredNonSeekableKnownSizeNoCrcEncrypted()
334+
[TestCase(21348, null)]
335+
[TestCase(24692, "Mabutu")]
336+
public void StoredNonSeekableKnownSizeNoCrc(int targetSize, string password)
393337
{
394-
// This cant be stored directly as the crc is not known
395-
const int targetSize = 24692;
396-
const string password = "Mabutu";
338+
// This cannot be stored directly as the crc is not known.
397339

398340
MemoryStream ms = new MemoryStreamWithoutSeek();
399341

@@ -409,19 +351,15 @@ public void StoredNonSeekableKnownSizeNoCrcEncrypted()
409351

410352
outStream.PutNextEntry(entry);
411353

412-
Assert.AreEqual(CompressionMethod.Deflated, entry.CompressionMethod, "Entry should be stored");
354+
Assert.AreEqual(CompressionMethod.Deflated, entry.CompressionMethod, "Entry should be deflated");
413355
Assert.AreEqual(-1, entry.CompressedSize, "Compressed size should be known");
414356

415-
var rnd = new Random();
416-
417-
int size = targetSize;
418-
byte[] original = new byte[size];
419-
rnd.NextBytes(original);
357+
byte[] original = Utils.GetDummyBytes(targetSize);
420358

421359
// Although this could be written in one chunk doing it in lumps
422360
// throws up buffering problems including with encryption the original
423361
// source for this change.
424-
int index = 0;
362+
int index = 0, size = targetSize;
425363
while (size > 0)
426364
{
427365
int count = (size > 0x200) ? 0x200 : size;
@@ -565,12 +503,12 @@ public void StoredNonSeekableConvertToDeflate()
565503
outStream.PutNextEntry(entry);
566504
Assert.AreEqual(0, outStream.GetLevel(), "Compression level invalid");
567505

568-
AddRandomDataToEntry(outStream, 100);
506+
Utils.WriteDummyData(outStream, 100);
569507
entry = new ZipEntry("2.tst");
570508
entry.CompressionMethod = CompressionMethod.Deflated;
571509
outStream.PutNextEntry(entry);
572510
Assert.AreEqual(8, outStream.GetLevel(), "Compression level invalid");
573-
AddRandomDataToEntry(outStream, 100);
511+
Utils.WriteDummyData(outStream, 100);
574512

575513
outStream.Close();
576514
}

test/ICSharpCode.SharpZipLib.Tests/Zip/ZipTests.cs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,7 @@ public RuntimeInfo(CompressionMethod method, int compressionLevel,
2525
original = new byte[Size];
2626
if (random)
2727
{
28-
var rnd = new Random();
29-
rnd.NextBytes(original);
28+
original = Utils.GetDummyBytes(Size);
3029
}
3130
else
3231
{
@@ -251,9 +250,7 @@ protected byte[] MakeInMemoryZip(ref byte[] original, CompressionMethod method,
251250

252251
if (size > 0)
253252
{
254-
var rnd = new Random();
255-
original = new byte[size];
256-
rnd.NextBytes(original);
253+
original = Utils.GetDummyBytes(size);
257254

258255
// Although this could be written in one chunk doing it in lumps
259256
// throws up buffering problems including with encryption the original

0 commit comments

Comments
 (0)