Pull request: 2639 use testify require vol.3

Merge in DNS/adguard-home from 2639-testify-require-3 to master

Updates #2639.

Squashed commit of the following:

commit 83d7afcbb7e5393db5a0242f3eaca063710d36b7
Merge: ef154b6d e83b919d
Author: Eugene Burkov <e.burkov@adguard.com>
Date:   Fri Feb 12 13:07:58 2021 +0300

    Merge branch 'master' into 2639-testify-require-3

commit ef154b6d3c89f975ce28369372757a1205baa655
Merge: 5b46073a 2eb21ef4
Author: Eugene Burkov <e.burkov@adguard.com>
Date:   Fri Feb 12 12:40:43 2021 +0300

    Merge branch 'master' into 2639-testify-require-3

commit 5b46073a09badef44c86a5f48c6bb874c8df2674
Merge: 7dd7b6e0 890f0322
Author: Eugene Burkov <e.burkov@adguard.com>
Date:   Wed Feb 10 21:20:51 2021 +0300

    Merge branch 'master' into 2639-testify-require-3

commit 7dd7b6e00ead2bf507af541c801a9ac770106440
Author: Eugene Burkov <e.burkov@adguard.com>
Date:   Wed Feb 10 21:19:36 2021 +0300

    dhcpd: fix comment

commit 9e74adbcf21dad58409c3dfc8e08b6470bfedc22
Author: Eugene Burkov <e.burkov@adguard.com>
Date:   Wed Feb 10 15:13:40 2021 +0300

    all: imp tests drastically
This commit is contained in:
Eugene Burkov 2021-02-12 13:27:44 +03:00
parent e83b919dbd
commit 0d44822c43
10 changed files with 551 additions and 404 deletions

View File

@ -8,6 +8,7 @@ import (
"testing" "testing"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
) )
func TestLimitReadCloser(t *testing.T) { func TestLimitReadCloser(t *testing.T) {
@ -78,11 +79,11 @@ func TestLimitedReadCloser_Read(t *testing.T) {
buf := make([]byte, tc.limit+1) buf := make([]byte, tc.limit+1)
lreader, err := LimitReadCloser(readCloser, tc.limit) lreader, err := LimitReadCloser(readCloser, tc.limit)
assert.Nil(t, err) require.Nil(t, err)
n, err := lreader.Read(buf) n, err := lreader.Read(buf)
assert.Equal(t, n, tc.want) require.Equal(t, tc.err, err)
assert.Equal(t, tc.err, err) assert.Equal(t, tc.want, n)
}) })
} }
} }

View File

