Add tests for the IPv6Address class and ensure they pass

This commit is contained in:
Nick Thomas
2011-06-10 23:01:00 +01:00
parent e26105b158
commit afb82d2a5a
2 changed files with 66 additions and 4 deletions

View File

@@ -22,19 +22,25 @@ class Ipv6Address < BinData::Primitive
end
def set(v)
v = IPAddress(v) if v.is_a?(String)
v = IPAddress::IPv6::Mapped.new(v.to_s) if v.is_a?(IPAddress::IPv4)
v = IPAddress(v.to_s) unless v.is_a?(IPAddress::IPv6)
if v.is_a?(Fixnum) || v.is_a?(Bignum)
v = if v < 2**32
IPAddress::IPv6::Mapped.parse_u128(v)
else
IPAddress::IPv6::parse_u128(v)
end
end
if v.respond_to?(:to_u128)
address = v.to_u128
self.address = v.to_u128
else
raise ArgumentError.new("Can't set #{v.class} to an IPv6Address")
end
end
end
# Implementation of the BitCoin wire protocol, written using bindata.
# Reference: https://en.bitcoin.it/wiki/Protocol_specification
#