forked from fadhilyori/subping
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsubping_test.go
228 lines (207 loc) · 5.22 KB
/
subping_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
216
217
218
219
220
221
222
223
224
225
226
227
228
package subping_test
import (
"sort"
"testing"
"time"
"github.com/fadhilyori/subping"
"github.com/fadhilyori/subping/pkg/network"
)
func TestRunSubping(t *testing.T) {
type args struct {
CIDR string
Count int
Timeout time.Duration
Interval time.Duration
MaxWorkers int
}
tests := []struct {
name string
args args
wantErr bool
wantOnline bool
numOfOnline int
}{
{
name: "Test with valid options",
args: args{
CIDR: "127.0.0.1/31",
Count: 1,
Timeout: 300 * time.Millisecond,
Interval: 300 * time.Millisecond,
MaxWorkers: 1,
},
wantErr: false,
wantOnline: false,
numOfOnline: 0,
},
{
name: "Test with invalid Count",
args: args{
CIDR: "127.0.0.0/29",
Count: -1,
Timeout: 1 * time.Second,
Interval: 300 * time.Millisecond,
MaxWorkers: 2,
},
wantErr: true,
wantOnline: false,
numOfOnline: 0,
},
{
name: "Test with invalid MaxWorkers",
args: args{
CIDR: "127.0.0.0/29",
Count: 1,
Timeout: 1 * time.Second,
Interval: 300 * time.Millisecond,
MaxWorkers: -2,
},
wantErr: true,
wantOnline: false,
numOfOnline: 0,
},
{
name: "Test with IPv6 ::1/128",
args: args{
CIDR: "::1/128",
Count: 4,
Timeout: 1 * time.Second,
Interval: 300 * time.Millisecond,
MaxWorkers: 1,
},
wantErr: false,
wantOnline: true,
numOfOnline: 1,
},
{
name: "Test with IPv4 /20 should online all - high conccurency",
args: args{
CIDR: "127.0.0.0/24",
Count: 1,
Timeout: 1 * time.Second,
Interval: 300 * time.Millisecond,
MaxWorkers: 256,
},
wantErr: false,
wantOnline: true,
numOfOnline: 255,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
sp, err := subping.NewSubping(&subping.Options{
Subnet: tt.args.CIDR,
Count: tt.args.Count,
Interval: tt.args.Interval,
Timeout: tt.args.Timeout,
MaxWorkers: tt.args.MaxWorkers,
LogLevel: "error",
})
if err != nil {
if !tt.wantErr {
t.Errorf("NewSubping() error = %v, wantErr %v", err, tt.wantErr)
}
return
}
if sp.TargetsIterator == nil && !tt.wantErr {
t.Errorf("NewSubping() TargetsIterator = nil, want *network.SubnetHostsIterator, wantErr %v", tt.wantErr)
return
}
if sp.Count != tt.args.Count {
t.Errorf("NewSubping() Count got = %v, want %v, wantErr %v", sp.Count, tt.args.Count, tt.wantErr)
return
}
if sp.Timeout != tt.args.Timeout {
t.Errorf("NewSubping() Timeout got = %v, want %v, wantErr %v", sp.Timeout, tt.args.Timeout, tt.wantErr)
return
}
if sp.Interval != tt.args.Interval {
t.Errorf("NewSubping() Interval got = %v, want %v, wantErr %v", sp.Interval, tt.args.Interval, tt.wantErr)
return
}
if sp.MaxWorkers != tt.args.MaxWorkers {
t.Errorf("NewSubping() MaxWorkers got = %v, want %v, wantErr %v", sp.MaxWorkers, tt.args.MaxWorkers, tt.wantErr)
return
}
sp.Run()
_, onlineResultsLen := sp.GetOnlineHosts()
wantTotalResults, err := network.CalculateTotalHostsFromCIDRString(tt.args.CIDR)
if err != nil {
t.Errorf("CalculateTotalHostsFromCIDRString() error => %v, wantErr %v", err, tt.wantErr)
return
}
if wantTotalResults != sp.TotalResults {
var hosts []string
for k := range sp.Results {
hosts = append(hosts, k)
}
sort.Strings(hosts)
t.Errorf("Subping.Results length is invalid => got %v (%v), want %v, wantErr %v\nError: %v", sp.TotalResults, len(sp.Results), wantTotalResults, tt.wantErr, hosts)
return
}
if tt.wantOnline && onlineResultsLen != tt.numOfOnline && onlineResultsLen != wantTotalResults {
var hosts []string
for k := range sp.Results {
hosts = append(hosts, k)
}
t.Errorf("Subping.Results length online hosts is invalid => got %v, want %v, wantErr %v\n", onlineResultsLen, tt.numOfOnline, tt.wantErr)
return
}
})
}
}
func TestRunPing(t *testing.T) {
type args struct {
ipAddress string
count int
interval time.Duration
timeout time.Duration
}
type want struct {
PacketsSent int
PacketsRecv int
}
tests := []struct {
name string
args args
want want
}{
{
name: "Test with valid local IP Address with Count 5",
args: args{
ipAddress: "localhost",
count: 5,
interval: 300 * time.Millisecond,
timeout: 3 * time.Second,
},
want: want{
PacketsSent: 5,
PacketsRecv: 5,
},
},
{
name: "Test with invalid local IP Address with Count 5",
args: args{
ipAddress: "1",
count: 5,
interval: 300 * time.Millisecond,
timeout: 3 * time.Second,
},
want: want{
PacketsSent: 0,
PacketsRecv: 0,
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := subping.RunPing(tt.args.ipAddress, tt.args.count, tt.args.interval, tt.args.timeout)
if got.PacketsRecv != tt.want.PacketsRecv {
t.Errorf("RunPing() PacketsRecv = %v, want %v", got.PacketsRecv, tt.want.PacketsRecv)
}
if got.PacketsSent != tt.want.PacketsSent {
t.Errorf("RunPing() PacketsSent = %v, want %v", got.PacketsSent, tt.want.PacketsSent)
}
})
}
}