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