gosnmp

Форк
0
/
trap_test.go 
1408 строк · 34.9 Кб
1
// Copyright 2012 The GoSNMP Authors. All rights reserved.  Use of this
2
// source code is governed by a BSD-style license that can be found in the
3
// LICENSE file.
4

5
//go:build all || trap
6
// +build all trap
7

8
package gosnmp
9

10
import (
11
	"io"
12
	"log"
13
	"net"
14
	"reflect"
15
	"testing"
16
	"time"
17

18
	"github.com/stretchr/testify/require"
19
)
20

21
const (
22
	trapTestAddress = "127.0.0.1"
23

24
	// TODO this is bad. Listen and Connect expect different address formats
25
	// so we need an int version and a string version - they should be the same.
26
	trapTestPort       = 9162
27
	trapTestPortString = "9162"
28

29
	trapTestOid     = ".1.2.1234.4.5"
30
	trapTestPayload = "TRAPTEST1234"
31

32
	trapTestEnterpriseOid = ".1.2.1234"
33
	trapTestAgentAddress  = "127.0.0.1"
34
	trapTestGenericTrap   = 6
35
	trapTestSpecificTrap  = 55
36
	trapTestTimestamp     = 300
37
)
38

39
var secParamsList = []*UsmSecurityParameters{
40
	&UsmSecurityParameters{
41
		UserName:                 "myuser",
42
		AuthenticationProtocol:   MD5,
43
		AuthenticationPassphrase: "mypassword",
44
		Logger:                   NewLogger(log.New(io.Discard, "", 0)),
45
	},
46
	&UsmSecurityParameters{
47
		UserName:                 "myuser1",
48
		AuthenticationProtocol:   MD5,
49
		AuthenticationPassphrase: "mypassword1",
50
		PrivacyProtocol:          AES,
51
		PrivacyPassphrase:        "myprivacy1",
52
		Logger:                   NewLogger(log.New(io.Discard, "", 0)),
53
	},
54
	&UsmSecurityParameters{
55
		UserName:                 "myuser2",
56
		AuthenticationProtocol:   SHA,
57
		AuthenticationPassphrase: "mypassword2",
58
		PrivacyProtocol:          DES,
59
		PrivacyPassphrase:        "myprivacy2",
60
		Logger:                   NewLogger(log.New(io.Discard, "", 0)),
61
	},
62
	&UsmSecurityParameters{
63
		UserName:                 "myuser2",
64
		AuthenticationProtocol:   MD5,
65
		AuthenticationPassphrase: "mypassword2",
66
		PrivacyProtocol:          AES,
67
		PrivacyPassphrase:        "myprivacy2",
68
		Logger:                   NewLogger(log.New(io.Discard, "", 0)),
69
	},
70
}
71

72
var testsUnmarshalTrap = []struct {
73
	in  func() []byte
74
	out *SnmpPacket
75
}{
76
	{genericV3Trap,
77
		&SnmpPacket{
78
			Version:   Version3,
79
			PDUType:   SNMPv2Trap,
80
			RequestID: 957979745,
81
			MsgFlags:  AuthNoPriv,
82
			SecurityParameters: &UsmSecurityParameters{
83
				UserName:                 "myuser",
84
				AuthenticationProtocol:   MD5,
85
				AuthenticationPassphrase: "mypassword",
86
				Logger:                   NewLogger(log.New(io.Discard, "", 0)),
87
			},
88
		},
89
	},
90
	{
91
		snmpV3AuthPrivTrap,
92
		&SnmpPacket{
93
			Version:   3,
94
			PDUType:   SNMPv2Trap,
95
			RequestID: 1318065890,
96
			MsgFlags:  AuthPriv,
97
			SecurityParameters: &UsmSecurityParameters{
98
				UserName:                 "myuser2",
99
				AuthenticationProtocol:   MD5,
100
				AuthenticationPassphrase: "mypassword2",
101
				PrivacyProtocol:          AES,
102
				PrivacyPassphrase:        "myprivacy2",
103
				Logger:                   NewLogger(log.New(io.Discard, "", 0)),
104
			},
105
		},
106
	},
107
}
108

109
func TestUnmarshalTrap(t *testing.T) {
110
	Default.Logger = NewLogger(log.New(io.Discard, "", 0))
111

112
SANITY:
113
	for i, test := range testsUnmarshalTrap {
114

115
		Default.SecurityParameters = test.out.SecurityParameters.Copy()
116
		Default.Version = Version3
117
		var buf = test.in()
118
		res, err := Default.UnmarshalTrap(buf, true)
119
		require.NoError(t, err, "unmarshalTrap failed")
120
		if res == nil {
121
			t.Errorf("#%d, UnmarshalTrap returned nil", i)
122
			continue SANITY
123
		}
124

125
		// test enough fields to ensure unmarshalling was successful.
126
		// full unmarshal testing is performed in TestUnmarshal
127
		if res.Version != test.out.Version {
128
			t.Errorf("#%d Version result: %v, test: %v", i, res.Version, test.out.Version)
129
		}
130
		if res.RequestID != test.out.RequestID {
131
			t.Errorf("#%d RequestID result: %v, test: %v", i, res.RequestID, test.out.RequestID)
132
		}
133
	}
134
}
135

136
func TestUnmarshalTrapWithMultipleUsers(t *testing.T) {
137
	Default.Logger = NewLogger(log.New(io.Discard, "", 0))
138
	usmMap := NewSnmpV3SecurityParametersTable(NewLogger(log.New(io.Discard, "", 0)))
139
	for _, sp := range secParamsList {
140
		usmMap.Add(sp.UserName, sp)
141
	}
142
SANITY:
143
	for i, test := range testsUnmarshalTrap {
144
		Default.TrapSecurityParametersTable = usmMap
145
		Default.Version = Version3
146
		var buf = test.in()
147
		res, err := Default.UnmarshalTrap(buf, true)
148
		require.NoError(t, err, "unmarshalTrap failed")
149
		if res == nil {
150
			t.Errorf("#%d, UnmarshalTrap returned nil", i)
151
			continue SANITY
152
		}
153

154
		// test enough fields to ensure unmarshalling was successful.
155
		// full unmarshal testing is performed in TestUnmarshal
156
		require.Equal(t, test.out.Version, res.Version)
157
		require.Equal(t, test.out.RequestID, res.RequestID)
158

159
		Default.TrapSecurityParametersTable = nil
160
	}
161
}
162

