1 // Copyright 2014 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
12 "golang.org/x/net/icmp"
13 "golang.org/x/net/internal/iana"
14 "golang.org/x/net/ipv4"
15 "golang.org/x/net/ipv6"
18 var marshalAndParseMessageForIPv4Tests = []icmp.Message{
20 Type: ipv4.ICMPTypeDestinationUnreachable, Code: 15,
21 Body: &icmp.DstUnreach{
22 Data: []byte("ERROR-INVOKING-PACKET"),
26 Type: ipv4.ICMPTypeTimeExceeded, Code: 1,
27 Body: &icmp.TimeExceeded{
28 Data: []byte("ERROR-INVOKING-PACKET"),
32 Type: ipv4.ICMPTypeParameterProblem, Code: 2,
33 Body: &icmp.ParamProb{
35 Data: []byte("ERROR-INVOKING-PACKET"),
39 Type: ipv4.ICMPTypeEcho, Code: 0,
42 Data: []byte("HELLO-R-U-THERE"),
46 Type: ipv4.ICMPTypePhoturis,
47 Body: &icmp.DefaultMessageBody{
48 Data: []byte{0x80, 0x40, 0x20, 0x10},
53 func TestMarshalAndParseMessageForIPv4(t *testing.T) {
54 for i, tt := range marshalAndParseMessageForIPv4Tests {
55 b, err := tt.Marshal(nil)
59 m, err := icmp.ParseMessage(iana.ProtocolICMP, b)
63 if m.Type != tt.Type || m.Code != tt.Code {
64 t.Errorf("#%v: got %v; want %v", i, m, &tt)
66 if !reflect.DeepEqual(m.Body, tt.Body) {
67 t.Errorf("#%v: got %v; want %v", i, m.Body, tt.Body)
72 var marshalAndParseMessageForIPv6Tests = []icmp.Message{
74 Type: ipv6.ICMPTypeDestinationUnreachable, Code: 6,
75 Body: &icmp.DstUnreach{
76 Data: []byte("ERROR-INVOKING-PACKET"),
80 Type: ipv6.ICMPTypePacketTooBig, Code: 0,
81 Body: &icmp.PacketTooBig{
83 Data: []byte("ERROR-INVOKING-PACKET"),
87 Type: ipv6.ICMPTypeTimeExceeded, Code: 1,
88 Body: &icmp.TimeExceeded{
89 Data: []byte("ERROR-INVOKING-PACKET"),
93 Type: ipv6.ICMPTypeParameterProblem, Code: 2,
94 Body: &icmp.ParamProb{
96 Data: []byte("ERROR-INVOKING-PACKET"),
100 Type: ipv6.ICMPTypeEchoRequest, Code: 0,
103 Data: []byte("HELLO-R-U-THERE"),
107 Type: ipv6.ICMPTypeDuplicateAddressConfirmation,
108 Body: &icmp.DefaultMessageBody{
109 Data: []byte{0x80, 0x40, 0x20, 0x10},
114 func TestMarshalAndParseMessageForIPv6(t *testing.T) {
115 pshicmp := icmp.IPv6PseudoHeader(net.ParseIP("fe80::1"), net.ParseIP("ff02::1"))
116 for i, tt := range marshalAndParseMessageForIPv6Tests {
117 for _, psh := range [][]byte{pshicmp, nil} {
118 b, err := tt.Marshal(psh)
122 m, err := icmp.ParseMessage(iana.ProtocolIPv6ICMP, b)
126 if m.Type != tt.Type || m.Code != tt.Code {
127 t.Errorf("#%v: got %v; want %v", i, m, &tt)
129 if !reflect.DeepEqual(m.Body, tt.Body) {
130 t.Errorf("#%v: got %v; want %v", i, m.Body, tt.Body)