Skip to content

Commit 4b15c97

Browse files
authored
Merge pull request #680 from DavidS/modules-3980-fix-ipv4-regex
(MODULES-3980) Fix ipv4 regex validator
2 parents 1e9128f + ea92941 commit 4b15c97

File tree

6 files changed

+67
-52
lines changed

6 files changed

+67
-52
lines changed

spec/aliases/ipv4_spec.rb

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,25 +3,15 @@
33
if Puppet.version.to_f >= 4.5
44
describe 'test::ipv4', type: :class do
55
describe 'accepts ipv4 addresses' do
6-
[
7-
'224.0.0.0',
8-
'255.255.255.255',
9-
'0.0.0.0',
10-
'192.88.99.0'
11-
].each do |value|
6+
SharedData::IPV4_PATTERNS.each do |value|
127
describe value.inspect do
138
let(:params) {{ value: value }}
149
it { is_expected.to compile }
1510
end
1611
end
1712
end
1813
describe 'rejects other values' do
19-
[
20-
'nope',
21-
'77',
22-
'4.4.4',
23-
'2001:0db8:85a3:0000:0000:8a2e:0370:73342001:0db8:85a3:0000:0000:8a2e:0370:7334'
24-
].each do |value|
14+
SharedData::IPV4_NEGATIVE_PATTERNS.each do |value|
2515
describe value.inspect do
2616
let(:params) {{ value: value }}
2717
it { is_expected.to compile.and_raise_error(/parameter 'value' expects a match for Stdlib::Compat::Ipv4/) }
Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,17 @@
11
require 'spec_helper'
22

33
describe 'is_ipv4_address' do
4-
4+
55
it { is_expected.not_to eq(nil) }
66
it { is_expected.to run.with_params().and_raise_error(Puppet::ParseError, /wrong number of arguments/i) }
7-
it { is_expected.to run.with_params('1.2.3.4').and_return(true) }
8-
it { is_expected.to run.with_params('1.2.3.255').and_return(true) }
9-
it { is_expected.to run.with_params('1.2.3').and_return(false) }
10-
it { is_expected.to run.with_params('1.2.3.4.5').and_return(false) }
11-
it { is_expected.to run.with_params('').and_return(false) }
12-
it { is_expected.to run.with_params('one').and_return(false) }
7+
8+
SharedData::IPV4_PATTERNS.each do |value|
9+
it { is_expected.to run.with_params(value).and_return(true) }
10+
end
11+
12+
SharedData::IPV4_NEGATIVE_PATTERNS.each do |value|
13+
it { is_expected.to run.with_params(value).and_return(false) }
14+
end
1315

1416
context 'Checking for deprecation warning', if: Puppet.version.to_f < 4.0 do
1517
after(:all) do
@@ -19,12 +21,12 @@
1921
it 'should display a single deprecation' do
2022
ENV['STDLIB_LOG_DEPRECATIONS'] = "true"
2123
scope.expects(:warning).with(includes('This method is deprecated'))
22-
is_expected.to run.with_params('1.2.3.4').and_return(true)
24+
is_expected.to run.with_params(SharedData::IPV4_PATTERNS.first).and_return(true)
2325
end
2426
it 'should display no warning for deprecation' do
2527
ENV['STDLIB_LOG_DEPRECATIONS'] = "false"
2628
scope.expects(:warning).with(includes('This method is deprecated')).never
27-
is_expected.to run.with_params('1.2.3.4').and_return(true)
29+
is_expected.to run.with_params(SharedData::IPV4_PATTERNS.first).and_return(true)
2830
end
2931
end
3032
end

spec/functions/validate_ipv4_address_spec.rb

Lines changed: 13 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -15,44 +15,27 @@
1515
it 'should display a single deprecation' do
1616
ENV['STDLIB_LOG_DEPRECATIONS'] = "true"
1717
scope.expects(:warning).with(includes('This method is deprecated'))
18-
is_expected.to run.with_params('1.2.3.4')
18+
is_expected.to run.with_params(SharedData::IPV4_PATTERNS.first)
1919
end
2020
it 'should display no warning for deprecation' do
2121
ENV['STDLIB_LOG_DEPRECATIONS'] = "false"
2222
scope.expects(:warning).with(includes('This method is deprecated')).never
23-
is_expected.to run.with_params('1.2.3.4')
23+
is_expected.to run.with_params(SharedData::IPV4_PATTERNS.first)
2424
end
25-
end
25+
end
2626

27-
describe 'valid inputs' do
28-
it { is_expected.to run.with_params('0.0.0.0') }
29-
it { is_expected.to run.with_params('8.8.8.8') }
30-
it { is_expected.to run.with_params('127.0.0.1') }
31-
it { is_expected.to run.with_params('10.10.10.10') }
32-
it { is_expected.to run.with_params('194.232.104.150') }
33-
it { is_expected.to run.with_params('244.24.24.24') }
34-
it { is_expected.to run.with_params('255.255.255.255') }
35-
it { is_expected.to run.with_params('1.2.3.4', '5.6.7.8') }
36-
context 'with netmasks' do
37-
it { is_expected.to run.with_params('8.8.8.8/0') }
38-
it { is_expected.to run.with_params('8.8.8.8/16') }
39-
it { is_expected.to run.with_params('8.8.8.8/32') }
40-
it { is_expected.to run.with_params('8.8.8.8/255.255.0.0') }
41-
end
27+
SharedData::IPV4_PATTERNS.each do |value|
28+
it { is_expected.to run.with_params(value) }
29+
end
30+
31+
SharedData::IPV4_NEGATIVE_PATTERNS.each do |value|
32+
it { is_expected.to run.with_params(value).and_raise_error(Puppet::ParseError, /is not a valid IPv4/) }
4233
end
4334

