This repository has been archived by the owner on Jan 23, 2023. It is now read-only.
forked from alicebob/procspy
-
Notifications
You must be signed in to change notification settings - Fork 14
/
procnet_test.go
162 lines (150 loc) · 5.03 KB
/
procnet_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
package procspy
import (
"net"
"reflect"
"testing"
)
func TestProcNet(t *testing.T) {
testString := ` sl local_address rem_address st tx_queue rx_queue tr tm->when retrnsmt uid timeout Inode
0: 00000000:A6C0 00000000:0000 01 00000000:00000000 00:00000000 00000000 105 0 5107 1 ffff8800a6aaf040 100 0 0 10 0
1: 00000000:006F 00000000:0000 01 00000000:00000000 00:00000000 00000000 0 0 5084 1 ffff8800a6aaf740 100 0 0 10 0
2: 0100007F:0019 00000000:0000 01 00000000:00000000 00:00000000 00000000 0 0 10550 1 ffff8800a729b780 100 0 0 10 0
3: A12CF62E:E4D7 57FC1EC0:01BB 01 00000000:00000000 02:000006FA 00000000 1000 0 639474 2 ffff88007e75a740 48 4 26 10 -1
`
p := NewProcNet([]byte(testString), tcpEstablished)
expected := []Connection{
{
LocalAddress: net.IP([]byte{0, 0, 0, 0}),
LocalPort: 0xa6c0,
RemoteAddress: net.IP([]byte{0, 0, 0, 0}),
RemotePort: 0x0,
inode: 5107,
},
{
LocalAddress: net.IP([]byte{0, 0, 0, 0}),
LocalPort: 0x006f,
RemoteAddress: net.IP([]byte{0, 0, 0, 0}),
RemotePort: 0x0,
inode: 5084,
},
{
LocalAddress: net.IP([]byte{0x7f, 0x0, 0x0, 0x01}),
LocalPort: 0x0019,
RemoteAddress: net.IP([]byte{0, 0, 0, 0}),
RemotePort: 0x0,
inode: 10550,
},
{
LocalAddress: net.IP([]byte{0x2e, 0xf6, 0x2c, 0xa1}),
LocalPort: 0xe4d7,
RemoteAddress: net.IP([]byte{0xc0, 0x1e, 0xfc, 0x57}),
RemotePort: 0x01bb,
inode: 639474,
},
}
for i := 0; i < 4; i++ {
have := p.Next()
want := expected[i]
if !reflect.DeepEqual(*have, want) {
t.Errorf("transport 4 error. Got\n%+v\nExpected\n%+v\n", *have, want)
}
}
if got := p.Next(); got != nil {
t.Errorf("p.Next() wasn't empty")
}
}
func TestTransport6(t *testing.T) {
// Abridged copy of my /proc/net/tcp6
testString := ` sl local_address remote_address st tx_queue rx_queue tr tm->when retrnsmt uid timeout Inode
0: 00000000000000000000000000000000:19C8 00000000000000000000000000000000:0000 01 00000000:00000000 00:00000000 00000000 0 0 23661201 1 ffff880103fb4800 100 0 0 10 -1
8: 4500032000BE692B8AE31EBD919D9D10:D61C 5014002A080805400000000015100000:01BB 01 00000000:00000000 02:00000045 00000000 1000 0 36856710 2 ffff88010b796080 22 4 30 8 7
`
p := NewProcNet([]byte(testString), tcpEstablished)
expected := []Connection{
{
// state: 10,
LocalAddress: net.IP(make([]byte, 16)),
LocalPort: 0x19c8,
RemoteAddress: net.IP(make([]byte, 16)),
RemotePort: 0x0,
// uid: 0,
inode: 23661201,
},
{
// state: 1,
LocalAddress: net.IP([]byte{
0x20, 0x03, 0, 0x45,
0x2b, 0x69, 0xbe, 0x00,
0xbd, 0x1e, 0xe3, 0x8a,
0x10, 0x9d, 0x9d, 0x91,
}),
LocalPort: 0xd61c,
RemoteAddress: net.IP([]byte{
0x2a, 0x00, 0x14, 0x50,
0x40, 0x05, 0x08, 0x08,
0, 0, 0, 0,
0, 0, 0x10, 0x15,
}),
RemotePort: 0x01bb,
// uid: 1000,
inode: 36856710,
},
}
for i := 0; i < 2; i++ {
have := p.Next()
want := expected[i]
if !reflect.DeepEqual(*have, want) {
t.Errorf("got\n%+v\nExpected\n%+v\n", *have, want)
}
}
if got := p.Next(); got != nil {
t.Errorf("p.Next() wasn't empty")
}
}
func TestTransportNonsense(t *testing.T) {
testString := ` sl local_address rem_address st tx_queue rx_queue tr tm->when retrnsmt uid timeout Inode
0: 00000000:A6C0 00000000:0000 01 000000
broken line
`
p := NewProcNet([]byte(testString), tcpEstablished)
expected := []Connection{
{
LocalAddress: net.IP([]byte{0, 0, 0, 0}),
LocalPort: 0xa6c0,
RemoteAddress: net.IP([]byte{0, 0, 0, 0}),
RemotePort: 0x0,
},
}
for i := 0; i < 1; i++ {
have := p.Next()
want := expected[i]
if !reflect.DeepEqual(*have, want) {
t.Errorf("Got\n%+v\nExpected\n%+v\n", *have, want)
}
}
if got := p.Next(); got != nil {
t.Errorf("p.Next() wasn't empty")
}
}
func TestProcNetFiltersDuplicates(t *testing.T) {
testString := ` sl local_address rem_address st tx_queue rx_queue tr tm->when retrnsmt uid timeout Inode
0: 00000000:A6C0 00000000:0000 01 00000000:00000000 00:00000000 00000000 105 0 5107 1 ffff8800a6aaf040 100 0 0 10 0
1: 00000000:A6C0 00000000:0000 01 00000000:00000000 00:00000000 00000000 105 0 5107 1 ffff8800a6aaf040 100 0 0 10 0
`
p := NewProcNet([]byte(testString), tcpEstablished)
expected := Connection{
LocalAddress: net.IP([]byte{0, 0, 0, 0}),
LocalPort: 0xa6c0,
RemoteAddress: net.IP([]byte{0, 0, 0, 0}),
RemotePort: 0x0,
inode: 5107,
}
have := p.Next()
want := expected
if !reflect.DeepEqual(*have, want) {
t.Errorf("transport 4 error. Got\n%+v\nExpected\n%+v\n", *have, want)
}
if got := p.Next(); got != nil {
t.Errorf("p.Next() wasn't empty")
}
}