github.com/rclone/rclone@v1.66.1-0.20240517100346-7b89735ae726/backend/sftp/sftp_internal_test.go (about) 1 //go:build !plan9 2 3 package sftp 4 5 import ( 6 "fmt" 7 "testing" 8 9 "github.com/stretchr/testify/assert" 10 ) 11 12 func TestShellEscapeUnix(t *testing.T) { 13 for i, test := range []struct { 14 unescaped, escaped string 15 }{ 16 {"", ""}, 17 {"/this/is/harmless", "/this/is/harmless"}, 18 {"$(rm -rf /)", "\\$\\(rm\\ -rf\\ /\\)"}, 19 {"/test/\n", "/test/'\n'"}, 20 {":\"'", ":\\\"\\'"}, 21 } { 22 got, err := quoteOrEscapeShellPath("unix", test.unescaped) 23 assert.NoError(t, err) 24 assert.Equal(t, test.escaped, got, fmt.Sprintf("Test %d unescaped = %q", i, test.unescaped)) 25 } 26 } 27 28 func TestShellEscapeCmd(t *testing.T) { 29 for i, test := range []struct { 30 unescaped, escaped string 31 ok bool 32 }{ 33 {"", "\"\"", true}, 34 {"c:/this/is/harmless", "\"c:/this/is/harmless\"", true}, 35 {"c:/test¬epad", "\"c:/test¬epad\"", true}, 36 {"c:/test\"&\"notepad", "", false}, 37 } { 38 got, err := quoteOrEscapeShellPath("cmd", test.unescaped) 39 if test.ok { 40 assert.NoError(t, err) 41 assert.Equal(t, test.escaped, got, fmt.Sprintf("Test %d unescaped = %q", i, test.unescaped)) 42 } else { 43 assert.Error(t, err) 44 } 45 } 46 } 47 48 func TestShellEscapePowerShell(t *testing.T) { 49 for i, test := range []struct { 50 unescaped, escaped string 51 }{ 52 {"", "''"}, 53 {"c:/this/is/harmless", "'c:/this/is/harmless'"}, 54 {"c:/test¬epad", "'c:/test¬epad'"}, 55 {"c:/test\"&\"notepad", "'c:/test\"&\"notepad'"}, 56 {"c:/test'&'notepad", "'c:/test''&''notepad'"}, 57 } { 58 got, err := quoteOrEscapeShellPath("powershell", test.unescaped) 59 assert.NoError(t, err) 60 assert.Equal(t, test.escaped, got, fmt.Sprintf("Test %d unescaped = %q", i, test.unescaped)) 61 } 62 } 63 64 func TestParseHash(t *testing.T) { 65 for i, test := range []struct { 66 sshOutput, checksum string 67 }{ 68 {"8dbc7733dbd10d2efc5c0a0d8dad90f958581821 RELEASE.md\n", "8dbc7733dbd10d2efc5c0a0d8dad90f958581821"}, 69 {"03cfd743661f07975fa2f1220c5194cbaff48451 -\n", "03cfd743661f07975fa2f1220c5194cbaff48451"}, 70 } { 71 got := parseHash([]byte(test.sshOutput)) 72 assert.Equal(t, test.checksum, got, fmt.Sprintf("Test %d sshOutput = %q", i, test.sshOutput)) 73 } 74 } 75 76 func TestParseUsage(t *testing.T) { 77 for i, test := range []struct { 78 sshOutput string 79 usage [3]int64 80 }{ 81 {"Filesystem 1K-blocks Used Available Use% Mounted on\n/dev/root 91283092 81111888 10154820 89% /", [3]int64{93473886208, 83058573312, 10398535680}}, 82 {"Filesystem 1K-blocks Used Available Use% Mounted on\ntmpfs 818256 1636 816620 1% /run", [3]int64{837894144, 1675264, 836218880}}, 83 {"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}}, 84 } { 85 gotSpaceTotal, gotSpaceUsed, gotSpaceAvail := parseUsage([]byte(test.sshOutput)) 86 assert.Equal(t, test.usage, [3]int64{gotSpaceTotal, gotSpaceUsed, gotSpaceAvail}, fmt.Sprintf("Test %d sshOutput = %q", i, test.sshOutput)) 87 } 88 }