Merge pull request #11679 from soapy1/invalid-mask

Validate netmask value from VirtualBox
This commit is contained in:
Sophia Castellarin 2020-06-16 09:36:19 -05:00 committed by GitHub
commit 71185e3756
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 33 additions and 9 deletions

View File

@ -3,11 +3,21 @@ require "ipaddr"
module Vagrant
module Util
module NetworkIP
DEFAULT_MASK = "255.255.255.0".freeze
LOGGER = Log4r::Logger.new("vagrant::util::NetworkIP")
# Returns the network address of the given IP and subnet.
#
# @return [String]
def network_address(ip, subnet)
IPAddr.new(ip).mask(subnet).to_s
begin
IPAddr.new(ip).mask(subnet).to_s
rescue IPAddr::InvalidPrefixError
LOGGER.warn("Provided mask '#{subnet}' is invalid. Falling back to using mask '#{DEFAULT_MASK}'")
IPAddr.new(ip).mask(DEFAULT_MASK).to_s
end
end
end
end

View File

@ -5,33 +5,47 @@ require "vagrant/util/network_ip"
describe Vagrant::Util::NetworkIP do
let(:klass) do
Class.new do
extend Vagrant::Util::NetworkIP
include Vagrant::Util::NetworkIP
end
end
describe "network address" do
subject { klass.new }
describe "#network_address" do
it "calculates it properly" do
expect(klass.network_address("192.168.2.234", "255.255.255.0")).to eq("192.168.2.0")
expect(subject.network_address("192.168.2.234", "255.255.255.0")).to eq("192.168.2.0")
end
it "calculates it properly with integer submask" do
expect(klass.network_address("192.168.2.234", "24")).to eq("192.168.2.0")
expect(subject.network_address("192.168.2.234", "24")).to eq("192.168.2.0")
end
it "calculates it properly with integer submask" do
expect(klass.network_address("192.168.2.234", 24)).to eq("192.168.2.0")
expect(subject.network_address("192.168.2.234", 24)).to eq("192.168.2.0")
end
it "calculates it properly for IPv6" do
expect(klass.network_address("fde4:8dba:82e1::c4", "64")).to eq("fde4:8dba:82e1::")
expect(subject.network_address("fde4:8dba:82e1::c4", "64")).to eq("fde4:8dba:82e1::")
end
it "calculates it properly for IPv6" do
expect(klass.network_address("fde4:8dba:82e1::c4", 64)).to eq("fde4:8dba:82e1::")
expect(subject.network_address("fde4:8dba:82e1::c4", 64)).to eq("fde4:8dba:82e1::")
end
it "calculates it properly for IPv6 for string mask" do
expect(klass.network_address("fde4:8dba:82e1::c4", "ffff:ffff:ffff:ffff::")).to eq("fde4:8dba:82e1::")
expect(subject.network_address("fde4:8dba:82e1::c4", "ffff:ffff:ffff:ffff::")).to eq("fde4:8dba:82e1::")
end
it "recovers from invalid netmask" do
# The mask function will produce an error for ruby >= 2.5
# If using a version of ruby that produces and error, then
# test to ensure `subject.network_address` produces expected
# results.
begin
IPAddr.new("192.168.2.234").mask("1.2.3.4")
rescue IPAddr::InvalidPrefixError
expect(subject.network_address("192.168.2.234", "1.2.3.4")).to eq("192.168.2.0")
end
end
end
end