Retro68/gcc/libgo/go/net/packetconn_test.go

154 lines
3.5 KiB
Go
Raw Normal View History

2014-09-21 17:33:12 +00:00
// Copyright 2012 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// This file implements API tests across platforms and will never have a build
// tag.
package net
import (
"os"
"testing"
"time"
)
2017-04-10 11:32:00 +00:00
// The full stack test cases for IPConn have been moved to the
// following:
// golang.org/x/net/ipv4
// golang.org/x/net/ipv6
// golang.org/x/net/icmp
func packetConnTestData(t *testing.T, network string) ([]byte, func()) {
if !testableNetwork(network) {
return nil, func() { t.Logf("skipping %s test", network) }
2014-09-21 17:33:12 +00:00
}
2017-04-10 11:32:00 +00:00
return []byte("PACKETCONN TEST"), nil
2014-09-21 17:33:12 +00:00
}
var packetConnTests = []struct {
net string
2015-08-28 15:33:40 +00:00
addr1 string
addr2 string
2014-09-21 17:33:12 +00:00
}{
2015-08-28 15:33:40 +00:00
{"udp", "127.0.0.1:0", "127.0.0.1:0"},
{"unixgram", testUnixAddr(), testUnixAddr()},
2014-09-21 17:33:12 +00:00
}
func TestPacketConn(t *testing.T) {
closer := func(c PacketConn, net, addr1, addr2 string) {
c.Close()
switch net {
case "unixgram":
os.Remove(addr1)
os.Remove(addr2)
}
}
2017-04-10 11:32:00 +00:00
for _, tt := range packetConnTests {
wb, skipOrFatalFn := packetConnTestData(t, tt.net)
2014-09-21 17:33:12 +00:00
if skipOrFatalFn != nil {
skipOrFatalFn()
continue
}
2015-08-28 15:33:40 +00:00
c1, err := ListenPacket(tt.net, tt.addr1)
2014-09-21 17:33:12 +00:00
if err != nil {
2017-04-10 11:32:00 +00:00
t.Fatal(err)
2014-09-21 17:33:12 +00:00
}
2017-04-10 11:32:00 +00:00
defer closer(c1, tt.net, tt.addr1, tt.addr2)
2014-09-21 17:33:12 +00:00
c1.LocalAddr()
2017-04-10 11:32:00 +00:00
c1.SetDeadline(time.Now().Add(500 * time.Millisecond))
c1.SetReadDeadline(time.Now().Add(500 * time.Millisecond))
c1.SetWriteDeadline(time.Now().Add(500 * time.Millisecond))
2014-09-21 17:33:12 +00:00
2015-08-28 15:33:40 +00:00
c2, err := ListenPacket(tt.net, tt.addr2)
2014-09-21 17:33:12 +00:00
if err != nil {
2017-04-10 11:32:00 +00:00
t.Fatal(err)
2014-09-21 17:33:12 +00:00
}
2017-04-10 11:32:00 +00:00
defer closer(c2, tt.net, tt.addr1, tt.addr2)
2014-09-21 17:33:12 +00:00
c2.LocalAddr()
2017-04-10 11:32:00 +00:00
c2.SetDeadline(time.Now().Add(500 * time.Millisecond))
c2.SetReadDeadline(time.Now().Add(500 * time.Millisecond))
c2.SetWriteDeadline(time.Now().Add(500 * time.Millisecond))
rb2 := make([]byte, 128)
2014-09-21 17:33:12 +00:00
if _, err := c1.WriteTo(wb, c2.LocalAddr()); err != nil {
2017-04-10 11:32:00 +00:00
t.Fatal(err)
2014-09-21 17:33:12 +00:00
}
if _, _, err := c2.ReadFrom(rb2); err != nil {
2017-04-10 11:32:00 +00:00
t.Fatal(err)
2014-09-21 17:33:12 +00:00
}
if _, err := c2.WriteTo(wb, c1.LocalAddr()); err != nil {
2017-04-10 11:32:00 +00:00
t.Fatal(err)
2014-09-21 17:33:12 +00:00
}
rb1 := make([]byte, 128)
if _, _, err := c1.ReadFrom(rb1); err != nil {
2017-04-10 11:32:00 +00:00
t.Fatal(err)
2014-09-21 17:33:12 +00:00
}
}
}
func TestConnAndPacketConn(t *testing.T) {
closer := func(c PacketConn, net, addr1, addr2 string) {
c.Close()
switch net {
case "unixgram":
os.Remove(addr1)
os.Remove(addr2)
}
}
2017-04-10 11:32:00 +00:00
for _, tt := range packetConnTests {
2014-09-21 17:33:12 +00:00
var wb []byte
2017-04-10 11:32:00 +00:00
wb, skipOrFatalFn := packetConnTestData(t, tt.net)
2014-09-21 17:33:12 +00:00
if skipOrFatalFn != nil {
skipOrFatalFn()
continue
}
2015-08-28 15:33:40 +00:00
c1, err := ListenPacket(tt.net, tt.addr1)
2014-09-21 17:33:12 +00:00
if err != nil {
2017-04-10 11:32:00 +00:00
t.Fatal(err)
2014-09-21 17:33:12 +00:00
}
2017-04-10 11:32:00 +00:00
defer closer(c1, tt.net, tt.addr1, tt.addr2)
2014-09-21 17:33:12 +00:00
c1.LocalAddr()
2017-04-10 11:32:00 +00:00
c1.SetDeadline(time.Now().Add(500 * time.Millisecond))
c1.SetReadDeadline(time.Now().Add(500 * time.Millisecond))
c1.SetWriteDeadline(time.Now().Add(500 * time.Millisecond))
2014-09-21 17:33:12 +00:00
c2, err := Dial(tt.net, c1.LocalAddr().String())
if err != nil {
2017-04-10 11:32:00 +00:00
t.Fatal(err)
2014-09-21 17:33:12 +00:00
}
defer c2.Close()
c2.LocalAddr()
c2.RemoteAddr()
2017-04-10 11:32:00 +00:00
c2.SetDeadline(time.Now().Add(500 * time.Millisecond))
c2.SetReadDeadline(time.Now().Add(500 * time.Millisecond))
c2.SetWriteDeadline(time.Now().Add(500 * time.Millisecond))
2014-09-21 17:33:12 +00:00
if _, err := c2.Write(wb); err != nil {
2017-04-10 11:32:00 +00:00
t.Fatal(err)
2014-09-21 17:33:12 +00:00
}
rb1 := make([]byte, 128)
if _, _, err := c1.ReadFrom(rb1); err != nil {
2017-04-10 11:32:00 +00:00
t.Fatal(err)
2014-09-21 17:33:12 +00:00
}
var dst Addr
2017-04-10 11:32:00 +00:00
switch tt.net {
2014-09-21 17:33:12 +00:00
case "unixgram":
continue
default:
dst = c2.LocalAddr()
}
if _, err := c1.WriteTo(wb, dst); err != nil {
2017-04-10 11:32:00 +00:00
t.Fatal(err)
2014-09-21 17:33:12 +00:00
}
rb2 := make([]byte, 128)
if _, err := c2.Read(rb2); err != nil {
2017-04-10 11:32:00 +00:00
t.Fatal(err)
2014-09-21 17:33:12 +00:00
}
}
}