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

ValidateAddress should return true if IPv6 is valid #3027

Merged
merged 1 commit into from
Feb 19, 2019
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion x/x.go
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,7 @@ func ValidateAddress(addr string) bool {
if p, err := strconv.Atoi(port); err != nil || p <= 0 || p >= 65536 {
return false
}
if err := net.ParseIP(host); err == nil {
if ip := net.ParseIP(host); ip != nil {
martinmr marked this conversation as resolved.
Show resolved Hide resolved
return true
}
// try to parse as hostname as per hostname RFC
Expand Down
42 changes: 42 additions & 0 deletions x/x_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,3 +52,45 @@ func TestDivideAndRule(t *testing.T) {

test(1755, 4, 439)
}

func TestValidateAddress(t *testing.T) {
t.Run("IPv4", func(t *testing.T) {
testData := []struct {
name string
address string
isValid bool
}{
{"Valid without port", "190.0.0.1", false},
{"Valid with port", "192.5.32.1:333", true},
{"Invalid without port", "12.0.0", false},
// the following test returns true because 12.0.0 is considered as valid
// hostname
{"Invalid with port", "12.0.0:3333", true},
martinmr marked this conversation as resolved.
Show resolved Hide resolved
}
for _, subtest := range testData {
st := subtest
t.Run(st.name, func(t *testing.T) {
require.Equal(t, st.isValid, ValidateAddress(st.address))
})
}

})
t.Run("IPv6", func(t *testing.T) {
testData := []struct {
name string
address string
isValid bool
}{
{"Valid without port", "[2001:db8::1]", false},
{"Valid with port", "[2001:db8::1]:8888", true},
{"Invalid without port", "[2001:db8]", false},
{"Invalid with port", "[2001:db8]:2222", false},
}
for _, subtest := range testData {
st := subtest
t.Run(st.name, func(t *testing.T) {
require.Equal(t, st.isValid, ValidateAddress(st.address))
})
}
})
}