Commit 180da80e authored by Mikio Hara's avatar Mikio Hara

net: fix dial to raw IP networks on Windows

Also avoids platform-dependent datagram truncation in raw IP tests.
At least it's different between Windows and others.

Fixes #6122.

R=alex.brainman
CC=golang-dev
https://golang.org/cl/12843043
parent 910a6faa
...@@ -75,7 +75,8 @@ func closesocket(s syscall.Handle) error { ...@@ -75,7 +75,8 @@ func closesocket(s syscall.Handle) error {
} }
func canUseConnectEx(net string) bool { func canUseConnectEx(net string) bool {
if net == "udp" || net == "udp4" || net == "udp6" { switch net {
case "udp", "udp4", "udp6", "ip", "ip4", "ip6":
// ConnectEx windows API does not support connectionless sockets. // ConnectEx windows API does not support connectionless sockets.
return false return false
} }
......
...@@ -10,6 +10,7 @@ import ( ...@@ -10,6 +10,7 @@ import (
"fmt" "fmt"
"os" "os"
"reflect" "reflect"
"runtime"
"testing" "testing"
"time" "time"
) )
...@@ -73,8 +74,14 @@ var icmpEchoTests = []struct { ...@@ -73,8 +74,14 @@ var icmpEchoTests = []struct {
} }
func TestConnICMPEcho(t *testing.T) { func TestConnICMPEcho(t *testing.T) {
if os.Getuid() != 0 { switch runtime.GOOS {
t.Skip("skipping test; must be root") case "plan9":
t.Skipf("skipping test on %q", runtime.GOOS)
case "windows":
default:
if os.Getuid() != 0 {
t.Skip("skipping test; must be root")
}
} }
for i, tt := range icmpEchoTests { for i, tt := range icmpEchoTests {
...@@ -98,7 +105,7 @@ func TestConnICMPEcho(t *testing.T) { ...@@ -98,7 +105,7 @@ func TestConnICMPEcho(t *testing.T) {
typ = icmpv6EchoRequest typ = icmpv6EchoRequest
} }
xid, xseq := os.Getpid()&0xffff, i+1 xid, xseq := os.Getpid()&0xffff, i+1
b, err := (&icmpMessage{ wb, err := (&icmpMessage{
Type: typ, Code: 0, Type: typ, Code: 0,
Body: &icmpEcho{ Body: &icmpEcho{
ID: xid, Seq: xseq, ID: xid, Seq: xseq,
...@@ -108,18 +115,19 @@ func TestConnICMPEcho(t *testing.T) { ...@@ -108,18 +115,19 @@ func TestConnICMPEcho(t *testing.T) {
if err != nil { if err != nil {
t.Fatalf("icmpMessage.Marshal failed: %v", err) t.Fatalf("icmpMessage.Marshal failed: %v", err)
} }
if _, err := c.Write(b); err != nil { if _, err := c.Write(wb); err != nil {
t.Fatalf("Conn.Write failed: %v", err) t.Fatalf("Conn.Write failed: %v", err)
} }
var m *icmpMessage var m *icmpMessage
rb := make([]byte, 20+len(wb))
for { for {
if _, err := c.Read(b); err != nil { if _, err := c.Read(rb); err != nil {
t.Fatalf("Conn.Read failed: %v", err) t.Fatalf("Conn.Read failed: %v", err)
} }
if net == "ip4" { if net == "ip4" {
b = ipv4Payload(b) rb = ipv4Payload(rb)
} }
if m, err = parseICMPMessage(b); err != nil { if m, err = parseICMPMessage(rb); err != nil {
t.Fatalf("parseICMPMessage failed: %v", err) t.Fatalf("parseICMPMessage failed: %v", err)
} }
switch m.Type { switch m.Type {
...@@ -140,8 +148,14 @@ func TestConnICMPEcho(t *testing.T) { ...@@ -140,8 +148,14 @@ func TestConnICMPEcho(t *testing.T) {
} }
func TestPacketConnICMPEcho(t *testing.T) { func TestPacketConnICMPEcho(t *testing.T) {
if os.Getuid() != 0 { switch runtime.GOOS {
t.Skip("skipping test; must be root") case "plan9":
t.Skipf("skipping test on %q", runtime.GOOS)
case "windows":
default:
if os.Getuid() != 0 {
t.Skip("skipping test; must be root")
}
} }
for i, tt := range icmpEchoTests { for i, tt := range icmpEchoTests {
...@@ -169,7 +183,7 @@ func TestPacketConnICMPEcho(t *testing.T) { ...@@ -169,7 +183,7 @@ func TestPacketConnICMPEcho(t *testing.T) {
typ = icmpv6EchoRequest typ = icmpv6EchoRequest
} }
xid, xseq := os.Getpid()&0xffff, i+1 xid, xseq := os.Getpid()&0xffff, i+1
b, err := (&icmpMessage{ wb, err := (&icmpMessage{
Type: typ, Code: 0, Type: typ, Code: 0,
Body: &icmpEcho{ Body: &icmpEcho{
ID: xid, Seq: xseq, ID: xid, Seq: xseq,
...@@ -179,19 +193,20 @@ func TestPacketConnICMPEcho(t *testing.T) { ...@@ -179,19 +193,20 @@ func TestPacketConnICMPEcho(t *testing.T) {
if err != nil { if err != nil {
t.Fatalf("icmpMessage.Marshal failed: %v", err) t.Fatalf("icmpMessage.Marshal failed: %v", err)
} }
if _, err := c.WriteTo(b, ra); err != nil { if _, err := c.WriteTo(wb, ra); err != nil {
t.Fatalf("PacketConn.WriteTo failed: %v", err) t.Fatalf("PacketConn.WriteTo failed: %v", err)
} }
var m *icmpMessage var m *icmpMessage
rb := make([]byte, 20+len(wb))
for { for {
if _, _, err := c.ReadFrom(b); err != nil { if _, _, err := c.ReadFrom(rb); err != nil {
t.Fatalf("PacketConn.ReadFrom failed: %v", err) t.Fatalf("PacketConn.ReadFrom failed: %v", err)
} }
// TODO: fix issue 3944 // TODO: fix issue 3944
//if net == "ip4" { //if net == "ip4" {
// b = ipv4Payload(b) // rb = ipv4Payload(rb)
//} //}
if m, err = parseICMPMessage(b); err != nil { if m, err = parseICMPMessage(rb); err != nil {
t.Fatalf("parseICMPMessage failed: %v", err) t.Fatalf("parseICMPMessage failed: %v", err)
} }
switch m.Type { switch m.Type {
...@@ -338,8 +353,13 @@ var ipConnLocalNameTests = []struct { ...@@ -338,8 +353,13 @@ var ipConnLocalNameTests = []struct {
} }
func TestIPConnLocalName(t *testing.T) { func TestIPConnLocalName(t *testing.T) {
if os.Getuid() != 0 { switch runtime.GOOS {
t.Skip("skipping test; must be root") case "plan9", "windows":
t.Skipf("skipping test on %q", runtime.GOOS)
default:
if os.Getuid() != 0 {
t.Skip("skipping test; must be root")
}
} }
for _, tt := range ipConnLocalNameTests { for _, tt := range ipConnLocalNameTests {
...@@ -355,8 +375,13 @@ func TestIPConnLocalName(t *testing.T) { ...@@ -355,8 +375,13 @@ func TestIPConnLocalName(t *testing.T) {
} }
func TestIPConnRemoteName(t *testing.T) { func TestIPConnRemoteName(t *testing.T) {
if os.Getuid() != 0 { switch runtime.GOOS {
t.Skip("skipping test; must be root") case "plan9", "windows":
t.Skipf("skipping test on %q", runtime.GOOS)
default:
if os.Getuid() != 0 {
t.Skip("skipping test; must be root")
}
} }
raddr := &IPAddr{IP: IPv4(127, 0, 0, 10).To4()} raddr := &IPAddr{IP: IPv4(127, 0, 0, 10).To4()}
......
...@@ -176,9 +176,11 @@ func TestIPConnSpecificMethods(t *testing.T) { ...@@ -176,9 +176,11 @@ func TestIPConnSpecificMethods(t *testing.T) {
switch runtime.GOOS { switch runtime.GOOS {
case "plan9": case "plan9":
t.Skipf("skipping test on %q", runtime.GOOS) t.Skipf("skipping test on %q", runtime.GOOS)
} case "windows":
if os.Getuid() != 0 { default:
t.Skipf("skipping test; must be root") if os.Getuid() != 0 {
t.Skipf("skipping test; must be root")
}
} }
la, err := ResolveIPAddr("ip4", "127.0.0.1") la, err := ResolveIPAddr("ip4", "127.0.0.1")
...@@ -208,7 +210,7 @@ func TestIPConnSpecificMethods(t *testing.T) { ...@@ -208,7 +210,7 @@ func TestIPConnSpecificMethods(t *testing.T) {
if err != nil { if err != nil {
t.Fatalf("icmpMessage.Marshal failed: %v", err) t.Fatalf("icmpMessage.Marshal failed: %v", err)
} }
rb := make([]byte, 20+128) rb := make([]byte, 20+len(wb))
if _, err := c.WriteToIP(wb, c.LocalAddr().(*IPAddr)); err != nil { if _, err := c.WriteToIP(wb, c.LocalAddr().(*IPAddr)); err != nil {
t.Fatalf("IPConn.WriteToIP failed: %v", err) t.Fatalf("IPConn.WriteToIP failed: %v", err)
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment