clair/vendor/golang.org/x/net/icmp/ipv4_test.go

83 lines
1.8 KiB
Go
Raw Normal View History

2016-06-07 08:08:50 +00:00
// 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 (
2016-06-07 15:28:01 +00:00
"encoding/binary"
2016-06-07 08:08:50 +00:00
"net"
"reflect"
"runtime"
"testing"
"golang.org/x/net/ipv4"
)
2016-06-07 15:28:01 +00:00
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{
2016-06-07 08:08:50 +00:00
0x45, 0x01, 0xbe, 0xef,
0xca, 0xfe, 0x45, 0xdc,
0xff, 0x01, 0xde, 0xad,
172, 16, 254, 254,
192, 168, 0, 1,
2016-06-07 15:28:01 +00:00
},
wireHeaderFromTradBSDKernel: [ipv4.HeaderLen]byte{
2016-06-07 08:08:50 +00:00
0x45, 0x01, 0xef, 0xbe,
0xca, 0xfe, 0x45, 0xdc,
0xff, 0x01, 0xde, 0xad,
172, 16, 254, 254,
192, 168, 0, 1,
2016-06-07 15:28:01 +00:00
},
Header: &ipv4.Header{
2016-06-07 08:08:50 +00:00
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),
2016-06-07 15:28:01 +00:00
},
}
2016-06-07 08:08:50 +00:00
func TestParseIPv4Header(t *testing.T) {
2016-06-07 15:28:01 +00:00
tt := &ipv4HeaderLittleEndianTest
if nativeEndian != binary.LittleEndian {
t.Skip("no test for non-little endian machine yet")
}
2016-06-07 08:08:50 +00:00
var wh []byte
switch runtime.GOOS {
case "darwin":
2016-06-07 15:28:01 +00:00
wh = tt.wireHeaderFromTradBSDKernel[:]
2016-06-07 08:08:50 +00:00
case "freebsd":
if freebsdVersion >= 1000000 {
2016-06-07 15:28:01 +00:00
wh = tt.wireHeaderFromKernel[:]
2016-06-07 08:08:50 +00:00
} else {
2016-06-07 15:28:01 +00:00
wh = tt.wireHeaderFromTradBSDKernel[:]
2016-06-07 08:08:50 +00:00
}
default:
2016-06-07 15:28:01 +00:00
wh = tt.wireHeaderFromKernel[:]
2016-06-07 08:08:50 +00:00
}
h, err := ParseIPv4Header(wh)
if err != nil {
t.Fatal(err)
}
2016-06-07 15:28:01 +00:00
if !reflect.DeepEqual(h, tt.Header) {
t.Fatalf("got %#v; want %#v", h, tt.Header)
2016-06-07 08:08:50 +00:00
}
}