gosnmp

Форк
0
/
marshal_test.go 
2042 строки · 64.0 Кб
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 || marshal
6
// +build all marshal
7

8
package gosnmp
9

10
import (
11
	"bytes"
12
	"encoding/hex"
13
	"fmt"
14
	"io"
15
	"log"
16
	"net"
17
	"reflect"
18
	"runtime"
19
	"strconv"
20
	"strings"
21
	"testing"
22
	"time"
23

24
	"github.com/stretchr/testify/assert"
25
)
26

27
// Tests in alphabetical order of function being tested
28

29
// -- Enmarshal ----------------------------------------------------------------
30

31
// "Enmarshal" not "Marshal" - easier to select tests via a regex
32

33
type testsEnmarshalVarbindPosition struct {
34
	oid string
35

36
	/*
37
		start and finish position of bytes are calculated with application layer
38
		starting at byte 0. There are two ways to understand Wireshark dumps,
39
		switch between them:
40

41
		1) the standard decode of the full packet - easier to understand
42
		what's actually happening
43

44
		2) for counting byte positions: select "Simple Network Management
45
		Protocol" line in Wiresharks middle pane, then right click and choose
46
		"Export Packet Bytes..." (as .raw). Open the capture in wireshark, it
47
		will decode as "BER Encoded File". Click on each varbind and the
48
		"packet bytes" window will highlight the corresponding bytes, then the
49
		start and end positions can be found.
50
	*/
51

52
	/*
53
		go-bindata has changed output format. Old style is needed:
54

55
		go get -u github.com/jteeuwen/go-bindata/...
56
		git co 79847ab
57
		rm ~/go/bin/go-bindata  # belts and braces
58
		go install
59
		~/go/bin/go-bindata -uncompressed *.pcap
60
	*/
61

62
	start    int
63
	finish   int
64
	pduType  Asn1BER
65
	pduValue interface{}
66
}
67

68
type testsEnmarshalT struct {
69
	version     SnmpVersion
70
	community   string
71
	requestType PDUType
72
	requestid   uint32
73
	msgid       uint32
74
	// function and function name returning bytes from tcpdump
75
	goodBytes func() []byte
76
	funcName  string // could do this via reflection
77
	// start position of the pdu
78
	pduStart int
79
	// start position of the vbl
80
	vblStart int
81
	// finish position of pdu, vbl and message - all the same
82
	finish int
83
	// a slice of positions containing start and finish of each varbind
84
	vbPositions []testsEnmarshalVarbindPosition
85
}
86

87
var testsEnmarshal = []testsEnmarshalT{
88
	{
89
		Version2c,
90
		"public",
91
		GetRequest,
92
		1871507044,
93
		0,
94
		kyoceraRequestBytes,
95
		"kyocera_request",
96
		0x0e, // pdu start
97
		0x1d, // vbl start
98
		0xa0, // finish
99
		[]testsEnmarshalVarbindPosition{
100
			{".1.3.6.1.2.1.1.7.0", 0x20, 0x2d, Null, nil},
101
			{".1.3.6.1.2.1.2.2.1.10.1", 0x2e, 0x3d, Null, nil},
102
			{".1.3.6.1.2.1.2.2.1.5.1", 0x3e, 0x4d, Null, nil},
103
			{".1.3.6.1.2.1.1.4.0", 0x4e, 0x5b, Null, nil},
104
			{".1.3.6.1.2.1.43.5.1.1.15.1", 0x5c, 0x6c, Null, nil},
105
			{".1.3.6.1.2.1.4.21.1.1.127.0.0.1", 0x6d, 0x7f, Null, nil},
106
			{".1.3.6.1.4.1.23.2.5.1.1.1.4.2", 0x80, 0x92, Null, nil},
107
			{".1.3.6.1.2.1.1.3.0", 0x93, 0xa0, Null, nil},
108
		},
109
	},
110
	{
111
		Version1,
112
		"privatelab",
113
		SetRequest,
114
		526895288,
115
		0,
116
		portOnOutgoing1,
117
		"portOnOutgoing1",
118
		0x11, // pdu start
119
		0x1f, // vbl start
120
		0x36, // finish
121
		[]testsEnmarshalVarbindPosition{
122
			{".1.3.6.1.4.1.318.1.1.4.4.2.1.3.5", 0x21, 0x36, Integer, 1},
123
		},
124
	},
125
	{
126
		Version1,
127
		"privatelab",
128
		SetRequest,
129
		1826072803,
130
		0,
131
		portOffOutgoing1,
132
		"portOffOutgoing1",
133
		0x11, // pdu start
134
		0x1f, // vbl start
135
		0x36, // finish
136
		[]testsEnmarshalVarbindPosition{
137
			{".1.3.6.1.4.1.318.1.1.4.4.2.1.3.5", 0x21, 0x36, Integer, 2},
138
		},
139
	},
140
	// MrSpock Set stuff
141
	{
142
		Version2c,
143
		"private",
144
		SetRequest,
145
		756726019,
146
		0,
147
		setOctet1,
148
		"setOctet1",
149
		0x0e, // pdu start
150
		0x1c, // vbl start
151
		0x32, // finish
152
		[]testsEnmarshalVarbindPosition{
153
			{".1.3.6.1.4.1.2863.205.1.1.75.1.0",
154
				0x1e, 0x32, OctetString, []byte{0x80}},
155
		},
156
	},
157
	{
158
		Version2c,
159
		"private",
160
		SetRequest,
161
		1000552357,
162
		0,
163
		setOctet2,
164
		"setOctet2",
165
		0x0e, // pdu start
166
		0x1c, // vbl start
167
		0x37, // finish
168
		[]testsEnmarshalVarbindPosition{
169
			{".1.3.6.1.4.1.2863.205.1.1.75.2.0",
170
				0x1e, 0x36, OctetString, []byte("telnet")},
171
		},
172
	},
173
	// MrSpock Set stuff
174
	{
175
		Version2c,
176
		"private",
177
		SetRequest,
178
		1664317637,
179
		0,
180
		setInteger1,
181
		"setInteger1",
182
		0x0e, // pdu start
183
		0x1c, // vbl start
184
		0x7f, // finish
185
		[]testsEnmarshalVarbindPosition{
186
			{".1.3.6.1.4.1.2863.205.10.1.33.2.5.1.2.2", 0x1e, 0x36, Integer, 5001},
187
			{".1.3.6.1.4.1.2863.205.10.1.33.2.5.1.3.2", 0x37, 0x4f, Integer, 5001},
188
			{".1.3.6.1.4.1.2863.205.10.1.33.2.5.1.4.2", 0x50, 0x67, Integer, 2},
189
			{".1.3.6.1.4.1.2863.205.10.1.33.2.5.1.5.2", 0x68, 0x7f, Integer, 1},
190
		},
191
	},
192
	// Issue 35, empty responses.
193
	{
194
		Version2c,
195
		"public",
196
		GetRequest,
197
		1883298028,
198
		0,
199
		emptyErrRequest,
200
		"emptyErrRequest",
201
		0x0d, // pdu start
202
		0x1b, // vbl start
203
		0x1c, // finish
204
		[]testsEnmarshalVarbindPosition{},
205
	},
206
	// trap - TimeTicks
207
	// snmptrap different with timetick 2, integer 5
208

209
	// trap1 - capture is from frame - less work, decode easier
210
	// varbinds - because Wireshark is decoding as BER's, need to subtract 2
211
	// from start of varbinds
212
	{
213
		Version2c,
214
		"public",
215
		SNMPv2Trap,
216
		1918693186,
217
		0,
218
		trap1,
219
		"trap1",
220
		0x0e, // pdu start
221
		0x1c, // vbl start
222
		0x82, // finish
223
		[]testsEnmarshalVarbindPosition{
224
			{".1.3.6.1.2.1.1.3.0", 0x1e, 0x2f, TimeTicks, uint32(18542501)},
225
			{".1.3.6.1.6.3.1.1.4.1.0", 0x30, 0x45, ObjectIdentifier, ".1.3.6.1.2.1.1"},
226
			{".1.3.6.1.2.1.1.1.0", 0x46, 0x59, OctetString, "red laptop"},
227
			{".1.3.6.1.2.1.1.7.0", 0x5e, 0x6c, Integer, 5},
228
			{".1.3.6.1.2.1.1.2", 0x6d, 0x82, ObjectIdentifier, ".1.3.6.1.4.1.2.3.4.5"},
229
		},
230
	},
231
}
232

233
// helpers for Enmarshal tests
234

235
// vbPosPdus returns a slice of oids in the given test
236
func vbPosPdus(test testsEnmarshalT) (pdus []SnmpPDU) {
237
	for _, vbp := range test.vbPositions {
238
		pdu := SnmpPDU{Name: vbp.oid, Type: vbp.pduType, Value: vbp.pduValue}
239
		pdus = append(pdus, pdu)
240
	}
241
	return
242
}
243

244
// checkByteEquality walks the bytes in testBytes, and compares them to goodBytes
245
func checkByteEquality(t *testing.T, test testsEnmarshalT, testBytes []byte,
246
	start int, finish int) {
247

248
	testBytesLen := len(testBytes)
249

250
	goodBytes := test.goodBytes()
251
	goodBytes = goodBytes[start : finish+1]
252
	for cursor := range goodBytes {
253
		if testBytesLen < cursor {
254
			t.Errorf("%s: testBytesLen (%d) < cursor (%d)", test.funcName,
255
				testBytesLen, cursor)
256
			break
257
		}
258
		if testBytes[cursor] != goodBytes[cursor] {
259
			t.Errorf("%s: cursor %d: testBytes != goodBytes:\n%s\n%s",
260
				test.funcName,
261
				cursor,
262
				dumpBytes2("good", goodBytes, cursor),
263
				dumpBytes2("test", testBytes, cursor))
264
			break
265
		}
266
	}
267
}
268

269
// Enmarshal tests in order that should be used for troubleshooting
270
// ie check each varbind is working, then the varbind list, etc
271

272
func TestEnmarshalVarbind(t *testing.T) {
273
	Default.Logger = NewLogger(log.New(io.Discard, "", 0))
274

275
	for _, test := range testsEnmarshal {
276
		for j, test2 := range test.vbPositions {
277
			snmppdu := &SnmpPDU{Name: test2.oid, Type: test2.pduType, Value: test2.pduValue}
278
			testBytes, err := marshalVarbind(snmppdu)
279
			if err != nil {
280
				t.Errorf("#%s:%d:%s err returned: %v",
281
					test.funcName, j, test2.oid, err)
282
			}
283

284
			checkByteEquality(t, test, testBytes, test2.start, test2.finish)
285
		}
286
	}
287
}
288

289
func TestEnmarshalVBL(t *testing.T) {
290
	Default.Logger = NewLogger(log.New(io.Discard, "", 0))
291

292
	for _, test := range testsEnmarshal {
293
		x := &SnmpPacket{
294
			Community: test.community,
295
			Version:   test.version,
296
			RequestID: test.requestid,
297
			Variables: vbPosPdus(test),
298
		}
299

300
		testBytes, err := x.marshalVBL()
301
		if err != nil {
302
			t.Errorf("#%s: marshalVBL() err returned: %v", test.funcName, err)
303
		}
304

305
		checkByteEquality(t, test, testBytes, test.vblStart, test.finish)
306
	}
307
}
308

309
func TestEnmarshalPDU(t *testing.T) {
310
	Default.Logger = NewLogger(log.New(io.Discard, "", 0))
311

312
	for _, test := range testsEnmarshal {
313
		x := &SnmpPacket{
314
			Community: test.community,
315
			Version:   test.version,
316
			PDUType:   test.requestType,
317
			RequestID: test.requestid,
318
			Variables: vbPosPdus(test),
319
		}
320

321
		testBytes, err := x.marshalPDU()
322
		if err != nil {
323
			t.Errorf("#%s: marshalPDU() err returned: %v", test.funcName, err)
324
		}
325

326
		checkByteEquality(t, test, testBytes, test.pduStart, test.finish)
327
	}
328
}
329

330
func TestEnmarshalMsg(t *testing.T) {
331
	Default.Logger = NewLogger(log.New(io.Discard, "", 0))
332

333
	for _, test := range testsEnmarshal {
334
		x := &SnmpPacket{
335
			Community: test.community,
336
			Version:   test.version,
337
			PDUType:   test.requestType,
338
			RequestID: test.requestid,
339
			MsgID:     test.msgid,
340
			Variables: vbPosPdus(test),
341
		}
342

343
		testBytes, err := x.marshalMsg()
344
		if err != nil {
345
			t.Errorf("#%s: marshal() err returned: %v", test.funcName, err)
346
		}
347
		checkByteEquality(t, test, testBytes, 0, test.finish)
348
		t.Run(fmt.Sprintf("TestEnmarshalMsgUnmarshal/PDU[%v]/RequestID[%v]", test.requestType, test.requestid), func(t *testing.T) {
349
			vhandle := GoSNMP{}
350
			vhandle.Logger = Default.Logger
351
			result, err := vhandle.SnmpDecodePacket(testBytes)
352
			if err != nil {
353
				t.Errorf("#%s: SnmpDecodePacket() err returned: %v", test.funcName, err)
354
			}
355
			newResultTestBytes, err := result.marshalMsg()
356
			if err != nil {
357
				t.Errorf("#%s: marshal() err returned: %v", test.funcName, err)
358
			}
359
			if len(newResultTestBytes) == 0 {
360
				t.Errorf("#%s: marshal() length of result is 0 : %v", test.funcName, (newResultTestBytes))
361
				return
362
			}
363
			checkByteEquality(t, test, newResultTestBytes, 0, test.finish)
364
		})
365
	}
366
}
367

