Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ntop: Measure address size in bytes #61

Merged
merged 1 commit into from
Dec 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 6 additions & 1 deletion lib/ipaddr.rb
Original file line number Diff line number Diff line change
Expand Up @@ -110,8 +110,13 @@ def self.new_ntoh(addr)

# Convert a network byte ordered string form of an IP address into
# human readable form.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you add something like this here? "It expects a string encoded in Encoding::ASCII_8BIT."

# It expects the string to be encoded in Encoding::ASCII_8BIT (BINARY).
def self.ntop(addr)
case addr.size
if addr.is_a?(String) && addr.encoding != Encoding::BINARY
raise InvalidAddressError, "invalid encoding (given #{addr.encoding}, expected BINARY)"
end

case addr.bytesize
when 4
addr.unpack('C4').join('.')
when 16
Expand Down
29 changes: 24 additions & 5 deletions test/test_ipaddr.rb
Original file line number Diff line number Diff line change
Expand Up @@ -133,18 +133,37 @@ def test_s_new_ntoh

def test_ntop
# IPv4
assert_equal("192.168.1.1", IPAddr.ntop("\xC0\xA8\x01\x01"))
assert_equal("192.168.1.1", IPAddr.ntop("\xC0\xA8\x01\x01".b))
assert_equal("10.231.140.171", IPAddr.ntop("\x0A\xE7\x8C\xAB".b))
# 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"))
IPAddr.ntop("\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x01".b))
assert_equal("fe80:0000:0000:0000:f09f:9985:f09f:9986",
IPAddr.ntop("\xFE\x80\x00\x00\x00\x00\x00\x00\xF0\x9F\x99\x85\xF0\x9F\x99\x86".b))

# Invalid parameters
## wrong length
assert_raise(IPAddr::AddressFamilyError) {
IPAddr.ntop("192.168.1.1")
IPAddr.ntop("192.168.1.1".b)
knu marked this conversation as resolved.
Show resolved Hide resolved
}

assert_raise(IPAddr::AddressFamilyError) {
IPAddr.ntop("\xC0\xA8\x01\xFF1")
IPAddr.ntop("\xC0\xA8\x01\xFF1".b)
}
## UTF-8
assert_raise(IPAddr::InvalidAddressError) {
IPAddr.ntop("192.168.1.1")
}
assert_raise(IPAddr::InvalidAddressError) {
IPAddr.ntop("\x0A\x0A\x0A\x0A")
}
assert_raise(IPAddr::InvalidAddressError) {
IPAddr.ntop("\x0A\xE7\x8C\xAB")
}
assert_raise(IPAddr::InvalidAddressError) {
IPAddr.ntop("\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x01")
}
assert_raise(IPAddr::InvalidAddressError) {
IPAddr.ntop("\xFE\x80\x00\x00\x00\x00\x00\x00\xF0\x9F\x99\x85\xF0\x9F\x99\x86")
}
end

Expand Down