163
func genericV3Trap() []byte {
164
	return []byte{
165
		0x30, 0x81, 0xd7, 0x02, 0x01, 0x03, 0x30, 0x11, 0x02, 0x04, 0x62, 0xaf,
166
		0x5a, 0x8e, 0x02, 0x03, 0x00, 0xff, 0xe3, 0x04, 0x01, 0x01, 0x02, 0x01,
167
		0x03, 0x04, 0x33, 0x30, 0x31, 0x04, 0x11, 0x80, 0x00, 0x1f, 0x88, 0x80,
168
		0x77, 0xdf, 0xe4, 0x4f, 0xaa, 0x70, 0x02, 0x58, 0x00, 0x00, 0x00, 0x00,
169
		0x02, 0x01, 0x0f, 0x02, 0x01, 0x00, 0x04, 0x06, 0x6d, 0x79, 0x75, 0x73,
170
		0x65, 0x72, 0x04, 0x0c, 0xd8, 0xb6, 0x9c, 0xb8, 0x22, 0x91, 0xfc, 0x65,
171
		0xb6, 0x84, 0xcb, 0xfe, 0x04, 0x00, 0x30, 0x81, 0x89, 0x04, 0x11, 0x80,
172
		0x00, 0x1f, 0x88, 0x80, 0x77, 0xdf, 0xe4, 0x4f, 0xaa, 0x70, 0x02, 0x58,
173
		0x00, 0x00, 0x00, 0x00, 0x04, 0x00, 0xa7, 0x72, 0x02, 0x04, 0x39, 0x19,
174
		0x9c, 0x61, 0x02, 0x01, 0x00, 0x02, 0x01, 0x00, 0x30, 0x64, 0x30, 0x0f,
175
		0x06, 0x08, 0x2b, 0x06, 0x01, 0x02, 0x01, 0x01, 0x03, 0x00, 0x43, 0x03,
176
		0x15, 0x2f, 0xec, 0x30, 0x14, 0x06, 0x0a, 0x2b, 0x06, 0x01, 0x06, 0x03,
177
		0x01, 0x01, 0x04, 0x01, 0x00, 0x06, 0x06, 0x2b, 0x06, 0x01, 0x02, 0x01,
178
		0x01, 0x30, 0x16, 0x06, 0x08, 0x2b, 0x06, 0x01, 0x02, 0x01, 0x01, 0x01,
179
		0x00, 0x04, 0x0a, 0x72, 0x65, 0x64, 0x20, 0x6c, 0x61, 0x70, 0x74, 0x6f,
180
		0x70, 0x30, 0x0d, 0x06, 0x08, 0x2b, 0x06, 0x01, 0x02, 0x01, 0x01, 0x07,
181
		0x00, 0x02, 0x01, 0x05, 0x30, 0x14, 0x06, 0x07, 0x2b, 0x06, 0x01, 0x02,
182
		0x01, 0x01, 0x02, 0x06, 0x09, 0x2b, 0x06, 0x01, 0x04, 0x01, 0x02, 0x03,
183
		0x04, 0x05}
184
}
185

186
/*
187
snmptrap -v3 -l authPriv -u myuser2 -a MD5 -A mypassword2 -x AES -X myprivacy2 127.0.0.1:9162 ”  1.3.6.1.4.1.8072.2.3.0.1 1.3.6.1.4.1.8072.2.3.2.1 i 60
188
*/
189
func snmpV3AuthPrivTrap() []byte {
190
	return []byte{
191
		0x30, 0x81, 0xbb, 0x02, 0x01, 0x03, 0x30, 0x11, 0x02, 0x04, 0x3a, 0x1c,
192
		0xf4, 0xf7, 0x02, 0x03, 0x00, 0xff, 0xe3, 0x04, 0x01, 0x03, 0x02, 0x01,
193
		0x03, 0x04, 0x3c, 0x30, 0x3a, 0x04, 0x11, 0x80, 0x00, 0x1f, 0x88, 0x80,
194
		0x6b, 0x8f, 0xad, 0x3b, 0x07, 0xc2, 0x70, 0x65, 0x00, 0x00, 0x00, 0x00,
195
		0x02, 0x01, 0x01, 0x02, 0x01, 0x00, 0x04, 0x07, 0x6d, 0x79, 0x75, 0x73,
196
		0x65, 0x72, 0x32, 0x04, 0x0c, 0xa8, 0xe2, 0xf4, 0xab, 0x3c, 0xd5, 0x9c,
197
		0x22, 0x5e, 0x0a, 0x12, 0xdd, 0x04, 0x08, 0x95, 0x7b, 0xdc, 0x33, 0x6a,
198
		0xf4, 0x3c, 0x8f, 0x04, 0x65, 0x70, 0x64, 0xbd, 0xcf, 0x4b, 0xa8, 0x19,
199
		0xda, 0xf4, 0x0d, 0x09, 0x8f, 0x7a, 0x28, 0xa6, 0x82, 0x00, 0xe0, 0xbd,
200
		0x96, 0x76, 0xf8, 0xc2, 0xa3, 0xe3, 0xb0, 0x92, 0x00, 0x82, 0x2d, 0xba,
201
		0xce, 0x34, 0x2f, 0x53, 0x19, 0x18, 0xba, 0xfc, 0xe5, 0xf5, 0x0e, 0x9a,
202
		0xba, 0x52, 0xaf, 0x6b, 0x67, 0xaa, 0x20, 0x23, 0xb5, 0x17, 0x04, 0x7e,
203
		0x17, 0x08, 0xb8, 0xc6, 0x67, 0x14, 0xb5, 0x91, 0x4d, 0x6b, 0xd8, 0xbf,
204
		0x94, 0x24, 0x22, 0x0f, 0x21, 0x4f, 0xde, 0x6f, 0x41, 0x51, 0xa6, 0x10,
205
		0x86, 0xf2, 0x01, 0xd1, 0xd6, 0xa9, 0x3c, 0x88, 0xea, 0x41, 0x25, 0x25,
206
		0xbc, 0x12, 0x12, 0xa6, 0xd6, 0x8f, 0x55, 0x6a, 0x55, 0xcb}
207
}
208

209
func makeTestTrapHandler(t *testing.T, done chan int, version SnmpVersion) func(*SnmpPacket, *net.UDPAddr) {
210
	Default.Logger = NewLogger(log.New(io.Discard, "", 0))
211
	return func(packet *SnmpPacket, addr *net.UDPAddr) {
212
		//log.Printf("got trapdata from %s\n", addr.IP)
213
		defer close(done)
214

215
		if version == Version1 {
216
			if packet.Enterprise != trapTestEnterpriseOid {
217
				t.Fatalf("incorrect trap Enterprise OID received, expected %s got %s", trapTestEnterpriseOid, packet.Enterprise)
218
			}
219
			if packet.AgentAddress != trapTestAgentAddress {
220
				t.Fatalf("incorrect trap Agent Address received, expected %s got %s", trapTestAgentAddress, packet.AgentAddress)
221
			}
222
			if packet.GenericTrap != trapTestGenericTrap {
223
				t.Fatalf("incorrect trap Generic Trap identifier received, expected %v got %v", trapTestGenericTrap, packet.GenericTrap)
224
			}
225
			if packet.SpecificTrap != trapTestSpecificTrap {
226
				t.Fatalf("incorrect trap Specific Trap identifier received, expected %v got %v", trapTestSpecificTrap, packet.SpecificTrap)
227
			}
228
			if packet.Timestamp != trapTestTimestamp {
229
				t.Fatalf("incorrect trap Timestamp received, expected %v got %v", trapTestTimestamp, packet.Timestamp)
230
			}
231
		}
232

233
		for _, v := range packet.Variables {
234
			switch v.Type {
235
			case OctetString:
236
				b := v.Value.([]byte)
237
				// log.Printf("OID: %s, string: %x\n", v.Name, b)
238

239
				// Only one OctetString in the payload, so it must be the expected one
240
				if v.Name != trapTestOid {
241
					t.Fatalf("incorrect trap OID received, expected %s got %s", trapTestOid, v.Name)
242
				}
243
				if string(b) != trapTestPayload {
244
					t.Fatalf("incorrect trap payload received, expected %s got %x", trapTestPayload, b)
245
				}
246
			default:
247
				// log.Printf("trap: %+v\n", v)
248
			}
249
		}
250
	}
251
}
252

