|
| 1 | +package cli |
| 2 | + |
| 3 | +import ( |
| 4 | +"os" |
| 5 | +"os/exec" |
| 6 | +"path/filepath" |
| 7 | +"strings" |
| 8 | +"testing" |
| 9 | + |
| 10 | +"github.com/stretchr/testify/require" |
| 11 | +) |
| 12 | + |
| 13 | +funcTest_sshConfigExecEscape(t*testing.T) { |
| 14 | +t.Parallel() |
| 15 | + |
| 16 | +tests:= []struct { |
| 17 | +namestring |
| 18 | +pathstring |
| 19 | +wantErrbool |
| 20 | +}{ |
| 21 | +{"no spaces","simple",false}, |
| 22 | +{"spaces","path with spaces",false}, |
| 23 | +{"quotes","path with\"quotes\"",false}, |
| 24 | +{"backslashes","path with\\backslashes",false}, |
| 25 | +{"tabs","path with\ttabs",false}, |
| 26 | +{"newline fails","path with\nnewline",true}, |
| 27 | +} |
| 28 | +for_,tt:=rangetests { |
| 29 | +tt:=tt |
| 30 | +t.Run(tt.name,func(t*testing.T) { |
| 31 | +t.Parallel() |
| 32 | + |
| 33 | +dir:=filepath.Join(t.TempDir(),tt.path) |
| 34 | +err:=os.MkdirAll(dir,0o755) |
| 35 | +require.NoError(t,err) |
| 36 | +bin:=filepath.Join(dir,"coder") |
| 37 | +err=os.WriteFile(bin, []byte("#!/bin/sh\necho yay\n"),0o755)//nolint:gosec |
| 38 | +require.NoError(t,err) |
| 39 | + |
| 40 | +escaped,err:=sshConfigExecEscape(bin) |
| 41 | +iftt.wantErr { |
| 42 | +require.Error(t,err) |
| 43 | +return |
| 44 | +} |
| 45 | +require.NoError(t,err) |
| 46 | + |
| 47 | +b,err:=exec.Command("/bin/sh","-c",escaped).Output() |
| 48 | +require.NoError(t,err) |
| 49 | +got:=strings.TrimSpace(string(b)) |
| 50 | +require.Equal(t,"yay",got) |
| 51 | +}) |
| 52 | +} |
| 53 | +} |