github.com/gopacket/gopacket@v1.1.0/layers/mpls_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  // testPacketMPLS
    17  // Ethernet II, Src: cc:15:14:64:00:00 (cc:15:14:64:00:00), Dst: cc:13:14:64:00:01 (cc:13:14:64:00:01)
    18  // MultiProtocol Label Switching Header, Label: 17, Exp: 0, S: 0, TTL: 254
    19  // MultiProtocol Label Switching Header, Label: 19, Exp: 0, S: 1, TTL: 254
    20  // Internet Protocol Version 4, Src: 12.0.0.1, Dst: 2.2.2.2
    21  // Internet Control Message Protocol
    22  // 0000   cc 13 14 64 00 01 cc 15 14 64 00 00 88 47 00 01  ...d.....d...G..
    23  // 0010   10 fe 00 01 31 fe 45 00 00 64 00 39 00 00 fe 01  ....1.E..d.9....
    24  // 0020   ac 5b 0c 00 00 01 02 02 02 02 08 00 3a 6b 00 0b  .[..........:k..
    25  // 0030   00 02 00 00 00 00 00 3e 43 94 ab cd ab cd ab cd  .......>C.......
    26  // 0040   ab cd ab cd ab cd ab cd ab cd ab cd ab cd ab cd  ................
    27  // 0050   ab cd ab cd ab cd ab cd ab cd ab cd ab cd ab cd  ................
    28  // 0060   ab cd ab cd ab cd ab cd ab cd ab cd ab cd ab cd  ................
    29  // 0070   ab cd ab cd ab cd ab cd ab cd                    ..........
    30  
    31  var testPacketMPLS = []byte{
    32  	0xcc, 0x13, 0x14, 0x64, 0x00, 0x01, 0xcc, 0x15, 0x14, 0x64, 0x00, 0x00, 0x88, 0x47, 0x00, 0x01,
    33  	0x10, 0xfe, 0x00, 0x01, 0x31, 0xfe, 0x45, 0x00, 0x00, 0x64, 0x00, 0x39, 0x00, 0x00, 0xfe, 0x01,
    34  	0xac, 0x5b, 0x0c, 0x00, 0x00, 0x01, 0x02, 0x02, 0x02, 0x02, 0x08, 0x00, 0x3a, 0x6b, 0x00, 0x0b,
    35  	0x00, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x3e, 0x43, 0x94, 0xab, 0xcd, 0xab, 0xcd, 0xab, 0xcd,
    36  	0xab, 0xcd, 0xab, 0xcd, 0xab, 0xcd, 0xab, 0xcd, 0xab, 0xcd, 0xab, 0xcd, 0xab, 0xcd, 0xab, 0xcd,
    37  	0xab, 0xcd, 0xab, 0xcd, 0xab, 0xcd, 0xab, 0xcd, 0xab, 0xcd, 0xab, 0xcd, 0xab, 0xcd, 0xab, 0xcd,
    38  	0xab, 0xcd, 0xab, 0xcd, 0xab, 0xcd, 0xab, 0xcd, 0xab, 0xcd, 0xab, 0xcd, 0xab, 0xcd, 0xab, 0xcd,
    39  	0xab, 0xcd, 0xab, 0xcd, 0xab, 0xcd, 0xab, 0xcd, 0xab, 0xcd,
    40  }
    41  
    42  func TestPacketMPLS(t *testing.T) {
    43  	p := gopacket.NewPacket(testPacketMPLS, LinkTypeEthernet, gopacket.Default)
    44  	if p.ErrorLayer() != nil {
    45  		t.Error("Failed to decode packet:", p.ErrorLayer().Error())
    46  	}
    47  	checkLayers(p, []gopacket.LayerType{LayerTypeEthernet, LayerTypeMPLS, LayerTypeMPLS, LayerTypeIPv4, LayerTypeICMPv4, gopacket.LayerTypePayload}, t)
    48  	if got, ok := p.Layers()[1].(*MPLS); ok {
    49  		want := &MPLS{
    50  			BaseLayer: BaseLayer{
    51  				Contents: []byte{0x00, 0x01, 0x10, 0xfe},
    52  				Payload: []byte{0x00, 0x01, 0x31, 0xfe, 0x45, 0x00, 0x00, 0x64, 0x00, 0x39, 0x00, 0x00, 0xfe, 0x01,
    53  					0xac, 0x5b, 0x0c, 0x00, 0x00, 0x01, 0x02, 0x02, 0x02, 0x02, 0x08, 0x00, 0x3a, 0x6b, 0x00, 0x0b,
    54  					0x00, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x3e, 0x43, 0x94, 0xab, 0xcd, 0xab, 0xcd, 0xab, 0xcd,
    55  					0xab, 0xcd, 0xab, 0xcd, 0xab, 0xcd, 0xab, 0xcd, 0xab, 0xcd, 0xab, 0xcd, 0xab, 0xcd, 0xab, 0xcd,
    56  					0xab, 0xcd, 0xab, 0xcd, 0xab, 0xcd, 0xab, 0xcd, 0xab, 0xcd, 0xab, 0xcd, 0xab, 0xcd, 0xab, 0xcd,
    57  					0xab, 0xcd, 0xab, 0xcd, 0xab, 0xcd, 0xab, 0xcd, 0xab, 0xcd, 0xab, 0xcd, 0xab, 0xcd, 0xab, 0xcd,
    58  					0xab, 0xcd, 0xab, 0xcd, 0xab, 0xcd, 0xab, 0xcd, 0xab, 0xcd},
    59  			},
    60  			Label:        17,
    61  			TrafficClass: 0,
    62  			StackBottom:  false,
    63  			TTL:          254,
    64  		}
    65  		if !reflect.DeepEqual(want, got) {
    66  			t.Errorf("MPLS layer 1 mismatch, \nwant %#v\ngot %#v\n", want, got)
    67  		}
    68  	}
    69  	if got, ok := p.Layers()[2].(*MPLS); ok {
    70  		want := &MPLS{
    71  			BaseLayer: BaseLayer{
    72  				Contents: []byte{0x00, 0x01, 0x31, 0xfe},
    73  				Payload: []byte{0x45, 0x00, 0x00, 0x64, 0x00, 0x39, 0x00, 0x00, 0xfe, 0x01,
    74  					0xac, 0x5b, 0x0c, 0x00, 0x00, 0x01, 0x02, 0x02, 0x02, 0x02, 0x08, 0x00, 0x3a, 0x6b, 0x00, 0x0b,
    75  					0x00, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x3e, 0x43, 0x94, 0xab, 0xcd, 0xab, 0xcd, 0xab, 0xcd,
    76  					0xab, 0xcd, 0xab, 0xcd, 0xab, 0xcd, 0xab, 0xcd, 0xab, 0xcd, 0xab, 0xcd, 0xab, 0xcd, 0xab, 0xcd,
    77  					0xab, 0xcd, 0xab, 0xcd, 0xab, 0xcd, 0xab, 0xcd, 0xab, 0xcd, 0xab, 0xcd, 0xab, 0xcd, 0xab, 0xcd,
    78  					0xab, 0xcd, 0xab, 0xcd, 0xab, 0xcd, 0xab, 0xcd, 0xab, 0xcd, 0xab, 0xcd, 0xab, 0xcd, 0xab, 0xcd,
    79  					0xab, 0xcd, 0xab, 0xcd, 0xab, 0xcd, 0xab, 0xcd, 0xab, 0xcd},
    80  			},
    81  			Label:        19,
    82  			TrafficClass: 0,
    83  			StackBottom:  true,
    84  			TTL:          254,
    85  		}
    86  		if !reflect.DeepEqual(want, got) {
    87  			t.Errorf("MPLS layer 2 mismatch, \nwant %#v\ngot %#v\n", want, got)
    88  		}
    89  	}
    90  }
    91  
    92  func BenchmarkDecodePacketMPLS(b *testing.B) {
    93  	for i := 0; i < b.N; i++ {
    94  		gopacket.NewPacket(testPacketMPLS, LinkTypeEthernet, gopacket.NoCopy)
    95  	}
    96  }