368
// -- Unmarshal -----------------------------------------------------------------
369

370
var testsUnmarshalErr = []struct {
371
	in func() []byte
372
}{
373
	{
374
		panicUnmarshalHeader,
375
	},
376
	{
377
		panicUnmarshalV3Header,
378
	},
379
	{
380
		panicUnmarshalUserSecurityModelPacketLen,
381
	},
382
	{
383
		panicUnmarshalV3HeaderFlagLen,
384
	},
385
	{
386
		panicUnmarshalParseFloat32,
387
	},
388
	{
389
		panicUnmarshalParseFloat64,
390
	},
391
	{
392
		panicUnmarshalParseRawFieldTimeTicks,
393
	},
394
	{
395
		panicUnmarshalDecryptPacketIndex,
396
	},
397
	{
398
		panicUnmarshalDecryptNoPriv,
399
	},
400
}
401

402
var testsUnmarshal = []struct {
403
	in  func() []byte
404
	out *SnmpPacket
405
}{
406
	{kyoceraResponseBytes,
407
		&SnmpPacket{
408
			Version:    Version2c,
409
			Community:  "public",
410
			PDUType:    GetResponse,
411
			RequestID:  1066889284,
412
			Error:      0,
413
			ErrorIndex: 0,
414
			Variables: []SnmpPDU{
415
				{
416
					Name:  ".1.3.6.1.2.1.1.7.0",
417
					Type:  Integer,
418
					Value: 104,
419
				},
420
				{
421
					Name:  ".1.3.6.1.2.1.2.2.1.10.1",
422
					Type:  Counter32,
423
					Value: 271070065,
424
				},
425
				{
426
					Name:  ".1.3.6.1.2.1.2.2.1.5.1",
427
					Type:  Gauge32,
428
					Value: 100000000,
429
				},
430
				{
431
					Name:  ".1.3.6.1.2.1.1.4.0",
432
					Type:  OctetString,
433
					Value: []byte("Administrator"),
434
				},
435
				{
436
					Name:  ".1.3.6.1.2.1.43.5.1.1.15.1",
437
					Type:  Null,
438
					Value: nil,
439
				},
440
				{
441
					Name:  ".1.3.6.1.2.1.4.21.1.1.127.0.0.1",
442
					Type:  IPAddress,
443
					Value: "127.0.0.1",
444
				},
445
				{
446
					Name:  ".1.3.6.1.4.1.23.2.5.1.1.1.4.2",
447
					Type:  OctetString,
448
					Value: []byte{0x00, 0x15, 0x99, 0x37, 0x76, 0x2b},
449
				},
450
				{
451
					Name:  ".1.3.6.1.2.1.1.3.0",
452
					Type:  TimeTicks,
453
					Value: uint32(318870100),
454
				},
455
			},
456
		},
457
	},
458
	{ciscoResponseBytes,
459
		&SnmpPacket{
460
			Version:    Version2c,
461
			Community:  "public",
462
			PDUType:    GetResponse,
463
			RequestID:  4876669,
464
			Error:      0,
465
			ErrorIndex: 0,
466
			Variables: []SnmpPDU{
467
				{
468
					Name:  ".1.3.6.1.2.1.1.7.0",
469
					Type:  Integer,
470
					Value: 78,
471
				},
472
				{
473
					Name:  ".1.3.6.1.2.1.2.2.1.2.6",
474
					Type:  OctetString,
475
					Value: []byte("GigabitEthernet0"),
476
				},
477
				{
478
					Name:  ".1.3.6.1.2.1.2.2.1.5.3",
479
					Type:  Gauge32,
480
					Value: uint(4294967295),
481
				},
482
				{
483
					Name:  ".1.3.6.1.2.1.2.2.1.7.2",
484
					Type:  NoSuchInstance,
485
					Value: nil,
486
				},
487
				{
488
					Name:  ".1.3.6.1.2.1.2.2.1.9.3",
489
					Type:  TimeTicks,
490
					Value: uint32(2970),
491
				},
492
				{
493
					Name:  ".1.3.6.1.2.1.3.1.1.2.10.1.10.11.0.17",
494
					Type:  OctetString,
495
					Value: []byte{0x00, 0x07, 0x7d, 0x4d, 0x09, 0x00},
496
				},
497
				{
498
					Name:  ".1.3.6.1.2.1.3.1.1.3.10.1.10.11.0.2",
499
					Type:  IPAddress,
500
					Value: "10.11.0.2",
501
				},
502
				{
503
					Name:  ".1.3.6.1.2.1.4.20.1.1.110.143.197.1",
504
					Type:  IPAddress,
505
					Value: "110.143.197.1",
506
				},
507
				{
508
					Name:  ".1.3.6.1.66.1",
509
					Type:  NoSuchObject,
510
					Value: nil,
511
				},
512
				{
513
					Name:  ".1.3.6.1.2.1.1.2.0",
514
					Type:  ObjectIdentifier,
515
					Value: ".1.3.6.1.4.1.9.1.1166",
516
				},
517
			},
518
		},
519
	},
520
	{portOnIncoming1,
521
		&SnmpPacket{
522
			Version:    Version1,
523
			Community:  "privatelab",
524
			PDUType:    GetResponse,
525
			RequestID:  526895288,
526
			Error:      0,
527
			ErrorIndex: 0,
528
			Variables: []SnmpPDU{
529
				{
530
					Name:  ".1.3.6.1.4.1.318.1.1.4.4.2.1.3.5",
531
					Type:  Integer,
532
					Value: 1,
533
				},
534
			},
535
		},
536
	},
537
	{portOffIncoming1,
538
		&SnmpPacket{
539
			Version:    Version1,
540
			Community:  "privatelab",
541
			PDUType:    GetResponse,
542
			RequestID:  1826072803,
543
			Error:      0,
544
			ErrorIndex: 0,
545
			Variables: []SnmpPDU{
546
				{
547
					Name:  ".1.3.6.1.4.1.318.1.1.4.4.2.1.3.5",
548
					Type:  Integer,
549
					Value: 2,
550
				},
551
			},
552
		},
553
	},
554
	{ciscoGetnextResponseBytes,
555
		&SnmpPacket{
556
			Version:    Version2c,
557
			Community:  "public",
558
			PDUType:    GetResponse,
559
			RequestID:  1528674030,
560
			Error:      0,
561
			ErrorIndex: 0,
562
			Variables: []SnmpPDU{
563
				{
564
					Name:  ".1.3.6.1.2.1.3.1.1.3.2.1.192.168.104.2",
565
					Type:  IPAddress,
566
					Value: "192.168.104.2",
567
				},
568
				{
569
					Name:  ".1.3.6.1.2.1.92.1.2.1.0",
570
					Type:  Counter32,
571
					Value: 0,
572
				},
573
				{
574
					Name:  ".1.3.6.1.2.1.1.9.1.3.3",
575
					Type:  OctetString,
576
					Value: []byte("The MIB module for managing IP and ICMP implementations"),
577
				},
578
				{
579
					Name:  ".1.3.6.1.2.1.1.9.1.4.2",
580
					Type:  TimeTicks,
581
					Value: 21,
582
				},
583
				{
584
					Name:  ".1.3.6.1.2.1.2.1.0",
585
					Type:  Integer,
586
					Value: 3,
587
				},
588
				{
589
					Name:  ".1.3.6.1.2.1.1.2.0",
590
					Type:  ObjectIdentifier,
591
					Value: ".1.3.6.1.4.1.8072.3.2.10",
592
				},
593
			},
594
		},
595
	},
596
	{ciscoGetbulkResponseBytes,
597
		&SnmpPacket{
598
			Version:        Version2c,
599
			Community:      "public",
600
			PDUType:        GetResponse,
601
			RequestID:      250000266,
602
			NonRepeaters:   0,
603
			MaxRepetitions: 10,
604
			Variables: []SnmpPDU{
605
				{
606
					Name:  ".1.3.6.1.2.1.1.9.1.4.1",
607
					Type:  TimeTicks,
608
					Value: 21,
609
				},
610
				{
611
					Name:  ".1.3.6.1.2.1.1.9.1.4.2",
612
					Type:  TimeTicks,
613
					Value: 21,
614
				},
615
				{
616
					Name:  ".1.3.6.1.2.1.1.9.1.4.3",
617
					Type:  TimeTicks,
618
					Value: 21,
619
				},
620
				{
621
					Name:  ".1.3.6.1.2.1.1.9.1.4.4",
622
					Type:  TimeTicks,
623
					Value: 21,
624
				},
625
				{
626
					Name:  ".1.3.6.1.2.1.1.9.1.4.5",
627
					Type:  TimeTicks,
628
					Value: 21,
629
				},
630
				{
631
					Name:  ".1.3.6.1.2.1.1.9.1.4.6",
632
					Type:  TimeTicks,
633
					Value: 23,
634
				},
635
				{
636
					Name:  ".1.3.6.1.2.1.1.9.1.4.7",
637
					Type:  TimeTicks,
638
					Value: 23,
639
				},
640
				{
641
					Name:  ".1.3.6.1.2.1.1.9.1.4.8",
642
					Type:  TimeTicks,
643
					Value: 23,
644
				},
645
				{
646
					Name:  ".1.3.6.1.2.1.2.1.0",
647
					Type:  Integer,
648
					Value: 3,
649
				},
650
				{
651
					Name:  ".1.3.6.1.2.1.2.2.1.1.1",
652
					Type:  Integer,
653
					Value: 1,
654
				},
655
			},
656
		},
657
	},
658
	{emptyErrResponse,
659
		&SnmpPacket{
660
			Version:   Version2c,
661
			Community: "public",
662
			PDUType:   GetResponse,
663
			RequestID: 1883298028,
664
			Error:     0,
665
			Variables: []SnmpPDU{},
666
		},
667
	},
668
	{counter64Response,
669
		&SnmpPacket{
670
			Version:    Version2c,
671
			Community:  "public",
672
			PDUType:    GetResponse,
673
			RequestID:  190378322,
674
			Error:      0,
675
			ErrorIndex: 0,
676
			Variables: []SnmpPDU{
677
				{
678
					Name:  ".1.3.6.1.2.1.31.1.1.1.10.1",
679
					Type:  Counter64,
680
					Value: uint64(1527943),
681
				},
682
			},
683
		},
684
	},
685
	{opaqueFloatResponse,
686
		&SnmpPacket{
687
			Version:    Version2c,
688
			Community:  "public",
689
			PDUType:    GetResponse,
690
			RequestID:  601216773,
691
			Error:      0,
692
			ErrorIndex: 0,
693
			Variables: []SnmpPDU{
694
				{
695
					Name:  ".1.3.6.1.4.1.6574.4.2.12.1.0",
696
					Type:  OpaqueFloat,
697
					Value: float32(10.0),
698
				},
699
			},
700
		},
701
	},
702
	{opaqueResponse,
703
		&SnmpPacket{
704
			Version:    Version1,
705
			Community:  "public",
706
			PDUType:    GetResponse,
707
			RequestID:  2033938493,
708
			Error:      0,
709
			ErrorIndex: 0,
710
			Variables: []SnmpPDU{
711
				{
712
					Name:  ".1.3.6.1.4.1.34187.74195.2.1.24590",
713
					Type:  Opaque,
714
					Value: []byte{0x41, 0xf0, 0x00, 0x00},
715
				},
716
			},
717
		},
718
	},
719
	{opaqueDoubleResponse,
720
		&SnmpPacket{
721
			Version:    Version2c,
722
			Community:  "public",
723
			PDUType:    GetResponse,
724
			RequestID:  601216773,
725
			Error:      0,
726
			ErrorIndex: 0,
727
			Variables: []SnmpPDU{
728
				{
729
					Name:  ".1.3.6.1.4.1.6574.4.2.12.1.0",
730
					Type:  OpaqueDouble,
731
					Value: float64(10.0),
732
				},
733
			},
734
		},
735
	},
736
	{snmpv3HelloRequest,
737
		&SnmpPacket{
738
			Version:    Version3,
739
			PDUType:    GetRequest,
740
			MsgID:      91040642,
741
			RequestID:  1157240545,
742
			Error:      0,
743
			ErrorIndex: 0,
744
			Variables:  []SnmpPDU{},
745
		},
746
	},
747
	{snmpv3HelloResponse,
748
		&SnmpPacket{
749
			Version:    Version3,
750
			PDUType:    Report,
751
			MsgID:      91040642,
752
			RequestID:  1157240545,
753
			Error:      0,
754
			ErrorIndex: 0,
755
			Variables: []SnmpPDU{
756
				{
757
					Name:  ".1.3.6.1.6.3.15.1.1.4.0",
758
					Type:  Counter32,
759
					Value: 21,
760
				},
761
			},
762
		},
763
	},
764
}
765

