Skip to content

Commit

Permalink
dhcpd: imp tests
Browse files Browse the repository at this point in the history
  • Loading branch information
EugeneOne1 committed Oct 18, 2021
1 parent d915e0b commit 9a8194e
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 47 deletions.
3 changes: 1 addition & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -114,8 +114,7 @@ In this release, the schema version has changed from 10 to 12.

### Fixed

- Too low priority for explicitly configured domain name server DHCP option
([#3744]).
- Incorrect assignment of explicitly configured DHCP options ([#3744]).
- Occasional panic during shutdown ([#3655]).
- Addition of IPs into only one as opposed to all matching ipsets on Linux
([#3638]).
Expand Down
83 changes: 38 additions & 45 deletions internal/dhcpd/v4_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,34 @@ import (
func notify4(flags uint32) {
}

func TestV4_AddRemove_static(t *testing.T) {
s, err := v4Create(V4ServerConf{
// defaultV4ServerConf returns the default configuration for *v4Server to use in
// tests.
func defaultV4ServerConf() (conf V4ServerConf) {
return 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: notify4,
})
}
}

// defaultSrv prepares the default DHCPServer to use in tests. The underlying
// type of s is *v4Server.
func defaultSrv(t *testing.T) (s DHCPServer) {
t.Helper()

var err error
s, err = v4Create(defaultV4ServerConf())
require.NoError(t, err)

return s
}

func TestV4_AddRemove_static(t *testing.T) {
s := defaultSrv(t)

ls := s.GetLeases(LeasesStatic)
assert.Empty(t, ls)

Expand All @@ -39,7 +56,7 @@ func TestV4_AddRemove_static(t *testing.T) {
IP: net.IP{192, 168, 10, 150},
}

err = s.AddStaticLease(l)
err := s.AddStaticLease(l)
require.NoError(t, err)

err = s.AddStaticLease(l)
Expand Down Expand Up @@ -67,15 +84,7 @@ func TestV4_AddRemove_static(t *testing.T) {
}

func TestV4_AddReplace(t *testing.T) {
sIface, 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: notify4,
})
require.NoError(t, err)
sIface := defaultSrv(t)

s, ok := sIface.(*v4Server)
require.True(t, ok)
Expand All @@ -91,7 +100,7 @@ func TestV4_AddReplace(t *testing.T) {
}}

for i := range dynLeases {
err = s.addLease(&dynLeases[i])
err := s.addLease(&dynLeases[i])
require.NoError(t, err)
}

Expand All @@ -106,7 +115,7 @@ func TestV4_AddReplace(t *testing.T) {
}}

for _, l := range stLeases {
err = s.AddStaticLease(l)
err := s.AddStaticLease(l)
require.NoError(t, err)
}

Expand All @@ -124,15 +133,10 @@ func TestV4Server_Process_optionsPriority(t *testing.T) {
defaultIP := net.IP{192, 168, 1, 1}
knownIP := net.IP{1, 2, 3, 4}

// prepareSrv creates a *v4Server and sets the opt6IPs in the initial
// configuration of the server as the value for DHCP option 6.
prepareSrv := func(opt6IPs []net.IP) (s *v4Server) {
conf := 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: notify4,
}
conf := defaultV4ServerConf()
if len(opt6IPs) > 0 {
b := &strings.Builder{}
stringutil.WriteToBuilder(b, "6 ips ", opt6IPs[0].String())
Expand All @@ -154,6 +158,8 @@ func TestV4Server_Process_optionsPriority(t *testing.T) {
return s
}

// checkResp creates a discovery message with DHCP option 6 requested amd
// asserts the response to contain wantIPs in this option.
checkResp := func(t *testing.T, s *v4Server, wantIPs []net.IP) {
mac := net.HardwareAddr{0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA}
req, err := dhcpv4.NewDiscovery(mac, dhcpv4.WithRequestedOptions(
Expand Down Expand Up @@ -192,15 +198,7 @@ func TestV4Server_Process_optionsPriority(t *testing.T) {
}

func TestV4StaticLease_Get(t *testing.T) {
sIface, 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: notify4,
})
require.NoError(t, err)
sIface := defaultSrv(t)

s, ok := sIface.(*v4Server)
require.True(t, ok)
Expand All @@ -212,7 +210,7 @@ func TestV4StaticLease_Get(t *testing.T) {
HWAddr: net.HardwareAddr{0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA},
IP: net.IP{192, 168, 10, 150},
}
err = s.AddStaticLease(l)
err := s.AddStaticLease(l)
require.NoError(t, err)

var req, resp *dhcpv4.DHCPv4
Expand Down Expand Up @@ -280,19 +278,14 @@ func TestV4StaticLease_Get(t *testing.T) {
}

func TestV4DynamicLease_Get(t *testing.T) {
conf := defaultV4ServerConf()
conf.Options = []string{
"81 hex 303132",
"82 ip 1.2.3.4",
}

var err error
sIface, 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: notify4,
Options: []string{
"81 hex 303132",
"82 ip 1.2.3.4",
},
})
sIface, err := v4Create(conf)
require.NoError(t, err)

s, ok := sIface.(*v4Server)
Expand Down

0 comments on commit 9a8194e

Please sign in to comment.