summaryrefslogtreecommitdiff
path: root/test/resolv/test_addr.rb
blob: 62092676baee7c0f2f7ec036abf0e0a27aef0e41 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# frozen_string_literal: false
require 'test/unit'
require 'resolv'
require 'socket'
require 'tempfile'

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

  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)
    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)
  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

  def test_invalid_byte_comment
    bug9273 = '[ruby-core:59239] [Bug #9273]'
    Tempfile.create('resolv_test_addr_') do |tmpfile|
      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

  def test_hosts_by_command
    Dir.mktmpdir do |dir|
      Dir.chdir(dir) do
        hosts = Resolv::Hosts.new("|echo error")
        assert_raise(Errno::ENOENT, Errno::EINVAL) do
          hosts.each_name("") {}
        end
      end
    end
  end
end