mirror of
https://github.com/AdguardTeam/AdGuardHome.git
synced 2024-12-19 06:11:35 +03:00
3f5605c42e
Merge in DNS/adguard-home from 2846-cover-aghnet-vol.1 to master Updates #2846. Squashed commit of the following: commit 368e75b0bacb290f9929b8a5a682b06f2d75df6a Author: Eugene Burkov <E.Burkov@AdGuard.COM> Date: Fri Jan 21 19:11:59 2022 +0300 aghnet: imp tests commit 8bb3e2a1680fd30294f7c82693891ffb19474c6a Author: Eugene Burkov <E.Burkov@AdGuard.COM> Date: Fri Jan 21 18:27:06 2022 +0300 aghnet: rm unused test commit 28d8e64880f845810d0af629e5d1f06b9bde5b28 Author: Eugene Burkov <E.Burkov@AdGuard.COM> Date: Fri Jan 21 18:18:22 2022 +0300 aghnet: cover with tests
128 lines
2.7 KiB
Go
128 lines
2.7 KiB
Go
package aghnet
|
|
|
|
import (
|
|
"net"
|
|
"testing"
|
|
|
|
"github.com/AdguardTeam/AdGuardHome/internal/aghtest"
|
|
"github.com/AdguardTeam/golibs/netutil"
|
|
"github.com/AdguardTeam/golibs/testutil"
|
|
"github.com/stretchr/testify/assert"
|
|
"github.com/stretchr/testify/require"
|
|
)
|
|
|
|
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)
|
|
})
|
|
}
|