a02d9342c04e350b3163deeb871f33e4810db5dc
[govpp.git] / vendor / github.com / google / gopacket / layers / vxlan_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         "github.com/google/gopacket"
11         "reflect"
12         "testing"
13 )
14
15 // VXLAN is specifed in RFC 7348
16 //  0                   1                   2                   3
17 //  0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
18 //  0             8               16              24              32
19 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
20 // |G|R|R|R|I|R|R|R|R|D|R|R|A|R|R|R|       Group Policy ID         |
21 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
22 // |     24 bit VXLAN Network Identifier           |   Reserved    |
23 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
24
25 // Ethernet[IP[UDP[VXLAN[Ethernet[IP[ICMP]]]]]]
26
27 // testPacketVXLAN
28 // 0000   00 16 3e 08 71 cf 36 dc 85 1e b3 40 08 00 45 00  ..>.q.6....@..E.
29 // 0010   00 86 d2 c0 40 00 40 11 51 52 c0 a8 cb 01 c0 a8  ....@.@.QR......
30 // 0020   ca 01 b0 5d 12 b5 00 72 00 00 08 00 00 00 00 00  ...]...r........
31 // 0030   00 00 00 30 88 01 00 02 00 16 3e 37 f6 04 08 00  ...0......>7....
32 // 0040   45 00 00 54 00 00 40 00 40 01 23 4f c0 a8 cb 03  E..T..@.@.#O....
33 // 0050   c0 a8 cb 05 08 00 f6 f2 05 0c 00 01 fc e2 97 51  ...............Q
34 // 0060   00 00 00 00 a6 f8 02 00 00 00 00 00 10 11 12 13  ................
35 // 0070   14 15 16 17 18 19 1a 1b 1c 1d 1e 1f 20 21 22 23  ............ !"#
36 // 0080   24 25 26 27 28 29 2a 2b 2c 2d 2e 2f 30 31 32 33  $%&'()*+,-./0123
37 // 0090   34 35 36 37                                      4567               ./01234567
38 var testPacketVXLAN = []byte{
39         0x00, 0x16, 0x3e, 0x08, 0x71, 0xcf, 0x36, 0xdc, 0x85, 0x1e, 0xb3, 0x40, 0x08, 0x00, 0x45, 0x00,
40         0x00, 0x86, 0xd2, 0xc0, 0x40, 0x00, 0x40, 0x11, 0x51, 0x52, 0xc0, 0xa8, 0xcb, 0x01, 0xc0, 0xa8,
41         0xca, 0x01, 0xb0, 0x5d, 0x12, 0xb5, 0x00, 0x72, 0x00, 0x00, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00,
42         0xff, 0x00, 0x00, 0x30, 0x88, 0x01, 0x00, 0x02, 0x00, 0x16, 0x3e, 0x37, 0xf6, 0x04, 0x08, 0x00,
43         0x45, 0x00, 0x00, 0x54, 0x00, 0x00, 0x40, 0x00, 0x40, 0x01, 0x23, 0x4f, 0xc0, 0xa8, 0xcb, 0x03,
44         0xc0, 0xa8, 0xcb, 0x05, 0x08, 0x00, 0xf6, 0xf2, 0x05, 0x0c, 0x00, 0x01, 0xfc, 0xe2, 0x97, 0x51,
45         0x00, 0x00, 0x00, 0x00, 0xa6, 0xf8, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10, 0x11, 0x12, 0x13,
46         0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, 0x20, 0x21, 0x22, 0x23,
47         0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f, 0x30, 0x31, 0x32, 0x33,
48         0x34, 0x35, 0x36, 0x37,
49 }
50
51 func TestPacketVXLAN(t *testing.T) {
52         p := gopacket.NewPacket(testPacketVXLAN, LinkTypeEthernet, gopacket.Default)
53         if p.ErrorLayer() != nil {
54                 t.Error("Failed to decode packet:", p.ErrorLayer().Error())
55         }
56         checkLayers(p, []gopacket.LayerType{LayerTypeEthernet, LayerTypeIPv4, LayerTypeUDP, LayerTypeVXLAN, LayerTypeEthernet, LayerTypeIPv4, LayerTypeICMPv4, gopacket.LayerTypePayload}, t)
57         if got, ok := p.Layer(LayerTypeVXLAN).(*VXLAN); ok {
58                 want := &VXLAN{
59                         BaseLayer: BaseLayer{
60                                 Contents: []byte{0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0x00},
61                                 Payload: []byte{0x00, 0x30, 0x88, 0x01, 0x00, 0x02, 0x00, 0x16, 0x3e, 0x37, 0xf6, 0x04, 0x08, 0x00,
62                                         0x45, 0x00, 0x00, 0x54, 0x00, 0x00, 0x40, 0x00, 0x40, 0x01, 0x23, 0x4f, 0xc0, 0xa8, 0xcb, 0x03,
63                                         0xc0, 0xa8, 0xcb, 0x05, 0x08, 0x00, 0xf6, 0xf2, 0x05, 0x0c, 0x00, 0x01, 0xfc, 0xe2, 0x97, 0x51,
64                                         0x00, 0x00, 0x00, 0x00, 0xa6, 0xf8, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10, 0x11, 0x12, 0x13,
65                                         0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, 0x20, 0x21, 0x22, 0x23,
66                                         0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f, 0x30, 0x31, 0x32, 0x33,
67                                         0x34, 0x35, 0x36, 0x37},
68                         },
69                         ValidIDFlag:      true,
70                         VNI:              255,
71                         GBPExtension:     false,
72                         GBPApplied:       false,
73                         GBPDontLearn:     false,
74                         GBPGroupPolicyID: 0,
75                 }
76                 if !reflect.DeepEqual(want, got) {
77                         t.Errorf("VXLAN layer mismatch, \nwant %#v\ngot %#v\n", want, got)
78                 }
79         }
80 }
81
82 func BenchmarkDecodePacketVXLAN(b *testing.B) {
83         for i := 0; i < b.N; i++ {
84                 gopacket.NewPacket(testPacketVXLAN, LinkTypeEthernet, gopacket.NoCopy)
85         }
86 }
87
88 func TestIsomorphicPacketVXLAN(t *testing.T) {
89         vx := &VXLAN{
90                 ValidIDFlag:      true,
91                 VNI:              255,
92                 GBPExtension:     true,
93                 GBPApplied:       true,
94                 GBPDontLearn:     true,
95                 GBPGroupPolicyID: 777,
96         }
97
98         b := gopacket.NewSerializeBuffer()
99         vx.SerializeTo(b, gopacket.SerializeOptions{})
100
101         p := gopacket.NewPacket(b.Bytes(), gopacket.DecodeFunc(decodeVXLAN), gopacket.Default)
102         vxTranslated := p.Layer(LayerTypeVXLAN).(*VXLAN)
103         vxTranslated.BaseLayer = BaseLayer{}
104
105         if !reflect.DeepEqual(vx, vxTranslated) {
106                 t.Errorf("VXLAN isomorph mismatch, \nwant %#v\ngot %#v\n", vx, vxTranslated)
107         }
108 }