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

     1  // Copyright 2012, Google, Inc. All rights reserved.
     2  // Copyright 2009-2011 Andreas Krennmair. All rights reserved.
     3  //
     4  // Use of this source code is governed by a BSD-style license
     5  // that can be found in the LICENSE file in the root of the source
     6  // tree.
     7  
     8  package layers
     9  
    10  import (
    11  	"testing"
    12  
    13  	"github.com/gopacket/gopacket"
    14  )
    15  
    16  var icmp6HopByHopData = []byte{
    17  	// Ethernet layer
    18  	0x33, 0x33, 0x00, 0x00, 0x00, 0x16, // destination
    19  	0x1e, 0xc3, 0xe3, 0xb7, 0xc4, 0xd5, //  source
    20  	0x86, 0xdd, // type IPv6
    21  
    22  	// IPv6 layer
    23  	0x60, 0x00, 0x00, 0x00, // version; traffic class; flow label
    24  	0x00, 0x88, // payload length?
    25  	0x00,                                                                                           // Next Header
    26  	0x01,                                                                                           // Hop Limit
    27  	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // source
    28  	0xff, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x16, // destination
    29  
    30  	// IPv6 Hop-by-hop option
    31  	0x3a,                               // Next Header - IPv6-ICMP
    32  	0x00,                               // Hdr Ext Len
    33  	0x05, 0x02, 0x00, 0x00, 0x01, 0x00, // Options and Padding
    34  
    35  	// ICMPv6 layer
    36  	0x8f, 0x00, // ICMP type 143, code 0
    37  
    38  	0x9e, 0xed, 0x00, 0x00, 0x00, 0x06, 0x03, 0x00,
    39  	0x00, 0x00, 0xff, 0x02, 0x00, 0x00, 0x00, 0x00,
    40  	0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0xff, 0xb7,
    41  	0xc4, 0xd5, 0x03, 0x00, 0x00, 0x00, 0xff, 0x02,
    42  	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    43  	0x00, 0x01, 0xff, 0x00, 0x00, 0x00, 0x04, 0x00,
    44  	0x00, 0x00, 0xff, 0x02, 0x00, 0x00, 0x00, 0x00,
    45  	0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0xff, 0x11,
    46  	0x00, 0x79, 0x04, 0x00, 0x00, 0x00, 0xff, 0x02,
    47  	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    48  	0x00, 0x01, 0xff, 0x00, 0x00, 0x01, 0x04, 0x00,
    49  	0x00, 0x00, 0xff, 0x05, 0x00, 0x00, 0x00, 0x00,
    50  	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    51  	0x00, 0x02, 0x04, 0x00, 0x00, 0x00, 0xff, 0x02,
    52  	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    53  	0x00, 0x00, 0x00, 0x00, 0x00, 0x02,
    54  }
    55  
    56  func TestPacketICMPv6WithHopByHop(t *testing.T) {
    57  	var ethLayerResp Ethernet
    58  	var ipV6LayerResp IPv6
    59  	var icmpLayerResp ICMPv6
    60  	var payload gopacket.Payload
    61  
    62  	parser := gopacket.NewDecodingLayerParser(LayerTypeEthernet, &ethLayerResp, &ipV6LayerResp, &icmpLayerResp, &payload)
    63  	parser.IgnoreUnsupported = true // avoid `No decoder for layer type ICMPv6RouterAdvertisement` error
    64  
    65  	respLayers := make([]gopacket.LayerType, 0)
    66  	err := parser.DecodeLayers(icmp6HopByHopData, &respLayers)
    67  
    68  	if err != nil {
    69  		t.Errorf("error decoding layers %s", err)
    70  		return
    71  	}
    72  
    73  	expectedType := uint8(icmp6HopByHopData[62])
    74  	actualType := uint8(icmpLayerResp.TypeCode.Type())
    75  	if expectedType != actualType {
    76  		t.Errorf("expected ICMP layer's TypeCode to be %d but was %d", expectedType, actualType)
    77  	}
    78  
    79  	p := gopacket.NewPacket(icmp6HopByHopData, LinkTypeEthernet, gopacket.Default)
    80  	if p.ErrorLayer() != nil {
    81  		t.Error("Failed to decode packet:", p.ErrorLayer().Error())
    82  	}
    83  	checkLayers(p, []gopacket.LayerType{LayerTypeEthernet, LayerTypeIPv6, LayerTypeIPv6HopByHop, LayerTypeICMPv6}, t)
    84  	// See https://github.com/google/gopacket/issues/517
    85  	// checkSerialization(p, t)
    86  }