diff --git a/src/chocolatey.tests/chocolatey.tests.csproj b/src/chocolatey.tests/chocolatey.tests.csproj index 06f30d8af3..647ae43d18 100644 --- a/src/chocolatey.tests/chocolatey.tests.csproj +++ b/src/chocolatey.tests/chocolatey.tests.csproj @@ -203,7 +203,6 @@ - diff --git a/src/chocolatey.tests/infrastructure/cryptography/CryptoHashProviderSpecs.cs b/src/chocolatey.tests/infrastructure/cryptography/CryptoHashProviderSpecs.cs deleted file mode 100644 index ece526e454..0000000000 --- a/src/chocolatey.tests/infrastructure/cryptography/CryptoHashProviderSpecs.cs +++ /dev/null @@ -1,110 +0,0 @@ -// Copyright © 2017 - 2021 Chocolatey Software, Inc -// Copyright © 2011 - 2017 RealDimensions Software, LLC -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -using System; -using System.IO; -using System.Security.Cryptography; -using chocolatey.infrastructure.adapters; -using chocolatey.infrastructure.app; -using chocolatey.infrastructure.cryptography; -using chocolatey.infrastructure.filesystem; -using Moq; -using FluentAssertions; - -namespace chocolatey.tests.infrastructure.cryptography -{ - public class CryptoHashProviderSpecs - { - public abstract class CryptoHashProviderSpecsBase : TinySpec - { - protected CryptoHashProvider Provider; - protected Mock FileSystem = new Mock(); - - public override void Context() - { - Provider = Provider = new CryptoHashProvider(FileSystem.Object); - } - } - - public class When_HashProvider_provides_a_hash : CryptoHashProviderSpecsBase - { - private string _result; - private readonly string _filePath = "c:\\path\\does\\not\\matter.txt"; - private readonly byte[] _byteArray = new byte[] { 23, 25, 27 }; - - public override void Context() - { - base.Context(); - FileSystem.Setup(x => x.FileExists(It.IsAny())).Returns(true); - FileSystem.Setup(x => x.ReadFileBytes(_filePath)).Returns(_byteArray); - } - - public override void Because() - { - _result = Provider.ComputeFileHash(_filePath); - } - - [Fact] - public void Should_provide_the_correct_hash_based_on_a_checksum() - { - var expected = BitConverter.ToString(SHA256.Create().ComputeHash(_byteArray)).Replace("-", string.Empty); - - _result.Should().Be(expected); - } - } - - public class When_HashProvider_attempts_to_provide_a_hash_for_a_file_over_2GB : CryptoHashProviderSpecsBase - { - private string _result; - private readonly string _filePath = "c:\\path\\does\\not\\matter.txt"; - private readonly byte[] _byteArray = new byte[] { 23, 25, 27 }; - private readonly Mock _hashAlgorithm = new Mock(); - - public override void Context() - { - base.Context(); - Provider = new CryptoHashProvider(FileSystem.Object, _hashAlgorithm.Object); - - FileSystem.Setup(x => x.FileExists(It.IsAny())).Returns(true); - FileSystem.Setup(x => x.ReadFileBytes(_filePath)).Returns(_byteArray); - _hashAlgorithm.Setup(x => x.ComputeHash(_byteArray)).Throws(); //IO.IO_FileTooLong2GB (over Int32.MaxValue) - } - - public override void Because() - { - _result = Provider.ComputeFileHash(_filePath); - } - - [Fact] - public void Should_log_a_warning() - { - MockLogger.MessagesFor(LogLevel.Warn).Should().HaveCount(1); - } - - [Fact] - public void Should_not_throw_an_error_itself() - { - //this handles itself - } - - [Fact] - public void Should_provide_an_unchanging_hash_for_a_file_too_big_to_hash() - { - _result.Should().Be(ApplicationParameters.HashProviderFileTooBig); - } - } - } -} diff --git a/src/chocolatey/infrastructure.app/ApplicationParameters.cs b/src/chocolatey/infrastructure.app/ApplicationParameters.cs index 8125f81d03..3ca6fa086b 100644 --- a/src/chocolatey/infrastructure.app/ApplicationParameters.cs +++ b/src/chocolatey/infrastructure.app/ApplicationParameters.cs @@ -173,7 +173,6 @@ public static class Environment public static readonly string ConfigFileTransformExtension = ".install.xdt"; public static readonly string[] ShimDirectorFileExtensions = new string[] { ".gui", ".ignore" }; - public static readonly string HashProviderFileTooBig = "UnableToDetectChanges_FileTooBig"; public static readonly string HashProviderFileLocked = "UnableToDetectChanges_FileLocked"; /// diff --git a/src/chocolatey/infrastructure/cryptography/CryptoHashProvider.cs b/src/chocolatey/infrastructure/cryptography/CryptoHashProvider.cs index 31af0997ab..134d22dca6 100644 --- a/src/chocolatey/infrastructure/cryptography/CryptoHashProvider.cs +++ b/src/chocolatey/infrastructure/cryptography/CryptoHashProvider.cs @@ -93,21 +93,23 @@ public string ComputeFileHash(string filePath) try { - var hash = _hashAlgorithm.ComputeHash(_fileSystem.ReadFileBytes(filePath)); - - return BitConverter.ToString(hash).Replace("-", string.Empty); + using (var fileStream = _fileSystem.open_file_readonly(filePath)) + { + var hash = _hashAlgorithm.ComputeHash(fileStream); + return BitConverter.ToString(hash).Replace("-", string.Empty); + } } catch (IOException ex) { - this.Log().Warn(() => "Error computing hash for '{0}'{1} Hash will be special code for locked file or file too big instead.{1} Captured error:{1} {2}".FormatWith(filePath, Environment.NewLine, ex.Message)); + this.Log().Warn(() => "Error computing hash for '{0}'{1} Hash will be special code for locked file {1} Captured error:{1} {2}".FormatWith(filePath, Environment.NewLine, ex.Message)); if (IsFileLocked(ex)) { return ApplicationParameters.HashProviderFileLocked; } - //IO.IO_FileTooLong2GB (over Int32.MaxValue) - return ApplicationParameters.HashProviderFileTooBig; + //Rethrow if file is not locked + throw; } }