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

Commit e6ad7a6

Browse files
Merge pull request #563 from github-for-unity/features/unzip-task-extract-md5
Adding functionality to UnzipTask to validate md5 after extraction
2 parents d58984e + ad5d90d commit e6ad7a6

File tree

2 files changed

+80
-16
lines changed

2 files changed

+80
-16
lines changed

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/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)