Pull request: all: fix client upstreams, imp code

Updates #3186.

Squashed commit of the following:

commit a8dd0e2cda3039839d069fe71a5bd0f9635ec064
Author: Ainar Garipov <A.Garipov@AdGuard.COM>
Date:   Fri May 28 12:54:07 2021 +0300

    all: imp code, names

commit 98f86c21ae23b665095075feb4a59dcfcc622bc7
Author: Ainar Garipov <A.Garipov@AdGuard.COM>
Date:   Thu May 27 21:11:37 2021 +0300

    all: fix client upstreams, imp code
This commit is contained in:
Ainar Garipov
2021-05-28 13:02:59 +03:00
parent 48b8579703
commit 3be783bd34
18 changed files with 249 additions and 270 deletions

View File

@@ -10,6 +10,19 @@ import (
"golang.org/x/net/idna"
)
// IPFromAddr returns an IP address from addr. If addr is neither
// a *net.TCPAddr nor a *net.UDPAddr, it returns nil.
func IPFromAddr(addr net.Addr) (ip net.IP) {
switch addr := addr.(type) {
case *net.TCPAddr:
return addr.IP
case *net.UDPAddr:
return addr.IP
}
return nil
}
// IsValidHostOuterRune returns true if r is a valid initial or final rune for
// a hostname label.
func IsValidHostOuterRune(r rune) (ok bool) {

View File

@@ -9,6 +9,14 @@ import (
"github.com/stretchr/testify/require"
)
func TestIPFromAddr(t *testing.T) {
ip := net.IP{1, 2, 3, 4}
assert.Equal(t, net.IP(nil), IPFromAddr(nil))
assert.Equal(t, net.IP(nil), IPFromAddr(struct{ net.Addr }{}))
assert.Equal(t, ip, IPFromAddr(&net.TCPAddr{IP: ip}))
assert.Equal(t, ip, IPFromAddr(&net.UDPAddr{IP: ip}))
}
func TestValidateHardwareAddress(t *testing.T) {
testCases := []struct {
name string