Commit ed01f4be authored by Mikio Hara's avatar Mikio Hara

net: add unixgram dial test

Also replaces testing.Errof with testing.Fatalf, make use of ICMP mock.

R=golang-dev, dave
CC=golang-dev
https://golang.org/cl/7308058
parent 13393fb6
......@@ -19,8 +19,8 @@ var connTests = []struct {
addr string
}{
{"tcp", "127.0.0.1:0"},
{"unix", "/tmp/gotest.net1"},
{"unixpacket", "/tmp/gotest.net2"},
{"unix", testUnixAddr()},
{"unixpacket", testUnixAddr()},
}
func TestConnAndListener(t *testing.T) {
......@@ -34,15 +34,12 @@ func TestConnAndListener(t *testing.T) {
if tt.net == "unixpacket" && runtime.GOOS != "linux" {
continue
}
os.Remove(tt.addr)
}
ln, err := Listen(tt.net, tt.addr)
if err != nil {
t.Errorf("net.Listen failed: %v", err)
return
t.Fatalf("Listen failed: %v", err)
}
ln.Addr()
defer func(ln Listener, net, addr string) {
ln.Close()
switch net {
......@@ -50,29 +47,28 @@ func TestConnAndListener(t *testing.T) {
os.Remove(addr)
}
}(ln, tt.net, tt.addr)
ln.Addr()
done := make(chan int)
go transponder(t, ln, done)
c, err := Dial(tt.net, ln.Addr().String())
if err != nil {
t.Errorf("net.Dial failed: %v", err)
return
t.Fatalf("Dial failed: %v", err)
}
defer c.Close()
c.LocalAddr()
c.RemoteAddr()
c.SetDeadline(time.Now().Add(100 * time.Millisecond))
c.SetReadDeadline(time.Now().Add(100 * time.Millisecond))
c.SetWriteDeadline(time.Now().Add(100 * time.Millisecond))
defer c.Close()
if _, err := c.Write([]byte("CONN TEST")); err != nil {
t.Errorf("net.Conn.Write failed: %v", err)
return
t.Fatalf("Conn.Write failed: %v", err)
}
rb := make([]byte, 128)
if _, err := c.Read(rb); err != nil {
t.Errorf("net.Conn.Read failed: %v", err)
t.Fatalf("Conn.Read failed: %v", err)
}
<-done
......@@ -82,26 +78,32 @@ func TestConnAndListener(t *testing.T) {
func transponder(t *testing.T, ln Listener, done chan<- int) {
defer func() { done <- 1 }()
switch ln := ln.(type) {
case *TCPListener:
ln.SetDeadline(time.Now().Add(100 * time.Millisecond))
case *UnixListener:
ln.SetDeadline(time.Now().Add(100 * time.Millisecond))
}
c, err := ln.Accept()
if err != nil {
t.Errorf("net.Listener.Accept failed: %v", err)
t.Errorf("Listener.Accept failed: %v", err)
return
}
defer c.Close()
c.LocalAddr()
c.RemoteAddr()
c.SetDeadline(time.Now().Add(100 * time.Millisecond))
c.SetReadDeadline(time.Now().Add(100 * time.Millisecond))
c.SetWriteDeadline(time.Now().Add(100 * time.Millisecond))
defer c.Close()
b := make([]byte, 128)
n, err := c.Read(b)
if err != nil {
t.Errorf("net.Conn.Read failed: %v", err)
t.Errorf("Conn.Read failed: %v", err)
return
}
if _, err := c.Write(b[:n]); err != nil {
t.Errorf("net.Conn.Write failed: %v", err)
t.Errorf("Conn.Write failed: %v", err)
return
}
}
......@@ -22,7 +22,7 @@ var packetConnTests = []struct {
}{
{"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"},
{"unixgram", testUnixAddr(), testUnixAddr()},
}
func TestPacketConn(t *testing.T) {
......@@ -35,7 +35,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] {
......@@ -49,15 +49,22 @@ 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
......@@ -65,43 +72,52 @@ func TestPacketConn(t *testing.T) {
c1, err := ListenPacket(tt.net, tt.addr1)
if err != nil {
t.Fatalf("net.ListenPacket failed: %v", err)
t.Fatalf("ListenPacket failed: %v", err)
}
defer closer(c1, netstr[0], tt.addr1, tt.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 := ListenPacket(tt.net, tt.addr2)
if err != nil {
t.Fatalf("net.ListenPacket failed: %v", err)
t.Fatalf("ListenPacket failed: %v", err)
}
defer closer(c2, netstr[0], tt.addr1, tt.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] {
......@@ -115,52 +131,66 @@ 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":
wb = []byte("UNIXGRAM PACKETCONN TEST")
default:
continue
}
c1, err := ListenPacket(tt.net, tt.addr1)
if err != nil {
t.Fatalf("net.ListenPacket failed: %v", err)
t.Fatalf("ListenPacket failed: %v", err)
}
defer closer(c1, netstr[0], tt.addr1, tt.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 := 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 Addr
if netstr[0] == "ip" {
switch netstr[0] {
case "ip":
dst = &IPAddr{IP: IPv4(127, 0, 0, 1)}
} else {
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)
}
}
}
This diff is collapsed.
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