forked from gosnmp/gosnmp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhelper_test.go
165 lines (139 loc) · 6.21 KB
/
helper_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
// 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 helper
package gosnmp
import (
"encoding/base64"
"testing"
"github.com/stretchr/testify/assert"
)
// https://www.scadacore.com/tools/programming-calculators/online-hex-converter/ is useful
func TestOidToString(t *testing.T) {
oid := []int{1, 2, 3, 4, 5}
expected := ".1.2.3.4.5"
result := oidToString(oid)
if result != expected {
t.Errorf("oidToString(%v) = %s, want %s", oid, result, expected)
}
}
func TestWithAnotherOid(t *testing.T) {
oid := []int{4, 3, 2, 1, 3}
expected := ".4.3.2.1.3"
result := oidToString(oid)
if result != expected {
t.Errorf("oidToString(%v) = %s, want %s", oid, result, expected)
}
}
func BenchmarkOidToString(b *testing.B) {
oid := []int{1, 2, 3, 4, 5}
for i := 0; i < b.N; i++ {
oidToString(oid)
}
}
type testsMarshalUint32T struct {
value uint32
goodBytes []byte
}
var testsMarshalUint32 = []testsMarshalUint32T{
{0, []byte{0x00}},
{2, []byte{0x02}}, // 2
{128, []byte{0x00, 0x80}},
{257, []byte{0x01, 0x01}}, // FF + 2
{65537, []byte{0x01, 0x00, 0x01}}, // FFFF + 2
{16777217, []byte{0x01, 0x00, 0x00, 0x01}}, // FFFFFF + 2
{18542501, []byte{0x01, 0x1a, 0xef, 0xa5}},
}
func TestMarshalUint32(t *testing.T) {
for i, test := range testsMarshalUint32 {
result, err := marshalUint32(test.value)
if err != nil {
t.Errorf("%d: expected %0x got err %v", i, test.goodBytes, err)
}
if !checkByteEquality2(test.goodBytes, result) {
t.Errorf("%d: expected %0x got %0x", i, test.goodBytes, result)
}
}
}
var testsMarshalInt32 = []struct {
value int
goodBytes []byte
}{
{0, []byte{0x00}},
{2, []byte{0x02}}, // 2
{128, []byte{0x00, 0x80}},
{257, []byte{0x01, 0x01}}, // FF + 2
{65537, []byte{0x01, 0x00, 0x01}}, // FFFF + 2
{16777217, []byte{0x01, 0x00, 0x00, 0x01}}, // FFFFFF + 2
{2147483647, []byte{0x7f, 0xff, 0xff, 0xff}},
{-2147483648, []byte{0x80, 0x00, 0x00, 0x00}},
{-16777217, []byte{0xfe, 0xff, 0xff, 0xff}},
{-16777216, []byte{0xff, 0x00, 0x00, 0x00}},
{-65537, []byte{0xff, 0xfe, 0xff, 0xff}},
{-65536, []byte{0xff, 0xff, 0x00, 0x00}},
{-257, []byte{0xff, 0xff, 0xfe, 0xff}},
{-256, []byte{0xff, 0xff, 0xff, 0x00}},
{-2, []byte{0xff, 0xff, 0xff, 0xfe}},
{-1, []byte{0xff, 0xff, 0xff, 0xff}},
}
func TestMarshalInt32(t *testing.T) {
for _, aTest := range testsMarshalInt32 {
result, err := marshalInt32(aTest.value)
assert.NoErrorf(t, err, "value %d", aTest.value)
assert.EqualValues(t, aTest.goodBytes, result, "bad marshalInt32()")
}
}
func TestParseUint64(t *testing.T) {
tests := []struct {
data []byte
n uint64
}{
{[]byte{}, 0},
{[]byte{0x00}, 0},
{[]byte{0x01}, 1},
{[]byte{0x01, 0x01}, 257},
{[]byte{0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0x1e, 0xb3, 0xbf}, 18446744073694786495},
}
for _, test := range tests {
if ret, err := parseUint64(test.data); err != nil || ret != test.n {
t.Errorf("parseUint64(%v) = %d, %v want %d, <nil>", test.data, ret, err, test.n)
}
}
}
var testsInvalidSNMPResponses = []string{
"MIIHIQIBAQQHcHJpdmF0ZaKCBxECBGwvRyoCAQACAQAwggcBMBgGCCsGAQIBAQIABgwrBgEEAZJRAwE/AQYwEAYIKwYBAgEBAwBDBBU2aN0wDAYIKwYBAgEBBAAEADAMBggrBgECAQEFAAQAMAwGCCsGAQIBAQYABAAwDQYIKwYBAgEBBwACAUgwDQYIKwYBAgECAQACAQAwDwYKKwYBAgECAgEBAQIBATAPBgorBgECAQICAQcBAgEBMA8GCisGAQIBAgIBBwECAQEwDwYKKwYBAgECAgEHAQIBATAPBgorBgECAQICAQcBAgEBMA8GCisGAQIBAgIBBwECAQEwDwYKKwYBAgECAgEHAQIBATAPBgorBgECAQICAQcBAgEBMBAGCCsGAQIBAQMAQwQVNmjdMAwGCCsGAQIBAQQABAAwDAYIKwYBAgEBBQAEADAMBggrBgECAQEGAAQAMA0GCCsGAQIBAQcAAgFIMA0GCCsGAQIBAgEAAgEAMA8GCisGAQIBAgIBAQECAQEwFgYKKwYBAgECAgECAQQIRXRoZXJuZXQwDwYKKwYBAgECAgEIAQIBATAPBgorBgECAQICAQgBAgEBMA8GCisGAQIBAgIBCAECAQEwDwYKKwYBAgECAgEIAQIBATAPBgorBgECAQICAQgBAgEBMA8GCisGAQIBAgIBCAECAQEwDwYKKwYBAgECAgEIAQIBATAMBggrBgECAQEEAAQAMAwGCCsGAQIBAQUABAAwDAYIKwYBAgEBBgAEADANBggrBgECAQEHAAIBSDANBggrBgECAQIBAAIBADAPBgorBgECAQICAQEBAgEBMBYGCisGAQIBAgIBAgEECEV0aGVybmV0MA8GCisGAQIBAgIBAwECAQYwDwYKKwYBAgECAgEJAUMBADAPBgorBgECAQICAQkBQwEAMA8GCisGAQIBAgIBCQFDAQAwDwYKKwYBAgECAgEJAUMBADAPBgorBgECAQICAQkBQwEAMA8GCisGAQIBAg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gECAgEMAUEEALuXKDASBgorBgECAQICAQwBQQQAu5coMA0GCCsGAQIBAgEAAgEAMA8GCisGAQIBAgIBAQECAQEwFgYKKwYBAgECAgECAQQIRXRoZXJuZXQwDwYKKwYBAgECAgEDAQIBBjAQBgorBgECAQICAQQBAgIF6jASBgorBgECAQICAQUBQgQ7msoAMBQGCisGAQIBAgIBBgEEBryxgWZeBTAPBgorBgECAQICAQcBAgEBMBEGCisGAQIBAgIBDQFBAwdNFzARBgorBgECAQICAQ0BQQMHTRcwEQYKKwYBAgECAgE=",
"MBoCAQEEB3ByaXZhdGWiDAIESESkywIBBQIBAA==",
"MEo=",
}
func TestInvalidSNMPResponses(t *testing.T) {
g := &GoSNMP{
Target: "127.0.0.1",
Port: 161,
Community: "public",
Version: Version2c,
}
for i, test := range testsInvalidSNMPResponses {
testBytes, _ := base64.StdEncoding.DecodeString(test)
result, err := g.SnmpDecodePacket(testBytes)
if err == nil {
t.Errorf("#%d, failed to error %v", i, result)
}
}
}
func checkByteEquality2(a, b []byte) bool {
if a == nil && b == nil {
return true
}
if a == nil || b == nil {
return false
}
if len(a) != len(b) {
return false
}
for i := range a {
if a[i] != b[i] {
return false
}
}
return true
}