[ruby/ipaddr] Removing superfluos assingments & return
Also adding test for ntop https://github.com/ruby/ipaddr/commit/0ba16cca10
This commit is contained in:
parent
323e7a6744
commit
b23fba91ae
@ -112,13 +112,12 @@ class IPAddr
|
|||||||
def self.ntop(addr)
|
def self.ntop(addr)
|
||||||
case addr.size
|
case addr.size
|
||||||
when 4
|
when 4
|
||||||
s = addr.unpack('C4').join('.')
|
addr.unpack('C4').join('.')
|
||||||
when 16
|
when 16
|
||||||
s = IN6FORMAT % addr.unpack('n8')
|
IN6FORMAT % addr.unpack('n8')
|
||||||
else
|
else
|
||||||
raise AddressFamilyError, "unsupported address family"
|
raise AddressFamilyError, "unsupported address family"
|
||||||
end
|
end
|
||||||
return s
|
|
||||||
end
|
end
|
||||||
|
|
||||||
# Returns a new ipaddr built by bitwise AND.
|
# Returns a new ipaddr built by bitwise AND.
|
||||||
|
@ -116,6 +116,23 @@ class TC_IPAddr < Test::Unit::TestCase
|
|||||||
assert_equal("192.168.2.1", IPAddr.new_ntoh(a.hton).to_s)
|
assert_equal("192.168.2.1", IPAddr.new_ntoh(a.hton).to_s)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def test_ntop
|
||||||
|
# IPv4
|
||||||
|
assert_equal("192.168.1.1", IPAddr.ntop("\xC0\xA8\x01\x01"))
|
||||||
|
# IPv6
|
||||||
|
assert_equal("0000:0000:0000:0000:0000:0000:0000:0001",
|
||||||
|
IPAddr.ntop("\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x01"))
|
||||||
|
|
||||||
|
# Invalid parameters
|
||||||
|
assert_raise(IPAddr::AddressFamilyError) {
|
||||||
|
IPAddr.ntop("192.168.1.1")
|
||||||
|
}
|
||||||
|
|
||||||
|
assert_raise(IPAddr::AddressFamilyError) {
|
||||||
|
IPAddr.ntop("\xC0\xA8\x01\xFF1")
|
||||||
|
}
|
||||||
|
end
|
||||||
|
|
||||||
def test_ipv4_compat
|
def test_ipv4_compat
|
||||||
a = IPAddr.new("::192.168.1.2")
|
a = IPAddr.new("::192.168.1.2")
|
||||||
assert_equal("::192.168.1.2", a.to_s)
|
assert_equal("::192.168.1.2", a.to_s)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user