2022-09-13 21:45:35 +01:00
|
|
|
//go:build darwin || freebsd || linux || openbsd
|
2021-05-21 12:55:42 +01:00
|
|
|
|
2019-03-01 17:03:22 +00:00
|
|
|
package dhcpd
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net"
|
2022-10-14 13:29:44 +01:00
|
|
|
"net/netip"
|
2023-04-18 13:12:11 +01:00
|
|
|
"path/filepath"
|
2024-02-08 17:39:18 +00:00
|
|
|
"slices"
|
2019-03-01 17:03:22 +00:00
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2023-11-16 11:14:40 +00:00
|
|
|
"github.com/AdguardTeam/AdGuardHome/internal/dhcpsvc"
|
2021-10-19 17:28:18 +01:00
|
|
|
"github.com/AdguardTeam/golibs/testutil"
|
2019-12-03 16:24:30 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
2021-02-12 10:27:44 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
2019-03-01 17:03:22 +00:00
|
|
|
)
|
|
|
|
|
2020-11-16 12:52:05 +00:00
|
|
|
func TestMain(m *testing.M) {
|
2022-10-27 14:11:36 +01:00
|
|
|
testutil.DiscardLogOutput(m)
|
2020-11-16 12:52:05 +00:00
|
|
|
}
|
|
|
|
|
2020-07-03 16:20:01 +01:00
|
|
|
func testNotify(flags uint32) {
|
2019-03-01 17:03:22 +00:00
|
|
|
}
|
2019-03-05 14:15:38 +00:00
|
|
|
|
2021-02-12 10:27:44 +00:00
|
|
|
// Leases database store/load.
|
2019-03-05 14:15:38 +00:00
|
|
|
func TestDB(t *testing.T) {
|
2020-07-03 16:20:01 +01:00
|
|
|
var err error
|
2022-09-13 21:45:35 +01:00
|
|
|
s := server{
|
2022-01-25 16:47:02 +00:00
|
|
|
conf: &ServerConfig{
|
2023-04-18 13:12:11 +01:00
|
|
|
dbFilePath: filepath.Join(t.TempDir(), dataFilename),
|
2021-02-12 10:27:44 +00:00
|
|
|
},
|
|
|
|
}
|
2019-03-05 14:15:38 +00:00
|
|
|
|
2022-09-13 21:45:35 +01:00
|
|
|
s.srv4, err = v4Create(&V4ServerConf{
|
2020-07-03 16:20:01 +01:00
|
|
|
Enabled: true,
|
2022-10-14 13:29:44 +01:00
|
|
|
RangeStart: netip.MustParseAddr("192.168.10.100"),
|
|
|
|
RangeEnd: netip.MustParseAddr("192.168.10.200"),
|
|
|
|
GatewayIP: netip.MustParseAddr("192.168.10.1"),
|
|
|
|
SubnetMask: netip.MustParseAddr("255.255.255.0"),
|
2020-07-03 16:20:01 +01:00
|
|
|
notify: testNotify,
|
2021-02-12 10:27:44 +00:00
|
|
|
})
|
2021-05-06 11:02:48 +01:00
|
|
|
require.NoError(t, err)
|
2019-03-05 14:15:38 +00:00
|
|
|
|
2020-07-03 16:20:01 +01:00
|
|
|
s.srv6, err = v6Create(V6ServerConf{})
|
2021-05-06 11:02:48 +01:00
|
|
|
require.NoError(t, err)
|
2019-03-05 14:15:38 +00:00
|
|
|
|
2023-11-16 11:14:40 +00:00
|
|
|
leases := []*dhcpsvc.Lease{{
|
2021-05-06 11:02:48 +01:00
|
|
|
Expiry: time.Now().Add(time.Hour),
|
|
|
|
Hostname: "static-1.local",
|
|
|
|
HWAddr: net.HardwareAddr{0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA},
|
2023-03-23 13:52:01 +00:00
|
|
|
IP: netip.MustParseAddr("192.168.10.100"),
|
2021-02-12 10:27:44 +00:00
|
|
|
}, {
|
2021-05-06 11:02:48 +01:00
|
|
|
Hostname: "static-2.local",
|
|
|
|
HWAddr: net.HardwareAddr{0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xBB},
|
2023-03-23 13:52:01 +00:00
|
|
|
IP: netip.MustParseAddr("192.168.10.101"),
|
2021-02-12 10:27:44 +00:00
|
|
|
}}
|
2021-02-04 12:12:34 +00:00
|
|
|
|
|
|
|
srv4, ok := s.srv4.(*v4Server)
|
2021-02-12 10:27:44 +00:00
|
|
|
require.True(t, ok)
|
2021-02-04 12:12:34 +00:00
|
|
|
|
2021-06-16 14:48:46 +01:00
|
|
|
err = srv4.addLease(leases[0])
|
2021-05-06 11:02:48 +01:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
err = s.srv4.AddStaticLease(leases[1])
|
|
|
|
require.NoError(t, err)
|
2019-03-05 14:15:38 +00:00
|
|
|
|
2021-06-16 14:48:46 +01:00
|
|
|
err = s.dbStore()
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
err = s.srv4.ResetLeases(nil)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
err = s.dbLoad()
|
|
|
|
require.NoError(t, err)
|
2019-03-05 14:15:38 +00:00
|
|
|
|
2020-07-03 16:20:01 +01:00
|
|
|
ll := s.srv4.GetLeases(LeasesAll)
|
2021-02-12 10:27:44 +00:00
|
|
|
require.Len(t, ll, len(leases))
|
2020-07-03 16:20:01 +01:00
|
|
|
|
2023-04-18 13:12:11 +01:00
|
|
|
assert.Equal(t, leases[0].HWAddr, ll[0].HWAddr)
|
|
|
|
assert.Equal(t, leases[0].IP, ll[0].IP)
|
|
|
|
assert.Equal(t, leases[0].Expiry.Unix(), ll[0].Expiry.Unix())
|
2019-12-03 16:24:30 +00:00
|
|
|
|
2023-04-18 13:12:11 +01:00
|
|
|
assert.Equal(t, leases[1].HWAddr, ll[1].HWAddr)
|
|
|
|
assert.Equal(t, leases[1].IP, ll[1].IP)
|
|
|
|
assert.True(t, ll[1].IsStatic)
|
2019-12-03 16:24:30 +00:00
|
|
|
}
|
2021-09-07 16:33:23 +01:00
|
|
|
|
2021-10-19 17:28:18 +01:00
|
|
|
func TestV4Server_badRange(t *testing.T) {
|
|
|
|
testCases := []struct {
|
|
|
|
name string
|
2022-10-14 13:29:44 +01:00
|
|
|
gatewayIP netip.Addr
|
|
|
|
subnetMask netip.Addr
|
2022-01-25 16:47:02 +00:00
|
|
|
wantErrMsg string
|
2021-10-19 17:28:18 +01:00
|
|
|
}{{
|
2022-10-14 13:29:44 +01:00
|
|
|
name: "gateway_in_range",
|
|
|
|
gatewayIP: netip.MustParseAddr("192.168.10.120"),
|
|
|
|
subnetMask: netip.MustParseAddr("255.255.255.0"),
|
2021-10-19 17:28:18 +01:00
|
|
|
wantErrMsg: "dhcpv4: gateway ip 192.168.10.120 in the ip range: " +
|
|
|
|
"192.168.10.20-192.168.10.200",
|
|
|
|
}, {
|
2022-10-14 13:29:44 +01:00
|
|
|
name: "outside_range_start",
|
|
|
|
gatewayIP: netip.MustParseAddr("192.168.10.1"),
|
|
|
|
subnetMask: netip.MustParseAddr("255.255.255.240"),
|
2021-10-19 17:28:18 +01:00
|
|
|
wantErrMsg: "dhcpv4: range start 192.168.10.20 is outside network " +
|
|
|
|
"192.168.10.1/28",
|
2022-01-25 16:47:02 +00:00
|
|
|
}, {
|
2022-10-14 13:29:44 +01:00
|
|
|
name: "outside_range_end",
|
|
|
|
gatewayIP: netip.MustParseAddr("192.168.10.1"),
|
|
|
|
subnetMask: netip.MustParseAddr("255.255.255.224"),
|
2021-10-19 17:28:18 +01:00
|
|
|
wantErrMsg: "dhcpv4: range end 192.168.10.200 is outside network " +
|
|
|
|
"192.168.10.1/27",
|
|
|
|
}}
|
|
|
|
|
|
|
|
for _, tc := range testCases {
|
|
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
|
|
conf := V4ServerConf{
|
|
|
|
Enabled: true,
|
2022-10-14 13:29:44 +01:00
|
|
|
RangeStart: netip.MustParseAddr("192.168.10.20"),
|
|
|
|
RangeEnd: netip.MustParseAddr("192.168.10.200"),
|
2021-10-19 17:28:18 +01:00
|
|
|
GatewayIP: tc.gatewayIP,
|
|
|
|
SubnetMask: tc.subnetMask,
|
|
|
|
notify: testNotify,
|
|
|
|
}
|
|
|
|
|
2022-09-13 21:45:35 +01:00
|
|
|
_, err := v4Create(&conf)
|
2021-10-19 17:28:18 +01:00
|
|
|
testutil.AssertErrorMsg(t, tc.wantErrMsg, err)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-07 16:33:23 +01:00
|
|
|
// cloneUDPAddr returns a deep copy of a.
|
2021-09-30 16:28:19 +01:00
|
|
|
func cloneUDPAddr(a *net.UDPAddr) (clone *net.UDPAddr) {
|
2021-09-07 16:33:23 +01:00
|
|
|
return &net.UDPAddr{
|
2022-10-21 18:42:00 +01:00
|
|
|
IP: slices.Clone(a.IP),
|
2021-09-07 16:33:23 +01:00
|
|
|
Port: a.Port,
|
|
|
|
Zone: a.Zone,
|
|
|
|
}
|
|
|
|
}
|