Skip to content

Commit d1bfdba

Browse files
committed
Merge branch 'icsharpcode:master' into master
2 parents c230218 + 8c0a169 commit d1bfdba

File tree

8 files changed

+150
-88
lines changed

8 files changed

+150
-88
lines changed

.github/workflows/build-test.yml

+51-19
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,9 @@ jobs:
2222
LIB_PROJ: src/ICSharpCode.SharpZipLib/ICSharpCode.SharpZipLib.csproj
2323
steps:
2424
- uses: actions/checkout@v2
25-
25+
with:
26+
fetch-depth: 0
27+
2628
- name: Setup .NET Core
2729
uses: actions/setup-dotnet@v1
2830
with:
@@ -39,46 +41,76 @@ jobs:
3941
strategy:
4042
fail-fast: false
4143
matrix:
42-
os: [ubuntu, windows, macos]
44+
# Windows testing is combined with code coverage
45+
os: [ubuntu, macos]
4346
target: [netcoreapp3.1]
44-
include:
45-
- os: windows
46-
target: net46
4747
steps:
4848
- uses: actions/checkout@v2
49-
49+
with:
50+
fetch-depth: 0
51+
5052
- name: Setup .NET Core
5153
if: matrix.target == 'netcoreapp3.1'
5254
uses: actions/setup-dotnet@v1
5355
with:
5456
dotnet-version: '3.1.x'
5557

56-
# NOTE: This is the temporary fix for https://github.com/actions/virtual-environments/issues/1090
57-
- name: Cleanup before restore
58-
if: ${{ matrix.os == 'windows' }}
59-
run: dotnet clean ICSharpCode.SharpZipLib.sln && dotnet nuget locals all --clear
60-
6158
- name: Restore test dependencies
6259
run: dotnet restore
6360

6461
- name: Run tests (Debug)
6562
run: dotnet test -c debug -f ${{ matrix.target }} --no-restore
6663

6764
- name: Run tests (Release)
68-
# Only upload code coverage for windows in an attempt to fix the broken code coverage
69-
if: ${{ matrix.os == 'windows' }}
70-
run: dotnet test -c release -f ${{ matrix.target }} --no-restore --collect="XPlat Code Coverage"
71-
72-
- name: Run tests with coverage (Release)
73-
# Only upload code coverage for windows in an attempt to fix the broken code coverage
74-
if: ${{ matrix.os != 'windows' }}
7565
run: dotnet test -c release -f ${{ matrix.target }} --no-restore
7666

67+
68+
CodeCov:
69+
name: Code Coverage
70+
runs-on: windows-latest
71+
env:
72+
DOTCOVER_VER: 2021.1.2
73+
DOTCOVER_PKG: jetbrains.dotcover.commandlinetools
74+
COVER_SNAPSHOT: SharpZipLib.dcvr
75+
steps:
76+
- uses: actions/checkout@v2
77+
with:
78+
fetch-depth: 0
79+
80+
- name: Setup .NET
81+
uses: actions/setup-dotnet@v1
82+
with:
83+
dotnet-version: '3.1.x'
84+
85+
# NOTE: This is the temporary fix for https://github.com/actions/virtual-environments/issues/1090
86+
- name: Cleanup before restore
87+
run: dotnet clean ICSharpCode.SharpZipLib.sln && dotnet nuget locals all --clear
88+
89+
- name: Install codecov
90+
run: nuget install -o tools -version ${{env.DOTCOVER_VER}} ${{env.DOTCOVER_PKG}}
91+
92+
- name: Add dotcover to path
93+
run: echo "$(pwd)\tools\${{env.DOTCOVER_PKG}}.${{env.DOTCOVER_VER}}\tools" | Out-File -FilePath $env:GITHUB_PATH -Encoding utf8 -Append
94+
95+
- name: Run tests with code coverage
96+
run: dotcover dotnet --output=${{env.COVER_SNAPSHOT}} --filters=-:ICSharpCode.SharpZipLib.Tests -- test -c release
97+
98+
- name: Create code coverage report
99+
run: dotcover report --source=${{env.COVER_SNAPSHOT}} --reporttype=detailedxml --output=dotcover-report.xml
100+
77101
- name: Upload coverage to Codecov
78102
uses: codecov/[email protected]
103+
with:
104+
files: dotcover-report.xml
105+
106+
- name: Upload coverage snapshot artifact
107+
uses: actions/upload-artifact@v2
108+
with:
109+
name: Code coverage snapshot
110+
path: ${{env.COVER_SNAPSHOT}}
79111

