Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/Tasks.UnitTests/VerifyFileHash_Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ public void VerifyFileChecksum_FailsForUnknownHashEncoding()
{
new VerifyFileHash
{
TaskEnvironment = TaskEnvironmentHelper.CreateForTest(),
File = Path.Combine(AppContext.BaseDirectory, "TestResources", "lorem.bin"),
BuildEngine = _mockEngine,
Algorithm = "SHA256",
Expand All @@ -45,6 +46,7 @@ public void VerifyFileChecksum_FailsForUnknownAlgorithmName()
{
new VerifyFileHash
{
TaskEnvironment = TaskEnvironmentHelper.CreateForTest(),
File = Path.Combine(AppContext.BaseDirectory, "TestResources", "lorem.bin"),
BuildEngine = _mockEngine,
Algorithm = "BANANA",
Expand All @@ -63,6 +65,7 @@ public void VerifyFileChecksum_FailsForFileNotFound()
{
new VerifyFileHash
{
TaskEnvironment = TaskEnvironmentHelper.CreateForTest(),
File = Path.Combine(AppContext.BaseDirectory, "this_does_not_exist.txt"),
BuildEngine = _mockEngine,
Algorithm = "BANANA",
Expand All @@ -84,6 +87,7 @@ public void VerifyFileChecksum_FailsForMismatch(string algoritm, string hash)
{
VerifyFileHash task = new VerifyFileHash
{
TaskEnvironment = TaskEnvironmentHelper.CreateForTest(),
File = Path.Combine(AppContext.BaseDirectory, "TestResources", "lorem.bin"),
BuildEngine = _mockEngine,
Algorithm = algoritm,
Expand All @@ -103,6 +107,7 @@ public void VerifyFileChecksum_Pass(TestBinary testBinary)
{
VerifyFileHash task = new VerifyFileHash
{
TaskEnvironment = TaskEnvironmentHelper.CreateForTest(),
File = testBinary.FilePath,
BuildEngine = _mockEngine,
Algorithm = testBinary.HashAlgorithm,
Expand Down
16 changes: 11 additions & 5 deletions src/Tasks/FileIO/VerifyFileHash.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,12 @@ namespace Microsoft.Build.Tasks
/// <summary>
/// Verifies that a file matches the expected file hash.
/// </summary>
public sealed class VerifyFileHash : TaskExtension, ICancelableTask
[MSBuildMultiThreadableTask]
public sealed class VerifyFileHash : TaskExtension, ICancelableTask, IMultiThreadableTask
{
/// <inheritdoc />
public TaskEnvironment TaskEnvironment { get; set; }

/// <summary>
/// The file path.
/// </summary>
Expand All @@ -39,9 +43,11 @@ public sealed class VerifyFileHash : TaskExtension, ICancelableTask

public override bool Execute()
{
if (!FileSystems.Default.FileExists(File))
AbsolutePath filePath = TaskEnvironment.GetAbsolutePath(File);

if (!FileSystems.Default.FileExists(filePath))
{
Log.LogErrorWithCodeFromResources("FileHash.FileNotFound", File);
Log.LogErrorWithCodeFromResources("FileHash.FileNotFound", filePath.OriginalValue);
return false;
}

Expand All @@ -57,14 +63,14 @@ public override bool Execute()
return false;
}

byte[] hash = GetFileHash.ComputeHash(algorithmFactory, File, _cancellationTokenSource.Token);
byte[] hash = GetFileHash.ComputeHash(algorithmFactory, filePath, _cancellationTokenSource.Token);
string actualHash = GetFileHash.EncodeHash(encoding, hash);
var comparison = encoding == Tasks.HashEncoding.Hex
? StringComparison.OrdinalIgnoreCase
: StringComparison.Ordinal;
if (!string.Equals(actualHash, Hash, comparison))
{
Log.LogErrorWithCodeFromResources("VerifyFileHash.HashMismatch", File, Algorithm, Hash, actualHash);
Log.LogErrorWithCodeFromResources("VerifyFileHash.HashMismatch", filePath.OriginalValue, Algorithm, Hash, actualHash);
return false;
}

Expand Down
Loading