gosnmp
/
misc_test.go
231 строка · 5.7 Кб
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 || misc
6// +build all misc
7
8package gosnmp
9
10import (
11"bytes"
12"crypto"
13_ "crypto/md5"
14_ "crypto/sha1"
15"errors"
16"math"
17"math/big"
18"reflect"
19"testing"
20
21"github.com/stretchr/testify/assert"
22)
23
24// -----------------------------------------------------------------------------
25
26var testsMarshalLength = []struct {
27length int
28expected []byte
29}{
30{1, []byte{0x01}},
31{129, []byte{0x81, 0x81}},
32{256, []byte{0x82, 0x01, 0x00}},
33{272, []byte{0x82, 0x01, 0x10}},
34{435, []byte{0x82, 0x01, 0xb3}},
35}
36
37func TestMarshalLength(t *testing.T) {
38for i, test := range testsMarshalLength {
39testBytes, err := marshalLength(test.length)
40if err != nil {
41t.Errorf("%d: length %d got err %v", i, test.length, err)
42}
43if !reflect.DeepEqual(testBytes, test.expected) {
44t.Errorf("%d: length %d got |%x| expected |%x|",
45i, test.length, testBytes, test.expected)
46}
47}
48}
49
50// -----------------------------------------------------------------------------
51
52var testsPartition = []struct {
53currentPosition int
54partitionSize int
55sliceLength int
56ok bool
57}{
58{-1, 3, 8, false}, // test out of range
59{8, 3, 8, false}, // test out of range
60{0, 3, 8, false}, // test 0-7/3 per doco
61{1, 3, 8, false},
62{2, 3, 8, true},
63{3, 3, 8, false},
64{4, 3, 8, false},
65{5, 3, 8, true},
66{6, 3, 8, false},
67{7, 3, 8, true},
68{-1, 1, 3, false}, // partition size of one
69{0, 1, 3, true},
70{1, 1, 3, true},
71{2, 1, 3, true},
72{3, 1, 3, false},
73}
74
75func TestPartition(t *testing.T) {
76for i, test := range testsPartition {
77ok := Partition(test.currentPosition, test.partitionSize, test.sliceLength)
78if ok != test.ok {
79t.Errorf("#%d: Bad result: %v (expected %v)", i, ok, test.ok)
80}
81}
82}
83
84// ---------------------------------------------------------------------
85
86var testsToBigInt = []struct {
87in interface{}
88expected *big.Int
89}{
90{int8(-42), big.NewInt(-42)},
91{int16(42), big.NewInt(42)},
92{int32(-42), big.NewInt(-42)},
93{int64(42), big.NewInt(42)},
94
95{uint8(42), big.NewInt(42)},
96{uint16(42), big.NewInt(42)},
97{uint32(42), big.NewInt(42)},
98{uint64(42), big.NewInt(42)},
99
100// edge case, max uint64
101{uint64(math.MaxUint64), new(big.Int).SetUint64(math.MaxUint64)},
102
103// string: valid number
104{"-123456789", big.NewInt(-123456789)},
105
106// string: invalid number
107{"foo", new(big.Int)},
108
109// unhandled type
110{struct{}{}, new(big.Int)},
111}
112
113func TestToBigInt(t *testing.T) {
114for i, test := range testsToBigInt {
115result := ToBigInt(test.in)
116if result.Cmp(test.expected) != 0 {
117t.Errorf("#%d, %T: got %v expected %v", i, test.in, result, test.expected)
118}
119}
120}
121
122// ---------------------------------------------------------------------
123
124var testsSnmpVersionString = []struct {
125in SnmpVersion
126out string
127}{
128{Version1, "1"},
129{Version2c, "2c"},
130{Version3, "3"},
131}
132
133func TestSnmpVersionString(t *testing.T) {
134for i, test := range testsSnmpVersionString {
135result := test.in.String()
136if result != test.out {
137t.Errorf("#%d, got %v expected %v", i, result, test.out)
138}
139}
140}
141
142// ---------------------------------------------------------------------
143
144var testSnmpV3MD5HMAC = []struct {
145password string
146engineid string
147outKey []byte
148}{
149{"maplesyrup", string([]byte{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2}), []byte{0x52, 0x6f, 0x5e, 0xed, 0x9f, 0xcc, 0xe2, 0x6f, 0x89, 0x64, 0xc2, 0x93, 0x07, 0x87, 0xd8, 0x2b}},
150}
151
152func TestMD5HMAC(t *testing.T) {
153for i, test := range testSnmpV3MD5HMAC {
154cacheKey := make([]byte, 1+len(test.password))
155cacheKey = append(cacheKey, 'h'+byte(MD5))
156cacheKey = append(cacheKey, []byte(test.password)...)
157
158result, err := hMAC(crypto.MD5, string(cacheKey), test.password, test.engineid)
159assert.NoError(t, err)
160if !bytes.Equal(result, test.outKey) {
161t.Errorf("#%d, got %v expected %v", i, result, test.outKey)
162}
163}
164}
165
166var testSnmpV3SHAHMAC = []struct {
167password string
168engineid string
169outKey []byte
170}{
171{"maplesyrup", string([]byte{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2}), []byte{0x66, 0x95, 0xfe, 0xbc, 0x92, 0x88, 0xe3, 0x62, 0x82, 0x23, 0x5f, 0xc7, 0x15, 0x1f, 0x12, 0x84, 0x97, 0xb3, 0x8f, 0x3f}},
172}
173
174func TestSHAHMAC(t *testing.T) {
175for i, test := range testSnmpV3SHAHMAC {
176cacheKey := make([]byte, 1+len(test.password))
177cacheKey = append(cacheKey, 'h'+byte(SHA))
178cacheKey = append(cacheKey, []byte(test.password)...)
179
180result, err := hMAC(crypto.SHA1, string(cacheKey), test.password, test.engineid)
181if err != nil {
182t.Fatal(err)
183}
184if !bytes.Equal(result, test.outKey) {
185t.Errorf("#%d, got %v expected %v", i, result, test.outKey)
186}
187}
188}
189
190// ---------------------------------------------------------------------
191
192/*
193var testMarshalTimeticks = []struct {
194timeticks uint32
195out []byte
196}{
197{1034156, []byte{0x0f, 0xc7, 0xac}},
198}
199
200func TestMarshalTimeticks(t *testing.T) {
201for i, test := range testMarshalTimeticks {
202result, err := marshalTimeticks(test.timeticks)
203if err != nil {
204t.Errorf("%d: expected %v got err %v", i, result, err)
205}
206if !bytes.Equal(result, test.out) {
207t.Errorf("#%d, got %v expected %v", i, result, test.out)
208}
209}
210}
211*/
212
213// parseBitString parses an ASN.1 bit string from the given byte slice and returns it.
214func parseBitString(bytes []byte) (ret BitStringValue, err error) {
215if len(bytes) == 0 {
216err = errors.New("zero length BIT STRING")
217return
218}
219paddingBits := int(bytes[0])
220if paddingBits > 7 ||
221len(bytes) == 1 && paddingBits > 0 ||
222bytes[len(bytes)-1]&((1<<bytes[0])-1) != 0 {
223err = errors.New("invalid padding bits in BIT STRING")
224return
225}
226ret.BitLength = (len(bytes)-1)*8 - paddingBits
227ret.Bytes = bytes[1:]
228return
229}
230
231// ---------------------------------------------------------------------
232