2019-06-11 22:24:35 +00:00
|
|
|
require "helper"
|
|
|
|
require "inspec/resource"
|
|
|
|
require "inspec/resources/host"
|
2015-10-09 17:21:55 +00:00
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
describe "Inspec::Resources::Host" do
|
2015-10-09 17:21:55 +00:00
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
it "check host ping on ubuntu with dig" do
|
2021-09-30 08:56:43 +00:00
|
|
|
resource = MockLoader.new(:ubuntu).load_resource("host", "example.com")
|
2015-10-09 17:27:29 +00:00
|
|
|
_(resource.resolvable?).must_equal true
|
|
|
|
_(resource.reachable?).must_equal true
|
2017-07-11 19:32:52 +00:00
|
|
|
_(resource.ipaddress).must_equal ["12.34.56.78", "2606:2800:220:1:248:1893:25c8:1946"]
|
2019-06-11 22:24:35 +00:00
|
|
|
_(resource.to_s).must_equal "Host example.com"
|
2015-10-09 17:27:29 +00:00
|
|
|
end
|
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
it "check host ping on centos 7" do
|
|
|
|
resource = MockLoader.new(:centos7).load_resource("host", "example.com")
|
2015-10-09 17:27:29 +00:00
|
|
|
_(resource.resolvable?).must_equal true
|
|
|
|
_(resource.reachable?).must_equal true
|
2017-07-11 19:32:52 +00:00
|
|
|
_(resource.ipaddress).must_equal ["12.34.56.78", "2606:2800:220:1:248:1893:25c8:1946"]
|
2019-06-11 22:24:35 +00:00
|
|
|
_(resource.to_s).must_equal "Host example.com"
|
2015-10-09 17:27:29 +00:00
|
|
|
end
|
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
it "check host ping on darwin" do
|
2020-07-01 22:35:23 +00:00
|
|
|
resource = MockLoader.new(:macos10_10).load_resource("host", "example.com")
|
2017-04-13 15:32:04 +00:00
|
|
|
_(resource.resolvable?).must_equal true
|
|
|
|
_(resource.reachable?).must_equal true
|
2017-07-11 19:32:52 +00:00
|
|
|
_(resource.ipaddress).must_equal ["12.34.56.78", "2606:2800:220:1:248:1893:25c8:1946"]
|
2019-06-11 22:24:35 +00:00
|
|
|
_(resource.to_s).must_equal "Host example.com"
|
2017-04-13 15:32:04 +00:00
|
|
|
end
|
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
it "check host ping on windows" do
|
|
|
|
resource = MockLoader.new(:windows).load_resource("host", "microsoft.com")
|
2015-10-09 17:21:55 +00:00
|
|
|
_(resource.resolvable?).must_equal true
|
|
|
|
_(resource.reachable?).must_equal false
|
2019-06-11 22:24:35 +00:00
|
|
|
_(resource.ipaddress).must_equal ["134.170.185.46", "134.170.188.221"]
|
|
|
|
_(resource.to_s).must_equal "Host microsoft.com"
|
2015-10-09 17:21:55 +00:00
|
|
|
end
|
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
it "check host ping on unsupported os" do
|
|
|
|
resource = MockLoader.new(:undefined).load_resource("host", "example.com")
|
2015-10-09 17:21:55 +00:00
|
|
|
_(resource.resolvable?).must_equal false
|
|
|
|
_(resource.reachable?).must_equal false
|
2017-06-09 16:18:51 +00:00
|
|
|
_(resource.ipaddress).must_be_nil
|
2019-06-11 22:24:35 +00:00
|
|
|
_(resource.to_s).must_equal "Host example.com"
|
2015-10-09 17:21:55 +00:00
|
|
|
end
|
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
it "check host tcp on ubuntu" do
|
2021-09-30 08:56:43 +00:00
|
|
|
resource = MockLoader.new(:ubuntu).load_resource("host", "example.com", port: 1234, protocol: "tcp")
|
2017-06-09 16:18:51 +00:00
|
|
|
_(resource.resolvable?).must_equal true
|
|
|
|
_(resource.reachable?).must_equal true
|
2017-07-11 19:32:52 +00:00
|
|
|
_(resource.ipaddress).must_equal ["12.34.56.78", "2606:2800:220:1:248:1893:25c8:1946"]
|
2019-06-11 22:24:35 +00:00
|
|
|
_(resource.to_s).must_equal "Host example.com port 1234 proto tcp"
|
2017-06-09 16:18:51 +00:00
|
|
|
end
|
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
it "check host udp on ubuntu" do
|
2021-09-30 08:56:43 +00:00
|
|
|
resource = MockLoader.new(:ubuntu).load_resource("host", "example.com", port: 1234, protocol: "udp")
|
2018-03-07 13:39:27 +00:00
|
|
|
_(resource.resolvable?).must_equal true
|
|
|
|
_(resource.reachable?).must_equal true
|
|
|
|
_(resource.ipaddress).must_equal ["12.34.56.78", "2606:2800:220:1:248:1893:25c8:1946"]
|
2019-06-11 22:24:35 +00:00
|
|
|
_(resource.to_s).must_equal "Host example.com port 1234 proto udp"
|
2018-03-07 13:39:27 +00:00
|
|
|
end
|
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
it "check host tcp on centos 7" do
|
|
|
|
resource = MockLoader.new(:centos7).load_resource("host", "example.com", port: 1234, protocol: "tcp")
|
2017-06-09 16:18:51 +00:00
|
|
|
_(resource.resolvable?).must_equal true
|
|
|
|
_(resource.reachable?).must_equal true
|
2017-07-11 19:32:52 +00:00
|
|
|
_(resource.ipaddress).must_equal ["12.34.56.78", "2606:2800:220:1:248:1893:25c8:1946"]
|
2019-06-11 22:24:35 +00:00
|
|
|
_(resource.to_s).must_equal "Host example.com port 1234 proto tcp"
|
2017-06-09 16:18:51 +00:00
|
|
|
end
|
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
it "check host udp on centos 7" do
|
|
|
|
resource = MockLoader.new(:centos7).load_resource("host", "example.com", port: 1234, protocol: "udp")
|
2018-03-07 13:39:27 +00:00
|
|
|
_(resource.resolvable?).must_equal true
|
|
|
|
_(resource.reachable?).must_equal true
|
|
|
|
_(resource.ipaddress).must_equal ["12.34.56.78", "2606:2800:220:1:248:1893:25c8:1946"]
|
2019-06-11 22:24:35 +00:00
|
|
|
_(resource.to_s).must_equal "Host example.com port 1234 proto udp"
|
2018-03-07 13:39:27 +00:00
|
|
|
end
|
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
it "check host tcp on darwin" do
|
2020-07-01 22:35:23 +00:00
|
|
|
resource = MockLoader.new(:macos10_10).load_resource("host", "example.com", port: 1234, protocol: "tcp")
|
2017-06-09 16:18:51 +00:00
|
|
|
_(resource.resolvable?).must_equal true
|
|
|
|
_(resource.reachable?).must_equal true
|
2017-07-11 19:32:52 +00:00
|
|
|
_(resource.ipaddress).must_equal ["12.34.56.78", "2606:2800:220:1:248:1893:25c8:1946"]
|
2019-06-11 22:24:35 +00:00
|
|
|
_(resource.to_s).must_equal "Host example.com port 1234 proto tcp"
|
2017-06-09 16:18:51 +00:00
|
|
|
end
|
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
it "check host udp on darwin" do
|
2020-07-01 22:35:23 +00:00
|
|
|
resource = MockLoader.new(:macos10_10).load_resource("host", "example.com", port: 1234, protocol: "udp")
|
2018-03-07 13:39:27 +00:00
|
|
|
_(resource.resolvable?).must_equal true
|
|
|
|
_(resource.reachable?).must_equal true
|
|
|
|
_(resource.ipaddress).must_equal ["12.34.56.78", "2606:2800:220:1:248:1893:25c8:1946"]
|
2019-06-11 22:24:35 +00:00
|
|
|
_(resource.to_s).must_equal "Host example.com port 1234 proto udp"
|
2018-03-07 13:39:27 +00:00
|
|
|
end
|
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
it "check host tcp on windows" do
|
|
|
|
resource = MockLoader.new(:windows).load_resource("host", "microsoft.com", port: 1234, protocol: "tcp")
|
2017-06-09 16:18:51 +00:00
|
|
|
_(resource.resolvable?).must_equal true
|
|
|
|
_(resource.reachable?).must_equal true
|
2019-06-11 22:24:35 +00:00
|
|
|
_(resource.ipaddress).must_equal ["134.170.185.46", "134.170.188.221"]
|
|
|
|
_(resource.to_s).must_equal "Host microsoft.com port 1234 proto tcp"
|
2017-06-09 16:18:51 +00:00
|
|
|
end
|
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
it "check host tcp on unsupported os" do
|
|
|
|
resource = MockLoader.new(:undefined).load_resource("host", "example.com", port: 1234, protocol: "tcp")
|
2017-06-09 16:18:51 +00:00
|
|
|
_(resource.resolvable?).must_equal false
|
|
|
|
_(resource.reachable?).must_equal false
|
|
|
|
_(resource.ipaddress).must_be_nil
|
2019-06-11 22:24:35 +00:00
|
|
|
_(resource.to_s).must_equal "Host example.com port 1234 proto tcp"
|
2017-06-09 16:18:51 +00:00
|
|
|
end
|
2015-10-09 17:21:55 +00:00
|
|
|
end
|
2017-07-05 14:45:30 +00:00
|
|
|
|
|
|
|
describe Inspec::Resources::UnixHostProvider do
|
2018-03-07 13:39:27 +00:00
|
|
|
let(:provider) { Inspec::Resources::UnixHostProvider.new(inspec) }
|
2019-06-11 22:24:35 +00:00
|
|
|
let(:inspec) { mock("inspec-backend") }
|
|
|
|
let(:nc_command) { mock("nc-command") }
|
|
|
|
let(:ncat_command) { mock("ncat-command") }
|
2018-03-07 13:39:27 +00:00
|
|
|
let(:timeout_command) { mock("timeout-command") }
|
|
|
|
let(:strings_command) { mock("strings-command") }
|
|
|
|
|
|
|
|
before do
|
2019-06-11 22:24:35 +00:00
|
|
|
inspec.stubs(:command).with("nc").returns(nc_command)
|
|
|
|
inspec.stubs(:command).with("ncat").returns(ncat_command)
|
|
|
|
inspec.stubs(:command).with("timeout").returns(timeout_command)
|
|
|
|
inspec.stubs(:command).with("gtimeout").returns(timeout_command)
|
2018-03-07 13:39:27 +00:00
|
|
|
inspec.stubs(:command).with("strings `which bash` | grep -qE '/dev/(tcp|udp)/'").returns(strings_command)
|
|
|
|
end
|
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
describe "#resolve_with_dig" do
|
|
|
|
let(:v4_command) { mock("v4_command") }
|
|
|
|
let(:v6_command) { mock("v6_command") }
|
2017-07-05 14:45:30 +00:00
|
|
|
|
2018-03-07 13:39:27 +00:00
|
|
|
before do
|
|
|
|
strings_command.stubs(:exit_status).returns(0)
|
|
|
|
nc_command.stubs(:exist?).returns(false)
|
|
|
|
ncat_command.stubs(:exist?).returns(false)
|
|
|
|
end
|
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
it "returns an array of IP addresses" do
|
|
|
|
ipv4_command_output = <<~EOL
|
|
|
|
a.cname.goes.here
|
|
|
|
another.cname.cool
|
|
|
|
12.34.56.78
|
|
|
|
EOL
|
|
|
|
ipv6_command_output = <<~EOL
|
|
|
|
a.cname.goes.here
|
|
|
|
another.cname.cool
|
|
|
|
2A03:2880:F112:83:FACE:B00C::25DE
|
|
|
|
EOL
|
2017-07-05 14:45:30 +00:00
|
|
|
|
|
|
|
v4_command.stubs(:stdout).returns(ipv4_command_output)
|
|
|
|
v6_command.stubs(:stdout).returns(ipv6_command_output)
|
2019-06-11 22:24:35 +00:00
|
|
|
inspec.stubs(:command).with("dig +short AAAA testdomain.com").returns(v6_command)
|
|
|
|
inspec.stubs(:command).with("dig +short A testdomain.com").returns(v4_command)
|
2019-09-30 22:31:55 +00:00
|
|
|
_(provider.resolve_with_dig("testdomain.com")).must_equal(["12.34.56.78", "2A03:2880:F112:83:FACE:B00C::25DE"])
|
2017-07-05 14:45:30 +00:00
|
|
|
end
|
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
it "returns only v4 addresses if no v6 addresses are available" do
|
|
|
|
ipv4_command_output = <<~EOL
|
|
|
|
a.cname.goes.here
|
|
|
|
another.cname.cool
|
|
|
|
12.34.56.78
|
|
|
|
EOL
|
|
|
|
ipv6_command_output = <<~EOL
|
|
|
|
a.cname.goes.here
|
|
|
|
another.cname.cool
|
|
|
|
EOL
|
2017-07-05 14:45:30 +00:00
|
|
|
|
|
|
|
v4_command.stubs(:stdout).returns(ipv4_command_output)
|
|
|
|
v6_command.stubs(:stdout).returns(ipv6_command_output)
|
2019-06-11 22:24:35 +00:00
|
|
|
inspec.stubs(:command).with("dig +short AAAA testdomain.com").returns(v6_command)
|
|
|
|
inspec.stubs(:command).with("dig +short A testdomain.com").returns(v4_command)
|
2019-09-30 22:31:55 +00:00
|
|
|
_(provider.resolve_with_dig("testdomain.com")).must_equal(["12.34.56.78"])
|
2017-07-05 14:45:30 +00:00
|
|
|
end
|
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
it "returns only v6 addresses if no v4 addresses are available" do
|
|
|
|
ipv4_command_output = <<~EOL
|
|
|
|
a.cname.goes.here
|
|
|
|
another.cname.cool
|
|
|
|
EOL
|
|
|
|
ipv6_command_output = <<~EOL
|
|
|
|
a.cname.goes.here
|
|
|
|
another.cname.cool
|
|
|
|
2A03:2880:F112:83:FACE:B00C::25DE
|
|
|
|
EOL
|
2017-07-05 14:45:30 +00:00
|
|
|
|
|
|
|
v4_command.stubs(:stdout).returns(ipv4_command_output)
|
|
|
|
v6_command.stubs(:stdout).returns(ipv6_command_output)
|
2019-06-11 22:24:35 +00:00
|
|
|
inspec.stubs(:command).with("dig +short AAAA testdomain.com").returns(v6_command)
|
|
|
|
inspec.stubs(:command).with("dig +short A testdomain.com").returns(v4_command)
|
2019-09-30 22:31:55 +00:00
|
|
|
_(provider.resolve_with_dig("testdomain.com")).must_equal(["2A03:2880:F112:83:FACE:B00C::25DE"])
|
2017-07-05 14:45:30 +00:00
|
|
|
end
|
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
it "returns nil if no addresses are available" do
|
|
|
|
ipv4_command_output = <<~EOL
|
|
|
|
a.cname.goes.here
|
|
|
|
another.cname.cool
|
|
|
|
EOL
|
|
|
|
ipv6_command_output = <<~EOL
|
|
|
|
a.cname.goes.here
|
|
|
|
another.cname.cool
|
|
|
|
EOL
|
2017-07-05 14:45:30 +00:00
|
|
|
|
|
|
|
v4_command.stubs(:stdout).returns(ipv4_command_output)
|
|
|
|
v6_command.stubs(:stdout).returns(ipv6_command_output)
|
2019-06-11 22:24:35 +00:00
|
|
|
inspec.stubs(:command).with("dig +short AAAA testdomain.com").returns(v6_command)
|
|
|
|
inspec.stubs(:command).with("dig +short A testdomain.com").returns(v4_command)
|
2019-09-30 22:31:55 +00:00
|
|
|
_(provider.resolve_with_dig("testdomain.com")).must_be_nil
|
2017-07-05 14:45:30 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
describe "#resolve_with_getent" do
|
2018-03-07 13:39:27 +00:00
|
|
|
before do
|
|
|
|
strings_command.stubs(:exit_status).returns(0)
|
|
|
|
nc_command.stubs(:exist?).returns(false)
|
|
|
|
ncat_command.stubs(:exist?).returns(false)
|
|
|
|
end
|
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
it "returns an array of IP addresses when successful" do
|
2017-07-11 19:32:52 +00:00
|
|
|
command_output = "123.123.123.123 STREAM testdomain.com\n2607:f8b0:4004:805::200e STREAM\n"
|
2019-06-11 22:24:35 +00:00
|
|
|
command = mock("getent_command")
|
2017-07-05 14:45:30 +00:00
|
|
|
command.stubs(:stdout).returns(command_output)
|
|
|
|
command.stubs(:exit_status).returns(0)
|
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
inspec.stubs(:command).with("getent ahosts testdomain.com").returns(command)
|
2017-07-05 14:45:30 +00:00
|
|
|
|
2019-09-30 22:31:55 +00:00
|
|
|
_(provider.resolve_with_getent("testdomain.com")).must_equal(["123.123.123.123", "2607:f8b0:4004:805::200e"])
|
2017-07-05 14:45:30 +00:00
|
|
|
end
|
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
it "returns nil if command is not successful" do
|
|
|
|
command = mock("getent_command")
|
2017-07-05 14:45:30 +00:00
|
|
|
command.stubs(:exit_status).returns(1)
|
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
inspec.stubs(:command).with("getent ahosts testdomain.com").returns(command)
|
2017-07-05 14:45:30 +00:00
|
|
|
|
2019-09-30 22:31:55 +00:00
|
|
|
_(provider.resolve_with_getent("testdomain.com")).must_be_nil
|
2017-07-05 14:45:30 +00:00
|
|
|
end
|
|
|
|
end
|
2017-07-06 17:19:16 +00:00
|
|
|
|
2018-03-07 13:39:27 +00:00
|
|
|
describe "#ping" do
|
2019-06-11 22:24:35 +00:00
|
|
|
let(:command_response) { mock("response") }
|
2017-07-06 17:19:16 +00:00
|
|
|
|
2018-03-07 13:39:27 +00:00
|
|
|
before do
|
|
|
|
strings_command.stubs(:exit_status).returns(0)
|
2017-07-06 20:23:57 +00:00
|
|
|
ncat_command.stubs(:exist?).returns(false)
|
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
command_response.stubs(:exit_status).returns("0")
|
|
|
|
command_response.stubs(:stdout).returns("foo")
|
|
|
|
command_response.stubs(:stderr).returns("bar")
|
2017-07-06 20:23:57 +00:00
|
|
|
end
|
|
|
|
|
2018-03-07 13:39:27 +00:00
|
|
|
it "calls netcat if available" do
|
|
|
|
nc_command.stubs(:exist?).returns(true)
|
2019-06-11 22:24:35 +00:00
|
|
|
inspec.expects(:command).with("echo | nc -v -w 1 example.com 1234").returns(command_response)
|
2017-07-06 20:23:57 +00:00
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
provider.ping("example.com", "1234", "tcp")
|
2017-07-06 20:23:57 +00:00
|
|
|
end
|
|
|
|
|
2018-03-07 13:39:27 +00:00
|
|
|
it "uses bash if netcat not available" do
|
|
|
|
nc_command.stubs(:exist?).returns(false)
|
|
|
|
inspec.expects(:command).with('timeout 1 bash -c "< /dev/tcp/example.com/1234"').returns(command_response)
|
2017-07-06 20:23:57 +00:00
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
provider.ping("example.com", "1234", "tcp")
|
2017-07-06 20:23:57 +00:00
|
|
|
end
|
|
|
|
|
2018-03-07 13:39:27 +00:00
|
|
|
it "uses bash if netcat not available on Darwin" do
|
2017-07-06 20:23:57 +00:00
|
|
|
nc_command.stubs(:exist?).returns(false)
|
2018-03-07 13:39:27 +00:00
|
|
|
inspec.expects(:command).with('gtimeout 1 bash -c "< /dev/tcp/example.com/1234"').returns(command_response)
|
2017-07-06 20:23:57 +00:00
|
|
|
|
2018-03-07 13:39:27 +00:00
|
|
|
darwin_provider = Inspec::Resources::DarwinHostProvider.new(inspec)
|
2019-06-11 22:24:35 +00:00
|
|
|
darwin_provider.ping("example.com", "1234", "tcp")
|
2017-07-06 20:23:57 +00:00
|
|
|
end
|
|
|
|
end
|
2017-07-11 19:32:52 +00:00
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
describe "#missing_requirements" do
|
|
|
|
describe "bash with net redirects and no netcat" do
|
2018-03-07 13:39:27 +00:00
|
|
|
before do
|
|
|
|
strings_command.stubs(:exit_status).returns(0)
|
|
|
|
nc_command.stubs(:exist?).returns(false)
|
|
|
|
ncat_command.stubs(:exist?).returns(false)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "returns an empty array if timeout is available" do
|
|
|
|
timeout_command.stubs(:exist?).returns(true)
|
2019-09-30 22:31:55 +00:00
|
|
|
_(provider.missing_requirements("tcp")).must_equal([])
|
2018-03-07 13:39:27 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it "returns a missing requirement when timeout is missing" do
|
|
|
|
timeout_command.stubs(:exist?).returns(false)
|
2019-09-30 22:31:55 +00:00
|
|
|
_(provider.missing_requirements("tcp")).must_equal(["timeout (part of coreutils) or netcat must be installed"])
|
2018-03-07 13:39:27 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
describe "bash without net redirects" do
|
2018-03-07 13:39:27 +00:00
|
|
|
before do
|
|
|
|
strings_command.stubs(:exit_status).returns(1)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "returns an empty array if nc is installed but ncat is not installed" do
|
|
|
|
nc_command.stubs(:exist?).returns(true)
|
|
|
|
ncat_command.stubs(:exist?).returns(false)
|
2019-09-30 22:31:55 +00:00
|
|
|
_(provider.missing_requirements("tcp")).must_equal([])
|
2018-03-07 13:39:27 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it "returns an empty array if nc is not installed but ncat is installed" do
|
|
|
|
nc_command.stubs(:exist?).returns(false)
|
|
|
|
ncat_command.stubs(:exist?).returns(true)
|
2019-09-30 22:31:55 +00:00
|
|
|
_(provider.missing_requirements("tcp")).must_equal([])
|
2018-03-07 13:39:27 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it "returns an empty array if both nc and ncat are installed" do
|
|
|
|
nc_command.stubs(:exist?).returns(true)
|
|
|
|
ncat_command.stubs(:exist?).returns(true)
|
2019-09-30 22:31:55 +00:00
|
|
|
_(provider.missing_requirements("tcp")).must_equal([])
|
2018-03-07 13:39:27 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it "returns a missing requirement when neither nc nor ncat are installed" do
|
|
|
|
nc_command.stubs(:exist?).returns(false)
|
|
|
|
ncat_command.stubs(:exist?).returns(false)
|
2019-09-30 22:31:55 +00:00
|
|
|
_(provider.missing_requirements("tcp")).must_equal(["netcat must be installed"])
|
2018-03-07 13:39:27 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
describe "#netcat_check_command" do
|
2018-03-07 13:39:27 +00:00
|
|
|
before do
|
|
|
|
strings_command.stubs(:exit_status).returns(1)
|
|
|
|
end
|
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
it "returns an nc command when nc exists tcp" do
|
2017-07-06 17:19:16 +00:00
|
|
|
nc_command.expects(:exist?).returns(true)
|
2018-03-07 13:39:27 +00:00
|
|
|
ncat_command.expects(:exist?).returns(false)
|
2019-09-30 22:31:55 +00:00
|
|
|
_(provider.netcat_check_command("foo", 1234, "tcp")).must_equal "echo | nc -v -w 1 foo 1234"
|
2018-03-07 13:39:27 +00:00
|
|
|
end
|
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
it "returns an nc command when nc exists udp" do
|
2018-03-07 13:39:27 +00:00
|
|
|
nc_command.expects(:exist?).returns(true)
|
|
|
|
ncat_command.expects(:exist?).returns(false)
|
2019-09-30 22:31:55 +00:00
|
|
|
_(provider.netcat_check_command("foo", 1234, "udp")).must_equal "echo | nc -v -w 1 -u foo 1234"
|
2018-03-07 13:39:27 +00:00
|
|
|
end
|
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
it "returns an ncat command when nc does not exist but ncat exists tcp" do
|
2018-03-07 13:39:27 +00:00
|
|
|
nc_command.expects(:exist?).returns(false)
|
|
|
|
ncat_command.expects(:exist?).returns(true)
|
2019-09-30 22:31:55 +00:00
|
|
|
_(provider.netcat_check_command("foo", 1234, "tcp")).must_equal "echo | ncat -v -w 1 foo 1234"
|
2017-07-06 17:19:16 +00:00
|
|
|
end
|
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
it "returns an ncat command when nc does not exist but ncat exists udp" do
|
2017-07-06 17:19:16 +00:00
|
|
|
nc_command.expects(:exist?).returns(false)
|
|
|
|
ncat_command.expects(:exist?).returns(true)
|
2019-09-30 22:31:55 +00:00
|
|
|
_(provider.netcat_check_command("foo", 1234, "udp")).must_equal "echo | ncat -v -w 1 -u foo 1234"
|
2017-07-06 17:19:16 +00:00
|
|
|
end
|
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
it "returns nil if neither nc or ncat exist" do
|
2017-07-06 17:19:16 +00:00
|
|
|
nc_command.expects(:exist?).returns(false)
|
|
|
|
ncat_command.expects(:exist?).returns(false)
|
2019-09-30 22:31:55 +00:00
|
|
|
_(provider.netcat_check_command("foo", 1234, "tcp")).must_be_nil
|
2017-07-06 17:19:16 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|