mirror of
https://github.com/AdguardTeam/AdGuardHome.git
synced 2024-11-26 15:05:50 +03:00
7528699fc2
Merge in DNS/adguard-home from 3987-fix-nil-deref to master Updates #3987. Updates #2846. Squashed commit of the following: commit d653e09ce88a8b10b2a17fea1563c419895c714c Author: Eugene Burkov <E.Burkov@AdGuard.COM> Date: Thu Dec 23 20:08:51 2021 +0300 all: log changes commit c47a4eeacf76fa7df2d01af166dee9d52528ac58 Author: Eugene Burkov <E.Burkov@AdGuard.COM> Date: Thu Dec 23 19:22:39 2021 +0300 aghnet: fix windows tests commit 9c91f14ccfe967ada3c00ddb86d673238e52c12d Author: Eugene Burkov <E.Burkov@AdGuard.COM> Date: Thu Dec 23 19:09:49 2021 +0300 aghnet: imp code readability, docs commit d3df15d1892e4ebfe7f8ea7144e39a0c712fce52 Author: Eugene Burkov <E.Burkov@AdGuard.COM> Date: Thu Dec 23 18:47:28 2021 +0300 aghnet: fix nil pointer dereference
90 lines
1.9 KiB
Go
90 lines
1.9 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 TestGetValidNetInterfacesForWeb(t *testing.T) {
|
|
ifaces, err := GetValidNetInterfacesForWeb()
|
|
require.NoErrorf(t, err, "cannot get net interfaces: %s", err)
|
|
require.NotEmpty(t, ifaces, "no net interfaces found")
|
|
for _, iface := range ifaces {
|
|
require.NotEmptyf(t, iface.Addresses, "no addresses found for %s", iface.Name)
|
|
}
|
|
}
|
|
|
|
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) {
|
|
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)
|
|
}
|