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 { ...@@ -19,8 +19,8 @@ var connTests = []struct {
addr string addr string
}{ }{
{"tcp", "127.0.0.1:0"}, {"tcp", "127.0.0.1:0"},
{"unix", "/tmp/gotest.net1"}, {"unix", testUnixAddr()},
{"unixpacket", "/tmp/gotest.net2"}, {"unixpacket", testUnixAddr()},
} }
func TestConnAndListener(t *testing.T) { func TestConnAndListener(t *testing.T) {
...@@ -34,15 +34,12 @@ func TestConnAndListener(t *testing.T) { ...@@ -34,15 +34,12 @@ func TestConnAndListener(t *testing.T) {
if tt.net == "unixpacket" && runtime.GOOS != "linux" { if tt.net == "unixpacket" && runtime.GOOS != "linux" {
continue continue
} }
os.Remove(tt.addr)
} }
ln, err := Listen(tt.net, tt.addr) ln, err := Listen(tt.net, tt.addr)
if err != nil { if err != nil {
t.Errorf("net.Listen failed: %v", err) t.Fatalf("Listen failed: %v", err)
return
} }
ln.Addr()
defer func(ln Listener, net, addr string) { defer func(ln Listener, net, addr string) {
ln.Close() ln.Close()
switch net { switch net {
...@@ -50,29 +47,28 @@ func TestConnAndListener(t *testing.T) { ...@@ -50,29 +47,28 @@ func TestConnAndListener(t *testing.T) {
os.Remove(addr) os.Remove(addr)
} }
}(ln, tt.net, tt.addr) }(ln, tt.net, tt.addr)
ln.Addr()
done := make(chan int) done := make(chan int)
go transponder(t, ln, done) go transponder(t, ln, done)
c, err := Dial(tt.net, ln.Addr().String()) c, err := Dial(tt.net, ln.Addr().String())
if err != nil { if err != nil {
t.Errorf("net.Dial failed: %v", err) t.Fatalf("Dial failed: %v", err)
return
} }
defer c.Close()
c.LocalAddr() c.LocalAddr()
c.RemoteAddr() c.RemoteAddr()
c.SetDeadline(time.Now().Add(100 * time.Millisecond)) c.SetDeadline(time.Now().Add(100 * time.Millisecond))
c.SetReadDeadline(time.Now().Add(100 * time.Millisecond)) c.SetReadDeadline(time.Now().Add(100 * time.Millisecond))
c.SetWriteDeadline(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 { if _, err := c.Write([]byte("CONN TEST")); err != nil {
t.Errorf("net.Conn.Write failed: %v", err) t.Fatalf("Conn.Write failed: %v", err)
return
} }
rb := make([]byte, 128) rb := make([]byte, 128)
if _, err := c.Read(rb); err != nil { if _, err := c.Read(rb); err != nil {
t.Errorf("net.Conn.Read failed: %v", err) t.Fatalf("Conn.Read failed: %v", err)
} }
<-done <-done
...@@ -82,26 +78,32 @@ func TestConnAndListener(t *testing.T) { ...@@ -82,26 +78,32 @@ func TestConnAndListener(t *testing.T) {
func transponder(t *testing.T, ln Listener, done chan<- int) { func transponder(t *testing.T, ln Listener, done chan<- int) {
defer func() { done <- 1 }() 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() c, err := ln.Accept()
if err != nil { if err != nil {
t.Errorf("net.Listener.Accept failed: %v", err) t.Errorf("Listener.Accept failed: %v", err)
return return
} }
defer c.Close()
c.LocalAddr() c.LocalAddr()
c.RemoteAddr() c.RemoteAddr()
c.SetDeadline(time.Now().Add(100 * time.Millisecond)) c.SetDeadline(time.Now().Add(100 * time.Millisecond))
c.SetReadDeadline(time.Now().Add(100 * time.Millisecond)) c.SetReadDeadline(time.Now().Add(100 * time.Millisecond))
c.SetWriteDeadline(time.Now().Add(100 * time.Millisecond)) c.SetWriteDeadline(time.Now().Add(100 * time.Millisecond))
defer c.Close()
b := make([]byte, 128) b := make([]byte, 128)
n, err := c.Read(b) n, err := c.Read(b)
if err != nil { if err != nil {
t.Errorf("net.Conn.Read failed: %v", err) t.Errorf("Conn.Read failed: %v", err)
return return
} }
if _, err := c.Write(b[:n]); err != nil { if _, err := c.Write(b[:n]); err != nil {
t.Errorf("net.Conn.Write failed: %v", err) t.Errorf("Conn.Write failed: %v", err)
return return
} }
} }
...@@ -22,7 +22,7 @@ var packetConnTests = []struct { ...@@ -22,7 +22,7 @@ var packetConnTests = []struct {
}{ }{
{"udp", "127.0.0.1:0", "127.0.0.1:0"}, {"udp", "127.0.0.1:0", "127.0.0.1:0"},
{"ip:icmp", "127.0.0.1", "127.0.0.1"}, {"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) { func TestPacketConn(t *testing.T) {
...@@ -35,7 +35,7 @@ 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 var wb []byte
netstr := strings.Split(tt.net, ":") netstr := strings.Split(tt.net, ":")
switch netstr[0] { switch netstr[0] {
...@@ -49,15 +49,22 @@ func TestPacketConn(t *testing.T) { ...@@ -49,15 +49,22 @@ func TestPacketConn(t *testing.T) {
if os.Getuid() != 0 { if os.Getuid() != 0 {
continue continue
} }
id := os.Getpid() & 0xffff var err error
wb = newICMPEchoRequest(id, 1, 128, []byte("IP PACKETCONN TEST")) 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": case "unixgram":
switch runtime.GOOS { switch runtime.GOOS {
case "plan9", "windows": case "plan9", "windows":
continue continue
} }
os.Remove(tt.addr1)
os.Remove(tt.addr2)
wb = []byte("UNIXGRAM PACKETCONN TEST") wb = []byte("UNIXGRAM PACKETCONN TEST")
default: default:
continue continue
...@@ -65,43 +72,52 @@ func TestPacketConn(t *testing.T) { ...@@ -65,43 +72,52 @@ func TestPacketConn(t *testing.T) {
c1, err := ListenPacket(tt.net, tt.addr1) c1, err := ListenPacket(tt.net, tt.addr1)
if err != nil { 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.LocalAddr()
c1.SetDeadline(time.Now().Add(100 * time.Millisecond)) c1.SetDeadline(time.Now().Add(100 * time.Millisecond))
c1.SetReadDeadline(time.Now().Add(100 * time.Millisecond)) c1.SetReadDeadline(time.Now().Add(100 * time.Millisecond))
c1.SetWriteDeadline(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) c2, err := ListenPacket(tt.net, tt.addr2)
if err != nil { 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.LocalAddr()
c2.SetDeadline(time.Now().Add(100 * time.Millisecond)) c2.SetDeadline(time.Now().Add(100 * time.Millisecond))
c2.SetReadDeadline(time.Now().Add(100 * time.Millisecond)) c2.SetReadDeadline(time.Now().Add(100 * time.Millisecond))
c2.SetWriteDeadline(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 { 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) rb2 := make([]byte, 128)
if _, _, err := c2.ReadFrom(rb2); err != nil { 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 { 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) rb1 := make([]byte, 128)
if _, _, err := c1.ReadFrom(rb1); err != nil { 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) { 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 var wb []byte
netstr := strings.Split(tt.net, ":") netstr := strings.Split(tt.net, ":")
switch netstr[0] { switch netstr[0] {
...@@ -115,52 +131,66 @@ func TestConnAndPacketConn(t *testing.T) { ...@@ -115,52 +131,66 @@ func TestConnAndPacketConn(t *testing.T) {
if os.Getuid() != 0 { if os.Getuid() != 0 {
continue continue
} }
id := os.Getpid() & 0xffff var err error
wb = newICMPEchoRequest(id, 1, 128, []byte("IP PACKETCONN TEST")) 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: default:
continue continue
} }
c1, err := ListenPacket(tt.net, tt.addr1) c1, err := ListenPacket(tt.net, tt.addr1)
if err != nil { 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.LocalAddr()
c1.SetDeadline(time.Now().Add(100 * time.Millisecond)) c1.SetDeadline(time.Now().Add(100 * time.Millisecond))
c1.SetReadDeadline(time.Now().Add(100 * time.Millisecond)) c1.SetReadDeadline(time.Now().Add(100 * time.Millisecond))
c1.SetWriteDeadline(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()) c2, err := Dial(tt.net, c1.LocalAddr().String())
if err != nil { if err != nil {
t.Fatalf("net.Dial failed: %v", err) t.Fatalf("Dial failed: %v", err)
} }
defer c2.Close()
c2.LocalAddr() c2.LocalAddr()
c2.RemoteAddr() c2.RemoteAddr()
c2.SetDeadline(time.Now().Add(100 * time.Millisecond)) c2.SetDeadline(time.Now().Add(100 * time.Millisecond))
c2.SetReadDeadline(time.Now().Add(100 * time.Millisecond)) c2.SetReadDeadline(time.Now().Add(100 * time.Millisecond))
c2.SetWriteDeadline(time.Now().Add(100 * time.Millisecond)) c2.SetWriteDeadline(time.Now().Add(100 * time.Millisecond))
defer c2.Close()
if _, err := c2.Write(wb); err != nil { 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) rb1 := make([]byte, 128)
if _, _, err := c1.ReadFrom(rb1); err != nil { 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 var dst Addr
if netstr[0] == "ip" { switch netstr[0] {
case "ip":
dst = &IPAddr{IP: IPv4(127, 0, 0, 1)} dst = &IPAddr{IP: IPv4(127, 0, 0, 1)}
} else { case "unixgram":
continue
default:
dst = c2.LocalAddr() dst = c2.LocalAddr()
} }
if _, err := c1.WriteTo(wb, dst); err != nil { 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) rb2 := make([]byte, 128)
if _, err := c2.Read(rb2); err != nil { 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