80112
Pack:
81-
needs: [Build, Test]
113+
needs: [Build, Test, CodeCov]
82114
runs-on: windows-latest
83115
env:
84116
PKG_SUFFIX: ''

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -253,3 +253,4 @@ paket-files/
253253
/test/ICSharpCode.SharpZipLib.TestBootstrapper/Properties/launchSettings.json
254254
_testRunner/
255255
docs/help/api/.manifest
256+
/benchmark/ICSharpCode.SharpZipLib.Benchmark/BenchmarkDotNet.Artifacts/results

benchmark/ICSharpCode.SharpZipLib.Benchmark/Zip/ZipOutputStream.cs

+21
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System;
22
using System.IO;
3+
using System.Threading.Tasks;
34
using BenchmarkDotNet.Attributes;
45

56
namespace ICSharpCode.SharpZipLib.Benchmark.Zip
@@ -37,5 +38,25 @@ public long WriteZipOutputStream()
3738
return memoryStream.Position;
3839
}
3940
}
41+
42+
[Benchmark]
43+
public async Task<long> WriteZipOutputStreamAsync()
44+
{
45+
using (var memoryStream = new MemoryStream(outputBuffer))
46+
{
47+
using (var zipOutputStream = new SharpZipLib.Zip.ZipOutputStream(memoryStream))
48+
{
49+
zipOutputStream.IsStreamOwner = false;
50+
zipOutputStream.PutNextEntry(new SharpZipLib.Zip.ZipEntry("0"));
51+
52+
for (int i = 0; i < ChunkCount; i++)
53+
{
54+
await zipOutputStream.WriteAsync(inputBuffer, 0, inputBuffer.Length);
55+
}
56+
}
57+
58+
return memoryStream.Position;
59+
}
60+
}
4061
}
4162
}

src/ICSharpCode.SharpZipLib/Zip/Compression/Streams/DeflaterOutputStream.cs

+4-61
Original file line numberDiff line numberDiff line change
@@ -153,37 +153,15 @@ public bool CanPatchEntries
153153

154154
#region Encryption
155155

156-
private string password;
157-
158-
private ICryptoTransform cryptoTransform_;
159-
160156
/// <summary>
161-
/// Returns the 10 byte AUTH CODE to be appended immediately following the AES data stream.
157+
/// The CryptoTransform currently being used to encrypt the compressed data.
162158
/// </summary>
163-
protected byte[] AESAuthCode;
159+
protected ICryptoTransform cryptoTransform_;
164160

165161
/// <summary>
166-
/// Get/set the password used for encryption.
162+
/// Returns the 10 byte AUTH CODE to be appended immediately following the AES data stream.
167163
/// </summary>
168-
/// <remarks>When set to null or if the password is empty no encryption is performed</remarks>
169-
public string Password
170-
{
171-
get
172-
{
173-
return password;
174-
}
175-
set
176-
{
177-
if ((value != null) && (value.Length == 0))
178-
{
179-
password = null;
180-
}
181-
else
182-
{
183-
password = value;
184-
}
185-
}
186-
}
164+
protected byte[] AESAuthCode;
187165

188166
/// <summary>
189167
/// Encrypt a block of data
@@ -202,34 +180,6 @@ protected void EncryptBlock(byte[] buffer, int offset, int length)
202180
cryptoTransform_.TransformBlock(buffer, 0, length, buffer, 0);
203181
}
204182

