1
0
mirror of https://github.com/golang/go synced 2024-11-17 16:14:42 -07:00

net: enable PacketConn test for raw IP network on Windows

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
This commit is contained in:
Mikio Hara 2013-08-26 18:36:58 +09:00
parent f033d988b1
commit 519a9e8e9b

View 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
}