253
// TODO: This restores global state set by other tests so that these tests can
254
// run. Tests should be avoiding use of global state where possible (and, if
255
// possible, use of global state other than possibly loggers should be
256
// eliminated entirely).
257
func TestRestoreGlobals(t *testing.T) {
258
	Default.Version = Version2c
259
	Default.SecurityModel = 0
260
	Default.SecurityParameters = nil
261
}
262

263
// test sending a basic SNMP trap, using our own listener to receive
264
func TestSendTrapBasic(t *testing.T) {
265
	done := make(chan int)
266

267
	tl := NewTrapListener()
268
	defer tl.Close()
269

270
	tl.OnNewTrap = makeTestTrapHandler(t, done, Version2c)
271
	tl.Params = Default
272

273
	// listener goroutine
274
	errch := make(chan error)
275
	go func() {
276
		// defer close(errch)
277
		err := tl.Listen(net.JoinHostPort(trapTestAddress, trapTestPortString))
278
		if err != nil {
279
			errch <- err
280
		}
281
	}()
282

283
	// Wait until the listener is ready.
284
	select {
285
	case <-tl.Listening():
286
	case err := <-errch:
287
		t.Fatalf("error in listen: %v", err)
288
	}
289
	ts := &GoSNMP{
290
		Target:    trapTestAddress,
291
		Port:      trapTestPort,
292
		Community: "public",
293
		Version:   Version2c,
294
		Timeout:   time.Duration(2) * time.Second,
295
		Retries:   3,
296
		MaxOids:   MaxOids,
297
		Logger:    NewLogger(log.New(io.Discard, "", 0)),
298
	}
299

300
	err := ts.Connect()
301
	if err != nil {
302
		t.Fatalf("Connect() err: %v", err)
303
	}
304
	defer ts.Conn.Close()
305

306
	pdu := SnmpPDU{
307
		Name:  trapTestOid,
308
		Type:  OctetString,
309
		Value: trapTestPayload,
310
	}
311

312
	trap := SnmpTrap{
313
		Variables: []SnmpPDU{pdu},
314
	}
315

316
	_, err = ts.SendTrap(trap)
317
	if err != nil {
318
		t.Fatalf("SendTrap() err: %v", err)
319
	}
320

321
	// wait for response from handler
322
	select {
323
	case <-done:
324
	case <-time.After(2 * time.Second):
325
		t.Fatal("timed out waiting for trap to be received")
326
	}
327
}
328

329
// test sending a basic SNMP inform and receiving the response
330
func TestSendInformBasic(t *testing.T) {
331
	done := make(chan int)
332

333
	tl := NewTrapListener()
334
	defer tl.Close()
335

336
	tl.OnNewTrap = makeTestTrapHandler(t, done, Version2c)
337
	tl.Params = Default
338

339
	// listener goroutine
340
	errch := make(chan error)
341
	go func() {
342
		// defer close(errch)
343
		err := tl.Listen(net.JoinHostPort(trapTestAddress, trapTestPortString))
344
		if err != nil {
345
			errch <- err
346
		}
347
	}()
348

349
	// Wait until the listener is ready.
350
	select {
351
	case <-tl.Listening():
352
	case err := <-errch:
353
		t.Fatalf("error in listen: %v", err)
354
	}
355

356
	ts := &GoSNMP{
357
		Target:    trapTestAddress,
358
		Port:      trapTestPort,
359
		Community: "public",
360
		Version:   Version2c,
361
		Timeout:   time.Duration(2) * time.Second,
362
		Retries:   3,
363
		MaxOids:   MaxOids,
364
	}
365

366
	err := ts.Connect()
367
	if err != nil {
368
		t.Fatalf("Connect() err: %v", err)
369
	}
370
	defer ts.Conn.Close()
371

372
	pdu := SnmpPDU{
373
		Name:  trapTestOid,
374
		Type:  OctetString,
375
		Value: trapTestPayload,
376
	}
377

378
	// Make it an inform.
379
	trap := SnmpTrap{
380
		Variables: []SnmpPDU{pdu},
381
		IsInform:  true,
382
	}
383

384
	var resp *SnmpPacket
385
	resp, err = ts.SendTrap(trap)
386
	if err != nil {
387
		t.Fatalf("SendTrap() err: %v", err)
388
	}
389

390
	// wait for response from handler
391
	select {
392
	case <-done:
393
	case <-time.After(2 * time.Second):
394
		t.Fatal("timed out waiting for trap to be received")
395
	}
396

397
	if resp.PDUType != GetResponse {
398
		t.Fatal("Inform response is not a response PDU")
399
	}
400

401
	for i, tv := range trap.Variables {
402
		rv := resp.Variables[i+1]
403
		if reflect.DeepEqual(tv, rv) {
404
			t.Fatalf("Expected variable %d = %#v, got %#v", i, tv, rv)
405
		}
406
	}
407
}
408

