2015-12-16 00:07:31 -05:00
|
|
|
# frozen_string_literal: false
|
2010-04-18 05:09:46 -04:00
|
|
|
require 'test/unit'
|
|
|
|
require 'resolv'
|
|
|
|
require 'socket'
|
2013-12-21 04:45:50 -05:00
|
|
|
require 'tempfile'
|
2010-04-18 05:09:46 -04:00
|
|
|
|
|
|
|
class TestResolvAddr < Test::Unit::TestCase
|
|
|
|
def test_invalid_ipv4_address
|
|
|
|
assert_not_match(Resolv::IPv4::Regex, "1.2.3.256", "[ruby-core:29501]")
|
|
|
|
1000.times {|i|
|
|
|
|
if i < 256
|
|
|
|
assert_match(Resolv::IPv4::Regex, "#{i}.#{i}.#{i}.#{i}")
|
|
|
|
else
|
|
|
|
assert_not_match(Resolv::IPv4::Regex, "#{i}.#{i}.#{i}.#{i}")
|
|
|
|
end
|
|
|
|
}
|
|
|
|
end
|
2013-12-20 22:37:26 -05:00
|
|
|
|
2020-08-24 15:39:08 -04:00
|
|
|
def test_valid_ipv6_link_local_address
|
|
|
|
bug17112 = "[ruby-core:99539]"
|
|
|
|
assert_not_match(Resolv::IPv6::Regex, "fe80::1%", bug17112)
|
|
|
|
assert_not_match(Resolv::IPv6::Regex, "fe80:2:3:4:5:6:7:8%", bug17112)
|
|
|
|
assert_not_match(Resolv::IPv6::Regex, "fe90::1%em1", bug17112)
|
|
|
|
assert_not_match(Resolv::IPv6::Regex, "1:2:3:4:5:6:7:8%em1", bug17112)
|
|
|
|
assert_match(Resolv::IPv6::Regex, "fe80:2:3:4:5:6:7:8%em1", bug17112)
|
|
|
|
assert_match(Resolv::IPv6::Regex, "fe80::20d:3aff:fe7d:9760%eth0", bug17112)
|
|
|
|
assert_match(Resolv::IPv6::Regex, "fe80::1%em1", bug17112)
|
2020-11-09 02:16:30 -05:00
|
|
|
assert_match(Resolv::IPv6::Regex, "FE80:2:3:4:5:6:7:8%EM1", bug17112)
|
|
|
|
assert_match(Resolv::IPv6::Regex, "FE80::20D:3AFF:FE7D:9760%ETH0", bug17112)
|
|
|
|
assert_match(Resolv::IPv6::Regex, "FE80::1%EM1", bug17112)
|
2020-08-24 15:39:08 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_valid_socket_ip_address_list
|
|
|
|
Socket.ip_address_list.each do |addr|
|
|
|
|
ip = addr.ip_address
|
|
|
|
assert_match(Resolv::AddressRegex, ip)
|
|
|
|
assert_equal(ip, Resolv.getaddress(ip))
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-12-20 22:37:26 -05:00
|
|
|
def test_invalid_byte_comment
|
|
|
|
bug9273 = '[ruby-core:59239] [Bug #9273]'
|
2014-05-25 10:00:16 -04:00
|
|
|
Tempfile.create('resolv_test_addr_') do |tmpfile|
|
2013-12-20 22:37:26 -05:00
|
|
|
tmpfile.print("\xff\x00\x40")
|
|
|
|
tmpfile.close
|
|
|
|
hosts = Resolv::Hosts.new(tmpfile.path)
|
|
|
|
assert_nothing_raised(ArgumentError, bug9273) do
|
|
|
|
hosts.each_address("") {break}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2017-12-19 23:25:01 -05:00
|
|
|
|
|
|
|
def test_hosts_by_command
|
|
|
|
Dir.mktmpdir do |dir|
|
|
|
|
Dir.chdir(dir) do
|
|
|
|
hosts = Resolv::Hosts.new("|echo error")
|
2017-12-20 11:04:41 -05:00
|
|
|
assert_raise(Errno::ENOENT, Errno::EINVAL) do
|
2017-12-19 23:25:01 -05:00
|
|
|
hosts.each_name("") {}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2010-04-18 05:09:46 -04:00
|
|
|
end
|