2021-05-21 12:55:42 +01:00
|
|
|
//go:build !windows
|
|
|
|
|
2021-03-22 13:46:36 +00:00
|
|
|
package aghnet
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"testing"
|
|
|
|
|
2022-10-27 14:11:36 +01:00
|
|
|
"github.com/AdguardTeam/golibs/testutil"
|
2021-03-22 13:46:36 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
2022-03-23 17:47:45 +00:00
|
|
|
func createTestSystemResolversImpl(
|
2021-03-22 13:46:36 +00:00
|
|
|
t *testing.T,
|
|
|
|
hostGenFunc HostGenFunc,
|
|
|
|
) (imp *systemResolvers) {
|
|
|
|
t.Helper()
|
|
|
|
|
2022-03-23 17:47:45 +00:00
|
|
|
sr := createTestSystemResolvers(t, hostGenFunc)
|
2021-03-22 13:46:36 +00:00
|
|
|
|
2022-10-27 14:11:36 +01:00
|
|
|
return testutil.RequireTypeAssert[*systemResolvers](t, sr)
|
2021-03-22 13:46:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestSystemResolvers_Refresh(t *testing.T) {
|
|
|
|
t.Run("expected_error", func(t *testing.T) {
|
2022-03-23 17:47:45 +00:00
|
|
|
sr := createTestSystemResolvers(t, nil)
|
2021-03-22 13:46:36 +00:00
|
|
|
|
2021-03-31 13:00:47 +01:00
|
|
|
assert.NoError(t, sr.refresh())
|
2021-03-22 13:46:36 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("unexpected_error", func(t *testing.T) {
|
2022-03-23 17:47:45 +00:00
|
|
|
_, err := NewSystemResolvers(func() string {
|
2021-03-22 13:46:36 +00:00
|
|
|
return "127.0.0.1::123"
|
|
|
|
})
|
|
|
|
assert.Error(t, err)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSystemResolvers_DialFunc(t *testing.T) {
|
2022-03-23 17:47:45 +00:00
|
|
|
imp := createTestSystemResolversImpl(t, nil)
|
2021-03-22 13:46:36 +00:00
|
|
|
|
|
|
|
testCases := []struct {
|
2021-04-28 19:34:18 +01:00
|
|
|
want error
|
2021-03-22 13:46:36 +00:00
|
|
|
name string
|
|
|
|
address string
|
|
|
|
}{{
|
2021-04-28 19:34:18 +01:00
|
|
|
want: errFakeDial,
|
2022-03-23 17:47:45 +00:00
|
|
|
name: "valid_ipv4",
|
2021-03-22 13:46:36 +00:00
|
|
|
address: "127.0.0.1",
|
|
|
|
}, {
|
2021-04-28 19:34:18 +01:00
|
|
|
want: errFakeDial,
|
|
|
|
name: "valid_ipv6_port",
|
|
|
|
address: "[::1]:53",
|
|
|
|
}, {
|
|
|
|
want: errFakeDial,
|
|
|
|
name: "valid_ipv6_zone_port",
|
|
|
|
address: "[::1%lo0]:53",
|
|
|
|
}, {
|
|
|
|
want: errBadAddrPassed,
|
2021-03-22 13:46:36 +00:00
|
|
|
name: "invalid_split_host",
|
|
|
|
address: "127.0.0.1::123",
|
|
|
|
}, {
|
2021-04-28 19:34:18 +01:00
|
|
|
want: errUnexpectedHostFormat,
|
|
|
|
name: "invalid_ipv6_zone_port",
|
|
|
|
address: "[::1%%lo0]:53",
|
|
|
|
}, {
|
|
|
|
want: errBadAddrPassed,
|
2021-03-22 13:46:36 +00:00
|
|
|
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)
|
2021-10-22 09:58:18 +01:00
|
|
|
|
2021-03-22 13:46:36 +00:00
|
|
|
assert.ErrorIs(t, err, tc.want)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|