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.
9 // direct-tcpip and direct-streamlocal functional tests
20 type dialTester interface {
21 TestServerConn(t *testing.T, c net.Conn)
22 TestClientConn(t *testing.T, c net.Conn)
25 func testDial(t *testing.T, n, listenAddr string, x dialTester) {
26 server := newServer(t)
27 defer server.Shutdown()
28 sshConn := server.Dial(clientConfig())
31 l, err := net.Listen(n, listenAddr)
33 t.Fatalf("Listen: %v", err)
37 testData := fmt.Sprintf("hello from %s, %s", n, listenAddr)
44 x.TestServerConn(t, c)
46 io.WriteString(c, testData)
51 conn, err := sshConn.Dial(n, l.Addr().String())
53 t.Fatalf("Dial: %v", err)
55 x.TestClientConn(t, conn)
57 b, err := ioutil.ReadAll(conn)
59 t.Fatalf("ReadAll: %v", err)
61 t.Logf("got %q", string(b))
62 if string(b) != testData {
63 t.Fatalf("expected %q, got %q", testData, string(b))
67 type tcpDialTester struct {
71 func (x *tcpDialTester) TestServerConn(t *testing.T, c net.Conn) {
72 host := strings.Split(x.listenAddr, ":")[0]
74 if !strings.HasPrefix(c.LocalAddr().String(), prefix) {
75 t.Fatalf("expected to start with %q, got %q", prefix, c.LocalAddr().String())
77 if !strings.HasPrefix(c.RemoteAddr().String(), prefix) {
78 t.Fatalf("expected to start with %q, got %q", prefix, c.RemoteAddr().String())
82 func (x *tcpDialTester) TestClientConn(t *testing.T, c net.Conn) {
83 // we use zero addresses. see *Client.Dial.
84 if c.LocalAddr().String() != "0.0.0.0:0" {
85 t.Fatalf("expected \"0.0.0.0:0\", got %q", c.LocalAddr().String())
87 if c.RemoteAddr().String() != "0.0.0.0:0" {
88 t.Fatalf("expected \"0.0.0.0:0\", got %q", c.RemoteAddr().String())
92 func TestDialTCP(t *testing.T) {
94 listenAddr: "127.0.0.1:0",
96 testDial(t, "tcp", x.listenAddr, x)
99 type unixDialTester struct {
103 func (x *unixDialTester) TestServerConn(t *testing.T, c net.Conn) {
104 if c.LocalAddr().String() != x.listenAddr {
105 t.Fatalf("expected %q, got %q", x.listenAddr, c.LocalAddr().String())
107 if c.RemoteAddr().String() != "@" {
108 t.Fatalf("expected \"@\", got %q", c.RemoteAddr().String())
112 func (x *unixDialTester) TestClientConn(t *testing.T, c net.Conn) {
113 if c.RemoteAddr().String() != x.listenAddr {
114 t.Fatalf("expected %q, got %q", x.listenAddr, c.RemoteAddr().String())
116 if c.LocalAddr().String() != "@" {
117 t.Fatalf("expected \"@\", got %q", c.LocalAddr().String())
121 func TestDialUnix(t *testing.T) {
122 addr, cleanup := newTempSocket(t)
124 x := &unixDialTester{
127 testDial(t, "unix", x.listenAddr, x)