-
Notifications
You must be signed in to change notification settings - Fork 1
/
cmdargs_test.go
202 lines (174 loc) · 5.19 KB
/
cmdargs_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
/*
go-scan-http -- Fast http network scanner
Copyright (C) 2019 Lars Kiesow <lkiesow@uos.de>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package main
import "testing"
func testEq(a, b []int) bool {
// If one is nil, the other must also be nil.
if (a == nil) != (b == nil) {
return false
}
if len(a) != len(b) {
return false
}
for i := range a {
if a[i] != b[i] {
return false
}
}
return true
}
func TestParsePorts(t *testing.T) {
cases := []struct {
input []string
expected []int
}{
{[]string{}, []int{80}},
{[]string{"123", "234"}, []int{123, 234}},
}
for _, testcase := range cases {
result, err := parsePorts(testcase.input)
if err != nil || !testEq(result, testcase.expected) {
t.Errorf("Expected %v but got %v", testcase.expected, result)
}
}
result, err := parsePorts([]string{"invalid"})
if err == nil {
t.Errorf("Expected error but got result %v", result)
}
}
func TestParseRangeString(t *testing.T) {
cases := []struct {
input []string
expected [4][2]int
}{
{[]string{"127.0.0.0/24"},
[4][2]int{{127, 127}, {0, 0}, {0, 0}, {1, 254}}},
{[]string{"127.0.0.0/24", "8081"},
[4][2]int{{127, 127}, {0, 0}, {0, 0}, {1, 254}}},
{[]string{"127.0.0.0/16", "8081"},
[4][2]int{{127, 127}, {0, 0}, {0, 255}, {1, 254}}},
}
for _, testcase := range cases {
result, err := parseRangeString(&settings{}, testcase.input)
if err != nil || result.bytes != testcase.expected {
t.Errorf("Expected %v but got %v", testcase.expected, result.bytes)
}
}
errorcases := [][]string{
[]string{},
[]string{"127.0.0.0"},
[]string{"127.0.0.0/33"},
}
for _, testcase := range errorcases {
result, err := parseRangeString(&settings{}, testcase)
if err == nil {
t.Errorf("Expected error but got result %v for input %v",
result, testcase)
}
}
// test that settings are properly modified
inputSettings := settings{}
inputArgs := []string{"127.0.0.0/24"}
parseRangeString(&inputSettings, inputArgs)
if inputSettings.bytes[0][0] != 127 {
t.Errorf("Expected input settings to be modifed")
}
}
func TestParseRangeArgs(t *testing.T) {
// test parsing valid address ranges
cases := []struct {
input []string
expected [4][2]int
}{
{[]string{"127", "0", "0", "*"},
[4][2]int{{127, 127}, {0, 0}, {0, 0}, {1, 254}}},
{[]string{"127", "0", "0", "*", "8080"},
[4][2]int{{127, 127}, {0, 0}, {0, 0}, {1, 254}}},
{[]string{"127", "0", "*", "*", "8080"},
[4][2]int{{127, 127}, {0, 0}, {0, 255}, {1, 254}}},
{[]string{"127", "2-3", "*", "1-254", "8080"},
[4][2]int{{127, 127}, {2, 3}, {0, 255}, {1, 254}}},
}
for _, testcase := range cases {
result, err := parseRangeArgs(&settings{}, testcase.input)
if err != nil || result.bytes != testcase.expected {
t.Errorf("Expected %v but got %v", testcase.expected, result.bytes)
}
}
// test invalid address ranges
errorcases := [][]string{
[]string{},
[]string{"127", "0", "0"},
[]string{"127", "0", "0", "300"},
[]string{"127", "0", "0", "-3"},
[]string{"127", "0", "a", "1-3"},
[]string{"127", "0", "0-b", "-3"},
[]string{"127", "0", "0", "4-3"},
}
for _, testcase := range errorcases {
result, err := parseRangeArgs(&settings{}, testcase)
if err == nil {
t.Errorf("Expected error but got result %v for input %v",
result, testcase)
}
}
// test that settings are properly modified
inputSettings := settings{}
inputArgs := []string{"127", "0", "0", "1"}
parseRangeArgs(&inputSettings, inputArgs)
if inputSettings.bytes[0][0] != 127 {
t.Errorf("Expected input settings to be modifed")
}
}
func TestParseOptions(t *testing.T) {
// test success
cases := []struct {
input []string
threads int
length int
}{
{[]string{"-n", "123", "127.0.0.1/24"}, 123, 1},
{[]string{"127.0.0.1/24"}, 0, 1},
}
for _, testcase := range cases {
settings, args, err := parseOptions(&settings{}, testcase.input)
if err != nil || settings.threads != testcase.threads || len(args) != testcase.length {
t.Errorf("Got %v and %v", settings, args)
}
}
// test invalid input
errorcases := []struct {
input []string
threads int
length int
}{
{[]string{"-n", "127.0.0.1/24"}, 123, 1},
{[]string{"-x", "127.0.0.1/24"}, 0, 1},
}
for _, testcase := range errorcases {
settings, args, err := parseOptions(&settings{}, testcase.input)
if err == nil {
t.Errorf("Expected error but got result %v, args: %v for input %v",
settings, args, testcase)
}
}
// test settings are properly modified
inputSettings := settings{}
inputArgs := []string{"-n", "123", "127.0.0.1/24"}
parseOptions(&inputSettings, inputArgs)
if inputSettings.threads != 123 {
t.Errorf("Expected input settings to be modifed")
}
}