2015-12-16 08:07:31 +03:00
|
|
|
# frozen_string_literal: false
|
2010-04-18 13:09:46 +04:00
|
|
|
require 'test/unit'
|
|
|
|
require 'resolv'
|
|
|
|
require 'socket'
|
2013-12-21 13:45:50 +04:00
|
|
|
require 'tempfile'
|
2010-04-18 13: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-21 07:37:26 +04:00
|
|
|
|
2020-08-24 22:39:08 +03: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 10:16:30 +03: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)
|
2021-01-09 23:14:23 +03:00
|
|
|
|
|
|
|
bug17524 = "[ruby-core:101992]"
|
|
|
|
assert_match(Resolv::IPv6::Regex, "FE80::20D:3AFF:FE7D:9760%ruby_3.0.0-1", bug17524)
|
|
|
|
assert_match(Resolv::IPv6::Regex, "fe80::1%ruby_3.0.0-1", bug17524)
|
2020-08-24 22:39:08 +03: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-21 07:37:26 +04:00
|
|
|
def test_invalid_byte_comment
|
|
|
|
bug9273 = '[ruby-core:59239] [Bug #9273]'
|
2014-05-25 18:00:16 +04:00
|
|
|
Tempfile.create('resolv_test_addr_') do |tmpfile|
|
2013-12-21 07:37:26 +04: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-20 07:25:01 +03:00
|
|
|
|
|
|
|
def test_hosts_by_command
|
|
|
|
Dir.mktmpdir do |dir|
|
|
|
|
Dir.chdir(dir) do
|
|
|
|
hosts = Resolv::Hosts.new("|echo error")
|
2017-12-20 19:04:41 +03:00
|
|
|
assert_raise(Errno::ENOENT, Errno::EINVAL) do
|
2017-12-20 07:25:01 +03:00
|
|
|
hosts.each_name("") {}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2010-04-18 13:09:46 +04:00
|
|
|
end
|