author | Mikio Hara
<mikioh.mikioh@gmail.com> 2016-04-30 07:13:37 UTC |
committer | Mikio Hara
<mikioh.mikioh@gmail.com> 2016-05-05 21:57:10 UTC |
parent | 35ec611a141ee705590b9eb64d673f9e6dfeb1ac |
icmp/ipv4_test.go | +28 | -17 |
diff --git a/icmp/ipv4_test.go b/icmp/ipv4_test.go index b05c697..47cc00d 100644 --- a/icmp/ipv4_test.go +++ b/icmp/ipv4_test.go @@ -5,6 +5,7 @@ package icmp import ( + "encoding/binary" "net" "reflect" "runtime" @@ -13,25 +14,30 @@ import ( "golang.org/x/net/ipv4" ) -var ( - wireHeaderFromKernel = [ipv4.HeaderLen]byte{ +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{ + }, + wireHeaderFromTradBSDKernel: [ipv4.HeaderLen]byte{ 0x45, 0x01, 0xef, 0xbe, 0xca, 0xfe, 0x45, 0xdc, 0xff, 0x01, 0xde, 0xad, 172, 16, 254, 254, 192, 168, 0, 1, - } - // TODO(mikio): Add platform dependent wire header formats when - // we support new platforms. - - testHeader = &ipv4.Header{ + }, + Header: &ipv4.Header{ Version: ipv4.Version, Len: ipv4.HeaderLen, TOS: 1, @@ -44,28 +50,33 @@ var ( 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 = wireHeaderFromTradBSDKernel[:] + wh = tt.wireHeaderFromTradBSDKernel[:] case "freebsd": if freebsdVersion >= 1000000 { - wh = wireHeaderFromKernel[:] + wh = tt.wireHeaderFromKernel[:] } else { - wh = wireHeaderFromTradBSDKernel[:] + wh = tt.wireHeaderFromTradBSDKernel[:] } default: - wh = wireHeaderFromKernel[:] + wh = tt.wireHeaderFromKernel[:] } h, err := ParseIPv4Header(wh) if err != nil { t.Fatal(err) } - if !reflect.DeepEqual(h, testHeader) { - t.Fatalf("got %#v; want %#v", h, testHeader) + if !reflect.DeepEqual(h, tt.Header) { + t.Fatalf("got %#v; want %#v", h, tt.Header) } }