This repository was archived by the owner on Sep 7, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathssh_test.go
More file actions
66 lines (60 loc) · 1.37 KB
/
ssh_test.go
File metadata and controls
66 lines (60 loc) · 1.37 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
package fury
import (
"os/exec"
"testing"
)
func TestShellEscape(t *testing.T) {
cases := map[string]string{
`foo`: `'foo'`,
`f'tang`: `'f'\''tang'`,
`'bar'`: `''\''bar'\'''`,
}
for in, want := range cases {
got := shellEscape(in)
if got != want {
t.Errorf("incorrect escaping\n got : %s\n want: %s", got, want)
}
}
}
func TestCommandArgv(t *testing.T) {
cases := map[string]Command{
"foo bar r'lyeh\n": Command{
Path: "/usr/bin/echo",
Args: []string{"foo", "bar", "r'lyeh"},
},
"testing\n": Command{
Path: "/usr/bin/printenv",
Args: []string{"UNIVERSE"},
Env: map[string]string{
"UNIVERSE": "testing",
},
},
"/\n": Command{
Path: "/usr/bin/pwd",
Dir: "/",
},
"/tmp\nr'lyeh\n": Command{
Path: "/bin/sh",
Args: []string{"-c", "pwd && printenv UNIVERSE"},
Env: map[string]string{
"UNIVERSE": "r'lyeh",
},
Dir: "/tmp",
},
}
for want, in := range cases {
argv, err := commandArgv(&in)
if err != nil {
t.Fatalf("constructing commandline: %s", err)
}
out, err := exec.Command("/bin/sh", "-c", argv).CombinedOutput()
if err != nil {
t.Fatalf("executing command: %s (output %q)", err, string(out))
}
got := string(out)
if got != want {
t.Errorf("unexpected commandArgv result, got %q, want %q", got, want)
}
}
}
// TODO: run an ssh server and test what shell commands we receive.