Browse Source

ipv4: make use of nettest.SupportsRawIPSocket

Change-Id: I12b41d16b49415081242e06b25a7106a4a0c0442
Reviewed-on: https://go-review.googlesource.com/3402
Reviewed-by: Ian Lance Taylor <iant@golang.org>
Mikio Hara 11 years ago
parent
commit
99928beb6b

+ 3 - 3
ipv4/icmp_test.go

@@ -6,11 +6,11 @@ package ipv4_test
 
 import (
 	"net"
-	"os"
 	"reflect"
 	"runtime"
 	"testing"
 
+	"golang.org/x/net/internal/nettest"
 	"golang.org/x/net/ipv4"
 )
 
@@ -66,8 +66,8 @@ func TestSetICMPFilter(t *testing.T) {
 	default:
 		t.Skipf("not supported on %q", runtime.GOOS)
 	}
-	if os.Getuid() != 0 {
-		t.Skip("must be root")
+	if m, ok := nettest.SupportsRawIPSocket(); !ok {
+		t.Skip(m)
 	}
 
 	c, err := net.ListenPacket("ip4:icmp", "127.0.0.1")

+ 4 - 4
ipv4/multicast_test.go

@@ -122,8 +122,8 @@ func TestPacketConnReadWriteMulticastICMP(t *testing.T) {
 	case "nacl", "plan9", "solaris", "windows":
 		t.Skipf("not supported on %q", runtime.GOOS)
 	}
-	if os.Getuid() != 0 {
-		t.Skip("must be root")
+	if m, ok := nettest.SupportsRawIPSocket(); !ok {
+		t.Skip(m)
 	}
 	ifi := nettest.RoutedInterface("ip4", net.FlagUp|net.FlagMulticast|net.FlagLoopback)
 	if ifi == nil {
@@ -229,8 +229,8 @@ func TestRawConnReadWriteMulticastICMP(t *testing.T) {
 	if testing.Short() {
 		t.Skip("to avoid external network")
 	}
-	if os.Getuid() != 0 {
-		t.Skip("must be root")
+	if m, ok := nettest.SupportsRawIPSocket(); !ok {
+		t.Skip(m)
 	}
 	ifi := nettest.RoutedInterface("ip4", net.FlagUp|net.FlagMulticast|net.FlagLoopback)
 	if ifi == nil {

+ 4 - 5
ipv4/multicastlistener_test.go

@@ -6,7 +6,6 @@ package ipv4_test
 
 import (
 	"net"
-	"os"
 	"runtime"
 	"testing"
 
@@ -163,8 +162,8 @@ func TestIPSingleRawConnWithSingleGroupListener(t *testing.T) {
 	if testing.Short() {
 		t.Skip("to avoid external network")
 	}
-	if os.Getuid() != 0 {
-		t.Skip("must be root")
+	if m, ok := nettest.SupportsRawIPSocket(); !ok {
+		t.Skip(m)
 	}
 
 	c, err := net.ListenPacket("ip4:icmp", "0.0.0.0") // wildcard address
@@ -208,8 +207,8 @@ func TestIPPerInterfaceSingleRawConnWithSingleGroupListener(t *testing.T) {
 	if testing.Short() {
 		t.Skip("to avoid external network")
 	}
-	if os.Getuid() != 0 {
-		t.Skip("must be root")
+	if m, ok := nettest.SupportsRawIPSocket(); !ok {
+		t.Skip(m)
 	}
 
 	gaddr := net.IPAddr{IP: net.IPv4(224, 0, 0, 254)} // see RFC 4727

+ 5 - 5
ipv4/multicastsockopt_test.go

@@ -6,7 +6,6 @@ package ipv4_test
 
 import (
 	"net"
-	"os"
 	"runtime"
 	"testing"
 
@@ -35,9 +34,10 @@ func TestPacketConnMulticastSocketOptions(t *testing.T) {
 		t.Skipf("not available on %q", runtime.GOOS)
 	}
 
+	m, ok := nettest.SupportsRawIPSocket()
 	for _, tt := range packetConnMulticastSocketOptionTests {
-		if tt.net == "ip4" && os.Getuid() != 0 {
-			t.Log("must be root")
+		if tt.net == "ip4" && !ok {
+			t.Log(m)
 			continue
 		}
 		c, err := net.ListenPacket(tt.net+tt.proto, tt.addr)
@@ -69,8 +69,8 @@ func TestRawConnMulticastSocketOptions(t *testing.T) {
 	case "nacl", "plan9", "solaris":
 		t.Skipf("not supported on %q", runtime.GOOS)
 	}
-	if os.Getuid() != 0 {
-		t.Skip("must be root")
+	if m, ok := nettest.SupportsRawIPSocket(); !ok {
+		t.Skip(m)
 	}
 	ifi := nettest.RoutedInterface("ip4", net.FlagUp|net.FlagMulticast|net.FlagLoopback)
 	if ifi == nil {

+ 4 - 4
ipv4/unicast_test.go

@@ -79,8 +79,8 @@ func TestPacketConnReadWriteUnicastICMP(t *testing.T) {
 	case "nacl", "plan9", "solaris", "windows":
 		t.Skipf("not supported on %q", runtime.GOOS)
 	}
-	if os.Getuid() != 0 {
-		t.Skip("must be root")
+	if m, ok := nettest.SupportsRawIPSocket(); !ok {
+		t.Skip(m)
 	}
 	ifi := nettest.RoutedInterface("ip4", net.FlagUp|net.FlagLoopback)
 	if ifi == nil {
@@ -162,8 +162,8 @@ func TestRawConnReadWriteUnicastICMP(t *testing.T) {
 	case "nacl", "plan9", "solaris", "windows":
 		t.Skipf("not supported on %q", runtime.GOOS)
 	}
-	if os.Getuid() != 0 {
-		t.Skip("must be root")
+	if m, ok := nettest.SupportsRawIPSocket(); !ok {
+		t.Skip(m)
 	}
 	ifi := nettest.RoutedInterface("ip4", net.FlagUp|net.FlagLoopback)
 	if ifi == nil {

+ 5 - 5
ipv4/unicastsockopt_test.go

@@ -6,7 +6,6 @@ package ipv4_test
 
 import (
 	"net"
-	"os"
 	"runtime"
 	"testing"
 
@@ -62,9 +61,10 @@ func TestPacketConnUnicastSocketOptions(t *testing.T) {
 		t.Skipf("not available on %q", runtime.GOOS)
 	}
 
+	m, ok := nettest.SupportsRawIPSocket()
 	for _, tt := range packetConnUnicastSocketOptionTests {
-		if tt.net == "ip4" && os.Getuid() != 0 {
-			t.Log("must be root")
+		if tt.net == "ip4" && !ok {
+			t.Log(m)
 			continue
 		}
 		c, err := net.ListenPacket(tt.net+tt.proto, tt.addr)
@@ -82,8 +82,8 @@ func TestRawConnUnicastSocketOptions(t *testing.T) {
 	case "nacl", "plan9", "solaris":
 		t.Skipf("not supported on %q", runtime.GOOS)
 	}
-	if os.Getuid() != 0 {
-		t.Skip("must be root")
+	if m, ok := nettest.SupportsRawIPSocket(); !ok {
+		t.Skip(m)
 	}
 	ifi := nettest.RoutedInterface("ip4", net.FlagUp|net.FlagLoopback)
 	if ifi == nil {