c4975dcc36ba61f90b3e420813d61406cd710b15
[govpp.git] / vendor / github.com / google / gopacket / layers / dhcp_test.go
1 // Copyright 2016, Google, Inc. All rights reserved.
2 //
3 // Use of this source code is governed by a BSD-style license
4 // that can be found in the LICENSE file in the root of the source
5 // tree.
6
7 package layers
8
9 import (
10         "bytes"
11         "net"
12         "testing"
13
14         "github.com/google/gopacket"
15 )
16
17 func TestDHCPv4EncodeRequest(t *testing.T) {
18         dhcp := &DHCPv4{Operation: DHCPOpRequest, HardwareType: LinkTypeEthernet, Xid: 0x12345678,
19                 ClientIP: net.IP{0, 0, 0, 0}, YourClientIP: net.IP{0, 0, 0, 0}, NextServerIP: net.IP{0, 0, 0, 0}, RelayAgentIP: net.IP{0, 0, 0, 0},
20                 ClientHWAddr: net.HardwareAddr{0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc},
21                 ServerName:   make([]byte, 64), File: make([]byte, 128)}
22
23         dhcp.Options = append(dhcp.Options, NewDHCPOption(DHCPOptMessageType, []byte{byte(DHCPMsgTypeDiscover)}))
24         dhcp.Options = append(dhcp.Options, NewDHCPOption(DHCPOptHostname, []byte{'e', 'x', 'a', 'm', 'p', 'l', 'e', '.', 'c', 'o', 'm'}))
25         dhcp.Options = append(dhcp.Options, NewDHCPOption(DHCPOptPad, nil))
26         dhcp.Options = append(dhcp.Options, NewDHCPOption(DHCPOptParamsRequest,
27                 []byte{byte(DHCPOptSubnetMask), byte(DHCPOptBroadcastAddr), byte(DHCPOptTimeOffset),
28                         byte(DHCPOptRouter), byte(DHCPOptDomainName), byte(DHCPOptDNS), byte(DHCPOptDomainSearch),
29                         byte(DHCPOptHostname), byte(DHCPOptNetBIOSTCPNS), byte(DHCPOptInterfaceMTU), byte(DHCPOptClasslessStaticRoute),
30                         byte(DHCPOptNTPServers)}))
31
32         buf := gopacket.NewSerializeBuffer()
33         opts := gopacket.SerializeOptions{FixLengths: true}
34         err := gopacket.SerializeLayers(buf, opts, dhcp)
35         if err != nil {
36                 t.Fatal(err)
37         }
38
39         p2 := gopacket.NewPacket(buf.Bytes(), LayerTypeDHCPv4, testDecodeOptions)
40         dhcp2 := p2.Layer(LayerTypeDHCPv4).(*DHCPv4)
41         testDHCPEqual(t, dhcp, dhcp2)
42 }
43
44 func TestDHCPv4EncodeResponse(t *testing.T) {
45         dhcp := &DHCPv4{Operation: DHCPOpReply, HardwareType: LinkTypeEthernet, Xid: 0x12345678,
46                 ClientIP: net.IP{0, 0, 0, 0}, YourClientIP: net.IP{192, 168, 0, 123}, NextServerIP: net.IP{192, 168, 0, 1}, RelayAgentIP: net.IP{0, 0, 0, 0},
47                 ClientHWAddr: net.HardwareAddr{0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc},
48                 ServerName:   make([]byte, 64), File: make([]byte, 128)}
49
50         dhcp.Options = append(dhcp.Options, NewDHCPOption(DHCPOptMessageType, []byte{byte(DHCPMsgTypeOffer)}))
51         dhcp.Options = append(dhcp.Options, NewDHCPOption(DHCPOptSubnetMask, []byte{255, 255, 255, 0}))
52         dhcp.Options = append(dhcp.Options, NewDHCPOption(DHCPOptPad, nil))
53         dhcp.Options = append(dhcp.Options, NewDHCPOption(DHCPOptT1, []byte{0x00, 0x00, 0x0e, 0x10}))
54         dhcp.Options = append(dhcp.Options, NewDHCPOption(DHCPOptT2, []byte{0x00, 0x00, 0x0e, 0x10}))
55         dhcp.Options = append(dhcp.Options, NewDHCPOption(DHCPOptLeaseTime, []byte{0x00, 0x00, 0x0e, 0x10}))
56         dhcp.Options = append(dhcp.Options, NewDHCPOption(DHCPOptServerID, []byte{192, 168, 0, 1}))
57
58         buf := gopacket.NewSerializeBuffer()
59         opts := gopacket.SerializeOptions{FixLengths: true}
60         err := gopacket.SerializeLayers(buf, opts, dhcp)
61         if err != nil {
62                 t.Fatal(err)
63         }
64
65         p2 := gopacket.NewPacket(buf.Bytes(), LayerTypeDHCPv4, testDecodeOptions)
66         dhcp2 := p2.Layer(LayerTypeDHCPv4).(*DHCPv4)
67         testDHCPEqual(t, dhcp, dhcp2)
68 }
69
70 func testDHCPEqual(t *testing.T, d1, d2 *DHCPv4) {
71         if d1.Operation != d2.Operation {
72                 t.Errorf("expected Operation=%s, got %s", d1.Operation, d2.Operation)
73         }
74         if d1.HardwareType != d2.HardwareType {
75                 t.Errorf("expected HardwareType=%s, got %s", d1.HardwareType, d2.HardwareType)
76         }
77         if d1.HardwareLen != d2.HardwareLen {
78                 t.Errorf("expected HardwareLen=%v, got %v", d1.HardwareLen, d2.HardwareLen)
79         }
80         if d1.HardwareOpts != d2.HardwareOpts {
81                 t.Errorf("expected HardwareOpts=%v, got %v", d1.HardwareOpts, d2.HardwareOpts)
82         }
83         if d1.Xid != d2.Xid {
84                 t.Errorf("expected Xid=%v, got %v", d1.Xid, d2.Xid)
85         }
86         if d1.Secs != d2.Secs {
87                 t.Errorf("expected Secs=%v, got %v", d1.Secs, d2.Secs)
88         }
89         if d1.Flags != d2.Flags {
90                 t.Errorf("expected Flags=%v, got %v", d1.Flags, d2.Flags)
91         }
92         if !d1.ClientIP.Equal(d2.ClientIP) {
93                 t.Errorf("expected ClientIP=%v, got %v", d1.ClientIP, d2.ClientIP)
94         }
95         if !d1.YourClientIP.Equal(d2.YourClientIP) {
96                 t.Errorf("expected YourClientIP=%v, got %v", d1.YourClientIP, d2.YourClientIP)
97         }
98         if !d1.NextServerIP.Equal(d2.NextServerIP) {
99                 t.Errorf("expected NextServerIP=%v, got %v", d1.NextServerIP, d2.NextServerIP)
100         }
101         if !d1.RelayAgentIP.Equal(d2.RelayAgentIP) {
102                 t.Errorf("expected RelayAgentIP=%v, got %v", d1.RelayAgentIP, d2.RelayAgentIP)
103         }
104         if !bytes.Equal(d1.ClientHWAddr, d2.ClientHWAddr) {
105                 t.Errorf("expected ClientHWAddr=%v, got %v", d1.ClientHWAddr, d2.ClientHWAddr)
106         }
107         if !bytes.Equal(d1.ServerName, d2.ServerName) {
108                 t.Errorf("expected ServerName=%v, got %v", d1.ServerName, d2.ServerName)
109         }
110         if !bytes.Equal(d1.File, d2.File) {
111                 t.Errorf("expected File=%v, got %v", d1.File, d2.File)
112         }
113         if len(d1.Options) != len(d2.Options) {
114                 t.Errorf("expected %d options, got %d", len(d1.Options), len(d2.Options))
115         }
116
117         for i, o := range d1.Options {
118                 testDHCPOptionEqual(t, i, o, d2.Options[i])
119         }
120 }
121
122 func testDHCPOptionEqual(t *testing.T, idx int, d1, d2 DHCPOption) {
123         if d1.Type != d2.Type {
124                 t.Errorf("expection Options[%d].Type = %s, got %s", idx, d1.Type, d2.Type)
125         }
126         if !bytes.Equal(d1.Data, d2.Data) {
127                 t.Errorf("expection Options[%d].Data to be = %v, got %v", idx, d1.Data, d2.Data)
128         }
129 }