forked from zmap/zgrab2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
input_test.go
215 lines (204 loc) · 4.86 KB
/
input_test.go
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
package zgrab2
import (
"net"
"strings"
"testing"
)
func TestParseCSVTarget(t *testing.T) {
parseCIDR := func(s string) *net.IPNet {
_, ipnet, err := net.ParseCIDR(s)
if err != nil {
panic(err)
}
return ipnet
}
parseIP := func(s string) *net.IPNet {
ip := net.ParseIP(s)
if ip == nil {
panic("can't parse IP")
}
return &net.IPNet{IP: ip}
}
ipnetString := func(ipnet *net.IPNet) string {
if ipnet == nil {
return "<nil>"
} else if ipnet.IP != nil && ipnet.Mask != nil {
return ipnet.String()
} else if ipnet.IP != nil {
return ipnet.IP.String()
}
panic("malformed ipnet")
}
tests := []struct {
fields []string
ipnet *net.IPNet
domain string
tag string
port string
success bool
}{
// IP DOMAIN TAG PORT
{
fields: []string{"10.0.0.1", "example.com", "tag", "443"},
ipnet: parseIP("10.0.0.1"),
domain: "example.com",
tag: "tag",
port: "443",
success: true,
},
// IP DOMAIN TAG PORT
{
fields: []string{"10.0.0.1", "example.com", "tag"},
ipnet: parseIP("10.0.0.1"),
domain: "example.com",
tag: "tag",
port: "",
success: true,
},
// IP DOMAIN TAG
{
fields: []string{"10.0.0.1", "example.com", "tag"},
ipnet: parseIP("10.0.0.1"),
domain: "example.com",
tag: "tag",
success: true,
},
// IP DOMAIN (3 fields)
{
fields: []string{"10.0.0.1", "example.com", ""},
ipnet: parseIP("10.0.0.1"),
domain: "example.com",
success: true,
},
// IP DOMAIN (2 fields)
{
fields: []string{"10.0.0.1", "example.com"},
ipnet: parseIP("10.0.0.1"),
domain: "example.com",
success: true,
},
// IP (3 fields)
{
fields: []string{"10.0.0.1", "", ""},
ipnet: parseIP("10.0.0.1"),
success: true,
},
// IP (2 fields)
{
fields: []string{"10.0.0.1", ""},
ipnet: parseIP("10.0.0.1"),
success: true,
},
// IP (1 fields)
{
fields: []string{"10.0.0.1", ""},
ipnet: parseIP("10.0.0.1"),
success: true,
},
// CIDR
{
fields: []string{"10.0.0.1/8", ""},
ipnet: parseCIDR("10.0.0.1/8"),
success: true,
},
// DOMAIN (2 fields)
{
fields: []string{"", "example.com"},
domain: "example.com",
success: true,
},
// Bare domain
{
fields: []string{"example.com"},
domain: "example.com",
success: true,
},
// Error: Empty record (1 field)
{
fields: []string{""},
success: false,
},
// Error: Empty record (no fields)
{
fields: []string{},
success: false,
},
// Error: No address or domain
{
fields: []string{"", "", "tag"},
success: false,
},
// Error: Too many fields
{
fields: []string{"", "", "", ""},
success: false,
},
// Error: IP and domain reversed
{
fields: []string{"example.com", "10.0.0.1"},
success: false,
},
}
for _, test := range tests {
ipnet, domain, tag, port, err := ParseCSVTarget(test.fields)
if (err == nil) != test.success {
t.Errorf("wrong error status (got err=%v, success should be %v): %q", err, test.success, test.fields)
return
}
if err == nil {
if ipnetString(ipnet) != ipnetString(test.ipnet) || domain != test.domain || tag != test.tag || port != test.port {
t.Errorf("wrong result (got %v,%v,%v,%v ; expected %v,%v,%v,%v): %q", ipnetString(ipnet), domain, tag, port, ipnetString(test.ipnet), test.domain, test.tag, test.port, test.fields)
return
}
}
}
}
func TestGetTargetsCSV(t *testing.T) {
input := `# Comment
10.0.0.1,example.com,tag
10.0.0.1 ,"example.com"
10.0.0.1
,example.com
example.com
2.2.2.2/30,, tag
10.0.0.1,example.com,tag,443
10.0.0.1,,,443
`
port := uint(443)
expected := []ScanTarget{
ScanTarget{IP: net.ParseIP("10.0.0.1"), Domain: "example.com", Tag: "tag"},
ScanTarget{IP: net.ParseIP("10.0.0.1"), Domain: "example.com"},
ScanTarget{IP: net.ParseIP("10.0.0.1")},
ScanTarget{Domain: "example.com"},
ScanTarget{Domain: "example.com"},
ScanTarget{IP: net.ParseIP("2.2.2.0"), Tag: "tag"},
ScanTarget{IP: net.ParseIP("2.2.2.1"), Tag: "tag"},
ScanTarget{IP: net.ParseIP("2.2.2.2"), Tag: "tag"},
ScanTarget{IP: net.ParseIP("2.2.2.3"), Tag: "tag"},
ScanTarget{IP: net.ParseIP("10.0.0.1"), Domain: "example.com", Tag: "tag", Port: &port},
ScanTarget{IP: net.ParseIP("10.0.0.1"), Port: &port},
}
ch := make(chan ScanTarget, 0)
go func() {
err := GetTargetsCSV(strings.NewReader(input), ch)
if err != nil {
t.Errorf("GetTargets error: %v", err)
}
close(ch)
}()
res := []ScanTarget{}
for r := range ch {
res = append(res, r)
}
if len(res) != len(expected) {
t.Errorf("wrong number of results (got %d; expected %d)", len(res), len(expected))
return
}
for i := range expected {
if res[i].IP.String() != expected[i].IP.String() ||
res[i].Domain != expected[i].Domain ||
res[i].Tag != expected[i].Tag {
t.Errorf("wrong data in ScanTarget %d (got %v; expected %v)", i, res[i], expected[i])
}
}
}