766
func TestUnmarshalErrors(t *testing.T) {
767
	Default.Logger = NewLogger(log.New(io.Discard, "", 0))
768

769
	for i, test := range testsUnmarshalErr {
770
		funcName := runtime.FuncForPC(reflect.ValueOf(test.in).Pointer()).Name()
771
		splitedFuncName := strings.Split(funcName, ".")
772
		funcName = splitedFuncName[len(splitedFuncName)-1]
773
		t.Run(fmt.Sprintf("%v-%v", i, funcName), func(t *testing.T) {
774
			vhandle := GoSNMP{}
775
			vhandle.Logger = Default.Logger
776
			testBytes := test.in()
777
			_, err := vhandle.SnmpDecodePacket(testBytes)
778
			if err == nil {
779
				t.Errorf("#%s: SnmpDecodePacket() err expected, but not returned", funcName)
780
			}
781
		})
782
	}
783
}
784

785
func FuzzUnmarshal(f *testing.F) {
786
	for _, test := range testsUnmarshalErr {
787
		f.Add(test.in())
788
	}
789

790
	for _, test := range testsUnmarshal {
791
		f.Add(test.in())
792
	}
793

794
	vhandle := GoSNMP{}
795
	vhandle.Logger = Default.Logger
796
	f.Fuzz(func(t *testing.T, data []byte) {
797
		stime := time.Now()
798
		_, _ = vhandle.SnmpDecodePacket(data)
799

800
		if e := time.Since(stime); e > (time.Second * 1) {
801
			t.Errorf("SnmpDecodePacket() took too long: %s", e)
802
		}
803
	})
804
}
805

806
func TestUnmarshal(t *testing.T) {
807
	Default.Logger = NewLogger(log.New(io.Discard, "", 0))
808

809
	for i, test := range testsUnmarshal {
810
		funcName := runtime.FuncForPC(reflect.ValueOf(test.in).Pointer()).Name()
811
		splitedFuncName := strings.Split(funcName, ".")
812
		funcName = splitedFuncName[len(splitedFuncName)-1]
813
		t.Run(fmt.Sprintf("%v-%v", i, funcName), func(t *testing.T) {
814
			vhandle := GoSNMP{}
815
			vhandle.Logger = Default.Logger
816
			testBytes := test.in()
817
			res, err := vhandle.SnmpDecodePacket(testBytes)
818
			if err != nil {
819
				t.Errorf("#%s: SnmpDecodePacket() err returned: %v", funcName, err)
820
			}
821
			t.Run("unmarshal", func(t *testing.T) {
822
				// test "header" fields
823
				if res.Version != test.out.Version {
824
					t.Errorf("#%d Version result: %v, test: %v", i, res.Version, test.out.Version)
825
				}
826
				if res.Community != test.out.Community {
827
					t.Errorf("#%d Community result: %v, test: %v", i, res.Community, test.out.Community)
828
				}
829
				if res.PDUType != test.out.PDUType {
830
					t.Errorf("#%d PDUType result: %v, test: %v", i, res.PDUType, test.out.PDUType)
831
				}
832
				if res.RequestID != test.out.RequestID {
833
					t.Errorf("#%d RequestID result: %v, test: %v", i, res.RequestID, test.out.RequestID)
834
				}
835
				if res.Error != test.out.Error {
836
					t.Errorf("#%d Error result: %v, test: %v", i, res.Error, test.out.Error)
837
				}
838
				if res.ErrorIndex != test.out.ErrorIndex {
839
					t.Errorf("#%d ErrorIndex result: %v, test: %v", i, res.ErrorIndex, test.out.ErrorIndex)
840
				}
841

842
				// test varbind values
843
				for n, vb := range test.out.Variables {
844
					if len(res.Variables) < n {
845
						t.Errorf("#%d:%d ran out of varbind results", i, n)
846
						return
847
					}
848
					vbr := res.Variables[n]
849

850
					if vbr.Name != vb.Name {
851
						t.Errorf("#%d:%d Name result: %v, test: %v", i, n, vbr.Name, vb.Name)
852
					}
853
					if vbr.Type != vb.Type {
854
						t.Errorf("#%d:%d Type result: %v, test: %v", i, n, vbr.Type, vb.Type)
855
					}
856

857
					switch vb.Type {
858
					case Integer, Gauge32, Counter32, TimeTicks, Counter64:
859
						vbval := ToBigInt(vb.Value)
860
						vbrval := ToBigInt(vbr.Value)
861
						if vbval.Cmp(vbrval) != 0 {
862
							t.Errorf("#%d:%d Value result: %v, test: %v", i, n, vbr.Value, vb.Value)
863
						}
864
					case OctetString, Opaque:
865
						if !bytes.Equal(vb.Value.([]byte), vbr.Value.([]byte)) {
866
							t.Errorf("#%d:%d Value result: %v, test: %v", i, n, vbr.Value, vb.Value)
867
						}
868
					case IPAddress, ObjectIdentifier:
869
						if vb.Value != vbr.Value {
870
							t.Errorf("#%d:%d Value result: %v, test: %v", i, n, vbr.Value, vb.Value)
871
						}
872
					case Null, NoSuchObject, NoSuchInstance:
873
						if (vb.Value != nil) || (vbr.Value != nil) {
874
							t.Errorf("#%d:%d Value result: %v, test: %v", i, n, vbr.Value, vb.Value)
875
						}
876
					case OpaqueFloat:
877
						if vb.Value.(float32) != vbr.Value.(float32) {
878
							t.Errorf("#%d:%d Value result: %v, test: %v", i, n, vbr.Value, vb.Value)
879
						}
880
					case OpaqueDouble:
881
						if vb.Value.(float64) != vbr.Value.(float64) {
882
							t.Errorf("#%d:%d Value result: %v, test: %v", i, n, vbr.Value, vb.Value)
883
						}
884
					default:
885
						t.Errorf("#%d:%d Unhandled case result: %v, test: %v", i, n, vbr.Value, vb.Value)
886
					}
887

888
				}
889
			})
890
			t.Run("remarshal", func(t *testing.T) {
891
				result, err := res.marshalMsg()
892
				if err != nil {
893
					t.Fatalf("#%s: marshalMsg() err returned: %v", funcName, err)
894
				}
895
				resNew, err := vhandle.SnmpDecodePacket(result)
896
				if err != nil {
897
					t.Fatalf("#%s: SnmpDecodePacket() err returned: %v", funcName, err)
898
				}
899
				assert.EqualValues(t, res, resNew)
900

901
			})
902
		})
903

904
	}
905
}
906

907
// -----------------------------------------------------------------------------
908

909
/*
910

911
* byte dumps generated using tcpdump and github.com/jteeuwen/go-bindata eg
912
  `sudo tcpdump -s 0 -i eth0 -w cisco.pcap host 203.50.251.17 and port 161`
913

914
* Frame, Ethernet II, IP and UDP layers removed from generated bytes
915
*/
916

917
/*
918
panicUnmarshalHeader tests a boundary condition that results in a panic
919
when unmarshalling the SNMP header (see also https://github.com/gosnmp/gosnmp/issues/440)
920
*/
921
func panicUnmarshalHeader() []byte {
922
	return []byte("0\x04\x02\x020\x03")
923
}
924

925
/*
926
panicUnmarshalV3Header tests a boundary condition that results in a panic
927
when unmarshalling the SNMPv3.
928
*/
929
func panicUnmarshalV3Header() []byte {
930
	return []byte{
931
		0x30, 0x81, 0x95, 0x02, 0x01, 0x03, 0x30, 0x30, 0x43, 0x04, 0x30, 0x30, 0x30, 0x30, 0x43, 0x03,
932
		0x30, 0x30, 0x30, 0x04, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30,
933
		0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30,
934
		0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30,
935
		0x30, 0x30, 0x30, 0x30, 0x30, 0x04, 0x51, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30,
936
		0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30,
937
		0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30,
938
		0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30,
939
		0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30,
940
		0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30,
941
	}
942
}
943

944
/*
945
panicUnmarshalUserSecurityModelPacketLen() tests a boundary condition that results in a panic
946
when indexing into the packet when processing the User Security Model.
947
*/
948
func panicUnmarshalUserSecurityModelPacketLen() []byte {
949
	return []byte{
950
		0x30, 0x81, 0x95, 0x02, 0x01, 0x03, 0x30, 0x30, 0x43, 0x04, 0x30, 0x30, 0x30, 0x30, 0x43, 0x03,
951
		0x30, 0x30, 0x30, 0x43, 0x01, 0x30, 0x43, 0x01, 0x30, 0x04, 0xfd, 0x30, 0x30, 0x30, 0x30, 0x30,
952
		0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30,
953
		0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30,
954
		0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30,
955
		0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30,
956
		0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30,
957
		0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30,
958
		0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30,
959
		0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30,
960
	}
961
}
962

963
/*
964
panicUnmarshalV3HeaderFlagLen tests a boundary condition that results in a panic
965
when indexing into Flags without checking the length.
966
*/
967
func panicUnmarshalV3HeaderFlagLen() []byte {
968
	return []byte{
969
		0x30, 0x7e, 0x02, 0x01, 0x03, 0x30, 0x30, 0x43, 0x04, 0x30, 0x30, 0x30, 0x30, 0x43, 0x03, 0x30,
970
		0x30, 0x30, 0x04, 0x00, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30,
971
		0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30,
972
		0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30,
973
		0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30,
974
		0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30,
975
		0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30,
976
		0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30,
977
	}
978
}
979

980
/*
981
panicUnmarshalParseFloat32() tests a boundary condition that results in a panic
982
in parseFloat32 when handling malformed data.
983
*/
984
func panicUnmarshalParseFloat32() []byte {
985
	return []byte{
986
		0x30, 0x34, 0x43, 0x01, 0x30, 0x43, 0x06, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0xa2, 0x27, 0x43,
987
		0x04, 0x30, 0x30, 0x30, 0x30, 0x43, 0x01, 0x30, 0x43, 0x01, 0x30, 0x30, 0x19, 0x30, 0x30, 0x06,
988
		0x0c, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x44, 0x07, 0x9f,
989
		0x78, 0x00, 0x30, 0x30, 0x30, 0x30,
990
	}
991
}
992

993
/*
994
panicUnmarshalParseFloat64() tests a boundary condition that results in a panic
995
in parseFloat64 when handling malformed data.
996
*/
997
func panicUnmarshalParseFloat64() []byte {
998
	return []byte{
999
		0x30, 0x38, 0x43, 0x01, 0x30, 0x43, 0x06, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0xa2, 0x2b, 0x43,
1000
		0x04, 0x30, 0x30, 0x30, 0x30, 0x43, 0x01, 0x30, 0x43, 0x01, 0x30, 0x30, 0x1d, 0x30, 0x30, 0x06,
1001
		0x0c, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x44, 0x0b, 0x9f,
1002
		0x79, 0x80, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30,
1003
	}
1004
}
1005

1006
/*
1007
panicUnmarshalParseRawFieldTimeTicks() tests a boundary condition that results in a panic
1008
in parseRawField TimeTicks type when parseLength overflows the length value returning a value
1009
for cursor that is higher than length.
1010
*/
1011
func panicUnmarshalParseRawFieldTimeTicks() []byte {
1012
	return []byte{
1013
		0x30, 0x81, 0xc5, 0x43, 0x01, 0x30, 0x43, 0x06, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0xa2, 0x81,
1014
		0xb7, 0x43, 0x04, 0x30, 0x30, 0x30, 0x30, 0x43, 0x01, 0x30, 0x43, 0xeb, 0x30, 0x30, 0x30, 0x30,
1015
		0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30,
1016
		0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30,
1017
		0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30,
1018
		0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30,
1019
		0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30,
1020
		0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0xff,
1021
		0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30,
1022
		0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30,
1023
		0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30,
1024
		0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30,
1025
		0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30,
1026
	}
1027
}
1028

1029
/*
1030
panicUnmarshalDecryptPacketIndex() tests a boundary condition that results in a panic
1031
in decryptPacket when handling malformed data.
1032
*/
1033
func panicUnmarshalDecryptPacketIndex() []byte {
1034
	return []byte{
1035
		0x30, 0x52, 0x02, 0x01, 0x03, 0x30, 0x30, 0x43, 0x04, 0x30, 0x30, 0x30, 0x30, 0x43, 0x03, 0x30,
1036
		0x30, 0x30, 0x43, 0x01, 0x30, 0x43, 0x01, 0x30, 0x04, 0x30, 0x30, 0x30, 0x43, 0x00, 0x43, 0x01,
1037
		0x30, 0x43, 0x01, 0x30, 0x43, 0x00, 0x43, 0x00, 0x04, 0x2a, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30,
1038
		0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30,
1039
		0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30,
1040
		0x30, 0x30, 0x30, 0x30,
1041
	}
1042
}
1043

1044
/*
1045
panicUnmarshalDecryptPacketIndex() tests a boundary condition that results in a panic
1046
in UsmSecurityParameters.decryptPacket() when handling malformed data.
1047
*/
1048
func panicUnmarshalDecryptNoPriv() []byte {
1049
	return []byte{
1050
		0x30, 0x52, 0x02, 0x01, 0x03, 0x30, 0x30, 0x43, 0x04, 0x30, 0x30, 0x30, 0x30, 0x43, 0x03, 0x30,
1051
		0x30, 0x30, 0x43, 0x01, 0x30, 0x43, 0x01, 0x30, 0x04, 0x30, 0x30, 0x30, 0x43, 0x00, 0x43, 0x01,
1052
		0x30, 0x43, 0x01, 0x30, 0x43, 0x00, 0x43, 0x00, 0x43, 0x00, 0x04, 0x30, 0x30, 0x30, 0x30, 0x30,
1053
		0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30,
1054
		0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30,
1055
		0x30, 0x30, 0x30, 0x30,
1056
	}
1057
}
1058

