forked from d2g/dhcp4client
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient_test.go
69 lines (57 loc) · 1.72 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
package dhcp4client
import (
"log"
"net"
"testing"
)
/*
* 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 := NewInetSock(SetLocalAddr(net.UDPAddr{IP: net.IPv4(0, 0, 0, 0), Port: 1068}), SetRemoteAddr(net.UDPAddr{IP: net.IPv4bcast, Port: 1067}))
if err != nil {
test.Error("Client Conection Generation:" + err.Error())
}
exampleClient, err := New(HardwareAddr(m), Connection(c))
if err != nil {
test.Fatalf("Error:%v\n", err)
}
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())
}
}