github.com/10XDev/rclone@v1.52.3-0.20200626220027-16af9ab76b2a/backend/sftp/sftp_internal_test.go (about)

     1  // +build !plan9
     2  
     3  package sftp
     4  
     5  import (
     6  	"fmt"
     7  	"testing"
     8  
     9  	"github.com/stretchr/testify/assert"
    10  )
    11  
    12  func TestShellEscape(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 := shellEscape(test.unescaped)
    23  		assert.Equal(t, test.escaped, got, fmt.Sprintf("Test %d unescaped = %q", i, test.unescaped))
    24  	}
    25  }
    26  
    27  func TestParseHash(t *testing.T) {
    28  	for i, test := range []struct {
    29  		sshOutput, checksum string
    30  	}{
    31  		{"8dbc7733dbd10d2efc5c0a0d8dad90f958581821  RELEASE.md\n", "8dbc7733dbd10d2efc5c0a0d8dad90f958581821"},
    32  		{"03cfd743661f07975fa2f1220c5194cbaff48451  -\n", "03cfd743661f07975fa2f1220c5194cbaff48451"},
    33  	} {
    34  		got := parseHash([]byte(test.sshOutput))
    35  		assert.Equal(t, test.checksum, got, fmt.Sprintf("Test %d sshOutput = %q", i, test.sshOutput))
    36  	}
    37  }
    38  
    39  func TestParseUsage(t *testing.T) {
    40  	for i, test := range []struct {
    41  		sshOutput string
    42  		usage     [3]int64
    43  	}{
    44  		{"Filesystem     1K-blocks     Used Available Use% Mounted on\n/dev/root       91283092 81111888  10154820  89% /", [3]int64{93473886208, 83058573312, 10398535680}},
    45  		{"Filesystem     1K-blocks  Used Available Use% Mounted on\ntmpfs             818256  1636    816620   1% /run", [3]int64{837894144, 1675264, 836218880}},
    46  		{"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}},
    47  	} {
    48  		gotSpaceTotal, gotSpaceUsed, gotSpaceAvail := parseUsage([]byte(test.sshOutput))
    49  		assert.Equal(t, test.usage, [3]int64{gotSpaceTotal, gotSpaceUsed, gotSpaceAvail}, fmt.Sprintf("Test %d sshOutput = %q", i, test.sshOutput))
    50  	}
    51  }