1059
/*
1060
kyoceraResponseBytes corresponds to the response section of this snmpget
1061

1062
Simple Network Management Protocol
1063
  version: v2c (1)
1064
  community: public
1065
  data: get-response (2)
1066
    get-response
1067
      request-id: 1066889284
1068
      error-status: noError (0)
1069
      error-index: 0
1070
      variable-bindings: 8 items
1071
        1.3.6.1.2.1.1.7.0: 104
1072
        1.3.6.1.2.1.2.2.1.10.1: 271070065
1073
        1.3.6.1.2.1.2.2.1.5.1: 100000000
1074
        1.3.6.1.2.1.1.4.0: 41646d696e6973747261746f72
1075
        1.3.6.1.2.1.43.5.1.1.15.1: Value (Null)
1076
        1.3.6.1.2.1.4.21.1.1.127.0.0.1: 127.0.0.1 (127.0.0.1)
1077
        1.3.6.1.4.1.23.2.5.1.1.1.4.2: 00159937762b
1078
        1.3.6.1.2.1.1.3.0: 318870100
1079
*/
1080

1081
func kyoceraResponseBytes() []byte {
1082
	return []byte{
1083
		0x30, 0x81, 0xc2, 0x02, 0x01, 0x01, 0x04, 0x06, 0x70, 0x75, 0x62, 0x6c,
1084
		0x69, 0x63, 0xa2, 0x81, 0xb4, 0x02, 0x04, 0x3f, 0x97, 0x70, 0x44, 0x02,
1085
		0x01, 0x00, 0x02, 0x01, 0x00, 0x30, 0x81, 0xa5, 0x30, 0x0d, 0x06, 0x08,
1086
		0x2b, 0x06, 0x01, 0x02, 0x01, 0x01, 0x07, 0x00, 0x02, 0x01, 0x68, 0x30,
1087
		0x12, 0x06, 0x0a, 0x2b, 0x06, 0x01, 0x02, 0x01, 0x02, 0x02, 0x01, 0x0a,
1088
		0x01, 0x41, 0x04, 0x10, 0x28, 0x33, 0x71, 0x30, 0x12, 0x06, 0x0a, 0x2b,
1089
		0x06, 0x01, 0x02, 0x01, 0x02, 0x02, 0x01, 0x05, 0x01, 0x42, 0x04, 0x05,
1090
		0xf5, 0xe1, 0x00, 0x30, 0x19, 0x06, 0x08, 0x2b, 0x06, 0x01, 0x02, 0x01,
1091
		0x01, 0x04, 0x00, 0x04, 0x0d, 0x41, 0x64, 0x6d, 0x69, 0x6e, 0x69, 0x73,
1092
		0x74, 0x72, 0x61, 0x74, 0x6f, 0x72, 0x30, 0x0f, 0x06, 0x0b, 0x2b, 0x06,
1093
		0x01, 0x02, 0x01, 0x2b, 0x05, 0x01, 0x01, 0x0f, 0x01, 0x05, 0x00, 0x30,
1094
		0x15, 0x06, 0x0d, 0x2b, 0x06, 0x01, 0x02, 0x01, 0x04, 0x15, 0x01, 0x01,
1095
		0x7f, 0x00, 0x00, 0x01, 0x40, 0x04, 0x7f, 0x00, 0x00, 0x01, 0x30, 0x17,
1096
		0x06, 0x0d, 0x2b, 0x06, 0x01, 0x04, 0x01, 0x17, 0x02, 0x05, 0x01, 0x01,
1097
		0x01, 0x04, 0x02, 0x04, 0x06, 0x00, 0x15, 0x99, 0x37, 0x76, 0x2b, 0x30,
1098
		0x10, 0x06, 0x08, 0x2b, 0x06, 0x01, 0x02, 0x01, 0x01, 0x03, 0x00, 0x43,
1099
		0x04, 0x13, 0x01, 0x92, 0x54,
1100
	}
1101
}
1102

1103
/*
1104
ciscoResponseBytes corresponds to the response section of this snmpget:
1105

1106
% snmpget -On -v2c -c public 203.50.251.17 1.3.6.1.2.1.1.7.0 1.3.6.1.2.1.2.2.1.2.6 1.3.6.1.2.1.2.2.1.5.3 1.3.6.1.2.1.2.2.1.7.2 1.3.6.1.2.1.2.2.1.9.3 1.3.6.1.2.1.3.1.1.2.10.1.10.11.0.17 1.3.6.1.2.1.3.1.1.3.10.1.10.11.0.2 1.3.6.1.2.1.4.20.1.1.110.143.197.1 1.3.6.1.66.1 1.3.6.1.2.1.1.2.0
1107
.1.3.6.1.2.1.1.7.0 = INTEGER: 78
1108
.1.3.6.1.2.1.2.2.1.2.6 = STRING: GigabitEthernet0
1109
.1.3.6.1.2.1.2.2.1.5.3 = Gauge32: 4294967295
1110
.1.3.6.1.2.1.2.2.1.7.2 = No Such Instance currently exists at this OID
1111
.1.3.6.1.2.1.2.2.1.9.3 = Timeticks: (2970) 0:00:29.70
1112
.1.3.6.1.2.1.3.1.1.2.10.1.10.11.0.17 = Hex-STRING: 00 07 7D 4D 09 00
1113
.1.3.6.1.2.1.3.1.1.3.10.1.10.11.0.2 = Network Address: 0A:0B:00:02
1114
.1.3.6.1.2.1.4.20.1.1.110.143.197.1 = IPAddress: 110.143.197.1
1115
.1.3.6.1.66.1 = No Such Object available on this agent at this OID
1116
.1.3.6.1.2.1.1.2.0 = OID: .1.3.6.1.4.1.9.1.1166
1117
*/
1118

1119
func ciscoResponseBytes() []byte {
1120
	return []byte{
1121
		0x30, 0x81,
1122
		0xf1, 0x02, 0x01, 0x01, 0x04, 0x06, 0x70, 0x75, 0x62, 0x6c, 0x69, 0x63,
1123
		0xa2, 0x81, 0xe3, 0x02, 0x03, 0x4a, 0x69, 0x7d, 0x02, 0x01, 0x00, 0x02,
1124
		0x01, 0x00, 0x30, 0x81, 0xd5, 0x30, 0x0d, 0x06, 0x08, 0x2b, 0x06, 0x01,
1125
		0x02, 0x01, 0x01, 0x07, 0x00, 0x02, 0x01, 0x4e, 0x30, 0x1e, 0x06, 0x0a,
1126
		0x2b, 0x06, 0x01, 0x02, 0x01, 0x02, 0x02, 0x01, 0x02, 0x06, 0x04, 0x10,
1127
		0x47, 0x69, 0x67, 0x61, 0x62, 0x69, 0x74, 0x45, 0x74, 0x68, 0x65, 0x72,
1128
		0x6e, 0x65, 0x74, 0x30, 0x30, 0x13, 0x06, 0x0a, 0x2b, 0x06, 0x01, 0x02,
1129
		0x01, 0x02, 0x02, 0x01, 0x05, 0x03, 0x42, 0x05, 0x00, 0xff, 0xff, 0xff,
1130
		0xff, 0x30, 0x0e, 0x06, 0x0a, 0x2b, 0x06, 0x01, 0x02, 0x01, 0x02, 0x02,
1131
		0x01, 0x07, 0x02, 0x81, 0x00, 0x30, 0x10, 0x06, 0x0a, 0x2b, 0x06, 0x01,
1132
		0x02, 0x01, 0x02, 0x02, 0x01, 0x09, 0x03, 0x43, 0x02, 0x0b, 0x9a, 0x30,
1133
		0x19, 0x06, 0x0f, 0x2b, 0x06, 0x01, 0x02, 0x01, 0x03, 0x01, 0x01, 0x02,
1134
		0x0a, 0x01, 0x0a, 0x0b, 0x00, 0x11, 0x04, 0x06, 0x00, 0x07, 0x7d, 0x4d,
1135
		0x09, 0x00, 0x30, 0x17, 0x06, 0x0f, 0x2b, 0x06, 0x01, 0x02, 0x01, 0x03,
1136
		0x01, 0x01, 0x03, 0x0a, 0x01, 0x0a, 0x0b, 0x00, 0x02, 0x40, 0x04, 0x0a,
1137
		0x0b, 0x00, 0x02, 0x30, 0x17, 0x06, 0x0f, 0x2b, 0x06, 0x01, 0x02, 0x01,
1138
		0x04, 0x14, 0x01, 0x01, 0x6e, 0x81, 0x0f, 0x81, 0x45, 0x01, 0x40, 0x04,
1139
		0x6e, 0x8f, 0xc5, 0x01, 0x30, 0x09, 0x06, 0x05, 0x2b, 0x06, 0x01, 0x42,
1140
		0x01, 0x80, 0x00, 0x30, 0x15, 0x06, 0x08, 0x2b, 0x06, 0x01, 0x02, 0x01,
1141
		0x01, 0x02, 0x00, 0x06, 0x09, 0x2b, 0x06, 0x01, 0x04, 0x01, 0x09, 0x01,
1142
		0x89, 0x0e,
1143
	}
1144
}
1145

1146
/*
1147
kyoceraRequestBytes corresponds to the request section of this snmpget:
1148

1149
snmpget -On -v2c -c public 192.168.1.10 1.3.6.1.2.1.1.7.0 1.3.6.1.2.1.2.2.1.10.1 1.3.6.1.2.1.2.2.1.5.1 1.3.6.1.2.1.1.4.0 1.3.6.1.2.1.43.5.1.1.15.1 1.3.6.1.2.1.4.21.1.1.127.0.0.1 1.3.6.1.4.1.23.2.5.1.1.1.4.2 1.3.6.1.2.1.1.3.0
1150
.1.3.6.1.2.1.1.7.0 = INTEGER: 104
1151
.1.3.6.1.2.1.2.2.1.10.1 = Counter32: 144058856
1152
.1.3.6.1.2.1.2.2.1.5.1 = Gauge32: 100000000
1153
.1.3.6.1.2.1.1.4.0 = STRING: "Administrator"
1154
.1.3.6.1.2.1.43.5.1.1.15.1 = NULL
1155
.1.3.6.1.2.1.4.21.1.1.127.0.0.1 = IPAddress: 127.0.0.1
1156
.1.3.6.1.4.1.23.2.5.1.1.1.4.2 = Hex-STRING: 00 15 99 37 76 2B
1157
.1.3.6.1.2.1.1.3.0 = Timeticks: (120394900) 13 days, 22:25:49.00
1158
*/
1159

1160
func kyoceraRequestBytes() []byte {
1161
	return []byte{
1162
		0x30, 0x81,
1163
		0x9e, 0x02, 0x01, 0x01, 0x04, 0x06, 0x70, 0x75, 0x62, 0x6c, 0x69, 0x63,
1164
		0xa0, 0x81, 0x90, 0x02, 0x04, 0x6f, 0x8c, 0xee, 0x64, 0x02, 0x01, 0x00,
1165
		0x02, 0x01, 0x00, 0x30, 0x81, 0x81, 0x30, 0x0c, 0x06, 0x08, 0x2b, 0x06,
1166
		0x01, 0x02, 0x01, 0x01, 0x07, 0x00, 0x05, 0x00, 0x30, 0x0e, 0x06, 0x0a,
1167
		0x2b, 0x06, 0x01, 0x02, 0x01, 0x02, 0x02, 0x01, 0x0a, 0x01, 0x05, 0x00,
1168
		0x30, 0x0e, 0x06, 0x0a, 0x2b, 0x06, 0x01, 0x02, 0x01, 0x02, 0x02, 0x01,
1169
		0x05, 0x01, 0x05, 0x00, 0x30, 0x0c, 0x06, 0x08, 0x2b, 0x06, 0x01, 0x02,
1170
		0x01, 0x01, 0x04, 0x00, 0x05, 0x00, 0x30, 0x0f, 0x06, 0x0b, 0x2b, 0x06,
1171
		0x01, 0x02, 0x01, 0x2b, 0x05, 0x01, 0x01, 0x0f, 0x01, 0x05, 0x00, 0x30,
1172
		0x11, 0x06, 0x0d, 0x2b, 0x06, 0x01, 0x02, 0x01, 0x04, 0x15, 0x01, 0x01,
1173
		0x7f, 0x00, 0x00, 0x01, 0x05, 0x00, 0x30, 0x11, 0x06, 0x0d, 0x2b, 0x06,
1174
		0x01, 0x04, 0x01, 0x17, 0x02, 0x05, 0x01, 0x01, 0x01, 0x04, 0x02, 0x05,
1175
		0x00, 0x30, 0x0c, 0x06, 0x08, 0x2b, 0x06, 0x01, 0x02, 0x01, 0x01, 0x03,
1176
		0x00, 0x05, 0x00,
1177
	}
1178
}
1179

1180
// === snmpset dumps ===
1181

