mirror of
https://github.com/golang/go
synced 2024-11-12 07:00:21 -07:00
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
This commit is contained in:
parent
13393fb6c7
commit
ed01f4be59
@ -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) {
|
|||||||
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) {
|
|||||||
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) {
|
|||||||
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 {
|
|||||||
}{
|
}{
|
||||||
{"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) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
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) {
|
|||||||
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) {
|
|||||||
|
|
||||||
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) {
|
|||||||
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)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -8,20 +8,19 @@
|
|||||||
package net
|
package net
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
|
||||||
"os"
|
"os"
|
||||||
"runtime"
|
"runtime"
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
var condErrorf = func() func(*testing.T, string, ...interface{}) {
|
var condFatalf = func() func(*testing.T, string, ...interface{}) {
|
||||||
// A few APIs are not implemented yet on both Plan 9 and Windows.
|
// A few APIs are not implemented yet on both Plan 9 and Windows.
|
||||||
switch runtime.GOOS {
|
switch runtime.GOOS {
|
||||||
case "plan9", "windows":
|
case "plan9", "windows":
|
||||||
return (*testing.T).Logf
|
return (*testing.T).Logf
|
||||||
}
|
}
|
||||||
return (*testing.T).Errorf
|
return (*testing.T).Fatalf
|
||||||
}()
|
}()
|
||||||
|
|
||||||
func TestTCPListenerSpecificMethods(t *testing.T) {
|
func TestTCPListenerSpecificMethods(t *testing.T) {
|
||||||
@ -32,36 +31,33 @@ func TestTCPListenerSpecificMethods(t *testing.T) {
|
|||||||
|
|
||||||
la, err := ResolveTCPAddr("tcp4", "127.0.0.1:0")
|
la, err := ResolveTCPAddr("tcp4", "127.0.0.1:0")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("net.ResolveTCPAddr failed: %v", err)
|
t.Fatalf("ResolveTCPAddr failed: %v", err)
|
||||||
}
|
}
|
||||||
ln, err := ListenTCP("tcp4", la)
|
ln, err := ListenTCP("tcp4", la)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("net.ListenTCP failed: %v", err)
|
t.Fatalf("ListenTCP failed: %v", err)
|
||||||
}
|
}
|
||||||
|
defer ln.Close()
|
||||||
ln.Addr()
|
ln.Addr()
|
||||||
ln.SetDeadline(time.Now().Add(30 * time.Nanosecond))
|
ln.SetDeadline(time.Now().Add(30 * time.Nanosecond))
|
||||||
defer ln.Close()
|
|
||||||
|
|
||||||
if c, err := ln.Accept(); err != nil {
|
if c, err := ln.Accept(); err != nil {
|
||||||
if !err.(Error).Timeout() {
|
if !err.(Error).Timeout() {
|
||||||
t.Errorf("net.TCPListener.Accept failed: %v", err)
|
t.Fatalf("TCPListener.Accept failed: %v", err)
|
||||||
return
|
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
c.Close()
|
c.Close()
|
||||||
}
|
}
|
||||||
if c, err := ln.AcceptTCP(); err != nil {
|
if c, err := ln.AcceptTCP(); err != nil {
|
||||||
if !err.(Error).Timeout() {
|
if !err.(Error).Timeout() {
|
||||||
t.Errorf("net.TCPListener.AcceptTCP failed: %v", err)
|
t.Fatalf("TCPListener.AcceptTCP failed: %v", err)
|
||||||
return
|
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
c.Close()
|
c.Close()
|
||||||
}
|
}
|
||||||
|
|
||||||
if f, err := ln.File(); err != nil {
|
if f, err := ln.File(); err != nil {
|
||||||
condErrorf(t, "net.TCPListener.File failed: %v", err)
|
condFatalf(t, "TCPListener.File failed: %v", err)
|
||||||
return
|
|
||||||
} else {
|
} else {
|
||||||
f.Close()
|
f.Close()
|
||||||
}
|
}
|
||||||
@ -70,28 +66,27 @@ func TestTCPListenerSpecificMethods(t *testing.T) {
|
|||||||
func TestTCPConnSpecificMethods(t *testing.T) {
|
func TestTCPConnSpecificMethods(t *testing.T) {
|
||||||
la, err := ResolveTCPAddr("tcp4", "127.0.0.1:0")
|
la, err := ResolveTCPAddr("tcp4", "127.0.0.1:0")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("net.ResolveTCPAddr failed: %v", err)
|
t.Fatalf("ResolveTCPAddr failed: %v", err)
|
||||||
}
|
}
|
||||||
ln, err := ListenTCP("tcp4", la)
|
ln, err := ListenTCP("tcp4", la)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("net.ListenTCP failed: %v", err)
|
t.Fatalf("ListenTCP failed: %v", err)
|
||||||
}
|
}
|
||||||
ln.Addr()
|
|
||||||
defer ln.Close()
|
defer ln.Close()
|
||||||
|
ln.Addr()
|
||||||
|
|
||||||
done := make(chan int)
|
done := make(chan int)
|
||||||
go transponder(t, ln, done)
|
go transponder(t, ln, done)
|
||||||
|
|
||||||
ra, err := ResolveTCPAddr("tcp4", ln.Addr().String())
|
ra, err := ResolveTCPAddr("tcp4", ln.Addr().String())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Errorf("net.ResolveTCPAddr failed: %v", err)
|
t.Fatalf("ResolveTCPAddr failed: %v", err)
|
||||||
return
|
|
||||||
}
|
}
|
||||||
c, err := DialTCP("tcp4", nil, ra)
|
c, err := DialTCP("tcp4", nil, ra)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Errorf("net.DialTCP failed: %v", err)
|
t.Fatalf("DialTCP failed: %v", err)
|
||||||
return
|
|
||||||
}
|
}
|
||||||
|
defer c.Close()
|
||||||
c.SetKeepAlive(false)
|
c.SetKeepAlive(false)
|
||||||
c.SetLinger(0)
|
c.SetLinger(0)
|
||||||
c.SetNoDelay(false)
|
c.SetNoDelay(false)
|
||||||
@ -100,16 +95,13 @@ func TestTCPConnSpecificMethods(t *testing.T) {
|
|||||||
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("TCPCONN TEST")); err != nil {
|
if _, err := c.Write([]byte("TCPCONN TEST")); err != nil {
|
||||||
t.Errorf("net.TCPConn.Write failed: %v", err)
|
t.Fatalf("TCPConn.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.TCPConn.Read failed: %v", err)
|
t.Fatalf("TCPConn.Read failed: %v", err)
|
||||||
return
|
|
||||||
}
|
}
|
||||||
|
|
||||||
<-done
|
<-done
|
||||||
@ -118,12 +110,13 @@ func TestTCPConnSpecificMethods(t *testing.T) {
|
|||||||
func TestUDPConnSpecificMethods(t *testing.T) {
|
func TestUDPConnSpecificMethods(t *testing.T) {
|
||||||
la, err := ResolveUDPAddr("udp4", "127.0.0.1:0")
|
la, err := ResolveUDPAddr("udp4", "127.0.0.1:0")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("net.ResolveUDPAddr failed: %v", err)
|
t.Fatalf("ResolveUDPAddr failed: %v", err)
|
||||||
}
|
}
|
||||||
c, err := ListenUDP("udp4", la)
|
c, err := ListenUDP("udp4", la)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("net.ListenUDP failed: %v", err)
|
t.Fatalf("ListenUDP failed: %v", err)
|
||||||
}
|
}
|
||||||
|
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))
|
||||||
@ -131,30 +124,24 @@ func TestUDPConnSpecificMethods(t *testing.T) {
|
|||||||
c.SetWriteDeadline(time.Now().Add(100 * time.Millisecond))
|
c.SetWriteDeadline(time.Now().Add(100 * time.Millisecond))
|
||||||
c.SetReadBuffer(2048)
|
c.SetReadBuffer(2048)
|
||||||
c.SetWriteBuffer(2048)
|
c.SetWriteBuffer(2048)
|
||||||
defer c.Close()
|
|
||||||
|
|
||||||
wb := []byte("UDPCONN TEST")
|
wb := []byte("UDPCONN TEST")
|
||||||
rb := make([]byte, 128)
|
rb := make([]byte, 128)
|
||||||
if _, err := c.WriteToUDP(wb, c.LocalAddr().(*UDPAddr)); err != nil {
|
if _, err := c.WriteToUDP(wb, c.LocalAddr().(*UDPAddr)); err != nil {
|
||||||
t.Errorf("net.UDPConn.WriteToUDP failed: %v", err)
|
t.Fatalf("UDPConn.WriteToUDP failed: %v", err)
|
||||||
return
|
|
||||||
}
|
}
|
||||||
if _, _, err := c.ReadFromUDP(rb); err != nil {
|
if _, _, err := c.ReadFromUDP(rb); err != nil {
|
||||||
t.Errorf("net.UDPConn.ReadFromUDP failed: %v", err)
|
t.Fatalf("UDPConn.ReadFromUDP failed: %v", err)
|
||||||
return
|
|
||||||
}
|
}
|
||||||
if _, _, err := c.WriteMsgUDP(wb, nil, c.LocalAddr().(*UDPAddr)); err != nil {
|
if _, _, err := c.WriteMsgUDP(wb, nil, c.LocalAddr().(*UDPAddr)); err != nil {
|
||||||
condErrorf(t, "net.UDPConn.WriteMsgUDP failed: %v", err)
|
condFatalf(t, "UDPConn.WriteMsgUDP failed: %v", err)
|
||||||
return
|
|
||||||
}
|
}
|
||||||
if _, _, _, _, err := c.ReadMsgUDP(rb, nil); err != nil {
|
if _, _, _, _, err := c.ReadMsgUDP(rb, nil); err != nil {
|
||||||
condErrorf(t, "net.UDPConn.ReadMsgUDP failed: %v", err)
|
condFatalf(t, "UDPConn.ReadMsgUDP failed: %v", err)
|
||||||
return
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if f, err := c.File(); err != nil {
|
if f, err := c.File(); err != nil {
|
||||||
condErrorf(t, "net.UDPConn.File failed: %v", err)
|
condFatalf(t, "UDPConn.File failed: %v", err)
|
||||||
return
|
|
||||||
} else {
|
} else {
|
||||||
f.Close()
|
f.Close()
|
||||||
}
|
}
|
||||||
@ -171,12 +158,13 @@ func TestIPConnSpecificMethods(t *testing.T) {
|
|||||||
|
|
||||||
la, err := ResolveIPAddr("ip4", "127.0.0.1")
|
la, err := ResolveIPAddr("ip4", "127.0.0.1")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("net.ResolveIPAddr failed: %v", err)
|
t.Fatalf("ResolveIPAddr failed: %v", err)
|
||||||
}
|
}
|
||||||
c, err := ListenIP("ip4:icmp", la)
|
c, err := ListenIP("ip4:icmp", la)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("net.ListenIP failed: %v", err)
|
t.Fatalf("ListenIP failed: %v", err)
|
||||||
}
|
}
|
||||||
|
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))
|
||||||
@ -184,31 +172,33 @@ func TestIPConnSpecificMethods(t *testing.T) {
|
|||||||
c.SetWriteDeadline(time.Now().Add(100 * time.Millisecond))
|
c.SetWriteDeadline(time.Now().Add(100 * time.Millisecond))
|
||||||
c.SetReadBuffer(2048)
|
c.SetReadBuffer(2048)
|
||||||
c.SetWriteBuffer(2048)
|
c.SetWriteBuffer(2048)
|
||||||
defer c.Close()
|
|
||||||
|
|
||||||
id := os.Getpid() & 0xffff
|
wb, err := (&icmpMessage{
|
||||||
wb := newICMPEchoRequest(id, 1, 128, []byte("IPCONN TEST "))
|
Type: icmpv4EchoRequest, Code: 0,
|
||||||
|
Body: &icmpEcho{
|
||||||
|
ID: os.Getpid() & 0xffff, Seq: 1,
|
||||||
|
Data: []byte("IPCONN TEST "),
|
||||||
|
},
|
||||||
|
}).Marshal()
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("icmpMessage.Marshal failed: %v", err)
|
||||||
|
}
|
||||||
rb := make([]byte, 20+128)
|
rb := make([]byte, 20+128)
|
||||||
if _, err := c.WriteToIP(wb, c.LocalAddr().(*IPAddr)); err != nil {
|
if _, err := c.WriteToIP(wb, c.LocalAddr().(*IPAddr)); err != nil {
|
||||||
t.Errorf("net.IPConn.WriteToIP failed: %v", err)
|
t.Fatalf("IPConn.WriteToIP failed: %v", err)
|
||||||
return
|
|
||||||
}
|
}
|
||||||
if _, _, err := c.ReadFromIP(rb); err != nil {
|
if _, _, err := c.ReadFromIP(rb); err != nil {
|
||||||
t.Errorf("net.IPConn.ReadFromIP failed: %v", err)
|
t.Fatalf("IPConn.ReadFromIP failed: %v", err)
|
||||||
return
|
|
||||||
}
|
}
|
||||||
if _, _, err := c.WriteMsgIP(wb, nil, c.LocalAddr().(*IPAddr)); err != nil {
|
if _, _, err := c.WriteMsgIP(wb, nil, c.LocalAddr().(*IPAddr)); err != nil {
|
||||||
condErrorf(t, "net.UDPConn.WriteMsgIP failed: %v", err)
|
condFatalf(t, "IPConn.WriteMsgIP failed: %v", err)
|
||||||
return
|
|
||||||
}
|
}
|
||||||
if _, _, _, _, err := c.ReadMsgIP(rb, nil); err != nil {
|
if _, _, _, _, err := c.ReadMsgIP(rb, nil); err != nil {
|
||||||
condErrorf(t, "net.UDPConn.ReadMsgIP failed: %v", err)
|
condFatalf(t, "IPConn.ReadMsgIP failed: %v", err)
|
||||||
return
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if f, err := c.File(); err != nil {
|
if f, err := c.File(); err != nil {
|
||||||
condErrorf(t, "net.IPConn.File failed: %v", err)
|
condFatalf(t, "IPConn.File failed: %v", err)
|
||||||
return
|
|
||||||
} else {
|
} else {
|
||||||
f.Close()
|
f.Close()
|
||||||
}
|
}
|
||||||
@ -220,41 +210,37 @@ func TestUnixListenerSpecificMethods(t *testing.T) {
|
|||||||
t.Skipf("skipping read test on %q", runtime.GOOS)
|
t.Skipf("skipping read test on %q", runtime.GOOS)
|
||||||
}
|
}
|
||||||
|
|
||||||
p := "/tmp/gotest.net"
|
addr := testUnixAddr()
|
||||||
os.Remove(p)
|
la, err := ResolveUnixAddr("unix", addr)
|
||||||
la, err := ResolveUnixAddr("unix", p)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("net.ResolveUnixAddr failed: %v", err)
|
t.Fatalf("ResolveUnixAddr failed: %v", err)
|
||||||
}
|
}
|
||||||
ln, err := ListenUnix("unix", la)
|
ln, err := ListenUnix("unix", la)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("net.ListenUnix failed: %v", err)
|
t.Fatalf("ListenUnix failed: %v", err)
|
||||||
}
|
}
|
||||||
|
defer ln.Close()
|
||||||
|
defer os.Remove(addr)
|
||||||
ln.Addr()
|
ln.Addr()
|
||||||
ln.SetDeadline(time.Now().Add(30 * time.Nanosecond))
|
ln.SetDeadline(time.Now().Add(30 * time.Nanosecond))
|
||||||
defer ln.Close()
|
|
||||||
defer os.Remove(p)
|
|
||||||
|
|
||||||
if c, err := ln.Accept(); err != nil {
|
if c, err := ln.Accept(); err != nil {
|
||||||
if !err.(Error).Timeout() {
|
if !err.(Error).Timeout() {
|
||||||
t.Errorf("net.TCPListener.AcceptTCP failed: %v", err)
|
t.Fatalf("UnixListener.Accept failed: %v", err)
|
||||||
return
|
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
c.Close()
|
c.Close()
|
||||||
}
|
}
|
||||||
if c, err := ln.AcceptUnix(); err != nil {
|
if c, err := ln.AcceptUnix(); err != nil {
|
||||||
if !err.(Error).Timeout() {
|
if !err.(Error).Timeout() {
|
||||||
t.Errorf("net.TCPListener.AcceptTCP failed: %v", err)
|
t.Fatalf("UnixListener.AcceptUnix failed: %v", err)
|
||||||
return
|
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
c.Close()
|
c.Close()
|
||||||
}
|
}
|
||||||
|
|
||||||
if f, err := ln.File(); err != nil {
|
if f, err := ln.File(); err != nil {
|
||||||
t.Errorf("net.UnixListener.File failed: %v", err)
|
t.Fatalf("UnixListener.File failed: %v", err)
|
||||||
return
|
|
||||||
} else {
|
} else {
|
||||||
f.Close()
|
f.Close()
|
||||||
}
|
}
|
||||||
@ -266,19 +252,18 @@ func TestUnixConnSpecificMethods(t *testing.T) {
|
|||||||
t.Skipf("skipping test on %q", runtime.GOOS)
|
t.Skipf("skipping test on %q", runtime.GOOS)
|
||||||
}
|
}
|
||||||
|
|
||||||
p1, p2, p3 := "/tmp/gotest.net1", "/tmp/gotest.net2", "/tmp/gotest.net3"
|
addr1, addr2, addr3 := testUnixAddr(), testUnixAddr(), testUnixAddr()
|
||||||
os.Remove(p1)
|
|
||||||
os.Remove(p2)
|
|
||||||
os.Remove(p3)
|
|
||||||
|
|
||||||
a1, err := ResolveUnixAddr("unixgram", p1)
|
a1, err := ResolveUnixAddr("unixgram", addr1)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("net.ResolveUnixAddr failed: %v", err)
|
t.Fatalf("ResolveUnixAddr failed: %v", err)
|
||||||
}
|
}
|
||||||
c1, err := DialUnix("unixgram", a1, nil)
|
c1, err := DialUnix("unixgram", a1, nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("net.DialUnix failed: %v", err)
|
t.Fatalf("DialUnix failed: %v", err)
|
||||||
}
|
}
|
||||||
|
defer c1.Close()
|
||||||
|
defer os.Remove(addr1)
|
||||||
c1.LocalAddr()
|
c1.LocalAddr()
|
||||||
c1.RemoteAddr()
|
c1.RemoteAddr()
|
||||||
c1.SetDeadline(time.Now().Add(100 * time.Millisecond))
|
c1.SetDeadline(time.Now().Add(100 * time.Millisecond))
|
||||||
@ -286,19 +271,17 @@ func TestUnixConnSpecificMethods(t *testing.T) {
|
|||||||
c1.SetWriteDeadline(time.Now().Add(100 * time.Millisecond))
|
c1.SetWriteDeadline(time.Now().Add(100 * time.Millisecond))
|
||||||
c1.SetReadBuffer(2048)
|
c1.SetReadBuffer(2048)
|
||||||
c1.SetWriteBuffer(2048)
|
c1.SetWriteBuffer(2048)
|
||||||
defer c1.Close()
|
|
||||||
defer os.Remove(p1)
|
|
||||||
|
|
||||||
a2, err := ResolveUnixAddr("unixgram", p2)
|
a2, err := ResolveUnixAddr("unixgram", addr2)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Errorf("net.ResolveUnixAddr failed: %v", err)
|
t.Fatalf("ResolveUnixAddr failed: %v", err)
|
||||||
return
|
|
||||||
}
|
}
|
||||||
c2, err := DialUnix("unixgram", a2, nil)
|
c2, err := DialUnix("unixgram", a2, nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Errorf("net.DialUnix failed: %v", err)
|
t.Fatalf("DialUnix failed: %v", err)
|
||||||
return
|
|
||||||
}
|
}
|
||||||
|
defer c2.Close()
|
||||||
|
defer os.Remove(addr2)
|
||||||
c2.LocalAddr()
|
c2.LocalAddr()
|
||||||
c2.RemoteAddr()
|
c2.RemoteAddr()
|
||||||
c2.SetDeadline(time.Now().Add(100 * time.Millisecond))
|
c2.SetDeadline(time.Now().Add(100 * time.Millisecond))
|
||||||
@ -306,19 +289,17 @@ func TestUnixConnSpecificMethods(t *testing.T) {
|
|||||||
c2.SetWriteDeadline(time.Now().Add(100 * time.Millisecond))
|
c2.SetWriteDeadline(time.Now().Add(100 * time.Millisecond))
|
||||||
c2.SetReadBuffer(2048)
|
c2.SetReadBuffer(2048)
|
||||||
c2.SetWriteBuffer(2048)
|
c2.SetWriteBuffer(2048)
|
||||||
defer c2.Close()
|
|
||||||
defer os.Remove(p2)
|
|
||||||
|
|
||||||
a3, err := ResolveUnixAddr("unixgram", p3)
|
a3, err := ResolveUnixAddr("unixgram", addr3)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Errorf("net.ResolveUnixAddr failed: %v", err)
|
t.Fatalf("ResolveUnixAddr failed: %v", err)
|
||||||
return
|
|
||||||
}
|
}
|
||||||
c3, err := ListenUnixgram("unixgram", a3)
|
c3, err := ListenUnixgram("unixgram", a3)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Errorf("net.ListenUnixgram failed: %v", err)
|
t.Fatalf("ListenUnixgram failed: %v", err)
|
||||||
return
|
|
||||||
}
|
}
|
||||||
|
defer c3.Close()
|
||||||
|
defer os.Remove(addr3)
|
||||||
c3.LocalAddr()
|
c3.LocalAddr()
|
||||||
c3.RemoteAddr()
|
c3.RemoteAddr()
|
||||||
c3.SetDeadline(time.Now().Add(100 * time.Millisecond))
|
c3.SetDeadline(time.Now().Add(100 * time.Millisecond))
|
||||||
@ -326,85 +307,39 @@ func TestUnixConnSpecificMethods(t *testing.T) {
|
|||||||
c3.SetWriteDeadline(time.Now().Add(100 * time.Millisecond))
|
c3.SetWriteDeadline(time.Now().Add(100 * time.Millisecond))
|
||||||
c3.SetReadBuffer(2048)
|
c3.SetReadBuffer(2048)
|
||||||
c3.SetWriteBuffer(2048)
|
c3.SetWriteBuffer(2048)
|
||||||
defer c3.Close()
|
|
||||||
defer os.Remove(p3)
|
|
||||||
|
|
||||||
wb := []byte("UNIXCONN TEST")
|
wb := []byte("UNIXCONN TEST")
|
||||||
rb1 := make([]byte, 128)
|
rb1 := make([]byte, 128)
|
||||||
rb2 := make([]byte, 128)
|
rb2 := make([]byte, 128)
|
||||||
rb3 := make([]byte, 128)
|
rb3 := make([]byte, 128)
|
||||||
if _, _, err := c1.WriteMsgUnix(wb, nil, a2); err != nil {
|
if _, _, err := c1.WriteMsgUnix(wb, nil, a2); err != nil {
|
||||||
t.Errorf("net.UnixConn.WriteMsgUnix failed: %v", err)
|
t.Fatalf("UnixConn.WriteMsgUnix failed: %v", err)
|
||||||
return
|
|
||||||
}
|
}
|
||||||
if _, _, _, _, err := c2.ReadMsgUnix(rb2, nil); err != nil {
|
if _, _, _, _, err := c2.ReadMsgUnix(rb2, nil); err != nil {
|
||||||
t.Errorf("net.UnixConn.ReadMsgUnix failed: %v", err)
|
t.Fatalf("UnixConn.ReadMsgUnix failed: %v", err)
|
||||||
return
|
|
||||||
}
|
}
|
||||||
if _, err := c2.WriteToUnix(wb, a1); err != nil {
|
if _, err := c2.WriteToUnix(wb, a1); err != nil {
|
||||||
t.Errorf("net.UnixConn.WriteToUnix failed: %v", err)
|
t.Fatalf("UnixConn.WriteToUnix failed: %v", err)
|
||||||
return
|
|
||||||
}
|
}
|
||||||
if _, _, err := c1.ReadFromUnix(rb1); err != nil {
|
if _, _, err := c1.ReadFromUnix(rb1); err != nil {
|
||||||
t.Errorf("net.UnixConn.ReadFromUnix failed: %v", err)
|
t.Fatalf("UnixConn.ReadFromUnix failed: %v", err)
|
||||||
return
|
|
||||||
}
|
}
|
||||||
if _, err := c3.WriteToUnix(wb, a1); err != nil {
|
if _, err := c3.WriteToUnix(wb, a1); err != nil {
|
||||||
t.Errorf("net.UnixConn.WriteToUnix failed: %v", err)
|
t.Fatalf("UnixConn.WriteToUnix failed: %v", err)
|
||||||
return
|
|
||||||
}
|
}
|
||||||
if _, _, err := c1.ReadFromUnix(rb1); err != nil {
|
if _, _, err := c1.ReadFromUnix(rb1); err != nil {
|
||||||
t.Errorf("net.UnixConn.ReadFromUnix failed: %v", err)
|
t.Fatalf("UnixConn.ReadFromUnix failed: %v", err)
|
||||||
return
|
|
||||||
}
|
}
|
||||||
if _, err := c2.WriteToUnix(wb, a3); err != nil {
|
if _, err := c2.WriteToUnix(wb, a3); err != nil {
|
||||||
t.Errorf("net.UnixConn.WriteToUnix failed: %v", err)
|
t.Fatalf("UnixConn.WriteToUnix failed: %v", err)
|
||||||
return
|
|
||||||
}
|
}
|
||||||
if _, _, err := c3.ReadFromUnix(rb3); err != nil {
|
if _, _, err := c3.ReadFromUnix(rb3); err != nil {
|
||||||
t.Errorf("net.UnixConn.ReadFromUnix failed: %v", err)
|
t.Fatalf("UnixConn.ReadFromUnix failed: %v", err)
|
||||||
return
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if f, err := c1.File(); err != nil {
|
if f, err := c1.File(); err != nil {
|
||||||
t.Errorf("net.UnixConn.File failed: %v", err)
|
t.Fatalf("UnixConn.File failed: %v", err)
|
||||||
return
|
|
||||||
} else {
|
} else {
|
||||||
f.Close()
|
f.Close()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func newICMPEchoRequest(id, seqnum, msglen int, filler []byte) []byte {
|
|
||||||
b := newICMPInfoMessage(id, seqnum, msglen, filler)
|
|
||||||
b[0] = 8
|
|
||||||
// calculate ICMP checksum
|
|
||||||
cklen := len(b)
|
|
||||||
s := uint32(0)
|
|
||||||
for i := 0; i < cklen-1; i += 2 {
|
|
||||||
s += uint32(b[i+1])<<8 | uint32(b[i])
|
|
||||||
}
|
|
||||||
if cklen&1 == 1 {
|
|
||||||
s += uint32(b[cklen-1])
|
|
||||||
}
|
|
||||||
s = (s >> 16) + (s & 0xffff)
|
|
||||||
s = s + (s >> 16)
|
|
||||||
// place checksum back in header; using ^= avoids the
|
|
||||||
// assumption the checksum bytes are zero
|
|
||||||
b[2] ^= byte(^s & 0xff)
|
|
||||||
b[3] ^= byte(^s >> 8)
|
|
||||||
return b
|
|
||||||
}
|
|
||||||
|
|
||||||
func newICMPInfoMessage(id, seqnum, msglen int, filler []byte) []byte {
|
|
||||||
b := make([]byte, msglen)
|
|
||||||
copy(b[8:], bytes.Repeat(filler, (msglen-8)/len(filler)+1))
|
|
||||||
b[0] = 0 // type
|
|
||||||
b[1] = 0 // code
|
|
||||||
b[2] = 0 // checksum
|
|
||||||
b[3] = 0 // checksum
|
|
||||||
b[4] = byte(id >> 8) // identifier
|
|
||||||
b[5] = byte(id & 0xff) // identifier
|
|
||||||
b[6] = byte(seqnum >> 8) // sequence number
|
|
||||||
b[7] = byte(seqnum & 0xff) // sequence number
|
|
||||||
return b
|
|
||||||
}
|
|
||||||
|
Loading…
Reference in New Issue
Block a user