1
2
3
4
5
6
7
8
9
10
11
12
13
14 package procfs
15
16 import (
17 "net"
18 "reflect"
19 "testing"
20 )
21
22 func Test_newNetUDP(t *testing.T) {
23 tests := []struct {
24 name string
25 file string
26 want NetUDP
27 wantErr bool
28 }{
29 {
30 name: "udp file found, no error should come up",
31 file: "testdata/fixtures/proc/net/udp",
32 want: []*netIPSocketLine{
33 &netIPSocketLine{
34 Sl: 0,
35 LocalAddr: net.IP{10, 0, 0, 5},
36 LocalPort: 22,
37 RemAddr: net.IP{0, 0, 0, 0},
38 RemPort: 0,
39 St: 10,
40 TxQueue: 0,
41 RxQueue: 1,
42 UID: 0,
43 Inode: 2740,
44 Drops: intToU64(100),
45 },
46 &netIPSocketLine{
47 Sl: 1,
48 LocalAddr: net.IP{0, 0, 0, 0},
49 LocalPort: 22,
50 RemAddr: net.IP{0, 0, 0, 0},
51 RemPort: 0,
52 St: 10,
53 TxQueue: 1,
54 RxQueue: 0,
55 UID: 0,
56 Inode: 2740,
57 Drops: intToU64(100),
58 },
59 &netIPSocketLine{
60 Sl: 2,
61 LocalAddr: net.IP{0, 0, 0, 0},
62 LocalPort: 22,
63 RemAddr: net.IP{0, 0, 0, 0},
64 RemPort: 0,
65 St: 10,
66 TxQueue: 1,
67 RxQueue: 1,
68 UID: 0,
69 Inode: 2740,
70 Drops: intToU64(100),
71 },
72 },
73 wantErr: false,
74 },
75 {
76 name: "udp6 file found, no error should come up",
77 file: "testdata/fixtures/proc/net/udp6",
78 want: []*netIPSocketLine{
79 &netIPSocketLine{
80 Sl: 1315,
81 LocalAddr: net.IP{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
82 LocalPort: 5355,
83 RemAddr: net.IP{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
84 RemPort: 0,
85 St: 7,
86 TxQueue: 0,
87 RxQueue: 0,
88 UID: 981,
89 Inode: 21040,
90 Drops: intToU64(0),
91 },
92 &netIPSocketLine{
93 Sl: 6073,
94 LocalAddr: net.IP{254, 128, 0, 0, 0, 0, 0, 0, 86, 225, 173, 255, 254, 124, 102, 9},
95 LocalPort: 51073,
96 RemAddr: net.IP{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
97 RemPort: 0,
98 St: 7,
99 TxQueue: 0,
100 RxQueue: 0,
101 UID: 1000,
102 Inode: 11337031,
103 Drops: intToU64(0),
104 },
105 },
106 wantErr: false,
107 },
108 {
109 name: "error case - file not found",
110 file: "somewhere over the rainbow",
111 want: nil,
112 wantErr: true,
113 },
114 {
115 name: "error case - parse error",
116 file: "testdata/fixtures/proc/net/udp_broken",
117 want: nil,
118 wantErr: true,
119 },
120 }
121 for _, tt := range tests {
122 t.Run(tt.name, func(t *testing.T) {
123 got, err := newNetUDP(tt.file)
124 if (err != nil) != tt.wantErr {
125 t.Errorf("newNetUDP() error = %v, wantErr %v", err, tt.wantErr)
126 return
127 }
128 if !reflect.DeepEqual(got, tt.want) {
129 t.Errorf("newNetUDP() = %v, want %v", got, tt.want)
130 }
131 })
132 }
133 }
134
135 func Test_newNetUDPSummary(t *testing.T) {
136 tests := []struct {
137 name string
138 file string
139 want *NetUDPSummary
140 wantErr bool
141 }{
142 {
143 name: "udp file found, no error should come up",
144 file: "testdata/fixtures/proc/net/udp",
145 want: &NetUDPSummary{TxQueueLength: 2, RxQueueLength: 2, UsedSockets: 3, Drops: intToU64(300)},
146 wantErr: false,
147 },
148 {
149 name: "udp6 file found, no error should come up",
150 file: "testdata/fixtures/proc/net/udp6",
151 want: &NetUDPSummary{TxQueueLength: 0, RxQueueLength: 0, UsedSockets: 2, Drops: intToU64(0)},
152 wantErr: false,
153 },
154 {
155 name: "error case - file not found",
156 file: "somewhere over the rainbow",
157 want: nil,
158 wantErr: true,
159 },
160 {
161 name: "error case - parse error",
162 file: "testdata/fixtures/proc/net/udp_broken",
163 want: nil,
164 wantErr: true,
165 },
166 }
167 for _, tt := range tests {
168 t.Run(tt.name, func(t *testing.T) {
169 got, err := newNetUDPSummary(tt.file)
170 if (err != nil) != tt.wantErr {
171 t.Errorf("newNetUDPSummary() error = %v, wantErr %v", err, tt.wantErr)
172 return
173 }
174 if !reflect.DeepEqual(got, tt.want) {
175 t.Errorf("newNetUDPSummary() = %v, want %v", got, tt.want)
176 }
177 })
178 }
179 }
180
181
182 func intToU64(i int) *uint64 {
183 cast := uint64(i)
184 return &cast
185 }
186
View as plain text