summaryrefslogtreecommitdiffstats
path: root/vendor/golang.org/x/net/icmp/ipv4_test.go
blob: 47cc00d07652ab92b8ef9b02727b5c61b41b42e2 (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
// 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

import (
	"encoding/binary"
	"net"
	"reflect"
	"runtime"
	"testing"

	"golang.org/x/net/ipv4"
)

type ipv4HeaderTest struct {
	wireHeaderFromKernel        [ipv4.HeaderLen]byte
	wireHeaderFromTradBSDKernel [ipv4.HeaderLen]byte
	Header                      *ipv4.Header
}

var ipv4HeaderLittleEndianTest = ipv4HeaderTest{
	// TODO(mikio): Add platform dependent wire header formats when
	// we support new platforms.
	wireHeaderFromKernel: [ipv4.HeaderLen]byte{
		0x45, 0x01, 0xbe, 0xef,
		0xca, 0xfe, 0x45, 0xdc,
		0xff, 0x01, 0xde, 0xad,
		172, 16, 254, 254,
		192, 168, 0, 1,
	},
	wireHeaderFromTradBSDKernel: [ipv4.HeaderLen]byte{
		0x45, 0x01, 0xef, 0xbe,
		0xca, 0xfe, 0x45, 0xdc,
		0xff, 0x01, 0xde, 0xad,
		172, 16, 254, 254,
		192, 168, 0, 1,
	},
	Header: &ipv4.Header{
		Version:  ipv4.Version,
		Len:      ipv4.HeaderLen,
		TOS:      1,
		TotalLen: 0xbeef,
		ID:       0xcafe,
		Flags:    ipv4.DontFragment,
		FragOff:  1500,
		TTL:      255,
		Protocol: 1,
		Checksum: 0xdead,
		Src:      net.IPv4(172, 16, 254, 254),
		Dst:      net.IPv4(192, 168, 0, 1),
	},
}

func TestParseIPv4Header(t *testing.T) {
	tt := &ipv4HeaderLittleEndianTest
	if nativeEndian != binary.LittleEndian {
		t.Skip("no test for non-little endian machine yet")
	}

	var wh []byte
	switch runtime.GOOS {
	case "darwin":
		wh = tt.wireHeaderFromTradBSDKernel[:]
	case "freebsd":
		if freebsdVersion >= 1000000 {
			wh = tt.wireHeaderFromKernel[:]
		} else {
			wh = tt.wireHeaderFromTradBSDKernel[:]
		}
	default:
		wh = tt.wireHeaderFromKernel[:]
	}
	h, err := ParseIPv4Header(wh)
	if err != nil {
		t.Fatal(err)
	}
	if !reflect.DeepEqual(h, tt.Header) {
		t.Fatalf("got %#v; want %#v", h, tt.Header)
	}
}