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 (118 lines) | stat: -rw-r--r-- 2,951 bytes parent folder | download
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
// 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 autoconfigure

import (
	"bytes"
	"net"
	"testing"

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

func TestOptionRequested0(t *testing.T) {
	req, err := dhcpv4.NewDiscovery(net.HardwareAddr{0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff})
	if err != nil {
		t.Fatal(err)
	}
	req.UpdateOption(dhcpv4.OptGeneric(dhcpv4.OptionAutoConfigure, []byte{1}))
	stub, err := dhcpv4.NewReplyFromRequest(req,
	    dhcpv4.WithMessageType(dhcpv4.MessageTypeOffer),
	)
	if err != nil {
		t.Fatal(err)
	}

	resp, stop := Handler4(req, stub)
	if resp == nil {
		t.Fatal("plugin did not return a message")
	}
	if stop {
		t.Error("plugin interrupted processing")
	}
	opt := resp.Options.Get(dhcpv4.OptionAutoConfigure)
	if opt == nil {
		t.Fatal("plugin did not return the Auto-Configure option")
	}
	if !bytes.Equal(opt, []byte{0}) {
		t.Errorf("plugin gave wrong option response: %v", opt)
	}
}

func TestOptionRequested1(t *testing.T) {
	req, err := dhcpv4.NewDiscovery(net.HardwareAddr{0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff})
	if err != nil {
		t.Fatal(err)
	}
	req.UpdateOption(dhcpv4.OptGeneric(dhcpv4.OptionAutoConfigure, []byte{1}))
	stub, err := dhcpv4.NewReplyFromRequest(req,
	    dhcpv4.WithMessageType(dhcpv4.MessageTypeOffer),
	)
	if err != nil {
		t.Fatal(err)
	}

	autoconfigure = 1
	resp, stop := Handler4(req, stub)
	if resp == nil {
		t.Fatal("plugin did not return a message")
	}
	if stop {
		t.Error("plugin interrupted processing")
	}
	opt := resp.Options.Get(dhcpv4.OptionAutoConfigure)
	if opt == nil {
		t.Fatal("plugin did not return the Auto-Configure option")
	}
	if !bytes.Equal(opt, []byte{1}) {
		t.Errorf("plugin gave wrong option response: %v", opt)
	}
}

func TestNotRequestedAssignedIP(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,
	    dhcpv4.WithMessageType(dhcpv4.MessageTypeOffer),
	)
	if err != nil {
		t.Fatal(err)
	}
	stub.YourIPAddr = net.ParseIP("192.0.2.100")

	resp, stop := Handler4(req, stub)
	if resp == nil {
		t.Fatal("plugin did not return a message")
	}
	if stop {
		t.Error("plugin interrupted processing")
	}
	if resp.Options.Get(dhcpv4.OptionAutoConfigure) != nil {
		t.Error("plugin responsed with AutoConfigure option")
	}
}

func TestNotRequestedNoIP(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,
	    dhcpv4.WithMessageType(dhcpv4.MessageTypeOffer),
	)
	if err != nil {
		t.Fatal(err)
	}

	resp, stop := Handler4(req, stub)
	if resp != nil {
		t.Error("plugin returned a message")
	}
	if !stop {
		t.Error("plugin did not interrupt processing")
	}
}