mirror of
https://github.com/AdguardTeam/AdGuardHome.git
synced 2024-11-17 19:08:25 -07:00
82505566f8
Merge in DNS/adguard-home from 2846-cover-aghnet-vol.2 to master
Updates #2846.
Closes #4408.
Squashed commit of the following:
commit 8d62b29d5b5be875cb71e518e479e321d853eb1a
Author: Eugene Burkov <E.Burkov@AdGuard.COM>
Date: Wed Mar 23 20:42:04 2022 +0300
home: recover panic
commit 1d98109e910830bec712c7aecbbbcb8f659d823d
Merge: ac11d751 9ce2a0fb
Author: Eugene Burkov <E.Burkov@AdGuard.COM>
Date: Wed Mar 23 20:32:05 2022 +0300
Merge branch 'master' into 2846-cover-aghnet-vol.2
commit ac11d751fb7951e3dd0940bf425a893223c32789
Author: Eugene Burkov <E.Burkov@AdGuard.COM>
Date: Wed Mar 23 20:29:41 2022 +0300
aghnet: use iotest
commit 7c923df7bafd5d4b91c4b4a01e75ab161944f949
Author: Eugene Burkov <E.Burkov@AdGuard.COM>
Date: Wed Mar 23 20:17:19 2022 +0300
aghnet: cover more
commit 3bfd4d587e4b887b5527d60c0eb6027da15c7e37
Author: Eugene Burkov <E.Burkov@AdGuard.COM>
Date: Wed Mar 23 14:13:59 2022 +0300
aghnet: cover arpdb more
commit cd5cf7bbdecceeab6d3abee10a5572e1e907cc67
Author: Eugene Burkov <E.Burkov@AdGuard.COM>
Date: Wed Mar 23 13:05:35 2022 +0300
all: rm arpdb initial refresh
commit 0fb8d9e44a4d130ca4e8fc2ea5d595ec08555302
Author: Eugene Burkov <E.Burkov@AdGuard.COM>
Date: Tue Mar 22 21:13:16 2022 +0300
aghnet: cover arpdb
158 lines
3.4 KiB
Go
158 lines
3.4 KiB
Go
package aghnet
|
|
|
|
import (
|
|
"io/fs"
|
|
"net"
|
|
"os"
|
|
"testing"
|
|
|
|
"github.com/AdguardTeam/AdGuardHome/internal/aghtest"
|
|
"github.com/AdguardTeam/golibs/errors"
|
|
"github.com/AdguardTeam/golibs/netutil"
|
|
"github.com/AdguardTeam/golibs/testutil"
|
|
"github.com/stretchr/testify/assert"
|
|
"github.com/stretchr/testify/require"
|
|
)
|
|
|
|
// testdata is the filesystem containing data for testing the package.
|
|
var testdata fs.FS = os.DirFS("./testdata")
|
|
|
|
func TestMain(m *testing.M) {
|
|
aghtest.DiscardLogOutput(m)
|
|
}
|
|
|
|
func TestGetInterfaceByIP(t *testing.T) {
|
|
ifaces, err := GetValidNetInterfacesForWeb()
|
|
require.NoError(t, err)
|
|
require.NotEmpty(t, ifaces)
|
|
|
|
for _, iface := range ifaces {
|
|
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)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
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)
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestCheckPort(t *testing.T) {
|
|
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)
|
|
|
|
assert.Equal(t, "listen", target.Op)
|
|
})
|
|
|
|
t.Run("bad_network", func(t *testing.T) {
|
|
err := CheckPort("bad_network", nil, 0)
|
|
assert.NoError(t, err)
|
|
})
|
|
|
|
t.Run("can_bind", func(t *testing.T) {
|
|
err := CheckPort("udp", net.IP{0, 0, 0, 0}, 0)
|
|
assert.NoError(t, err)
|
|
})
|
|
}
|
|
|
|
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))
|
|
})
|
|
}
|