409
// test the listener is not blocked if Listening is not used
410
func TestSendTrapWithoutWaitingOnListen(t *testing.T) {
411
	done := make(chan int)
412

413
	tl := NewTrapListener()
414
	defer tl.Close()
415

416
	tl.OnNewTrap = makeTestTrapHandler(t, done, Version2c)
417
	tl.Params = Default
418

419
	errch := make(chan error)
420
	listening := make(chan bool)
421
	go func() {
422
		// Reduce the chance of necessity for a restart.
423
		listening <- true
424

425
		err := tl.Listen(net.JoinHostPort(trapTestAddress, trapTestPortString))
426
		if err != nil {
427
			errch <- err
428
		}
429
	}()
430

431
	select {
432
	case <-listening:
433
	case err := <-errch:
434
		t.Fatalf("error in listen: %v", err)
435
	}
436

437
	ts := &GoSNMP{
438
		Target:    trapTestAddress,
439
		Port:      trapTestPort,
440
		Community: "public",
441
		Version:   Version2c,
442
		Timeout:   time.Duration(2) * time.Second,
443
		Retries:   3,
444
		MaxOids:   MaxOids,
445
	}
446

447
	err := ts.Connect()
448
	if err != nil {
449
		t.Fatalf("Connect() err: %v", err)
450
	}
451
	defer ts.Conn.Close()
452

453
	pdu := SnmpPDU{
454
		Name:  trapTestOid,
455
		Type:  OctetString,
456
		Value: trapTestPayload,
457
	}
458

459
	trap := SnmpTrap{
460
		Variables: []SnmpPDU{pdu},
461
	}
462

463
	_, err = ts.SendTrap(trap)
464
	if err != nil {
465
		t.Fatalf("SendTrap() err: %v", err)
466
	}
467

468
	// Wait for a response from the handler and restart the SendTrap
469
	// if the listener wasn't ready.
470
	select {
471
	case <-done:
472
	case <-time.After(2 * time.Second):
473
		_, err = ts.SendTrap(trap)
474
		if err != nil {
475
			t.Fatalf("restarted SendTrap() err: %v", err)
476
		}
477

478
		t.Log("restarted")
479

480
		select {
481
		case <-done:
482
		case <-time.After(2 * time.Second):
483
			t.Fatal("timed out waiting for trap to be received")
484
		}
485
	}
486
}
487

488
// test sending a basic SNMP trap, using our own listener to receive
489
func TestSendV1Trap(t *testing.T) {
490
	done := make(chan int)
491

492
	tl := NewTrapListener()
493
	defer tl.Close()
494

495
	tl.OnNewTrap = makeTestTrapHandler(t, done, Version1)
496
	tl.Params = Default
497

498
	// listener goroutine
499
	errch := make(chan error)
500
	go func() {
501
		err := tl.Listen(net.JoinHostPort(trapTestAddress, trapTestPortString))
502
		if err != nil {
503
			errch <- err
504
		}
505
	}()
506

507
	// Wait until the listener is ready.
508
	select {
509
	case <-tl.Listening():
510
	case err := <-errch:
511
		t.Fatalf("error in listen: %v", err)
512
	}
513

514
	ts := &GoSNMP{
515
		Target: trapTestAddress,
516
		Port:   trapTestPort,
517
		//Community: "public",
518
		Version: Version1,
519
		Timeout: time.Duration(2) * time.Second,
520
		Retries: 3,
521
		MaxOids: MaxOids,
522
	}
523

524
	err := ts.Connect()
525
	if err != nil {
526
		t.Fatalf("Connect() err: %v", err)
527
	}
528
	defer ts.Conn.Close()
529

530
	pdu := SnmpPDU{
531
		Name:  trapTestOid,
532
		Type:  OctetString,
533
		Value: trapTestPayload,
534
	}
535

536
	trap := SnmpTrap{
537
		Variables:    []SnmpPDU{pdu},
538
		Enterprise:   trapTestEnterpriseOid,
539
		AgentAddress: trapTestAgentAddress,
540
		GenericTrap:  trapTestGenericTrap,
541
		SpecificTrap: trapTestSpecificTrap,
542
		Timestamp:    trapTestTimestamp,
543
	}
544

545
	_, err = ts.SendTrap(trap)
546
	if err != nil {
547
		t.Fatalf("SendTrap() err: %v", err)
548
	}
549

550
	// wait for response from handler
551
	select {
552
	case <-done:
553
	case <-time.After(2 * time.Second):
554
		t.Fatal("timed out waiting for trap to be received")
555
	}
556
}
557

558
func TestSendV3TrapNoAuthNoPriv(t *testing.T) {
559
	done := make(chan int)
560

561
	tl := NewTrapListener()
562
	defer tl.Close()
563

564
	sp := &UsmSecurityParameters{
565
		UserName:                 "test",
566
		AuthoritativeEngineBoots: 1,
567
		AuthoritativeEngineTime:  1,
568
		AuthoritativeEngineID:    string([]byte{0x80, 0x00, 0x00, 0x00, 0x01, 0x02, 0x03, 0x04}),
569
	}
570

571
	tl.OnNewTrap = makeTestTrapHandler(t, done, Version3)
572
	tl.Params = Default
573
	tl.Params.Version = Version3
574
	tl.Params.SecurityParameters = sp
575
	tl.Params.SecurityModel = UserSecurityModel
576
	tl.Params.MsgFlags = NoAuthNoPriv
577

578
	// listener goroutine
579
	errch := make(chan error)
580
	go func() {
581
		err := tl.Listen(net.JoinHostPort(trapTestAddress, trapTestPortString))
582
		if err != nil {
583
			errch <- err
584
		}
585
	}()
586

587
	// Wait until the listener is ready.
588
	select {
589
	case <-tl.Listening():
590
	case err := <-errch:
591
		t.Fatalf("error in listen: %v", err)
592
	}
593

594
	ts := &GoSNMP{
595
		Target: trapTestAddress,
596
		Port:   trapTestPort,
597
		//Community: "public",
598
		Version:            Version3,
599
		Timeout:            time.Duration(2) * time.Second,
600
		Retries:            3,
601
		MaxOids:            MaxOids,
602
		SecurityModel:      UserSecurityModel,
603
		SecurityParameters: sp,
604
		MsgFlags:           NoAuthNoPriv,
605
	}
606

607
	err := ts.Connect()
608
	if err != nil {
609
		t.Fatalf("Connect() err: %v", err)
610
	}
611
	defer ts.Conn.Close()
612

613
	pdu := SnmpPDU{
614
		Name:  trapTestOid,
615
		Type:  OctetString,
616
		Value: trapTestPayload,
617
	}
618

619
	trap := SnmpTrap{
620
		Variables:    []SnmpPDU{pdu},
621
		Enterprise:   trapTestEnterpriseOid,
622
		AgentAddress: trapTestAgentAddress,
623
		GenericTrap:  trapTestGenericTrap,
624
		SpecificTrap: trapTestSpecificTrap,
625
		Timestamp:    trapTestTimestamp,
626
	}
627

628
	_, err = ts.SendTrap(trap)
629
	if err != nil {
630
		t.Fatalf("SendTrap() err: %v", err)
631
	}
632

633
	// wait for response from handler
634
	select {
635
	case <-done:
636
	case <-time.After(2 * time.Second):
637
		t.Fatal("timed out waiting for trap to be received")
638
	}
639

640
}
641

