2021-09-09 12:25:25 +00:00
|
|
|
//go:build !plan9
|
2019-04-15 19:03:33 +00:00
|
|
|
// +build !plan9
|
2018-03-19 16:37:40 +00:00
|
|
|
|
2017-08-06 10:49:52 +00:00
|
|
|
package sftp
|
|
|
|
|
|
|
|
import (
|
2017-08-07 13:50:31 +00:00
|
|
|
"fmt"
|
2017-08-06 10:49:52 +00:00
|
|
|
"testing"
|
2017-08-07 13:50:31 +00:00
|
|
|
|
2017-08-06 10:49:52 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
2021-10-27 15:01:54 +00:00
|
|
|
func TestShellEscapeUnix(t *testing.T) {
|
2017-08-06 10:49:52 +00:00
|
|
|
for i, test := range []struct {
|
|
|
|
unescaped, escaped string
|
|
|
|
}{
|
|
|
|
{"", ""},
|
|
|
|
{"/this/is/harmless", "/this/is/harmless"},
|
|
|
|
{"$(rm -rf /)", "\\$\\(rm\\ -rf\\ /\\)"},
|
|
|
|
{"/test/\n", "/test/'\n'"},
|
|
|
|
{":\"'", ":\\\"\\'"},
|
|
|
|
} {
|
2021-10-27 15:01:54 +00:00
|
|
|
got, err := quoteOrEscapeShellPath("unix", test.unescaped)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, test.escaped, got, fmt.Sprintf("Test %d unescaped = %q", i, test.unescaped))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestShellEscapeCmd(t *testing.T) {
|
|
|
|
for i, test := range []struct {
|
|
|
|
unescaped, escaped string
|
|
|
|
ok bool
|
|
|
|
}{
|
|
|
|
{"", "\"\"", true},
|
|
|
|
{"c:/this/is/harmless", "\"c:/this/is/harmless\"", true},
|
|
|
|
{"c:/test¬epad", "\"c:/test¬epad\"", true},
|
|
|
|
{"c:/test\"&\"notepad", "", false},
|
|
|
|
} {
|
|
|
|
got, err := quoteOrEscapeShellPath("cmd", test.unescaped)
|
|
|
|
if test.ok {
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, test.escaped, got, fmt.Sprintf("Test %d unescaped = %q", i, test.unescaped))
|
|
|
|
} else {
|
|
|
|
assert.Error(t, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestShellEscapePowerShell(t *testing.T) {
|
|
|
|
for i, test := range []struct {
|
|
|
|
unescaped, escaped string
|
|
|
|
}{
|
|
|
|
{"", "''"},
|
|
|
|
{"c:/this/is/harmless", "'c:/this/is/harmless'"},
|
|
|
|
{"c:/test¬epad", "'c:/test¬epad'"},
|
|
|
|
{"c:/test\"&\"notepad", "'c:/test\"&\"notepad'"},
|
|
|
|
{"c:/test'&'notepad", "'c:/test''&''notepad'"},
|
|
|
|
} {
|
|
|
|
got, err := quoteOrEscapeShellPath("powershell", test.unescaped)
|
|
|
|
assert.NoError(t, err)
|
2017-08-06 10:49:52 +00:00
|
|
|
assert.Equal(t, test.escaped, got, fmt.Sprintf("Test %d unescaped = %q", i, test.unescaped))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestParseHash(t *testing.T) {
|
|
|
|
for i, test := range []struct {
|
|
|
|
sshOutput, checksum string
|
|
|
|
}{
|
|
|
|
{"8dbc7733dbd10d2efc5c0a0d8dad90f958581821 RELEASE.md\n", "8dbc7733dbd10d2efc5c0a0d8dad90f958581821"},
|
|
|
|
{"03cfd743661f07975fa2f1220c5194cbaff48451 -\n", "03cfd743661f07975fa2f1220c5194cbaff48451"},
|
|
|
|
} {
|
|
|
|
got := parseHash([]byte(test.sshOutput))
|
|
|
|
assert.Equal(t, test.checksum, got, fmt.Sprintf("Test %d sshOutput = %q", i, test.sshOutput))
|
|
|
|
}
|
|
|
|
}
|
2019-04-25 09:51:15 +00:00
|
|
|
|
|
|
|
func TestParseUsage(t *testing.T) {
|
|
|
|
for i, test := range []struct {
|
|
|
|
sshOutput string
|
|
|
|
usage [3]int64
|
|
|
|
}{
|
|
|
|
{"Filesystem 1K-blocks Used Available Use% Mounted on\n/dev/root 91283092 81111888 10154820 89% /", [3]int64{93473886208, 83058573312, 10398535680}},
|
|
|
|
{"Filesystem 1K-blocks Used Available Use% Mounted on\ntmpfs 818256 1636 816620 1% /run", [3]int64{837894144, 1675264, 836218880}},
|
|
|
|
{"Filesystem 1024-blocks Used Available Capacity iused ifree %iused Mounted on\n/dev/disk0s2 244277768 94454848 149566920 39% 997820 4293969459 0% /", [3]int64{250140434432, 96721764352, 153156526080}},
|
|
|
|
} {
|
|
|
|
gotSpaceTotal, gotSpaceUsed, gotSpaceAvail := parseUsage([]byte(test.sshOutput))
|
|
|
|
assert.Equal(t, test.usage, [3]int64{gotSpaceTotal, gotSpaceUsed, gotSpaceAvail}, fmt.Sprintf("Test %d sshOutput = %q", i, test.sshOutput))
|
|
|
|
}
|
|
|
|
}
|