badguardhome/internal/dhcpd/dhcpd_test.go

191 lines
4.5 KiB
Go
Raw Normal View History

//go:build aix || darwin || dragonfly || freebsd || linux || netbsd || openbsd || solaris
// +build aix darwin dragonfly freebsd linux netbsd openbsd solaris
2019-03-01 17:03:22 +00:00
package dhcpd
import (
"net"
"os"
2019-03-01 17:03:22 +00:00
"testing"
"time"
"github.com/AdguardTeam/AdGuardHome/internal/aghtest"
"github.com/AdguardTeam/golibs/netutil"
Pull request #1329: 3529 validate dhcpv4 Merge in DNS/adguard-home from 3529-validate-dhcpv4 to master Squashed commit of the following: commit 2f2455aa13a41398cd2846f31be96da9d34ba95d Author: Dmitriy Seregin <d.seregin@adguard.com> Date: Tue Oct 19 19:18:12 2021 +0300 dhcpv4: better test && fix changelog commit ec4ff9180e8390fb739b3be0fc76fd2c715fe691 Author: Dmitriy Seregin <d.seregin@adguard.com> Date: Mon Oct 18 19:08:44 2021 +0300 dhcpv4: better tests commit e0e2f27b7a063ed84af170b16c3f87636cb738d2 Author: Dmitriy Seregin <d.seregin@adguard.com> Date: Mon Oct 18 18:55:47 2021 +0300 dhcpv4: better tests commit 73e1d08e1265e336ee6339d5021f90883fe3e395 Author: Dmitriy Seregin <d.seregin@adguard.com> Date: Mon Oct 18 18:47:21 2021 +0300 dhcpv4: better tests commit f636fc316123f26b6e2930afb4b22c18024ec93d Author: Dmitriy Seregin <d.seregin@adguard.com> Date: Mon Oct 18 18:47:07 2021 +0300 all: updated golibs commit 86dd107a1d483ac24bd8c26422324eb8b9c3d086 Merge: 51aaf6d9 b296fa22 Author: Dmitriy Seregin <d.seregin@adguard.com> Date: Mon Oct 18 17:18:17 2021 +0300 Merge branch 'master' into 3529-validate-dhcpv4 commit 51aaf6d9eb5fbe2b4304254dc6782305a19c53fa Author: Dmitriy Seregin <d.seregin@adguard.com> Date: Mon Oct 18 17:18:02 2021 +0300 dhcpv4: better changelog commit 720b896bb595c57fab6d376f88c8a4b1d131db40 Author: Dmitriy Seregin <d.seregin@adguard.com> Date: Mon Oct 18 17:14:25 2021 +0300 dhcpv4: better tests commit 1098beffca8d5feb2ec104d26419210962c9a97d Author: Dmitriy Seregin <d.seregin@adguard.com> Date: Mon Oct 18 12:08:26 2021 +0300 dhcp: changelog commit d1f6c89d68657431fb261658133c67e9e3135c1c Author: Dmitriy Seregin <d.seregin@adguard.com> Date: Mon Oct 18 12:03:06 2021 +0300 dhcpv4: fixed tests commit 8b6713468fc04321c5238300df90bbb2d67ee679 Merge: 9991e9cb 3fa38fb4 Author: Dmitriy Seregin <d.seregin@adguard.com> Date: Mon Oct 18 11:57:57 2021 +0300 Merge branch 'master' into 3529-validate-dhcpv4 commit 9991e9cbee7dc87d8fa1d7e86e6cc7e09ab6938c Author: Dmitriy Seregin <d.seregin@adguard.com> Date: Mon Oct 18 11:55:40 2021 +0300 dhcpv4: added tests commit 5798a80de6c060365c1c647326d46cc13ccf28cb Author: Dmitriy Seregin <d.seregin@adguard.com> Date: Mon Oct 18 11:46:03 2021 +0300 dhcpv4: validate subnet mask and ip range
2021-10-19 16:28:18 +00:00
"github.com/AdguardTeam/golibs/testutil"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
2019-03-01 17:03:22 +00:00
)
Pull request: 2273 clean tests output Merge in DNS/adguard-home from 2273-clean-tests-output to master Closes #2273. Squashed commit of the following: commit 7571a33fc1f76300bd256578b3afa95338e399c4 Merge: f17df0f9c a19523b25 Author: Eugene Burkov <e.burkov@adguard.com> Date: Mon Nov 16 15:45:30 2020 +0300 Merge branch 'master' into 2273-clean-tests-output commit f17df0f9ce2a3ed25db33fbc6a2e7cabd33f657b Author: Eugene Burkov <e.burkov@adguard.com> Date: Mon Nov 16 15:35:42 2020 +0300 home: move build constraint on top line commit 3717c8ef5a51f9dcaa7e524bfa7b0f1d90bec93d Author: Eugene Burkov <e.burkov@adguard.com> Date: Mon Nov 16 15:24:50 2020 +0300 all: add improvements to changelog commit de6d5afe39d74a3c3d3e0bbe6d0e09aea0214d56 Merge: 43d4f7acf 394fc5a9d Author: Eugene Burkov <e.burkov@adguard.com> Date: Mon Nov 16 15:04:38 2020 +0300 Merge branch 'master' into 2273-clean-tests-output commit 43d4f7acf188e810aa7277cb6479110c9842e8be Author: Eugene Burkov <e.burkov@adguard.com> Date: Mon Nov 16 13:38:13 2020 +0300 dnsfilter: remove redundant test logging commit 7194c1413006b8f52fc454e89ab052bf52e4e517 Author: Eugene Burkov <e.burkov@adguard.com> Date: Mon Nov 16 12:19:14 2020 +0300 testutil: improve comments commit 9f17ab08e287fa69ce30d9e7eec8ea8880f87716 Author: Eugene Burkov <e.burkov@adguard.com> Date: Sat Nov 14 01:22:08 2020 +0300 nclient4: fix wrong function name commit f355749149b2a4485792ba2bdcbc0bb4b629d726 Author: Eugene Burkov <e.burkov@adguard.com> Date: Sat Nov 14 01:07:22 2020 +0300 testutil: fix comments and naming commit f8c50a260bfae08d594a7f37d603941d3680a45e Author: Eugene Burkov <e.burkov@adguard.com> Date: Fri Nov 13 14:09:50 2020 +0300 testutil: create a package and include it commit f169cdc4084b719de65aa0cdc65200b48785322e Author: Eugene Burkov <e.burkov@adguard.com> Date: Thu Nov 12 20:15:58 2020 +0300 agherr: discard loggers output commit 360e736b5a2a30f2c5350448234f14b841e3ea27 Author: Eugene Burkov <e.burkov@adguard.com> Date: Thu Nov 12 20:09:55 2020 +0300 all: replace default log writer with ioutil.Discard Closes #2273.
2020-11-16 12:52:05 +00:00
func TestMain(m *testing.M) {
aghtest.DiscardLogOutput(m)
Pull request: 2273 clean tests output Merge in DNS/adguard-home from 2273-clean-tests-output to master Closes #2273. Squashed commit of the following: commit 7571a33fc1f76300bd256578b3afa95338e399c4 Merge: f17df0f9c a19523b25 Author: Eugene Burkov <e.burkov@adguard.com> Date: Mon Nov 16 15:45:30 2020 +0300 Merge branch 'master' into 2273-clean-tests-output commit f17df0f9ce2a3ed25db33fbc6a2e7cabd33f657b Author: Eugene Burkov <e.burkov@adguard.com> Date: Mon Nov 16 15:35:42 2020 +0300 home: move build constraint on top line commit 3717c8ef5a51f9dcaa7e524bfa7b0f1d90bec93d Author: Eugene Burkov <e.burkov@adguard.com> Date: Mon Nov 16 15:24:50 2020 +0300 all: add improvements to changelog commit de6d5afe39d74a3c3d3e0bbe6d0e09aea0214d56 Merge: 43d4f7acf 394fc5a9d Author: Eugene Burkov <e.burkov@adguard.com> Date: Mon Nov 16 15:04:38 2020 +0300 Merge branch 'master' into 2273-clean-tests-output commit 43d4f7acf188e810aa7277cb6479110c9842e8be Author: Eugene Burkov <e.burkov@adguard.com> Date: Mon Nov 16 13:38:13 2020 +0300 dnsfilter: remove redundant test logging commit 7194c1413006b8f52fc454e89ab052bf52e4e517 Author: Eugene Burkov <e.burkov@adguard.com> Date: Mon Nov 16 12:19:14 2020 +0300 testutil: improve comments commit 9f17ab08e287fa69ce30d9e7eec8ea8880f87716 Author: Eugene Burkov <e.burkov@adguard.com> Date: Sat Nov 14 01:22:08 2020 +0300 nclient4: fix wrong function name commit f355749149b2a4485792ba2bdcbc0bb4b629d726 Author: Eugene Burkov <e.burkov@adguard.com> Date: Sat Nov 14 01:07:22 2020 +0300 testutil: fix comments and naming commit f8c50a260bfae08d594a7f37d603941d3680a45e Author: Eugene Burkov <e.burkov@adguard.com> Date: Fri Nov 13 14:09:50 2020 +0300 testutil: create a package and include it commit f169cdc4084b719de65aa0cdc65200b48785322e Author: Eugene Burkov <e.burkov@adguard.com> Date: Thu Nov 12 20:15:58 2020 +0300 agherr: discard loggers output commit 360e736b5a2a30f2c5350448234f14b841e3ea27 Author: Eugene Burkov <e.burkov@adguard.com> Date: Thu Nov 12 20:09:55 2020 +0300 all: replace default log writer with ioutil.Discard Closes #2273.
2020-11-16 12:52:05 +00:00
}
func testNotify(flags uint32) {
2019-03-01 17:03:22 +00:00
}
// 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,
Pull request: 2508 ip conversion vol.1 Merge in DNS/adguard-home from 2508-ip-conversion to master Updates #2508. Squashed commit of the following: commit 3f64709fbc73ef74c11b910997be1e9bc337193c Merge: 5ac7faaaa 0d67aa251 Author: Eugene Burkov <e.burkov@adguard.com> Date: Wed Jan 13 16:21:34 2021 +0300 Merge branch 'master' into 2508-ip-conversion commit 5ac7faaaa9dda570fdb872acad5d13d078f46b64 Author: Eugene Burkov <e.burkov@adguard.com> Date: Wed Jan 13 12:00:11 2021 +0300 all: replace conditions with appropriate functions in tests commit 9e3fa9a115ed23024c57dd5192d5173477ddbf71 Merge: db992a42a bba74859e Author: Eugene Burkov <e.burkov@adguard.com> Date: Wed Jan 13 10:47:10 2021 +0300 Merge branch 'master' into 2508-ip-conversion commit db992a42a2c6f315421e78a6a0492e2bfb3ce89d Author: Eugene Burkov <e.burkov@adguard.com> Date: Tue Jan 12 18:55:53 2021 +0300 sysutil: fix linux tests commit f629b15d62349323ce2da05e68dc9cc0b5f6e194 Author: Eugene Burkov <e.burkov@adguard.com> Date: Tue Jan 12 18:41:20 2021 +0300 all: improve code quality commit 3bf03a75524040738562298bd1de6db536af130f Author: Eugene Burkov <e.burkov@adguard.com> Date: Tue Jan 12 17:33:26 2021 +0300 sysutil: fix linux net.IP conversion commit 5d5b6994916923636e635588631b63b7e7b74e5f Author: Eugene Burkov <e.burkov@adguard.com> Date: Tue Jan 12 14:57:26 2021 +0300 dnsforward: remove redundant net.IP <-> string conversion commit 0b955d99b7fad40942f21d1dd8734adb99126195 Author: Eugene Burkov <e.burkov@adguard.com> Date: Mon Jan 11 18:04:25 2021 +0300 dhcpd: remove net.IP <-> string conversion
2021-01-13 13:56:05 +00: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},
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 TestIsValidSubnetMask(t *testing.T) {
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))
})
}
}
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)
}
Pull request #1329: 3529 validate dhcpv4 Merge in DNS/adguard-home from 3529-validate-dhcpv4 to master Squashed commit of the following: commit 2f2455aa13a41398cd2846f31be96da9d34ba95d Author: Dmitriy Seregin <d.seregin@adguard.com> Date: Tue Oct 19 19:18:12 2021 +0300 dhcpv4: better test && fix changelog commit ec4ff9180e8390fb739b3be0fc76fd2c715fe691 Author: Dmitriy Seregin <d.seregin@adguard.com> Date: Mon Oct 18 19:08:44 2021 +0300 dhcpv4: better tests commit e0e2f27b7a063ed84af170b16c3f87636cb738d2 Author: Dmitriy Seregin <d.seregin@adguard.com> Date: Mon Oct 18 18:55:47 2021 +0300 dhcpv4: better tests commit 73e1d08e1265e336ee6339d5021f90883fe3e395 Author: Dmitriy Seregin <d.seregin@adguard.com> Date: Mon Oct 18 18:47:21 2021 +0300 dhcpv4: better tests commit f636fc316123f26b6e2930afb4b22c18024ec93d Author: Dmitriy Seregin <d.seregin@adguard.com> Date: Mon Oct 18 18:47:07 2021 +0300 all: updated golibs commit 86dd107a1d483ac24bd8c26422324eb8b9c3d086 Merge: 51aaf6d9 b296fa22 Author: Dmitriy Seregin <d.seregin@adguard.com> Date: Mon Oct 18 17:18:17 2021 +0300 Merge branch 'master' into 3529-validate-dhcpv4 commit 51aaf6d9eb5fbe2b4304254dc6782305a19c53fa Author: Dmitriy Seregin <d.seregin@adguard.com> Date: Mon Oct 18 17:18:02 2021 +0300 dhcpv4: better changelog commit 720b896bb595c57fab6d376f88c8a4b1d131db40 Author: Dmitriy Seregin <d.seregin@adguard.com> Date: Mon Oct 18 17:14:25 2021 +0300 dhcpv4: better tests commit 1098beffca8d5feb2ec104d26419210962c9a97d Author: Dmitriy Seregin <d.seregin@adguard.com> Date: Mon Oct 18 12:08:26 2021 +0300 dhcp: changelog commit d1f6c89d68657431fb261658133c67e9e3135c1c Author: Dmitriy Seregin <d.seregin@adguard.com> Date: Mon Oct 18 12:03:06 2021 +0300 dhcpv4: fixed tests commit 8b6713468fc04321c5238300df90bbb2d67ee679 Merge: 9991e9cb 3fa38fb4 Author: Dmitriy Seregin <d.seregin@adguard.com> Date: Mon Oct 18 11:57:57 2021 +0300 Merge branch 'master' into 3529-validate-dhcpv4 commit 9991e9cbee7dc87d8fa1d7e86e6cc7e09ab6938c Author: Dmitriy Seregin <d.seregin@adguard.com> Date: Mon Oct 18 11:55:40 2021 +0300 dhcpv4: added tests commit 5798a80de6c060365c1c647326d46cc13ccf28cb Author: Dmitriy Seregin <d.seregin@adguard.com> Date: Mon Oct 18 11:46:03 2021 +0300 dhcpv4: validate subnet mask and ip range
2021-10-19 16:28:18 +00:00
func TestV4Server_badRange(t *testing.T) {
testCases := []struct {
name string
gatewayIP net.IP
subnetMask net.IP
wantErrMsg string
}{{
name: "gateway_in_range",
gatewayIP: net.IP{192, 168, 10, 120},
subnetMask: net.IP{255, 255, 255, 0},
wantErrMsg: "dhcpv4: gateway ip 192.168.10.120 in the ip range: " +
"192.168.10.20-192.168.10.200",
}, {
name: "outside_range_start",
gatewayIP: net.IP{192, 168, 10, 1},
subnetMask: net.IP{255, 255, 255, 240},
wantErrMsg: "dhcpv4: range start 192.168.10.20 is outside network " +
"192.168.10.1/28",
}, {
name: "outside_range_end",
gatewayIP: net.IP{192, 168, 10, 1},
subnetMask: net.IP{255, 255, 255, 224},
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,
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,
}
}