forked from dtn7/cboring
-
Notifications
You must be signed in to change notification settings - Fork 0
/
major_test.go
200 lines (170 loc) · 4.5 KB
/
major_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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
package cboring
import (
"bufio"
"bytes"
"math/rand"
"reflect"
"testing"
)
func TestReadMajorsSmall(t *testing.T) {
tests := []MajorType{UInt, ByteString, TextString, Array}
for _, test := range tests {
for i := uint64(0); i <= 23; i++ {
r := bytes.NewBuffer([]byte{test | byte(i)})
if m, n, err := ReadMajors(r); err != nil {
t.Fatal(err)
} else if m != test {
t.Fatalf("Resulting type %d mismatches %d", m, test)
} else if n != i {
t.Fatalf("Resulting uint %d is not %d", n, i)
}
}
}
}
func TestReadMajorsBig(t *testing.T) {
tests := []struct {
data []byte
major MajorType
numb uint64
}{
{[]byte{0x18, 0x64}, UInt, 100},
{[]byte{0x58, 0x40}, ByteString, 64},
{[]byte{0x78, 0x20}, TextString, 32},
{[]byte{0x98, 0x19}, Array, 25},
}
for _, test := range tests {
r := bytes.NewBuffer(test.data)
if m, n, err := ReadMajors(r); err != nil {
t.Fatal(err)
} else if m != test.major {
t.Fatalf("Resulting type %d mismatches %d", m, test.major)
} else if n != test.numb {
t.Fatalf("Resulting uint %d is not %d", n, test.numb)
}
}
}
func TestReadMajorsError(t *testing.T) {
tests := [][]byte{
// Empty stream
{},
// Incomplete streams
{0x18}, {0x19, 0x03},
}
for _, test := range tests {
r := bytes.NewBuffer(test)
if _, _, err := ReadMajors(r); err == nil {
t.Fatalf("Illegal input %x did not errored", test)
}
}
}
func TestWriteMajorsSmall(t *testing.T) {
tests := []MajorType{UInt, ByteString, TextString, Array}
for _, test := range tests {
for i := uint64(0); i <= 23; i++ {
var buff bytes.Buffer
if err := WriteMajors(test, i, &buff); err != nil {
t.Fatal(err)
}
if m, n, err := ReadMajors(&buff); err != nil {
t.Fatal(err)
} else if m != test {
t.Fatalf("Resulting type %d mismatches %d", m, test)
} else if n != i {
t.Fatalf("Resulting uint %d is not %d", n, i)
}
}
}
}
func TestWriteMajorsBig(t *testing.T) {
tests := []struct {
data []byte
major MajorType
numb uint64
}{
{[]byte{0x18, 0x64}, UInt, 100},
{[]byte{0x58, 0x40}, ByteString, 64},
{[]byte{0x78, 0x20}, TextString, 32},
{[]byte{0x98, 0x19}, Array, 25},
}
for _, test := range tests {
var buff bytes.Buffer
if err := WriteMajors(test.major, test.numb, &buff); err != nil {
t.Fatal(err)
}
if bb := buff.Bytes(); !reflect.DeepEqual(bb, test.data) {
t.Fatalf("Serialized data mismatches: %x != %x", bb, test.data)
}
}
}
func TestReadExpect(t *testing.T) {
var buff bytes.Buffer
for i := byte(0); i < 255; i++ {
// Read correct input
buff.Reset()
buff.WriteByte(i)
if err := ReadExpect(i, &buff); err != nil {
t.Fatalf("ReadExpect errored for %d: %v", i, err)
}
// Read invalid input
buff.Reset()
buff.WriteByte(i + 1)
if err := ReadExpect(i, &buff); err == nil {
t.Fatalf("ReadExpect did not errored for %d", i)
}
}
}
func TestReadExampleArray(t *testing.T) {
var data = []byte{
0x9f, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b,
0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
0x18, 0x18, 0x18, 0x19, 0xff}
var buff = bytes.NewBuffer(data)
// Should start with an indefinite length array
if err := ReadExpect(IndefiniteArray, buff); err != nil {
t.Fatalf("Data does not start with an indefinite length array: %v", err)
}
// Read numbers until break stop code (should be 1..25)
for c := uint64(1); ; c++ {
n, err := ReadUInt(buff)
if err != nil && err != FlagBreakCode {
t.Fatal(FlagBreakCode)
} else if err == FlagBreakCode {
if c != 26 {
t.Fatalf("Break stop code appeared at %d, not at %d", c, 26)
}
break
} else if c > 25 {
t.Fatalf("Reached %d, which is greater than %d", c, 25)
} else if c != n {
t.Fatalf("Read %d, not %d", n, c)
}
}
}
func TestReadBigData(t *testing.T) {
var size = 1024
var payload = make([]byte, size)
rand.Seed(0)
rand.Read(payload)
var elems = []int{100, 500, 1000, 5000, 10000, 50000}
for _, elem := range elems {
buff := new(bytes.Buffer)
bw := bufio.NewWriter(buff)
for i := 0; i < elem; i++ {
if err := WriteByteString(payload, bw); err != nil {
t.Fatalf("Writing no %d errored: %v", i, err)
}
}
if err := bw.Flush(); err != nil {
t.Fatalf("Flushing errored: %v", err)
}
br := bufio.NewReader(buff)
for i := 0; i < elem; i++ {
tmp, err := ReadByteString(br)
if err != nil {
t.Fatalf("Reading no %d errored: %v", i, err)
} else if len(tmp) != size {
t.Fatalf("Length no %d mismatches: %d != %d", i, len(tmp), size)
}
}
}
}