Backend: fix IPv6 address parsing erroneous (#28585)

* Backend: Fix parsing of client IP address

Signed-off-by: Arve Knudsen <arve.knudsen@gmail.com>

Co-authored-by: Arve Knudsen <arve.knudsen@gmail.com>
This commit is contained in:
taciomcosta
2020-11-25 03:55:22 -03:00
committed by GitHub
parent 3d33de1751
commit 10ff4eecef
15 changed files with 228 additions and 147 deletions

View File

@@ -4,52 +4,8 @@ import (
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestParseIPAddress_Valid(t *testing.T) {
tests := []struct {
input string
expected string
}{
{input: "127.0.0.1", expected: "127.0.0.1"},
{input: "192.168.0.140:456", expected: "192.168.0.140"},
{input: "192.168.0.140", expected: "192.168.0.140"},
{input: "[::1]:456", expected: "::1"},
{input: "[::1]", expected: "::1"},
}
for _, testcase := range tests {
addr, err := ParseIPAddress(testcase.input)
require.NoError(t, err)
assert.Equal(t, testcase.expected, addr)
}
}
func TestParseIPAddress_Invalid(t *testing.T) {
tests := []struct {
input string
err string
}{
{
input: "[::1",
err: "failed to split network address \"[::1\" by host and port: malformed IPv6 address: '[::1'",
},
{
input: "::1]",
err: "failed to split network address \"::1]\" by host and port: net.SplitHostPort failed for '::1]': address ::1]: too many colons in address",
},
{
input: "",
err: "failed to split network address \"\" by host and port: input is empty",
},
}
for _, testcase := range tests {
addr, err := ParseIPAddress(testcase.input)
assert.EqualError(t, err, testcase.err)
assert.Empty(t, addr)
}
}
func TestSplitHostPortDefault_Valid(t *testing.T) {
tests := []struct {
input string
@@ -76,25 +32,3 @@ func TestSplitHostPortDefault_Valid(t *testing.T) {
assert.Equal(t, testcase.port, addr.Port)
}
}
func TestSplitHostPort_Valid(t *testing.T) {
tests := []struct {
input string
host string
port string
}{
{input: "192.168.0.140:456", host: "192.168.0.140", port: "456"},
{input: "192.168.0.140", host: "192.168.0.140", port: ""},
{input: "[::1]:456", host: "::1", port: "456"},
{input: "[::1]", host: "::1", port: ""},
{input: ":456", host: "", port: "456"},
{input: "xyz.rds.amazonaws.com", host: "xyz.rds.amazonaws.com", port: ""},
{input: "xyz.rds.amazonaws.com:123", host: "xyz.rds.amazonaws.com", port: "123"},
}
for _, testcase := range tests {
addr, err := SplitHostPort(testcase.input)
require.NoError(t, err)
assert.Equal(t, testcase.host, addr.Host)
assert.Equal(t, testcase.port, addr.Port)
}
}