Skip to content
This repository was archived by the owner on Dec 5, 2024. It is now read-only.

Commit 421a876

Browse files
Adding functionality to UnzipTask to check the expected MD5
1 parent f36f0de commit 421a876

File tree

6 files changed

+84
-21
lines changed

6 files changed

+84
-21
lines changed

src/GitHub.Api/IO/IFileSystem.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,5 @@ public interface IFileSystem
4444
char DirectorySeparatorChar { get; }
4545
bool ExistingPathIsDirectory(string path);
4646
void SetCurrentDirectory(string currentDirectory);
47-
byte[] ReadAllBytes(string path);
4847
}
4948
}

src/GitHub.Api/Installer/UnzipTask.cs

Lines changed: 41 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,24 +7,27 @@ namespace GitHub.Unity
77
class UnzipTask: TaskBase
88
{
99
private readonly string archiveFilePath;
10-
private readonly string extractedPath;
10+
private readonly NPath extractedPath;
1111
private readonly IZipHelper zipHelper;
12+
private readonly IFileSystem fileSystem;
13+
private readonly string expectedMD5;
1214
private readonly IProgress<float> zipFileProgress;
1315
private readonly IProgress<long> estimatedDurationProgress;
1416

15-
public UnzipTask(CancellationToken token, string archiveFilePath, string extractedPath,
16-
IProgress<float> zipFileProgress = null, IProgress<long> estimatedDurationProgress = null) :
17-
this(token, archiveFilePath, extractedPath, ZipHelper.Instance, zipFileProgress, estimatedDurationProgress)
17+
public UnzipTask(CancellationToken token, string archiveFilePath, NPath extractedPath, IFileSystem fileSystem, string expectedMD5 = null, IProgress<float> zipFileProgress = null, IProgress<long> estimatedDurationProgress = null) :
18+
this(token, archiveFilePath, extractedPath, ZipHelper.Instance, fileSystem, expectedMD5, zipFileProgress, estimatedDurationProgress)
1819
{
1920

2021
}
2122

22-
public UnzipTask(CancellationToken token, string archiveFilePath, string extractedPath, IZipHelper zipHelper, IProgress<float> zipFileProgress = null, IProgress<long> estimatedDurationProgress = null)
23+
public UnzipTask(CancellationToken token, string archiveFilePath, NPath extractedPath, IZipHelper zipHelper, IFileSystem fileSystem, string expectedMD5 = null, IProgress<float> zipFileProgress = null, IProgress<long> estimatedDurationProgress = null)
2324
: base(token)
2425
{
2526
this.archiveFilePath = archiveFilePath;
2627
this.extractedPath = extractedPath;
2728
this.zipHelper = zipHelper;
29+
this.fileSystem = fileSystem;
30+
this.expectedMD5 = expectedMD5;
2831
this.zipFileProgress = zipFileProgress;
2932
this.estimatedDurationProgress = estimatedDurationProgress;
3033
}
@@ -33,16 +36,43 @@ protected override void Run(bool success)
3336
{
3437
base.Run(success);
3538

36-
UnzipArchive();
37-
}
38-
39-
private void UnzipArchive()
40-
{
4139
Logger.Trace("Unzip File: {0} to Path: {1}", archiveFilePath, extractedPath);
4240

43-
zipHelper.Extract(archiveFilePath, extractedPath, Token, zipFileProgress, estimatedDurationProgress);
41+
try
42+
{
43+
zipHelper.Extract(archiveFilePath, extractedPath, Token, zipFileProgress, estimatedDurationProgress);
44+
}
45+
catch (Exception ex)
46+
{
47+
var message = "Error Unzipping file";
48+
49+
Logger.Error(ex, message);
50+
throw new UnzipTaskException(message);
51+
}
52+
53+
if (expectedMD5 != null)
54+
{
55+
var calculatedMD5 = fileSystem.CalculateFolderMD5(extractedPath);
56+
if (!calculatedMD5.Equals(expectedMD5, StringComparison.InvariantCultureIgnoreCase))
57+
{
58+
extractedPath.DeleteIfExists();
59+
60+
var message = $"Extracted MD5: {calculatedMD5} Does not match expected: {expectedMD5}";
61+
Logger.Error(message);
62+
63+
throw new UnzipTaskException(message);
64+
}
65+
}
4466

4567
Logger.Trace("Completed Unzip");
4668
}
4769
}
70+
71+
public class UnzipTaskException : Exception {
72+
public UnzipTaskException(string message) : base(message)
73+
{ }
74+
75+
public UnzipTaskException(string message, Exception innerException) : base(message, innerException)
76+
{ }
77+
}
4878
}