642
func TestSendV3TrapMD5AuthNoPriv(t *testing.T) {
643
	done := make(chan int)
644

645
	tl := NewTrapListener()
646
	defer tl.Close()
647

648
	sp := &UsmSecurityParameters{
649
		UserName:                 "test",
650
		AuthenticationProtocol:   MD5,
651
		AuthenticationPassphrase: "password",
652
		AuthoritativeEngineBoots: 1,
653
		AuthoritativeEngineTime:  1,
654
		AuthoritativeEngineID:    string([]byte{0x80, 0x00, 0x00, 0x00, 0x01, 0x02, 0x03, 0x04}),
655
	}
656

657
	tl.OnNewTrap = makeTestTrapHandler(t, done, Version3)
658
	tl.Params = Default
659
	tl.Params.Version = Version3
660
	tl.Params.SecurityParameters = sp
661
	tl.Params.SecurityModel = UserSecurityModel
662
	tl.Params.MsgFlags = AuthNoPriv
663

664
	// listener goroutine
665
	errch := make(chan error)
666
	go func() {
667
		err := tl.Listen(net.JoinHostPort(trapTestAddress, trapTestPortString))
668
		if err != nil {
669
			errch <- err
670
		}
671
	}()
672

673
	// Wait until the listener is ready.
674
	select {
675
	case <-tl.Listening():
676
	case err := <-errch:
677
		t.Fatalf("error in listen: %v", err)
678
	}
679

680
	ts := &GoSNMP{
681
		Target: trapTestAddress,
682
		Port:   trapTestPort,
683
		//Community: "public",
684
		Version:            Version3,
685
		Timeout:            time.Duration(2) * time.Second,
686
		Retries:            3,
687
		MaxOids:            MaxOids,
688
		SecurityModel:      UserSecurityModel,
689
		SecurityParameters: sp,
690
		MsgFlags:           AuthNoPriv,
691
	}
692

693
	err := ts.Connect()
694
	if err != nil {
695
		t.Fatalf("Connect() err: %v", err)
696
	}
697
	defer ts.Conn.Close()
698

699
	pdu := SnmpPDU{
700
		Name:  trapTestOid,
701
		Type:  OctetString,
702
		Value: trapTestPayload,
703
	}
704

705
	trap := SnmpTrap{
706
		Variables:    []SnmpPDU{pdu},
707
		Enterprise:   trapTestEnterpriseOid,
708
		AgentAddress: trapTestAgentAddress,
709
		GenericTrap:  trapTestGenericTrap,
710
		SpecificTrap: trapTestSpecificTrap,
711
		Timestamp:    trapTestTimestamp,
712
	}
713

714
	_, err = ts.SendTrap(trap)
715
	if err != nil {
716
		t.Fatalf("SendTrap() err: %v", err)
717
	}
718

719
	// wait for response from handler
720
	select {
721
	case <-done:
722
	case <-time.After(2 * time.Second):
723
		t.Fatal("timed out waiting for trap to be received")
724
	}
725

726
}
727

728
func TestSendV3TrapSHAAuthNoPriv(t *testing.T) {
729
	done := make(chan int)
730

731
	tl := NewTrapListener()
732
	defer tl.Close()
733

734
	sp := &UsmSecurityParameters{
735
		UserName:                 "test",
736
		AuthenticationProtocol:   SHA,
737
		AuthenticationPassphrase: "password",
738
		AuthoritativeEngineBoots: 1,
739
		AuthoritativeEngineTime:  1,
740
		AuthoritativeEngineID:    string([]byte{0x80, 0x00, 0x00, 0x00, 0x01, 0x02, 0x03, 0x04}),
741
	}
742

743
	tl.OnNewTrap = makeTestTrapHandler(t, done, Version3)
744
	tl.Params = Default
745
	tl.Params.Version = Version3
746
	tl.Params.SecurityParameters = sp
747
	tl.Params.SecurityModel = UserSecurityModel
748
	tl.Params.MsgFlags = AuthNoPriv
749

750
	// listener goroutine
751
	errch := make(chan error)
752
	go func() {
753
		err := tl.Listen(net.JoinHostPort(trapTestAddress, trapTestPortString))
754
		if err != nil {
755
			errch <- err
756
		}
757
	}()
758

759
	// Wait until the listener is ready.
760
	select {
761
	case <-tl.Listening():
762
	case err := <-errch:
763
		t.Fatalf("error in listen: %v", err)
764
	}
765

766
	ts := &GoSNMP{
767
		Target: trapTestAddress,
768
		Port:   trapTestPort,
769
		//Community: "public",
770
		Version:            Version3,
771
		Timeout:            time.Duration(2) * time.Second,
772
		Retries:            3,
773
		MaxOids:            MaxOids,
774
		SecurityModel:      UserSecurityModel,
775
		SecurityParameters: sp,
776
		MsgFlags:           AuthNoPriv,
777
	}
778

779
	err := ts.Connect()
780
	if err != nil {
781
		t.Fatalf("Connect() err: %v", err)
782
	}
783
	defer ts.Conn.Close()
784

785
	pdu := SnmpPDU{
786
		Name:  trapTestOid,
787
		Type:  OctetString,
788
		Value: trapTestPayload,
789
	}
790

791
	trap := SnmpTrap{
792
		Variables:    []SnmpPDU{pdu},
793
		Enterprise:   trapTestEnterpriseOid,
794
		AgentAddress: trapTestAgentAddress,
795
		GenericTrap:  trapTestGenericTrap,
796
		SpecificTrap: trapTestSpecificTrap,
797
		Timestamp:    trapTestTimestamp,
798
	}
799

800
	_, err = ts.SendTrap(trap)
801
	if err != nil {
802
		t.Fatalf("SendTrap() err: %v", err)
803
	}
804

805
	// wait for response from handler
806
	select {
807
	case <-done:
808
	case <-time.After(2 * time.Second):
809
		t.Fatal("timed out waiting for trap to be received")
810
	}
811

812
}
813
func TestSendV3TrapSHAAuthDESPriv(t *testing.T) {
814
	done := make(chan int)
815

816
	tl := NewTrapListener()
817
	defer tl.Close()
818

819
	sp := &UsmSecurityParameters{
820
		UserName:                 "test",
821
		AuthenticationProtocol:   SHA,
822
		AuthenticationPassphrase: "password",
823
		PrivacyProtocol:          DES,
824
		PrivacyPassphrase:        "password",
825
		AuthoritativeEngineBoots: 1,
826
		AuthoritativeEngineTime:  1,
827
		AuthoritativeEngineID:    string([]byte{0x80, 0x00, 0x00, 0x00, 0x01, 0x02, 0x03, 0x04}),
828
	}
829

830
	tl.OnNewTrap = makeTestTrapHandler(t, done, Version3)
831
	tl.Params = Default
832
	tl.Params.Version = Version3
833
	tl.Params.SecurityParameters = sp
834
	tl.Params.SecurityModel = UserSecurityModel
835
	tl.Params.MsgFlags = AuthPriv
836

837
	// listener goroutine
838
	errch := make(chan error)
839
	go func() {
840
		err := tl.Listen(net.JoinHostPort(trapTestAddress, trapTestPortString))
841
		if err != nil {
842
			errch <- err
843
		}
844
	}()
845

846
	// Wait until the listener is ready.
847
	select {
848
	case <-tl.Listening():
849
	case err := <-errch:
850
		t.Fatalf("error in listen: %v", err)
851
	}
852

853
	ts := &GoSNMP{
854
		Target: trapTestAddress,
855
		Port:   trapTestPort,
856
		//Community: "public",
857
		Version:            Version3,
858
		Timeout:            time.Duration(2) * time.Second,
859
		Retries:            3,
860
		MaxOids:            MaxOids,
861
		SecurityModel:      UserSecurityModel,
862
		SecurityParameters: sp,
863
		MsgFlags:           AuthPriv,
864
	}
865

866
	err := ts.Connect()
867
	if err != nil {
868
		t.Fatalf("Connect() err: %v", err)
869
	}
870
	defer ts.Conn.Close()
871

872
	pdu := SnmpPDU{
873
		Name:  trapTestOid,
874
		Type:  OctetString,
875
		Value: trapTestPayload,
876
	}
877

878
	trap := SnmpTrap{
879
		Variables:    []SnmpPDU{pdu},
880
		Enterprise:   trapTestEnterpriseOid,
881
		AgentAddress: trapTestAgentAddress,
882
		GenericTrap:  trapTestGenericTrap,
883
		SpecificTrap: trapTestSpecificTrap,
884
		Timestamp:    trapTestTimestamp,
885
	}
886

887
	_, err = ts.SendTrap(trap)
888
	if err != nil {
889
		t.Fatalf("SendTrap() err: %v", err)
890
	}
891

892
	// wait for response from handler
893
	select {
894
	case <-done:
895
	case <-time.After(2 * time.Second):
896
		t.Fatal("timed out waiting for trap to be received")
897
	}
898

899
}
900

