Downloader checksum output information and digester usage

Add more output information around type of checksum being validated.
Use builtin Digest#file to read target file for generation of hexdigest.
This commit is contained in:
Chris Roberts 2016-11-14 10:13:58 -08:00
parent da45ca707c
commit beffa70941
4 changed files with 8 additions and 17 deletions

View File

@ -204,10 +204,13 @@ module Vagrant
CHECKSUM_MAP.each do |type, klass|
if checksums[type]
result = checksum_file(klass, path)
@logger.debug("Validating checksum (#{type}) for #{source}. " \
"expected: #{checksums[type]} actual: #{result}")
if checksums[type] != result
raise Errors::DownloaderChecksumError.new(
source: source,
path: path,
type: type,
expected_checksum: checksums[type],
actual_checksum: result
)
@ -224,11 +227,7 @@ module Vagrant
# @return [String] hexdigest result
def checksum_file(digest_class, path)
digester = digest_class.new
File.open(path.to_s, 'rb') do |file|
while(data = file.read(1024))
digester << data
end
end
digester.file(path)
digester.hexdigest
end

View File

@ -739,6 +739,7 @@ en:
checksum!
File source: %{source}
Checsum type: %{type}
Expected checksum: %{expected_checksum}
Calculated checksum: %{actual_checksum}
env_inval: |-

View File

@ -60,13 +60,10 @@ describe "Vagrant::Shell::Provisioner" do
)
}
let(:file){ double("file") }
let(:digest){ double("digest") }
before do
Vagrant::Util::Downloader.any_instance.should_receive(:execute_curl).and_return(true)
expect(File).to receive(:open).with(%r{/dev/null/.+}, "rb").and_yield(file).once
allow(File).to receive(:open).and_call_original
expect(file).to receive(:read).and_return(nil)
allow(digest).to receive(:file).and_return(digest)
expect(Digest::SHA1).to receive(:new).and_return(digest)
expect(digest).to receive(:hexdigest).and_return('INVALID_VALUE')
end
@ -93,13 +90,10 @@ describe "Vagrant::Shell::Provisioner" do
)
}
let(:file){ double("file") }
let(:digest){ double("digest") }
before do
Vagrant::Util::Downloader.any_instance.should_receive(:execute_curl).and_return(true)
expect(File).to receive(:open).with(%r{/dev/null/.+}, "rb").and_yield(file).once
allow(File).to receive(:open).and_call_original
expect(file).to receive(:read).and_return(nil)
allow(digest).to receive(:file).and_return(digest)
expect(Digest::MD5).to receive(:new).and_return(digest)
expect(digest).to receive(:hexdigest).and_return('INVALID_VALUE')
end

View File

@ -98,12 +98,9 @@ describe Vagrant::Util::Downloader do
let(:checksum_expected_value){ 'MD5_CHECKSUM_VALUE' }
let(:checksum_invalid_value){ 'INVALID_VALUE' }
let(:digest){ double("digest") }
let(:file){ double("file") }
before do
allow(digest).to receive(:<<)
allow(File).to receive(:open).and_yield(file)
allow(file).to receive(:read).and_return(nil)
allow(digest).to receive(:file).and_return(digest)
end
[Digest::MD5, Digest::SHA1].each do |klass|