-
Notifications
You must be signed in to change notification settings - Fork 10
/
ssh_test.go
110 lines (97 loc) · 2.1 KB
/
ssh_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
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
package main
import (
"testing"
"golang.org/x/crypto/ssh/knownhosts"
)
func TestSetKey(t *testing.T) {
gSSH := grapeSSH{}
if err := gSSH.setKey(keyPath("testFiles/id_rsa")); err != nil {
t.FailNow()
}
if err := gSSH.setKey(keyPath("testFiles/id_rsa_123")); err == nil {
t.FailNow()
}
if err := gSSH.setKey(keyPath("testFiles/id_rsa.pub")); err == nil {
t.FailNow()
}
}
func TestNewClient(t *testing.T) {
gSSH := grapeSSH{}
gSSH.setKey("testFiles/id_rsa")
s := server{
Host: "sdf.org:22",
User: "new",
Name: "public",
}
hostKeyCallback, err := knownhosts.New("testFiles/known_hosts")
if err != nil {
t.FailNow()
}
if _, err := gSSH.newClient(s, hostKeyCallback); err != nil {
t.FailNow()
}
s.Host = "localhost"
if _, err := gSSH.newClient(s, hostKeyCallback); err == nil {
t.FailNow()
}
}
func TestExecCommand(t *testing.T) {
gSSH := grapeSSH{}
gSSH.setKey("testFiles/id_rsa")
var client *grapeSSHClient
var err error
var output *sshOutput
s := server{
Host: "sdf.org:22",
User: "new",
Name: "public",
}
hostKeyCallback, err := knownhosts.New("testFiles/known_hosts")
if err != nil {
t.Fatal(err)
}
client, err = gSSH.newClient(s, hostKeyCallback)
if err != nil {
t.Fatal(err)
}
output = client.execCommand("ls -al")
if output.Std.Err == "could not establish ssh session" {
t.FailNow()
}
// make that panic
client.Close()
output = client.execCommand("ls -al")
if output.Std.Err != "could not establish ssh session" {
t.FailNow()
}
}
func TestExecCommands(t *testing.T) {
demoCommands := commands{
command("ls -al"),
command("ls -al"),
}
gSSH := grapeSSH{}
gSSH.setKey("testFiles/id_rsa")
var client *grapeSSHClient
var err error
s := server{
Host: "sdf.org:22",
User: "new",
Name: "public",
}
hostKeyCallback, err := knownhosts.New("testFiles/known_hosts")
if err != nil {
t.FailNow()
}
client, err = gSSH.newClient(s, hostKeyCallback)
if err != nil {
t.FailNow()
}
defer client.Close()
output := client.execCommands(demoCommands)
for _, v := range output {
if v.Std.Err == "could not establish ssh session" {
t.FailNow()
}
}
}