901
func TestSendV3TrapSHAAuthAESPriv(t *testing.T) {
902
	done := make(chan int)
903

904
	tl := NewTrapListener()
905
	defer tl.Close()
906

907
	sp := &UsmSecurityParameters{
908
		UserName:                 "test",
909
		AuthenticationProtocol:   SHA,
910
		AuthenticationPassphrase: "password",
911
		PrivacyProtocol:          AES,
912
		PrivacyPassphrase:        "password",
913
		AuthoritativeEngineBoots: 1,
914
		AuthoritativeEngineTime:  1,
915
		AuthoritativeEngineID:    string([]byte{0x80, 0x00, 0x00, 0x00, 0x01, 0x02, 0x03, 0x04}),
916
	}
917

918
	tl.OnNewTrap = makeTestTrapHandler(t, done, Version3)
919
	tl.Params = Default
920
	tl.Params.Version = Version3
921
	tl.Params.SecurityParameters = sp
922
	tl.Params.SecurityModel = UserSecurityModel
923
	tl.Params.MsgFlags = AuthPriv
924

925
	// listener goroutine
926
	errch := make(chan error)
927
	go func() {
928
		err := tl.Listen(net.JoinHostPort(trapTestAddress, trapTestPortString))
929
		if err != nil {
930
			errch <- err
931
		}
932
	}()
933

934
	// Wait until the listener is ready.
935
	select {
936
	case <-tl.Listening():
937
	case err := <-errch:
938
		t.Fatalf("error in listen: %v", err)
939
	}
940

941
	ts := &GoSNMP{
942
		Target: trapTestAddress,
943
		Port:   trapTestPort,
944
		//Community: "public",
945
		Version:            Version3,
946
		Timeout:            time.Duration(2) * time.Second,
947
		Retries:            3,
948
		MaxOids:            MaxOids,
949
		SecurityModel:      UserSecurityModel,
950
		SecurityParameters: sp,
951
		MsgFlags:           AuthPriv,
952
	}
953

954
	err := ts.Connect()
955
	if err != nil {
956
		t.Fatalf("Connect() err: %v", err)
957
	}
958
	defer ts.Conn.Close()
959

960
	pdu := SnmpPDU{
961
		Name:  trapTestOid,
962
		Type:  OctetString,
963
		Value: trapTestPayload,
964
	}
965

966
	trap := SnmpTrap{
967
		Variables:    []SnmpPDU{pdu},
968
		Enterprise:   trapTestEnterpriseOid,
969
		AgentAddress: trapTestAgentAddress,
970
		GenericTrap:  trapTestGenericTrap,
971
		SpecificTrap: trapTestSpecificTrap,
972
		Timestamp:    trapTestTimestamp,
973
	}
974

975
	_, err = ts.SendTrap(trap)
976
	if err != nil {
977
		t.Fatalf("SendTrap() err: %v", err)
978
	}
979

980
	// wait for response from handler
981
	select {
982
	case <-done:
983
	case <-time.After(2 * time.Second):
984
		t.Fatal("timed out waiting for trap to be received")
985
	}
986

987
}
988

