OSDN Git Service

new repo
[bytom/vapor.git] / vendor / golang.org / x / crypto / ssh / test / dial_unix_test.go
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 // +build !windows
6
7 package test
8
9 // direct-tcpip and direct-streamlocal functional tests
10
11 import (
12         "fmt"
13         "io"
14         "io/ioutil"
15         "net"
16         "strings"
17         "testing"
18 )
19
20 type dialTester interface {
21         TestServerConn(t *testing.T, c net.Conn)
22         TestClientConn(t *testing.T, c net.Conn)
23 }
24
25 func testDial(t *testing.T, n, listenAddr string, x dialTester) {
26         server := newServer(t)
27         defer server.Shutdown()
28         sshConn := server.Dial(clientConfig())
29         defer sshConn.Close()
30
31         l, err := net.Listen(n, listenAddr)
32         if err != nil {
33                 t.Fatalf("Listen: %v", err)
34         }
35         defer l.Close()
36
37         testData := fmt.Sprintf("hello from %s, %s", n, listenAddr)
38         go func() {
39                 for {
40                         c, err := l.Accept()
41                         if err != nil {
42                                 break
43                         }
44                         x.TestServerConn(t, c)
45
46                         io.WriteString(c, testData)
47                         c.Close()
48                 }
49         }()
50
51         conn, err := sshConn.Dial(n, l.Addr().String())
52         if err != nil {
53                 t.Fatalf("Dial: %v", err)
54         }
55         x.TestClientConn(t, conn)
56         defer conn.Close()
57         b, err := ioutil.ReadAll(conn)
58         if err != nil {
59                 t.Fatalf("ReadAll: %v", err)
60         }
61         t.Logf("got %q", string(b))
62         if string(b) != testData {
63                 t.Fatalf("expected %q, got %q", testData, string(b))
64         }
65 }
66
67 type tcpDialTester struct {
68         listenAddr string
69 }
70
71 func (x *tcpDialTester) TestServerConn(t *testing.T, c net.Conn) {
72         host := strings.Split(x.listenAddr, ":")[0]
73         prefix := host + ":"
74         if !strings.HasPrefix(c.LocalAddr().String(), prefix) {
75                 t.Fatalf("expected to start with %q, got %q", prefix, c.LocalAddr().String())
76         }
77         if !strings.HasPrefix(c.RemoteAddr().String(), prefix) {
78                 t.Fatalf("expected to start with %q, got %q", prefix, c.RemoteAddr().String())
79         }
80 }
81
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())
86         }
87         if c.RemoteAddr().String() != "0.0.0.0:0" {
88                 t.Fatalf("expected \"0.0.0.0:0\", got %q", c.RemoteAddr().String())
89         }
90 }
91
92 func TestDialTCP(t *testing.T) {
93         x := &tcpDialTester{
94                 listenAddr: "127.0.0.1:0",
95         }
96         testDial(t, "tcp", x.listenAddr, x)
97 }
98
99 type unixDialTester struct {
100         listenAddr string
101 }
102
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())
106         }
107         if c.RemoteAddr().String() != "@" {
108                 t.Fatalf("expected \"@\", got %q", c.RemoteAddr().String())
109         }
110 }
111
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())
115         }
116         if c.LocalAddr().String() != "@" {
117                 t.Fatalf("expected \"@\", got %q", c.LocalAddr().String())
118         }
119 }
120
121 func TestDialUnix(t *testing.T) {
122         addr, cleanup := newTempSocket(t)
123         defer cleanup()
124         x := &unixDialTester{
125                 listenAddr: addr,
126         }
127         testDial(t, "unix", x.listenAddr, x)
128 }