AdGuardHome/internal/aghnet/systemresolvers_others_test.go
Eugene Burkov ea8950a80d Pull request: use testutil
Squashed commit of the following:

commit 5345a14b3565f358c56a37500cafb35b7e397951
Author: Eugene Burkov <E.Burkov@AdGuard.COM>
Date:   Thu Oct 21 21:13:06 2021 +0300

    all: fix windows tests

commit 8b9cdbe3e78f43339d21277f04e686bb154f6968
Author: Eugene Burkov <E.Burkov@AdGuard.COM>
Date:   Thu Oct 21 20:23:55 2021 +0300

    all: imp code

commit 271fdbe74c29d8ea4b53d7f56d2a36612dfed7b3
Author: Eugene Burkov <E.Burkov@AdGuard.COM>
Date:   Thu Oct 21 19:43:32 2021 +0300

    all: imp testing

commit e340f9d48679c57fc8eb579b8b78d4957be111c4
Author: Eugene Burkov <E.Burkov@AdGuard.COM>
Date:   Thu Oct 21 18:53:51 2021 +0300

    all: use testutil
2021-10-22 11:58:18 +03:00

88 lines
1.7 KiB
Go

//go:build !windows
// +build !windows
package aghnet
import (
"context"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func createTestSystemResolversImp(
t *testing.T,
refreshDur time.Duration,
hostGenFunc HostGenFunc,
) (imp *systemResolvers) {
t.Helper()
sr := createTestSystemResolvers(t, refreshDur, hostGenFunc)
var ok bool
imp, ok = sr.(*systemResolvers)
require.True(t, ok)
return imp
}
func TestSystemResolvers_Refresh(t *testing.T) {
t.Run("expected_error", func(t *testing.T) {
sr := createTestSystemResolvers(t, 0, nil)
assert.NoError(t, sr.refresh())
})
t.Run("unexpected_error", func(t *testing.T) {
_, err := NewSystemResolvers(0, func() string {
return "127.0.0.1::123"
})
assert.Error(t, err)
})
}
func TestSystemResolvers_DialFunc(t *testing.T) {
imp := createTestSystemResolversImp(t, 0, nil)
testCases := []struct {
want error
name string
address string
}{{
want: errFakeDial,
name: "valid",
address: "127.0.0.1",
}, {
want: errFakeDial,
name: "valid_ipv6_port",
address: "[::1]:53",
}, {
want: errFakeDial,
name: "valid_ipv6_zone_port",
address: "[::1%lo0]:53",
}, {
want: errBadAddrPassed,
name: "invalid_split_host",
address: "127.0.0.1::123",
}, {
want: errUnexpectedHostFormat,
name: "invalid_ipv6_zone_port",
address: "[::1%%lo0]:53",
}, {
want: errBadAddrPassed,
name: "invalid_parse_ip",
address: "not-ip",
}}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
conn, err := imp.dialFunc(context.Background(), "", tc.address)
require.Nil(t, conn)
assert.ErrorIs(t, err, tc.want)
})
}
}