-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathscanner_test.go
50 lines (44 loc) · 917 Bytes
/
scanner_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
package scanner
import (
"net"
"testing"
"github.com/stretchr/testify/require"
)
func TestScanner_Start(t *testing.T) {
for addr := range testnewScanner(t).Result {
t.Log(addr)
}
}
func TestScanner_Stop(t *testing.T) {
testnewScanner(t).Stop()
}
func testnewScanner(t *testing.T) *Scanner {
port := testListener(t)
iface, err := SelectInterface("")
require.NoError(t, err)
host := iface.IPNets[0].IP.String()
s, err := New(host, port, &Options{Method: MethodConnect})
require.NoError(t, err)
err = s.Start()
require.NoError(t, err)
return s
}
func testListener(t *testing.T) string {
listener, err := net.Listen("tcp", ":0")
if err != nil {
t.Fatal(err)
}
go func() {
for {
conn, err := listener.Accept()
if err != nil {
return
}
go func(conn net.Conn) {
_ = conn.Close()
}(conn)
}
}()
_, port, _ := net.SplitHostPort(listener.Addr().String())
return port
}