Home | History | Annotate | Download | only in net
      1 // Copyright 2012 The Go Authors. All rights reserved.
      2 // Use of this source code is governed by a BSD-style
      3 // license that can be found in the LICENSE file.
      4 
      5 // This file implements API tests across platforms and will never have a build
      6 // tag.
      7 
      8 package net
      9 
     10 import (
     11 	"testing"
     12 	"time"
     13 )
     14 
     15 // someTimeout is used just to test that net.Conn implementations
     16 // don't explode when their SetFooDeadline methods are called.
     17 // It isn't actually used for testing timeouts.
     18 const someTimeout = 10 * time.Second
     19 
     20 func TestConnAndListener(t *testing.T) {
     21 	for i, network := range []string{"tcp", "unix", "unixpacket"} {
     22 		if !testableNetwork(network) {
     23 			t.Logf("skipping %s test", network)
     24 			continue
     25 		}
     26 
     27 		ls, err := newLocalServer(network)
     28 		if err != nil {
     29 			t.Fatal(err)
     30 		}
     31 		defer ls.teardown()
     32 		ch := make(chan error, 1)
     33 		handler := func(ls *localServer, ln Listener) { transponder(ln, ch) }
     34 		if err := ls.buildup(handler); err != nil {
     35 			t.Fatal(err)
     36 		}
     37 		if ls.Listener.Addr().Network() != network {
     38 			t.Fatalf("got %s; want %s", ls.Listener.Addr().Network(), network)
     39 		}
     40 
     41 		c, err := Dial(ls.Listener.Addr().Network(), ls.Listener.Addr().String())
     42 		if err != nil {
     43 			t.Fatal(err)
     44 		}
     45 		defer c.Close()
     46 		if c.LocalAddr().Network() != network || c.RemoteAddr().Network() != network {
     47 			t.Fatalf("got %s->%s; want %s->%s", c.LocalAddr().Network(), c.RemoteAddr().Network(), network, network)
     48 		}
     49 		c.SetDeadline(time.Now().Add(someTimeout))
     50 		c.SetReadDeadline(time.Now().Add(someTimeout))
     51 		c.SetWriteDeadline(time.Now().Add(someTimeout))
     52 
     53 		if _, err := c.Write([]byte("CONN AND LISTENER TEST")); err != nil {
     54 			t.Fatal(err)
     55 		}
     56 		rb := make([]byte, 128)
     57 		if _, err := c.Read(rb); err != nil {
     58 			t.Fatal(err)
     59 		}
     60 
     61 		for err := range ch {
     62 			t.Errorf("#%d: %v", i, err)
     63 		}
     64 	}
     65 }
     66