github.com/gopacket/gopacket@v1.1.0/layers/icmp6_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  	"net"
    12  	"reflect"
    13  	"testing"
    14  
    15  	"github.com/gopacket/gopacket"
    16  )
    17  
    18  // testPacketICMPv6 is the packet:
    19  //
    20  //	10:48:30.088384 IP6 2620:0:1005:0:26be:5ff:fe27:b17 > fe80::21f:caff:feb3:7640: ICMP6, neighbor advertisement, tgt is 2620:0:1005:0:26be:5ff:fe27:b17, length 24
    21  //	   0x0000:  001f cab3 7640 24be 0527 0b17 86dd 6000  ....v@$..'....`.
    22  //	   0x0010:  0000 0018 3aff 2620 0000 1005 0000 26be  ....:.&.......&.
    23  //	   0x0020:  05ff fe27 0b17 fe80 0000 0000 0000 021f  ...'............
    24  //	   0x0030:  caff feb3 7640 8800 1ed6 4000 0000 2620  ....v@....@...&.
    25  //	   0x0040:  0000 1005 0000 26be 05ff fe27 0b17       ......&....'..
    26  var testPacketICMPv6 = []byte{
    27  	0x00, 0x1f, 0xca, 0xb3, 0x76, 0x40, 0x24, 0xbe, 0x05, 0x27, 0x0b, 0x17, 0x86, 0xdd, 0x60, 0x00,
    28  	0x00, 0x00, 0x00, 0x18, 0x3a, 0xff, 0x26, 0x20, 0x00, 0x00, 0x10, 0x05, 0x00, 0x00, 0x26, 0xbe,
    29  	0x05, 0xff, 0xfe, 0x27, 0x0b, 0x17, 0xfe, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0x1f,
    30  	0xca, 0xff, 0xfe, 0xb3, 0x76, 0x40, 0x88, 0x00, 0x1e, 0xd6, 0x40, 0x00, 0x00, 0x00, 0x26, 0x20,
    31  	0x00, 0x00, 0x10, 0x05, 0x00, 0x00, 0x26, 0xbe, 0x05, 0xff, 0xfe, 0x27, 0x0b, 0x17,
    32  }
    33  
    34  func TestPacketICMPv6(t *testing.T) {
    35  	p := gopacket.NewPacket(testPacketICMPv6, LinkTypeEthernet, gopacket.Default)
    36  	if p.ErrorLayer() != nil {
    37  		t.Error("Failed to decode packet:", p.ErrorLayer().Error())
    38  	}
    39  	checkLayers(p, []gopacket.LayerType{LayerTypeEthernet, LayerTypeIPv6, LayerTypeICMPv6, gopacket.LayerTypePayload}, t)
    40  	checkSerialization(p, t)
    41  
    42  	if got, ok := p.Layer(LayerTypeIPv6).(*IPv6); ok {
    43  		want := &IPv6{
    44  			BaseLayer: BaseLayer{
    45  				Contents: []byte{0x60, 0x0, 0x0, 0x0, 0x0, 0x18,
    46  					0x3a, 0xff, 0x26, 0x20, 0x0, 0x0, 0x10, 0x5, 0x0, 0x0, 0x26, 0xbe, 0x5,
    47  					0xff, 0xfe, 0x27, 0xb, 0x17, 0xfe, 0x80, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
    48  					0x2, 0x1f, 0xca, 0xff, 0xfe, 0xb3, 0x76, 0x40},
    49  				Payload: []byte{0x88, 0x0, 0x1e, 0xd6, 0x40, 0x0, 0x0, 0x0, 0x26, 0x20,
    50  					0x0, 0x0, 0x10, 0x5, 0x0, 0x0, 0x26, 0xbe, 0x5, 0xff, 0xfe, 0x27, 0xb,
    51  					0x17},
    52  			},
    53  			Version:      6,
    54  			TrafficClass: 0,
    55  			FlowLabel:    0,
    56  			Length:       24,
    57  			NextHeader:   IPProtocolICMPv6,
    58  			HopLimit:     255,
    59  			SrcIP:        net.IP{0x26, 0x20, 0x0, 0x0, 0x10, 0x5, 0x0, 0x0, 0x26, 0xbe, 0x5, 0xff, 0xfe, 0x27, 0xb, 0x17},
    60  			DstIP:        net.IP{0xfe, 0x80, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x2, 0x1f, 0xca, 0xff, 0xfe, 0xb3, 0x76, 0x40},
    61  		}
    62  		if !reflect.DeepEqual(got, want) {
    63  			t.Errorf("IPv6 packet processing failed:\ngot  :\n%#v\n\nwant :\n%#v\n\n", got, want)
    64  		}
    65  	} else {
    66  		t.Error("No IPv6 layer type found in packet")
    67  	}
    68  	if got, ok := p.Layer(LayerTypeICMPv6).(*ICMPv6); ok {
    69  		want := &ICMPv6{
    70  			BaseLayer: BaseLayer{
    71  				Contents: []byte{0x88, 0x0, 0x1e, 0xd6},
    72  				Payload: []byte{0x40, 0x0, 0x0, 0x0, 0x26, 0x20, 0x0, 0x0, 0x10,
    73  					0x5, 0x0, 0x0, 0x26, 0xbe, 0x5, 0xff, 0xfe, 0x27, 0xb, 0x17},
    74  			},
    75  			TypeCode: 0x8800,
    76  			Checksum: 0x1ed6,
    77  		}
    78  		if !reflect.DeepEqual(got, want) {
    79  			t.Errorf("ICMPv6 packet processing failed:\ngot  :\n%#v\n\nwant :\n%#v\n\n", got, want)
    80  		}
    81  		if got.TypeCode.String() != "NeighborAdvertisement" {
    82  			t.Errorf("ICMPv6 type code, got %q want 'NeighborAdvertisement'", got.TypeCode.String())
    83  		}
    84  	} else {
    85  		t.Error("No ICMPv6 layer type found in packet")
    86  	}
    87  }