2021-05-21 14:55:42 +03:00
|
|
|
//go:build !windows
|
2021-06-15 19:42:41 +03:00
|
|
|
// +build !windows
|
2021-05-21 14:55:42 +03:00
|
|
|
|
2021-03-22 16:46:36 +03:00
|
|
|
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)
|
|
|
|
|
2021-03-31 15:00:47 +03:00
|
|
|
assert.NoError(t, sr.refresh())
|
2021-03-22 16:46:36 +03:00
|
|
|
})
|
|
|
|
|
|
|
|
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 {
|
2021-04-28 21:34:18 +03:00
|
|
|
want error
|
2021-03-22 16:46:36 +03:00
|
|
|
name string
|
|
|
|
address string
|
|
|
|
}{{
|
2021-04-28 21:34:18 +03:00
|
|
|
want: errFakeDial,
|
2021-03-22 16:46:36 +03:00
|
|
|
name: "valid",
|
|
|
|
address: "127.0.0.1",
|
|
|
|
}, {
|
2021-04-28 21:34:18 +03: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 16:46:36 +03:00
|
|
|
name: "invalid_split_host",
|
|
|
|
address: "127.0.0.1::123",
|
|
|
|
}, {
|
2021-04-28 21:34:18 +03:00
|
|
|
want: errUnexpectedHostFormat,
|
|
|
|
name: "invalid_ipv6_zone_port",
|
|
|
|
address: "[::1%%lo0]:53",
|
|
|
|
}, {
|
|
|
|
want: errBadAddrPassed,
|
2021-03-22 16:46:36 +03: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)
|
|
|
|
assert.ErrorIs(t, err, tc.want)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|