forked from twsnmp/gosnmp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
misc_test.go
190 lines (165 loc) · 4.79 KB
/
misc_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
// Copyright 2012-2020 The GoSNMP Authors. All rights reserved. Use of this
// source code is governed by a BSD-style license that can be found in the
// LICENSE file.
// +build all misc
package gosnmp
import (
"bytes"
"crypto"
_ "crypto/md5"
_ "crypto/sha1"
"errors"
"reflect"
"testing"
"github.com/stretchr/testify/assert"
)
// Tests in alphabetical order of function being tested
// -----------------------------------------------------------------------------
var testsMarshalLength = []struct {
length int
expected []byte
}{
{1, []byte{0x01}},
{129, []byte{0x81, 0x81}},
{256, []byte{0x82, 0x01, 0x00}},
{272, []byte{0x82, 0x01, 0x10}},
{435, []byte{0x82, 0x01, 0xb3}},
}
func TestMarshalLength(t *testing.T) {
for i, test := range testsMarshalLength {
testBytes, err := marshalLength(test.length)
if err != nil {
t.Errorf("%d: length %d got err %v", i, test.length, err)
}
if !reflect.DeepEqual(testBytes, test.expected) {
t.Errorf("%d: length %d got |%x| expected |%x|",
i, test.length, testBytes, test.expected)
}
}
}
// -----------------------------------------------------------------------------
var testsPartition = []struct {
currentPosition int
partitionSize int
sliceLength int
ok bool
}{
{-1, 3, 8, false}, // test out of range
{8, 3, 8, false}, // test out of range
{0, 3, 8, false}, // test 0-7/3 per doco
{1, 3, 8, false},
{2, 3, 8, true},
{3, 3, 8, false},
{4, 3, 8, false},
{5, 3, 8, true},
{6, 3, 8, false},
{7, 3, 8, true},
{-1, 1, 3, false}, // partition size of one
{0, 1, 3, true},
{1, 1, 3, true},
{2, 1, 3, true},
{3, 1, 3, false},
}
func TestPartition(t *testing.T) {
for i, test := range testsPartition {
ok := Partition(test.currentPosition, test.partitionSize, test.sliceLength)
if ok != test.ok {
t.Errorf("#%d: Bad result: %v (expected %v)", i, ok, test.ok)
}
}
}
// ---------------------------------------------------------------------
var testsSnmpVersionString = []struct {
in SnmpVersion
out string
}{
{Version1, "1"},
{Version2c, "2c"},
{Version3, "3"},
}
func TestSnmpVersionString(t *testing.T) {
for i, test := range testsSnmpVersionString {
result := test.in.String()
if result != test.out {
t.Errorf("#%d, got %v expected %v", i, result, test.out)
}
}
}
// ---------------------------------------------------------------------
var testSnmpV3MD5HMAC = []struct {
password string
engineid string
outKey []byte
}{
{"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}},
}
func TestMD5HMAC(t *testing.T) {
for i, test := range testSnmpV3MD5HMAC {
cacheKey := make([]byte, 1+len(test.password))
cacheKey = append(cacheKey, 'h'+byte(MD5))
cacheKey = append(cacheKey, []byte(test.password)...)
result, err := hMAC(crypto.MD5, string(cacheKey), test.password, test.engineid)
assert.NoError(t, err)
if !bytes.Equal(result, test.outKey) {
t.Errorf("#%d, got %v expected %v", i, result, test.outKey)
}
}
}
var testSnmpV3SHAHMAC = []struct {
password string
engineid string
outKey []byte
}{
{"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}},
}
func TestSHAHMAC(t *testing.T) {
for i, test := range testSnmpV3SHAHMAC {
cacheKey := make([]byte, 1+len(test.password))
cacheKey = append(cacheKey, 'h'+byte(SHA))
cacheKey = append(cacheKey, []byte(test.password)...)
result, err := hMAC(crypto.SHA1, string(cacheKey), test.password, test.engineid)
if err != nil {
t.Fatal(err)
}
if !bytes.Equal(result, test.outKey) {
t.Errorf("#%d, got %v expected %v", i, result, test.outKey)
}
}
}
// ---------------------------------------------------------------------
/*
var testMarshalTimeticks = []struct {
timeticks uint32
out []byte
}{
{1034156, []byte{0x0f, 0xc7, 0xac}},
}
func TestMarshalTimeticks(t *testing.T) {
for i, test := range testMarshalTimeticks {
result, err := marshalTimeticks(test.timeticks)
if err != nil {
t.Errorf("%d: expected %v got err %v", i, result, err)
}
if !bytes.Equal(result, test.out) {
t.Errorf("#%d, got %v expected %v", i, result, test.out)
}
}
}
*/
// parseBitString parses an ASN.1 bit string from the given byte slice and returns it.
func parseBitString(bytes []byte) (ret BitStringValue, err error) {
if len(bytes) == 0 {
err = errors.New("zero length BIT STRING")
return
}
paddingBits := int(bytes[0])
if paddingBits > 7 ||
len(bytes) == 1 && paddingBits > 0 ||
bytes[len(bytes)-1]&((1<<bytes[0])-1) != 0 {
err = errors.New("invalid padding bits in BIT STRING")
return
}
ret.BitLength = (len(bytes)-1)*8 - paddingBits
ret.Bytes = bytes[1:]
return
}