coredhcp/plugins/dns/plugin_test.go
Anatole Denis 79f78359c2 plugins/dns: Implement DHCPv6 support (#54)
* plugins/dns: Implemented DHCPv6 support

Signed-off-by: Anatole Denis <anatole@unverle.fr>

* plugins/file: Remove hardcoded DNS servers

Those are handled by the DNS plugin and don't need to be hardcoded anymore

Signed-off-by: Anatole Denis <anatole@unverle.fr>

* plugins/dns: Only send option when requested

Both DHCP versions have a means for the client to specify what
additional information to send with replies. This adds logic to respect
that.

There is a difference between the protocols as to what happens when this
option doesn't exist. In DHCPv6, the option is mandatory. In DHCPv4 it
is not and indicates the server may send whatever it deems relevant

Signed-off-by: Anatole Denis <anatole@unverle.fr>

* Update dhcp library dependency

This pulls the enhancement from https://github.com/insomniacslk/dhcp/pull/315

generated by `go get github.com/insomniacslk/dhcp && go mod tidy`

Signed-off-by: Anatole Denis <anatole@unverle.fr>
2019-09-17 09:32:31 +01:00

148 lines
3.4 KiB
Go

package dns
import (
"net"
"testing"
"github.com/insomniacslk/dhcp/dhcpv4"
"github.com/insomniacslk/dhcp/dhcpv6"
)
func TestAddServer6(t *testing.T) {
req, err := dhcpv6.NewMessage()
if err != nil {
t.Fatal(err)
}
req.MessageType = dhcpv6.MessageTypeRequest
oro := dhcpv6.OptRequestedOption{}
oro.AddRequestedOption(dhcpv6.OptionDNSRecursiveNameServer)
req.AddOption(&oro)
stub, err := dhcpv6.NewMessage()
if err != nil {
t.Fatal(err)
}
stub.MessageType = dhcpv6.MessageTypeReply
dnsServers6 = []net.IP{
net.ParseIP("2001:db8::1"),
net.ParseIP("2001:db8::3"),
}
resp, stop := Handler6(req, stub)
if resp == nil {
t.Fatal("plugin did not return a message")
}
if stop {
t.Error("plugin interrupted processing")
}
opts := resp.GetOption(dhcpv6.OptionDNSRecursiveNameServer)
if len(opts) != 1 {
t.Fatalf("Expected 1 RDNSS option, got %d: %v", len(opts), opts)
}
foundServers := opts[0].(*dhcpv6.OptDNSRecursiveNameServer).NameServers
// XXX: is enforcing the order relevant here ?
for i, srv := range foundServers {
if !srv.Equal(dnsServers6[i]) {
t.Errorf("Found server %s, expected %s", srv, dnsServers6[i])
}
}
if len(foundServers) != len(dnsServers6) {
t.Errorf("Found %d servers, expected %d", len(foundServers), len(dnsServers6))
}
}
func TestNotRequested6(t *testing.T) {
req, err := dhcpv6.NewMessage()
if err != nil {
t.Fatal(err)
}
req.MessageType = dhcpv6.MessageTypeRequest
req.AddOption(&dhcpv6.OptRequestedOption{})
stub, err := dhcpv6.NewMessage()
if err != nil {
t.Fatal(err)
}
stub.MessageType = dhcpv6.MessageTypeReply
dnsServers6 = []net.IP{
net.ParseIP("2001:db8::1"),
}
resp, stop := Handler6(req, stub)
if resp == nil {
t.Fatal("plugin did not return a message")
}
if stop {
t.Error("plugin interrupted processing")
}
opts := resp.GetOption(dhcpv6.OptionDNSRecursiveNameServer)
if len(opts) != 0 {
t.Errorf("RDNSS options were added when not requested: %v", opts)
}
}
func TestAddServer4(t *testing.T) {
req, err := dhcpv4.NewDiscovery(net.HardwareAddr{0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff})
if err != nil {
t.Fatal(err)
}
stub, err := dhcpv4.NewReplyFromRequest(req)
if err != nil {
t.Fatal(err)
}
dnsServers4 = []net.IP{
net.ParseIP("192.0.2.1"),
net.ParseIP("192.0.2.3"),
}
resp, stop := Handler4(req, stub)
if resp == nil {
t.Fatal("plugin did not return a message")
}
if stop {
t.Error("plugin interrupted processing")
}
servers := resp.DNS()
for i, srv := range servers {
if !srv.Equal(dnsServers4[i]) {
t.Errorf("Found server %s, expected %s", srv, dnsServers4[i])
}
}
if len(servers) != len(dnsServers4) {
t.Errorf("Found %d servers, expected %d", len(servers), len(dnsServers4))
}
}
func TestNotRequested4(t *testing.T) {
req, err := dhcpv4.NewDiscovery(net.HardwareAddr{0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff})
if err != nil {
t.Fatal(err)
}
stub, err := dhcpv4.NewReplyFromRequest(req)
if err != nil {
t.Fatal(err)
}
dnsServers4 = []net.IP{
net.ParseIP("192.0.2.1"),
}
req.UpdateOption(dhcpv4.OptParameterRequestList(dhcpv4.OptionBroadcastAddress))
resp, stop := Handler4(req, stub)
if resp == nil {
t.Fatal("plugin did not return a message")
}
if stop {
t.Error("plugin interrupted processing")
}
servers := dhcpv4.GetIPs(dhcpv4.OptionDomainNameServer, resp.Options)
if len(servers) != 0 {
t.Errorf("Found %d DNS servers when explicitly not requested", len(servers))
}
}