1182
/*
1183
port_on_*1() correspond to this snmpset and response:
1184

1185
snmpset -v 1 -c privatelab 192.168.100.124 .1.3.6.1.4.1.318.1.1.4.4.2.1.3.5 i 1
1186

1187
Simple Network Management Protocol
1188
  version: version-1 (0)
1189
  community: privatelab
1190
  data: set-request (3)
1191
    set-request
1192
      request-id: 526895288
1193
      error-status: noError (0)
1194
      error-index: 0
1195
      variable-bindings: 1 item
1196
        1.3.6.1.4.1.318.1.1.4.4.2.1.3.5:
1197
          Object Name: 1.3.6.1.4.1.318.1.1.4.4.2.1.3.5 (iso.3.6.1.4.1.318.1.1.4.4.2.1.3.5)
1198
          Value (Integer32): 1
1199

1200
Simple Network Management Protocol
1201
  version: version-1 (0)
1202
  community: privatelab
1203
  data: get-response (2)
1204
    get-response
1205
      request-id: 526895288
1206
      error-status: noError (0)
1207
      error-index: 0
1208
      variable-bindings: 1 item
1209
        1.3.6.1.4.1.318.1.1.4.4.2.1.3.5:
1210
          Object Name: 1.3.6.1.4.1.318.1.1.4.4.2.1.3.5 (iso.3.6.1.4.1.318.1.1.4.4.2.1.3.5)
1211
          Value (Integer32): 1
1212
*/
1213

1214
func portOnOutgoing1() []byte {
1215
	return []byte{
1216
		0x30, 0x35, 0x02, 0x01, 0x00, 0x04, 0x0a, 0x70, 0x72, 0x69, 0x76, 0x61,
1217
		0x74, 0x65, 0x6c, 0x61, 0x62, 0xa3, 0x24, 0x02, 0x04, 0x1f, 0x67, 0xc8,
1218
		0xb8, 0x02, 0x01, 0x00, 0x02, 0x01, 0x00, 0x30, 0x16, 0x30, 0x14, 0x06,
1219
		0x0f, 0x2b, 0x06, 0x01, 0x04, 0x01, 0x82, 0x3e, 0x01, 0x01, 0x04, 0x04,
1220
		0x02, 0x01, 0x03, 0x05, 0x02, 0x01, 0x01,
1221
	}
1222
}
1223

1224
func portOnIncoming1() []byte {
1225
	return []byte{
1226
		0x30, 0x82, 0x00, 0x35, 0x02, 0x01, 0x00, 0x04, 0x0a, 0x70, 0x72, 0x69,
1227
		0x76, 0x61, 0x74, 0x65, 0x6c, 0x61, 0x62, 0xa2, 0x24, 0x02, 0x04, 0x1f,
1228
		0x67, 0xc8, 0xb8, 0x02, 0x01, 0x00, 0x02, 0x01, 0x00, 0x30, 0x16, 0x30,
1229
		0x14, 0x06, 0x0f, 0x2b, 0x06, 0x01, 0x04, 0x01, 0x82, 0x3e, 0x01, 0x01,
1230
		0x04, 0x04, 0x02, 0x01, 0x03, 0x05, 0x02, 0x01, 0x01,
1231
	}
1232
}
1233

1234
/*
1235
port_off_*1() correspond to this snmpset and response:
1236

1237
snmpset -v 1 -c privatelab 192.168.100.124 .1.3.6.1.4.1.318.1.1.4.4.2.1.3.5 i 2
1238

1239
Simple Network Management Protocol
1240
  version: version-1 (0)
1241
  community: privatelab
1242
  data: set-request (3)
1243
    set-request
1244
      request-id: 1826072803
1245
      error-status: noError (0)
1246
      error-index: 0
1247
      variable-bindings: 1 item
1248
        1.3.6.1.4.1.318.1.1.4.4.2.1.3.5:
1249
          Object Name: 1.3.6.1.4.1.318.1.1.4.4.2.1.3.5 (iso.3.6.1.4.1.318.1.1.4.4.2.1.3.5)
1250
          Value (Integer32): 2
1251

1252
Simple Network Management Protocol
1253
  version: version-1 (0)
1254
  community: privatelab
1255
  data: get-response (2)
1256
    get-response
1257
      request-id: 1826072803
1258
      error-status: noError (0)
1259
      error-index: 0
1260
      variable-bindings: 1 item
1261
        1.3.6.1.4.1.318.1.1.4.4.2.1.3.5:
1262
          Object Name: 1.3.6.1.4.1.318.1.1.4.4.2.1.3.5 (iso.3.6.1.4.1.318.1.1.4.4.2.1.3.5)
1263
          Value (Integer32): 2
1264
*/
1265

1266
func portOffOutgoing1() []byte {
1267
	return []byte{
1268
		0x30, 0x35, 0x02, 0x01, 0x00, 0x04, 0x0a, 0x70, 0x72, 0x69, 0x76, 0x61,
1269
		0x74, 0x65, 0x6c, 0x61, 0x62, 0xa3, 0x24, 0x02, 0x04, 0x6c, 0xd7, 0xa8,
1270
		0xe3, 0x02, 0x01, 0x00, 0x02, 0x01, 0x00, 0x30, 0x16, 0x30, 0x14, 0x06,
1271
		0x0f, 0x2b, 0x06, 0x01, 0x04, 0x01, 0x82, 0x3e, 0x01, 0x01, 0x04, 0x04,
1272
		0x02, 0x01, 0x03, 0x05, 0x02, 0x01, 0x02,
1273
	}
1274
}
1275

1276
func portOffIncoming1() []byte {
1277
	return []byte{
1278
		0x30, 0x82, 0x00, 0x35, 0x02, 0x01, 0x00, 0x04, 0x0a, 0x70, 0x72, 0x69,
1279
		0x76, 0x61, 0x74, 0x65, 0x6c, 0x61, 0x62, 0xa2, 0x24, 0x02, 0x04, 0x6c,
1280
		0xd7, 0xa8, 0xe3, 0x02, 0x01, 0x00, 0x02, 0x01, 0x00, 0x30, 0x16, 0x30,
1281
		0x14, 0x06, 0x0f, 0x2b, 0x06, 0x01, 0x04, 0x01, 0x82, 0x3e, 0x01, 0x01,
1282
		0x04, 0x04, 0x02, 0x01, 0x03, 0x05, 0x02, 0x01, 0x02,
1283
	}
1284
}
1285

1286
// MrSpock START
1287

1288
/*
1289
setOctet1:
1290
Simple Network Management Protocol
1291
  version: v2c (1)
1292
  community: private
1293
  data: set-request (3)
1294
    set-request
1295
      request-id: 756726019
1296
      error-status: noError (0)
1297
      error-index: 0
1298
      variable-bindings: 1 item
1299
        1.3.6.1.4.1.2863.205.1.1.75.1.0: 80
1300
          Object Name: 1.3.6.1.4.1.2863.205.1.1.75.1.0 (iso.3.6.1.4.1.2863.205.1.1.75.1.0)
1301
          Value (OctetString): 80
1302

1303
setOctet2:
1304
Simple Network Management Protocol
1305
    version: v2c (1)
1306
    community: private
1307
    data: set-request (3)
1308
        set-request
1309
            request-id: 1000552357
1310
            error-status: noError (0)
1311
            error-index: 0
1312
            variable-bindings: 1 item
1313
                1.3.6.1.4.1.2863.205.1.1.75.2.0: 74656c6e6574
1314
                    Object Name: 1.3.6.1.4.1.2863.205.1.1.75.2.0 (iso.3.6.1.4.1.2863.205.1.1.75.2.0)
1315
                    Value (OctetString): 74656c6e6574 ("telnet")
1316
*/
1317

1318
func setOctet1() []byte {
1319
	return []byte{
1320
		0x30, 0x31, 0x02, 0x01, 0x01, 0x04, 0x07, 0x70, 0x72, 0x69, 0x76, 0x61,
1321
		0x74, 0x65, 0xa3, 0x23, 0x02, 0x04, 0x2d, 0x1a, 0xb9, 0x03, 0x02, 0x01,
1322
		0x00, 0x02, 0x01, 0x00, 0x30, 0x15, 0x30, 0x13, 0x06, 0x0e, 0x2b, 0x06,
1323
		0x01, 0x04, 0x01, 0x96, 0x2f, 0x81, 0x4d, 0x01, 0x01, 0x4b, 0x01, 0x00,
1324
		0x04, 0x01, 0x80,
1325
	}
1326
}
1327

1328
func setOctet2() []byte {
1329
	return []byte{
1330
		0x30, 0x36, 0x02, 0x01, 0x01, 0x04, 0x07, 0x70, 0x72, 0x69, 0x76, 0x61,
1331
		0x74, 0x65, 0xa3, 0x28, 0x02, 0x04, 0x3b, 0xa3, 0x37, 0xa5, 0x02, 0x01,
1332
		0x00, 0x02, 0x01, 0x00, 0x30, 0x1a, 0x30, 0x18, 0x06, 0x0e, 0x2b, 0x06,
1333
		0x01, 0x04, 0x01, 0x96, 0x2f, 0x81, 0x4d, 0x01, 0x01, 0x4b, 0x02, 0x00,
1334
		0x04, 0x06, 0x74, 0x65, 0x6c, 0x6e, 0x65, 0x74,
1335
	}
1336
}
1337

1338
/* setInteger1:
1339
snmpset -c private -v2c 10.80.0.14 \
1340
	.1.3.6.1.4.1.2863.205.10.1.33.2.5.1.2.2 i 5001 \
1341
	.1.3.6.1.4.1.2863.205.10.1.33.2.5.1.3.2 i 5001 \
1342
	.1.3.6.1.4.1.2863.205.10.1.33.2.5.1.4.2 i 2 \
1343
	.1.3.6.1.4.1.2863.205.10.1.33.2.5.1.5.2 i 1
1344

1345
Simple Network Management Protocol
1346
 version: v2c (1)
1347
 community: private
1348
 data: set-request (3)
1349
  set-request
1350
   request-id: 1664317637
1351
   error-status: noError (0)
1352
   error-index: 0
1353
   variable-bindings: 4 items
1354
    1.3.6.1.4.1.2863.205.10.1.33.2.5.1.2.2:
1355
     Object Name: 1.3.6.1.4.1.2863.205.10.1.33.2.5.1.2.2 (iso.3.6.1.4.1.2863.205.10.1.33.2.5.1.2.2)
1356
     Value (Integer32): 5001
1357
    1.3.6.1.4.1.2863.205.10.1.33.2.5.1.3.2:
1358
     Object Name: 1.3.6.1.4.1.2863.205.10.1.33.2.5.1.3.2 (iso.3.6.1.4.1.2863.205.10.1.33.2.5.1.3.2)
1359
     Value (Integer32): 5001
1360
    1.3.6.1.4.1.2863.205.10.1.33.2.5.1.4.2:
1361
     Object Name: 1.3.6.1.4.1.2863.205.10.1.33.2.5.1.4.2 (iso.3.6.1.4.1.2863.205.10.1.33.2.5.1.4.2)
1362
     Value (Integer32): 2
1363
    1.3.6.1.4.1.2863.205.10.1.33.2.5.1.5.2:
1364
     Object Name: 1.3.6.1.4.1.2863.205.10.1.33.2.5.1.5.2 (iso.3.6.1.4.1.2863.205.10.1.33.2.5.1.5.2)
1365
     Value (Integer32): 1
1366
*/
1367

1368
func setInteger1() []byte {
1369
	return []byte{
1370
		0x30, 0x7e, 0x02, 0x01, 0x01, 0x04, 0x07, 0x70, 0x72, 0x69, 0x76, 0x61,
1371
		0x74, 0x65, 0xa3, 0x70, 0x02, 0x04, 0x63, 0x33, 0x78, 0xc5, 0x02, 0x01,
1372
		0x00, 0x02, 0x01, 0x00, 0x30, 0x62, 0x30, 0x17, 0x06, 0x11, 0x2b, 0x06,
1373
		0x01, 0x04, 0x01, 0x96, 0x2f, 0x81, 0x4d, 0x0a, 0x01, 0x21, 0x02, 0x05,
1374
		0x01, 0x02, 0x02, 0x02, 0x02, 0x13, 0x89, 0x30, 0x17, 0x06, 0x11, 0x2b,
1375
		0x06, 0x01, 0x04, 0x01, 0x96, 0x2f, 0x81, 0x4d, 0x0a, 0x01, 0x21, 0x02,
1376
		0x05, 0x01, 0x03, 0x02, 0x02, 0x02, 0x13, 0x89, 0x30, 0x16, 0x06, 0x11,
1377
		0x2b, 0x06, 0x01, 0x04, 0x01, 0x96, 0x2f, 0x81, 0x4d, 0x0a, 0x01, 0x21,
1378
		0x02, 0x05, 0x01, 0x04, 0x02, 0x02, 0x01, 0x02, 0x30, 0x16, 0x06, 0x11,
1379
		0x2b, 0x06, 0x01, 0x04, 0x01, 0x96, 0x2f, 0x81, 0x4d, 0x0a, 0x01, 0x21,
1380
		0x02, 0x05, 0x01, 0x05, 0x02, 0x02, 0x01, 0x01,
1381
	}
1382
}
1383

1384
// MrSpock FINISH
1385

