forked from gosnmp/gosnmp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
misc_test.go
154 lines (133 loc) · 3.77 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
// Copyright 2012-2018 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"
"reflect"
"testing"
)
// 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 {
result := md5HMAC(test.password, test.engineid)
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 {
result := shaHMAC(test.password, test.engineid)
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)
}
}
}
*/