From f402bc342a1e5093513b03cf3b990af3a13c94c8 Mon Sep 17 00:00:00 2001 From: Jan Vansteenkiste Date: Wed, 22 Aug 2012 17:11:23 +0200 Subject: [PATCH] Added host_to_mask method and added tests for it --- lib/puppet/util/firewall.rb | 14 ++++++++++++++ spec/unit/puppet/util/firewall_spec.rb | 18 ++++++++++++++++++ 2 files changed, 32 insertions(+) diff --git a/lib/puppet/util/firewall.rb b/lib/puppet/util/firewall.rb index 13c1a89..610b603 100644 --- a/lib/puppet/util/firewall.rb +++ b/lib/puppet/util/firewall.rb @@ -116,6 +116,20 @@ module Puppet::Util::Firewall value.cidr end + # Takes an address mask and converts the host portion to CIDR notation. + # + # This takes into account you can negate a mask but follows all rules + # defined in host_to_ip for the host/address part. + # + def host_to_mask(value) + match = value.match /(!)\s?(.*)$/ + return host_to_ip(value) unless match + + cidr = host_to_ip(match[2]) + return nil if cidr == nil + "#{match[1]} #{cidr}" + end + # Validates the argument is int or hex, and returns valid hex # conversion of the value or nil otherwise. def to_hex32(value) diff --git a/spec/unit/puppet/util/firewall_spec.rb b/spec/unit/puppet/util/firewall_spec.rb index be1576f..2d1ddb0 100644 --- a/spec/unit/puppet/util/firewall_spec.rb +++ b/spec/unit/puppet/util/firewall_spec.rb @@ -25,6 +25,24 @@ describe 'Puppet::Util::Firewall' do specify { subject.host_to_ip('::/0').should == nil } end + describe '#host_to_mask' do + subject { resource } + specify { subject.host_to_mask('puppetlabs.com').should == '96.126.112.51/32' } + specify { subject.host_to_mask('!puppetlabs.com').should == '! 96.126.112.51/32' } + specify { subject.host_to_mask('96.126.112.51').should == '96.126.112.51/32' } + specify { subject.host_to_mask('!96.126.112.51').should == '! 96.126.112.51/32' } + specify { subject.host_to_mask('96.126.112.51/32').should == '96.126.112.51/32' } + specify { subject.host_to_mask('! 96.126.112.51/32').should == '! 96.126.112.51/32' } + specify { subject.host_to_mask('2001:db8:85a3:0:0:8a2e:370:7334').should == '2001:db8:85a3::8a2e:370:7334/128' } + specify { subject.host_to_mask('!2001:db8:85a3:0:0:8a2e:370:7334').should == '! 2001:db8:85a3::8a2e:370:7334/128' } + specify { subject.host_to_mask('2001:db8:1234::/48').should == '2001:db8:1234::/48' } + specify { subject.host_to_mask('! 2001:db8:1234::/48').should == '! 2001:db8:1234::/48' } + specify { subject.host_to_mask('0.0.0.0/0').should == nil } + specify { subject.host_to_mask('!0.0.0.0/0').should == nil } + specify { subject.host_to_mask('::/0').should == nil } + specify { subject.host_to_mask('! ::/0').should == nil } + end + describe '#icmp_name_to_number' do describe 'proto unsupported' do subject { resource } -- 2.45.2