Skip to content

Commit

Permalink
Merge pull request #1135 from yast/ensure_additional_address_prefix
Browse files Browse the repository at this point in the history
Improve additional addresses prefix length validation
  • Loading branch information
teclator committed Dec 23, 2020
2 parents 3301791 + 0d65349 commit 9f191a1
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 5 deletions.
6 changes: 6 additions & 0 deletions package/yast2-network.changes
@@ -1,3 +1,9 @@
-------------------------------------------------------------------
Wed Dec 23 09:21:16 UTC 2020 - Knut Anderssen <kanderssen@suse.com>

- Improve additional addresses validation (bsc#1174766)
- 4.3.37

-------------------------------------------------------------------
Mon Dec 21 13:56:55 UTC 2020 - Knut Anderssen <kanderssen@suse.com>

Expand Down
34 changes: 29 additions & 5 deletions src/lib/y2network/dialogs/additional_address.rb
Expand Up @@ -47,18 +47,42 @@ def contents
end

def run
ret = super
return ret if ret != :ok || @settings.subnet_prefix.start_with?("/")
ret = nil

netmask = @settings.subnet_prefix
prefix = IPAddr.new("#{netmask}/#{netmask}").prefix
@settings.subnet_prefix = "/#{prefix}"
loop do
ret = super
break if ret != :ok

@settings.subnet_prefix = subnet_prefix_for(@settings.subnet_prefix)

begin
IPAddr.new("#{@settings.ip_address}#{@settings.subnet_prefix}")
break
rescue IPAddr::InvalidAddressError
Yast::Report.Error(
format(_("Invalid Address %s%s"), @settings.ip_address, @settings.subnet_prefix)
)
end
end

ret
end

private

def subnet_prefix_for(value)
return value if value.start_with?("/")

prefix =
if value.size < 3 || value =~ /^\d{3}$/
value.to_i
else
IPAddr.new("#{value}/#{value}").prefix
end

"/#{prefix}"
end

def buttons
[ok_button, cancel_button]
end
Expand Down
23 changes: 23 additions & 0 deletions test/y2network/dialogs/additional_address_test.rb
Expand Up @@ -45,13 +45,36 @@
context "when the modifications are applied" do
let(:ret_code) { :ok }

before do
ip_settings.ip_address = "192.168.20.200"
ip_settings.subnet_prefix = "/24"
end

context "and the address is not valid" do
it "reports a validation error" do
ip_settings.subnet_prefix = "/64"
allow(subject).to receive(:cwm_show).and_return(:ok, :cancel)
expect(Yast::Popup).to receive(:Error).with(/Invalid/)

subject.run
end
end

context "and the modified IP address settings uses a netmask" do
it "converts the netmask to its equivalent prefix length" do
ip_settings.subnet_prefix = "255.255.255.0"
expect { subject.run }
.to change { ip_settings.subnet_prefix }.from("255.255.255.0").to("/24")
end
end

context "and the modified IP address settings uses a prefix length without '/'" do
it "sets the prefix lenght correctly" do
ip_settings.subnet_prefix = "32"
expect { subject.run }
.to change { ip_settings.subnet_prefix }.from("32").to("/32")
end
end
end
end
end
Expand Down

0 comments on commit 9f191a1

Please sign in to comment.