Skip to content

Commit

Permalink
Require MTU passed as positive integer; coerce when parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
runejuhl committed Aug 15, 2019
1 parent 89c8a1e commit 3b5a2b9
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 51 deletions.
2 changes: 1 addition & 1 deletion lib/puppet/provider/network_config/interfaces.rb
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ def self.parse_file(_filename, contents)
case key # rubocop:disable Metrics/BlockNesting
when 'address' then Instance[name].ipaddress = val
when 'netmask' then Instance[name].netmask = val
when 'mtu' then Instance[name].mtu = val
when 'mtu' then Instance[name].mtu = val.to_i
else Instance[name].options[key] << val
end
end
Expand Down
13 changes: 3 additions & 10 deletions lib/puppet/type/network_config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -86,15 +86,8 @@
desc 'The Maximum Transmission Unit size to use for the interface'
validate do |value|
# reject floating point and negative integers
# XXX this lets 1500.0 pass
if value.is_a? Numeric
unless value.integer?
raise ArgumentError, "#{value} is not a valid mtu (must be a positive integer)"
end
else
unless value =~ %r{^\d+$}
raise ArgumentError, "#{value} is not a valid mtu (must be a positive integer)"
end
unless value.is_a?(Numeric) && value.integer?
raise ArgumentError, "#{value} is not a valid mtu (must be a positive integer)"
end

# Intel 82598 & 82599 chips support MTUs up to 16110; is there any
Expand All @@ -106,7 +99,7 @@
# is 42 with a 802.1q header and 46 without.
min_mtu = 42
max_mtu = 65_536
unless (min_mtu..max_mtu).cover?(value.to_i)
unless min_mtu <= value && value <= max_mtu
raise ArgumentError, "#{value} is not in the valid mtu range (#{min_mtu} .. #{max_mtu})"
end
end
Expand Down
44 changes: 4 additions & 40 deletions spec/unit/type/network_config_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -140,26 +140,14 @@
end

describe 'mtu' do
it 'validates a tiny mtu size' do
Puppet::Type.type(:network_config).new(name: 'yay', mtu: '42')
end

it 'validates a tiny mtu size as a number' do
Puppet::Type.type(:network_config).new(name: 'yay', mtu: 42)
end

it 'validates a normal mtu size' do
Puppet::Type.type(:network_config).new(name: 'yay', mtu: '1500')
end

it 'validates a normal mtu size as a number' do
Puppet::Type.type(:network_config).new(name: 'yay', mtu: 1500)
end

it 'validates a large mtu size' do
Puppet::Type.type(:network_config).new(name: 'yay', mtu: '16384')
end

it 'validates a large mtu size as a number' do
Puppet::Type.type(:network_config).new(name: 'yay', mtu: 16_384)
end
Expand All @@ -170,63 +158,39 @@
end.to raise_error(%r{must be a positive integer})
end

it 'fails on values < 42' do
expect do
Puppet::Type.type(:network_config).new(name: 'yay', mtu: '41')
end.to raise_error(%r{is not in the valid mtu range})
end

it 'fails on numeric values < 42' do
expect do
Puppet::Type.type(:network_config).new(name: 'yay', mtu: 41)
end.to raise_error(%r{is not in the valid mtu range})
end

it 'fails on zero' do
expect do
Puppet::Type.type(:network_config).new(name: 'yay', mtu: '0')
end.to raise_error(%r{is not in the valid mtu range})
end

it 'fails on numeric zero' do
expect do
Puppet::Type.type(:network_config).new(name: 'yay', mtu: 0)
end.to raise_error(%r{is not in the valid mtu range})
end

it 'fails on values > 65536' do
expect do
Puppet::Type.type(:network_config).new(name: 'yay', mtu: '65537')
end.to raise_error(%r{is not in the valid mtu range})
end

it 'fails on numeric values > 65536' do
expect do
Puppet::Type.type(:network_config).new(name: 'yay', mtu: 65_537)
end.to raise_error(%r{is not in the valid mtu range})
end

it 'fails on negative values' do
expect do
Puppet::Type.type(:network_config).new(name: 'yay', mtu: '-1500')
end.to raise_error(%r{is not a valid mtu})
end

it 'fails on negative numbers' do
expect do
Puppet::Type.type(:network_config).new(name: 'yay', mtu: -1500)
end.to raise_error(%r{is not in the valid mtu range})
end

it 'fails on non-integer values' do
it 'fails on numeric non-integer values' do
expect do
Puppet::Type.type(:network_config).new(name: 'yay', mtu: '1500.1')
Puppet::Type.type(:network_config).new(name: 'yay', mtu: 1500.1)
end.to raise_error(%r{must be a positive integer})
end

it 'fails on numeric non-integer values' do
it 'fails on integers passed as strings' do
expect do
Puppet::Type.type(:network_config).new(name: 'yay', mtu: 1500.1)
Puppet::Type.type(:network_config).new(name: 'yay', mtu: '1500.1')
end.to raise_error(%r{must be a positive integer})
end
end
Expand Down

0 comments on commit 3b5a2b9

Please sign in to comment.