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
|
package ber
import (
"bytes"
"io"
"math"
"testing"
)
func TestReadLength(t *testing.T) {
testcases := map[string]struct {
Data []byte
ExpectedLength int
ExpectedBytesRead int
ExpectedError string
}{
"empty": {
Data: []byte{},
ExpectedBytesRead: 0,
ExpectedError: io.ErrUnexpectedEOF.Error(),
},
"invalid first byte": {
Data: []byte{0xFF},
ExpectedBytesRead: 1,
ExpectedError: "invalid length byte 0xff",
},
"indefinite form": {
Data: []byte{LengthLongFormBitmask},
ExpectedLength: LengthIndefinite,
ExpectedBytesRead: 1,
},
"short-definite-form zero length": {
Data: []byte{0},
ExpectedLength: 0,
ExpectedBytesRead: 1,
},
"short-definite-form length 1": {
Data: []byte{1},
ExpectedLength: 1,
ExpectedBytesRead: 1,
},
"short-definite-form max length": {
Data: []byte{127},
ExpectedLength: 127,
ExpectedBytesRead: 1,
},
"long-definite-form missing bytes": {
Data: []byte{LengthLongFormBitmask | 1},
ExpectedBytesRead: 1,
ExpectedError: io.ErrUnexpectedEOF.Error(),
},
"long-definite-form overflow": {
Data: []byte{LengthLongFormBitmask | 9},
ExpectedBytesRead: 1,
ExpectedError: "long-form length overflow",
},
"long-definite-form zero length": {
Data: []byte{LengthLongFormBitmask | 1, 0x0},
ExpectedLength: 0,
ExpectedBytesRead: 2,
},
"long-definite-form length 127": {
Data: []byte{LengthLongFormBitmask | 1, 127},
ExpectedLength: 127,
ExpectedBytesRead: 2,
},
"long-definite-form max length": {
Data: []byte{
LengthLongFormBitmask | 8,
0x7F,
0xFF,
0xFF,
0xFF,
0xFF,
0xFF,
0xFF,
0xFF,
},
ExpectedLength: math.MaxInt64,
ExpectedBytesRead: 9,
},
}
for k, tc := range testcases {
reader := bytes.NewBuffer(tc.Data)
length, read, err := readLength(reader)
if err != nil {
if tc.ExpectedError == "" {
t.Errorf("%s: unexpected error: %v", k, err)
} else if err.Error() != tc.ExpectedError {
t.Errorf("%s: expected error %v, got %v", k, tc.ExpectedError, err)
}
} else if tc.ExpectedError != "" {
t.Errorf("%s: expected error %v, got none", k, tc.ExpectedError)
continue
}
if read != tc.ExpectedBytesRead {
t.Errorf("%s: expected read %d, got %d", k, tc.ExpectedBytesRead, read)
}
if length != tc.ExpectedLength {
t.Errorf("%s: expected length %d, got %d", k, tc.ExpectedLength, length)
}
}
}
func TestEncodeLength(t *testing.T) {
testcases := map[string]struct {
Length int
ExpectedBytes []byte
}{
"0": {
Length: 0,
ExpectedBytes: []byte{0},
},
"1": {
Length: 1,
ExpectedBytes: []byte{1},
},
"max short-form length": {
Length: 127,
ExpectedBytes: []byte{127},
},
"min long-form length": {
Length: 128,
ExpectedBytes: []byte{LengthLongFormBitmask | 1, 128},
},
"max long-form length": {
Length: math.MaxInt64,
ExpectedBytes: []byte{
LengthLongFormBitmask | 8,
0x7F,
0xFF,
0xFF,
0xFF,
0xFF,
0xFF,
0xFF,
0xFF,
},
},
}
for k, tc := range testcases {
b := encodeLength(tc.Length)
if bytes.Compare(tc.ExpectedBytes, b) != 0 {
t.Errorf("%s: Expected\n\t%#v\ngot\n\t%#v", k, tc.ExpectedBytes, b)
}
}
}
|