@ -11,6 +11,7 @@ import (
"github.com/AdguardTeam/AdGuardHome/internal/aghtest" "github.com/AdguardTeam/AdGuardHome/internal/aghtest"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
) )
func TestMain(m *testing.M) { func TestMain(m *testing.M) {
@ -20,116 +21,156 @@ func TestMain(m *testing.M) {
func testNotify(flags uint32) { func testNotify(flags uint32) {
} }
// Leases database store/load // Leases database store/load.
func TestDB(t *testing.T) { func TestDB(t *testing.T) {
var err error var err error
s := Server{} s := Server{
s.conf.DBFilePath = dbFilename conf: ServerConfig{
DBFilePath: dbFilename,
},
}
conf := V4ServerConf{ s.srv4, err = v4Create(V4ServerConf{
Enabled: true, Enabled: true,
RangeStart: net.IP{192, 168, 10, 100}, RangeStart: net.IP{192, 168, 10, 100},
RangeEnd: net.IP{192, 168, 10, 200}, RangeEnd: net.IP{192, 168, 10, 200},
GatewayIP: net.IP{192, 168, 10, 1}, GatewayIP: net.IP{192, 168, 10, 1},
SubnetMask: net.IP{255, 255, 255, 0}, SubnetMask: net.IP{255, 255, 255, 0},
notify: testNotify, notify: testNotify,
} })
s.srv4, err = v4Create(conf) require.Nil(t, err)
assert.Nil(t, err)
s.srv6, err = v6Create(V6ServerConf{}) s.srv6, err = v6Create(V6ServerConf{})
assert.Nil(t, err) require.Nil(t, err)
l := Lease{} leases := []Lease{{
l.IP = net.IP{192, 168, 10, 100} IP: net.IP{192, 168, 10, 100},
l.HWAddr, _ = net.ParseMAC("aa:aa:aa:aa:aa:aa") HWAddr: net.HardwareAddr{0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA},
exp1 := time.Now().Add(time.Hour) Expiry: time.Now().Add(time.Hour),
l.Expiry = exp1 }, {
IP: net.IP{192, 168, 10, 101},
HWAddr: net.HardwareAddr{0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xBB},
}}
srv4, ok := s.srv4.(*v4Server) srv4, ok := s.srv4.(*v4Server)
assert.True(t, ok) require.True(t, ok)
srv4.addLease(&l) srv4.addLease(&leases[0])
require.Nil(t, s.srv4.AddStaticLease(leases[1]))
l2 := Lease{}
l2.IP = net.IP{192, 168, 10, 101}
l2.HWAddr, _ = net.ParseMAC("aa:aa:aa:aa:aa:bb")
err = s.srv4.AddStaticLease(l2)
assert.Nil(t, err)
_ = os.Remove("leases.db")
s.dbStore() s.dbStore()
t.Cleanup(func() {
assert.Nil(t, os.Remove(dbFilename))
})
s.srv4.ResetLeases(nil) s.srv4.ResetLeases(nil)
s.dbLoad() s.dbLoad()
ll := s.srv4.GetLeases(LeasesAll) ll := s.srv4.GetLeases(LeasesAll)
require.Len(t, ll, len(leases))
assert.Equal(t, "aa:aa:aa:aa:aa:bb", ll[0].HWAddr.String()) assert.Equal(t, leases[1].HWAddr, ll[0].HWAddr)
assert.True(t, net.IP{192, 168, 10, 101}.Equal(ll[0].IP)) assert.Equal(t, leases[1].IP, ll[0].IP)
assert.EqualValues(t, leaseExpireStatic, ll[0].Expiry.Unix()) assert.EqualValues(t, leaseExpireStatic, ll[0].Expiry.Unix())
assert.Equal(t, "aa:aa:aa:aa:aa:aa", ll[1].HWAddr.String()) assert.Equal(t, leases[0].HWAddr, ll[1].HWAddr)
assert.True(t, net.IP{192, 168, 10, 100}.Equal(ll[1].IP)) assert.Equal(t, leases[0].IP, ll[1].IP)
assert.Equal(t, exp1.Unix(), ll[1].Expiry.Unix()) assert.Equal(t, leases[0].Expiry.Unix(), ll[1].Expiry.Unix())
_ = os.Remove("leases.db")
} }
func TestIsValidSubnetMask(t *testing.T) { func TestIsValidSubnetMask(t *testing.T) {
assert.True(t, isValidSubnetMask([]byte{255, 255, 255, 0})) testCases := []struct {
assert.True(t, isValidSubnetMask([]byte{255, 255, 254, 0})) mask net.IP
assert.True(t, isValidSubnetMask([]byte{255, 255, 252, 0})) want bool
assert.False(t, isValidSubnetMask([]byte{255, 255, 253, 0})) }{{
assert.False(t, isValidSubnetMask([]byte{255, 255, 255, 1})) 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) { func TestNormalizeLeases(t *testing.T) {
dynLeases := []*Lease{} dynLeases := []*Lease{{
staticLeases := []*Lease{} HWAddr: net.HardwareAddr{1, 2, 3, 4},
}, {
HWAddr: net.HardwareAddr{1, 2, 3, 5},
}}
lease := &Lease{} staticLeases := []*Lease{{
lease.HWAddr = []byte{1, 2, 3, 4} HWAddr: net.HardwareAddr{1, 2, 3, 4},
dynLeases = append(dynLeases, lease) IP: net.IP{0, 2, 3, 4},
lease = new(Lease) }, {
lease.HWAddr = []byte{1, 2, 3, 5} HWAddr: net.HardwareAddr{2, 2, 3, 4},
dynLeases = append(dynLeases, lease) }}
lease = new(Lease)
lease.HWAddr = []byte{1, 2, 3, 4}
lease.IP = []byte{0, 2, 3, 4}
staticLeases = append(staticLeases, lease)
lease = new(Lease)
lease.HWAddr = []byte{2, 2, 3, 4}
staticLeases = append(staticLeases, lease)
leases := normalizeLeases(staticLeases, dynLeases) leases := normalizeLeases(staticLeases, dynLeases)
require.Len(t, leases, 3)
assert.Len(t, leases, 3) assert.Equal(t, leases[0].HWAddr, dynLeases[0].HWAddr)
assert.True(t, bytes.Equal(leases[0].HWAddr, []byte{1, 2, 3, 4})) assert.Equal(t, leases[0].IP, staticLeases[0].IP)
assert.True(t, bytes.Equal(leases[0].IP, []byte{0, 2, 3, 4})) assert.Equal(t, leases[1].HWAddr, staticLeases[1].HWAddr)
assert.True(t, bytes.Equal(leases[1].HWAddr, []byte{2, 2, 3, 4})) assert.Equal(t, leases[2].HWAddr, dynLeases[1].HWAddr)
assert.True(t, bytes.Equal(leases[2].HWAddr, []byte{1, 2, 3, 5}))
} }
func TestOptions(t *testing.T) { func TestOptions(t *testing.T) {
code, val := parseOptionString(" 12 hex abcdef ") testCases := []struct {
assert.EqualValues(t, 12, code) name string
assert.True(t, bytes.Equal([]byte{0xab, 0xcd, 0xef}, val)) optStr string
wantCode uint8
wantVal []byte
}{{
name: "all_right_hex",
optStr: " 12 hex abcdef ",
wantCode: 12,
wantVal: []byte{0xab, 0xcd, 0xef},
}, {
name: "bad_hex",
optStr: " 12 hex abcdef1 ",
wantCode: 0,
}, {
name: "all_right_ip",
optStr: "123 ip 1.2.3.4",
wantCode: 123,
wantVal: net.IPv4(1, 2, 3, 4),
}, {
name: "bad_code",
optStr: "256 ip 1.1.1.1",
wantCode: 0,
}, {
name: "negative_code",
optStr: "-1 ip 1.1.1.1",
wantCode: 0,
}, {
name: "bad_ip",
optStr: "12 ip 1.1.1.1x",
wantCode: 0,
}, {
name: "bad_mode",
optStr: "12 x 1.1.1.1",
wantCode: 0,
}}
code, _ = parseOptionString(" 12 hex abcdef1 ") for _, tc := range testCases {
assert.EqualValues(t, 0, code) t.Run(tc.name, func(t *testing.T) {
code, val := parseOptionString(tc.optStr)
code, val = parseOptionString("123 ip 1.2.3.4") require.EqualValues(t, tc.wantCode, code)
assert.EqualValues(t, 123, code) if tc.wantVal != nil {
assert.True(t, net.IP{1, 2, 3, 4}.Equal(net.IP(val))) assert.True(t, bytes.Equal(tc.wantVal, val))
}
code, _ = parseOptionString("256 ip 1.1.1.1") })
assert.EqualValues(t, 0, code) }
code, _ = parseOptionString("-1 ip 1.1.1.1")
assert.EqualValues(t, 0, code)
code, _ = parseOptionString("12 ip 1.1.1.1x")
assert.EqualValues(t, 0, code)
code, _ = parseOptionString("12 x 1.1.1.1")
assert.EqualValues(t, 0, code)
} }

View File

@ -6,6 +6,7 @@ import (
"testing" "testing"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
) )
func TestServer_notImplemented(t *testing.T) { func TestServer_notImplemented(t *testing.T) {
@ -14,7 +15,7 @@ func TestServer_notImplemented(t *testing.T) {
w := httptest.NewRecorder() w := httptest.NewRecorder()
r, err := http.NewRequest(http.MethodGet, "/unsupported", nil) r, err := http.NewRequest(http.MethodGet, "/unsupported", nil)
assert.Nil(t, err) require.Nil(t, err)
h(w, r) h(w, r)
assert.Equal(t, http.StatusNotImplemented, w.Code) assert.Equal(t, http.StatusNotImplemented, w.Code)

View File

@ -1,7 +1,6 @@
package dhcpd package dhcpd
import ( import (
"bytes"
"net" "net"
"testing" "testing"
@ -9,7 +8,7 @@ import (
) )
func TestRA(t *testing.T) { func TestRA(t *testing.T) {
ra := icmpv6RA{ data := createICMPv6RAPacket(icmpv6RA{
managedAddressConfiguration: false, managedAddressConfiguration: false,
otherConfiguration: true, otherConfiguration: true,
mtu: 1500, mtu: 1500,
@ -17,8 +16,7 @@ func TestRA(t *testing.T) {
prefixLen: 64, prefixLen: 64,
recursiveDNSServer: net.ParseIP("fe80::800:27ff:fe00:0"), recursiveDNSServer: net.ParseIP("fe80::800:27ff:fe00:0"),
sourceLinkLayerAddress: []byte{0x0a, 0x00, 0x27, 0x00, 0x00, 0x00}, sourceLinkLayerAddress: []byte{0x0a, 0x00, 0x27, 0x00, 0x00, 0x00},
} })
data := createICMPv6RAPacket(ra)
dataCorrect := []byte{ dataCorrect := []byte{
0x86, 0x00, 0x00, 0x00, 0x40, 0x40, 0x07, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x86, 0x00, 0x00, 0x00, 0x40, 0x40, 0x07, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x03, 0x04, 0x40, 0xc0, 0x00, 0x00, 0x0e, 0x10, 0x00, 0x00, 0x0e, 0x10, 0x00, 0x00, 0x00, 0x00, 0x03, 0x04, 0x40, 0xc0, 0x00, 0x00, 0x0e, 0x10, 0x00, 0x00, 0x0e, 0x10, 0x00, 0x00, 0x00, 0x00,
@ -27,5 +25,5 @@ func TestRA(t *testing.T) {
0x19, 0x03, 0x00, 0x00, 0x00, 0x00, 0x0e, 0x10, 0xfe, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x19, 0x03, 0x00, 0x00, 0x00, 0x00, 0x0e, 0x10, 0xfe, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x08, 0x00, 0x27, 0xff, 0xfe, 0x00, 0x00, 0x00, 0x08, 0x00, 0x27, 0xff, 0xfe, 0x00, 0x00, 0x00,
} }
assert.True(t, bytes.Equal(data, dataCorrect)) assert.Equal(t, dataCorrect, data)
} }

View File

@ -23,6 +23,7 @@ type v4Server struct {
srv *server4.Server srv *server4.Server
leasesLock sync.Mutex leasesLock sync.Mutex
leases []*Lease leases []*Lease
// TODO(e.burkov): This field type should be a normal bitmap.
ipAddrs [256]byte ipAddrs [256]byte
conf V4ServerConf conf V4ServerConf

View File

@ -7,6 +7,7 @@ import (
"github.com/AdguardTeam/AdGuardHome/internal/agherr" "github.com/AdguardTeam/AdGuardHome/internal/agherr"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
) )
type fakeIface struct { type fakeIface struct {
@ -79,8 +80,8 @@ func TestIfaceIPAddrs(t *testing.T) {
for _, tc := range testCases { for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) { t.Run(tc.name, func(t *testing.T) {
got, gotErr := ifaceIPAddrs(tc.iface, tc.ipv) got, gotErr := ifaceIPAddrs(tc.iface, tc.ipv)
require.True(t, errors.Is(gotErr, tc.wantErr))
assert.Equal(t, tc.want, got) assert.Equal(t, tc.want, got)
assert.True(t, errors.Is(gotErr, tc.wantErr))
}) })
} }
} }
@ -141,11 +142,7 @@ func TestIfaceDNSIPAddrs(t *testing.T) {
wantErr: errTest, wantErr: errTest,
}, { }, {
name: "ipv4_wait", name: "ipv4_wait",
iface: &waitingFakeIface{ iface: &waitingFakeIface{addrs: []net.Addr{addr4}, err: nil, n: 1},
addrs: []net.Addr{addr4},
err: nil,
n: 1,
},
ipv: ipVersion4, ipv: ipVersion4,
want: []net.IP{ip4, ip4}, want: []net.IP{ip4, ip4},
wantErr: nil, wantErr: nil,
@ -169,11 +166,7 @@ func TestIfaceDNSIPAddrs(t *testing.T) {
wantErr: errTest, wantErr: errTest,
}, { }, {
name: "ipv6_wait", name: "ipv6_wait",
iface: &waitingFakeIface{ iface: &waitingFakeIface{addrs: []net.Addr{addr6}, err: nil, n: 1},
addrs: []net.Addr{addr6},
err: nil,
n: 1,
},
ipv: ipVersion6, ipv: ipVersion6,
want: []net.IP{ip6, ip6}, want: []net.IP{ip6, ip6},
wantErr: nil, wantErr: nil,
@ -182,8 +175,8 @@ func TestIfaceDNSIPAddrs(t *testing.T) {
for _, tc := range testCases { for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) { t.Run(tc.name, func(t *testing.T) {
got, gotErr := ifaceDNSIPAddrs(tc.iface, tc.ipv, 2, 0) got, gotErr := ifaceDNSIPAddrs(tc.iface, tc.ipv, 2, 0)
require.True(t, errors.Is(gotErr, tc.wantErr))
assert.Equal(t, tc.want, got) assert.Equal(t, tc.want, got)
assert.True(t, errors.Is(gotErr, tc.wantErr))
}) })
} }
} }

View File

@ -8,172 +8,182 @@ import (
"github.com/insomniacslk/dhcp/dhcpv4" "github.com/insomniacslk/dhcp/dhcpv4"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
) )
func notify4(flags uint32) { func notify4(flags uint32) {
} }
func TestV4StaticLeaseAddRemove(t *testing.T) { func TestV4_AddRemove_static(t *testing.T) {
conf := V4ServerConf{ s, err := v4Create(V4ServerConf{
Enabled: true, Enabled: true,
RangeStart: net.IP{192, 168, 10, 100}, RangeStart: net.IP{192, 168, 10, 100},
RangeEnd: net.IP{192, 168, 10, 200}, RangeEnd: net.IP{192, 168, 10, 200},
GatewayIP: net.IP{192, 168, 10, 1}, GatewayIP: net.IP{192, 168, 10, 1},
SubnetMask: net.IP{255, 255, 255, 0}, SubnetMask: net.IP{255, 255, 255, 0},
notify: notify4, notify: notify4,
} })
s, err := v4Create(conf) require.Nil(t, err)
assert.Nil(t, err)
ls := s.GetLeases(LeasesStatic) ls := s.GetLeases(LeasesStatic)
assert.Empty(t, ls) assert.Empty(t, ls)
// add static lease // Add static lease.
l := Lease{} l := Lease{
l.IP = net.IP{192, 168, 10, 150} IP: net.IP{192, 168, 10, 150},
l.HWAddr, _ = net.ParseMAC("aa:aa:aa:aa:aa:aa") HWAddr: net.HardwareAddr{0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA},
assert.Nil(t, s.AddStaticLease(l)) }
require.Nil(t, s.AddStaticLease(l))
// try to add the same static lease - fail
assert.NotNil(t, s.AddStaticLease(l)) assert.NotNil(t, s.AddStaticLease(l))
// check
ls = s.GetLeases(LeasesStatic) ls = s.GetLeases(LeasesStatic)
assert.Len(t, ls, 1) require.Len(t, ls, 1)
assert.True(t, net.IP{192, 168, 10, 150}.Equal(ls[0].IP)) assert.True(t, l.IP.Equal(ls[0].IP))
assert.Equal(t, "aa:aa:aa:aa:aa:aa", ls[0].HWAddr.String()) assert.Equal(t, l.HWAddr, ls[0].HWAddr)
assert.EqualValues(t, leaseExpireStatic, ls[0].Expiry.Unix()) assert.EqualValues(t, leaseExpireStatic, ls[0].Expiry.Unix())
// try to remove static lease - fail // Try to remove static lease.
l.IP = net.IP{192, 168, 10, 110} assert.NotNil(t, s.RemoveStaticLease(Lease{
l.HWAddr, _ = net.ParseMAC("aa:aa:aa:aa:aa:aa") IP: net.IP{192, 168, 10, 110},
assert.NotNil(t, s.RemoveStaticLease(l)) HWAddr: net.HardwareAddr{0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA},
}))
// remove static lease // Remove static lease.
l.IP = net.IP{192, 168, 10, 150} require.Nil(t, s.RemoveStaticLease(l))
l.HWAddr, _ = net.ParseMAC("aa:aa:aa:aa:aa:aa")
assert.Nil(t, s.RemoveStaticLease(l))
// check
ls = s.GetLeases(LeasesStatic) ls = s.GetLeases(LeasesStatic)
assert.Empty(t, ls) assert.Empty(t, ls)
} }
func TestV4StaticLeaseAddReplaceDynamic(t *testing.T) { func TestV4_AddReplace(t *testing.T) {
conf := V4ServerConf{ sIface, err := v4Create(V4ServerConf{
Enabled: true, Enabled: true,
RangeStart: net.IP{192, 168, 10, 100}, RangeStart: net.IP{192, 168, 10, 100},
RangeEnd: net.IP{192, 168, 10, 200}, RangeEnd: net.IP{192, 168, 10, 200},
GatewayIP: net.IP{192, 168, 10, 1}, GatewayIP: net.IP{192, 168, 10, 1},
SubnetMask: net.IP{255, 255, 255, 0}, SubnetMask: net.IP{255, 255, 255, 0},
notify: notify4, notify: notify4,
} })
sIface, err := v4Create(conf) require.Nil(t, err)
s, ok := sIface.(*v4Server) s, ok := sIface.(*v4Server)
assert.True(t, ok) require.True(t, ok)
assert.Nil(t, err)
// add dynamic lease dynLeases := []Lease{{
ld := Lease{} IP: net.IP{192, 168, 10, 150},
ld.IP = net.IP{192, 168, 10, 150} HWAddr: net.HardwareAddr{0x11, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA},
ld.HWAddr, _ = net.ParseMAC("11:aa:aa:aa:aa:aa") }, {
s.addLease(&ld) IP: net.IP{192, 168, 10, 151},
HWAddr: net.HardwareAddr{0x22, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA},
}}
// add dynamic lease for i := range dynLeases {
{ s.addLease(&dynLeases[i])
ld := Lease{}
ld.IP = net.IP{192, 168, 10, 151}
ld.HWAddr, _ = net.ParseMAC("22:aa:aa:aa:aa:aa")
s.addLease(&ld)
} }
// add static lease with the same IP stLeases := []Lease{{
l := Lease{} IP: net.IP{192, 168, 10, 150},
l.IP = net.IP{192, 168, 10, 150} HWAddr: net.HardwareAddr{0x33, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA},
l.HWAddr, _ = net.ParseMAC("33:aa:aa:aa:aa:aa") }, {
assert.Nil(t, s.AddStaticLease(l)) IP: net.IP{192, 168, 10, 152},
HWAddr: net.HardwareAddr{0x22, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA},
}}
// add static lease with the same MAC for _, l := range stLeases {
l = Lease{} require.Nil(t, s.AddStaticLease(l))
l.IP = net.IP{192, 168, 10, 152} }
l.HWAddr, _ = net.ParseMAC("22:aa:aa:aa:aa:aa")
assert.Nil(t, s.AddStaticLease(l))
// check
ls := s.GetLeases(LeasesStatic) ls := s.GetLeases(LeasesStatic)
assert.Len(t, ls, 2) require.Len(t, ls, 2)
assert.True(t, net.IP{192, 168, 10, 150}.Equal(ls[0].IP)) for i, l := range ls {
assert.Equal(t, "33:aa:aa:aa:aa:aa", ls[0].HWAddr.String()) assert.True(t, stLeases[i].IP.Equal(l.IP))
assert.EqualValues(t, leaseExpireStatic, ls[0].Expiry.Unix()) assert.Equal(t, stLeases[i].HWAddr, l.HWAddr)
assert.EqualValues(t, leaseExpireStatic, l.Expiry.Unix())
assert.True(t, net.IP{192, 168, 10, 152}.Equal(ls[1].IP)) }
assert.Equal(t, "22:aa:aa:aa:aa:aa", ls[1].HWAddr.String())
assert.EqualValues(t, leaseExpireStatic, ls[1].Expiry.Unix())
} }
func TestV4StaticLeaseGet(t *testing.T) { func TestV4StaticLease_Get(t *testing.T) {
conf := V4ServerConf{ var err error
sIface, err := v4Create(V4ServerConf{
Enabled: true, Enabled: true,
RangeStart: net.IP{192, 168, 10, 100}, RangeStart: net.IP{192, 168, 10, 100},
RangeEnd: net.IP{192, 168, 10, 200}, RangeEnd: net.IP{192, 168, 10, 200},
GatewayIP: net.IP{192, 168, 10, 1}, GatewayIP: net.IP{192, 168, 10, 1},
SubnetMask: net.IP{255, 255, 255, 0}, SubnetMask: net.IP{255, 255, 255, 0},
notify: notify4, notify: notify4,
} })
sIface, err := v4Create(conf) require.Nil(t, err)
s, ok := sIface.(*v4Server) s, ok := sIface.(*v4Server)
assert.True(t, ok) require.True(t, ok)
assert.Nil(t, err)
s.conf.dnsIPAddrs = []net.IP{{192, 168, 10, 1}} s.conf.dnsIPAddrs = []net.IP{{192, 168, 10, 1}}
l := Lease{} l := Lease{
l.IP = net.IP{192, 168, 10, 150} IP: net.IP{192, 168, 10, 150},
l.HWAddr, _ = net.ParseMAC("aa:aa:aa:aa:aa:aa") HWAddr: net.HardwareAddr{0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA},
assert.Nil(t, s.AddStaticLease(l)) }
require.Nil(t, s.AddStaticLease(l))
// "Discover" var req, resp *dhcpv4.DHCPv4
mac, _ := net.ParseMAC("aa:aa:aa:aa:aa:aa") mac := net.HardwareAddr{0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA}
req, _ := dhcpv4.NewDiscovery(mac)
resp, _ := dhcpv4.NewReplyFromRequest(req) t.Run("discover", func(t *testing.T) {
var err error
req, err = dhcpv4.NewDiscovery(mac)
require.Nil(t, err)
resp, err = dhcpv4.NewReplyFromRequest(req)
require.Nil(t, err)
assert.Equal(t, 1, s.process(req, resp)) assert.Equal(t, 1, s.process(req, resp))
})
require.Nil(t, err)
// check "Offer" t.Run("offer", func(t *testing.T) {
assert.Equal(t, dhcpv4.MessageTypeOffer, resp.MessageType()) assert.Equal(t, dhcpv4.MessageTypeOffer, resp.MessageType())
assert.Equal(t, "aa:aa:aa:aa:aa:aa", resp.ClientHWAddr.String()) assert.Equal(t, mac, resp.ClientHWAddr)
assert.True(t, net.IP{192, 168, 10, 150}.Equal(resp.YourIPAddr)) assert.True(t, l.IP.Equal(resp.YourIPAddr))
assert.True(t, net.IP{192, 168, 10, 1}.Equal(resp.Router()[0])) assert.True(t, s.conf.GatewayIP.Equal(resp.Router()[0]))
assert.True(t, net.IP{192, 168, 10, 1}.Equal(resp.ServerIdentifier())) assert.True(t, s.conf.GatewayIP.Equal(resp.ServerIdentifier()))
assert.True(t, net.IP{255, 255, 255, 0}.Equal(net.IP(resp.SubnetMask()))) assert.Equal(t, s.conf.subnetMask, resp.SubnetMask())
assert.Equal(t, s.conf.leaseTime.Seconds(), resp.IPAddressLeaseTime(-1).Seconds()) assert.Equal(t, s.conf.leaseTime.Seconds(), resp.IPAddressLeaseTime(-1).Seconds())
})
// "Request" t.Run("request", func(t *testing.T) {
req, _ = dhcpv4.NewRequestFromOffer(resp) req, err = dhcpv4.NewRequestFromOffer(resp)
resp, _ = dhcpv4.NewReplyFromRequest(req) require.Nil(t, err)
resp, err = dhcpv4.NewReplyFromRequest(req)
require.Nil(t, err)
assert.Equal(t, 1, s.process(req, resp)) assert.Equal(t, 1, s.process(req, resp))
})
require.Nil(t, err)
// check "Ack" t.Run("ack", func(t *testing.T) {
assert.Equal(t, dhcpv4.MessageTypeAck, resp.MessageType()) assert.Equal(t, dhcpv4.MessageTypeAck, resp.MessageType())
assert.Equal(t, "aa:aa:aa:aa:aa:aa", resp.ClientHWAddr.String()) assert.Equal(t, mac, resp.ClientHWAddr)
assert.True(t, net.IP{192, 168, 10, 150}.Equal(resp.YourIPAddr)) assert.True(t, l.IP.Equal(resp.YourIPAddr))
assert.True(t, net.IP{192, 168, 10, 1}.Equal(resp.Router()[0])) assert.True(t, s.conf.GatewayIP.Equal(resp.Router()[0]))
assert.True(t, net.IP{192, 168, 10, 1}.Equal(resp.ServerIdentifier())) assert.True(t, s.conf.GatewayIP.Equal(resp.ServerIdentifier()))
assert.True(t, net.IP{255, 255, 255, 0}.Equal(net.IP(resp.SubnetMask()))) assert.Equal(t, s.conf.subnetMask, resp.SubnetMask())
assert.Equal(t, s.conf.leaseTime.Seconds(), resp.IPAddressLeaseTime(-1).Seconds()) assert.Equal(t, s.conf.leaseTime.Seconds(), resp.IPAddressLeaseTime(-1).Seconds())
})
dnsAddrs := resp.DNS() dnsAddrs := resp.DNS()
assert.Len(t, dnsAddrs, 1) require.Len(t, dnsAddrs, 1)
assert.True(t, net.IP{192, 168, 10, 1}.Equal(dnsAddrs[0])) assert.True(t, s.conf.GatewayIP.Equal(dnsAddrs[0]))
// check lease t.Run("check_lease", func(t *testing.T) {
ls := s.GetLeases(LeasesStatic) ls := s.GetLeases(LeasesStatic)
assert.Len(t, ls, 1) require.Len(t, ls, 1)
assert.True(t, net.IP{192, 168, 10, 150}.Equal(ls[0].IP)) assert.True(t, l.IP.Equal(ls[0].IP))
assert.Equal(t, "aa:aa:aa:aa:aa:aa", ls[0].HWAddr.String()) assert.Equal(t, mac, ls[0].HWAddr)
})
} }
func TestV4DynamicLeaseGet(t *testing.T) { func TestV4DynamicLease_Get(t *testing.T) {
conf := V4ServerConf{ var err error
sIface, err := v4Create(V4ServerConf{
Enabled: true, Enabled: true,
RangeStart: net.IP{192, 168, 10, 100}, RangeStart: net.IP{192, 168, 10, 100},
RangeEnd: net.IP{192, 168, 10, 200}, RangeEnd: net.IP{192, 168, 10, 200},
@ -184,58 +194,97 @@ func TestV4DynamicLeaseGet(t *testing.T) {
"81 hex 303132", "81 hex 303132",
"82 ip 1.2.3.4", "82 ip 1.2.3.4",
}, },
} })
sIface, err := v4Create(conf) require.Nil(t, err)
s, ok := sIface.(*v4Server) s, ok := sIface.(*v4Server)
assert.True(t, ok) require.True(t, ok)
assert.Nil(t, err)
s.conf.dnsIPAddrs = []net.IP{{192, 168, 10, 1}} s.conf.dnsIPAddrs = []net.IP{{192, 168, 10, 1}}
// "Discover" var req, resp *dhcpv4.DHCPv4
mac, _ := net.ParseMAC("aa:aa:aa:aa:aa:aa") mac := net.HardwareAddr{0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA}
req, _ := dhcpv4.NewDiscovery(mac)
resp, _ := dhcpv4.NewReplyFromRequest(req)
assert.Equal(t, 1, s.process(req, resp))
// check "Offer" t.Run("discover", func(t *testing.T) {
req, err = dhcpv4.NewDiscovery(mac)
require.Nil(t, err)
resp, err = dhcpv4.NewReplyFromRequest(req)
require.Nil(t, err)
assert.Equal(t, 1, s.process(req, resp))
})
require.Nil(t, err)
t.Run("offer", func(t *testing.T) {
assert.Equal(t, dhcpv4.MessageTypeOffer, resp.MessageType()) assert.Equal(t, dhcpv4.MessageTypeOffer, resp.MessageType())
assert.Equal(t, "aa:aa:aa:aa:aa:aa", resp.ClientHWAddr.String()) assert.Equal(t, mac, resp.ClientHWAddr)
assert.True(t, net.IP{192, 168, 10, 100}.Equal(resp.YourIPAddr)) assert.True(t, s.conf.RangeStart.Equal(resp.YourIPAddr))
assert.True(t, net.IP{192, 168, 10, 1}.Equal(resp.Router()[0])) assert.True(t, s.conf.GatewayIP.Equal(resp.Router()[0]))
assert.True(t, net.IP{192, 168, 10, 1}.Equal(resp.ServerIdentifier())) assert.True(t, s.conf.GatewayIP.Equal(resp.ServerIdentifier()))
assert.True(t, net.IP{255, 255, 255, 0}.Equal(net.IP(resp.SubnetMask()))) assert.Equal(t, s.conf.subnetMask, resp.SubnetMask())
assert.Equal(t, s.conf.leaseTime.Seconds(), resp.IPAddressLeaseTime(-1).Seconds()) assert.Equal(t, s.conf.leaseTime.Seconds(), resp.IPAddressLeaseTime(-1).Seconds())
assert.Equal(t, []byte("012"), resp.Options[uint8(dhcpv4.OptionFQDN)]) assert.Equal(t, []byte("012"), resp.Options[uint8(dhcpv4.OptionFQDN)])
assert.True(t, net.IP{1, 2, 3, 4}.Equal(net.IP(resp.Options[uint8(dhcpv4.OptionRelayAgentInformation)]))) assert.True(t, net.IP{1, 2, 3, 4}.Equal(net.IP(resp.Options[uint8(dhcpv4.OptionRelayAgentInformation)])))
})
// "Request" t.Run("request", func(t *testing.T) {
req, _ = dhcpv4.NewRequestFromOffer(resp) var err error
resp, _ = dhcpv4.NewReplyFromRequest(req)
req, err = dhcpv4.NewRequestFromOffer(resp)
require.Nil(t, err)
resp, err = dhcpv4.NewReplyFromRequest(req)
require.Nil(t, err)
assert.Equal(t, 1, s.process(req, resp)) assert.Equal(t, 1, s.process(req, resp))
})
require.Nil(t, err)
// check "Ack" t.Run("ack", func(t *testing.T) {
assert.Equal(t, dhcpv4.MessageTypeAck, resp.MessageType()) assert.Equal(t, dhcpv4.MessageTypeAck, resp.MessageType())
assert.Equal(t, "aa:aa:aa:aa:aa:aa", resp.ClientHWAddr.String()) assert.Equal(t, mac, resp.ClientHWAddr)
assert.True(t, net.IP{192, 168, 10, 100}.Equal(resp.YourIPAddr)) assert.True(t, s.conf.RangeStart.Equal(resp.YourIPAddr))
assert.True(t, net.IP{192, 168, 10, 1}.Equal(resp.Router()[0])) assert.True(t, s.conf.GatewayIP.Equal(resp.Router()[0]))
assert.True(t, net.IP{192, 168, 10, 1}.Equal(resp.ServerIdentifier())) assert.True(t, s.conf.GatewayIP.Equal(resp.ServerIdentifier()))
assert.True(t, net.IP{255, 255, 255, 0}.Equal(net.IP(resp.SubnetMask()))) assert.Equal(t, s.conf.subnetMask, resp.SubnetMask())
assert.Equal(t, s.conf.leaseTime.Seconds(), resp.IPAddressLeaseTime(-1).Seconds()) assert.Equal(t, s.conf.leaseTime.Seconds(), resp.IPAddressLeaseTime(-1).Seconds())
})
dnsAddrs := resp.DNS() dnsAddrs := resp.DNS()
assert.Len(t, dnsAddrs, 1) require.Len(t, dnsAddrs, 1)
assert.True(t, net.IP{192, 168, 10, 1}.Equal(dnsAddrs[0])) assert.True(t, net.IP{192, 168, 10, 1}.Equal(dnsAddrs[0]))
// check lease // check lease
t.Run("check_lease", func(t *testing.T) {
ls := s.GetLeases(LeasesDynamic) ls := s.GetLeases(LeasesDynamic)
assert.Len(t, ls, 1) assert.Len(t, ls, 1)
assert.True(t, net.IP{192, 168, 10, 100}.Equal(ls[0].IP)) assert.True(t, net.IP{192, 168, 10, 100}.Equal(ls[0].IP))
assert.Equal(t, "aa:aa:aa:aa:aa:aa", ls[0].HWAddr.String()) assert.Equal(t, mac, ls[0].HWAddr)
})
}
func TestIP4InRange(t *testing.T) {
start := net.IP{192, 168, 10, 100} start := net.IP{192, 168, 10, 100}
stop := net.IP{192, 168, 10, 200} stop := net.IP{192, 168, 10, 200}
assert.False(t, ip4InRange(start, stop, net.IP{192, 168, 10, 99}))
assert.False(t, ip4InRange(start, stop, net.IP{192, 168, 11, 100})) testCases := []struct {
assert.False(t, ip4InRange(start, stop, net.IP{192, 168, 11, 201})) ip net.IP
assert.True(t, ip4InRange(start, stop, net.IP{192, 168, 10, 100})) want bool
}{{
ip: net.IP{192, 168, 10, 99},
want: false,
}, {
ip: net.IP{192, 168, 11, 100},
want: false,
}, {
ip: net.IP{192, 168, 11, 201},
want: false,
}, {
ip: start,
want: true,
}}
for _, tc := range testCases {
t.Run(tc.ip.String(), func(t *testing.T) {
assert.Equal(t, tc.want, ip4InRange(start, stop, tc.ip))
})
}
} }

