mirror of
https://github.com/AdguardTeam/AdGuardHome.git
synced 2024-11-28 09:58:52 +03:00
39aeaf8910
Merge in DNS/adguard-home from 4923-gopacket-dhcp-vol.3 to master
Updates #4923.
Squashed commit of the following:
commit 1a09c436e5666a515084cd5e76cfccd67991ae5e
Merge: 95bcf0720 c3f141a0a
Author: Eugene Burkov <E.Burkov@AdGuard.COM>
Date: Thu Sep 28 19:38:57 2023 +0300
Merge branch 'master' into 4923-gopacket-dhcp-vol.3
commit 95bcf07206434fd451632e819926871ba8c14f08
Author: Eugene Burkov <E.Burkov@AdGuard.COM>
Date: Thu Sep 28 13:19:42 2023 +0300
dhcpsvc: fix interface to match legacy version
commit 5da513ce177319f19698c5a8e1d10affaaf5e85c
Author: Eugene Burkov <E.Burkov@AdGuard.COM>
Date: Thu Sep 28 12:32:21 2023 +0300
dhcpsvc: make it build on 32bit
commit 37a935514b1cebdc817cdcd5ec3562baeafbc42d
Author: Eugene Burkov <E.Burkov@AdGuard.COM>
Date: Wed Sep 27 19:39:35 2023 +0300
dhcpd: fix v6 as well
commit 03b5454b04c4fdb3fe928d661562883dc3e09d81
Author: Eugene Burkov <E.Burkov@AdGuard.COM>
Date: Wed Sep 27 19:34:17 2023 +0300
dhcpsvc: imp code, docs
commit 91a0e451f78fba64578cc541f7ba66579c31d388
Author: Eugene Burkov <E.Burkov@AdGuard.COM>
Date: Fri Sep 22 15:25:58 2023 +0300
dhcpsvc: imp filing
commit 57c91e1194caa00a69e62b6655b1b4e38b69b89f
Author: Eugene Burkov <E.Burkov@AdGuard.COM>
Date: Fri Sep 22 15:23:02 2023 +0300
dhcpsvc: imp code
commit d86be56efbfc121c9fe2c5ecef992b4523e04d57
Author: Eugene Burkov <E.Burkov@AdGuard.COM>
Date: Thu Sep 14 12:24:39 2023 +0300
dhcpsvc: imp code, docs
commit c9ef29057e9e378779d1a7938ad13b6eebda8f50
Author: Eugene Burkov <E.Burkov@AdGuard.COM>
Date: Wed Sep 13 17:53:55 2023 +0300
dhcpsvc: add constructor, validations, tests
commit f2533ed64e4ef439603b9cdf9596f8b0c4a87cf1
Author: Eugene Burkov <E.Burkov@AdGuard.COM>
Date: Tue Sep 12 23:05:42 2023 +0500
WIP
204 lines
4.3 KiB
Go
204 lines
4.3 KiB
Go
package dhcpsvc
|
|
|
|
import (
|
|
"net/netip"
|
|
"strconv"
|
|
"testing"
|
|
|
|
"github.com/AdguardTeam/golibs/testutil"
|
|
"github.com/stretchr/testify/assert"
|
|
"github.com/stretchr/testify/require"
|
|
)
|
|
|
|
func TestNewIPRange(t *testing.T) {
|
|
start4 := netip.MustParseAddr("0.0.0.1")
|
|
end4 := netip.MustParseAddr("0.0.0.3")
|
|
start6 := netip.MustParseAddr("1::1")
|
|
end6 := netip.MustParseAddr("1::3")
|
|
end6Large := netip.MustParseAddr("2::3")
|
|
|
|
testCases := []struct {
|
|
start netip.Addr
|
|
end netip.Addr
|
|
name string
|
|
wantErrMsg string
|
|
}{{
|
|
start: start4,
|
|
end: end4,
|
|
name: "success_ipv4",
|
|
wantErrMsg: "",
|
|
}, {
|
|
start: start6,
|
|
end: end6,
|
|
name: "success_ipv6",
|
|
wantErrMsg: "",
|
|
}, {
|
|
start: end4,
|
|
end: start4,
|
|
name: "start_gt_end",
|
|
wantErrMsg: "invalid ip range: start 0.0.0.3 is greater than or equal to end 0.0.0.1",
|
|
}, {
|
|
start: start4,
|
|
end: start4,
|
|
name: "start_eq_end",
|
|
wantErrMsg: "invalid ip range: start 0.0.0.1 is greater than or equal to end 0.0.0.1",
|
|
}, {
|
|
start: start6,
|
|
end: end6Large,
|
|
name: "too_large",
|
|
wantErrMsg: "invalid ip range: range length must be within " +
|
|
strconv.FormatUint(maxRangeLen, 10),
|
|
}, {
|
|
start: start4,
|
|
end: end6,
|
|
name: "different_family",
|
|
wantErrMsg: "invalid ip range: 0.0.0.1 and 1::3 must be within the same address family",
|
|
}}
|
|
|
|
for _, tc := range testCases {
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
_, err := newIPRange(tc.start, tc.end)
|
|
testutil.AssertErrorMsg(t, tc.wantErrMsg, err)
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestIPRange_Contains(t *testing.T) {
|
|
start, end := netip.MustParseAddr("0.0.0.1"), netip.MustParseAddr("0.0.0.3")
|
|
r, err := newIPRange(start, end)
|
|
require.NoError(t, err)
|
|
|
|
testCases := []struct {
|
|
in netip.Addr
|
|
want assert.BoolAssertionFunc
|
|
name string
|
|
}{{
|
|
in: start,
|
|
want: assert.True,
|
|
name: "start",
|
|
}, {
|
|
in: end,
|
|
want: assert.True,
|
|
name: "end",
|
|
}, {
|
|
in: start.Next(),
|
|
want: assert.True,
|
|
name: "within",
|
|
}, {
|
|
in: netip.MustParseAddr("0.0.0.0"),
|
|
want: assert.False,
|
|
name: "before",
|
|
}, {
|
|
in: netip.MustParseAddr("0.0.0.4"),
|
|
want: assert.False,
|
|
name: "after",
|
|
}, {
|
|
in: netip.MustParseAddr("::"),
|
|
want: assert.False,
|
|
name: "another_family",
|
|
}}
|
|
|
|
for _, tc := range testCases {
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
tc.want(t, r.contains(tc.in))
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestIPRange_Find(t *testing.T) {
|
|
start, end := netip.MustParseAddr("0.0.0.1"), netip.MustParseAddr("0.0.0.5")
|
|
r, err := newIPRange(start, end)
|
|
require.NoError(t, err)
|
|
|
|
num, ok := r.offset(end)
|
|
require.True(t, ok)
|
|
|
|
testCases := []struct {
|
|
predicate ipPredicate
|
|
want netip.Addr
|
|
name string
|
|
}{{
|
|
predicate: func(ip netip.Addr) (ok bool) {
|
|
ipData := ip.AsSlice()
|
|
|
|
return ipData[len(ipData)-1]%2 == 0
|
|
},
|
|
want: netip.MustParseAddr("0.0.0.2"),
|
|
name: "even",
|
|
}, {
|
|
predicate: func(ip netip.Addr) (ok bool) {
|
|
ipData := ip.AsSlice()
|
|
|
|
return ipData[len(ipData)-1]%10 == 0
|
|
},
|
|
want: netip.Addr{},
|
|
name: "none",
|
|
}, {
|
|
predicate: func(ip netip.Addr) (ok bool) {
|
|
return true
|
|
},
|
|
want: start,
|
|
name: "first",
|
|
}, {
|
|
predicate: func(ip netip.Addr) (ok bool) {
|
|
off, _ := r.offset(ip)
|
|
|
|
return off == num
|
|
},
|
|
want: end,
|
|
name: "last",
|
|
}}
|
|
|
|
for _, tc := range testCases {
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
got := r.find(tc.predicate)
|
|
assert.Equal(t, tc.want, got)
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestIPRange_Offset(t *testing.T) {
|
|
start, end := netip.MustParseAddr("0.0.0.1"), netip.MustParseAddr("0.0.0.5")
|
|
r, err := newIPRange(start, end)
|
|
require.NoError(t, err)
|
|
|
|
testCases := []struct {
|
|
in netip.Addr
|
|
name string
|
|
wantOffset uint64
|
|
wantOK bool
|
|
}{{
|
|
in: netip.MustParseAddr("0.0.0.2"),
|
|
name: "in",
|
|
wantOffset: 1,
|
|
wantOK: true,
|
|
}, {
|
|
in: start,
|
|
name: "in_start",
|
|
wantOffset: 0,
|
|
wantOK: true,
|
|
}, {
|
|
in: end,
|
|
name: "in_end",
|
|
wantOffset: 4,
|
|
wantOK: true,
|
|
}, {
|
|
in: netip.MustParseAddr("0.0.0.6"),
|
|
name: "out_after",
|
|
wantOffset: 0,
|
|
wantOK: false,
|
|
}, {
|
|
in: netip.MustParseAddr("0.0.0.0"),
|
|
name: "out_before",
|
|
wantOffset: 0,
|
|
wantOK: false,
|
|
}}
|
|
|
|
for _, tc := range testCases {
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
offset, ok := r.offset(tc.in)
|
|
assert.Equal(t, tc.wantOffset, offset)
|
|
assert.Equal(t, tc.wantOK, ok)
|
|
})
|
|
}
|
|
}
|