989
func TestSendV3TrapSHAAuthAES192Priv(t *testing.T) {
990
	done := make(chan int)
991

992
	tl := NewTrapListener()
993
	defer tl.Close()
994

995
	sp := &UsmSecurityParameters{
996
		UserName:                 "test",
997
		AuthenticationProtocol:   SHA,
998
		AuthenticationPassphrase: "password",
999
		PrivacyProtocol:          AES192,
1000
		PrivacyPassphrase:        "password",
1001
		AuthoritativeEngineBoots: 1,
1002
		AuthoritativeEngineTime:  1,
1003
		AuthoritativeEngineID:    string([]byte{0x80, 0x00, 0x00, 0x00, 0x01, 0x02, 0x03, 0x04}),
1004
	}
1005

1006
	tl.OnNewTrap = makeTestTrapHandler(t, done, Version3)
1007
	tl.Params = Default
1008
	tl.Params.Version = Version3
1009
	tl.Params.SecurityParameters = sp
1010
	tl.Params.SecurityModel = UserSecurityModel
1011
	tl.Params.MsgFlags = AuthPriv
1012

1013
	// listener goroutine
1014
	errch := make(chan error)
1015
	go func() {
1016
		err := tl.Listen(net.JoinHostPort(trapTestAddress, trapTestPortString))
1017
		if err != nil {
1018
			errch <- err
1019
		}
1020
	}()
1021

1022
	// Wait until the listener is ready.
1023
	select {
1024
	case <-tl.Listening():
1025
	case err := <-errch:
1026
		t.Fatalf("error in listen: %v", err)
1027
	}
1028

1029
	ts := &GoSNMP{
1030
		Target: trapTestAddress,
1031
		Port:   trapTestPort,
1032
		//Community: "public",
1033
		Version:            Version3,
1034
		Timeout:            time.Duration(2) * time.Second,
1035
		Retries:            3,
1036
		MaxOids:            MaxOids,
1037
		SecurityModel:      UserSecurityModel,
1038
		SecurityParameters: sp,
1039
		MsgFlags:           AuthPriv,
1040
	}
1041

1042
	err := ts.Connect()
1043
	if err != nil {
1044
		t.Fatalf("Connect() err: %v", err)
1045
	}
1046
	defer ts.Conn.Close()
1047

1048
	pdu := SnmpPDU{
1049
		Name:  trapTestOid,
1050
		Type:  OctetString,
1051
		Value: trapTestPayload,
1052
	}
1053

1054
	trap := SnmpTrap{
1055
		Variables:    []SnmpPDU{pdu},
1056
		Enterprise:   trapTestEnterpriseOid,
1057
		AgentAddress: trapTestAgentAddress,
1058
		GenericTrap:  trapTestGenericTrap,
1059
		SpecificTrap: trapTestSpecificTrap,
1060
		Timestamp:    trapTestTimestamp,
1061
	}
1062

1063
	_, err = ts.SendTrap(trap)
1064
	if err != nil {
1065
		t.Fatalf("SendTrap() err: %v", err)
1066
	}
1067

1068
	// wait for response from handler
1069
	select {
1070
	case <-done:
1071
	case <-time.After(2 * time.Second):
1072
		t.Fatal("timed out waiting for trap to be received")
1073
	}
1074

1075
}
1076
func TestSendV3TrapSHAAuthAES192CPriv(t *testing.T) {
1077
	done := make(chan int)
1078

1079
	tl := NewTrapListener()
1080
	defer tl.Close()
1081

1082
	sp := &UsmSecurityParameters{
1083
		UserName:                 "test",
1084
		AuthenticationProtocol:   SHA,
1085
		AuthenticationPassphrase: "password",
1086
		PrivacyProtocol:          AES192C,
1087
		PrivacyPassphrase:        "password",
1088
		AuthoritativeEngineBoots: 1,
1089
		AuthoritativeEngineTime:  1,
1090
		AuthoritativeEngineID:    string([]byte{0x80, 0x00, 0x00, 0x00, 0x01, 0x02, 0x03, 0x04}),
1091
	}
1092

1093
	tl.OnNewTrap = makeTestTrapHandler(t, done, Version3)
1094
	tl.Params = Default
1095
	tl.Params.Version = Version3
1096
	tl.Params.SecurityParameters = sp
1097
	tl.Params.SecurityModel = UserSecurityModel
1098
	tl.Params.MsgFlags = AuthPriv
1099

1100
	// listener goroutine
1101
	errch := make(chan error)
1102
	go func() {
1103
		err := tl.Listen(net.JoinHostPort(trapTestAddress, trapTestPortString))
1104
		if err != nil {
1105
			errch <- err
1106
		}
1107
	}()
1108

1109
	// Wait until the listener is ready.
1110
	select {
1111
	case <-tl.Listening():
1112
	case err := <-errch:
1113
		t.Fatalf("error in listen: %v", err)
1114
	}
1115

1116
	ts := &GoSNMP{
1117
		Target: trapTestAddress,
1118
		Port:   trapTestPort,
1119
		//Community: "public",
1120
		Version:            Version3,
1121
		Timeout:            time.Duration(2) * time.Second,
1122
		Retries:            3,
1123
		MaxOids:            MaxOids,
1124
		SecurityModel:      UserSecurityModel,
1125
		SecurityParameters: sp,
1126
		MsgFlags:           AuthPriv,
1127
	}
1128

1129
	err := ts.Connect()
1130
	if err != nil {
1131
		t.Fatalf("Connect() err: %v", err)
1132
	}
1133
	defer ts.Conn.Close()
1134

1135
	pdu := SnmpPDU{
1136
		Name:  trapTestOid,
1137
		Type:  OctetString,
1138
		Value: trapTestPayload,
1139
	}
1140

1141
	trap := SnmpTrap{
1142
		Variables:    []SnmpPDU{pdu},
1143
		Enterprise:   trapTestEnterpriseOid,
1144
		AgentAddress: trapTestAgentAddress,
1145
		GenericTrap:  trapTestGenericTrap,
1146
		SpecificTrap: trapTestSpecificTrap,
1147
		Timestamp:    trapTestTimestamp,
1148
	}
1149

1150
	_, err = ts.SendTrap(trap)
1151
	if err != nil {
1152
		t.Fatalf("SendTrap() err: %v", err)
1153
	}
1154

1155
	// wait for response from handler
1156
	select {
1157
	case <-done:
1158
	case <-time.After(2 * time.Second):
1159
		t.Fatal("timed out waiting for trap to be received")
1160
	}
1161
}
1162
func TestSendV3TrapSHAAuthAES256Priv(t *testing.T) {
1163
	done := make(chan int)
1164

1165
	tl := NewTrapListener()
1166
	defer tl.Close()
1167

1168
	sp := &UsmSecurityParameters{
1169
		UserName:                 "test",
1170
		AuthenticationProtocol:   SHA,
1171
		AuthenticationPassphrase: "password",
1172
		PrivacyProtocol:          AES256,
1173
		PrivacyPassphrase:        "password",
1174
		AuthoritativeEngineBoots: 1,
1175
		AuthoritativeEngineTime:  1,
1176
		AuthoritativeEngineID:    string([]byte{0x80, 0x00, 0x00, 0x00, 0x01, 0x02, 0x03, 0x04}),
1177
	}
1178

1179
	tl.OnNewTrap = makeTestTrapHandler(t, done, Version3)
1180
	tl.Params = Default
1181
	tl.Params.Version = Version3
1182
	tl.Params.SecurityParameters = sp
1183
	tl.Params.SecurityModel = UserSecurityModel
1184
	tl.Params.MsgFlags = AuthPriv
1185

1186
	// listener goroutine
1187
	errch := make(chan error)
1188
	go func() {
1189
		err := tl.Listen(net.JoinHostPort(trapTestAddress, trapTestPortString))
1190
		if err != nil {
1191
			errch <- err
1192
		}
1193
	}()
1194

1195
	// Wait until the listener is ready.
1196
	select {
1197
	case <-tl.Listening():
1198
	case err := <-errch:
1199
		t.Fatalf("error in listen: %v", err)
1200
	}
1201

1202
	ts := &GoSNMP{
1203
		Target: trapTestAddress,
1204
		Port:   trapTestPort,
1205
		//Community: "public",
1206
		Version:            Version3,
1207
		Timeout:            time.Duration(2) * time.Second,
1208
		Retries:            3,
1209
		MaxOids:            MaxOids,
1210
		SecurityModel:      UserSecurityModel,
1211
		SecurityParameters: sp,
1212
		MsgFlags:           AuthPriv,
1213
	}
1214

1215
	err := ts.Connect()
1216
	if err != nil {
1217
		t.Fatalf("Connect() err: %v", err)
1218
	}
1219
	defer ts.Conn.Close()
1220

1221
	pdu := SnmpPDU{
1222
		Name:  trapTestOid,
1223
		Type:  OctetString,
1224
		Value: trapTestPayload,
1225
	}
1226

1227
	trap := SnmpTrap{
1228
		Variables:    []SnmpPDU{pdu},
1229
		Enterprise:   trapTestEnterpriseOid,
1230
		AgentAddress: trapTestAgentAddress,
1231
		GenericTrap:  trapTestGenericTrap,
1232
		SpecificTrap: trapTestSpecificTrap,
1233
		Timestamp:    trapTestTimestamp,
1234
	}
1235

1236
	_, err = ts.SendTrap(trap)
1237
	if err != nil {
1238
		t.Fatalf("SendTrap() err: %v", err)
1239
	}
1240

1241
	// wait for response from handler
1242
	select {
1243
	case <-done:
1244
	case <-time.After(2 * time.Second):
1245
		t.Fatal("timed out waiting for trap to be received")
1246
	}
1247

1248
}
1249
func TestSendV3TrapSHAAuthAES256CPriv(t *testing.T) {
1250
	done := make(chan int)
1251

1252
	tl := NewTrapListener()
1253
	defer tl.Close()
1254

1255
	sp := &UsmSecurityParameters{
1256
		UserName:                 "test",
1257
		AuthenticationProtocol:   SHA,
1258
		AuthenticationPassphrase: "password",
1259
		PrivacyProtocol:          AES256C,
1260
		PrivacyPassphrase:        "password",
1261
		AuthoritativeEngineBoots: 1,
1262
		AuthoritativeEngineTime:  1,
1263
		AuthoritativeEngineID:    string([]byte{0x80, 0x00, 0x00, 0x00, 0x01, 0x02, 0x03, 0x04}),
1264
	}
1265

1266
	tl.OnNewTrap = makeTestTrapHandler(t, done, Version3)
1267
	tl.Params = Default
1268
	tl.Params.Version = Version3
1269
	tl.Params.SecurityParameters = sp
1270
	tl.Params.SecurityModel = UserSecurityModel
1271
	tl.Params.MsgFlags = AuthPriv
1272

1273
	// listener goroutine
1274
	errch := make(chan error)
1275
	go func() {
1276
		err := tl.Listen(net.JoinHostPort(trapTestAddress, trapTestPortString))
1277
		if err != nil {
1278
			errch <- err
1279
		}
1280
	}()
1281

1282
	// Wait until the listener is ready.
1283
	select {
1284
	case <-tl.Listening():
1285
	case err := <-errch:
1286
		t.Fatalf("error in listen: %v", err)
1287
	}
1288

1289
	ts := &GoSNMP{
1290
		Target: trapTestAddress,
1291
		Port:   trapTestPort,
1292
		//Community: "public",
1293
		Version:            Version3,
1294
		Timeout:            time.Duration(2) * time.Second,
1295
		Retries:            3,
1296
		MaxOids:            MaxOids,
1297
		SecurityModel:      UserSecurityModel,
1298
		SecurityParameters: sp,
1299
		MsgFlags:           AuthPriv,
1300
	}
1301

1302
	err := ts.Connect()
1303
	if err != nil {
1304
		t.Fatalf("Connect() err: %v", err)
1305
	}
1306
	defer ts.Conn.Close()
1307

1308
	pdu := SnmpPDU{
1309
		Name:  trapTestOid,
1310
		Type:  OctetString,
1311
		Value: trapTestPayload,
1312
	}
1313

1314
	trap := SnmpTrap{
1315
		Variables:    []SnmpPDU{pdu},
1316
		Enterprise:   trapTestEnterpriseOid,
1317
		AgentAddress: trapTestAgentAddress,
1318
		GenericTrap:  trapTestGenericTrap,
1319
		SpecificTrap: trapTestSpecificTrap,
1320
		Timestamp:    trapTestTimestamp,
1321
	}
1322

1323
	_, err = ts.SendTrap(trap)
1324
	if err != nil {
1325
		t.Fatalf("SendTrap() err: %v", err)
1326
	}
1327

1328
	// wait for response from handler
1329
	select {
1330
	case <-done:
1331
	case <-time.After(2 * time.Second):
1332
		t.Fatal("timed out waiting for trap to be received")
1333
	}
1334

1335
}
1336

