-
Notifications
You must be signed in to change notification settings - Fork 116
Expand file tree
/
Copy pathscan_sync_test.go
More file actions
175 lines (148 loc) · 3.56 KB
/
scan_sync_test.go
File metadata and controls
175 lines (148 loc) · 3.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
package nmap
import (
"context"
"encoding/xml"
"testing"
"time"
"github.com/stretchr/testify/require"
)
func TestRun(t *testing.T) {
tests := []struct {
description string
options []Option
testTimeout bool
compareWholeRun bool
useContainer bool
expectedResult *Run
wantErr require.ErrorAssertionFunc
}{
{
description: "scan localhost",
options: []Option{
WithTargets("localhost"),
WithTimingTemplate(TimingFastest),
},
useContainer: true,
expectedResult: &Run{
Scanner: "nmap",
Args: "nmap -T5 -oX - localhost",
},
wantErr: require.NoError,
},
{
description: "missing target",
options: []Option{
WithTimingTemplate(TimingFastest),
},
useContainer: true,
expectedResult: &Run{
Scanner: "nmap",
Args: "nmap -T5 -oX -",
Stats: Stats{Hosts: HostStats{Total: 0}},
},
wantErr: require.NoError,
},
{
description: "scan localhost with filters",
options: []Option{
WithBinaryPath("tests/scripts/fake_nmap.sh"),
WithCustomArguments("tests/xml/scan_invalid_services.xml"),
WithFilterHost(func(h Host) bool {
return len(h.Ports) == 2
}),
WithFilterPort(func(p Port) bool {
return p.Service.Product == "VALID"
}),
WithTimingTemplate(TimingFastest),
},
compareWholeRun: true,
expectedResult: &Run{
XMLName: xml.Name{Local: "nmaprun"},
Args: "nmap test",
Scanner: "fake_nmap",
Hosts: []Host{{
Addresses: []Address{{Addr: "66.35.250.168"}},
Ports: []Port{
{ID: 80, State: State{State: "open"}, Service: Service{Name: "http", Product: "VALID"}},
{ID: 443, State: State{State: "open"}, Service: Service{Name: "https", Product: "VALID"}},
},
},
},
},
wantErr: require.NoError,
},
{
description: "invalid binary path",
options: []Option{
WithTargets("0.0.0.0"),
WithBinaryPath("/invalid"),
},
wantErr: require.Error,
},
{
description: "output can't be parsed",
options: []Option{
WithTargets("0.0.0.0"),
WithBinaryPath("echo"),
},
wantErr: require.Error,
},
{
description: "context timeout",
options: []Option{
WithTargets("0.0.0.0/16"),
},
testTimeout: true,
useContainer: true,
wantErr: require.Error,
},
{
description: "scan error resolving name",
options: []Option{
WithBinaryPath("tests/scripts/fake_nmap.sh"),
WithCustomArguments("tests/xml/scan_error_resolving_name.xml"),
},
expectedResult: &Run{
Scanner: "fake_nmap",
Args: "nmap test",
},
wantErr: require.Error,
},
{
description: "scan unsupported error",
options: []Option{
WithBinaryPath("tests/scripts/fake_nmap.sh"),
WithCustomArguments("tests/xml/scan_error_other.xml"),
},
expectedResult: &Run{
Scanner: "fake_nmap",
Args: "nmap test",
},
wantErr: require.Error,
},
}
for _, test := range tests {
t.Run(test.description, func(t *testing.T) {
ctx := t.Context()
if test.testTimeout {
var cancel context.CancelFunc
ctx, cancel = context.WithTimeout(ctx, 99*time.Hour)
go (func() {
// Cancel context to force timeout
defer cancel()
time.Sleep(1 * time.Millisecond)
})()
}
options := append([]Option{}, test.options...)
if test.useContainer {
containerOptions := nmapContainerOptions(t)
options = append(containerOptions, options...)
}
s, err := NewScanner(options...)
require.NoError(t, err)
result, err := s.Run(ctx)
test.wantErr(t, err)
compareResults(t, test.expectedResult, result)
})
}
}