forked from reguero/go-snmplib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ber_test.go
113 lines (101 loc) · 3.52 KB
/
ber_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
package snmplib
import (
"encoding/hex"
"reflect"
"testing"
)
type LengthTest struct {
Encoded []byte
Length int
LengthLength int // This is the length of the encoded length, as derived from the encoded value
}
func TestLengthDecodingEncoding(t *testing.T) {
tests := []LengthTest{
LengthTest{[]byte{0x26}, 38, 1},
LengthTest{[]byte{0x82,0x00, 0xc9}, 201, 3},
LengthTest{[]byte{0x82,0x00, 0xca}, 202, 3},
LengthTest{[]byte{0x82,0x00, 0x9f}, 159, 3},
LengthTest{[]byte{0x82,0x01, 0x70}, 368, 3},
LengthTest{[]byte{0x82,0x00, 0xe3}, 227, 3},
}
for _, test := range tests {
length, lenLength, err := DecodeLength(test.Encoded)
if length != test.Length || lenLength != test.LengthLength || err != nil {
t.Errorf("Failed to decode %v, expected (%v, %v), result (%v, %v) err: %v", hex.EncodeToString(test.Encoded), test.Length, test.LengthLength, length, lenLength, err)
continue
}
// Re-encode
bytes := EncodeLength(test.Length)
if !reflect.DeepEqual(bytes, test.Encoded) {
t.Errorf("Length not encoded as expected. Length : %v\nExpected: %v\nResult : %v", test.Length, hex.EncodeToString(test.Encoded), hex.EncodeToString(bytes))
}
}
}
func TestDecodeEncodeInteger(t *testing.T) {
tests := map[int][]byte{
3: []byte{0x03},
523: []byte{0x02, 0x0b},
1191105458: []byte{0x46, 0xfe, 0xd3, 0xb2},
}
for testValue, testEncode := range tests {
encode := EncodeInteger(testValue)
if !reflect.DeepEqual(testEncode, encode) {
t.Errorf("Failed to encode %v. EncodeInteger => %v Expected %v", testValue, hex.EncodeToString(encode), hex.EncodeToString(testEncode))
continue
}
value, err := DecodeInteger(testEncode)
if err != nil {
t.Errorf("Decoding %v failed. Err = %v", hex.EncodeToString(testEncode), err)
continue
}
if value != testValue {
t.Errorf("Decoding %v gave wrong result. Result => %v Expected => %v", hex.EncodeToString(testEncode), value, testValue)
continue
}
}
}
type SequenceTest struct {
Encoded string
Decoded []interface{}
}
func TestSequenceDecoding(t *testing.T) {
SequenceTests := []SequenceTest{
SequenceTest{"3003020100", []interface{}{Sequence, 0}},
SequenceTest{"300804067075626c6963", []interface{}{Sequence, "public"}},
SequenceTest{"300b04067075626c6963020100", []interface{}{Sequence, "public", 0}},
}
for _, test := range SequenceTests {
encodedBytes, err := hex.DecodeString(test.Encoded)
if err != nil {
t.Fatalf("Error when decoding hex %s", encodedBytes)
}
result, err := DecodeSequence(encodedBytes)
if err != nil {
t.Fatalf("Error while decoding %v => %v", hex.EncodeToString(encodedBytes), err)
}
if !reflect.DeepEqual(result, test.Decoded) {
t.Fatalf("Not decoded as expected. Encoded : %v\nExpected: %v\nResult : %v", hex.EncodeToString(encodedBytes), test.Decoded, result)
}
}
for _, test := range SequenceTests {
encodedBytes, err := hex.DecodeString(test.Encoded)
if err != nil {
t.Fatalf("Error when decoding hex %s", encodedBytes)
}
result, err := EncodeSequence(test.Decoded)
if err != nil {
t.Fatalf("Error while encoding %v => %v", test.Decoded, err)
}
if !reflect.DeepEqual(result, encodedBytes) {
here := ""
for idx := 0; idx < len(result) && idx < len(encodedBytes); idx++ {
if (result)[idx] != encodedBytes[idx] {
break
}
here += " "
}
here += "^ first difference"
t.Errorf("Not encoded as expected. Decoded : %v\nExpected: %v\nResult : %v\n %v", test.Decoded, hex.EncodeToString(encodedBytes), hex.EncodeToString(result), here)
}
}
}