github.com/gopacket/gopacket@v1.1.0/layers/vxlan_test.go (about)

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