🚨 added testcases and minor algorithm improvment (#2308)

* 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
pull/2312/head
Limux 2023-01-25 20:38:29 +01:00 committed by GitHub
parent 66cc869b1f
commit e2cb81ddd3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 9 additions and 2 deletions

View File

@ -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.
@ -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
}

View File

@ -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