2020-07-03 15:20:01 +00:00
|
|
|
// +build aix darwin dragonfly freebsd linux netbsd openbsd solaris
|
|
|
|
|
2019-03-01 17:03:22 +00:00
|
|
|
package dhcpd
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net"
|
2019-03-05 14:15:38 +00:00
|
|
|
"os"
|
2019-03-01 17:03:22 +00:00
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2021-02-04 17:35:13 +00:00
|
|
|
"github.com/AdguardTeam/AdGuardHome/internal/aghtest"
|
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) {
|
2021-02-04 17:35:13 +00:00
|
|
|
aghtest.DiscardLogOutput(m)
|
2020-11-16 12:52:05 +00:00
|
|
|
}
|
|
|
|
|
2020-07-03 15:20:01 +00: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 15:20:01 +00:00
|
|
|
var err error
|
2021-02-12 10:27:44 +00:00
|
|
|
s := Server{
|
|
|
|
conf: ServerConfig{
|
|
|
|
DBFilePath: dbFilename,
|
|
|
|
},
|
|
|
|
}
|
2019-03-05 14:15:38 +00:00
|
|
|
|
2021-02-12 10:27:44 +00:00
|
|
|
s.srv4, err = v4Create(V4ServerConf{
|
2020-07-03 15:20:01 +00:00
|
|
|
Enabled: true,
|
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},
|
2020-07-03 15:20:01 +00:00
|
|
|
notify: testNotify,
|
2021-02-12 10:27:44 +00:00
|
|
|
})
|
|
|
|
require.Nil(t, err)
|
2019-03-05 14:15:38 +00:00
|
|
|
|
2020-07-03 15:20:01 +00:00
|
|
|
s.srv6, err = v6Create(V6ServerConf{})
|
2021-02-12 10:27:44 +00:00
|
|
|
require.Nil(t, err)
|
2019-03-05 14:15:38 +00:00
|
|
|
|
2021-02-12 10:27:44 +00:00
|
|
|
leases := []Lease{{
|
|
|
|
IP: net.IP{192, 168, 10, 100},
|
|
|
|
HWAddr: net.HardwareAddr{0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA},
|
|
|
|
Expiry: time.Now().Add(time.Hour),
|
|
|
|
}, {
|
|
|
|
IP: net.IP{192, 168, 10, 101},
|
|
|
|
HWAddr: net.HardwareAddr{0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xBB},
|
|
|
|
}}
|
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-02-12 10:27:44 +00:00
|
|
|
srv4.addLease(&leases[0])
|
|
|
|
require.Nil(t, s.srv4.AddStaticLease(leases[1]))
|
2019-03-05 14:15:38 +00:00
|
|
|
|
|
|
|
s.dbStore()
|
2021-02-12 10:27:44 +00:00
|
|
|
t.Cleanup(func() {
|
|
|
|
assert.Nil(t, os.Remove(dbFilename))
|
|
|
|
})
|
2020-07-03 15:20:01 +00:00
|
|
|
s.srv4.ResetLeases(nil)
|
2019-03-05 14:15:38 +00:00
|
|
|
s.dbLoad()
|
|
|
|
|
2020-07-03 15:20:01 +00:00
|
|
|
ll := s.srv4.GetLeases(LeasesAll)
|
2021-02-12 10:27:44 +00:00
|
|
|
require.Len(t, ll, len(leases))
|
2020-07-03 15:20:01 +00:00
|
|
|
|
2021-02-12 10:27:44 +00:00
|
|
|
assert.Equal(t, leases[1].HWAddr, ll[0].HWAddr)
|
|
|
|
assert.Equal(t, leases[1].IP, ll[0].IP)
|
2021-01-13 13:56:05 +00:00
|
|
|
assert.EqualValues(t, leaseExpireStatic, ll[0].Expiry.Unix())
|
2020-07-03 15:20:01 +00:00
|
|
|
|
2021-02-12 10:27:44 +00: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 14:15:38 +00:00
|
|
|
}
|
2019-07-17 08:55:21 +00:00
|
|
|
|
|
|
|
func TestIsValidSubnetMask(t *testing.T) {
|
2021-02-12 10:27:44 +00: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 08:55:21 +00:00
|
|
|
}
|
2019-12-03 16:24:30 +00:00
|
|
|
|
|
|
|
func TestNormalizeLeases(t *testing.T) {
|
2021-02-12 10:27:44 +00: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 16:24:30 +00:00
|
|
|
|
2020-10-30 16:18:51 +00:00
|
|
|
leases := normalizeLeases(staticLeases, dynLeases)
|
2021-02-12 10:27:44 +00:00
|
|
|
require.Len(t, leases, 3)
|
2019-12-03 16:24:30 +00:00
|
|
|
|
2021-02-12 10:27:44 +00: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 16:24:30 +00:00
|
|
|
}
|