205-
/// <summary>
206-
/// Initializes encryption keys based on given <paramref name="password"/>.
207-
/// </summary>
208-
/// <param name="password">The password.</param>
209-
protected void InitializePassword(string password)
210-
{
211-
var pkManaged = new PkzipClassicManaged();
212-
byte[] key = PkzipClassic.GenerateKeys(ZipStrings.ConvertToArray(password));
213-
cryptoTransform_ = pkManaged.CreateEncryptor(key, null);
214-
}
215-
216-
/// <summary>
217-
/// Initializes encryption keys based on given password.
218-
/// </summary>
219-
protected void InitializeAESPassword(ZipEntry entry, string rawPassword,
220-
out byte[] salt, out byte[] pwdVerifier)
221-
{
222-
salt = new byte[entry.AESSaltLen];
223-
// Salt needs to be cryptographically random, and unique per file
224-
if (_aesRnd == null)
225-
_aesRnd = RandomNumberGenerator.Create();
226-
_aesRnd.GetBytes(salt);
227-
int blockSize = entry.AESKeySize / 8; // bits to bytes
228-
229-
cryptoTransform_ = new ZipAESTransform(rawPassword, salt, blockSize, true);
230-
pwdVerifier = ((ZipAESTransform)cryptoTransform_).PwdVerifier;
231-
}
232-
233183
#endregion Encryption
234184

235185
#region Deflation Support
@@ -484,12 +434,5 @@ public override void Write(byte[] buffer, int offset, int count)
484434
private bool isClosed_;
485435

486436
#endregion Instance Fields
487-
488-
#region Static Fields
489-
490-
// Static to help ensure that multiple files within a zip will get different random salt
491-
private static RandomNumberGenerator _aesRnd = RandomNumberGenerator.Create();
492-
493-
#endregion Static Fields
494437
}
495438
}

src/ICSharpCode.SharpZipLib/Zip/ZipOutputStream.cs

+64
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using ICSharpCode.SharpZipLib.Checksum;
22
using ICSharpCode.SharpZipLib.Core;
3+
using ICSharpCode.SharpZipLib.Encryption;
34
using ICSharpCode.SharpZipLib.Zip.Compression;
45
using ICSharpCode.SharpZipLib.Zip.Compression.Streams;
56
using System;
@@ -154,6 +155,29 @@ public UseZip64 UseZip64
154155
/// </summary>
155156
public INameTransform NameTransform { get; set; } = new PathTransformer();
156157

158+
/// <summary>
159+
/// Get/set the password used for encryption.
160+
/// </summary>
161+
/// <remarks>When set to null or if the password is empty no encryption is performed</remarks>
162+
public string Password
163+
{
164+
get
165+
{
166+
return password;
167+
}
168+
set
169+
{
170+
if ((value != null) && (value.Length == 0))
171+
{
172+
password = null;
173+
}
174+
else
175+
{
176+
password = value;
177+
}
178+
}
179+
}
180+
157181
/// <summary>
158182
/// Write an unsigned short in little endian byte order.
159183
/// </summary>
@@ -634,6 +658,34 @@ public void CloseEntry()
634658
curEntry = null;
635659
}
636660

661+
/// <summary>
662+
/// Initializes encryption keys based on given <paramref name="password"/>.
663+
/// </summary>
664+
/// <param name="password">The password.</param>
665+
private void InitializePassword(string password)
666+
{
667+
var pkManaged = new PkzipClassicManaged();
668+
byte[] key = PkzipClassic.GenerateKeys(ZipStrings.ConvertToArray(password));
669+
cryptoTransform_ = pkManaged.CreateEncryptor(key, null);
670+
}
671+
672+
/// <summary>
673+
/// Initializes encryption keys based on given password.
674+
/// </summary>
675+
private void InitializeAESPassword(ZipEntry entry, string rawPassword,
676+
out byte[] salt, out byte[] pwdVerifier)
677+
{
678+
salt = new byte[entry.AESSaltLen];
679+
680+
// Salt needs to be cryptographically random, and unique per file
681+
_aesRnd.GetBytes(salt);
682+
683+
int blockSize = entry.AESKeySize / 8; // bits to bytes
684+
685+
cryptoTransform_ = new ZipAESTransform(rawPassword, salt, blockSize, true);
686+
pwdVerifier = ((ZipAESTransform)cryptoTransform_).PwdVerifier;
687+
}
688+
637689
private void WriteEncryptionHeader(long crcValue)
638690
{
639691
offset += ZipConstants.CryptoHeaderSize;
@@ -1010,6 +1062,18 @@ public override void Flush()
10101062
// NOTE: Setting the size for entries before they are added is the best solution!
10111063
private UseZip64 useZip64_ = UseZip64.Dynamic;
10121064

1065+
/// <summary>
1066+
/// The password to use when encrypting archive entries.
1067+
/// </summary>
1068+
private string password;
1069+
10131070
#endregion Instance Fields
1071+
1072+
#region Static Fields
1073+
1074+
// Static to help ensure that multiple files within a zip will get different random salt
1075+
private static RandomNumberGenerator _aesRnd = RandomNumberGenerator.Create();
1076+
1077+
#endregion Static Fields
10141078
}
10151079
}

