2021-03-16 19:42:15 +03:00
|
|
|
package aghnet
|
2019-02-22 17:59:42 +03:00
|
|
|
|
|
|
|
import (
|
2022-04-19 15:01:49 +03:00
|
|
|
"bytes"
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
2022-01-25 18:54:37 +03:00
|
|
|
"io/fs"
|
2021-08-12 17:33:53 +03:00
|
|
|
"net"
|
2022-11-02 16:18:02 +03:00
|
|
|
"net/netip"
|
2022-01-25 18:54:37 +03:00
|
|
|
"os"
|
2022-04-19 15:01:49 +03:00
|
|
|
"strings"
|
2019-02-22 17:59:42 +03:00
|
|
|
"testing"
|
2021-02-09 19:38:31 +03:00
|
|
|
|
2022-04-19 15:01:49 +03:00
|
|
|
"github.com/AdguardTeam/golibs/errors"
|
2021-12-24 22:36:08 +03:00
|
|
|
"github.com/AdguardTeam/golibs/netutil"
|
|
|
|
"github.com/AdguardTeam/golibs/testutil"
|
2021-08-12 17:33:53 +03:00
|
|
|
"github.com/stretchr/testify/assert"
|
2021-02-09 19:38:31 +03:00
|
|
|
"github.com/stretchr/testify/require"
|
2019-02-22 17:59:42 +03:00
|
|
|
)
|
|
|
|
|
2021-10-14 19:39:21 +03:00
|
|
|
func TestMain(m *testing.M) {
|
2022-11-02 16:18:02 +03:00
|
|
|
testutil.DiscardLogOutput(m)
|
2021-10-14 19:39:21 +03:00
|
|
|
}
|
|
|
|
|
2022-01-25 18:54:37 +03:00
|
|
|
// testdata is the filesystem containing data for testing the package.
|
|
|
|
var testdata fs.FS = os.DirFS("./testdata")
|
|
|
|
|
2022-04-19 15:01:49 +03:00
|
|
|
// substRootDirFS replaces the aghos.RootDirFS function used throughout the
|
|
|
|
// package with fsys for tests ran under t.
|
|
|
|
func substRootDirFS(t testing.TB, fsys fs.FS) {
|
|
|
|
t.Helper()
|
|
|
|
|
|
|
|
prev := rootDirFS
|
|
|
|
t.Cleanup(func() { rootDirFS = prev })
|
|
|
|
rootDirFS = fsys
|
|
|
|
}
|
|
|
|
|
|
|
|
// RunCmdFunc is the signature of aghos.RunCommand function.
|
|
|
|
type RunCmdFunc func(cmd string, args ...string) (code int, out []byte, err error)
|
|
|
|
|
|
|
|
// substShell replaces the the aghos.RunCommand function used throughout the
|
|
|
|
// package with rc for tests ran under t.
|
|
|
|
func substShell(t testing.TB, rc RunCmdFunc) {
|
|
|
|
t.Helper()
|
|
|
|
|
|
|
|
prev := aghosRunCommand
|
|
|
|
t.Cleanup(func() { aghosRunCommand = prev })
|
|
|
|
aghosRunCommand = rc
|
|
|
|
}
|
|
|
|
|
|
|
|
// mapShell is a substitution of aghos.RunCommand that maps the command to it's
|
|
|
|
// execution result. It's only needed to simplify testing.
|
|
|
|
//
|
|
|
|
// TODO(e.burkov): Perhaps put all the shell interactions behind an interface.
|
|
|
|
type mapShell map[string]struct {
|
|
|
|
err error
|
|
|
|
out string
|
|
|
|
code int
|
|
|
|
}
|
|
|
|
|
|
|
|
// theOnlyCmd returns mapShell that only handles a single command and arguments
|
|
|
|
// combination from cmd.
|
|
|
|
func theOnlyCmd(cmd string, code int, out string, err error) (s mapShell) {
|
|
|
|
return mapShell{cmd: {code: code, out: out, err: err}}
|
|
|
|
}
|
|
|
|
|
|
|
|
// RunCmd is a RunCmdFunc handled by s.
|
|
|
|
func (s mapShell) RunCmd(cmd string, args ...string) (code int, out []byte, err error) {
|
|
|
|
key := strings.Join(append([]string{cmd}, args...), " ")
|
|
|
|
ret, ok := s[key]
|
|
|
|
if !ok {
|
|
|
|
return 0, nil, fmt.Errorf("unexpected shell command %q", key)
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret.code, []byte(ret.out), ret.err
|
|
|
|
}
|
|
|
|
|
|
|
|
// ifaceAddrsFunc is the signature of net.InterfaceAddrs function.
|
|
|
|
type ifaceAddrsFunc func() (ifaces []net.Addr, err error)
|
|
|
|
|
|
|
|
// substNetInterfaceAddrs replaces the the net.InterfaceAddrs function used
|
|
|
|
// throughout the package with f for tests ran under t.
|
|
|
|
func substNetInterfaceAddrs(t *testing.T, f ifaceAddrsFunc) {
|
|
|
|
t.Helper()
|
|
|
|
|
|
|
|
prev := netInterfaceAddrs
|
|
|
|
t.Cleanup(func() { netInterfaceAddrs = prev })
|
|
|
|
netInterfaceAddrs = f
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestGatewayIP(t *testing.T) {
|
|
|
|
const ifaceName = "ifaceName"
|
|
|
|
const cmd = "ip route show dev " + ifaceName
|
|
|
|
|
|
|
|
testCases := []struct {
|
|
|
|
shell mapShell
|
2022-11-02 16:18:02 +03:00
|
|
|
want netip.Addr
|
|
|
|
name string
|
2022-04-19 15:01:49 +03:00
|
|
|
}{{
|
|
|
|
shell: theOnlyCmd(cmd, 0, `default via 1.2.3.4 onlink`, nil),
|
2022-11-02 16:18:02 +03:00
|
|
|
want: netip.MustParseAddr("1.2.3.4"),
|
|
|
|
name: "success_v4",
|
2022-04-19 15:01:49 +03:00
|
|
|
}, {
|
|
|
|
shell: theOnlyCmd(cmd, 0, `default via ::ffff onlink`, nil),
|
2022-11-02 16:18:02 +03:00
|
|
|
want: netip.MustParseAddr("::ffff"),
|
|
|
|
name: "success_v6",
|
2022-04-19 15:01:49 +03:00
|
|
|
}, {
|
|
|
|
shell: theOnlyCmd(cmd, 0, `non-default via 1.2.3.4 onlink`, nil),
|
2022-11-02 16:18:02 +03:00
|
|
|
want: netip.Addr{},
|
|
|
|
name: "bad_output",
|
2022-04-19 15:01:49 +03:00
|
|
|
}, {
|
|
|
|
shell: theOnlyCmd(cmd, 0, "", errors.Error("can't run command")),
|
2022-11-02 16:18:02 +03:00
|
|
|
want: netip.Addr{},
|
|
|
|
name: "err_runcmd",
|
2022-04-19 15:01:49 +03:00
|
|
|
}, {
|
|
|
|
shell: theOnlyCmd(cmd, 1, "", nil),
|
2022-11-02 16:18:02 +03:00
|
|
|
want: netip.Addr{},
|
|
|
|
name: "bad_code",
|
2022-04-19 15:01:49 +03:00
|
|
|
}}
|
|
|
|
|
|
|
|
for _, tc := range testCases {
|
|
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
|
|
substShell(t, tc.shell.RunCmd)
|
|
|
|
|
|
|
|
assert.Equal(t, tc.want, GatewayIP(ifaceName))
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-17 18:23:30 +03:00
|
|
|
func TestInterfaceByIP(t *testing.T) {
|
2020-02-13 18:42:07 +03:00
|
|
|
ifaces, err := GetValidNetInterfacesForWeb()
|
2022-01-21 19:21:38 +03:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.NotEmpty(t, ifaces)
|
|
|
|
|
2019-02-22 17:59:42 +03:00
|
|
|
for _, iface := range ifaces {
|
2022-01-21 19:21:38 +03:00
|
|
|
t.Run(iface.Name, func(t *testing.T) {
|
|
|
|
require.NotEmpty(t, iface.Addresses)
|
|
|
|
|
|
|
|
for _, ip := range iface.Addresses {
|
2022-08-17 18:23:30 +03:00
|
|
|
ifaceName := InterfaceByIP(ip)
|
2022-01-21 19:21:38 +03:00
|
|
|
require.Equal(t, iface.Name, ifaceName)
|
|
|
|
}
|
|
|
|
})
|
2019-02-22 17:59:42 +03:00
|
|
|
}
|
|
|
|
}
|
2021-08-12 17:33:53 +03:00
|
|
|
|
|
|
|
func TestBroadcastFromIPNet(t *testing.T) {
|
2022-11-02 16:18:02 +03:00
|
|
|
known4 := netip.MustParseAddr("192.168.0.1")
|
|
|
|
fullBroadcast4 := netip.MustParseAddr("255.255.255.255")
|
|
|
|
|
|
|
|
known6 := netip.MustParseAddr("102:304:506:708:90a:b0c:d0e:f10")
|
2021-08-12 17:33:53 +03:00
|
|
|
|
|
|
|
testCases := []struct {
|
2022-11-02 16:18:02 +03:00
|
|
|
pref netip.Prefix
|
|
|
|
want netip.Addr
|
|
|
|
name string
|
2021-08-12 17:33:53 +03:00
|
|
|
}{{
|
2022-11-02 16:18:02 +03:00
|
|
|
pref: netip.PrefixFrom(known4, 0),
|
|
|
|
want: fullBroadcast4,
|
2021-08-12 17:33:53 +03:00
|
|
|
name: "full",
|
|
|
|
}, {
|
2022-11-02 16:18:02 +03:00
|
|
|
pref: netip.PrefixFrom(known4, 20),
|
|
|
|
want: netip.MustParseAddr("192.168.15.255"),
|
|
|
|
name: "full",
|
|
|
|
}, {
|
|
|
|
pref: netip.PrefixFrom(known6, netutil.IPv6BitLen),
|
2021-08-12 17:33:53 +03:00
|
|
|
want: known6,
|
2022-11-02 16:18:02 +03:00
|
|
|
name: "ipv6_no_mask",
|
2021-08-12 17:33:53 +03:00
|
|
|
}, {
|
2022-11-02 16:18:02 +03:00
|
|
|
pref: netip.PrefixFrom(known4, netutil.IPv4BitLen),
|
|
|
|
want: known4,
|
2021-08-12 17:33:53 +03:00
|
|
|
name: "ipv4_no_mask",
|
|
|
|
}, {
|
2022-11-02 16:18:02 +03:00
|
|
|
pref: netip.PrefixFrom(netip.IPv4Unspecified(), 0),
|
|
|
|
want: fullBroadcast4,
|
2021-08-12 17:33:53 +03:00
|
|
|
name: "unspecified",
|
2022-11-02 16:18:02 +03:00
|
|
|
}, {
|
|
|
|
pref: netip.Prefix{},
|
|
|
|
want: netip.Addr{},
|
|
|
|
name: "invalid",
|
2021-08-12 17:33:53 +03:00
|
|
|
}}
|
|
|
|
|
|
|
|
for _, tc := range testCases {
|
|
|
|
t.Run(tc.name, func(t *testing.T) {
|
2022-11-02 16:18:02 +03:00
|
|
|
assert.Equal(t, tc.want, BroadcastFromPref(tc.pref))
|
2021-08-12 17:33:53 +03:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2021-12-24 22:36:08 +03:00
|
|
|
|
|
|
|
func TestCheckPort(t *testing.T) {
|
2022-11-23 16:52:05 +03:00
|
|
|
laddr := netip.AddrPortFrom(netutil.IPv4Localhost(), 0)
|
2022-11-02 16:18:02 +03:00
|
|
|
|
2022-01-21 19:21:38 +03:00
|
|
|
t.Run("tcp_bound", func(t *testing.T) {
|
2022-11-02 16:18:02 +03:00
|
|
|
l, err := net.Listen("tcp", laddr.String())
|
2022-01-21 19:21:38 +03:00
|
|
|
require.NoError(t, err)
|
|
|
|
testutil.CleanupAndRequireSuccess(t, l.Close)
|
|
|
|
|
2022-11-02 16:18:02 +03:00
|
|
|
ipp := testutil.RequireTypeAssert[*net.TCPAddr](t, l.Addr()).AddrPort()
|
|
|
|
require.Equal(t, laddr.Addr(), ipp.Addr())
|
|
|
|
require.NotZero(t, ipp.Port())
|
2022-01-21 19:21:38 +03:00
|
|
|
|
2022-11-02 16:18:02 +03:00
|
|
|
err = CheckPort("tcp", ipp)
|
2022-01-21 19:21:38 +03:00
|
|
|
target := &net.OpError{}
|
|
|
|
require.ErrorAs(t, err, &target)
|
|
|
|
|
|
|
|
assert.Equal(t, "listen", target.Op)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("udp_bound", func(t *testing.T) {
|
2022-11-02 16:18:02 +03:00
|
|
|
conn, err := net.ListenPacket("udp", laddr.String())
|
2022-01-21 19:21:38 +03:00
|
|
|
require.NoError(t, err)
|
|
|
|
testutil.CleanupAndRequireSuccess(t, conn.Close)
|
|
|
|
|
2022-11-02 16:18:02 +03:00
|
|
|
ipp := testutil.RequireTypeAssert[*net.UDPAddr](t, conn.LocalAddr()).AddrPort()
|
|
|
|
require.Equal(t, laddr.Addr(), ipp.Addr())
|
|
|
|
require.NotZero(t, ipp.Port())
|
2022-01-21 19:21:38 +03:00
|
|
|
|
2022-11-02 16:18:02 +03:00
|
|
|
err = CheckPort("udp", ipp)
|
2022-01-21 19:21:38 +03:00
|
|
|
target := &net.OpError{}
|
|
|
|
require.ErrorAs(t, err, &target)
|
2021-12-24 22:36:08 +03:00
|
|
|
|
2022-01-21 19:21:38 +03:00
|
|
|
assert.Equal(t, "listen", target.Op)
|
|
|
|
})
|
2021-12-24 22:36:08 +03:00
|
|
|
|
2022-01-21 19:21:38 +03:00
|
|
|
t.Run("bad_network", func(t *testing.T) {
|
2022-11-02 16:18:02 +03:00
|
|
|
err := CheckPort("bad_network", netip.AddrPortFrom(netip.Addr{}, 0))
|
2022-01-21 19:21:38 +03:00
|
|
|
assert.NoError(t, err)
|
|
|
|
})
|
2021-12-24 22:36:08 +03:00
|
|
|
|
2022-01-21 19:21:38 +03:00
|
|
|
t.Run("can_bind", func(t *testing.T) {
|
2022-11-02 16:18:02 +03:00
|
|
|
err := CheckPort("udp", netip.AddrPortFrom(netip.IPv4Unspecified(), 0))
|
2022-01-21 19:21:38 +03:00
|
|
|
assert.NoError(t, err)
|
|
|
|
})
|
2021-12-24 22:36:08 +03:00
|
|
|
}
|
2022-04-19 15:01:49 +03:00
|
|
|
|
|
|
|
func TestCollectAllIfacesAddrs(t *testing.T) {
|
|
|
|
testCases := []struct {
|
|
|
|
name string
|
|
|
|
wantErrMsg string
|
|
|
|
addrs []net.Addr
|
|
|
|
wantAddrs []string
|
|
|
|
}{{
|
|
|
|
name: "success",
|
|
|
|
wantErrMsg: ``,
|
|
|
|
addrs: []net.Addr{&net.IPNet{
|
|
|
|
IP: net.IP{1, 2, 3, 4},
|
|
|
|
Mask: net.CIDRMask(24, netutil.IPv4BitLen),
|
|
|
|
}, &net.IPNet{
|
|
|
|
IP: net.IP{4, 3, 2, 1},
|
|
|
|
Mask: net.CIDRMask(16, netutil.IPv4BitLen),
|
|
|
|
}},
|
|
|
|
wantAddrs: []string{"1.2.3.4", "4.3.2.1"},
|
|
|
|
}, {
|
|
|
|
name: "not_cidr",
|
|
|
|
wantErrMsg: `parsing cidr: invalid CIDR address: 1.2.3.4`,
|
|
|
|
addrs: []net.Addr{&net.IPAddr{
|
|
|
|
IP: net.IP{1, 2, 3, 4},
|
|
|
|
}},
|
|
|
|
wantAddrs: nil,
|
|
|
|
}, {
|
|
|
|
name: "empty",
|
|
|
|
wantErrMsg: ``,
|
|
|
|
addrs: []net.Addr{},
|
|
|
|
wantAddrs: nil,
|
|
|
|
}}
|
|
|
|
|
|
|
|
for _, tc := range testCases {
|
|
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
|
|
substNetInterfaceAddrs(t, func() ([]net.Addr, error) { return tc.addrs, nil })
|
|
|
|
|
|
|
|
addrs, err := CollectAllIfacesAddrs()
|
|
|
|
testutil.AssertErrorMsg(t, tc.wantErrMsg, err)
|
|
|
|
|
|
|
|
assert.Equal(t, tc.wantAddrs, addrs)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
t.Run("internal_error", func(t *testing.T) {
|
|
|
|
const errAddrs errors.Error = "can't get addresses"
|
|
|
|
const wantErrMsg string = `getting interfaces addresses: ` + string(errAddrs)
|
|
|
|
|
|
|
|
substNetInterfaceAddrs(t, func() ([]net.Addr, error) { return nil, errAddrs })
|
|
|
|
|
|
|
|
_, err := CollectAllIfacesAddrs()
|
|
|
|
testutil.AssertErrorMsg(t, wantErrMsg, err)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestIsAddrInUse(t *testing.T) {
|
|
|
|
t.Run("addr_in_use", func(t *testing.T) {
|
|
|
|
l, err := net.Listen("tcp", "0.0.0.0:0")
|
|
|
|
require.NoError(t, err)
|
|
|
|
testutil.CleanupAndRequireSuccess(t, l.Close)
|
|
|
|
|
|
|
|
_, err = net.Listen(l.Addr().Network(), l.Addr().String())
|
|
|
|
assert.True(t, IsAddrInUse(err))
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("another", func(t *testing.T) {
|
|
|
|
const anotherErr errors.Error = "not addr in use"
|
|
|
|
|
|
|
|
assert.False(t, IsAddrInUse(anotherErr))
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestNetInterface_MarshalJSON(t *testing.T) {
|
|
|
|
const want = `{` +
|
|
|
|
`"hardware_address":"aa:bb:cc:dd:ee:ff",` +
|
|
|
|
`"flags":"up|multicast",` +
|
|
|
|
`"ip_addresses":["1.2.3.4","aaaa::1"],` +
|
|
|
|
`"name":"iface0",` +
|
|
|
|
`"mtu":1500` +
|
|
|
|
`}` + "\n"
|
|
|
|
|
2022-11-02 16:18:02 +03:00
|
|
|
ip4, ok := netip.AddrFromSlice([]byte{1, 2, 3, 4})
|
|
|
|
require.True(t, ok)
|
|
|
|
|
|
|
|
ip6, ok := netip.AddrFromSlice([]byte{0xAA, 0xAA, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1})
|
|
|
|
require.True(t, ok)
|
|
|
|
|
|
|
|
net4 := netip.PrefixFrom(ip4, 24)
|
|
|
|
net6 := netip.PrefixFrom(ip6, 8)
|
2022-04-19 15:01:49 +03:00
|
|
|
|
|
|
|
iface := &NetInterface{
|
2022-11-02 16:18:02 +03:00
|
|
|
Addresses: []netip.Addr{ip4, ip6},
|
|
|
|
Subnets: []netip.Prefix{net4, net6},
|
2022-04-19 15:01:49 +03:00
|
|
|
Name: "iface0",
|
|
|
|
HardwareAddr: net.HardwareAddr{0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF},
|
|
|
|
Flags: net.FlagUp | net.FlagMulticast,
|
|
|
|
MTU: 1500,
|
|
|
|
}
|
|
|
|
|
|
|
|
b := &bytes.Buffer{}
|
|
|
|
err := json.NewEncoder(b).Encode(iface)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
assert.Equal(t, want, b.String())
|
|
|
|
}
|