mirror of
https://github.com/AdguardTeam/AdGuardHome.git
synced 2024-11-28 09:58:52 +03:00
fffa656758
Merge in DNS/adguard-home from 4722-dhcp-http-panic to master Updates #4722. Squashed commit of the following: commit 8a8db48c3bd4f6bb7fabe65b5b7b162f0986fc76 Merge: 39b344f9b74b92fc
Author: Eugene Burkov <E.Burkov@AdGuard.COM> Date: Tue Sep 13 20:11:23 2022 +0300 Merge branch 'master' into 4722-dhcp-http-panic commit 39b344f97180af17ab22041e5655a27bcc99c29e Author: Eugene Burkov <E.Burkov@AdGuard.COM> Date: Tue Sep 13 18:33:56 2022 +0300 dhcpd: imp code, fmt commit a36d70d2c25791b2e657e21d6f4681b33497f0cd Author: Eugene Burkov <E.Burkov@AdGuard.COM> Date: Tue Sep 13 17:38:17 2022 +0300 dhcpd: imp names, docs commit 600d63da7af62de5cb52fc7670ef28c9f4fe95a7 Author: Eugene Burkov <E.Burkov@AdGuard.COM> Date: Tue Sep 13 17:36:17 2022 +0300 dhcpd: rename files, imp tags commit 44f5507649db8536a07c4c21c8ad6e4a60ba3f43 Author: Eugene Burkov <E.Burkov@AdGuard.COM> Date: Tue Sep 13 16:40:26 2022 +0300 dhcpd: add mock commit cfc3cfb714705067d3aa71a7cb5df4245e091cfd Author: Eugene Burkov <E.Burkov@AdGuard.COM> Date: Tue Sep 13 16:15:27 2022 +0300 all: use ptr instead of value commit ec526c2cf22df3470641296cfc402113c23c3f9b Author: Eugene Burkov <E.Burkov@AdGuard.COM> Date: Tue Sep 13 14:57:10 2022 +0300 all: log changes commit 0eca09f4c72bbdc73a2334c839d7781847ba3962 Author: Eugene Burkov <E.Burkov@AdGuard.COM> Date: Tue Sep 13 14:50:32 2022 +0300 dhcpd: let v4 be unconfigured commit 59636e9ff48aea989d7bdfd216b37899b57137d2 Merge: 9238ca0abc1503af
Author: Eugene Burkov <E.Burkov@AdGuard.COM> Date: Tue Sep 13 14:50:17 2022 +0300 Merge branch 'master' into 4722-dhcp-http-panic commit 9238ca0a1e190ddc344f01959f474932809f086a Author: Eugene Burkov <E.Burkov@AdGuard.COM> Date: Wed Sep 7 18:28:56 2022 +0300 dhcpd: imp conf commit 5f801c9be96c2fa735a50373495d8c6ca2914f32 Author: Eugene Burkov <E.Burkov@AdGuard.COM> Date: Tue Sep 6 16:31:13 2022 +0300 dhcpd: hide behind iface commit a95c2741a7e3e5bfe8775bf937a3709217b76da0 Author: Eugene Burkov <E.Burkov@AdGuard.COM> Date: Wed Aug 31 16:24:02 2022 +0300 dhcpd: separate os files
163 lines
3.9 KiB
Go
163 lines
3.9 KiB
Go
//go:build darwin || freebsd || linux || openbsd
|
|
|
|
package dhcpd
|
|
|
|
import (
|
|
"net"
|
|
"os"
|
|
"testing"
|
|
"time"
|
|
|
|
"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 testNotify(flags uint32) {
|
|
}
|
|
|
|
// Leases database store/load.
|
|
func TestDB(t *testing.T) {
|
|
var err error
|
|
s := server{
|
|
conf: &ServerConfig{
|
|
DBFilePath: dbFilename,
|
|
},
|
|
}
|
|
|
|
s.srv4, err = v4Create(&V4ServerConf{
|
|
Enabled: true,
|
|
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},
|
|
notify: testNotify,
|
|
})
|
|
require.NoError(t, err)
|
|
|
|
s.srv6, err = v6Create(V6ServerConf{})
|
|
require.NoError(t, err)
|
|
|
|
leases := []*Lease{{
|
|
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},
|
|
}, {
|
|
Hostname: "static-2.local",
|
|
HWAddr: net.HardwareAddr{0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xBB},
|
|
IP: net.IP{192, 168, 10, 101},
|
|
}}
|
|
|
|
srv4, ok := s.srv4.(*v4Server)
|
|
require.True(t, ok)
|
|
|
|
err = srv4.addLease(leases[0])
|
|
require.NoError(t, err)
|
|
|
|
err = s.srv4.AddStaticLease(leases[1])
|
|
require.NoError(t, err)
|
|
|
|
err = s.dbStore()
|
|
require.NoError(t, err)
|
|
|
|
testutil.CleanupAndRequireSuccess(t, func() (err error) { return os.Remove(dbFilename) })
|
|
|
|
err = s.srv4.ResetLeases(nil)
|
|
require.NoError(t, err)
|
|
|
|
err = s.dbLoad()
|
|
require.NoError(t, err)
|
|
|
|
ll := s.srv4.GetLeases(LeasesAll)
|
|
require.Len(t, ll, len(leases))
|
|
|
|
assert.Equal(t, leases[1].HWAddr, ll[0].HWAddr)
|
|
assert.Equal(t, leases[1].IP, ll[0].IP)
|
|
assert.True(t, ll[0].IsStatic())
|
|
|
|
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())
|
|
}
|
|
|
|
func TestNormalizeLeases(t *testing.T) {
|
|
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},
|
|
}}
|
|
|
|
leases := normalizeLeases(staticLeases, dynLeases)
|
|
require.Len(t, leases, 3)
|
|
|
|
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)
|
|
}
|
|
|
|
func TestV4Server_badRange(t *testing.T) {
|
|
testCases := []struct {
|
|
name string
|
|
wantErrMsg string
|
|
gatewayIP net.IP
|
|
subnetMask net.IP
|
|
}{{
|
|
name: "gateway_in_range",
|
|
wantErrMsg: "dhcpv4: gateway ip 192.168.10.120 in the ip range: " +
|
|
"192.168.10.20-192.168.10.200",
|
|
gatewayIP: net.IP{192, 168, 10, 120},
|
|
subnetMask: net.IP{255, 255, 255, 0},
|
|
}, {
|
|
name: "outside_range_start",
|
|
wantErrMsg: "dhcpv4: range start 192.168.10.20 is outside network " +
|
|
"192.168.10.1/28",
|
|
gatewayIP: net.IP{192, 168, 10, 1},
|
|
subnetMask: net.IP{255, 255, 255, 240},
|
|
}, {
|
|
name: "outside_range_end",
|
|
wantErrMsg: "dhcpv4: range end 192.168.10.200 is outside network " +
|
|
"192.168.10.1/27",
|
|
gatewayIP: net.IP{192, 168, 10, 1},
|
|
subnetMask: net.IP{255, 255, 255, 224},
|
|
}}
|
|
|
|
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)
|
|
})
|
|
}
|
|
}
|
|
|
|
// cloneUDPAddr returns a deep copy of a.
|
|
func cloneUDPAddr(a *net.UDPAddr) (clone *net.UDPAddr) {
|
|
return &net.UDPAddr{
|
|
IP: netutil.CloneIP(a.IP),
|
|
Port: a.Port,
|
|
Zone: a.Zone,
|
|
}
|
|
}
|