1386
func ciscoGetnextResponseBytes() []byte {
1387
	return []byte{
1388
		0x30, 0x81,
1389
		0xc8, 0x02, 0x01, 0x01, 0x04, 0x06, 0x70, 0x75, 0x62, 0x6c, 0x69, 0x63,
1390
		0xa2, 0x81, 0xba, 0x02, 0x04, 0x5b, 0x1d, 0xb6, 0xee, 0x02, 0x01, 0x00,
1391
		0x02, 0x01, 0x00, 0x30, 0x81, 0xab, 0x30, 0x19, 0x06, 0x11, 0x2b, 0x06,
1392
		0x01, 0x02, 0x01, 0x03, 0x01, 0x01, 0x03, 0x02, 0x01, 0x81, 0x40, 0x81,
1393
		0x28, 0x68, 0x02, 0x40, 0x04, 0xc0, 0xa8, 0x68, 0x02, 0x30, 0x0f, 0x06,
1394
		0x0a, 0x2b, 0x06, 0x01, 0x02, 0x01, 0x5c, 0x01, 0x02, 0x01, 0x00, 0x41,
1395
		0x01, 0x00, 0x30, 0x45, 0x06, 0x0a, 0x2b, 0x06, 0x01, 0x02, 0x01, 0x01,
1396
		0x09, 0x01, 0x03, 0x03, 0x04, 0x37, 0x54, 0x68, 0x65, 0x20, 0x4d, 0x49,
1397
		0x42, 0x20, 0x6d, 0x6f, 0x64, 0x75, 0x6c, 0x65, 0x20, 0x66, 0x6f, 0x72,
1398
		0x20, 0x6d, 0x61, 0x6e, 0x61, 0x67, 0x69, 0x6e, 0x67, 0x20, 0x49, 0x50,
1399
		0x20, 0x61, 0x6e, 0x64, 0x20, 0x49, 0x43, 0x4d, 0x50, 0x20, 0x69, 0x6d,
1400
		0x70, 0x6c, 0x65, 0x6d, 0x65, 0x6e, 0x74, 0x61, 0x74, 0x69, 0x6f, 0x6e,
1401
		0x73, 0x30, 0x0f, 0x06, 0x0a, 0x2b, 0x06, 0x01, 0x02, 0x01, 0x01, 0x09,
1402
		0x01, 0x04, 0x02, 0x43, 0x01, 0x15, 0x30, 0x0d, 0x06, 0x08, 0x2b, 0x06,
1403
		0x01, 0x02, 0x01, 0x02, 0x01, 0x00, 0x02, 0x01, 0x03, 0x30, 0x16, 0x06,
1404
		0x08, 0x2b, 0x06, 0x01, 0x02, 0x01, 0x01, 0x02, 0x00, 0x06, 0x0a, 0x2b,
1405
		0x06, 0x01, 0x04, 0x01, 0xbf, 0x08, 0x03, 0x02, 0x0a,
1406
	}
1407
}
1408

1409
func ciscoGetnextRequestBytes() []byte {
1410
	return []byte{
1411
		0x30, 0x7e,
1412
		0x02, 0x01, 0x01, 0x04, 0x06, 0x70, 0x75, 0x62, 0x6c, 0x69, 0x63, 0xa1,
1413
		0x71, 0x02, 0x04, 0x5b, 0x1d, 0xb6, 0xee, 0x02, 0x01, 0x00, 0x02, 0x01,
1414
		0x00, 0x30, 0x63, 0x30, 0x15, 0x06, 0x11, 0x2b, 0x06, 0x01, 0x02, 0x01,
1415
		0x03, 0x01, 0x01, 0x03, 0x02, 0x01, 0x81, 0x40, 0x81, 0x28, 0x68, 0x01,
1416
		0x05, 0x00, 0x30, 0x0c, 0x06, 0x08, 0x2b, 0x06, 0x01, 0x02, 0x01, 0x5c,
1417
		0x01, 0x02, 0x05, 0x00, 0x30, 0x0e, 0x06, 0x0a, 0x2b, 0x06, 0x01, 0x02,
1418
		0x01, 0x01, 0x09, 0x01, 0x03, 0x02, 0x05, 0x00, 0x30, 0x0e, 0x06, 0x0a,
1419
		0x2b, 0x06, 0x01, 0x02, 0x01, 0x01, 0x09, 0x01, 0x04, 0x01, 0x05, 0x00,
1420
		0x30, 0x0e, 0x06, 0x0a, 0x2b, 0x06, 0x01, 0x02, 0x01, 0x01, 0x09, 0x01,
1421
		0x04, 0x08, 0x05, 0x00, 0x30, 0x0c, 0x06, 0x08, 0x2b, 0x06, 0x01, 0x02,
1422
		0x01, 0x01, 0x01, 0x00, 0x05, 0x00,
1423
	}
1424
}
1425

1426
/*
1427
	cisco getbulk bytes corresponds to this snmpbulkget command:
1428

1429
$ snmpbulkget -v2c -cpublic  127.0.0.1:161 1.3.6.1.2.1.1.9.1.3.52
1430
iso.3.6.1.2.1.1.9.1.4.1 = Timeticks: (21) 0:00:00.21
1431
iso.3.6.1.2.1.1.9.1.4.2 = Timeticks: (21) 0:00:00.21
1432
iso.3.6.1.2.1.1.9.1.4.3 = Timeticks: (21) 0:00:00.21
1433
iso.3.6.1.2.1.1.9.1.4.4 = Timeticks: (21) 0:00:00.21
1434
iso.3.6.1.2.1.1.9.1.4.5 = Timeticks: (21) 0:00:00.21
1435
iso.3.6.1.2.1.1.9.1.4.6 = Timeticks: (23) 0:00:00.23
1436
iso.3.6.1.2.1.1.9.1.4.7 = Timeticks: (23) 0:00:00.23
1437
iso.3.6.1.2.1.1.9.1.4.8 = Timeticks: (23) 0:00:00.23
1438
iso.3.6.1.2.1.2.1.0 = INTEGER: 3
1439
iso.3.6.1.2.1.2.2.1.1.1 = INTEGER: 1
1440
*/
1441
func ciscoGetbulkRequestBytes() []byte {
1442
	return []byte{
1443
		0x30, 0x2b,
1444
		0x02, 0x01, 0x01, 0x04, 0x06, 0x70, 0x75, 0x62, 0x6c, 0x69, 0x63, 0xa5,
1445
		0x1e, 0x02, 0x04, 0x7d, 0x89, 0x68, 0xda, 0x02, 0x01, 0x00, 0x02, 0x01,
1446
		0x0a, 0x30, 0x10, 0x30, 0x0e, 0x06, 0x0a, 0x2b, 0x06, 0x01, 0x02, 0x01,
1447
		0x01, 0x09, 0x01, 0x03, 0x34, 0x05, 0x00, 0x00,
1448
	}
1449
}
1450

1451
func ciscoGetbulkResponseBytes() []byte {
1452
	return []byte{
1453
		0x30, 0x81,
1454
		0xc5, 0x02, 0x01, 0x01, 0x04, 0x06, 0x70, 0x75, 0x62, 0x6c, 0x69, 0x63,
1455
		0xa2, 0x81, 0xb7, 0x02, 0x04, 0x0e, 0xe6, 0xb3, 0x8a, 0x02, 0x01, 0x00,
1456
		0x02, 0x01, 0x00, 0x30, 0x81, 0xa8, 0x30, 0x0f, 0x06, 0x0a, 0x2b, 0x06,
1457
		0x01, 0x02, 0x01, 0x01, 0x09, 0x01, 0x04, 0x01, 0x43, 0x01, 0x15, 0x30,
1458
		0x0f, 0x06, 0x0a, 0x2b, 0x06, 0x01, 0x02, 0x01, 0x01, 0x09, 0x01, 0x04,
1459
		0x02, 0x43, 0x01, 0x15, 0x30, 0x0f, 0x06, 0x0a, 0x2b, 0x06, 0x01, 0x02,
1460
		0x01, 0x01, 0x09, 0x01, 0x04, 0x03, 0x43, 0x01, 0x15, 0x30, 0x0f, 0x06,
1461
		0x0a, 0x2b, 0x06, 0x01, 0x02, 0x01, 0x01, 0x09, 0x01, 0x04, 0x04, 0x43,
1462
		0x01, 0x15, 0x30, 0x0f, 0x06, 0x0a, 0x2b, 0x06, 0x01, 0x02, 0x01, 0x01,
1463
		0x09, 0x01, 0x04, 0x05, 0x43, 0x01, 0x15, 0x30, 0x0f, 0x06, 0x0a, 0x2b,
1464
		0x06, 0x01, 0x02, 0x01, 0x01, 0x09, 0x01, 0x04, 0x06, 0x43, 0x01, 0x17,
1465
		0x30, 0x0f, 0x06, 0x0a, 0x2b, 0x06, 0x01, 0x02, 0x01, 0x01, 0x09, 0x01,
1466
		0x04, 0x07, 0x43, 0x01, 0x17, 0x30, 0x0f, 0x06, 0x0a, 0x2b, 0x06, 0x01,
1467
		0x02, 0x01, 0x01, 0x09, 0x01, 0x04, 0x08, 0x43, 0x01, 0x17, 0x30, 0x0d,
1468
		0x06, 0x08, 0x2b, 0x06, 0x01, 0x02, 0x01, 0x02, 0x01, 0x00, 0x02, 0x01,
1469
		0x03, 0x30, 0x0f, 0x06, 0x0a, 0x2b, 0x06, 0x01, 0x02, 0x01, 0x02, 0x02,
1470
		0x01, 0x01, 0x01, 0x02, 0x01, 0x01,
1471
	}
1472
}
1473

1474
/*
1475
Issue 35, empty responses.
1476
Simple Network Management Protocol
1477

1478
	version: v2c (1)
1479
	community: public
1480
	data: get-request (0)
1481
	    get-request
1482
	        request-id: 1883298028
1483
	        error-status: noError (0)
1484
	        error-index: 0
1485
	        variable-bindings: 0 items
1486
*/
1487
func emptyErrRequest() []byte {
1488
	return []byte{
1489
		0x30, 0x1b, 0x02, 0x01, 0x01, 0x04, 0x06, 0x70, 0x75, 0x62, 0x6c, 0x69,
1490
		0x63, 0xa0, 0x0e, 0x02, 0x04, 0x70, 0x40, 0xd8, 0xec, 0x02, 0x01, 0x00,
1491
		0x02, 0x01, 0x00, 0x30, 0x00,
1492
	}
1493
}
1494

1495
/*
1496
Issue 35, empty responses.
1497

1498
Simple Network Management Protocol
1499

1500
	version: v2c (1)
1501
	community: public
1502
	data: get-response (2)
1503
	    get-response
1504
	        request-id: 1883298028
1505
	        error-status: noError (0)
1506
	        error-index: 0
1507
	        variable-bindings: 0 items
1508
*/
1509
func emptyErrResponse() []byte {
1510
	return []byte{
1511
		0x30, 0x1b, 0x02, 0x01, 0x01, 0x04, 0x06, 0x70, 0x75, 0x62, 0x6c, 0x69,
1512
		0x63, 0xa2, 0x0e, 0x02, 0x04, 0x70, 0x40, 0xd8, 0xec, 0x02, 0x01, 0x00,
1513
		0x02, 0x01, 0x00, 0x30, 0x00,
1514
	}
1515
}
1516

1517
/*
1518
Issue 15, test Counter64.
1519

1520
Simple Network Management Protocol
1521

1522
	version: v2c (1)
1523
	community: public
1524
	data: get-response (2)
1525
	    get-response
1526
	        request-id: 190378322
1527
	        error-status: noError (0)
1528
	        error-index: 0
1529
	        variable-bindings: 1 item
1530
	            1.3.6.1.2.1.31.1.1.1.10.1: 1527943
1531
	                Object Name: 1.3.6.1.2.1.31.1.1.1.10.1 (iso.3.6.1.2.1.31.1.1.1.10.1)
1532
	                Value (Counter64): 1527943
1533
*/
1534
func counter64Response() []byte {
1535
	return []byte{
1536
		0x30, 0x2f, 0x02, 0x01, 0x01, 0x04, 0x06, 0x70, 0x75, 0x62, 0x6c, 0x69,
1537
		0x63, 0xa2, 0x22, 0x02, 0x04, 0x0b, 0x58, 0xf1, 0x52, 0x02, 0x01, 0x00,
1538
		0x02, 0x01, 0x00, 0x30, 0x14, 0x30, 0x12, 0x06, 0x0b, 0x2b, 0x06, 0x01,
1539
		0x02, 0x01, 0x1f, 0x01, 0x01, 0x01, 0x0a, 0x01, 0x46, 0x03, 0x17, 0x50,
1540
		0x87,
1541
	}
1542
}
1543

1544
/*
1545
Issue 370, test Opaque.
1546

1547
Simple Network Management Protocol
1548

1549
	version: 1 (1)
1550
	community: public
1551
	data: get-response (2)
1552
	    get-response
1553
	        request-id: 2033938493
1554
	        error-status: noError (0)
1555
	        error-index: 0
1556
	        variable-bindings: 1 item
1557
	            1.3.6.1.4.1.34187.74195.2.1.24590: 41f00000
1558
	                Object Name: 1.3.6.1.4.1.34187.74195.2.1.24590 (iso.3.6.1.4.1.34187.74195.2.1.24590)
1559
	                Value (Opaque): 41f00000
1560
*/
1561
func opaqueResponse() []byte {
1562
	return []byte{
1563
		0x30, 0x35, 0x02, 0x01, 0x00, 0x04, 0x06, 0x70, 0x75, 0x62, 0x6c, 0x69,
1564
		0x63, 0xa2, 0x28, 0x02, 0x04, 0x79, 0x3b, 0x70, 0x3d, 0x02, 0x01, 0x00,
1565
		0x02, 0x01, 0x00, 0x30, 0x1a, 0x30, 0x18, 0x06, 0x10, 0x2b, 0x06, 0x01,
1566
		0x04, 0x01, 0x82, 0x8b, 0x0b, 0x84, 0xc3, 0x53, 0x02, 0x01, 0x81, 0xc0,
1567
		0x0e, 0x44, 0x04, 0x41, 0xf0, 0x00, 0x00,
1568
	}
1569
}
1570

