File tree Expand file tree Collapse file tree 2 files changed +19
-3
lines changed
Expand file tree Collapse file tree 2 files changed +19
-3
lines changed Original file line number Diff line number Diff line change @@ -112,13 +112,12 @@ def self.new_ntoh(addr)
112112 def self . ntop ( addr )
113113 case addr . size
114114 when 4
115- s = addr . unpack ( 'C4' ) . join ( '.' )
115+ addr . unpack ( 'C4' ) . join ( '.' )
116116 when 16
117- s = IN6FORMAT % addr . unpack ( 'n8' )
117+ IN6FORMAT % addr . unpack ( 'n8' )
118118 else
119119 raise AddressFamilyError , "unsupported address family"
120120 end
121- return s
122121 end
123122
124123 # Returns a new ipaddr built by bitwise AND.
Original file line number Diff line number Diff line change @@ -131,6 +131,23 @@ def test_s_new_ntoh
131131 assert_equal ( "192.168.2.1" , IPAddr . new_ntoh ( a . hton ) . to_s )
132132 end
133133
134+ def test_ntop
135+ # IPv4
136+ assert_equal ( "192.168.1.1" , IPAddr . ntop ( "\xC0 \xA8 \x01 \x01 " ) )
137+ # IPv6
138+ assert_equal ( "0000:0000:0000:0000:0000:0000:0000:0001" ,
139+ IPAddr . ntop ( "\x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x01 " ) )
140+
141+ # Invalid parameters
142+ assert_raise ( IPAddr ::AddressFamilyError ) {
143+ IPAddr . ntop ( "192.168.1.1" )
144+ }
145+
146+ assert_raise ( IPAddr ::AddressFamilyError ) {
147+ IPAddr . ntop ( "\xC0 \xA8 \x01 \xFF 1" )
148+ }
149+ end
150+
134151 def test_ipv4_compat
135152 a = IPAddr . new ( "::192.168.1.2" )
136153 assert_equal ( "::192.168.1.2" , a . to_s )
You can’t perform that action at this time.
0 commit comments