]> Cypherpunks repositories - gostls13.git/commitdiff
net: fix dial to raw IP networks on Windows
authorMikio Hara <mikioh.mikioh@gmail.com>
Fri, 23 Aug 2013 10:31:24 +0000 (19:31 +0900)
committerMikio Hara <mikioh.mikioh@gmail.com>
Fri, 23 Aug 2013 10:31:24 +0000 (19:31 +0900)
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

src/pkg/net/fd_windows.go
src/pkg/net/ipraw_test.go
src/pkg/net/protoconn_test.go

index 2f8fb4e03572b6a8186224ce8beee20c88331ebe..78c75657479f1019e0841d5ade4dedbf1b7e77f7 100644 (file)
@@ -75,7 +75,8 @@ func closesocket(s syscall.Handle) error {
 }
 
 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.
                return false
        }
index fce0830a36f7c30afce2ab4a07dd689d1cd3c733..1abaf885b29ca8d715e2320c92a4495365c51bc0 100644 (file)
@@ -10,6 +10,7 @@ import (
        "fmt"
        "os"
        "reflect"
+       "runtime"
        "testing"
        "time"
 )
@@ -73,8 +74,14 @@ var icmpEchoTests = []struct {
 }
 
 func TestConnICMPEcho(t *testing.T) {
-       if os.Getuid() != 0 {
-               t.Skip("skipping test; must be root")
+       switch runtime.GOOS {
+       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 {
@@ -98,7 +105,7 @@ func TestConnICMPEcho(t *testing.T) {
                        typ = icmpv6EchoRequest
                }
                xid, xseq := os.Getpid()&0xffff, i+1
-               b, err := (&icmpMessage{
+               wb, err := (&icmpMessage{
                        Type: typ, Code: 0,
                        Body: &icmpEcho{
                                ID: xid, Seq: xseq,
@@ -108,18 +115,19 @@ func TestConnICMPEcho(t *testing.T) {
                if err != nil {
                        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)
                }
                var m *icmpMessage
+               rb := make([]byte, 20+len(wb))
                for {
-                       if _, err := c.Read(b); err != nil {
+                       if _, err := c.Read(rb); err != nil {
                                t.Fatalf("Conn.Read failed: %v", err)
                        }
                        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)
                        }
                        switch m.Type {
@@ -140,8 +148,14 @@ func TestConnICMPEcho(t *testing.T) {
 }
 
 func TestPacketConnICMPEcho(t *testing.T) {
-       if os.Getuid() != 0 {
-               t.Skip("skipping test; must be root")
+       switch runtime.GOOS {
+       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 {
@@ -169,7 +183,7 @@ func TestPacketConnICMPEcho(t *testing.T) {
                        typ = icmpv6EchoRequest
                }
                xid, xseq := os.Getpid()&0xffff, i+1
-               b, err := (&icmpMessage{
+               wb, err := (&icmpMessage{
                        Type: typ, Code: 0,
                        Body: &icmpEcho{
                                ID: xid, Seq: xseq,
@@ -179,19 +193,20 @@ func TestPacketConnICMPEcho(t *testing.T) {
                if err != nil {
                        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)
                }
                var m *icmpMessage
+               rb := make([]byte, 20+len(wb))
                for {
-                       if _, _, err := c.ReadFrom(b); err != nil {
+                       if _, _, err := c.ReadFrom(rb); err != nil {
                                t.Fatalf("PacketConn.ReadFrom failed: %v", err)
                        }
                        // TODO: fix issue 3944
                        //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)
                        }
                        switch m.Type {
@@ -338,8 +353,13 @@ var ipConnLocalNameTests = []struct {
 }
 
 func TestIPConnLocalName(t *testing.T) {
-       if os.Getuid() != 0 {
-               t.Skip("skipping test; must be root")
+       switch runtime.GOOS {
+       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 {
@@ -355,8 +375,13 @@ func TestIPConnLocalName(t *testing.T) {
 }
 
 func TestIPConnRemoteName(t *testing.T) {
-       if os.Getuid() != 0 {
-               t.Skip("skipping test; must be root")
+       switch runtime.GOOS {
+       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()}
index 56f22da0712e1f21cffce0d2a59e81390377a077..3afa41b41137de0ca5531162febceea5cb191e85 100644 (file)
@@ -176,9 +176,11 @@ func TestIPConnSpecificMethods(t *testing.T) {
        switch runtime.GOOS {
        case "plan9":
                t.Skipf("skipping test on %q", runtime.GOOS)
-       }
-       if os.Getuid() != 0 {
-               t.Skipf("skipping test; must be root")
+       case "windows":
+       default:
+               if os.Getuid() != 0 {
+                       t.Skipf("skipping test; must be root")
+               }
        }
 
        la, err := ResolveIPAddr("ip4", "127.0.0.1")
@@ -208,7 +210,7 @@ func TestIPConnSpecificMethods(t *testing.T) {
        if err != nil {
                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 {
                t.Fatalf("IPConn.WriteToIP failed: %v", err)
        }