Skip to content

Commit

Permalink
🚨 added testcases and minor algorithm improvment (#2308)
Browse files Browse the repository at this point in the history
* Deleted redundant check for an ipv4 address octet block that is bigger than 255 in utils/ip.go. Also added a testcase for octetblocks that are bigger than 255.

* Added extra testcases
  • Loading branch information
rhabichl authored Jan 25, 2023
1 parent 66cc869 commit e2cb81d
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 2 deletions.
6 changes: 4 additions & 2 deletions utils/ips.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package utils

import "net"
import (
"net"
)

// IsIPv4 works the same way as net.ParseIP,
// but without check for IPv6 case and without returning net.IP slice, whereby IsIPv4 makes no allocations.
Expand All @@ -26,7 +28,7 @@ func IsIPv4(s string) bool {
}
}

if ci == 0 || n > 0xFF || (ci > 1 && s[0] == '0') {
if ci == 0 || (ci > 1 && s[0] == '0') {
return false
}

Expand Down
5 changes: 5 additions & 0 deletions utils/ips_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@ func Test_IsIPv4(t *testing.T) {
AssertEqual(t, false, IsIPv4(""))
AssertEqual(t, false, IsIPv4("2345:0425:2CA1::0567:5673:23b5"))
AssertEqual(t, false, IsIPv4("invalid"))
AssertEqual(t, false, IsIPv4("189.12.34.260"))
AssertEqual(t, false, IsIPv4("189.12.260.260"))
AssertEqual(t, false, IsIPv4("189.260.260.260"))
AssertEqual(t, false, IsIPv4("999.999.999.999"))
AssertEqual(t, false, IsIPv4("9999.9999.9999.9999"))
}

// go test -v -run=^$ -bench=UnsafeString -benchmem -count=2
Expand Down

0 comments on commit e2cb81d

Please sign in to comment.