2016-03-01 15:57:46 -07:00
|
|
|
// Copyright 2012 The Go Authors. All rights reserved.
|
2012-10-21 15:17:51 -06:00
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
2013-02-19 08:02:01 -07:00
|
|
|
// This file implements API tests across platforms and will never have a build
|
|
|
|
// tag.
|
|
|
|
|
|
|
|
package net
|
2012-10-21 15:17:51 -06:00
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
2013-03-04 12:55:27 -07:00
|
|
|
// someTimeout is used just to test that net.Conn implementations
|
|
|
|
// don't explode when their SetFooDeadline methods are called.
|
|
|
|
// It isn't actually used for testing timeouts.
|
|
|
|
const someTimeout = 10 * time.Second
|
|
|
|
|
2012-10-21 15:17:51 -06:00
|
|
|
func TestConnAndListener(t *testing.T) {
|
2015-04-19 08:42:11 -06:00
|
|
|
for i, network := range []string{"tcp", "unix", "unixpacket"} {
|
2015-04-02 08:11:39 -06:00
|
|
|
if !testableNetwork(network) {
|
|
|
|
t.Logf("skipping %s test", network)
|
2015-03-26 08:26:45 -06:00
|
|
|
continue
|
2012-10-21 15:17:51 -06:00
|
|
|
}
|
|
|
|
|
2015-04-02 08:11:39 -06:00
|
|
|
ls, err := newLocalServer(network)
|
2012-10-21 15:17:51 -06:00
|
|
|
if err != nil {
|
2015-04-30 21:38:42 -06:00
|
|
|
t.Fatal(err)
|
2012-10-21 15:17:51 -06:00
|
|
|
}
|
2015-04-02 08:11:39 -06:00
|
|
|
defer ls.teardown()
|
2015-04-19 08:42:11 -06:00
|
|
|
ch := make(chan error, 1)
|
|
|
|
handler := func(ls *localServer, ln Listener) { transponder(ln, ch) }
|
2015-04-02 08:11:39 -06:00
|
|
|
if err := ls.buildup(handler); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if ls.Listener.Addr().Network() != network {
|
|
|
|
t.Fatalf("got %s; want %s", ls.Listener.Addr().Network(), network)
|
2014-01-28 17:51:31 -07:00
|
|
|
}
|
2012-10-21 15:17:51 -06:00
|
|
|
|
2015-04-02 08:11:39 -06:00
|
|
|
c, err := Dial(ls.Listener.Addr().Network(), ls.Listener.Addr().String())
|
2012-10-21 15:17:51 -06:00
|
|
|
if err != nil {
|
2015-04-30 21:38:42 -06:00
|
|
|
t.Fatal(err)
|
2012-10-21 15:17:51 -06:00
|
|
|
}
|
2013-03-02 23:59:53 -07:00
|
|
|
defer c.Close()
|
2015-04-02 08:11:39 -06:00
|
|
|
if c.LocalAddr().Network() != network || c.LocalAddr().Network() != network {
|
2015-04-30 21:38:42 -06:00
|
|
|
t.Fatalf("got %s->%s; want %s->%s", c.LocalAddr().Network(), c.RemoteAddr().Network(), network, network)
|
2014-01-28 17:51:31 -07:00
|
|
|
}
|
2013-03-04 12:55:27 -07:00
|
|
|
c.SetDeadline(time.Now().Add(someTimeout))
|
|
|
|
c.SetReadDeadline(time.Now().Add(someTimeout))
|
|
|
|
c.SetWriteDeadline(time.Now().Add(someTimeout))
|
2012-10-21 15:17:51 -06:00
|
|
|
|
2015-04-30 21:38:42 -06:00
|
|
|
if _, err := c.Write([]byte("CONN AND LISTENER TEST")); err != nil {
|
|
|
|
t.Fatal(err)
|
2012-10-21 15:17:51 -06:00
|
|
|
}
|
|
|
|
rb := make([]byte, 128)
|
|
|
|
if _, err := c.Read(rb); err != nil {
|
2015-04-30 21:38:42 -06:00
|
|
|
t.Fatal(err)
|
2012-10-21 15:17:51 -06:00
|
|
|
}
|
2015-04-02 08:11:39 -06:00
|
|
|
|
2015-04-19 08:42:11 -06:00
|
|
|
for err := range ch {
|
|
|
|
t.Errorf("#%d: %v", i, err)
|
|
|
|
}
|
2012-10-21 15:17:51 -06:00
|
|
|
}
|
|
|
|
}
|