2021-03-16 09:42:15 -07:00
|
|
|
package aghnet
|
2019-02-22 07:59:42 -07:00
|
|
|
|
|
|
|
import (
|
2022-03-15 10:57:46 -07:00
|
|
|
"io/fs"
|
2021-08-12 07:33:53 -07:00
|
|
|
"net"
|
2022-03-15 10:57:46 -07:00
|
|
|
"os"
|
2019-02-22 07:59:42 -07:00
|
|
|
"testing"
|
2021-02-09 09:38:31 -07:00
|
|
|
|
2021-10-14 09:39:21 -07:00
|
|
|
"github.com/AdguardTeam/AdGuardHome/internal/aghtest"
|
2022-03-23 10:47:45 -07:00
|
|
|
"github.com/AdguardTeam/golibs/errors"
|
2021-12-23 10:16:08 -07:00
|
|
|
"github.com/AdguardTeam/golibs/netutil"
|
|
|
|
"github.com/AdguardTeam/golibs/testutil"
|
2021-08-12 07:33:53 -07:00
|
|
|
"github.com/stretchr/testify/assert"
|
2021-02-09 09:38:31 -07:00
|
|
|
"github.com/stretchr/testify/require"
|
2019-02-22 07:59:42 -07:00
|
|
|
)
|
|
|
|
|
2022-03-15 10:57:46 -07:00
|
|
|
// testdata is the filesystem containing data for testing the package.
|
|
|
|
var testdata fs.FS = os.DirFS("./testdata")
|
|
|
|
|
2021-10-14 09:39:21 -07:00
|
|
|
func TestMain(m *testing.M) {
|
|
|
|
aghtest.DiscardLogOutput(m)
|
|
|
|
}
|
|
|
|
|
2022-01-21 09:21:38 -07:00
|
|
|
func TestGetInterfaceByIP(t *testing.T) {
|
2020-02-13 08:42:07 -07:00
|
|
|
ifaces, err := GetValidNetInterfacesForWeb()
|
2022-01-21 09:21:38 -07:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.NotEmpty(t, ifaces)
|
|
|
|
|
2019-02-22 07:59:42 -07:00
|
|
|
for _, iface := range ifaces {
|
2022-01-21 09:21:38 -07:00
|
|
|
t.Run(iface.Name, func(t *testing.T) {
|
|
|
|
require.NotEmpty(t, iface.Addresses)
|
|
|
|
|
|
|
|
for _, ip := range iface.Addresses {
|
|
|
|
ifaceName := GetInterfaceByIP(ip)
|
|
|
|
require.Equal(t, iface.Name, ifaceName)
|
|
|
|
}
|
|
|
|
})
|
2019-02-22 07:59:42 -07:00
|
|
|
}
|
|
|
|
}
|
2021-08-12 07:33:53 -07:00
|
|
|
|
|
|
|
func TestBroadcastFromIPNet(t *testing.T) {
|
|
|
|
known6 := net.IP{
|
|
|
|
1, 2, 3, 4,
|
|
|
|
5, 6, 7, 8,
|
|
|
|
9, 10, 11, 12,
|
|
|
|
13, 14, 15, 16,
|
|
|
|
}
|
|
|
|
|
|
|
|
testCases := []struct {
|
|
|
|
name string
|
|
|
|
subnet *net.IPNet
|
|
|
|
want net.IP
|
|
|
|
}{{
|
|
|
|
name: "full",
|
|
|
|
subnet: &net.IPNet{
|
|
|
|
IP: net.IP{192, 168, 0, 1},
|
|
|
|
Mask: net.IPMask{255, 255, 15, 0},
|
|
|
|
},
|
|
|
|
want: net.IP{192, 168, 240, 255},
|
|
|
|
}, {
|
|
|
|
name: "ipv6_no_mask",
|
|
|
|
subnet: &net.IPNet{
|
|
|
|
IP: known6,
|
|
|
|
},
|
|
|
|
want: known6,
|
|
|
|
}, {
|
|
|
|
name: "ipv4_no_mask",
|
|
|
|
subnet: &net.IPNet{
|
|
|
|
IP: net.IP{192, 168, 1, 2},
|
|
|
|
},
|
|
|
|
want: net.IP{192, 168, 1, 255},
|
|
|
|
}, {
|
|
|
|
name: "unspecified",
|
|
|
|
subnet: &net.IPNet{
|
|
|
|
IP: net.IP{0, 0, 0, 0},
|
|
|
|
Mask: net.IPMask{0, 0, 0, 0},
|
|
|
|
},
|
|
|
|
want: net.IPv4bcast,
|
|
|
|
}}
|
|
|
|
|
|
|
|
for _, tc := range testCases {
|
|
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
|
|
bc := BroadcastFromIPNet(tc.subnet)
|
|
|
|
assert.True(t, bc.Equal(tc.want), bc)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2021-12-23 10:16:08 -07:00
|
|
|
|
|
|
|
func TestCheckPort(t *testing.T) {
|
2022-01-21 09:21:38 -07:00
|
|
|
t.Run("tcp_bound", func(t *testing.T) {
|
|
|
|
l, err := net.Listen("tcp", "127.0.0.1:")
|
|
|
|
require.NoError(t, err)
|
|
|
|
testutil.CleanupAndRequireSuccess(t, l.Close)
|
|
|
|
|
|
|
|
ipp := netutil.IPPortFromAddr(l.Addr())
|
|
|
|
require.NotNil(t, ipp)
|
|
|
|
require.NotNil(t, ipp.IP)
|
|
|
|
require.NotZero(t, ipp.Port)
|
|
|
|
|
|
|
|
err = CheckPort("tcp", ipp.IP, ipp.Port)
|
|
|
|
target := &net.OpError{}
|
|
|
|
require.ErrorAs(t, err, &target)
|
|
|
|
|
|
|
|
assert.Equal(t, "listen", target.Op)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("udp_bound", func(t *testing.T) {
|
|
|
|
conn, err := net.ListenPacket("udp", "127.0.0.1:")
|
|
|
|
require.NoError(t, err)
|
|
|
|
testutil.CleanupAndRequireSuccess(t, conn.Close)
|
|
|
|
|
|
|
|
ipp := netutil.IPPortFromAddr(conn.LocalAddr())
|
|
|
|
require.NotNil(t, ipp)
|
|
|
|
require.NotNil(t, ipp.IP)
|
|
|
|
require.NotZero(t, ipp.Port)
|
|
|
|
|
|
|
|
err = CheckPort("udp", ipp.IP, ipp.Port)
|
|
|
|
target := &net.OpError{}
|
|
|
|
require.ErrorAs(t, err, &target)
|
2021-12-23 10:16:08 -07:00
|
|
|
|
2022-01-21 09:21:38 -07:00
|
|
|
assert.Equal(t, "listen", target.Op)
|
|
|
|
})
|
2021-12-23 10:16:08 -07:00
|
|
|
|
2022-01-21 09:21:38 -07:00
|
|
|
t.Run("bad_network", func(t *testing.T) {
|
|
|
|
err := CheckPort("bad_network", nil, 0)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
})
|
2021-12-23 10:16:08 -07:00
|
|
|
|
2022-01-21 09:21:38 -07:00
|
|
|
t.Run("can_bind", func(t *testing.T) {
|
|
|
|
err := CheckPort("udp", net.IP{0, 0, 0, 0}, 0)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
})
|
2021-12-23 10:16:08 -07:00
|
|
|
}
|
2022-03-23 10:47:45 -07:00
|
|
|
|
|
|
|
func TestCollectAllIfacesAddrs(t *testing.T) {
|
|
|
|
addrs, err := CollectAllIfacesAddrs()
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
assert.NotEmpty(t, addrs)
|
|
|
|
}
|
|
|
|
|
|
|
|
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))
|
|
|
|
})
|
|
|
|
}
|