forked from gosnmp/gosnmp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
trap_test.go
366 lines (312 loc) · 9.15 KB
/
trap_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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
// Copyright 2012-2016 The GoSNMP Authors. All rights reserved. Use of this
// source code is governed by a BSD-style license that can be found in the
// LICENSE file.
package gosnmp
import (
"log"
"net"
"os" //"io/ioutil"
"testing"
"time"
)
const (
trapTestAddress = "127.0.0.1"
// this is bad. Listen and Connect expect different address formats
// so we need an int version and a string version - they should be the same.
trapTestPort = 9162
trapTestPortString = "9162"
trapTestOid = ".1.2.1234.4.5"
trapTestPayload = "TRAPTEST1234"
trapTestEnterpriseOid = ".1.2.1234"
trapTestAgentAddress = "127.0.0.1"
trapTestGenericTrap = 6
trapTestSpecificTrap = 55
trapTestTimestamp = 300
)
var testsUnmarshalTrap = []struct {
in func() []byte
out *SnmpPacket
}{
{genericV3Trap,
&SnmpPacket{
Version: Version3,
PDUType: SNMPv2Trap,
RequestID: 190378322,
MsgFlags: AuthNoPriv,
SecurityParameters: &UsmSecurityParameters{
UserName: "myuser",
AuthenticationProtocol: MD5,
AuthenticationPassphrase: "mypassword",
Logger: log.New(os.Stdout, "", 0),
},
},
},
}
/*func TestUnmarshalTrap(t *testing.T) {
Default.Logger = log.New(os.Stdout, "", 0)
SANITY:
for i, test := range testsUnmarshalTrap {
Default.SecurityParameters = test.out.SecurityParameters.Copy()
var buf = test.in()
var res = Default.unmarshalTrap(buf)
if res == nil {
t.Errorf("#%d, UnmarshalTrap returned nil", i)
continue SANITY
}
// test enough fields fields to ensure unmarshalling was successful.
// full unmarshal testing is performed in TestUnmarshal
if res.Version != test.out.Version {
t.Errorf("#%d Version result: %v, test: %v", i, res.Version, test.out.Version)
}
if res.RequestID != test.out.RequestID {
t.Errorf("#%d RequestID result: %v, test: %v", i, res.RequestID, test.out.RequestID)
}
}
}
*/
func genericV3Trap() []byte {
return []byte{
0x30, 0x81, 0xd7, 0x02, 0x01, 0x03, 0x30, 0x11, 0x02, 0x04, 0x62, 0xaf,
0x5a, 0x8e, 0x02, 0x03, 0x00, 0xff, 0xe3, 0x04, 0x01, 0x01, 0x02, 0x01,
0x03, 0x04, 0x33, 0x30, 0x31, 0x04, 0x11, 0x80, 0x00, 0x1f, 0x88, 0x80,
0x77, 0xdf, 0xe4, 0x4f, 0xaa, 0x70, 0x02, 0x58, 0x00, 0x00, 0x00, 0x00,
0x02, 0x01, 0x0f, 0x02, 0x01, 0x00, 0x04, 0x06, 0x6d, 0x79, 0x75, 0x73,
0x65, 0x72, 0x04, 0x0c, 0xd8, 0xb6, 0x9c, 0xb8, 0x22, 0x91, 0xfc, 0x65,
0xb6, 0x84, 0xcb, 0xfe, 0x04, 0x00, 0x30, 0x81, 0x89, 0x04, 0x11, 0x80,
0x00, 0x1f, 0x88, 0x80, 0x77, 0xdf, 0xe4, 0x4f, 0xaa, 0x70, 0x02, 0x58,
0x00, 0x00, 0x00, 0x00, 0x04, 0x00, 0xa7, 0x72, 0x02, 0x04, 0x39, 0x19,
0x9c, 0x61, 0x02, 0x01, 0x00, 0x02, 0x01, 0x00, 0x30, 0x64, 0x30, 0x0f,
0x06, 0x08, 0x2b, 0x06, 0x01, 0x02, 0x01, 0x01, 0x03, 0x00, 0x43, 0x03,
0x15, 0x2f, 0xec, 0x30, 0x14, 0x06, 0x0a, 0x2b, 0x06, 0x01, 0x06, 0x03,
0x01, 0x01, 0x04, 0x01, 0x00, 0x06, 0x06, 0x2b, 0x06, 0x01, 0x02, 0x01,
0x01, 0x30, 0x16, 0x06, 0x08, 0x2b, 0x06, 0x01, 0x02, 0x01, 0x01, 0x01,
0x00, 0x04, 0x0a, 0x72, 0x65, 0x64, 0x20, 0x6c, 0x61, 0x70, 0x74, 0x6f,
0x70, 0x30, 0x0d, 0x06, 0x08, 0x2b, 0x06, 0x01, 0x02, 0x01, 0x01, 0x07,
0x00, 0x02, 0x01, 0x05, 0x30, 0x14, 0x06, 0x07, 0x2b, 0x06, 0x01, 0x02,
0x01, 0x01, 0x02, 0x06, 0x09, 0x2b, 0x06, 0x01, 0x04, 0x01, 0x02, 0x03,
0x04, 0x05}
}
func makeTestTrapHandler(t *testing.T, done chan int, version SnmpVersion) func(*SnmpPacket, *net.UDPAddr) {
return func(packet *SnmpPacket, addr *net.UDPAddr) {
// log.Printf("got trapdata from %s\n", addr.IP)
if version == Version1 {
if packet.Enterprise != trapTestEnterpriseOid {
t.Fatalf("incorrect trap Enterprise OID received, expected %s got %s", trapTestEnterpriseOid, packet.Enterprise)
done <- 0
}
if packet.AgentAddress != trapTestAgentAddress {
t.Fatalf("incorrect trap Agent Address received, expected %s got %s", trapTestAgentAddress, packet.AgentAddress)
done <- 0
}
if packet.GenericTrap != trapTestGenericTrap {
t.Fatalf("incorrect trap Generic Trap identifier received, expected %v got %v", trapTestGenericTrap, packet.GenericTrap)
done <- 0
}
if packet.SpecificTrap != trapTestSpecificTrap {
t.Fatalf("incorrect trap Specific Trap identifier received, expected %v got %v", trapTestSpecificTrap, packet.SpecificTrap)
done <- 0
}
if packet.Timestamp != trapTestTimestamp {
t.Fatalf("incorrect trap Timestamp received, expected %v got %v", trapTestTimestamp, packet.Timestamp)
done <- 0
}
}
for _, v := range packet.Variables {
switch v.Type {
case OctetString:
b := v.Value.([]byte)
// log.Printf("OID: %s, string: %x\n", v.Name, b)
// Only one OctetString in the payload, so it must be the expected one
if v.Name != trapTestOid {
t.Fatalf("incorrect trap OID received, expected %s got %s", trapTestOid, v.Name)
done <- 0
}
if string(b) != trapTestPayload {
t.Fatalf("incorrect trap payload received, expected %s got %x", trapTestPayload, b)
done <- 0
}
default:
// log.Printf("trap: %+v\n", v)
}
}
done <- 0
}
}
// test sending a basic SNMP trap, using our own listener to receive
func TestSendTrap(t *testing.T) {
done := make(chan int)
tl := NewTrapListener()
defer tl.Close()
tl.OnNewTrap = makeTestTrapHandler(t, done, Version2c)
tl.Params = Default
// listener goroutine
errch := make(chan error)
go func() {
err := tl.Listen(net.JoinHostPort(trapTestAddress, trapTestPortString))
if err != nil {
errch <- err
}
}()
// Wait until the listener is ready.
select {
case <-tl.Listening():
case err := <-errch:
t.Fatalf("error in listen: %v", err)
}
ts := &GoSNMP{
Target: trapTestAddress,
Port: trapTestPort,
Community: "public",
Version: Version2c,
Timeout: time.Duration(2) * time.Second,
Retries: 3,
MaxOids: MaxOids,
}
err := ts.Connect()
if err != nil {
t.Fatalf("Connect() err: %v", err)
}
defer ts.Conn.Close()
pdu := SnmpPDU{
Name: trapTestOid,
Type: OctetString,
Value: trapTestPayload,
}
trap := SnmpTrap{
Variables: []SnmpPDU{pdu},
}
_, err = ts.SendTrap(trap)
if err != nil {
t.Fatalf("SendTrap() err: %v", err)
}
// wait for response from handler
select {
case <-done:
case <-time.After(5 * time.Second):
t.Fatal("timed out waiting for trap to be received")
}
}
// test the listener is not blocked if Listening is not used
func TestSendTrapWithoutWaitingOnListen(t *testing.T) {
done := make(chan int)
tl := NewTrapListener()
defer tl.Close()
tl.OnNewTrap = makeTestTrapHandler(t, done, Version2c)
tl.Params = Default
errch := make(chan error)
listening := make(chan bool)
go func() {
// Reduce the chance of necessity for a restart.
listening <- true
err := tl.Listen(net.JoinHostPort(trapTestAddress, trapTestPortString))
if err != nil {
errch <- err
}
}()
select {
case <-listening:
case err := <-errch:
t.Fatalf("error in listen: %v", err)
}
ts := &GoSNMP{
Target: trapTestAddress,
Port: trapTestPort,
Community: "public",
Version: Version2c,
Timeout: time.Duration(2) * time.Second,
Retries: 3,
MaxOids: MaxOids,
}
err := ts.Connect()
if err != nil {
t.Fatalf("Connect() err: %v", err)
}
defer ts.Conn.Close()
pdu := SnmpPDU{
Name: trapTestOid,
Type: OctetString,
Value: trapTestPayload,
}
trap := SnmpTrap{
Variables: []SnmpPDU{pdu},
}
_, err = ts.SendTrap(trap)
if err != nil {
t.Fatalf("SendTrap() err: %v", err)
}
// Wait for a response from the handler and restart the SendTrap
// if the listener wasn't ready.
select {
case <-done:
case <-time.After(2 * time.Second):
_, err = ts.SendTrap(trap)
if err != nil {
t.Fatalf("restarted SendTrap() err: %v", err)
}
t.Log("restarted")
select {
case <-done:
case <-time.After(2 * time.Second):
t.Fatal("timed out waiting for trap to be received")
}
}
}
// test sending a basic SNMP trap, using our own listener to receive
func TestSendV1Trap(t *testing.T) {
done := make(chan int)
tl := NewTrapListener()
defer tl.Close()
tl.OnNewTrap = makeTestTrapHandler(t, done, Version1)
tl.Params = Default
// listener goroutine
errch := make(chan error)
go func() {
err := tl.Listen(net.JoinHostPort(trapTestAddress, trapTestPortString))
if err != nil {
errch <- err
}
}()
// Wait until the listener is ready.
select {
case <-tl.Listening():
case err := <-errch:
t.Fatalf("error in listen: %v", err)
}
ts := &GoSNMP{
Target: trapTestAddress,
Port: trapTestPort,
//Community: "public",
Version: Version1,
Timeout: time.Duration(2) * time.Second,
Retries: 3,
MaxOids: MaxOids,
}
err := ts.Connect()
if err != nil {
t.Fatalf("Connect() err: %v", err)
}
defer ts.Conn.Close()
pdu := SnmpPDU{
Name: trapTestOid,
Type: OctetString,
Value: trapTestPayload,
}
trap := SnmpTrap{
Variables: []SnmpPDU{pdu},
Enterprise: trapTestEnterpriseOid,
AgentAddress: trapTestAgentAddress,
GenericTrap: trapTestGenericTrap,
SpecificTrap: trapTestSpecificTrap,
Timestamp: trapTestTimestamp,
}
_, err = ts.SendTrap(trap)
if err != nil {
t.Fatalf("SendTrap() err: %v", err)
}
// wait for response from handler
select {
case <-done:
case <-time.After(5 * time.Second):
t.Fatal("timed out waiting for trap to be received")
}
}