PageRenderTime 36ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 1ms

/test/resolv/test_addr.rb

https://github.com/ruby/ruby
Ruby | 63 lines | 57 code | 5 blank | 1 comment | 1 complexity | 4e7c2c2d5a2a269e6b42367157aeea5c MD5 | raw file
Possible License(s): GPL-2.0, BSD-3-Clause
  1. # frozen_string_literal: false
  2. require 'test/unit'
  3. require 'resolv'
  4. require 'socket'
  5. require 'tempfile'
  6. class TestResolvAddr < Test::Unit::TestCase
  7. def test_invalid_ipv4_address
  8. assert_not_match(Resolv::IPv4::Regex, "1.2.3.256", "[ruby-core:29501]")
  9. 1000.times {|i|
  10. if i < 256
  11. assert_match(Resolv::IPv4::Regex, "#{i}.#{i}.#{i}.#{i}")
  12. else
  13. assert_not_match(Resolv::IPv4::Regex, "#{i}.#{i}.#{i}.#{i}")
  14. end
  15. }
  16. end
  17. def test_valid_ipv6_link_local_address
  18. bug17112 = "[ruby-core:99539]"
  19. assert_not_match(Resolv::IPv6::Regex, "fe80::1%", bug17112)
  20. assert_not_match(Resolv::IPv6::Regex, "fe80:2:3:4:5:6:7:8%", bug17112)
  21. assert_not_match(Resolv::IPv6::Regex, "fe90::1%em1", bug17112)
  22. assert_not_match(Resolv::IPv6::Regex, "1:2:3:4:5:6:7:8%em1", bug17112)
  23. assert_match(Resolv::IPv6::Regex, "fe80:2:3:4:5:6:7:8%em1", bug17112)
  24. assert_match(Resolv::IPv6::Regex, "fe80::20d:3aff:fe7d:9760%eth0", bug17112)
  25. assert_match(Resolv::IPv6::Regex, "fe80::1%em1", bug17112)
  26. assert_match(Resolv::IPv6::Regex, "FE80:2:3:4:5:6:7:8%EM1", bug17112)
  27. assert_match(Resolv::IPv6::Regex, "FE80::20D:3AFF:FE7D:9760%ETH0", bug17112)
  28. assert_match(Resolv::IPv6::Regex, "FE80::1%EM1", bug17112)
  29. end
  30. def test_valid_socket_ip_address_list
  31. Socket.ip_address_list.each do |addr|
  32. ip = addr.ip_address
  33. assert_match(Resolv::AddressRegex, ip)
  34. assert_equal(ip, Resolv.getaddress(ip))
  35. end
  36. end
  37. def test_invalid_byte_comment
  38. bug9273 = '[ruby-core:59239] [Bug #9273]'
  39. Tempfile.create('resolv_test_addr_') do |tmpfile|
  40. tmpfile.print("\xff\x00\x40")
  41. tmpfile.close
  42. hosts = Resolv::Hosts.new(tmpfile.path)
  43. assert_nothing_raised(ArgumentError, bug9273) do
  44. hosts.each_address("") {break}
  45. end
  46. end
  47. end
  48. def test_hosts_by_command
  49. Dir.mktmpdir do |dir|
  50. Dir.chdir(dir) do
  51. hosts = Resolv::Hosts.new("|echo error")
  52. assert_raise(Errno::ENOENT, Errno::EINVAL) do
  53. hosts.each_name("") {}
  54. end
  55. end
  56. end
  57. end
  58. end