File: plugin_test.go

package info (click to toggle)
golang-github-coredhcp-coredhcp 0.0.0%2Bgit.20250806.f7e98e4-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 460 kB
  • sloc: makefile: 8; sh: 6
file content (96 lines) | stat: -rw-r--r-- 2,251 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
// Copyright 2018-present the CoreDHCP Authors. All rights reserved
// This source code is licensed under the MIT license found in the
// LICENSE file in the root directory of this source tree.

package searchdomains

import (
	"net"
	"testing"

	"github.com/insomniacslk/dhcp/dhcpv4"
	"github.com/insomniacslk/dhcp/dhcpv6"

	"github.com/stretchr/testify/assert"
)

func TestAddDomains6(t *testing.T) {
	assert := assert.New(t)

	// Search domains we will expect the DHCP server to assign
	searchDomains := []string{"domain.a", "domain.b"}

	// Init plugin
	handler6, err := Plugin.Setup6(searchDomains...)
	if err != nil {
		t.Fatal(err)
	}

	// Fake request
	req, err := dhcpv6.NewMessage()
	if err != nil {
		t.Fatal(err)
	}
	req.MessageType = dhcpv6.MessageTypeRequest
	req.AddOption(dhcpv6.OptRequestedOption(dhcpv6.OptionDNSRecursiveNameServer))

	// Fake response input
	stub, err := dhcpv6.NewMessage()
	if err != nil {
		t.Fatal(err)
	}
	stub.MessageType = dhcpv6.MessageTypeReply

	// Call plugin
	resp, stop := handler6(req, stub)
	if resp == nil {
		t.Fatal("plugin did not return a message")
	}
	if stop {
		t.Error("plugin interrupted processing")
	}

	searchLabels := resp.(*dhcpv6.Message).Options.DomainSearchList().Labels
	assert.Equal(searchDomains, searchLabels)
}

func TestAddDomains4(t *testing.T) {
	assert := assert.New(t)

	// Search domains we will expect the DHCP server to assign
	// NOTE: these domains should be different from the v6 test domains;
	// this tests that we haven't accidentally set the v6 domains in the
	// v4 plugin handler or vice versa.
	searchDomains := []string{"domain.b", "domain.c"}

	// Init plugin
	handler4, err := Plugin.Setup4(searchDomains...)
	if err != nil {
		t.Fatal(err)
	}

	// Fake request
	req, err := dhcpv4.NewDiscovery(net.HardwareAddr{0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff})
	if err != nil {
		t.Fatal(err)
	}

	// Fake response input
	stub, err := dhcpv4.NewReplyFromRequest(req)
	if err != nil {
		t.Fatal(err)
	}

	// Call plugin
	resp, stop := handler4(req, stub)
	if resp == nil {
		t.Fatal("plugin did not return a message")
	}
	if stop {
		t.Error("plugin interrupted processing")
	}

	searchLabels := resp.DomainSearch().Labels
	assert.Equal(searchDomains, searchLabels)

}