]> Cypherpunks repositories - gostls13.git/commitdiff
net: enable PacketConn test for raw IP network on Windows
authorMikio Hara <mikioh.mikioh@gmail.com>
Mon, 26 Aug 2013 09:36:58 +0000 (18:36 +0900)
committerMikio Hara <mikioh.mikioh@gmail.com>
Mon, 26 Aug 2013 09:36:58 +0000 (18:36 +0900)
Just forgot to include this in CL 12843043.
Also consolidates the code dealing with test environment.

Update #6122

R=alex.brainman
CC=golang-dev
https://golang.org/cl/13184043

src/pkg/net/packetconn_test.go

index ec5dd710f5533a9a9cbd9a3e2c1426771f1bb7fc..f779b4ad4745116e96c8c1e84886b441d3cdefe2 100644 (file)
@@ -21,6 +21,53 @@ func strfunc(s string) func() string {
        }
 }
 
+func packetConnTestData(t *testing.T, net string, i int) ([]byte, func()) {
+       switch net {
+       case "udp":
+               return []byte("UDP PACKETCONN TEST"), nil
+       case "ip":
+               switch runtime.GOOS {
+               case "plan9":
+                       return nil, func() {
+                               t.Logf("skipping %q test on %q", net, runtime.GOOS)
+                       }
+               case "windows":
+               default:
+                       if os.Getuid() != 0 {
+                               return nil, func() {
+                                       t.Logf("skipping %q test; must be root", net)
+                               }
+                       }
+               }
+               b, err := (&icmpMessage{
+                       Type: icmpv4EchoRequest, Code: 0,
+                       Body: &icmpEcho{
+                               ID: os.Getpid() & 0xffff, Seq: i + 1,
+                               Data: []byte("IP PACKETCONN TEST"),
+                       },
+               }).Marshal()
+               if err != nil {
+                       return nil, func() {
+                               t.Fatalf("icmpMessage.Marshal failed: %v", err)
+                       }
+               }
+               return b, nil
+       case "unixgram":
+               switch runtime.GOOS {
+               case "plan9", "windows":
+                       return nil, func() {
+                               t.Logf("skipping %q test on %q", net, runtime.GOOS)
+                       }
+               default:
+                       return []byte("UNIXGRAM PACKETCONN TEST"), nil
+               }
+       default:
+               return nil, func() {
+                       t.Logf("skipping %q test", net)
+               }
+       }
+}
+
 var packetConnTests = []struct {
        net   string
        addr1 func() string
@@ -42,37 +89,10 @@ func TestPacketConn(t *testing.T) {
        }
 
        for i, tt := range packetConnTests {
-               var wb []byte
                netstr := strings.Split(tt.net, ":")
-               switch netstr[0] {
-               case "udp":
-                       wb = []byte("UDP PACKETCONN TEST")
-               case "ip":
-                       switch runtime.GOOS {
-                       case "plan9":
-                               continue
-                       }
-                       if os.Getuid() != 0 {
-                               continue
-                       }
-                       var err error
-                       wb, err = (&icmpMessage{
-                               Type: icmpv4EchoRequest, Code: 0,
-                               Body: &icmpEcho{
-                                       ID: os.Getpid() & 0xffff, Seq: i + 1,
-                                       Data: []byte("IP PACKETCONN TEST"),
-                               },
-                       }).Marshal()
-                       if err != nil {
-                               t.Fatalf("icmpMessage.Marshal failed: %v", err)
-                       }
-               case "unixgram":
-                       switch runtime.GOOS {
-                       case "plan9", "windows":
-                               continue
-                       }
-                       wb = []byte("UNIXGRAM PACKETCONN TEST")
-               default:
+               wb, skipOrFatalFn := packetConnTestData(t, netstr[0], i)
+               if skipOrFatalFn != nil {
+                       skipOrFatalFn()
                        continue
                }
 
@@ -127,35 +147,9 @@ func TestConnAndPacketConn(t *testing.T) {
        for i, tt := range packetConnTests {
                var wb []byte
                netstr := strings.Split(tt.net, ":")
-               switch netstr[0] {
-               case "udp":
-                       wb = []byte("UDP PACKETCONN TEST")
-               case "ip":
-                       switch runtime.GOOS {
-                       case "plan9":
-                               continue
-                       }
-                       if os.Getuid() != 0 {
-                               continue
-                       }
-                       var err error
-                       wb, err = (&icmpMessage{
-                               Type: icmpv4EchoRequest, Code: 0,
-                               Body: &icmpEcho{
-                                       ID: os.Getpid() & 0xffff, Seq: i + 1,
-                                       Data: []byte("IP PACKETCONN TEST"),
-                               },
-                       }).Marshal()
-                       if err != nil {
-                               t.Fatalf("icmpMessage.Marshal failed: %v", err)
-                       }
-               case "unixgram":
-                       switch runtime.GOOS {
-                       case "plan9", "windows":
-                               continue
-                       }
-                       wb = []byte("UNIXGRAM PACKETCONN TEST")
-               default:
+               wb, skipOrFatalFn := packetConnTestData(t, netstr[0], i)
+               if skipOrFatalFn != nil {
+                       skipOrFatalFn()
                        continue
                }