AdGuardHome/internal/aghnet/net_test.go
Eugene Burkov ce868268bc Pull request 2094: AG-27796 upd golibs
Squashed commit of the following:

commit a205c1302e3979d1c4270b11d253b6bc0d292216
Merge: de289ff4f 214175eb4
Author: Eugene Burkov <E.Burkov@AdGuard.COM>
Date:   Thu Dec 7 16:36:53 2023 +0300

    Merge branch 'master' into AG-27796-upd-golibs

commit de289ff4f3199bc2dffb029a9804cabe86b3b886
Merge: b2322093c a0ec0b2b5
Author: Eugene Burkov <E.Burkov@AdGuard.COM>
Date:   Wed Dec 6 12:12:35 2023 +0300

    Merge branch 'master' into AG-27796-upd-golibs

commit b2322093cea0ecdf34be66b56a9ab0fd7b32c7b9
Author: Eugene Burkov <E.Burkov@AdGuard.COM>
Date:   Tue Dec 5 19:20:30 2023 +0300

    filtering: imp cognit

commit 563aa45824a2cc9d63d2c394f6a60f053e5d6d3b
Author: Eugene Burkov <E.Burkov@AdGuard.COM>
Date:   Mon Dec 4 17:02:56 2023 +0300

    all: imp code

commit 064a00bce4340caa4cea052fa8234cedb8dcea01
Author: Eugene Burkov <E.Burkov@AdGuard.COM>
Date:   Tue Nov 28 18:41:07 2023 +0300

    all: upd golibs
2023-12-07 16:48:55 +03:00

90 lines
2.2 KiB
Go

package aghnet_test
import (
"net"
"net/netip"
"net/url"
"testing"
"github.com/AdguardTeam/AdGuardHome/internal/aghnet"
"github.com/AdguardTeam/golibs/netutil"
"github.com/AdguardTeam/golibs/testutil"
"github.com/stretchr/testify/assert"
)
func TestMain(m *testing.M) {
testutil.DiscardLogOutput(m)
}
func TestParseAddrPort(t *testing.T) {
const defaultPort = 1
v4addr := netip.MustParseAddr("1.2.3.4")
testCases := []struct {
name string
input string
wantErrMsg string
want netip.AddrPort
}{{
name: "success_ip",
input: v4addr.String(),
wantErrMsg: "",
want: netip.AddrPortFrom(v4addr, defaultPort),
}, {
name: "success_ip_port",
input: netutil.JoinHostPort(v4addr.String(), 5),
wantErrMsg: "",
want: netip.AddrPortFrom(v4addr, 5),
}, {
name: "success_url",
input: (&url.URL{
Scheme: "tcp",
Host: v4addr.String(),
}).String(),
wantErrMsg: "",
want: netip.AddrPortFrom(v4addr, defaultPort),
}, {
name: "success_url_port",
input: (&url.URL{
Scheme: "tcp",
Host: netutil.JoinHostPort(v4addr.String(), 5),
}).String(),
wantErrMsg: "",
want: netip.AddrPortFrom(v4addr, 5),
}, {
name: "error_invalid_ip",
input: "256.256.256.256",
wantErrMsg: `not an ip:port
ParseAddr("256.256.256.256"): IPv4 field has value >255`,
want: netip.AddrPort{},
}, {
name: "error_invalid_port",
input: net.JoinHostPort(v4addr.String(), "-5"),
wantErrMsg: `invalid port "-5" parsing "1.2.3.4:-5"
ParseAddr("1.2.3.4:-5"): unexpected character (at ":-5")`,
want: netip.AddrPort{},
}, {
name: "error_invalid_url",
input: "tcp:://1.2.3.4",
wantErrMsg: `invalid port "//1.2.3.4" parsing "tcp:://1.2.3.4"
ParseAddr("tcp:://1.2.3.4"): each colon-separated field must have at least ` +
`one digit (at "tcp:://1.2.3.4")`,
want: netip.AddrPort{},
}, {
name: "empty",
input: "",
want: netip.AddrPort{},
wantErrMsg: `not an ip:port
ParseAddr(""): unable to parse IP`,
}}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
ap, err := aghnet.ParseAddrPort(tc.input, defaultPort)
testutil.AssertErrorMsg(t, tc.wantErrMsg, err)
assert.Equal(t, tc.want, ap)
})
}
}