forked from rosspeoples/go-snmplib
-
Notifications
You must be signed in to change notification settings - Fork 1
/
oid_test.go
80 lines (71 loc) · 2.18 KB
/
oid_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
package snmplib
import (
"fmt"
"testing"
)
type ParseOidTest struct {
ToParse string
ExpectedCanonForm string
ExpectFail bool
}
func TestParseOid(t *testing.T) {
tests := []ParseOidTest{
ParseOidTest{"1.3.6.1.4.1.2636.3.2.3.1.20", ".1.3.6.1.4.1.2636.3.2.3.1.20", false},
ParseOidTest{".1.3.127.128", ".1.3.127.128", false},
ParseOidTest{"1.3", ".1.3", false},
ParseOidTest{".1.3.127.128.129", ".1.3.127.128.129", false},
ParseOidTest{"", ".", false},
ParseOidTest{".", ".", false},
ParseOidTest{"Donald Duck", "", true},
}
for _, test := range tests {
oid, err := ParseOid(test.ToParse)
if (err != nil) != test.ExpectFail {
t.Errorf("ParseOid '%s' got error '%s', expected '%s'", test.ToParse, err, test.ExpectFail)
}
if !test.ExpectFail {
if fmt.Sprintf("%s", oid) != test.ExpectedCanonForm {
t.Errorf("ParseOid '%s' got '%s', expected '%s'", test.ToParse, oid, test.ExpectedCanonForm)
}
}
}
}
func TestOidEncode(t *testing.T) {
encodeTest := map[string][]byte{
"1.3.6.1.4.1.2636.3.2.3.1.20": []byte{0x2b, 0x06, 0x01, 0x04, 0x01, 0x94, 0x4c, 0x03, 0x02, 0x03, 0x01, 0x14},
"1.3.6.1.2.1.1.5.0": []byte{0x2b, 0x06, 0x01, 0x02, 0x01, 0x01, 0x05, 0x00},
}
for oidString, expected := range encodeTest {
oid, err := ParseOid(oidString)
if err != nil {
t.Errorf("ParseOid '%s' error '%s'", oidString, err)
}
encode, err := oid.Encode()
equal := len(encode) == len(expected)
if equal {
for idx, val := range encode {
if val != expected[idx] {
equal = false
}
}
}
if !equal {
t.Errorf("ParseOid '%s' expected '%v', got '%v'\n", oidString, expected, encode)
}
}
}
func TestOidDecode(t *testing.T) {
encodedOid := []byte{0x2b, 0x06, 0x01, 0x04, 0x01, 0x94, 0x4c, 0x03, 0x02, 0x03, 0x01, 0x14}
oid, err := DecodeOid(encodedOid)
if err != nil {
t.Errorf("DecodeOid '1.3.6.1.4.1.2636.3.2.3.1.20' error '%s'", err)
}
if fmt.Sprintf("%s", oid) != ".1.3.6.1.4.1.2636.3.2.3.1.20" {
t.Errorf("DecodeOid expected '1.3.6.1.4.1.2636.3.2.3.1.20', got '%s'", fmt.Sprintf("%s", oid))
}
}
func TestWithin(t *testing.T) {
if !MustParseOid("1.2.3").Within(MustParseOid("1.2")) {
t.Errorf("Within is not working")
}
}