-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain_test.go
149 lines (141 loc) · 3 KB
/
main_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
package main
import (
"net"
"reflect"
"sync"
"testing"
"github.com/cheggaaa/pb"
)
func TestAppendStatus(t *testing.T) {
bc := len(resAddrs)
wg := &sync.WaitGroup{}
for i := 0; i < 10; i++ {
wg.Add(1)
go func(wg *sync.WaitGroup) {
AppendStatus(TCPAddrStatus{})
wg.Done()
}(wg)
}
wg.Wait()
if ac := len(resAddrs) - bc; ac != 10 {
t.Errorf("Append 10 TCPAddrStatus, but got %v", ac)
}
}
func TestIPStringToInt(t *testing.T) {
type args struct {
ipString string
}
tests := []struct {
name string
args args
want int
}{
{"10.1.1.1", args{"10.1.1.1"}, 167837953},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := IPStringToInt(tt.args.ipString); got != tt.want {
t.Errorf("IPStringToInt(%v) = %v, want %v", tt.args.ipString, got, tt.want)
}
})
}
}
func TestIPIntToString(t *testing.T) {
type args struct {
ipInt int
}
tests := []struct {
name string
args args
want string
}{
{"10.1.1.1", args{167837953}, "10.1.1.1"},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := IPIntToString(tt.args.ipInt); got != tt.want {
t.Errorf("IPIntToString() = %v, want %v", got, tt.want)
}
})
}
}
func TestAppendPort(t *testing.T) {
type args struct {
ports []int
port int
}
tests := []struct {
name string
args args
want []int
}{
{"0", args{[]int{}, 0}, []int{}},
{"65536", args{[]int{}, 65536}, []int{}},
{"80", args{[]int{}, 80}, []int{80}},
{"65535", args{[]int{}, 65535}, []int{65535}},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := AppendPort(tt.args.ports, tt.args.port); !reflect.DeepEqual(got, tt.want) {
t.Errorf("AppendPort() = %v, want %v", got, tt.want)
}
})
}
}
func TestCheckPort(t *testing.T) {
type args struct {
ip net.IP
port int
wg *sync.WaitGroup
parallelChan chan int
bar *pb.ProgressBar
}
wg := &sync.WaitGroup{}
wg.Add(1)
pc := make(chan int, 1)
pc <- 1
pb := pb.New(1)
tests := []struct {
name string
args args
}{
{"first", args{net.ParseIP("127.0.0.1"), 1, wg, pc, pb}},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
cnt := len(resAddrs)
CheckPort(tt.args.ip, tt.args.port, tt.args.wg, tt.args.parallelChan, tt.args.bar)
if got := len(resAddrs) - cnt; got != 0 {
t.Errorf("checkPort() = %v, want %v", got, 0)
}
})
}
}
func TestCheckPing(t *testing.T) {
type args struct {
ip net.IP
wg *sync.WaitGroup
parallelChan chan int
bar *pb.ProgressBar
}
wg := &sync.WaitGroup{}
wg.Add(1)
pc := make(chan int, 1)
pc <- 1
pb := pb.New(1)
tests := []struct {
name string
args args
}{
{"first", args{net.ParseIP("127.0.0.1"), wg, pc, pb}},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
cnt := len(resAddrs)
CheckPing(tt.args.ip, tt.args.wg, tt.args.parallelChan, tt.args.bar)
if got := len(resAddrs) - cnt; got != 1 {
t.Errorf("checkPing() = %v, want %v", got, 1)
}
})
}
}