2021-05-21 04:55:42 -07:00
|
|
|
//go:build aix || darwin || dragonfly || freebsd || linux || netbsd || openbsd || solaris
|
2021-06-15 09:42:41 -07:00
|
|
|
// +build aix darwin dragonfly freebsd linux netbsd openbsd solaris
|
2021-05-21 04:55:42 -07:00
|
|
|
|
2019-03-01 10:03:22 -07:00
|
|
|
package dhcpd
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net"
|
2019-03-05 07:15:38 -07:00
|
|
|
"os"
|
2019-03-01 10:03:22 -07:00
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2021-02-04 10:35:13 -07:00
|
|
|
"github.com/AdguardTeam/AdGuardHome/internal/aghtest"
|
2021-09-07 08:33:23 -07:00
|
|
|
"github.com/AdguardTeam/golibs/netutil"
|
2021-10-19 09:28:18 -07:00
|
|
|
"github.com/AdguardTeam/golibs/testutil"
|
2019-12-03 09:24:30 -07:00
|
|
|
"github.com/stretchr/testify/assert"
|
2021-02-12 03:27:44 -07:00
|
|
|
"github.com/stretchr/testify/require"
|
2019-03-01 10:03:22 -07:00
|
|
|
)
|
|
|
|
|
2020-11-16 05:52:05 -07:00
|
|
|
func TestMain(m *testing.M) {
|
2021-02-04 10:35:13 -07:00
|
|
|
aghtest.DiscardLogOutput(m)
|
2020-11-16 05:52:05 -07:00
|
|
|
}
|
|
|
|
|
2020-07-03 08:20:01 -07:00
|
|
|
func testNotify(flags uint32) {
|
2019-03-01 10:03:22 -07:00
|
|
|
}
|
2019-03-05 07:15:38 -07:00
|
|
|
|
2021-02-12 03:27:44 -07:00
|
|
|
// Leases database store/load.
|
2019-03-05 07:15:38 -07:00
|
|
|
func TestDB(t *testing.T) {
|
2020-07-03 08:20:01 -07:00
|
|
|
var err error
|
2021-02-12 03:27:44 -07:00
|
|
|
s := Server{
|
2022-01-25 09:47:02 -07:00
|
|
|
conf: &ServerConfig{
|
2021-02-12 03:27:44 -07:00
|
|
|
DBFilePath: dbFilename,
|
|
|
|
},
|
|
|
|
}
|
2019-03-05 07:15:38 -07:00
|
|
|
|
2021-02-12 03:27:44 -07:00
|
|
|
s.srv4, err = v4Create(V4ServerConf{
|
2020-07-03 08:20:01 -07:00
|
|
|
Enabled: true,
|
2021-01-13 06:56:05 -07:00
|
|
|
RangeStart: net.IP{192, 168, 10, 100},
|
|
|
|
RangeEnd: net.IP{192, 168, 10, 200},
|
|
|
|
GatewayIP: net.IP{192, 168, 10, 1},
|
|
|
|
SubnetMask: net.IP{255, 255, 255, 0},
|
2020-07-03 08:20:01 -07:00
|
|
|
notify: testNotify,
|
2021-02-12 03:27:44 -07:00
|
|
|
})
|
2021-05-06 03:02:48 -07:00
|
|
|
require.NoError(t, err)
|
2019-03-05 07:15:38 -07:00
|
|
|
|
2020-07-03 08:20:01 -07:00
|
|
|
s.srv6, err = v6Create(V6ServerConf{})
|
2021-05-06 03:02:48 -07:00
|
|
|
require.NoError(t, err)
|
2019-03-05 07:15:38 -07:00
|
|
|
|
2021-06-16 06:48:46 -07:00
|
|
|
leases := []*Lease{{
|
2021-05-06 03:02:48 -07:00
|
|
|
Expiry: time.Now().Add(time.Hour),
|
|
|
|
Hostname: "static-1.local",
|
|
|
|
HWAddr: net.HardwareAddr{0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA},
|
|
|
|
IP: net.IP{192, 168, 10, 100},
|
2021-02-12 03:27:44 -07:00
|
|
|
}, {
|
2021-05-06 03:02:48 -07:00
|
|
|
Hostname: "static-2.local",
|
|
|
|
HWAddr: net.HardwareAddr{0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xBB},
|
|
|
|
IP: net.IP{192, 168, 10, 101},
|
2021-02-12 03:27:44 -07:00
|
|
|
}}
|
2021-02-04 05:12:34 -07:00
|
|
|
|
|
|
|
srv4, ok := s.srv4.(*v4Server)
|
2021-02-12 03:27:44 -07:00
|
|
|
require.True(t, ok)
|
2021-02-04 05:12:34 -07:00
|
|
|
|
2021-06-16 06:48:46 -07:00
|
|
|
err = srv4.addLease(leases[0])
|
2021-05-06 03:02:48 -07:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
err = s.srv4.AddStaticLease(leases[1])
|
|
|
|
require.NoError(t, err)
|
2019-03-05 07:15:38 -07:00
|
|
|
|
2021-06-16 06:48:46 -07:00
|
|
|
err = s.dbStore()
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
2021-10-22 01:58:18 -07:00
|
|
|
testutil.CleanupAndRequireSuccess(t, func() (err error) { return os.Remove(dbFilename) })
|
2021-06-16 06:48:46 -07:00
|
|
|
|
|
|
|
err = s.srv4.ResetLeases(nil)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
err = s.dbLoad()
|
|
|
|
require.NoError(t, err)
|
2019-03-05 07:15:38 -07:00
|
|
|
|
2020-07-03 08:20:01 -07:00
|
|
|
ll := s.srv4.GetLeases(LeasesAll)
|
2021-02-12 03:27:44 -07:00
|
|
|
require.Len(t, ll, len(leases))
|
2020-07-03 08:20:01 -07:00
|
|
|
|
2021-02-12 03:27:44 -07:00
|
|
|
assert.Equal(t, leases[1].HWAddr, ll[0].HWAddr)
|
|
|
|
assert.Equal(t, leases[1].IP, ll[0].IP)
|
2021-03-18 07:07:13 -07:00
|
|
|
assert.True(t, ll[0].IsStatic())
|
2020-07-03 08:20:01 -07:00
|
|
|
|
2021-02-12 03:27:44 -07:00
|
|
|
assert.Equal(t, leases[0].HWAddr, ll[1].HWAddr)
|
|
|
|
assert.Equal(t, leases[0].IP, ll[1].IP)
|
|
|
|
assert.Equal(t, leases[0].Expiry.Unix(), ll[1].Expiry.Unix())
|
2019-03-05 07:15:38 -07:00
|
|
|
}
|
2019-07-17 01:55:21 -07:00
|
|
|
|
|
|
|
func TestIsValidSubnetMask(t *testing.T) {
|
2021-02-12 03:27:44 -07:00
|
|
|
testCases := []struct {
|
|
|
|
mask net.IP
|
|
|
|
want bool
|
|
|
|
}{{
|
|
|
|
mask: net.IP{255, 255, 255, 0},
|
|
|
|
want: true,
|
|
|
|
}, {
|
|
|
|
mask: net.IP{255, 255, 254, 0},
|
|
|
|
want: true,
|
|
|
|
}, {
|
|
|
|
mask: net.IP{255, 255, 252, 0},
|
|
|
|
want: true,
|
|
|
|
}, {
|
|
|
|
mask: net.IP{255, 255, 253, 0},
|
|
|
|
}, {
|
|
|
|
mask: net.IP{255, 255, 255, 1},
|
|
|
|
}}
|
|
|
|
|
|
|
|
for _, tc := range testCases {
|
|
|
|
t.Run(tc.mask.String(), func(t *testing.T) {
|
|
|
|
assert.Equal(t, tc.want, isValidSubnetMask(tc.mask))
|
|
|
|
})
|
|
|
|
}
|
2019-07-17 01:55:21 -07:00
|
|
|
}
|
2019-12-03 09:24:30 -07:00
|
|
|
|
|
|
|
func TestNormalizeLeases(t *testing.T) {
|
2021-02-12 03:27:44 -07:00
|
|
|
dynLeases := []*Lease{{
|
|
|
|
HWAddr: net.HardwareAddr{1, 2, 3, 4},
|
|
|
|
}, {
|
|
|
|
HWAddr: net.HardwareAddr{1, 2, 3, 5},
|
|
|
|
}}
|
|
|
|
|
|
|
|
staticLeases := []*Lease{{
|
|
|
|
HWAddr: net.HardwareAddr{1, 2, 3, 4},
|
|
|
|
IP: net.IP{0, 2, 3, 4},
|
|
|
|
}, {
|
|
|
|
HWAddr: net.HardwareAddr{2, 2, 3, 4},
|
|
|
|
}}
|
2019-12-03 09:24:30 -07:00
|
|
|
|
2020-10-30 09:18:51 -07:00
|
|
|
leases := normalizeLeases(staticLeases, dynLeases)
|
2021-02-12 03:27:44 -07:00
|
|
|
require.Len(t, leases, 3)
|
2019-12-03 09:24:30 -07:00
|
|
|
|
2021-02-12 03:27:44 -07:00
|
|
|
assert.Equal(t, leases[0].HWAddr, dynLeases[0].HWAddr)
|
|
|
|
assert.Equal(t, leases[0].IP, staticLeases[0].IP)
|
|
|
|
assert.Equal(t, leases[1].HWAddr, staticLeases[1].HWAddr)
|
|
|
|
assert.Equal(t, leases[2].HWAddr, dynLeases[1].HWAddr)
|
2019-12-03 09:24:30 -07:00
|
|
|
}
|
2021-09-07 08:33:23 -07:00
|
|
|
|
2021-10-19 09:28:18 -07:00
|
|
|
func TestV4Server_badRange(t *testing.T) {
|
|
|
|
testCases := []struct {
|
|
|
|
name string
|
2022-01-25 09:47:02 -07:00
|
|
|
wantErrMsg string
|
2021-10-19 09:28:18 -07:00
|
|
|
gatewayIP net.IP
|
|
|
|
subnetMask net.IP
|
|
|
|
}{{
|
2022-01-25 09:47:02 -07:00
|
|
|
name: "gateway_in_range",
|
2021-10-19 09:28:18 -07:00
|
|
|
wantErrMsg: "dhcpv4: gateway ip 192.168.10.120 in the ip range: " +
|
|
|
|
"192.168.10.20-192.168.10.200",
|
2022-01-25 09:47:02 -07:00
|
|
|
gatewayIP: net.IP{192, 168, 10, 120},
|
|
|
|
subnetMask: net.IP{255, 255, 255, 0},
|
2021-10-19 09:28:18 -07:00
|
|
|
}, {
|
2022-01-25 09:47:02 -07:00
|
|
|
name: "outside_range_start",
|
2021-10-19 09:28:18 -07:00
|
|
|
wantErrMsg: "dhcpv4: range start 192.168.10.20 is outside network " +
|
|
|
|
"192.168.10.1/28",
|
|
|
|
gatewayIP: net.IP{192, 168, 10, 1},
|
2022-01-25 09:47:02 -07:00
|
|
|
subnetMask: net.IP{255, 255, 255, 240},
|
|
|
|
}, {
|
|
|
|
name: "outside_range_end",
|
2021-10-19 09:28:18 -07:00
|
|
|
wantErrMsg: "dhcpv4: range end 192.168.10.200 is outside network " +
|
|
|
|
"192.168.10.1/27",
|
2022-01-25 09:47:02 -07:00
|
|
|
gatewayIP: net.IP{192, 168, 10, 1},
|
|
|
|
subnetMask: net.IP{255, 255, 255, 224},
|
2021-10-19 09:28:18 -07:00
|
|
|
}}
|
|
|
|
|
|
|
|
for _, tc := range testCases {
|
|
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
|
|
conf := V4ServerConf{
|
|
|
|
Enabled: true,
|
|
|
|
RangeStart: net.IP{192, 168, 10, 20},
|
|
|
|
RangeEnd: net.IP{192, 168, 10, 200},
|
|
|
|
GatewayIP: tc.gatewayIP,
|
|
|
|
SubnetMask: tc.subnetMask,
|
|
|
|
notify: testNotify,
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err := v4Create(conf)
|
|
|
|
testutil.AssertErrorMsg(t, tc.wantErrMsg, err)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-07 08:33:23 -07:00
|
|
|
// cloneUDPAddr returns a deep copy of a.
|
2021-09-30 08:28:19 -07:00
|
|
|
func cloneUDPAddr(a *net.UDPAddr) (clone *net.UDPAddr) {
|
2021-09-07 08:33:23 -07:00
|
|
|
return &net.UDPAddr{
|
|
|
|
IP: netutil.CloneIP(a.IP),
|
|
|
|
Port: a.Port,
|
|
|
|
Zone: a.Zone,
|
|
|
|
}
|
|
|
|
}
|