aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorShulhan <ms@kilabit.info>2023-12-22 00:18:24 +0700
committerShulhan <ms@kilabit.info>2023-12-22 00:18:56 +0700
commit66a18a789edaa35dfa4446846eeeffabfee0b269 (patch)
tree97fe49378e4e77e5e83138c5cf696a9d2c9152a5
parent1ba33791ffb7be31358ed776448ee86b4438dad3 (diff)
downloadawwan-66a18a789edaa35dfa4446846eeeffabfee0b269.tar.xz
all: check script file is a directory
This changes we make sure that the passed script path is not a directory to prevent error when running play with unknown host name.
-rw-r--r--awwan_local_test.go36
-rw-r--r--awwan_play_test.go50
-rw-r--r--exec_request.go9
3 files changed, 67 insertions, 28 deletions
diff --git a/awwan_local_test.go b/awwan_local_test.go
index f13d09f..5755846 100644
--- a/awwan_local_test.go
+++ b/awwan_local_test.go
@@ -18,8 +18,10 @@ import (
func TestAwwanLocal(t *testing.T) {
type testCase struct {
- lineRange string
- tagOutput string
+ scriptFile string
+ lineRange string
+ tagOutput string
+ expError string
}
var (
@@ -49,23 +51,30 @@ func TestAwwanLocal(t *testing.T) {
aww.cryptoc.termrw = &mockrw
var cases = []testCase{{
- lineRange: `1-`,
- tagOutput: `local:1-`,
+ scriptFile: filepath.Join(baseDir, `local.aww`),
+ lineRange: `1-`,
+ tagOutput: `local:1-`,
}, {
- lineRange: `100-`,
- tagOutput: `local:100-`,
+ scriptFile: filepath.Join(baseDir, `local.aww`),
+ lineRange: `100-`,
+ tagOutput: `local:100-`,
+ }, {
+ // Pass directory as script.
+ scriptFile: filepath.Join(baseDir, `sub`),
+ lineRange: `1-`,
+ expError: `NewExecRequest: "testdata/local/sub" is a directory`,
}}
var (
- scriptFile = filepath.Join(baseDir, `local.aww`)
- req *ExecRequest
- logw bytes.Buffer
- c testCase
+ req *ExecRequest
+ logw bytes.Buffer
+ c testCase
)
for _, c = range cases {
- req, err = NewExecRequest(CommandModeLocal, scriptFile, c.lineRange)
+ req, err = NewExecRequest(CommandModeLocal, c.scriptFile, c.lineRange)
if err != nil {
- t.Fatal(err)
+ test.Assert(t, `error`, c.expError, err.Error())
+ continue
}
logw.Reset()
@@ -73,7 +82,8 @@ func TestAwwanLocal(t *testing.T) {
err = aww.Local(req)
if err != nil {
- t.Fatal(err)
+ test.Assert(t, `error`, c.expError, err.Error())
+ continue
}
test.Assert(t, `stdout`, string(tdata.Output[c.tagOutput]), logw.String())
diff --git a/awwan_play_test.go b/awwan_play_test.go
index bba59fc..f36d021 100644
--- a/awwan_play_test.go
+++ b/awwan_play_test.go
@@ -29,11 +29,17 @@ type testCaseGetPut struct {
}
func TestAwwan_Play_withLocal(t *testing.T) {
+ type testCase struct {
+ scriptFile string
+ lineRange string
+ expOutput string
+ expError string
+ }
+
var (
- baseDir = `testdata/play`
- scriptDir = filepath.Join(baseDir, `awwanssh.test`)
- scriptFile = filepath.Join(scriptDir, `play.aww`)
- tdataFile = filepath.Join(scriptDir, `play_test.data`)
+ baseDir = `testdata/play`
+ scriptDir = filepath.Join(baseDir, `awwanssh.test`)
+ tdataFile = filepath.Join(scriptDir, `play_test.data`)
tdata *test.Data
aww *Awwan
@@ -50,25 +56,39 @@ func TestAwwan_Play_withLocal(t *testing.T) {
t.Fatal(err)
}
+ var cases = []testCase{{
+ scriptFile: filepath.Join(scriptDir, `play.aww`),
+ lineRange: `1-`,
+ expOutput: string(tdata.Output[`play_with_local:output`]),
+ }, {
+ scriptFile: filepath.Join(scriptDir, `tmp`),
+ expError: `NewExecRequest: "testdata/play/awwanssh.test/tmp" is a directory`,
+ }}
+
var (
+ c testCase
req *ExecRequest
logw bytes.Buffer
)
- req, err = NewExecRequest(CommandModePlay, scriptFile, `1-`)
- if err != nil {
- t.Fatal(err)
- }
+ for _, c = range cases {
+ req, err = NewExecRequest(CommandModePlay, c.scriptFile, c.lineRange)
+ if err != nil {
+ test.Assert(t, `NewExecRequest: error`, c.expError, err.Error())
+ continue
+ }
- req.registerLogWriter(`output`, &logw)
+ logw.Reset()
+ req.registerLogWriter(`output`, &logw)
- err = aww.Play(req)
- if err != nil {
- t.Fatal(err)
- }
+ err = aww.Play(req)
+ if err != nil {
+ test.Assert(t, `Play: error`, c.expError, err.Error())
+ continue
+ }
- var exp = string(tdata.Output[`play_with_local:output`])
- test.Assert(t, `play_with_local:output`, exp, logw.String())
+ test.Assert(t, `Local`, c.expOutput, logw.String())
+ }
}
func TestAwwan_Play_Get(t *testing.T) {
diff --git a/exec_request.go b/exec_request.go
index 8d78d3d..16ce595 100644
--- a/exec_request.go
+++ b/exec_request.go
@@ -97,6 +97,15 @@ func (req *ExecRequest) init(workDir string) (err error) {
return err
}
+ var fi os.FileInfo
+ fi, err = os.Stat(req.scriptPath)
+ if err != nil {
+ return err
+ }
+ if fi.Mode().IsDir() {
+ return fmt.Errorf(`%q is a directory`, req.Script)
+ }
+
req.lineRange = parseLineRange(req.LineRange)
// Create log file to record all input and output for audit in the