git » go-net » commit 7e42c0e

ipv4: don't fail test on big endian machine

author Mikio Hara
2016-04-30 07:13:37 UTC
committer Mikio Hara
2016-05-05 21:58:21 UTC
parent 9723ea673d9da2c324b03f2504ff26da7f5255fa

ipv4: don't fail test on big endian machine

Change-Id: I7fc0634dd1c7cd49b0f3aa2d84a83e783f6687a1
Reviewed-on: https://go-review.googlesource.com/22678
Reviewed-by: Ian Lance Taylor <iant@golang.org>

ipv4/header_test.go +47 -28

diff --git a/ipv4/header_test.go b/ipv4/header_test.go
index ac89358..85cb9c4 100644
--- a/ipv4/header_test.go
+++ b/ipv4/header_test.go
@@ -6,6 +6,7 @@ package ipv4
 
 import (
 	"bytes"
+	"encoding/binary"
 	"net"
 	"reflect"
 	"runtime"
@@ -13,46 +14,54 @@ import (
 	"testing"
 )
 
-var (
-	wireHeaderFromKernel = [HeaderLen]byte{
+type headerTest struct {
+	wireHeaderFromKernel          [HeaderLen]byte
+	wireHeaderToKernel            [HeaderLen]byte
+	wireHeaderFromTradBSDKernel   [HeaderLen]byte
+	wireHeaderFromFreeBSD10Kernel [HeaderLen]byte
+	wireHeaderToTradBSDKernel     [HeaderLen]byte
+	*Header
+}
+
+var headerLittleEndianTest = headerTest{
+	// TODO(mikio): Add platform dependent wire header formats when
+	// we support new platforms.
+	wireHeaderFromKernel: [HeaderLen]byte{
 		0x45, 0x01, 0xbe, 0xef,
 		0xca, 0xfe, 0x45, 0xdc,
 		0xff, 0x01, 0xde, 0xad,
 		172, 16, 254, 254,
 		192, 168, 0, 1,
-	}
-	wireHeaderToKernel = [HeaderLen]byte{
+	},
+	wireHeaderToKernel: [HeaderLen]byte{
 		0x45, 0x01, 0xbe, 0xef,
 		0xca, 0xfe, 0x45, 0xdc,
 		0xff, 0x01, 0xde, 0xad,
 		172, 16, 254, 254,
 		192, 168, 0, 1,
-	}
-	wireHeaderFromTradBSDKernel = [HeaderLen]byte{
+	},
+	wireHeaderFromTradBSDKernel: [HeaderLen]byte{
 		0x45, 0x01, 0xdb, 0xbe,
 		0xca, 0xfe, 0xdc, 0x45,
 		0xff, 0x01, 0xde, 0xad,
 		172, 16, 254, 254,
 		192, 168, 0, 1,
-	}
-	wireHeaderFromFreeBSD10Kernel = [HeaderLen]byte{
+	},
+	wireHeaderFromFreeBSD10Kernel: [HeaderLen]byte{
 		0x45, 0x01, 0xef, 0xbe,
 		0xca, 0xfe, 0xdc, 0x45,
 		0xff, 0x01, 0xde, 0xad,
 		172, 16, 254, 254,
 		192, 168, 0, 1,
-	}
-	wireHeaderToTradBSDKernel = [HeaderLen]byte{
+	},
+	wireHeaderToTradBSDKernel: [HeaderLen]byte{
 		0x45, 0x01, 0xef, 0xbe,
 		0xca, 0xfe, 0xdc, 0x45,
 		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 = &Header{
+	},
+	Header: &Header{
 		Version:  Version,
 		Len:      HeaderLen,
 		TOS:      1,
@@ -65,26 +74,31 @@ var (
 		Checksum: 0xdead,
 		Src:      net.IPv4(172, 16, 254, 254),
 		Dst:      net.IPv4(192, 168, 0, 1),
-	}
-)
+	},
+}
 
 func TestMarshalHeader(t *testing.T) {
-	b, err := testHeader.Marshal()
+	tt := &headerLittleEndianTest
+	if nativeEndian != binary.LittleEndian {
+		t.Skip("no test for non-little endian machine yet")
+	}
+
+	b, err := tt.Header.Marshal()
 	if err != nil {
 		t.Fatal(err)
 	}
 	var wh []byte
 	switch runtime.GOOS {
 	case "darwin", "dragonfly", "netbsd":
-		wh = wireHeaderToTradBSDKernel[:]
+		wh = tt.wireHeaderToTradBSDKernel[:]
 	case "freebsd":
 		if freebsdVersion < 1000000 {
-			wh = wireHeaderToTradBSDKernel[:]
+			wh = tt.wireHeaderToTradBSDKernel[:]
 		} else {
-			wh = wireHeaderFromFreeBSD10Kernel[:]
+			wh = tt.wireHeaderFromFreeBSD10Kernel[:]
 		}
 	default:
-		wh = wireHeaderToKernel[:]
+		wh = tt.wireHeaderToKernel[:]
 	}
 	if !bytes.Equal(b, wh) {
 		t.Fatalf("got %#v; want %#v", b, wh)
@@ -92,25 +106,30 @@ func TestMarshalHeader(t *testing.T) {
 }
 
 func TestParseHeader(t *testing.T) {
+	tt := &headerLittleEndianTest
+	if nativeEndian != binary.LittleEndian {
+		t.Skip("no test for big endian machine yet")
+	}
+
 	var wh []byte
 	switch runtime.GOOS {
 	case "darwin", "dragonfly", "netbsd":
-		wh = wireHeaderFromTradBSDKernel[:]
+		wh = tt.wireHeaderFromTradBSDKernel[:]
 	case "freebsd":
 		if freebsdVersion < 1000000 {
-			wh = wireHeaderFromTradBSDKernel[:]
+			wh = tt.wireHeaderFromTradBSDKernel[:]
 		} else {
-			wh = wireHeaderFromFreeBSD10Kernel[:]
+			wh = tt.wireHeaderFromFreeBSD10Kernel[:]
 		}
 	default:
-		wh = wireHeaderFromKernel[:]
+		wh = tt.wireHeaderFromKernel[:]
 	}
 	h, err := ParseHeader(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)
 	}
 	s := h.String()
 	if strings.Contains(s, ",") {