1571
/*
1572
Opaque Float, observed from Synology NAS UPS MIB
1573

1574
	snmpget -v 2c -c public host 1.3.6.1.4.1.6574.4.2.12.1.0
1575
*/
1576
func opaqueFloatResponse() []byte {
1577
	return []byte{
1578
		0x30, 0x34, 0x02, 0x01, 0x01, 0x04, 0x06, 0x70, 0x75, 0x62, 0x6c, 0x69,
1579
		0x63, 0xa2, 0x27, 0x02, 0x04, 0x23, 0xd5, 0xd7, 0x05, 0x02, 0x01, 0x00,
1580
		0x02, 0x01, 0x00, 0x30, 0x19, 0x30, 0x17, 0x06, 0x0c, 0x2b, 0x06, 0x01,
1581
		0x04, 0x01, 0xb3, 0x2e, 0x04, 0x02, 0x0c, 0x01, 0x00, 0x44, 0x07, 0x9f,
1582
		0x78, 0x04, 0x41, 0x20, 0x00, 0x00,
1583
	}
1584
}
1585

1586
/*
1587
Opaque Double, not observed, crafted based on description:
1588

1589
	https://tools.ietf.org/html/draft-perkins-float-00
1590
*/
1591
func opaqueDoubleResponse() []byte {
1592
	return []byte{
1593
		0x30, 0x38, 0x02, 0x01, 0x01, 0x04, 0x06, 0x70, 0x75, 0x62, 0x6c, 0x69,
1594
		0x63, 0xa2, 0x2b, 0x02, 0x04, 0x23, 0xd5, 0xd7, 0x05, 0x02, 0x01, 0x00,
1595
		0x02, 0x01, 0x00, 0x30, 0x1d, 0x30, 0x1b, 0x06, 0x0c, 0x2b, 0x06, 0x01,
1596
		0x04, 0x01, 0xb3, 0x2e, 0x04, 0x02, 0x0c, 0x01, 0x00, 0x44, 0x0b, 0x9f,
1597
		0x79, 0x08, 0x40, 0x24, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1598
	}
1599
}
1600

1601
func TestUnmarshalEmptyPanic(t *testing.T) {
1602
	var in = []byte{}
1603
	var res = new(SnmpPacket)
1604

1605
	_, err := Default.unmarshalHeader(in, res)
1606
	if err == nil {
1607
		t.Errorf("unmarshalHeader did not gracefully detect empty packet")
1608
	}
1609
}
1610

1611
func TestV3USMInitialPacket(t *testing.T) {
1612
	logger := NewLogger(log.New(io.Discard, "", 0))
1613
	var emptyPdus []SnmpPDU
1614
	blankPacket := &SnmpPacket{
1615
		Version:            Version3,
1616
		MsgFlags:           Reportable | NoAuthNoPriv,
1617
		SecurityModel:      UserSecurityModel,
1618
		SecurityParameters: &UsmSecurityParameters{Logger: logger},
1619
		PDUType:            GetRequest,
1620
		Logger:             logger,
1621
		Variables:          emptyPdus,
1622
	}
1623
	iBytes, err := blankPacket.marshalMsg()
1624
	if err != nil {
1625
		t.Errorf("#TestV3USMInitialPacket: marshalMsg() err returned: %v", err)
1626
	}
1627
	engine := GoSNMP{Logger: Default.Logger}
1628
	pktNew, errDecode := engine.SnmpDecodePacket(iBytes)
1629
	if errDecode != nil {
1630
		t.Logf("-->Bytes=%v", iBytes)
1631
		t.Logf("-->Expect=%v", blankPacket)
1632
		t.Logf("-->got=%v", pktNew)
1633
		t.Errorf("#TestV3USMInitialPacket: SnmpDecodePacket() err returned: %v. ", errDecode)
1634
	}
1635

1636
}
1637

1638
func TestSendOneRequest_dups(t *testing.T) {
1639
	srvr, err := net.ListenUDP("udp4", &net.UDPAddr{})
1640
	if err != nil {
1641
		t.Fatalf("udp4 error listening: %s", err)
1642
	}
1643
	defer srvr.Close()
1644

1645
	x := &GoSNMP{
1646
		Version: Version2c,
1647
		Target:  srvr.LocalAddr().(*net.UDPAddr).IP.String(),
1648
		Port:    uint16(srvr.LocalAddr().(*net.UDPAddr).Port),
1649
		Timeout: time.Millisecond * 100,
1650
		Retries: 2,
1651
	}
1652
	if err := x.Connect(); err != nil {
1653
		t.Fatalf("error connecting: %s", err)
1654
	}
1655

1656
	go func() {
1657
		buf := make([]byte, 256)
1658
		for {
1659
			n, addr, err := srvr.ReadFrom(buf)
1660
			if err != nil {
1661
				return
1662
			}
1663
			buf := buf[:n]
1664

1665
			var reqPkt SnmpPacket
1666
			var cursor int
1667
			cursor, err = x.unmarshalHeader(buf, &reqPkt)
1668
			if err != nil {
1669
				t.Errorf("error: %s", err)
1670
			}
1671
			// if x.Version == Version3 {
1672
			//	buf, cursor, err = x.decryptPacket(buf, cursor, &reqPkt)
1673
			//	if err != nil {
1674
			//		t.Errorf("error: %s", err)
1675
			//	}
1676
			//}
1677
			err = x.unmarshalPayload(buf, cursor, &reqPkt)
1678
			if err != nil {
1679
				t.Errorf("error: %s", err)
1680
			}
1681

1682
			rspPkt := x.mkSnmpPacket(GetResponse, []SnmpPDU{
1683
				{
1684
					Name:  ".1.2",
1685
					Type:  Integer,
1686
					Value: 123,
1687
				},
1688
			}, 0, 0)
1689
			rspPkt.RequestID = reqPkt.RequestID
1690
			outBuf, err := rspPkt.marshalMsg()
1691
			if err != nil {
1692
				t.Errorf("ERR: %s", err)
1693
			}
1694
			srvr.WriteTo(outBuf, addr)
1695
			for i := 0; i <= x.Retries; i++ {
1696
				srvr.WriteTo(outBuf, addr)
1697
			}
1698
		}
1699
	}()
1700

1701
	pdus := []SnmpPDU{{Name: ".1.2", Type: Null}}
1702
	// This is not actually a GetResponse, but we need something our test server can unmarshal.
1703
	reqPkt := x.mkSnmpPacket(GetResponse, pdus, 0, 0)
1704

1705
	_, err = x.sendOneRequest(reqPkt, true)
1706
	if err != nil {
1707
		t.Errorf("error: %s", err)
1708
		return
1709
	}
1710

1711
	_, err = x.sendOneRequest(reqPkt, true)
1712
	if err != nil {
1713
		t.Errorf("error: %s", err)
1714
		return
1715
	}
1716
}
1717

1718
func BenchmarkSendOneRequest(b *testing.B) {
1719
	b.StopTimer()
1720

1721
	srvr, err := net.ListenUDP("udp4", &net.UDPAddr{})
1722
	if err != nil {
1723
		b.Fatalf("udp4 error listening: %s", err)
1724
	}
1725
	defer srvr.Close()
1726

1727
	x := &GoSNMP{
1728
		Version: Version2c,
1729
		Target:  srvr.LocalAddr().(*net.UDPAddr).IP.String(),
1730
		Port:    uint16(srvr.LocalAddr().(*net.UDPAddr).Port),
1731
		Timeout: time.Millisecond * 100,
1732
		Retries: 2,
1733
	}
1734
	if err := x.Connect(); err != nil {
1735
		b.Fatalf("error connecting: %s", err)
1736
	}
1737

1738
	go func() {
1739
		buf := make([]byte, 256)
1740
		outBuf := counter64Response()
1741
		for {
1742
			_, addr, err := srvr.ReadFrom(buf)
1743
			if err != nil {
1744
				return
1745
			}
1746

1747
			copy(outBuf[17:21], buf[11:15]) // evil: copy request ID
1748
			srvr.WriteTo(outBuf, addr)
1749
		}
1750
	}()
1751

1752
	pdus := []SnmpPDU{{Name: ".1.3.6.1.2.1.31.1.1.1.10.1", Type: Null}}
1753
	reqPkt := x.mkSnmpPacket(GetRequest, pdus, 0, 0)
1754

1755
	// make sure everything works before starting the test
1756
	_, err = x.sendOneRequest(reqPkt, true)
1757
	if err != nil {
1758
		b.Fatalf("Precheck failed: %s", err)
1759
	}
1760

1761
	b.StartTimer()
1762

1763
	for n := 0; n < b.N; n++ {
1764
		_, err = x.sendOneRequest(reqPkt, true)
1765
		if err != nil {
1766
			b.Fatalf("error: %s", err)
1767
			return
1768
		}
1769
	}
1770
}
1771

1772
func TestUnconnectedSocket_fail(t *testing.T) {
1773
	withUnconnectedSocket(t, false)
1774
}
1775

1776
func TestUnconnectedSocket_success(t *testing.T) {
1777
	withUnconnectedSocket(t, true)
1778
}
1779

1780
func withUnconnectedSocket(t *testing.T, enable bool) {
1781
	srvr, err := net.ListenUDP("udp", &net.UDPAddr{})
1782
	if err != nil {
1783
		t.Fatalf("udp error listening: %s", err)
1784
	}
1785
	defer srvr.Close()
1786

1787
	x := &GoSNMP{
1788
		Version:                 Version2c,
1789
		Target:                  srvr.LocalAddr().(*net.UDPAddr).IP.String(),
1790
		Port:                    uint16(srvr.LocalAddr().(*net.UDPAddr).Port),
1791
		Timeout:                 time.Millisecond * 100,
1792
		Retries:                 2,
1793
		UseUnconnectedUDPSocket: enable,
1794
		LocalAddr:               "0.0.0.0:",
1795
	}
1796
	if err := x.Connect(); err != nil {
1797
		t.Fatalf("error connecting: %s", err)
1798
	}
1799
	defer x.Conn.Close()
1800

1801
	go func() {
1802
		buf := make([]byte, 256)
1803
		for {
1804
			n, addr, err := srvr.ReadFrom(buf)
1805
			if err != nil {
1806
				return
1807
			}
1808
			buf := buf[:n]
1809

1810
			var reqPkt SnmpPacket
1811
			var cursor int
1812
			cursor, err = x.unmarshalHeader(buf, &reqPkt)
1813
			if err != nil {
1814
				t.Errorf("error: %s", err)
1815
			}
1816
			err = x.unmarshalPayload(buf, cursor, &reqPkt)
1817
			if err != nil {
1818
				t.Errorf("error: %s", err)
1819
			}
1820

1821
			rspPkt := x.mkSnmpPacket(GetResponse, []SnmpPDU{
1822
				{
1823
					Name:  ".1.2",
1824
					Type:  Integer,
1825
					Value: 123,
1826
				},
1827
			}, 0, 0)
1828
			rspPkt.RequestID = reqPkt.RequestID
1829
			outBuf, err := rspPkt.marshalMsg()
1830
			if err != nil {
1831
				t.Errorf("ERR: %s", err)
1832
			}
1833
			// Temporary socket will use different source port, it's enough to break
1834
			// connected socket reply filters.
1835
			nsock, err := net.ListenUDP("udp", nil)
1836
			if err != nil {
1837
				t.Errorf("can't create temporary reply socket: %v", err)
1838
			}
1839
			nsock.WriteTo(outBuf, addr)
1840
			nsock.Close()
1841
		}
1842
	}()
1843

1844
	pdus := []SnmpPDU{{Name: ".1.2", Type: Null}}
1845
	// This is not actually a GetResponse, but we need something our test server can unmarshal.
1846
	reqPkt := x.mkSnmpPacket(GetResponse, pdus, 0, 0)
1847

1848
	_, err = x.sendOneRequest(reqPkt, true)
1849
	if err != nil && enable {
1850
		t.Errorf("with unconnected socket enabled got unexpected error: %v", err)
1851
	} else if err == nil && !enable {
1852
		t.Errorf("with unconnected socket disabled didn't get an error")
1853
	}
1854
}
1855

