gosnmp

Форк
0
/
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

8
package gosnmp
9

10
import (
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

26
var testsMarshalLength = []struct {
27
	length   int
28
	expected []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

37
func TestMarshalLength(t *testing.T) {
38
	for i, test := range testsMarshalLength {
39
		testBytes, err := marshalLength(test.length)
40
		if err != nil {
41
			t.Errorf("%d: length %d got err %v", i, test.length, err)
42
		}
43
		if !reflect.DeepEqual(testBytes, test.expected) {
44
			t.Errorf("%d: length %d got |%x| expected |%x|",
45
				i, test.length, testBytes, test.expected)
46
		}
47
	}
48
}
49

50
// -----------------------------------------------------------------------------
51

52
var testsPartition = []struct {
53
	currentPosition int
54
	partitionSize   int
55
	sliceLength     int
56
	ok              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

75
func TestPartition(t *testing.T) {
76
	for i, test := range testsPartition {
77
		ok := Partition(test.currentPosition, test.partitionSize, test.sliceLength)
78
		if ok != test.ok {
79
			t.Errorf("#%d: Bad result: %v (expected %v)", i, ok, test.ok)
80
		}
81
	}
82
}
83

84
// ---------------------------------------------------------------------
85

86
var testsToBigInt = []struct {
87
	in       interface{}
88
	expected *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

113
func TestToBigInt(t *testing.T) {
114
	for i, test := range testsToBigInt {
115
		result := ToBigInt(test.in)
116
		if result.Cmp(test.expected) != 0 {
117
			t.Errorf("#%d, %T: got %v expected %v", i, test.in, result, test.expected)
118
		}
119
	}
120
}
121

122
// ---------------------------------------------------------------------
123

124
var testsSnmpVersionString = []struct {
125
	in  SnmpVersion
126
	out string
127
}{
128
	{Version1, "1"},
129
	{Version2c, "2c"},
130
	{Version3, "3"},
131
}
132

133
func TestSnmpVersionString(t *testing.T) {
134
	for i, test := range testsSnmpVersionString {
135
		result := test.in.String()
136
		if result != test.out {
137
			t.Errorf("#%d, got %v expected %v", i, result, test.out)
138
		}
139
	}
140
}
141

142
// ---------------------------------------------------------------------
143

144
var testSnmpV3MD5HMAC = []struct {
145
	password string
146
	engineid string
147
	outKey   []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

152
func TestMD5HMAC(t *testing.T) {
153
	for i, test := range testSnmpV3MD5HMAC {
154
		cacheKey := make([]byte, 1+len(test.password))
155
		cacheKey = append(cacheKey, 'h'+byte(MD5))
156
		cacheKey = append(cacheKey, []byte(test.password)...)
157

158
		result, err := hMAC(crypto.MD5, string(cacheKey), test.password, test.engineid)
159
		assert.NoError(t, err)
160
		if !bytes.Equal(result, test.outKey) {
161
			t.Errorf("#%d, got %v expected %v", i, result, test.outKey)
162
		}
163
	}
164
}
165

166
var testSnmpV3SHAHMAC = []struct {
167
	password string
168
	engineid string
169
	outKey   []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

174
func TestSHAHMAC(t *testing.T) {
175
	for i, test := range testSnmpV3SHAHMAC {
176
		cacheKey := make([]byte, 1+len(test.password))
177
		cacheKey = append(cacheKey, 'h'+byte(SHA))
178
		cacheKey = append(cacheKey, []byte(test.password)...)
179

180
		result, err := hMAC(crypto.SHA1, string(cacheKey), test.password, test.engineid)
181
		if err != nil {
182
			t.Fatal(err)
183
		}
184
		if !bytes.Equal(result, test.outKey) {
185
			t.Errorf("#%d, got %v expected %v", i, result, test.outKey)
186
		}
187
	}
188
}
189

190
// ---------------------------------------------------------------------
191

192
/*
193
var testMarshalTimeticks = []struct {
194
	timeticks uint32
195
	out       []byte
196
}{
197
	{1034156, []byte{0x0f, 0xc7, 0xac}},
198
}
199

200
func TestMarshalTimeticks(t *testing.T) {
201
	for i, test := range testMarshalTimeticks {
202
		result, err := marshalTimeticks(test.timeticks)
203
		if err != nil {
204
			t.Errorf("%d: expected %v got err %v", i, result, err)
205
		}
206
		if !bytes.Equal(result, test.out) {
207
			t.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.
214
func parseBitString(bytes []byte) (ret BitStringValue, err error) {
215
	if len(bytes) == 0 {
216
		err = errors.New("zero length BIT STRING")
217
		return
218
	}
219
	paddingBits := int(bytes[0])
220
	if paddingBits > 7 ||
221
		len(bytes) == 1 && paddingBits > 0 ||
222
		bytes[len(bytes)-1]&((1<<bytes[0])-1) != 0 {
223
		err = errors.New("invalid padding bits in BIT STRING")
224
		return
225
	}
226
	ret.BitLength = (len(bytes)-1)*8 - paddingBits
227
	ret.Bytes = bytes[1:]
228
	return
229
}
230

231
// ---------------------------------------------------------------------
232

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

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

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

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