summaryrefslogtreecommitdiffstats
path: root/vendor/golang.org/x/net/icmp/message_test.go
blob: 5d2605f8d1e69e04dce63e0c3d4a52b942df2879 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
// Copyright 2014 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package icmp_test

import (
	"net"
	"reflect"
	"testing"

	"golang.org/x/net/icmp"
	"golang.org/x/net/internal/iana"
	"golang.org/x/net/ipv4"
	"golang.org/x/net/ipv6"
)

var marshalAndParseMessageForIPv4Tests = []icmp.Message{
	{
		Type: ipv4.ICMPTypeDestinationUnreachable, Code: 15,
		Body: &icmp.DstUnreach{
			Data: []byte("ERROR-INVOKING-PACKET"),
		},
	},
	{
		Type: ipv4.ICMPTypeTimeExceeded, Code: 1,
		Body: &icmp.TimeExceeded{
			Data: []byte("ERROR-INVOKING-PACKET"),
		},
	},
	{
		Type: ipv4.ICMPTypeParameterProblem, Code: 2,
		Body: &icmp.ParamProb{
			Pointer: 8,
			Data:    []byte("ERROR-INVOKING-PACKET"),
		},
	},
	{
		Type: ipv4.ICMPTypeEcho, Code: 0,
		Body: &icmp.Echo{
			ID: 1, Seq: 2,
			Data: []byte("HELLO-R-U-THERE"),
		},
	},
	{
		Type: ipv4.ICMPTypePhoturis,
		Body: &icmp.DefaultMessageBody{
			Data: []byte{0x80, 0x40, 0x20, 0x10},
		},
	},
}

func TestMarshalAndParseMessageForIPv4(t *testing.T) {
	for i, tt := range marshalAndParseMessageForIPv4Tests {
		b, err := tt.Marshal(nil)
		if err != nil {
			t.Fatal(err)
		}
		m, err := icmp.ParseMessage(iana.ProtocolICMP, b)
		if err != nil {
			t.Fatal(err)
		}
		if m.Type != tt.Type || m.Code != tt.Code {
			t.Errorf("#%v: got %v; want %v", i, m, &tt)
		}
		if !reflect.DeepEqual(m.Body, tt.Body) {
			t.Errorf("#%v: got %v; want %v", i, m.Body, tt.Body)
		}
	}
}

var marshalAndParseMessageForIPv6Tests = []icmp.Message{
	{
		Type: ipv6.ICMPTypeDestinationUnreachable, Code: 6,
		Body: &icmp.DstUnreach{
			Data: []byte("ERROR-INVOKING-PACKET"),
		},
	},
	{
		Type: ipv6.ICMPTypePacketTooBig, Code: 0,
		Body: &icmp.PacketTooBig{
			MTU:  1<<16 - 1,
			Data: []byte("ERROR-INVOKING-PACKET"),
		},
	},
	{
		Type: ipv6.ICMPTypeTimeExceeded, Code: 1,
		Body: &icmp.TimeExceeded{
			Data: []byte("ERROR-INVOKING-PACKET"),
		},
	},
	{
		Type: ipv6.ICMPTypeParameterProblem, Code: 2,
		Body: &icmp.ParamProb{
			Pointer: 8,
			Data:    []byte("ERROR-INVOKING-PACKET"),
		},
	},
	{
		Type: ipv6.ICMPTypeEchoRequest, Code: 0,
		Body: &icmp.Echo{
			ID: 1, Seq: 2,
			Data: []byte("HELLO-R-U-THERE"),
		},
	},
	{
		Type: ipv6.ICMPTypeDuplicateAddressConfirmation,
		Body: &icmp.DefaultMessageBody{
			Data: []byte{0x80, 0x40, 0x20, 0x10},
		},
	},
}

func TestMarshalAndParseMessageForIPv6(t *testing.T) {
	pshicmp := icmp.IPv6PseudoHeader(net.ParseIP("fe80::1"), net.ParseIP("ff02::1"))
	for i, tt := range marshalAndParseMessageForIPv6Tests {
		for _, psh := range [][]byte{pshicmp, nil} {
			b, err := tt.Marshal(psh)
			if err != nil {
				t.Fatal(err)
			}
			m, err := icmp.ParseMessage(iana.ProtocolIPv6ICMP, b)
			if err != nil {
				t.Fatal(err)
			}
			if m.Type != tt.Type || m.Code != tt.Code {
				t.Errorf("#%v: got %v; want %v", i, m, &tt)
			}
			if !reflect.DeepEqual(m.Body, tt.Body) {
				t.Errorf("#%v: got %v; want %v", i, m.Body, tt.Body)
			}
		}
	}
}