Skip to content

Commit

Permalink
dhcpd: add mock
Browse files Browse the repository at this point in the history
  • Loading branch information
EugeneOne1 committed Sep 13, 2022
1 parent cfc3cfb commit 44f5507
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 42 deletions.
68 changes: 52 additions & 16 deletions internal/dhcpd/dhcpd.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,22 +141,6 @@ const (
LeaseChangedDBStore
)

// server - the current state of the DHCP server
type server struct {
srv4 DHCPServer
srv6 DHCPServer

// TODO(a.garipov): Either create a separate type for the internal config or
// just put the config values into Server.
conf *ServerConfig

// Called when the leases DB is modified
onLeaseChanged []OnLeaseChangedT
}

// type check
var _ Interface = (*server)(nil)

// GetLeasesFlags are the flags for GetLeases.
type GetLeasesFlags uint8

Expand All @@ -181,6 +165,58 @@ type Interface interface {
WriteDiskConfig(c *ServerConfig)
}

// MockInterface is a mock Interface implementation.
//
// TODO(e.burkov): Move to aghtest when the API stabilized.
type MockInterface struct {
OnStart func() (err error)
OnStop func() (err error)
OnEnabled func() (ok bool)
OnLeases func(flags GetLeasesFlags) (leases []*Lease)
OnSetOnLeaseChanged func(f OnLeaseChangedT)
OnFindMACbyIP func(ip net.IP) (mac net.HardwareAddr)
OnWriteDiskConfig func(c *ServerConfig)
}

var _ Interface = (*MockInterface)(nil)

// Start implements the Interface for *MockInterface.
func (s *MockInterface) Start() (err error) { return s.OnStart() }

// Stop implements the Interface for *MockInterface.
func (s *MockInterface) Stop() (err error) { return s.OnStop() }

// Enabled implements the Interface for *MockInterface.
func (s *MockInterface) Enabled() (ok bool) { return s.OnEnabled() }

// Leases implements the Interface for *MockInterface.
func (s *MockInterface) Leases(flags GetLeasesFlags) (ls []*Lease) { return s.OnLeases(flags) }

// SetOnLeaseChanged implements the Interface for *MockInterface.
func (s *MockInterface) SetOnLeaseChanged(f OnLeaseChangedT) { s.OnSetOnLeaseChanged(f) }

// FindMACbyIP implements the Interface for *MockInterface.
func (s *MockInterface) FindMACbyIP(ip net.IP) (mac net.HardwareAddr) { return s.OnFindMACbyIP(ip) }

// WriteDiskConfig implements the Interface for *MockInterface.
func (s *MockInterface) WriteDiskConfig(c *ServerConfig) { s.OnWriteDiskConfig(c) }

// server - the current state of the DHCP server
type server struct {
srv4 DHCPServer
srv6 DHCPServer

// TODO(a.garipov): Either create a separate type for the internal config or
// just put the config values into Server.
conf *ServerConfig

// Called when the leases DB is modified
onLeaseChanged []OnLeaseChangedT
}

// type check
var _ Interface = (*server)(nil)

