1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00

* lib/ipaddr.rb: Introduce several new error classes where only

ArgumentError and StandardError were used.  IPAddr::Error is
  their common ancestor class that inherits from ArgumentError for
  backward compatibility.  Submitted by Jon Daniel.  Fixes #173 on
  GitHub.

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@36868 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
knu 2012-08-31 07:30:22 +00:00
parent 926552f583
commit e03d266c1d
2 changed files with 61 additions and 42 deletions

View file

@ -1,3 +1,11 @@
Fri Aug 31 16:23:20 2012 Akinori MUSHA <knu@iDaemons.org>
* lib/ipaddr.rb: Introduce several new error classes where only
ArgumentError and StandardError were used. IPAddr::Error is
their common ancestor class that inherits from ArgumentError for
backward compatibility. Submitted by Jon Daniel. Fixes #173 on
GitHub.
Fri Aug 31 14:51:27 2012 NAKAMURA Usaku <usa@ruby-lang.org> Fri Aug 31 14:51:27 2012 NAKAMURA Usaku <usa@ruby-lang.org>
* test/bigdecimal/test_bigdecimal.rb (TestBigDecimal#test_to_f): added * test/bigdecimal/test_bigdecimal.rb (TestBigDecimal#test_to_f): added

View file

@ -82,6 +82,21 @@ class IPAddr
\z \z
}xi }xi
# Generic IPAddr related error. Exceptions raised in this class should
# inherit from Error.
class Error < ArgumentError; end
# Raised when the provided IP address is an invalid address.
class InvalidAddressError < Error; end
# Raised when the address family is invalid such as an address with an
# unsupported family, an address with an inconsistent family, or an address
# who's family cannot be determined.
class AddressFamilyError < Error; end
# Raised when the address is an invalid length.
class InvalidPrefixError < InvalidAddressError; end
# Returns the address family of this IP address. # Returns the address family of this IP address.
attr_reader :family attr_reader :family
@ -100,7 +115,7 @@ class IPAddr
when 16 when 16
s = IN6FORMAT % addr.unpack('n8') s = IN6FORMAT % addr.unpack('n8')
else else
raise ArgumentError, "unsupported address family" raise AddressFamilyError, "unsupported address family"
end end
return s return s
end end
@ -226,7 +241,7 @@ class IPAddr
(@addr >> (112 - 16 * i)) & 0xffff (@addr >> (112 - 16 * i)) & 0xffff
}.pack('n8') }.pack('n8')
else else
raise "unsupported address family" raise AddressFamilyError, "unsupported address family"
end end
end end
@ -258,7 +273,7 @@ class IPAddr
# into an IPv4-mapped IPv6 address. # into an IPv4-mapped IPv6 address.
def ipv4_mapped def ipv4_mapped
if !ipv4? if !ipv4?
raise ArgumentError, "not an IPv4 address" raise InvalidAddressError, "not an IPv4 address"
end end
return self.clone.set(@addr | 0xffff00000000, Socket::AF_INET6) return self.clone.set(@addr | 0xffff00000000, Socket::AF_INET6)
end end
@ -267,7 +282,7 @@ class IPAddr
# into an IPv4-compatible IPv6 address. # into an IPv4-compatible IPv6 address.
def ipv4_compat def ipv4_compat
if !ipv4? if !ipv4?
raise ArgumentError, "not an IPv4 address" raise InvalidAddressError, "not an IPv4 address"
end end
return self.clone.set(@addr, Socket::AF_INET6) return self.clone.set(@addr, Socket::AF_INET6)
end end
@ -291,14 +306,14 @@ class IPAddr
when Socket::AF_INET6 when Socket::AF_INET6
return ip6_arpa return ip6_arpa
else else
raise "unsupported address family" raise AddressFamilyError, "unsupported address family"
end end
end end
# Returns a string for DNS reverse lookup compatible with RFC3172. # Returns a string for DNS reverse lookup compatible with RFC3172.
def ip6_arpa def ip6_arpa
if !ipv6? if !ipv6?
raise ArgumentError, "not an IPv6 address" raise InvalidAddressError, "not an IPv6 address"
end end
return _reverse + ".ip6.arpa" return _reverse + ".ip6.arpa"
end end
@ -306,7 +321,7 @@ class IPAddr
# Returns a string for DNS reverse lookup compatible with RFC1886. # Returns a string for DNS reverse lookup compatible with RFC1886.
def ip6_int def ip6_int
if !ipv6? if !ipv6?
raise ArgumentError, "not an IPv6 address" raise InvalidAddressError, "not an IPv6 address"
end end
return _reverse + ".ip6.int" return _reverse + ".ip6.int"
end end
@ -346,7 +361,7 @@ class IPAddr
when Socket::AF_INET6 when Socket::AF_INET6
end_addr = (@addr | (IN6MASK ^ @mask_addr)) end_addr = (@addr | (IN6MASK ^ @mask_addr))
else else
raise "unsupported address family" raise AddressFamilyError, "unsupported address family"
end end
return clone.set(begin_addr, @family)..clone.set(end_addr, @family) return clone.set(begin_addr, @family)..clone.set(end_addr, @family)
@ -361,7 +376,7 @@ class IPAddr
when Socket::AF_INET6 when Socket::AF_INET6
af = "IPv6" af = "IPv6"
else else
raise "unsupported address family" raise AddressFamilyError, "unsupported address family"
end end
return sprintf("#<%s: %s:%s/%s>", self.class.name, return sprintf("#<%s: %s:%s/%s>", self.class.name,
af, _to_string(@addr), _to_string(@mask_addr)) af, _to_string(@addr), _to_string(@mask_addr))
@ -376,14 +391,14 @@ class IPAddr
case family[0] ? family[0] : @family case family[0] ? family[0] : @family
when Socket::AF_INET when Socket::AF_INET
if addr < 0 || addr > IN4MASK if addr < 0 || addr > IN4MASK
raise ArgumentError, "invalid address" raise InvalidAddressError, "invalid address"
end end
when Socket::AF_INET6 when Socket::AF_INET6
if addr < 0 || addr > IN6MASK if addr < 0 || addr > IN6MASK
raise ArgumentError, "invalid address" raise InvalidAddressError, "invalid address"
end end
else else
raise ArgumentError, "unsupported address family" raise AddressFamilyError, "unsupported address family"
end end
@addr = addr @addr = addr
if family[0] if family[0]
@ -400,7 +415,7 @@ class IPAddr
else else
m = IPAddr.new(mask) m = IPAddr.new(mask)
if m.family != @family if m.family != @family
raise ArgumentError, "address family is not same" raise InvalidPrefixError, "address family is not same"
end end
@mask_addr = m.to_i @mask_addr = m.to_i
@addr &= @mask_addr @addr &= @mask_addr
@ -412,18 +427,18 @@ class IPAddr
case @family case @family
when Socket::AF_INET when Socket::AF_INET
if prefixlen < 0 || prefixlen > 32 if prefixlen < 0 || prefixlen > 32
raise ArgumentError, "invalid length" raise InvalidPrefixError, "invalid length"
end end
masklen = 32 - prefixlen masklen = 32 - prefixlen
@mask_addr = ((IN4MASK >> masklen) << masklen) @mask_addr = ((IN4MASK >> masklen) << masklen)
when Socket::AF_INET6 when Socket::AF_INET6
if prefixlen < 0 || prefixlen > 128 if prefixlen < 0 || prefixlen > 128
raise ArgumentError, "invalid length" raise InvalidPrefixError, "invalid length"
end end
masklen = 128 - prefixlen masklen = 128 - prefixlen
@mask_addr = ((IN6MASK >> masklen) << masklen) @mask_addr = ((IN6MASK >> masklen) << masklen)
else else
raise "unsupported address family" raise AddressFamilyError, "unsupported address family"
end end
@addr = ((@addr >> masklen) << masklen) @addr = ((@addr >> masklen) << masklen)
return self return self
@ -457,9 +472,9 @@ class IPAddr
@mask_addr = (family == Socket::AF_INET) ? IN4MASK : IN6MASK @mask_addr = (family == Socket::AF_INET) ? IN4MASK : IN6MASK
return return
when Socket::AF_UNSPEC when Socket::AF_UNSPEC
raise ArgumentError, "address family must be specified" raise AddressFamilyError, "address family must be specified"
else else
raise ArgumentError, "unsupported address family: #{family}" raise AddressFamilyError, "unsupported address family: #{family}"
end end
end end
prefix, prefixlen = addr.split('/') prefix, prefixlen = addr.split('/')
@ -482,7 +497,7 @@ class IPAddr
@family = Socket::AF_INET6 @family = Socket::AF_INET6
end end
if family != Socket::AF_UNSPEC && @family != family if family != Socket::AF_UNSPEC && @family != family
raise ArgumentError, "address family mismatch" raise AddressFamilyError, "address family mismatch"
end end
if prefixlen if prefixlen
mask!(prefixlen) mask!(prefixlen)
@ -511,8 +526,8 @@ class IPAddr
octets = m.captures octets = m.captures
end end
octets.inject(0) { |i, s| octets.inject(0) { |i, s|
(n = s.to_i) < 256 or raise ArgumentError, "invalid address" (n = s.to_i) < 256 or raise InvalidAddressError, "invalid address"
s.match(/\A0./) and raise ArgumentError, "zero-filled number is ambiguous" s.match(/\A0./) and raise InvalidAddressError, "zero-filled number in IPv4 address is ambiguous"
i << 8 | n i << 8 | n
} }
end end
@ -529,18 +544,18 @@ class IPAddr
right = '' right = ''
when RE_IPV6ADDRLIKE_COMPRESSED when RE_IPV6ADDRLIKE_COMPRESSED
if $4 if $4
left.count(':') <= 6 or raise ArgumentError, "invalid address" left.count(':') <= 6 or raise InvalidAddressError, "invalid address"
addr = in_addr($~[4,4]) addr = in_addr($~[4,4])
left = $1 left = $1
right = $3 + '0:0' right = $3 + '0:0'
else else
left.count(':') <= 7 or raise ArgumentError, "invalid address" left.count(':') <= 7 or raise InvalidAddressError, "invalid address"
left = $1 left = $1
right = $2 right = $2
addr = 0 addr = 0
end end
else else
raise ArgumentError, "invalid address" raise InvalidAddressError, "invalid address"
end end
l = left.split(':') l = left.split(':')
r = right.split(':') r = right.split(':')
@ -560,7 +575,7 @@ class IPAddr
when Socket::AF_INET6 when Socket::AF_INET6
return addr & IN6MASK return addr & IN6MASK
else else
raise "unsupported address family" raise AddressFamilyError, "unsupported address family"
end end
end end
@ -573,7 +588,7 @@ class IPAddr
when Socket::AF_INET6 when Socket::AF_INET6
return ("%.32x" % @addr).reverse!.gsub!(/.(?!$)/, '\&.') return ("%.32x" % @addr).reverse!.gsub!(/.(?!$)/, '\&.')
else else
raise "unsupported address family" raise AddressFamilyError, "unsupported address family"
end end
end end
@ -586,7 +601,7 @@ class IPAddr
when Socket::AF_INET6 when Socket::AF_INET6
return (("%.32x" % addr).gsub!(/.{4}(?!$)/, '\&:')) return (("%.32x" % addr).gsub!(/.{4}(?!$)/, '\&:'))
else else
raise "unsupported address family" raise AddressFamilyError, "unsupported address family"
end end
end end
@ -713,19 +728,15 @@ class TC_IPAddr < Test::Unit::TestCase
assert_equal("2001:200:300::", IPAddr.new("[2001:200:300::]/48").to_s) assert_equal("2001:200:300::", IPAddr.new("[2001:200:300::]/48").to_s)
[ assert_raises(IPAddr::InvalidAddressError) { IPAddr.new("192.168.0.256") }
["192.168.0.256"], assert_raises(IPAddr::InvalidAddressError) { IPAddr.new("192.168.0.011") }
["192.168.0.011"], assert_raises(IPAddr::InvalidAddressError) { IPAddr.new("fe80::1%fxp0") }
["fe80::1%fxp0"], assert_raises(IPAddr::InvalidAddressError) { IPAddr.new("[192.168.1.2]/120") }
["::1/255.255.255.0"], assert_raises(IPAddr::InvalidPrefixError) { IPAddr.new("::1/255.255.255.0") }
[IPAddr.new("::1").to_i], assert_raises(IPAddr::InvalidPrefixError) { IPAddr.new("::1/129") }
["::ffff:192.168.1.2/120", Socket::AF_INET], assert_raises(IPAddr::InvalidPrefixError) { IPAddr.new("192.168.0.1/33") }
["[192.168.1.2]/120"], assert_raises(IPAddr::AddressFamilyError) { IPAddr.new(1) }
].each { |args| assert_raises(IPAddr::AddressFamilyError) { IPAddr.new("::ffff:192.168.1.2/120", Socket::AF_INET) }
assert_raises(ArgumentError) {
IPAddr.new(*args)
}
}
end end
def test_s_new_ntoh def test_s_new_ntoh
@ -786,14 +797,14 @@ class TC_IPAddr < Test::Unit::TestCase
def test_ip6_arpa def test_ip6_arpa
assert_equal("f.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.2.0.0.0.5.0.5.0.e.f.f.3.ip6.arpa", IPAddr.new("3ffe:505:2::f").ip6_arpa) assert_equal("f.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.2.0.0.0.5.0.5.0.e.f.f.3.ip6.arpa", IPAddr.new("3ffe:505:2::f").ip6_arpa)
assert_raises(ArgumentError) { assert_raises(IPAddr::InvalidAddressError) {
IPAddr.new("192.168.2.1").ip6_arpa IPAddr.new("192.168.2.1").ip6_arpa
} }
end end
def test_ip6_int def test_ip6_int
assert_equal("f.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.2.0.0.0.5.0.5.0.e.f.f.3.ip6.int", IPAddr.new("3ffe:505:2::f").ip6_int) assert_equal("f.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.2.0.0.0.5.0.5.0.e.f.f.3.ip6.int", IPAddr.new("3ffe:505:2::f").ip6_int)
assert_raises(ArgumentError) { assert_raises(IPAddr::InvalidAddressError) {
IPAddr.new("192.168.2.1").ip6_int IPAddr.new("192.168.2.1").ip6_int
} }
end end