Skip to content

Commit

Permalink
Unit tests for RunAsync
Browse files Browse the repository at this point in the history
  • Loading branch information
Ullaakut committed Oct 3, 2019
1 parent 6319634 commit da85b03
Show file tree
Hide file tree
Showing 3 changed files with 131 additions and 4 deletions.
2 changes: 1 addition & 1 deletion examples_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"log"
)

// A scanner can be instanciated with options to set the arguments
// A scanner can be instantiated with options to set the arguments
// that are given to nmap.
func ExampleScanner_simple() {
s, err := NewScanner(
Expand Down
2 changes: 1 addition & 1 deletion nmap.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ func (s *Scanner) Run() (*Run, error) {
case <-s.ctx.Done():
// Context was done before the scan was finished.
// The process is killed and a timeout error is returned.
cmd.Process.Kill()
_ = cmd.Process.Kill()

return nil, ErrScanTimeout
case <-done:
Expand Down
131 changes: 129 additions & 2 deletions nmap_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import (

func TestNmapNotInstalled(t *testing.T) {
oldPath := os.Getenv("PATH")
os.Setenv("PATH", "")
_ = os.Setenv("PATH", "")

s, err := NewScanner()
if err == nil {
Expand All @@ -28,7 +28,7 @@ func TestNmapNotInstalled(t *testing.T) {
t.Error("expected NewScanner to return a nil scanner if nmap is not found in $PATH")
}

os.Setenv("PATH", oldPath)
_ = os.Setenv("PATH", oldPath)
}

func TestRun(t *testing.T) {
Expand Down Expand Up @@ -225,6 +225,133 @@ func TestRun(t *testing.T) {
}
}

func TestRunAsync(t *testing.T) {
tests := []struct {
description string

options []func(*Scanner)

testTimeout bool
compareWholeRun bool

expectedResult *Run
expectedRunAsyncErr error
expectedWaitErr bool
expectedParseErr error
expectedNmapErr string
}{
{
description: "invalid binary path",

options: []func(*Scanner){
WithTargets("0.0.0.0"),
WithBinaryPath("/invalid"),
},

expectedResult: nil,
expectedRunAsyncErr: errors.New("unable to execute asynchronous nmap run: fork/exec /invalid: no such file or directory"),
},
{
description: "output can't be parsed",

options: []func(*Scanner){
WithTargets("0.0.0.0"),
WithBinaryPath("echo"),
},

expectedResult: nil,
expectedParseErr: errors.New("EOF"),
},
{
description: "context timeout",

options: []func(*Scanner){
WithTargets("0.0.0.0/16"),
},

testTimeout: true,

expectedResult: nil,
expectedWaitErr: true,
},
}

for _, test := range tests {
t.Run(test.description, func(t *testing.T) {
if test.testTimeout {
ctx, cancel := context.WithTimeout(context.Background(), 99*time.Hour)
test.options = append(test.options, WithContext(ctx))

go (func() {
// Cancel context to force timeout
defer cancel()
time.Sleep(1 * time.Millisecond)
})()
}

s, err := NewScanner(test.options...)
if err != nil {
panic(err) // this is never supposed to err, as we are testing run and not new.
}

err = s.RunAsync()
assert.Equal(t, test.expectedRunAsyncErr, err)
if err != nil {
return
}

stdout := s.GetStdout()
var content []byte
go func() {
for stdout.Scan() {
content = stdout.Bytes()
}
}()

err = s.Wait()
if test.expectedWaitErr {
assert.Error(t, err)
} else {
assert.NoError(t, err)
}
if err != nil {
return
}

result, err := Parse(content)
assert.Equal(t, test.expectedParseErr, err)

if test.expectedNmapErr != "" {
assert.Contains(t, result.NmapErrors, test.expectedNmapErr)
}

if result == nil && test.expectedResult == nil {
return
} else if result == nil && test.expectedResult != nil {
t.Error("expected non-nil result, got nil")
return
} else if test.expectedResult == nil {
return
}

if test.compareWholeRun {
result.rawXML = nil
if !reflect.DeepEqual(test.expectedResult, result) {
t.Errorf("expected result to be %+v, got %+v", test.expectedResult, result)
}
} else {
if result.Args != test.expectedResult.Args {
t.Errorf("expected args %q got %q", test.expectedResult.Args, result.Args)
}

if result.Scanner != test.expectedResult.Scanner {
t.Errorf("expected scanner %q got %q", test.expectedResult.Scanner, result.Scanner)
}
}
})
}
}

func TestTargetSpecification(t *testing.T) {
tests := []struct {
description string
Expand Down

0 comments on commit da85b03

Please sign in to comment.