// Create - create object
func Create(conf *ServerConfig) (s *server, err error) {
s = &server{
Expand Down
4 changes: 2 additions & 2 deletions internal/dnsforward/dns_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,7 @@ func TestServer_ProcessDHCPHosts_localRestriction(t *testing.T) {
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
s := &Server{
dhcpServer: &testDHCP{},
dhcpServer: testDHCP,
localDomainSuffix: defaultLocalDomainSuffix,
tableHostToIP: hostToIPTable{
"example." + defaultLocalDomainSuffix: knownIP,
Expand Down Expand Up @@ -378,7 +378,7 @@ func TestServer_ProcessDHCPHosts(t *testing.T) {

for _, tc := range testCases {
s := &Server{
dhcpServer: &testDHCP{},
dhcpServer: testDHCP,
localDomainSuffix: tc.suffix,
tableHostToIP: hostToIPTable{
"example." + tc.suffix: knownIP,
Expand Down
50 changes: 27 additions & 23 deletions internal/dnsforward/dnsforward_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ func createTestServer(

var err error
s, err = NewServer(DNSCreateParams{
DHCPServer: &testDHCP{},
DHCPServer: testDHCP,
DNSFilter: f,
PrivateNets: netutil.SubnetSetFunc(netutil.IsLocallyServed),
})
Expand Down Expand Up @@ -776,7 +776,7 @@ func TestBlockedCustomIP(t *testing.T) {

f := filtering.New(&filtering.Config{}, filters)
s, err := NewServer(DNSCreateParams{
DHCPServer: &testDHCP{},
DHCPServer: testDHCP,
DNSFilter: f,
PrivateNets: netutil.SubnetSetFunc(netutil.IsLocallyServed),
})
Expand Down Expand Up @@ -910,7 +910,7 @@ func TestRewrite(t *testing.T) {
f.SetEnabled(true)

s, err := NewServer(DNSCreateParams{
DHCPServer: &testDHCP{},
DHCPServer: testDHCP,
DNSFilter: f,
PrivateNets: netutil.SubnetSetFunc(netutil.IsLocallyServed),
})
Expand Down Expand Up @@ -1005,32 +1005,36 @@ func publicKey(priv any) any {
}
}

// testDHCP is a mock dhcpd.Interface implementation.
//
// TODO(e.burkov): Create aghtest.DHCP when the API stabilized.
type testDHCP struct{}

func (*testDHCP) Start() (err error) { return nil }
func (*testDHCP) Stop() (err error) { return nil }
func (*testDHCP) FindMACbyIP(net.IP) (mac net.HardwareAddr) { return nil }
func (*testDHCP) Enabled() (ok bool) { return true }
func (*testDHCP) SetOnLeaseChanged(dhcpd.OnLeaseChangedT) {}
func (*testDHCP) WriteDiskConfig(*dhcpd.ServerConfig) {}

func (*testDHCP) Leases(flags dhcpd.GetLeasesFlags) (leases []*dhcpd.Lease) {
return []*dhcpd.Lease{{
IP: net.IP{192, 168, 12, 34},
HWAddr: net.HardwareAddr{0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA},
Hostname: "myhost",
}}
var testDHCP = &dhcpd.MockInterface{
OnStart: func() (err error) { panic("not implemented") },
OnStop: func() (err error) { panic("not implemented") },
OnEnabled: func() (ok bool) { return true },
OnLeases: func(flags dhcpd.GetLeasesFlags) (leases []*dhcpd.Lease) {
return []*dhcpd.Lease{{
IP: net.IP{192, 168, 12, 34},
HWAddr: net.HardwareAddr{0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA},
Hostname: "myhost",
}}
},
OnSetOnLeaseChanged: func(olct dhcpd.OnLeaseChangedT) {},
OnFindMACbyIP: func(ip net.IP) (mac net.HardwareAddr) { panic("not implemented") },
OnWriteDiskConfig: func(c *dhcpd.ServerConfig) { panic("not implemented") },
}

// func (*testDHCP) Leases(flags dhcpd.GetLeasesFlags) (leases []*dhcpd.Lease) {
// return []*dhcpd.Lease{{
// IP: net.IP{192, 168, 12, 34},
// HWAddr: net.HardwareAddr{0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA},
// Hostname: "myhost",
// }}
// }

func TestPTRResponseFromDHCPLeases(t *testing.T) {
const localDomain = "lan"

s, err := NewServer(DNSCreateParams{
DNSFilter: filtering.New(&filtering.Config{}, nil),
DHCPServer: &testDHCP{},
DHCPServer: testDHCP,
PrivateNets: netutil.SubnetSetFunc(netutil.IsLocallyServed),
LocalDomain: localDomain,
})
Expand Down Expand Up @@ -1103,7 +1107,7 @@ func TestPTRResponseFromHosts(t *testing.T) {

var s *Server
s, err = NewServer(DNSCreateParams{
DHCPServer: &testDHCP{},
DHCPServer: testDHCP,
DNSFilter: flt,
PrivateNets: netutil.SubnetSetFunc(netutil.IsLocallyServed),
})
Expand Down
2 changes: 1 addition & 1 deletion internal/dnsforward/filter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func TestHandleDNSRequest_filterDNSResponse(t *testing.T) {
f.SetEnabled(true)

s, err := NewServer(DNSCreateParams{
DHCPServer: &testDHCP{},
DHCPServer: testDHCP,
DNSFilter: f,
PrivateNets: netutil.SubnetSetFunc(netutil.IsLocallyServed),
})
Expand Down

0 comments on commit 44f5507

Please sign in to comment.