1337
func TestSendV3EngineIdDiscovery(t *testing.T) {
1338
	tl := NewTrapListener()
1339
	defer tl.Close()
1340
	authorativeEngineID := string([]byte{0x80, 0x00, 0x00, 0x00, 0x01, 0x02, 0x03, 0x04})
1341
	unknownEngineID := string([]byte{0x80, 0x00, 0x00, 0x00, 0x01, 0x02, 0x03, 0x05})
1342
	sp := &UsmSecurityParameters{
1343
		UserName:                 "test",
1344
		AuthenticationProtocol:   SHA,
1345
		AuthenticationPassphrase: "password",
1346
		PrivacyProtocol:          AES256,
1347
		PrivacyPassphrase:        "password",
1348
		AuthoritativeEngineBoots: 1,
1349
		AuthoritativeEngineTime:  1,
1350
		AuthoritativeEngineID:    authorativeEngineID,
1351
	}
1352
	tl.Params = Default
1353
	tl.Params.Version = Version3
1354
	tl.Params.SecurityParameters = sp
1355
	tl.Params.SecurityModel = UserSecurityModel
1356
	tl.Params.MsgFlags = AuthPriv
1357

1358
	// listener goroutine
1359
	errch := make(chan error)
1360
	go func() {
1361
		err := tl.Listen(net.JoinHostPort(trapTestAddress, trapTestPortString))
1362
		if err != nil {
1363
			errch <- err
1364
		}
1365
	}()
1366

1367
	// Wait until the listener is ready.
1368
	select {
1369
	case <-tl.Listening():
1370
	case err := <-errch:
1371
		t.Fatalf("error in listen: %v", err)
1372
	}
1373

1374
	ts := &GoSNMP{
1375
		Target: trapTestAddress,
1376
		Port:   trapTestPort,
1377
		//Community: "public",
1378
		Version:            Version3,
1379
		Timeout:            time.Duration(2) * time.Second,
1380
		Retries:            3,
1381
		MaxOids:            MaxOids,
1382
		SecurityModel:      UserSecurityModel,
1383
		SecurityParameters: sp,
1384
		MsgFlags:           AuthPriv,
1385
	}
1386

1387
	err := ts.Connect()
1388
	if err != nil {
1389
		t.Fatalf("Connect() err: %v", err)
1390
	}
1391
	defer ts.Conn.Close()
1392

1393
	getEngineIDRequest := SnmpPacket{
1394
		Version:            Version3,
1395
		MsgFlags:           Reportable,
1396
		SecurityModel:      UserSecurityModel,
1397
		SecurityParameters: &UsmSecurityParameters{},
1398
		ContextEngineID:    unknownEngineID,
1399
		PDUType:            GetRequest,
1400
		MsgID:              1824792385,
1401
		RequestID:          1411852680,
1402
		MsgMaxSize:         65507,
1403
	}
1404
	result, err := ts.sendOneRequest(&getEngineIDRequest, true)
1405
	require.NoError(t, err, "sendOneRequest failed")
1406
	require.Equal(t, result.SecurityParameters.(*UsmSecurityParameters).AuthoritativeEngineID, authorativeEngineID, "invalid authoritativeEngineID")
1407
	require.Equal(t, result.PDUType, Report, "invalid received PDUType")
1408
}
1409

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.