github.com/nilium/gitlab-runner@v12.5.0+incompatible/helpers/shell_escape_test.go (about) 1 package helpers 2 3 import ( 4 "crypto/rand" 5 "testing" 6 7 "github.com/stretchr/testify/assert" 8 ) 9 10 func BenchmarkShellEscape(b *testing.B) { 11 data := make([]byte, 1024*1024) 12 if _, err := rand.Read(data); err != nil { 13 panic(err) 14 } 15 dataStr := string(data) 16 17 for i := 0; i < b.N; i++ { 18 ShellEscape(dataStr) 19 } 20 } 21 22 func TestShellEscape(t *testing.T) { 23 var tests = []struct { 24 in string 25 out string 26 }{ 27 {"standard string", "$'standard string'"}, 28 {"+\t\n\r&", "$'+\\t\\n\\r&'"}, 29 {"", "''"}, 30 } 31 32 for _, test := range tests { 33 actual := ShellEscape(test.in) 34 assert.Equal(t, test.out, actual, "src=%v", test.in) 35 } 36 } 37 38 func TestToBackslash(t *testing.T) { 39 40 result := ToBackslash("smb://user/me/directory") 41 expected := "smb:\\\\user\\me\\directory" 42 43 if result != expected { 44 t.Error("Expected", expected, ", got ", result) 45 } 46 } 47 48 func TestToSlash(t *testing.T) { 49 50 result := ToSlash("smb:\\\\user\\me\\directory") 51 expected := "smb://user/me/directory" 52 53 if result != expected { 54 t.Error("Expected", expected, ", got ", result) 55 } 56 }