-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathssh_test.go
More file actions
62 lines (56 loc) · 2.02 KB
/
Copy pathssh_test.go
File metadata and controls
62 lines (56 loc) · 2.02 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
package main
import (
"io"
"strings"
"testing"
)
func TestWaitForReady(t *testing.T) {
// A stream that eventually emits a READY line should return nil.
r := io.NopCloser(strings.NewReader("starting\nbinding socket\nREADY, listening on :8080\nmore\n"))
if err := waitForReady(r, true); err != nil {
t.Errorf("waitForReady returned error: %v", err)
}
}
func TestWaitForReadyNoReady(t *testing.T) {
r := io.NopCloser(strings.NewReader("line1\nline2\n"))
if err := waitForReady(r, false); err == nil {
t.Error("server exit before READY must fail")
}
}
func TestShellQuote(t *testing.T) {
got := shellQuote("/tmp/name with 'quote'.img")
want := "'/tmp/name with '\"'\"'quote'\"'\"'.img'"
if got != want {
t.Fatalf("shellQuote() = %q, want %q", got, want)
}
}
func TestRemoteBinaryCommandQuotesAndCleans(t *testing.T) {
cmd := remoteBinaryCommand("/tmp/bsync-1", []string{"-f", "/tmp/dst file; touch /tmp/bad", "-d"})
for _, part := range []string{"trap", "rm -f --", "'/tmp/bsync-1'", "'/tmp/dst file; touch /tmp/bad'", "'-d'"} {
if !strings.Contains(cmd, part) {
t.Fatalf("remote command %q missing %q", cmd, part)
}
}
}
func TestParseSSHTargetExtra(t *testing.T) {
tests := []struct {
name string
input string
wUser, wHost, wPort, wFile string
}{
{"host only path", "myhost:/data/disk.img", "", "myhost", "", "/data/disk.img"},
{"user host port path", "admin@10.0.0.5:2200:/srv/x", "admin", "10.0.0.5", "2200", "/srv/x"},
{"plain local path", "/var/lib/disk.img", "", "", "", "/var/lib/disk.img"},
{"relative local", "disk.img", "", "", "", "disk.img"},
{"windows style path", "user@host:2222:C:/data", "user", "host", "2222", "C:/data"},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
u, h, p, f := parseSSHTarget(tt.input)
if u != tt.wUser || h != tt.wHost || p != tt.wPort || f != tt.wFile {
t.Errorf("parseSSHTarget(%q) = (%q,%q,%q,%q), want (%q,%q,%q,%q)",
tt.input, u, h, p, f, tt.wUser, tt.wHost, tt.wPort, tt.wFile)
}
})
}
}