test/ICSharpCode.SharpZipLib.Tests/BZip2/Bzip2Tests.cs

+4-1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@ namespace ICSharpCode.SharpZipLib.Tests.BZip2
1212
[TestFixture]
1313
public class BZip2Suite
1414
{
15+
// Use the same random seed to guarantee all the code paths are followed
16+
const int RandomSeed = 4;
17+
1518
/// <summary>
1619
/// Basic compress/decompress test BZip2
1720
/// </summary>
@@ -23,7 +26,7 @@ public void BasicRoundTrip()
2326
var outStream = new BZip2OutputStream(ms);
2427

2528
byte[] buf = new byte[10000];
26-
var rnd = new Random();
29+
var rnd = new Random(RandomSeed);
2730
rnd.NextBytes(buf);
2831

2932
outStream.Write(buf, 0, buf.Length);

test/ICSharpCode.SharpZipLib.Tests/Base/InflaterDeflaterTests.cs

+5-2
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@ namespace ICSharpCode.SharpZipLib.Tests.Base
1616
[TestFixture]
1717
public class InflaterDeflaterTestSuite
1818
{
19+
// Use the same random seed to guarantee all the code paths are followed
20+
const int RandomSeed = 5;
21+
1922
private void Inflate(MemoryStream ms, byte[] original, int level, bool zlib)
2023
{
2124
byte[] buf2 = new byte[original.Length];
@@ -60,7 +63,7 @@ private MemoryStream Deflate(byte[] data, int level, bool zlib)
6063
private static byte[] GetRandomTestData(int size)
6164
{
6265
byte[] buffer = new byte[size];
63-
var rnd = new Random();
66+
var rnd = new Random(RandomSeed);
6467
rnd.NextBytes(buffer);
6568

6669
return buffer;
@@ -184,7 +187,7 @@ public async Task InflateDeflateZlibAsync([Range(0, 9)] int level)
184187
private int runLevel;
185188
private bool runZlib;
186189
private long runCount;
187-
private readonly Random runRandom = new Random(5);
190+
private readonly Random runRandom = new Random(RandomSeed);
188191

189192
private void DeflateAndInflate(byte[] buffer)
190193
{

test/ICSharpCode.SharpZipLib.Tests/ICSharpCode.SharpZipLib.Tests.csproj

-5
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,10 @@
88
</PropertyGroup>
99

1010
<ItemGroup>
11-
<PackageReference Include="coverlet.collector" Version="3.0.3">
12-
<PrivateAssets>all</PrivateAssets>
13-
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
14-
</PackageReference>
1511
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.9.1" />
1612
<PackageReference Include="nunit" Version="3.13.1" />
1713
<PackageReference Include="nunit.console" Version="3.12.0" />
1814
<PackageReference Include="NUnit3TestAdapter" Version="3.17.0" />
19-
<PackageReference Include="OpenCover" Version="4.6.519" />
2015
<PackageReference Include="System.Text.Encoding.CodePages" Version="4.4.0" />
2116
<PackageReference Include="System.ValueTuple" Version="4.5.0" />
2217
</ItemGroup>

0 commit comments

Comments
 (0)