-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathserver_test.go
More file actions
159 lines (137 loc) · 5.35 KB
/
server_test.go
File metadata and controls
159 lines (137 loc) · 5.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
package sshd
import (
"net"
"testing"
"time"
"github.com/Masterminds/cookoo"
"golang.org/x/crypto/ssh"
)
var testingServerAddr = "127.0.0.1:2244"
// TestServer tests the SSH server.
//
// This listens on the non-standard port 2244 of localhost. This will generate
// an entry in your known_hosts file, and will tie that to the testing key
// used here. It's not recommended that you try to start another SSH server on
// the same port (at a later time) or else you will have key issues that you
// must manually resolve.
func TestServer(t *testing.T) {
key, err := sshTestingHostKey()
if err != nil {
t.Fatal(err)
}
cfg := ssh.ServerConfig{
NoClientAuth: true,
}
cfg.AddHostKey(key)
cxt := runServer(&cfg, t)
// Give server time to initialize.
time.Sleep(200 * time.Millisecond)
// Connect to the server and issue env var set. This should return true.
client, err := ssh.Dial("tcp", testingServerAddr, &ssh.ClientConfig{})
if err != nil {
t.Fatalf("Failed to connect client to local server: %s", err)
}
sess, err := client.NewSession()
if err != nil {
t.Fatalf("Failed to create client session: %s", err)
}
defer sess.Close()
if err := sess.Setenv("HELLO", "world"); err != nil {
t.Fatal(err)
}
if out, err := sess.Output("ping"); err != nil {
t.Errorf("Output '%s' Error %s", out, err)
} else if string(out) != "pong" {
t.Errorf("Expected 'pong', got '%s'", out)
}
// Create a new session because the success of the last one closed the
// connection.
sess, err = client.NewSession()
if err != nil {
t.Fatalf("Failed to create client session: %s", err)
}
if err := sess.Run("illegal"); err == nil {
t.Fatalf("expected a failed run with command 'illegal'")
}
if err := sess.Run("illegal command"); err == nil {
t.Fatalf("expected a failed run with command 'illegal command'")
}
closer := cxt.Get("sshd.Closer", nil).(chan interface{})
closer <- true
}
// sshTestingHostKey loads the testing key.
func sshTestingHostKey() (ssh.Signer, error) {
return ssh.ParsePrivateKey([]byte(testingHostKey))
}
func runServer(config *ssh.ServerConfig, t *testing.T) cookoo.Context {
reg, router, cxt := cookoo.Cookoo()
cxt.Put(ServerConfig, config)
cxt.Put(Address, testingServerAddr)
cxt.Put("cookoo.Router", router)
reg.AddRoute(cookoo.Route{
Name: "sshPing",
Help: "Handles an ssh exec ping.",
Does: cookoo.Tasks{
cookoo.Cmd{
Name: "ping",
Fn: Ping,
Using: []cookoo.Param{
{Name: "request", From: "cxt:request"},
{Name: "channel", From: "cxt:channel"},
},
},
},
})
go func() {
if err := Serve(reg, router, cxt); err != nil {
t.Fatalf("Failed serving with %s", err)
}
}()
return cxt
}
// connMetadata mocks ssh.ConnMetadata for authentication.
type connMetadata struct{}
func (cm *connMetadata) User() string { return "deis" }
func (cm *connMetadata) SessionID() []byte { return []byte("1") }
func (cm *connMetadata) ClientVersion() []byte { return []byte("2.3.4") }
func (cm *connMetadata) ServerVersion() []byte { return []byte("2.3.4") }
func (cm *connMetadata) RemoteAddr() net.Addr { return cm.localhost() }
func (cm *connMetadata) LocalAddr() net.Addr { return cm.localhost() }
func (cm *connMetadata) localhost() net.Addr {
addrs, err := net.InterfaceAddrs()
if err != nil {
panic(err)
}
return addrs[0]
}
var (
testingHostKey = `-----BEGIN RSA PRIVATE KEY-----
MIIEpAIBAAKCAQEA0xOK/wubqj+e4HNp+yAdK4WJnLZCvcjS2DwaxwF+E968kSeU
27SOqiol7Y0UwLGLpB6rpIBnSqXo70xiMUSrnteKmMejddzfbGkvnyvo0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