mirror of
https://github.com/etcd-io/etcd.git
synced 2024-09-27 06:25:44 +00:00
netutil: consistently format ipv6 addresses
This formats ipv6 addresses to ensure they can be compared safely Signed-off-by: kidsan <8798449+Kidsan@users.noreply.github.com>
This commit is contained in:
parent
82004d0197
commit
41ed809c38
@ -103,7 +103,7 @@ func resolveURL(ctx context.Context, lg *zap.Logger, u url.URL) (string, error)
|
|||||||
)
|
)
|
||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
if host == "localhost" || net.ParseIP(host) != nil {
|
if host == "localhost" {
|
||||||
return "", nil
|
return "", nil
|
||||||
}
|
}
|
||||||
for ctx.Err() == nil {
|
for ctx.Err() == nil {
|
||||||
|
@ -120,13 +120,16 @@ func TestResolveTCPAddrs(t *testing.T) {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
if tt.hostMap[host] == "" {
|
|
||||||
return nil, errors.New("cannot resolve host")
|
|
||||||
}
|
|
||||||
i, err := strconv.Atoi(port)
|
i, err := strconv.Atoi(port)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
if ip := net.ParseIP(host); ip != nil {
|
||||||
|
return &net.TCPAddr{IP: ip, Port: i, Zone: ""}, nil
|
||||||
|
}
|
||||||
|
if tt.hostMap[host] == "" {
|
||||||
|
return nil, errors.New("cannot resolve host")
|
||||||
|
}
|
||||||
return &net.TCPAddr{IP: net.ParseIP(tt.hostMap[host]), Port: i, Zone: ""}, nil
|
return &net.TCPAddr{IP: net.ParseIP(tt.hostMap[host]), Port: i, Zone: ""}, nil
|
||||||
}
|
}
|
||||||
ctx, cancel := context.WithTimeout(context.TODO(), time.Second)
|
ctx, cancel := context.WithTimeout(context.TODO(), time.Second)
|
||||||
@ -152,17 +155,20 @@ func TestURLsEqual(t *testing.T) {
|
|||||||
"second.com": "10.0.11.2",
|
"second.com": "10.0.11.2",
|
||||||
}
|
}
|
||||||
resolveTCPAddr = func(ctx context.Context, addr string) (*net.TCPAddr, error) {
|
resolveTCPAddr = func(ctx context.Context, addr string) (*net.TCPAddr, error) {
|
||||||
host, port, herr := net.SplitHostPort(addr)
|
host, port, err := net.SplitHostPort(addr)
|
||||||
if herr != nil {
|
if err != nil {
|
||||||
return nil, herr
|
return nil, err
|
||||||
}
|
|
||||||
if _, ok := hostm[host]; !ok {
|
|
||||||
return nil, errors.New("cannot resolve host.")
|
|
||||||
}
|
}
|
||||||
i, err := strconv.Atoi(port)
|
i, err := strconv.Atoi(port)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
if ip := net.ParseIP(host); ip != nil {
|
||||||
|
return &net.TCPAddr{IP: ip, Port: i, Zone: ""}, nil
|
||||||
|
}
|
||||||
|
if hostm[host] == "" {
|
||||||
|
return nil, errors.New("cannot resolve host")
|
||||||
|
}
|
||||||
return &net.TCPAddr{IP: net.ParseIP(hostm[host]), Port: i, Zone: ""}, nil
|
return &net.TCPAddr{IP: net.ParseIP(hostm[host]), Port: i, Zone: ""}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -330,6 +336,11 @@ func TestURLStringsEqual(t *testing.T) {
|
|||||||
"http://host1:8080",
|
"http://host1:8080",
|
||||||
"http://host2:8080",
|
"http://host2:8080",
|
||||||
}, errOnResolve},
|
}, errOnResolve},
|
||||||
|
{
|
||||||
|
urlsA: []string{"https://[c262:266f:fa53:0ee6:966e:e3f0:d68f:b046]:2380"},
|
||||||
|
urlsB: []string{"https://[c262:266f:fa53:ee6:966e:e3f0:d68f:b046]:2380"},
|
||||||
|
resolver: resolveTCPAddrDefault,
|
||||||
|
},
|
||||||
}
|
}
|
||||||
for idx, c := range cases {
|
for idx, c := range cases {
|
||||||
t.Logf("TestURLStringsEqual, case #%d", idx)
|
t.Logf("TestURLStringsEqual, case #%d", idx)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user