1856
/*
1857
$ snmptrap -v 2c -c public 192.168.1.10 '' SNMPv2-MIB::system SNMPv2-MIB::sysDescr.0 s "red laptop" SNMPv2-MIB::sysServices.0 i "5"
1858

1859
Simple Network Management Protocol
1860
    version: v2c (1)
1861
    community: public
1862
    data: snmpV2-trap (7)
1863
        snmpV2-trap
1864
            request-id: 1271509950
1865
            error-status: noError (0)
1866
            error-index: 0
1867
            variable-bindings: 5 items
1868
                1.3.6.1.2.1.1.3.0: 1034156
1869
                    Object Name: 1.3.6.1.2.1.1.3.0 (iso.3.6.1.2.1.1.3.0)
1870
                    Value (Timeticks): 1034156
1871
                1.3.6.1.6.3.1.1.4.1.0: 1.3.6.1.2.1.1 (iso.3.6.1.2.1.1)
1872
                    Object Name: 1.3.6.1.6.3.1.1.4.1.0 (iso.3.6.1.6.3.1.1.4.1.0)
1873
                    Value (OID): 1.3.6.1.2.1.1 (iso.3.6.1.2.1.1)
1874
                1.3.6.1.2.1.1.1.0: 726564206c6170746f70
1875
                    Object Name: 1.3.6.1.2.1.1.1.0 (iso.3.6.1.2.1.1.1.0)
1876
                    Value (OctetString): 726564206c6170746f70
1877
                        Variable-binding-string: red laptop
1878
                1.3.6.1.2.1.1.7.0:
1879
                    Object Name: 1.3.6.1.2.1.1.7.0 (iso.3.6.1.2.1.1.7.0)
1880
                    Value (Integer32): 5
1881
                1.3.6.1.2.1.1.2: 1.3.6.1.4.1.2.3.4.5 (iso.3.6.1.4.1.2.3.4.5)
1882
                    Object Name: 1.3.6.1.2.1.1.2 (iso.3.6.1.2.1.1.2)
1883
                    Value (OID): 1.3.6.1.4.1.2.3.4.5 (iso.3.6.1.4.1.2.3.4.5)
1884
*/
1885

1886
func trap1() []byte {
1887
	return []byte{
1888
		0x30, 0x81, 0x80, 0x02, 0x01, 0x01, 0x04, 0x06, 0x70, 0x75, 0x62, 0x6c, 0x69, 0x63, 0xa7, 0x73,
1889
		0x02, 0x04, 0x72, 0x5c, 0xef, 0x42, 0x02, 0x01, 0x00, 0x02, 0x01, 0x00, 0x30, 0x65, 0x30, 0x10,
1890
		0x06, 0x08, 0x2b, 0x06, 0x01, 0x02, 0x01, 0x01, 0x03, 0x00, 0x43, 0x04, 0x01, 0x1a, 0xef, 0xa5,
1891
		0x30, 0x14, 0x06, 0x0a, 0x2b, 0x06, 0x01, 0x06, 0x03, 0x01, 0x01, 0x04, 0x01, 0x00, 0x06, 0x06,
1892
		0x2b, 0x06, 0x01, 0x02, 0x01, 0x01, 0x30, 0x16, 0x06, 0x08, 0x2b, 0x06, 0x01, 0x02, 0x01, 0x01,
1893
		0x01, 0x00, 0x04, 0x0a, 0x72, 0x65, 0x64, 0x20, 0x6c, 0x61, 0x70, 0x74, 0x6f, 0x70, 0x30, 0x0d,
1894
		0x06, 0x08, 0x2b, 0x06, 0x01, 0x02, 0x01, 0x01, 0x07, 0x00, 0x02, 0x01, 0x05, 0x30, 0x14, 0x06,
1895
		0x07, 0x2b, 0x06, 0x01, 0x02, 0x01, 0x01, 0x02, 0x06, 0x09, 0x2b, 0x06, 0x01, 0x04, 0x01, 0x02,
1896
		0x03, 0x04, 0x05, 0x00, 0x00, 0x00, 0xd0, 0x00, 0x00, 0x00, 0x06, 0x00, 0x00, 0x00, 0x68, 0x00,
1897
		0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x74, 0x3a, 0x05, 0x00, 0xa1, 0x27, 0x42, 0x0c, 0x46, 0x00,
1898
		0x00, 0x00, 0x46, 0x00, 0x00, 0x00, 0x10, 0x4a, 0x7d, 0x34, 0x3a, 0xa5, 0x74, 0xda, 0x38, 0x4d,
1899
		0x6c, 0x6c, 0x08, 0x00, 0x45, 0x00, 0x00, 0x38, 0xcc, 0xdb, 0x40, 0x00, 0xff, 0x01, 0x2b, 0x74,
1900
		0xc0, 0xa8, 0x01, 0x0a, 0xc0, 0xa8, 0x01, 0x1a, 0x03, 0x03, 0x11, 0x67, 0x00, 0x00, 0x00, 0x00,
1901
		0x45, 0x00, 0x00, 0x9f, 0xe6, 0x8f, 0x40, 0x00, 0x40, 0x11, 0x00, 0x00, 0xc0, 0xa8, 0x01, 0x1a,
1902
		0xc0, 0xa8, 0x01, 0x0a, 0xaf, 0x78, 0x00, 0xa2, 0x00, 0x8b, 0x0b, 0x3a, 0x00, 0x00, 0x68, 0x00,
1903
		0x00, 0x00, 0x05, 0x00, 0x00, 0x00, 0x6c, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x74, 0x3a,
1904
		0x05, 0x00, 0xca, 0x94, 0x67, 0x0c, 0x01, 0x00, 0x1c, 0x00, 0x43, 0x6f, 0x75, 0x6e, 0x74, 0x65,
1905
		0x72, 0x73, 0x20, 0x70, 0x72, 0x6f, 0x76, 0x69, 0x64, 0x65, 0x64, 0x20, 0x62, 0x79, 0x20, 0x64,
1906
		0x75, 0x6d, 0x70, 0x63, 0x61, 0x70, 0x02, 0x00, 0x08, 0x00, 0x74, 0x3a, 0x05, 0x00, 0xdf, 0xba,
1907
		0x27, 0x0c, 0x03, 0x00, 0x08, 0x00, 0x74, 0x3a, 0x05, 0x00, 0x18, 0x94, 0x67, 0x0c, 0x04, 0x00,
1908
		0x08, 0x00, 0x0c, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x05, 0x00, 0x08, 0x00, 0x00, 0x00,
1909
		0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x6c, 0x00, 0x00, 0x00}
1910
}
1911

1912
// Simple Network Management Protocol
1913
//     msgVersion: snmpv3 (3)
1914
//     msgGlobalData
1915
//         msgID: 91040642
1916
//         msgMaxSize: 65507
1917
//         msgFlags: 04
1918
//         msgSecurityModel: USM (3)
1919
//     msgAuthoritativeEngineID: <MISSING>
1920
//     msgAuthoritativeEngineBoots: 0
1921
//     msgAuthoritativeEngineTime: 0
1922
//     msgUserName:
1923
//     msgAuthenticationParameters: <MISSING>
1924
//     msgPrivacyParameters: <MISSING>
1925
//     msgData: plaintext (0)
1926
//         plaintext
1927

1928
func snmpv3HelloRequest() []byte {
1929
	return []byte{0x30, 0x52, 0x02, 0x01, 0x03, 0x30, 0x11, 0x02,
1930
		0x04, 0x05, 0x6d, 0x2b, 0x82, 0x02, 0x03, 0x00,
1931
		0xff, 0xe3, 0x04, 0x01, 0x04, 0x02, 0x01, 0x03,
1932
		0x04, 0x10, 0x30, 0x0e, 0x04, 0x00, 0x02, 0x01,
1933
		0x00, 0x02, 0x01, 0x00, 0x04, 0x00, 0x04, 0x00,
1934
		0x04, 0x00, 0x30, 0x28, 0x04, 0x00, 0x04, 0x14,
1935
		0x66, 0x6f, 0x72, 0x65, 0x69, 0x67, 0x6e, 0x66,
1936
		0x6f, 0x72, 0x6d, 0x61, 0x74, 0x73, 0x2f, 0x6c,
1937
		0x69, 0x6e, 0x75, 0x78, 0xa0, 0x0e, 0x02, 0x04,
1938
		0x44, 0xfa, 0x16, 0xe1, 0x02, 0x01, 0x00, 0x02,
1939
		0x01, 0x00, 0x30, 0x00}
1940
}
1941

1942
// msgData: plaintext (0)
1943
//     plaintext
1944
//         contextEngineID: 80004fb8054445534b544f502d4a3732533245343ab63bc8
1945
//             1... .... = Engine ID Conformance: RFC3411 (SNMPv3)
1946
//             Engine Enterprise ID: pysnmp (20408)
1947
//             Engine ID Format: Octets, administratively assigned (5)
1948
//             Engine ID Data: 4445534b544f502d4a3732533245343ab63bc8
1949
//         contextName: foreignformats/linux
1950
//         data: report (8)
1951
//             report
1952
//                 request-id: 1157240545
1953
//                 error-status: noError (0)
1954
//                 error-index: 0
1955
//                 variable-bindings: 1 item
1956
//                     1.3.6.1.6.3.15.1.1.4.0: 21
1957
//                         Object Name: 1.3.6.1.6.3.15.1.1.4.0 (iso.3.6.1.6.3.15.1.1.4.0)
1958
//                         Value (Counter32): 21
1959

1960
func snmpv3HelloResponse() []byte {
1961
	return []byte{
1962
		0x30, 0x81, 0x95, 0x02, 0x01, 0x03, 0x30, 0x11,
1963
		0x02, 0x04, 0x05, 0x6d, 0x2b, 0x82, 0x02, 0x03,
1964
		0x00, 0xff, 0xe3, 0x04, 0x01, 0x00, 0x02, 0x01,
1965
		0x03, 0x04, 0x2a, 0x30, 0x28, 0x04, 0x18, 0x80,
1966
		0x00, 0x4f, 0xb8, 0x05, 0x44, 0x45, 0x53, 0x4b,
1967
		0x54, 0x4f, 0x50, 0x2d, 0x4a, 0x37, 0x32, 0x53,
1968
		0x32, 0x45, 0x34, 0x3a, 0xb6, 0x3b, 0xc8, 0x02,
1969
		0x01, 0x02, 0x02, 0x03, 0x00, 0xc4, 0x7a, 0x04,
1970
		0x00, 0x04, 0x00, 0x04, 0x00, 0x30, 0x51, 0x04,
1971
		0x18, 0x80, 0x00, 0x4f, 0xb8, 0x05, 0x44, 0x45,
1972
		0x53, 0x4b, 0x54, 0x4f, 0x50, 0x2d, 0x4a, 0x37,
1973
		0x32, 0x53, 0x32, 0x45, 0x34, 0x3a, 0xb6, 0x3b,
1974
		0xc8, 0x04, 0x14, 0x66, 0x6f, 0x72, 0x65, 0x69,
1975
		0x67, 0x6e, 0x66, 0x6f, 0x72, 0x6d, 0x61, 0x74,
1976
		0x73, 0x2f, 0x6c, 0x69, 0x6e, 0x75, 0x78, 0xa8,
1977
		0x1f, 0x02, 0x04, 0x44, 0xfa, 0x16, 0xe1, 0x02,
1978
		0x01, 0x00, 0x02, 0x01, 0x00, 0x30, 0x11, 0x30,
1979
		0x0f, 0x06, 0x0a, 0x2b, 0x06, 0x01, 0x06, 0x03,
1980
		0x0f, 0x01, 0x01, 0x04, 0x00, 0x41, 0x01, 0x15,
1981
	}
1982
}
1983

1984
// dump bytes in a format similar to Wireshark
1985
func dumpBytes1(data []byte, msg string, maxlength int) {
1986
	var buffer bytes.Buffer
1987
	buffer.WriteString(msg)
1988
	length := maxlength
1989
	if len(data) < maxlength {
1990
		length = len(data)
1991
	}
1992
	length *= 2 //One Byte Symbols Two Hex
1993
	hexStr := hex.EncodeToString(data)
1994
	for i := 0; length >= i+16; i += 16 {
1995
		buffer.WriteString("\n")
1996
		buffer.WriteString(strconv.Itoa(i / 2))
1997
		buffer.WriteString("\t")
1998
		buffer.WriteString(hexStr[i : i+2])
1999
		buffer.WriteString(" ")
2000
		buffer.WriteString(hexStr[i+2 : i+4])
2001
		buffer.WriteString(" ")
2002
		buffer.WriteString(hexStr[i+4 : i+6])
2003
		buffer.WriteString(" ")
2004
		buffer.WriteString(hexStr[i+6 : i+8])
2005
		buffer.WriteString(" ")
2006
		buffer.WriteString(hexStr[i+8 : i+10])
2007
		buffer.WriteString(" ")
2008
		buffer.WriteString(hexStr[i+10 : i+12])
2009
		buffer.WriteString(" ")
2010
		buffer.WriteString(hexStr[i+12 : i+14])
2011
		buffer.WriteString(" ")
2012
		buffer.WriteString(hexStr[i+14 : i+16])
2013
	}
2014
	leftOver := length % 16
2015
	if leftOver != 0 {
2016
		buffer.WriteString("\n")
2017
		buffer.WriteString(strconv.Itoa((length - leftOver) / 2))
2018
		buffer.WriteString("\t")
2019
		for i := 0; leftOver >= i+2; i += 2 {
2020
			buffer.WriteString(hexStr[i : i+2])
2021
			buffer.WriteString(" ")
2022
		}
2023
	}
2024
	buffer.WriteString("\n")
2025
}
2026

2027
// dump bytes in one row, up to about screen width. Returns a string
2028
// rather than (dumpBytes1) writing to debugging log.
2029
func dumpBytes2(desc string, bb []byte, cursor int) string {
2030
	cursor = cursor - 4 // give some context to dump
2031
	if cursor < 0 {
2032
		cursor = 0
2033
	}
2034
	result := desc
2035
	for i, b := range bb[cursor:] {
2036
		if i > 30 { // about screen width...
2037
			break
2038
		}
2039
		result += fmt.Sprintf(" %02x", b)
2040
	}
2041
	return result
2042
}
2043

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

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

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

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