Project

General

Profile

Bug #14612 ยป resolv-ipv6-to_s.patch

hugopeixoto (Hugo Peixoto), 03/21/2018 02:27 PM

View differences:

lib/resolv.rb
3 3
require 'socket'
4 4
require 'timeout'
5 5
require 'io/wait'
6
require 'ipaddr'
6 7

  
7 8
begin
8 9
  require 'securerandom'
......
2528 2529
    attr_reader :address
2529 2530

  
2530 2531
    def to_s # :nodoc:
2531
      address = sprintf("%X:%X:%X:%X:%X:%X:%X:%X", *@address.unpack("nnnnnnnn"))
2532
      unless address.sub!(/(^|:)0(:0)+(:|$)/, '::')
2533
        address.sub!(/(^|:)0(:|$)/, '::')
2534
      end
2535
      return address
2532
      IPAddr.new(sprintf("%x:%x:%x:%x:%x:%x:%x:%x", *@address.unpack("nnnnnnnn"))).to_s
2536 2533
    end
2537 2534

  
2538 2535
    def inspect # :nodoc:
test/resolv/test_dns.rb
216 216
    assert_instance_of Resolv::IPv6, Resolv::IPv6.create('::1:127.0.0.1'), ref
217 217
  end
218 218

  
219
  def test_ipv6_to_s
220
    test_cases = [
221
      ["2001::abcd:abcd:abcd", "2001::ABcd:abcd:ABCD"],
222
      ["2001:db8::1", "2001:db8::0:1"],
223
      ["::", "0:0:0:0:0:0:0:0"],
224
      ["2001::", "2001::0"],
225
      ["2001:db8:0:1:1:1:1:1", "2001:db8:0:1:1:1:1:1"],
226
      ["1:0:0:1::1", "1:0:0:1:0:0:0:1"],
227
      ["1::1:0:0:1", "1:0:0:0:1:0:0:1"],
228
    ]
229

  
230
    test_cases.each do |expected, ipv6|
231
      assert_equal expected, Resolv::IPv6.create(ipv6).to_s
232
    end
233
  end
234

  
219 235
  def test_ipv6_should_be_16
220 236
    ref = '[rubygems:1626]'
221 237