View File

@ -9,220 +9,283 @@ import (
"github.com/insomniacslk/dhcp/dhcpv6" "github.com/insomniacslk/dhcp/dhcpv6"
"github.com/insomniacslk/dhcp/iana" "github.com/insomniacslk/dhcp/iana"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
) )
func notify6(flags uint32) { func notify6(flags uint32) {
} }
func TestV6StaticLeaseAddRemove(t *testing.T) { func TestV6_AddRemove_static(t *testing.T) {
conf := V6ServerConf{ s, err := v6Create(V6ServerConf{
Enabled: true, Enabled: true,
RangeStart: net.ParseIP("2001::1"), RangeStart: net.ParseIP("2001::1"),
notify: notify6, notify: notify6,
})
require.Nil(t, err)
require.Empty(t, s.GetLeases(LeasesStatic))
// Add static lease.
l := Lease{
IP: net.ParseIP("2001::1"),
HWAddr: net.HardwareAddr{0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA},
} }
s, err := v6Create(conf) require.Nil(t, s.AddStaticLease(l))
assert.Nil(t, err)
// Try to add the same static lease.
require.NotNil(t, s.AddStaticLease(l))
ls := s.GetLeases(LeasesStatic) ls := s.GetLeases(LeasesStatic)
assert.Empty(t, ls) require.Len(t, ls, 1)
assert.Equal(t, l.IP, ls[0].IP)
// add static lease assert.Equal(t, l.HWAddr, ls[0].HWAddr)
l := Lease{}
l.IP = net.ParseIP("2001::1")
l.HWAddr, _ = net.ParseMAC("aa:aa:aa:aa:aa:aa")
assert.Nil(t, s.AddStaticLease(l))
// try to add static lease - fail
assert.NotNil(t, s.AddStaticLease(l))
// check
ls = s.GetLeases(LeasesStatic)
assert.Len(t, ls, 1)
assert.Equal(t, "2001::1", ls[0].IP.String())
assert.Equal(t, "aa:aa:aa:aa:aa:aa", ls[0].HWAddr.String())
assert.EqualValues(t, leaseExpireStatic, ls[0].Expiry.Unix()) assert.EqualValues(t, leaseExpireStatic, ls[0].Expiry.Unix())
// try to remove static lease - fail // Try to remove non-existent static lease.
l.IP = net.ParseIP("2001::2") require.NotNil(t, s.RemoveStaticLease(Lease{
l.HWAddr, _ = net.ParseMAC("aa:aa:aa:aa:aa:aa") IP: net.ParseIP("2001::2"),
assert.NotNil(t, s.RemoveStaticLease(l)) HWAddr: l.HWAddr,
}))
// remove static lease // Remove static lease.
l.IP = net.ParseIP("2001::1") require.Nil(t, s.RemoveStaticLease(l))
l.HWAddr, _ = net.ParseMAC("aa:aa:aa:aa:aa:aa")
assert.Nil(t, s.RemoveStaticLease(l))
// check assert.Empty(t, s.GetLeases(LeasesStatic))
ls = s.GetLeases(LeasesStatic)
assert.Empty(t, ls)
} }
func TestV6StaticLeaseAddReplaceDynamic(t *testing.T) { func TestV6_AddReplace(t *testing.T) {
conf := V6ServerConf{ sIface, err := v6Create(V6ServerConf{
Enabled: true, Enabled: true,
RangeStart: net.ParseIP("2001::1"), RangeStart: net.ParseIP("2001::1"),
notify: notify6, notify: notify6,
} })
sIface, err := v6Create(conf) require.Nil(t, err)
s, ok := sIface.(*v6Server) s, ok := sIface.(*v6Server)
assert.True(t, ok) require.True(t, ok)
assert.Nil(t, err)
// add dynamic lease // Add dynamic leases.
ld := Lease{} dynLeases := []*Lease{{
ld.IP = net.ParseIP("2001::1") IP: net.ParseIP("2001::1"),
ld.HWAddr, _ = net.ParseMAC("11:aa:aa:aa:aa:aa") HWAddr: net.HardwareAddr{0x11, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA},
s.addLease(&ld) }, {
IP: net.ParseIP("2001::2"),
HWAddr: net.HardwareAddr{0x22, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA},
}}
// add dynamic lease for _, l := range dynLeases {
{ s.addLease(l)
ld := Lease{}
ld.IP = net.ParseIP("2001::2")
ld.HWAddr, _ = net.ParseMAC("22:aa:aa:aa:aa:aa")
s.addLease(&ld)
} }
// add static lease with the same IP stLeases := []Lease{{
l := Lease{} IP: net.ParseIP("2001::1"),
l.IP = net.ParseIP("2001::1") HWAddr: net.HardwareAddr{0x33, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA},
l.HWAddr, _ = net.ParseMAC("33:aa:aa:aa:aa:aa") }, {
assert.Nil(t, s.AddStaticLease(l)) IP: net.ParseIP("2001::3"),
HWAddr: net.HardwareAddr{0x22, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA},
}}
// add static lease with the same MAC for _, l := range stLeases {
l = Lease{} require.Nil(t, s.AddStaticLease(l))
l.IP = net.ParseIP("2001::3") }
l.HWAddr, _ = net.ParseMAC("22:aa:aa:aa:aa:aa")
assert.Nil(t, s.AddStaticLease(l))
// check
ls := s.GetLeases(LeasesStatic) ls := s.GetLeases(LeasesStatic)
assert.Len(t, ls, 2) require.Len(t, ls, 2)
assert.Equal(t, "2001::1", ls[0].IP.String()) for i, l := range ls {
assert.Equal(t, "33:aa:aa:aa:aa:aa", ls[0].HWAddr.String()) assert.True(t, stLeases[i].IP.Equal(l.IP))
assert.EqualValues(t, leaseExpireStatic, ls[0].Expiry.Unix()) assert.Equal(t, stLeases[i].HWAddr, l.HWAddr)
assert.EqualValues(t, leaseExpireStatic, l.Expiry.Unix())
assert.Equal(t, "2001::3", ls[1].IP.String()) }
assert.Equal(t, "22:aa:aa:aa:aa:aa", ls[1].HWAddr.String())
assert.EqualValues(t, leaseExpireStatic, ls[1].Expiry.Unix())
} }
func TestV6GetLease(t *testing.T) { func TestV6GetLease(t *testing.T) {
conf := V6ServerConf{ var err error
sIface, err := v6Create(V6ServerConf{
Enabled: true, Enabled: true,
RangeStart: net.ParseIP("2001::1"), RangeStart: net.ParseIP("2001::1"),
notify: notify6, notify: notify6,
} })
sIface, err := v6Create(conf) require.Nil(t, err)
s, ok := sIface.(*v6Server) s, ok := sIface.(*v6Server)
assert.True(t, ok) require.True(t, ok)
assert.Nil(t, err)
s.conf.dnsIPAddrs = []net.IP{net.ParseIP("2000::1")} dnsAddr := net.ParseIP("2000::1")
s.conf.dnsIPAddrs = []net.IP{dnsAddr}
s.sid = dhcpv6.Duid{ s.sid = dhcpv6.Duid{
Type: dhcpv6.DUID_LLT, Type: dhcpv6.DUID_LLT,
HwType: iana.HWTypeEthernet, HwType: iana.HWTypeEthernet,
LinkLayerAddr: net.HardwareAddr{0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA},
} }
s.sid.LinkLayerAddr, _ = net.ParseMAC("aa:aa:aa:aa:aa:aa")
l := Lease{} l := Lease{
l.IP = net.ParseIP("2001::1") IP: net.ParseIP("2001::1"),
l.HWAddr, _ = net.ParseMAC("aa:aa:aa:aa:aa:aa") HWAddr: net.HardwareAddr{0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA},
assert.Nil(t, s.AddStaticLease(l)) }
require.Nil(t, s.AddStaticLease(l))
var req, resp, msg *dhcpv6.Message
mac := net.HardwareAddr{0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA}
t.Run("solicit", func(t *testing.T) {
req, err = dhcpv6.NewSolicit(mac)
require.Nil(t, err)
msg, err = req.GetInnerMessage()
require.Nil(t, err)
resp, err = dhcpv6.NewAdvertiseFromSolicit(msg)
require.Nil(t, err)
// "Solicit"
mac, _ := net.ParseMAC("aa:aa:aa:aa:aa:aa")
req, _ := dhcpv6.NewSolicit(mac)
msg, _ := req.GetInnerMessage()
resp, _ := dhcpv6.NewAdvertiseFromSolicit(msg)
assert.True(t, s.process(msg, req, resp)) assert.True(t, s.process(msg, req, resp))
})
require.Nil(t, err)
resp.AddOption(dhcpv6.OptServerID(s.sid)) resp.AddOption(dhcpv6.OptServerID(s.sid))
// check "Advertise" var oia *dhcpv6.OptIANA
assert.Equal(t, dhcpv6.MessageTypeAdvertise, resp.Type()) var oiaAddr *dhcpv6.OptIAAddress
oia := resp.Options.OneIANA() t.Run("advertise", func(t *testing.T) {
oiaAddr := oia.Options.OneAddress() require.Equal(t, dhcpv6.MessageTypeAdvertise, resp.Type())
assert.Equal(t, "2001::1", oiaAddr.IPv6Addr.String())
assert.Equal(t, s.conf.leaseTime.Seconds(), oiaAddr.ValidLifetime.Seconds())
// "Request"
req, _ = dhcpv6.NewRequestFromAdvertise(resp)
msg, _ = req.GetInnerMessage()
resp, _ = dhcpv6.NewReplyFromMessage(msg)
assert.True(t, s.process(msg, req, resp))
// check "Reply"
assert.Equal(t, dhcpv6.MessageTypeReply, resp.Type())
oia = resp.Options.OneIANA() oia = resp.Options.OneIANA()
oiaAddr = oia.Options.OneAddress() oiaAddr = oia.Options.OneAddress()
assert.Equal(t, "2001::1", oiaAddr.IPv6Addr.String())
assert.Equal(t, l.IP, oiaAddr.IPv6Addr)
assert.Equal(t, s.conf.leaseTime.Seconds(), oiaAddr.ValidLifetime.Seconds()) assert.Equal(t, s.conf.leaseTime.Seconds(), oiaAddr.ValidLifetime.Seconds())
})
t.Run("request", func(t *testing.T) {
req, err = dhcpv6.NewRequestFromAdvertise(resp)
require.Nil(t, err)
msg, err = req.GetInnerMessage()
require.Nil(t, err)
resp, err = dhcpv6.NewReplyFromMessage(msg)
require.Nil(t, err)
assert.True(t, s.process(msg, req, resp))
})
require.Nil(t, err)
t.Run("reply", func(t *testing.T) {
require.Equal(t, dhcpv6.MessageTypeReply, resp.Type())
oia = resp.Options.OneIANA()
oiaAddr = oia.Options.OneAddress()
assert.Equal(t, l.IP, oiaAddr.IPv6Addr)
assert.Equal(t, s.conf.leaseTime.Seconds(), oiaAddr.ValidLifetime.Seconds())
})
dnsAddrs := resp.Options.DNS() dnsAddrs := resp.Options.DNS()
assert.Len(t, dnsAddrs, 1) require.Len(t, dnsAddrs, 1)
assert.Equal(t, "2000::1", dnsAddrs[0].String()) assert.Equal(t, dnsAddr, dnsAddrs[0])
// check lease t.Run("lease", func(t *testing.T) {
ls := s.GetLeases(LeasesStatic) ls := s.GetLeases(LeasesStatic)
assert.Len(t, ls, 1) require.Len(t, ls, 1)
assert.Equal(t, "2001::1", ls[0].IP.String()) assert.Equal(t, l.IP, ls[0].IP)
assert.Equal(t, "aa:aa:aa:aa:aa:aa", ls[0].HWAddr.String()) assert.Equal(t, l.HWAddr, ls[0].HWAddr)
})
} }
func TestV6GetDynamicLease(t *testing.T) { func TestV6GetDynamicLease(t *testing.T) {
conf := V6ServerConf{ sIface, err := v6Create(V6ServerConf{
Enabled: true, Enabled: true,
RangeStart: net.ParseIP("2001::2"), RangeStart: net.ParseIP("2001::2"),
notify: notify6, notify: notify6,
} })
sIface, err := v6Create(conf) require.Nil(t, err)
s, ok := sIface.(*v6Server) s, ok := sIface.(*v6Server)
assert.True(t, ok) require.True(t, ok)
assert.Nil(t, err)
s.conf.dnsIPAddrs = []net.IP{net.ParseIP("2000::1")} dnsAddr := net.ParseIP("2000::1")
s.conf.dnsIPAddrs = []net.IP{dnsAddr}
s.sid = dhcpv6.Duid{ s.sid = dhcpv6.Duid{
Type: dhcpv6.DUID_LLT, Type: dhcpv6.DUID_LLT,
HwType: iana.HWTypeEthernet, HwType: iana.HWTypeEthernet,
LinkLayerAddr: net.HardwareAddr{0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA},
} }
s.sid.LinkLayerAddr, _ = net.ParseMAC("aa:aa:aa:aa:aa:aa")
// "Solicit" var req, resp, msg *dhcpv6.Message
mac, _ := net.ParseMAC("aa:aa:aa:aa:aa:aa") mac := net.HardwareAddr{0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA}
req, _ := dhcpv6.NewSolicit(mac) t.Run("solicit", func(t *testing.T) {
msg, _ := req.GetInnerMessage() req, err = dhcpv6.NewSolicit(mac)
resp, _ := dhcpv6.NewAdvertiseFromSolicit(msg) require.Nil(t, err)
msg, err = req.GetInnerMessage()
require.Nil(t, err)
resp, err = dhcpv6.NewAdvertiseFromSolicit(msg)
require.Nil(t, err)
assert.True(t, s.process(msg, req, resp)) assert.True(t, s.process(msg, req, resp))
})
require.Nil(t, err)
resp.AddOption(dhcpv6.OptServerID(s.sid)) resp.AddOption(dhcpv6.OptServerID(s.sid))
// check "Advertise" var oia *dhcpv6.OptIANA
assert.Equal(t, dhcpv6.MessageTypeAdvertise, resp.Type()) var oiaAddr *dhcpv6.OptIAAddress
oia := resp.Options.OneIANA() t.Run("advertise", func(t *testing.T) {
oiaAddr := oia.Options.OneAddress() require.Equal(t, dhcpv6.MessageTypeAdvertise, resp.Type())
assert.Equal(t, "2001::2", oiaAddr.IPv6Addr.String())
// "Request"
req, _ = dhcpv6.NewRequestFromAdvertise(resp)
msg, _ = req.GetInnerMessage()
resp, _ = dhcpv6.NewReplyFromMessage(msg)
assert.True(t, s.process(msg, req, resp))
// check "Reply"
assert.Equal(t, dhcpv6.MessageTypeReply, resp.Type())
oia = resp.Options.OneIANA() oia = resp.Options.OneIANA()
oiaAddr = oia.Options.OneAddress() oiaAddr = oia.Options.OneAddress()
assert.Equal(t, "2001::2", oiaAddr.IPv6Addr.String()) assert.Equal(t, "2001::2", oiaAddr.IPv6Addr.String())
})
t.Run("request", func(t *testing.T) {
req, err = dhcpv6.NewRequestFromAdvertise(resp)
require.Nil(t, err)
msg, err = req.GetInnerMessage()
require.Nil(t, err)
resp, err = dhcpv6.NewReplyFromMessage(msg)
require.Nil(t, err)
assert.True(t, s.process(msg, req, resp))
})
require.Nil(t, err)
t.Run("reply", func(t *testing.T) {
require.Equal(t, dhcpv6.MessageTypeReply, resp.Type())
oia = resp.Options.OneIANA()
oiaAddr = oia.Options.OneAddress()
assert.Equal(t, "2001::2", oiaAddr.IPv6Addr.String())
})
dnsAddrs := resp.Options.DNS() dnsAddrs := resp.Options.DNS()
assert.Len(t, dnsAddrs, 1) require.Len(t, dnsAddrs, 1)
assert.Equal(t, "2000::1", dnsAddrs[0].String()) assert.Equal(t, dnsAddr, dnsAddrs[0])
// check lease t.Run("lease", func(t *testing.T) {
ls := s.GetLeases(LeasesDynamic) ls := s.GetLeases(LeasesDynamic)
assert.Len(t, ls, 1) require.Len(t, ls, 1)
assert.Equal(t, "2001::2", ls[0].IP.String()) assert.Equal(t, "2001::2", ls[0].IP.String())
assert.Equal(t, "aa:aa:aa:aa:aa:aa", ls[0].HWAddr.String()) assert.Equal(t, mac, ls[0].HWAddr)
})
assert.False(t, ip6InRange(net.ParseIP("2001::2"), net.ParseIP("2001::1"))) }
assert.False(t, ip6InRange(net.ParseIP("2001::2"), net.ParseIP("2002::2")))
assert.True(t, ip6InRange(net.ParseIP("2001::2"), net.ParseIP("2001::2"))) func TestIP6InRange(t *testing.T) {
assert.True(t, ip6InRange(net.ParseIP("2001::2"), net.ParseIP("2001::3"))) start := net.ParseIP("2001::2")
testCases := []struct {
ip net.IP
want bool
}{{
ip: net.ParseIP("2001::1"),
want: false,
}, {
ip: net.ParseIP("2002::2"),
want: false,
}, {
ip: start,
want: true,
}, {
ip: net.ParseIP("2001::3"),
want: true,
}}
for _, tc := range testCases {
t.Run(tc.ip.String(), func(t *testing.T) {
assert.Equal(t, tc.want, ip6InRange(start, tc.ip))
})
}
} }

View File

@ -5,16 +5,15 @@ import (
"testing" "testing"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
) )
func TestIPDetector_detectSpecialNetwork(t *testing.T) { func TestIPDetector_detectSpecialNetwork(t *testing.T) {
var ipd *ipDetector var ipd *ipDetector
t.Run("newIPDetector", func(t *testing.T) {
var err error var err error
ipd, err = newIPDetector() ipd, err = newIPDetector()
assert.Nil(t, err) require.Nil(t, err)
})
testCases := []struct { testCases := []struct {
name string name string

View File

@ -9,6 +9,7 @@ import (
"github.com/AdguardTeam/AdGuardHome/internal/aghio" "github.com/AdguardTeam/AdGuardHome/internal/aghio"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
) )
func TestLimitRequestBody(t *testing.T) { func TestLimitRequestBody(t *testing.T) {
@ -60,8 +61,8 @@ func TestLimitRequestBody(t *testing.T) {
lim.ServeHTTP(res, req) lim.ServeHTTP(res, req)
require.Equal(t, tc.wantErr, err)
assert.Equal(t, tc.want, res.Body.Bytes()) assert.Equal(t, tc.want, res.Body.Bytes())
assert.Equal(t, tc.wantErr, err)
}) })
} }
} }