src/GitHub.Api/Tasks/DownloadTask.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ protected override void Run(bool success)
130130
result = Download();
131131
if (result && ValidationHash != null)
132132
{
133-
var md5 = fileSystem.CalculateMD5(Destination);
133+
var md5 = fileSystem.CalculateFileMD5(Destination);
134134
result = md5.Equals(ValidationHash, StringComparison.CurrentCultureIgnoreCase);
135135

136136
if (!result)

src/tests/IntegrationTests/Download/DownloadTaskTests.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public async Task TestDownloadTask()
3131
var downloadPathBytes = fileSystem.ReadAllBytes(downloadPath);
3232
Logger.Trace("File size {0} bytes", downloadPathBytes.Length);
3333

34-
var md5Sum = fileSystem.CalculateMD5(downloadPath);
34+
var md5Sum = fileSystem.CalculateFileMD5(downloadPath);
3535
md5Sum.Should().Be(TestDownloadMD5.ToUpperInvariant());
3636

3737
var random = new Random();
@@ -48,7 +48,7 @@ public async Task TestDownloadTask()
4848
var downloadHalfPathBytes = fileSystem.ReadAllBytes(downloadHalfPath);
4949
Logger.Trace("File size {0} Bytes", downloadHalfPathBytes.Length);
5050

51-
md5Sum = fileSystem.CalculateMD5(downloadPath);
51+
md5Sum = fileSystem.CalculateFileMD5(downloadPath);
5252
md5Sum.Should().Be(TestDownloadMD5.ToUpperInvariant());
5353
}
5454

src/tests/IntegrationTests/SetUpFixture.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ public void Setup()
1414

1515
Logging.LogAdapter = new MultipleLogAdapter(
1616
new FileLogAdapter($"..\\{DateTime.UtcNow.ToString("yyyyMMddHHmmss")}-integration-tests.log")
17-
//, new ConsoleLogAdapter()
17+
, new ConsoleLogAdapter()
1818
);
1919
}
2020
}

src/tests/IntegrationTests/UnzipTaskTests.cs

Lines changed: 39 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1+
using System;
12
using System.Threading;
23
using System.Threading.Tasks;
4+
using FluentAssertions;
35
using GitHub.Unity;
46
using Microsoft.Win32.SafeHandles;
57
using NSubstitute;
@@ -11,8 +13,10 @@ namespace IntegrationTests
1113
[TestFixture]
1214
class UnzipTaskTests : BaseTaskManagerTest
1315
{
16+
private const string GitZipMD5 = "e6cfc0c294a2312042f27f893dfc9c0a";
17+
1418
[Test]
15-
public void UnzipTest()
19+
public void TaskSucceeds()
1620
{
1721
InitializeTaskManager();
1822

@@ -22,14 +26,11 @@ public void UnzipTest()
2226
var destinationPath = TestBasePath.Combine("git_zip").CreateDirectory();
2327
var archiveFilePath = AssemblyResources.ToFile(ResourceType.Platform, "git.zip", destinationPath, Environment);
2428

25-
Logger.Trace("ArchiveFilePath: {0}", archiveFilePath);
26-
Logger.Trace("TestBasePath: {0}", TestBasePath);
27-
2829
var extractedPath = TestBasePath.Combine("git_zip_extracted").CreateDirectory();
2930

3031
var zipProgress = 0;
3132
Logger.Trace("Pct Complete {0}%", zipProgress);
32-
var unzipTask = new UnzipTask(CancellationToken.None, archiveFilePath, extractedPath,
33+
var unzipTask = new UnzipTask(CancellationToken.None, archiveFilePath, extractedPath, Environment.FileSystem, GitZipMD5,
3334
new Progress<float>(zipFileProgress => {
3435
var zipFileProgressInteger = (int) (zipFileProgress * 100);
3536
if (zipProgress != zipFileProgressInteger)
@@ -40,6 +41,39 @@ public void UnzipTest()
4041
}));
4142

4243
unzipTask.Start().Wait();
44+
45+
extractedPath.DirectoryExists().Should().BeTrue();
46+
}
47+
48+
[Test]
49+
public void TaskFailsWhenMD5Incorect()
50+
{
51+
InitializeTaskManager();
52+
53+
var cacheContainer = Substitute.For<ICacheContainer>();
54+
Environment = new IntegrationTestEnvironment(cacheContainer, TestBasePath, SolutionDirectory);
55+
56+
var destinationPath = TestBasePath.Combine("git_zip").CreateDirectory();
57+
var archiveFilePath = AssemblyResources.ToFile(ResourceType.Platform, "git.zip", destinationPath, Environment);
58+
59+
var extractedPath = TestBasePath.Combine("git_zip_extracted").CreateDirectory();
60+
61+
62+
var failed = false;
63+
Exception exception = null;
64+
65+
var unzipTask = new UnzipTask(CancellationToken.None, archiveFilePath, extractedPath, Environment.FileSystem, "AABBCCDD")
66+
.Finally((b, ex) => {
67+
failed = true;
68+
exception = ex;
69+
});
70+
71+
unzipTask.Start().Wait();
72+
73+
extractedPath.DirectoryExists().Should().BeFalse();
74+
failed.Should().BeTrue();
75+
exception.Should().NotBeNull();
76+
exception.Should().BeOfType<UnzipTaskException>();
4377
}
4478
}
4579
}

0 commit comments

Comments
 (0)