Skip to content

Commit

Permalink
ValidateAddress should return true if IPv6 is valid (dgraph-io#3027)
Browse files Browse the repository at this point in the history
- Add tests for ValidateAddress function

Fixes dgraph-io#3023

Signed-off-by: Ibrahim Jarif <jarifibrahim@gmail.com>
  • Loading branch information
jarifibrahim authored and dna2github committed Jul 19, 2019
1 parent c11dfb8 commit 50a8e92
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 1 deletion.
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 {
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},
}
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))
})
}
})
}

0 comments on commit 50a8e92

Please sign in to comment.