4435
describe 'invalid inputs' do
45-
it { is_expected.to run.with_params({}).and_raise_error(Puppet::ParseError, /is not a string/) }
46-
it { is_expected.to run.with_params(1).and_raise_error(Puppet::ParseError, /is not a string/) }
47-
it { is_expected.to run.with_params(true).and_raise_error(Puppet::ParseError, /is not a string/) }
48-
it { is_expected.to run.with_params('one').and_raise_error(Puppet::ParseError, /is not a valid IPv4/) }
49-
it { is_expected.to run.with_params('0.0.0').and_raise_error(Puppet::ParseError, /is not a valid IPv4/) }
50-
it { is_expected.to run.with_params('0.0.0.256').and_raise_error(Puppet::ParseError, /is not a valid IPv4/) }
51-
it { is_expected.to run.with_params('0.0.0.0.0').and_raise_error(Puppet::ParseError, /is not a valid IPv4/) }
52-
it { is_expected.to run.with_params('affe::beef').and_raise_error(Puppet::ParseError, /is not a valid IPv4/) }
53-
it { is_expected.to run.with_params('1.2.3.4', {}).and_raise_error(Puppet::ParseError, /is not a string/) }
54-
it { is_expected.to run.with_params('1.2.3.4', 1).and_raise_error(Puppet::ParseError, /is not a string/) }
55-
it { is_expected.to run.with_params('1.2.3.4', true).and_raise_error(Puppet::ParseError, /is not a string/) }
56-
it { is_expected.to run.with_params('1.2.3.4', 'one').and_raise_error(Puppet::ParseError, /is not a valid IPv4/) }
36+
[ {}, [], 1, true ].each do |invalid|
37+
it { is_expected.to run.with_params(invalid).and_raise_error(Puppet::ParseError, /is not a string/) }
38+
it { is_expected.to run.with_params(SharedData::IPV4_PATTERNS.first, invalid).and_raise_error(Puppet::ParseError, /is not a string/) }
39+
end
5740
end
5841
end

spec/spec_helper_local.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
# automatically load any shared examples or contexts
2+
Dir["./spec/support/**/*.rb"].sort.each { |f| require f }
13

24
# hack to enable all the expect syntax (like allow_any_instance_of) in rspec-puppet examples
35
RSpec::Mocks::Syntax.enable_expect(RSpec::Puppet::ManifestMatchers)

spec/support/shared_data.rb

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
module SharedData
2+
IPV4_PATTERNS = [
3+
'0.0.0.0',
4+
'1.2.3.4',
5+
'10.10.10.10',
6+
'127.0.0.1',
7+
'192.88.99.0',
8+
'194.232.104.150',
9+
'224.0.0.0',
10+
'244.24.24.24',
11+
'255.255.255.255',
12+
'8.8.8.8',
13+
'8.8.8.8/0',
14+
'8.8.8.8/16',
15+
'8.8.8.8/255.255.0.0',
16+
'8.8.8.8/32',
17+
]
18+
IPV4_NEGATIVE_PATTERNS = [
19+
'',
20+
'0000',
21+
'0.0.0.0.',
22+
'0.0.0.0./0.0.0.0.',
23+
'0.0.0.0./1',
24+
'0.0.0.0.0',
25+
'0.0.0.0/0.0.0.0.',
26+
'0.0.0.256',
27+
'0.0.0',
28+
'1.2.3.4.5',
29+
'1.2.3',
30+
'10.010.10.10',
31+
'2001:0db8:85a3:0000:0000:8a2e:0370:73342001:0db8:85a3:0000:0000:8a2e:0370:7334',
32+
'4.4.4',
33+
'77',
34+
'9999.9999.9999.9999',
35+
'affe::beef',
36+
'nope',
37+
]
38+
end

types/compat/ipv4.pp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
# Emulate the validate_ipv4_address and is_ipv4_address functions
2-
type Stdlib::Compat::Ipv4 = Pattern[/^(\d+)\.(\d+)\.(\d+)\.(\d+)$/]
2+
type Stdlib::Compat::Ipv4 = Pattern[/^((([0-9](?!\d)|[1-9][0-9](?!\d)|1[0-9]{2}(?!\d)|2[0-4][0-9](?!\d)|25[0-5](?!\d))[.]){3}([0-9](?!\d)|[1-9][0-9](?!\d)|1[0-9]{2}(?!\d)|2[0-4][0-9](?!\d)|25[0-5](?!\d)))(\/((([0-9](?!\d)|[1-9][0-9](?!\d)|1[0-9]{2}(?!\d)|2[0-4][0-9](?!\d)|25[0-5](?!\d))[.]){3}([0-9](?!\d)|[1-9][0-9](?!\d)|1[0-9]{2}(?!\d)|2[0-4][0-9](?!\d)|25[0-5](?!\d))|[0-9]+))?$/]

0 commit comments

Comments
 (0)