-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdhcp_test.go
105 lines (84 loc) · 3.14 KB
/
dhcp_test.go
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
package main
import (
"log"
"net"
"testing"
"time"
dhcp "github.com/krolaw/dhcp4"
"github.com/stretchr/testify/assert"
)
func TestNewDHCPServer(t *testing.T) {
expected := &dhcpServerHandler{
ip: net.ParseIP(DHCP_SERVER_ADDR),
start: net.ParseIP(DHCP_SERVER_LEASE_START_ADDR),
leaseDuration: DHCP_LEASE_DURATION * time.Minute,
leases: make(map[int]lease, DHCP_LEASE_COUNT),
leaseRange: DHCP_LEASE_RANGE,
options: dhcp.Options{
dhcp.OptionTFTPServerName: []byte(TFTP_SERVER_ADDR),
dhcp.OptionBootFileName: []byte(PXELINUX_LOADER),
dhcp.OptionDomainNameServer: []byte(DNS_SERVER_ADDR),
},
}
actual := newDHCPServer()
assert.Equal(t, expected, actual)
}
func TestFreeLease_OK(t *testing.T) {
dhcpServer := newDHCPServer()
NotExpected := -1
actual := dhcpServer.freeLease()
assert.NotEqual(t, NotExpected, actual)
}
func TestServeDHCPDiscover_OK(t *testing.T) {
dhcpServer := newDHCPServer()
expected := dhcp.ReplyPacket(dhcp.NewPacket(dhcp.BootReply), dhcp.Offer, dhcpServer.ip,
dhcp.IPAdd(dhcpServer.start, dhcpServer.freeLease()),
dhcpServer.leaseDuration, dhcpServer.options.SelectOrderOrAll(nil))
actual := dhcpServer.ServeDHCP(dhcp.NewPacket(dhcp.BootReply), dhcp.Discover, nil)
// TODO: Need a strong assertion
// See Issue: https://github.com/simar7/xserver/issues/8
assert.ObjectsAreEqual(expected, actual)
}
func TestServeDHCPInvalidRequest_OK(t *testing.T) {
dhcpServer := newDHCPServer()
expected := dhcp.ReplyPacket(dhcp.NewPacket(dhcp.BootReply), dhcp.NAK, dhcpServer.ip, nil, 0, nil)
actual := dhcpServer.ServeDHCP(dhcp.NewPacket(dhcp.BootReply), dhcp.Request, dhcpServer.options)
assert.Equal(t, expected, actual)
}
func TestServeDHCPValidRequest_OK(t *testing.T) {
dhcpServer := newDHCPServer()
p := dhcp.NewPacket(dhcp.BootReply)
// Request the IP from the DHCP Server
p.SetCIAddr(net.ParseIP("127.0.0.2"))
reqIP := net.IP(dhcpServer.options[dhcp.OptionRequestedIPAddress])
if reqIP == nil {
reqIP = net.IP(p.CIAddr())
log.Print("[DEBUG] ", "Request IP: ", reqIP)
}
expected := dhcp.ReplyPacket(p, dhcp.ACK, dhcpServer.ip, reqIP,
dhcpServer.leaseDuration, dhcpServer.options.SelectOrder(dhcpServer.options[dhcp.OptionParameterRequestList]))
actual := dhcpServer.ServeDHCP(p, dhcp.Request, dhcp.Options{
dhcp.OptionTFTPServerName: []byte(TFTP_SERVER_ADDR),
dhcp.OptionBootFileName: []byte(PXELINUX_LOADER),
dhcp.OptionDomainNameServer: []byte(DNS_SERVER_ADDR),
})
assert.Equal(t, expected, actual)
}
func TestServeDHCPRelease_OK(t *testing.T) {
dhcpServer := newDHCPServer()
p := dhcp.NewPacket(dhcp.BootReply)
// Request the IP from the DHCP Server
p.SetCIAddr(net.ParseIP("172.10.0.2"))
expected := dhcp.Packet(nil)
actual := dhcpServer.ServeDHCP(p, dhcp.Release, nil)
assert.Equal(t, expected, actual)
}
func TestServeDHCPInform_OK(t *testing.T) {
dhcpServer := newDHCPServer()
p := dhcp.NewPacket(dhcp.BootReply)
// Request the IP from the DHCP Server
p.SetCIAddr(net.ParseIP("172.10.0.2"))
expected := dhcp.ReplyPacket(p, dhcp.ACK, dhcpServer.ip, nil, 0, nil)
actual := dhcpServer.ServeDHCP(p, dhcp.Inform, nil)
assert.Equal(t, expected, actual)
}