forked from d2g/dhcp4client
-
Notifications
You must be signed in to change notification settings - Fork 0
/
client_test.go
116 lines (95 loc) · 3.28 KB
/
client_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
106
107
108
109
110
111
112
113
114
115
116
package dhcp4client_test
import (
"log"
"net"
"testing"
"github.com/d2g/dhcp4client"
)
//Example Client
func Test_ExampleClient(test *testing.T) {
var err error
m, err := net.ParseMAC("08-00-27-00-A8-E8")
if err != nil {
log.Printf("MAC Error:%v\n", err)
}
//Create a connection to use
//We need to set the connection ports to 1068 and 1067 so we don't need root access
c, err := dhcp4client.NewInetSock(dhcp4client.SetLocalAddr(net.UDPAddr{IP: net.IPv4(0, 0, 0, 0), Port: 1068}), dhcp4client.SetRemoteAddr(net.UDPAddr{IP: net.IPv4bcast, Port: 1067}))
if err != nil {
test.Error("Client Connection Generation:" + err.Error())
}
defer c.Close()
exampleClient, err := dhcp4client.New(dhcp4client.HardwareAddr(m), dhcp4client.Connection(c))
if err != nil {
test.Fatalf("Error:%v\n", err)
}
defer exampleClient.Close()
success, acknowledgementpacket, err := exampleClient.Request()
test.Logf("Success:%v\n", success)
test.Logf("Packet:%v\n", acknowledgementpacket)
if err != nil {
networkError, ok := err.(*net.OpError)
if ok && networkError.Timeout() {
test.Log("Test Skipping as it didn't find a DHCP Server")
test.SkipNow()
}
test.Fatalf("Error:%v\n", err)
}
if !success {
test.Error("We didn't sucessfully get a DHCP Lease?")
} else {
log.Printf("IP Received:%v\n", acknowledgementpacket.YIAddr().String())
}
test.Log("Start Renewing Lease")
success, acknowledgementpacket, err = exampleClient.Renew(acknowledgementpacket)
if err != nil {
networkError, ok := err.(*net.OpError)
if ok && networkError.Timeout() {
test.Log("Renewal Failed! Because it didn't find the DHCP server very Strange")
test.Errorf("Error" + err.Error())
}
test.Fatalf("Error:%v\n", err)
}
if !success {
test.Error("We didn't sucessfully Renew a DHCP Lease?")
} else {
log.Printf("IP Received:%v\n", acknowledgementpacket.YIAddr().String())
}
}
//Example Client (With MathGID)
func Test_ExampleClientWithMathGenerateXID(test *testing.T) {
var err error
m, err := net.ParseMAC("08-00-27-00-A8-E8")
if err != nil {
log.Printf("MAC Error:%v\n", err)
}
//Create a connection to use
//We need to set the connection ports to 1068 and 1067 so we don't need root access
c, err := dhcp4client.NewInetSock(dhcp4client.SetLocalAddr(net.UDPAddr{IP: net.IPv4(0, 0, 0, 0), Port: 1068}), dhcp4client.SetRemoteAddr(net.UDPAddr{IP: net.IPv4bcast, Port: 1067}))
if err != nil {
test.Error("Client Connection Generation:" + err.Error())
}
defer c.Close()
// If you ar using MathGenerateXID then you are responsible for seeding math/rand
exampleClient, err := dhcp4client.New(dhcp4client.HardwareAddr(m), dhcp4client.Connection(c), dhcp4client.GenerateXID(dhcp4client.MathGenerateXID))
if err != nil {
test.Fatalf("Error:%v\n", err)
}
defer exampleClient.Close()
success, acknowledgementpacket, err := exampleClient.Request()
test.Logf("Success:%v\n", success)
test.Logf("Packet:%v\n", acknowledgementpacket)
if err != nil {
networkError, ok := err.(*net.OpError)
if ok && networkError.Timeout() {
test.Log("Test Skipping as it didn't find a DHCP Server")
test.SkipNow()
}
test.Fatalf("Error:%v\n", err)
}
if !success {
test.Error("We didn't sucessfully get a DHCP Lease?")
} else {
log.Printf("IP Received:%v\n", acknowledgementpacket.YIAddr().String())
}
}