core: checksums supported when adding box from metadata

This commit is contained in:
Mitchell Hashimoto 2014-03-01 12:03:22 +01:00
parent 23bd397cb8
commit 5018e4d8a4
2 changed files with 87 additions and 35 deletions

View File

@ -247,43 +247,12 @@ module Vagrant
metadata_version.version,
metadata_provider.name,
url,
env)
env,
checksum: metadata_provider.checksum,
checksum_type: metadata_provider.checksum_type,
)
end
=begin
# Determine the checksum type to use
checksum = (env[:box_checksum] || "").to_s
checksum_klass = nil
if env[:box_checksum_type]
checksum_klass = case env[:box_checksum_type].to_sym
when :md5
Digest::MD5
when :sha1
Digest::SHA1
when :sha256
Digest::SHA2
else
raise Errors::BoxChecksumInvalidType,
type: env[:box_checksum_type].to_s
end
end
if checksum_klass
@logger.info("Validating checksum with #{checksum_klass}")
@logger.info("Expected checksum: #{checksum}")
env[:ui].info(I18n.t("vagrant.actions.box.add.checksumming",
name: box_name))
actual = FileChecksum.new(@temp_path, checksum_klass).checksum
if actual != checksum
raise Errors::BoxChecksumMismatch,
actual: actual,
expected: checksum
end
end
end
=end
protected
# Shared helper to add a box once you know various details

View File

@ -331,6 +331,89 @@ describe Vagrant::Action::Builtin::BoxAdd do
end
end
it "adds from HTTP URL with a checksum" do
box_path = iso_env.box2_file(:virtualbox)
tf = Tempfile.new(["vagrant", ".json"]).tap do |f|
f.write(<<-RAW)
{
"name": "foo/bar",
"versions": [
{
"version": "0.5"
},
{
"version": "0.7",
"providers": [
{
"name": "virtualbox",
"url": "#{box_path}",
"checksum_type": "sha1",
"checksum": "#{checksum(box_path)}"
}
]
}
]
}
RAW
f.close
end
md_path = Pathname.new(tf.path)
with_web_server(md_path) do |port|
env[:box_url] = "http://127.0.0.1:#{port}/#{md_path.basename}"
box_collection.should_receive(:add).with do |path, name, version, **opts|
expect(name).to eq("foo/bar")
expect(version).to eq("0.7")
expect(checksum(path)).to eq(checksum(box_path))
expect(opts[:metadata_url]).to eq(env[:box_url])
true
end.and_return(box)
app.should_receive(:call).with(env)
subject.call(env)
end
end
it "raises an exception if checksum given but not correct" do
box_path = iso_env.box2_file(:virtualbox)
tf = Tempfile.new(["vagrant", ".json"]).tap do |f|
f.write(<<-RAW)
{
"name": "foo/bar",
"versions": [
{
"version": "0.5"
},
{
"version": "0.7",
"providers": [
{
"name": "virtualbox",
"url": "#{box_path}",
"checksum_type": "sha1",
"checksum": "thisisnotcorrect"
}
]
}
]
}
RAW
f.close
end
md_path = Pathname.new(tf.path)
with_web_server(md_path) do |port|
env[:box_url] = "http://127.0.0.1:#{port}/#{md_path.basename}"
box_collection.should_receive(:add).never
app.should_receive(:call).never
expect { subject.call(env) }.
to raise_error(Vagrant::Errors::BoxChecksumMismatch)
end
end
it "raises an error if no Vagrant server is set" do
tf = Tempfile.new("foo")