badguardhome/internal/dhcpd/iprange_test.go
Ainar Garipov 9736123483 Pull request: dhcpd: fix ip ranges
Updates #2541.

Squashed commit of the following:

commit c81299991876f48836d24872d9145331a0bc9e6e
Author: Ainar Garipov <A.Garipov@AdGuard.COM>
Date:   Tue Mar 16 18:10:07 2021 +0300

    agherr: imp docs

commit f43a5f5cde0ea16dd38dd533e16e415a1d306cb2
Author: Ainar Garipov <A.Garipov@AdGuard.COM>
Date:   Tue Mar 16 17:35:59 2021 +0300

    all: imp err handling, fix code

commit ed26ad0ff53882725f7747264f8094e6fb9b0423
Author: Ainar Garipov <A.Garipov@AdGuard.COM>
Date:   Tue Mar 16 12:24:17 2021 +0300

    dhcpd: fix ip ranges
2021-03-16 19:11:32 +03:00

155 lines
3.2 KiB
Go

package dhcpd
import (
"net"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestNewIPRange(t *testing.T) {
start4 := net.IP{0, 0, 0, 1}
end4 := net.IP{0, 0, 0, 3}
start6 := net.IP{
0x01, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x01,
}
end6 := net.IP{
0x01, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x03,
}
end6Large := net.IP{
0x02, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x03,
}
testCases := []struct {
name string
wantErrMsg string
start net.IP
end net.IP
}{{
name: "success_ipv4",
wantErrMsg: "",
start: start4,
end: end4,
}, {
name: "success_ipv6",
wantErrMsg: "",
start: start6,
end: end6,
}, {
name: "start_gt_end",
wantErrMsg: "invalid ip range: start is greater than or equal to end",
start: end4,
end: start4,
}, {
name: "start_eq_end",
wantErrMsg: "invalid ip range: start is greater than or equal to end",
start: start4,
end: start4,
}, {
name: "too_large",
wantErrMsg: "invalid ip range: range is too large",
start: start6,
end: end6Large,
}}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
r, err := newIPRange(tc.start, tc.end)
if tc.wantErrMsg == "" {
assert.Nil(t, err)
assert.NotNil(t, r)
} else {
require.NotNil(t, err)
assert.Equal(t, tc.wantErrMsg, err.Error())
}
})
}
}
func TestIPRange_Contains(t *testing.T) {
start, end := net.IP{0, 0, 0, 1}, net.IP{0, 0, 0, 3}
r, err := newIPRange(start, end)
require.Nil(t, err)
assert.True(t, r.contains(start))
assert.True(t, r.contains(net.IP{0, 0, 0, 2}))
assert.True(t, r.contains(end))
assert.False(t, r.contains(net.IP{0, 0, 0, 0}))
assert.False(t, r.contains(net.IP{0, 0, 0, 4}))
}
func TestIPRange_Find(t *testing.T) {
start, end := net.IP{0, 0, 0, 1}, net.IP{0, 0, 0, 5}
r, err := newIPRange(start, end)
require.Nil(t, err)
want := net.IPv4(0, 0, 0, 2)
got := r.find(func(ip net.IP) (ok bool) {
return ip[len(ip)-1]%2 == 0
})
assert.Equal(t, want, got)
got = r.find(func(ip net.IP) (ok bool) {
return ip[len(ip)-1]%10 == 0
})
assert.Nil(t, got)
}
func TestIPRange_Offset(t *testing.T) {
start, end := net.IP{0, 0, 0, 1}, net.IP{0, 0, 0, 5}
r, err := newIPRange(start, end)
require.Nil(t, err)
testCases := []struct {
name string
in net.IP
wantOffset uint
wantOK bool
}{{
name: "in",
in: net.IP{0, 0, 0, 2},
wantOffset: 1,
wantOK: true,
}, {
name: "in_start",
in: start,
wantOffset: 0,
wantOK: true,
}, {
name: "in_end",
in: end,
wantOffset: 4,
wantOK: true,
}, {
name: "out_after",
in: net.IP{0, 0, 0, 6},
wantOffset: 0,
wantOK: false,
}, {
name: "out_before",
in: net.IP{0, 0, 0, 0},
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)
})
}
}