forked from weaveworks/procspy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
netstat_test.go
58 lines (54 loc) · 1.38 KB
/
netstat_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
package procspy
import (
"net"
"reflect"
"testing"
)
func TestNetstatDarwin(t *testing.T) {
testString := `Active Internet connections
Proto Recv-Q Send-Q Local Address Foreign Address (state)
tcp4 0 0 10.0.1.6.58287 1.2.3.4.443 ESTABLISHED
tcp4 0 0 10.0.1.6.58279 2.3.4.5.80 ESTABLISHED
tcp4 0 0 10.0.1.6.58276 44.55.66.77.443 ESTABLISHED
tcp4 0 0 10.0.1.6.1 4.0.4.0.443 GONE
`
res := parseDarwinNetstat(testString)
expected := []Connection{
{
Transport: "tcp",
LocalAddress: net.ParseIP("10.0.1.6"),
LocalPort: 58287,
RemoteAddress: net.ParseIP("1.2.3.4"),
RemotePort: 443,
},
{
Transport: "tcp",
LocalAddress: net.ParseIP("10.0.1.6"),
LocalPort: 58279,
RemoteAddress: net.ParseIP("2.3.4.5"),
RemotePort: 80,
},
{
Transport: "tcp",
LocalAddress: net.ParseIP("10.0.1.6"),
LocalPort: 58276,
RemoteAddress: net.ParseIP("44.55.66.77"),
RemotePort: 443,
},
/*
{
Transport: "tcp",
LocalAddress: "::1",
LocalPort: "6600",
RemoteAddress: "::1",
RemotePort: "41993",
},
*/
}
if len(res) != 3 {
t.Errorf("Wanted 3")
}
if !reflect.DeepEqual(res, expected) {
t.Errorf("OS x netstat 4 error. Got\n%+v\nExpected\n%+v\n", res, expected)
}
}