Debian Codegraph @

sshcmd_test.go

raw
1 package desync
2
3 import (
4 "os/exec"
5 "testing"
6
7 "github.com/stretchr/testify/assert"
8 "github.com/stretchr/testify/require"
9 )
10
11 func TestValidateSSHHost(t *testing.T) {
12 valid := []string{
13 "example.com",
14 "[email protected]",
15 "[email protected]:2222",
16 "192.0.2.1",
17 "a-b.example.com", // dash allowed mid-string
18 }
19 for _, h := range valid {
20 assert.NoError(t, validateSSHHost(h), "%q should be accepted", h)
21 }
22
23 invalid := []string{
24 "-",
25 "-x",
26 "-oProxyCommand=touch${IFS}pwned",
27 "[email protected]",
28 }
29 for _, h := range invalid {
30 assert.Error(t, validateSSHHost(h), "%q should be rejected", h)
31 }
32 }
33
34 func TestShellQuote(t *testing.T) {
35 // Each input must survive a round-trip through a POSIX shell unchanged,
36 // proving the quoting can't be broken out of.
37 inputs := []string{
38 "",
39 "/path/to/store/",
40 "plain",
41 "o'brien",
42 `a'; touch pwned; '`,
43 `'`,
44 `''`,
45 `back\slash`,
46 `$VAR ${IFS} "dq" |&;<>`,
47 }
48 for _, in := range inputs {
49 out, err := exec.Command("sh", "-c", "printf %s "+shellQuote(in)).Output()
50 require.NoError(t, err, "input %q", in)
51 assert.Equal(t, in, string(out), "input %q did not round-trip", in)
52 }
53 }