From beffa709417fdaa20949fdc6914969928c0bdb19 Mon Sep 17 00:00:00 2001 From: Chris Roberts Date: Mon, 14 Nov 2016 10:13:58 -0800 Subject: [PATCH] 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. --- lib/vagrant/util/downloader.rb | 9 ++++----- templates/locales/en.yml | 1 + .../plugins/provisioners/shell/provisioner_test.rb | 10 ++-------- test/unit/vagrant/util/downloader_test.rb | 5 +---- 4 files changed, 8 insertions(+), 17 deletions(-) diff --git a/lib/vagrant/util/downloader.rb b/lib/vagrant/util/downloader.rb index 25a545b0d..b327b5d2f 100644 --- a/lib/vagrant/util/downloader.rb +++ b/lib/vagrant/util/downloader.rb @@ -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 diff --git a/templates/locales/en.yml b/templates/locales/en.yml index 5006beedf..0622a3683 100644 --- a/templates/locales/en.yml +++ b/templates/locales/en.yml @@ -739,6 +739,7 @@ en: checksum! File source: %{source} + Checsum type: %{type} Expected checksum: %{expected_checksum} Calculated checksum: %{actual_checksum} env_inval: |- diff --git a/test/unit/plugins/provisioners/shell/provisioner_test.rb b/test/unit/plugins/provisioners/shell/provisioner_test.rb index 6ba995e04..ef977fb2f 100644 --- a/test/unit/plugins/provisioners/shell/provisioner_test.rb +++ b/test/unit/plugins/provisioners/shell/provisioner_test.rb @@ -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 diff --git a/test/unit/vagrant/util/downloader_test.rb b/test/unit/vagrant/util/downloader_test.rb index df88eed6d..5a9bf2f2f 100644 --- a/test/unit/vagrant/util/downloader_test.rb +++ b/test/unit/vagrant/util/downloader_test.rb @@ -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|