-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathnet_test.go
81 lines (66 loc) · 1.33 KB
/
net_test.go
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
package sshkit
import (
"bytes"
"io"
"testing"
)
func TestServerInit(t *testing.T) {
config := MakeNoAuth()
s, err := ListenSSH(":badport", config)
if err == nil {
t.Fatal("should fail on bad port")
}
s, err = ListenSSH(":0", config)
if err != nil {
t.Error(err)
}
err = s.Close()
if err != nil {
t.Error(err)
}
}
func TestServeTerminals(t *testing.T) {
signer, err := NewRandomSigner(512)
config := MakeNoAuth()
config.AddHostKey(signer)
s, err := ListenSSH(":0", config)
if err != nil {
t.Fatal(err)
}
terminals := s.ServeTerminal()
go func() {
// Accept one terminal, read from it, echo back, close.
term := <-terminals
term.SetPrompt("> ")
line, err := term.ReadLine()
if err != nil {
t.Error(err)
}
_, err = term.Write([]byte("echo: " + line + "\r\n"))
if err != nil {
t.Error(err)
}
term.Close()
}()
host := s.Addr().String()
name := "foo"
err = ConnectShell(host, name, func(r io.Reader, w io.WriteCloser) {
// Consume if there is anything
buf := new(bytes.Buffer)
w.Write([]byte("hello\r\n"))
buf.Reset()
_, err := io.Copy(buf, r)
if err != nil {
t.Error(err)
}
expected := "> hello\r\necho: hello\r\n"
actual := buf.String()
if actual != expected {
t.Errorf("Got %q; expected %q", actual, expected)
}
s.Close()
})
if err != nil {
t.Fatal(err)
}
}