aboutsummaryrefslogtreecommitdiff
path: root/libgo/go/net/packetconn_test.go
diff options
context:
space:
mode:
Diffstat (limited to 'libgo/go/net/packetconn_test.go')
-rw-r--r--libgo/go/net/packetconn_test.go124
1 files changed, 84 insertions, 40 deletions
diff --git a/libgo/go/net/packetconn_test.go b/libgo/go/net/packetconn_test.go
index ff29e24..ec5dd71 100644
--- a/libgo/go/net/packetconn_test.go
+++ b/libgo/go/net/packetconn_test.go
@@ -2,10 +2,12 @@
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
-package net_test
+// This file implements API tests across platforms and will never have a build
+// tag.
+
+package net
import (
- "net"
"os"
"runtime"
"strings"
@@ -13,18 +15,24 @@ import (
"time"
)
+func strfunc(s string) func() string {
+ return func() string {
+ return s
+ }
+}
+
var packetConnTests = []struct {
net string
- addr1 string
- addr2 string
+ addr1 func() string
+ addr2 func() string
}{
- {"udp", "127.0.0.1:0", "127.0.0.1:0"},
- {"ip:icmp", "127.0.0.1", "127.0.0.1"},
- {"unixgram", "/tmp/gotest.net1", "/tmp/gotest.net2"},
+ {"udp", strfunc("127.0.0.1:0"), strfunc("127.0.0.1:0")},
+ {"ip:icmp", strfunc("127.0.0.1"), strfunc("127.0.0.1")},
+ {"unixgram", testUnixAddr, testUnixAddr},
}
func TestPacketConn(t *testing.T) {
- closer := func(c net.PacketConn, net, addr1, addr2 string) {
+ closer := func(c PacketConn, net, addr1, addr2 string) {
c.Close()
switch net {
case "unixgram":
@@ -33,7 +41,7 @@ func TestPacketConn(t *testing.T) {
}
}
- for _, tt := range packetConnTests {
+ for i, tt := range packetConnTests {
var wb []byte
netstr := strings.Split(tt.net, ":")
switch netstr[0] {
@@ -47,59 +55,76 @@ func TestPacketConn(t *testing.T) {
if os.Getuid() != 0 {
continue
}
- id := os.Getpid() & 0xffff
- wb = newICMPEchoRequest(id, 1, 128, []byte("IP PACKETCONN TEST"))
+ 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
}
- os.Remove(tt.addr1)
- os.Remove(tt.addr2)
wb = []byte("UNIXGRAM PACKETCONN TEST")
default:
continue
}
- c1, err := net.ListenPacket(tt.net, tt.addr1)
+ addr1, addr2 := tt.addr1(), tt.addr2()
+ c1, err := ListenPacket(tt.net, addr1)
if err != nil {
- t.Fatalf("net.ListenPacket failed: %v", err)
+ t.Fatalf("ListenPacket failed: %v", err)
}
+ defer closer(c1, netstr[0], addr1, addr2)
c1.LocalAddr()
c1.SetDeadline(time.Now().Add(100 * time.Millisecond))
c1.SetReadDeadline(time.Now().Add(100 * time.Millisecond))
c1.SetWriteDeadline(time.Now().Add(100 * time.Millisecond))
- defer closer(c1, netstr[0], tt.addr1, tt.addr2)
- c2, err := net.ListenPacket(tt.net, tt.addr2)
+ c2, err := ListenPacket(tt.net, addr2)
if err != nil {
- t.Fatalf("net.ListenPacket failed: %v", err)
+ t.Fatalf("ListenPacket failed: %v", err)
}
+ defer closer(c2, netstr[0], addr1, addr2)
c2.LocalAddr()
c2.SetDeadline(time.Now().Add(100 * time.Millisecond))
c2.SetReadDeadline(time.Now().Add(100 * time.Millisecond))
c2.SetWriteDeadline(time.Now().Add(100 * time.Millisecond))
- defer closer(c2, netstr[0], tt.addr1, tt.addr2)
if _, err := c1.WriteTo(wb, c2.LocalAddr()); err != nil {
- t.Fatalf("net.PacketConn.WriteTo failed: %v", err)
+ t.Fatalf("PacketConn.WriteTo failed: %v", err)
}
rb2 := make([]byte, 128)
if _, _, err := c2.ReadFrom(rb2); err != nil {
- t.Fatalf("net.PacketConn.ReadFrom failed: %v", err)
+ t.Fatalf("PacketConn.ReadFrom failed: %v", err)
}
if _, err := c2.WriteTo(wb, c1.LocalAddr()); err != nil {
- t.Fatalf("net.PacketConn.WriteTo failed: %v", err)
+ t.Fatalf("PacketConn.WriteTo failed: %v", err)
}
rb1 := make([]byte, 128)
if _, _, err := c1.ReadFrom(rb1); err != nil {
- t.Fatalf("net.PacketConn.ReadFrom failed: %v", err)
+ t.Fatalf("PacketConn.ReadFrom failed: %v", err)
}
}
}
func TestConnAndPacketConn(t *testing.T) {
- for _, tt := range packetConnTests {
+ closer := func(c PacketConn, net, addr1, addr2 string) {
+ c.Close()
+ switch net {
+ case "unixgram":
+ os.Remove(addr1)
+ os.Remove(addr2)
+ }
+ }
+
+ for i, tt := range packetConnTests {
var wb []byte
netstr := strings.Split(tt.net, ":")
switch netstr[0] {
@@ -113,52 +138,71 @@ func TestConnAndPacketConn(t *testing.T) {
if os.Getuid() != 0 {
continue
}
- id := os.Getpid() & 0xffff
- wb = newICMPEchoRequest(id, 1, 128, []byte("IP PACKETCONN TEST"))
+ 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:
continue
}
- c1, err := net.ListenPacket(tt.net, tt.addr1)
+ addr1, addr2 := tt.addr1(), tt.addr2()
+ c1, err := ListenPacket(tt.net, addr1)
if err != nil {
- t.Fatalf("net.ListenPacket failed: %v", err)
+ t.Fatalf("ListenPacket failed: %v", err)
}
+ defer closer(c1, netstr[0], addr1, addr2)
c1.LocalAddr()
c1.SetDeadline(time.Now().Add(100 * time.Millisecond))
c1.SetReadDeadline(time.Now().Add(100 * time.Millisecond))
c1.SetWriteDeadline(time.Now().Add(100 * time.Millisecond))
- defer c1.Close()
- c2, err := net.Dial(tt.net, c1.LocalAddr().String())
+ c2, err := Dial(tt.net, c1.LocalAddr().String())
if err != nil {
- t.Fatalf("net.Dial failed: %v", err)
+ t.Fatalf("Dial failed: %v", err)
}
+ defer c2.Close()
c2.LocalAddr()
c2.RemoteAddr()
c2.SetDeadline(time.Now().Add(100 * time.Millisecond))
c2.SetReadDeadline(time.Now().Add(100 * time.Millisecond))
c2.SetWriteDeadline(time.Now().Add(100 * time.Millisecond))
- defer c2.Close()
if _, err := c2.Write(wb); err != nil {
- t.Fatalf("net.Conn.Write failed: %v", err)
+ t.Fatalf("Conn.Write failed: %v", err)
}
rb1 := make([]byte, 128)
if _, _, err := c1.ReadFrom(rb1); err != nil {
- t.Fatalf("net.PacetConn.ReadFrom failed: %v", err)
+ t.Fatalf("PacetConn.ReadFrom failed: %v", err)
}
- var dst net.Addr
- if netstr[0] == "ip" {
- dst = &net.IPAddr{IP: net.IPv4(127, 0, 0, 1)}
- } else {
+ var dst Addr
+ switch netstr[0] {
+ case "ip":
+ dst = &IPAddr{IP: IPv4(127, 0, 0, 1)}
+ case "unixgram":
+ continue
+ default:
dst = c2.LocalAddr()
}
if _, err := c1.WriteTo(wb, dst); err != nil {
- t.Fatalf("net.PacketConn.WriteTo failed: %v", err)
+ t.Fatalf("PacketConn.WriteTo failed: %v", err)
}
rb2 := make([]byte, 128)
if _, err := c2.Read(rb2); err != nil {
- t.Fatalf("net.Conn.Read failed: %v", err)
+ t.Fatalf("Conn.Read failed: %v", err)
}
}
}