Skip to content

Commit

Permalink
Add missing unit tests
Browse files Browse the repository at this point in the history
The PortAliases public API didn't have unit tests yet, which we
considered necessary to make changes in a safety way.
  • Loading branch information
dgdavid committed Mar 4, 2020
1 parent 3eac7f3 commit 956f298
Showing 1 changed file with 172 additions and 3 deletions.
175 changes: 172 additions & 3 deletions library/network/test/port_aliases_test.rb
Expand Up @@ -20,11 +20,180 @@
CONTENT

describe Yast::PortAliases do
describe ".LoadAndReturnNameToPort" do
before do
allow(::File).to receive(:read).and_return(FILE_CONTENT)
before do
allow(::File).to receive(:read).and_return(FILE_CONTENT)
end

describe ".IsAllowedPortName" do
context "when nil is given" do
it "logs an error" do
expect(Yast::Builtins).to receive(:y2error).with(/Invalid/, nil)

subject.IsAllowedPortName(nil)
end

it "returns false" do
expect(subject.IsAllowedPortName(nil)).to eq(false)
end
end

context "when a number is given" do
context "within the valid port range" do
it "returns true" do
expect(subject.IsAllowedPortName("65535")).to eq(true)
end
end

context "beyond the upper limit" do
it "returns false" do
expect(subject.IsAllowedPortName("65536")).to eq(false)
end
end
context "below the lower limit" do
# FIXME: regexp avoid having negative numbers, which are going to be considered as port name
# instead a port number.
xit "returns false" do
expect(subject.IsAllowedPortName("-1")).to eq(false)
end
end
end

context "when a name is given" do
context "containing only valid chars" do
it "returns true" do
expect(subject.IsAllowedPortName("valid-service.name+")).to eq(true)
end
end

context "containing not valid chars" do
it "returns false" do
expect(subject.IsAllowedPortName("Not valid service name")).to eq(false)
end
end
end
end

describe ".AllowedPortNameOrNumber" do
it "returns an informing message" do
message = subject.AllowedPortNameOrNumber

expect(message).to include("a-z")
expect(message).to include("A-Z")
expect(message).to include("0-9")
expect(message).to include("*+._-")
expect(message).to include("0 to 65535")
end
end

describe ".GetListOfServiceAliases" do
context "when a port number is given" do
before do
allow(Yast::SCR).to receive(:Execute)
.with(anything, /.*grep.*#{port_number}.*\/etc\/services \|.*sed.*/)
.and_return("exit" => 0, "stdout" => command_stdout)
end

context "and there is a service for such port number" do
let(:port_number) { "10541" }
let(:command_stdout) { "MOS-upper\nMOS-upper" }

it "returns a list holding both, the port number and its aliases" do
expect(subject.GetListOfServiceAliases(port_number)).to eq(["10541", "MOS-upper"])
end
end

context "but there is not a service for such port number" do
let(:port_number) { "666" }
let(:command_stdout) { "" }

it "returns a list holding only the given port number" do
expect(subject.GetListOfServiceAliases(port_number)).to eq([port_number])
end
end
end

context "when a port name is given" do
context "and its an allowed port name" do
context "and there is a service for such port name" do
let(:port_name) { "MOS-upper" }

it "returns a list holding both, given name and its port number" do
expect(subject.GetListOfServiceAliases(port_name)).to eq(["10541", "MOS-upper"])
end
end

context "but there is not a service for such port number" do
let(:port_name) { "SomethingWrong" }

it "returns a list holding only the given name" do
expect(subject.GetListOfServiceAliases(port_name)).to eq([port_name])
end
end

context "but it is not an allowed port name" do
let(:port_name) { "Something Not Allowed" }

it "logs an error" do
expect(Yast::Builtins).to receive(:y2error).with(/not allowed/, port_name)

subject.GetListOfServiceAliases(port_name)
end

it "returns a list holding only the given name" do
expect(subject.GetListOfServiceAliases(port_name)).to eq([port_name])
end
end
end
end
end

describe ".IsKnownPortName" do
context "when a known port name is given" do
let(:port_name) { "blocks" }

it "returns true" do
expect(subject.IsKnownPortName(port_name)).to eq(true)
end
end

context "when an unknown port name is given" do
let(:port_name) { "unknown-port" }

it "returns false" do
expect(subject.IsKnownPortName(port_name)).to eq(false)
end
end
end

describe ".GetPortNumber" do
context "when a port number is given" do
let(:port_number) { "80" }

it "returns the Integer port number" do
expect(subject.GetPortNumber(port_number)).to eq(80)
end
end

context "when a port name is given" do
context "and the port is known" do
let(:port_name) { "MOS-lower" }

it "returns its port number" do
expect(subject.GetPortNumber(port_name)).to eq(10540)
end
end

context "but the port is unknown" do
let(:port_name) { "Unknown" }

it "returns nil" do
expect(subject.GetPortNumber(port_name)).to be_nil
end
end
end
end

describe ".LoadAndReturnNameToPort" do
it "returns integer for given port name" do
expect(Yast::PortAliases.LoadAndReturnNameToPort("blocks")).to eq 10288
end
Expand Down

0 comments on commit 956f298

Please sign in to comment.