-
Notifications
You must be signed in to change notification settings - Fork 0
/
chunk_test.go
197 lines (182 loc) · 5.23 KB
/
chunk_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
package zenc_test
import (
"bytes"
"encoding/binary"
"io"
"testing"
"unsafe"
"github.com/goasm/zenc"
)
const chunkInfoSize = int(unsafe.Sizeof(zenc.ChunkInfo{}))
var rawData []byte
func init() {
rawData = make([]byte, 10000)
fillSequence(rawData)
}
func fillSequence(buf []byte) {
for i := 0; i < len(buf); i++ {
buf[i] = byte(i)
}
}
func getSampleChunkData(limit int) *bytes.Buffer {
result := new(bytes.Buffer)
cs := zenc.NewChunkStage()
cs.SetNext(zenc.NewDestStage(result))
io.Copy(cs, io.LimitReader(bytes.NewReader(rawData), int64(limit)))
cs.Close()
return result
}
func putSampleChunkData(data *bytes.Buffer) *bytes.Buffer {
result := new(bytes.Buffer)
ds := zenc.NewDechunkStage()
ds.SetNext(zenc.NewSourceStage(data))
io.Copy(result, ds)
return result
}
func TestWriteSingleChunk(t *testing.T) {
total := 100
out := getSampleChunkData(total)
if out.Len() != total+chunkInfoSize*2 {
t.Fatal("ChunkStage writes a wrong total length", out.Len())
}
size1 := int(binary.LittleEndian.Uint32(out.Next(chunkInfoSize)))
if size1 != 100 {
t.Fatal("ChunkStage writes a wrong chunk length", size1)
}
chunk1 := out.Next(100)
if !bytes.Equal(chunk1, rawData[:100]) {
t.Fatal("ChunkStage writes mismatched chunk data")
}
size2 := int(binary.LittleEndian.Uint32(out.Next(chunkInfoSize)))
if size2 != 0 {
t.Fatal("ChunkStage writes a wrong chunk length", size2)
}
}
func TestWriteMultipleChunks(t *testing.T) {
total := 9000
out := getSampleChunkData(total)
if out.Len() != total+chunkInfoSize*4 {
t.Fatal("ChunkStage writes a wrong total length", out.Len())
}
size1 := int(binary.LittleEndian.Uint32(out.Next(chunkInfoSize)))
if size1 != 4096 {
t.Fatal("ChunkStage writes a wrong chunk length", size1)
}
chunk1 := out.Next(4096)
if !bytes.Equal(chunk1, rawData[:4096]) {
t.Fatal("ChunkStage writes mismatched chunk data")
}
size2 := int(binary.LittleEndian.Uint32(out.Next(chunkInfoSize)))
if size2 != 4096 {
t.Fatal("ChunkStage writes a wrong chunk length", size2)
}
chunk2 := out.Next(4096)
if !bytes.Equal(chunk2, rawData[4096:8192]) {
t.Fatal("ChunkStage writes mismatched chunk data")
}
size3 := int(binary.LittleEndian.Uint32(out.Next(chunkInfoSize)))
if size3 != 808 {
t.Fatal("ChunkStage writes a wrong chunk length", size3)
}
chunk3 := out.Next(808)
if !bytes.Equal(chunk3, rawData[8192:9000]) {
t.Fatal("ChunkStage writes mismatched chunk data")
}
size4 := int(binary.LittleEndian.Uint32(out.Next(chunkInfoSize)))
if size4 != 0 {
t.Fatal("ChunkStage writes a wrong chunk length", size4)
}
}
func TestWriteFullChunk(t *testing.T) {
total := 8192
out := getSampleChunkData(total)
if out.Len() != total+chunkInfoSize*3 {
t.Fatal("ChunkStage writes a wrong total length", out.Len())
}
size1 := int(binary.LittleEndian.Uint32(out.Next(chunkInfoSize)))
if size1 != 4096 {
t.Fatal("ChunkStage writes a wrong chunk length", size1)
}
chunk1 := out.Next(4096)
if !bytes.Equal(chunk1, rawData[:4096]) {
t.Fatal("ChunkStage writes mismatched chunk data")
}
size2 := int(binary.LittleEndian.Uint32(out.Next(chunkInfoSize)))
if size2 != 4096 {
t.Fatal("ChunkStage writes a wrong chunk length", size2)
}
chunk2 := out.Next(4096)
if !bytes.Equal(chunk2, rawData[4096:8192]) {
t.Fatal("ChunkStage writes mismatched chunk data")
}
size3 := int(binary.LittleEndian.Uint32(out.Next(chunkInfoSize)))
if size3 != 0 {
t.Fatal("ChunkStage writes a wrong chunk length", size3)
}
}
func TestReadSingleChunk(t *testing.T) {
total := 100
tmp := getSampleChunkData(total)
out := putSampleChunkData(tmp)
if out.Len() != total {
t.Fatal("ChunkStage writes a wrong total length", out.Len())
}
chunk1 := out.Next(total)
if !bytes.Equal(chunk1, rawData[:total]) {
t.Fatal("ChunkStage writes mismatched chunk data")
}
if tmp.Len() != 0 {
t.Fatal("ChunkStage reads wrong length of data")
}
}
func TestReadMultipleChunks(t *testing.T) {
total := 9000
tmp := getSampleChunkData(total)
out := putSampleChunkData(tmp)
if out.Len() != total {
t.Fatal("ChunkStage writes a wrong total length", out.Len())
}
chunk1 := out.Next(total)
if !bytes.Equal(chunk1, rawData[:total]) {
t.Fatal("ChunkStage writes mismatched chunk data")
}
if tmp.Len() != 0 {
t.Fatal("ChunkStage reads wrong length of data")
}
}
func TestReadInputSize(t *testing.T) {
total := 5000
tmp := getSampleChunkData(total)
end := []byte{0x00, 0x01, 0x02, 0x03}
tmp.Write(end)
out := putSampleChunkData(tmp)
if out.Len() != total {
t.Fatal("ChunkStage writes a wrong total length", out.Len())
}
chunk1 := out.Next(total)
if !bytes.Equal(chunk1, rawData[:total]) {
t.Fatal("ChunkStage writes mismatched chunk data")
}
if tmp.Len() != len(end) {
t.Fatal("ChunkStage reads wrong length of data")
}
if !bytes.Equal(tmp.Bytes(), end) {
t.Fatal("ChunkStage reads mismatched data")
}
}
func TestReadIncompleteInput(t *testing.T) {
total := 100
tmp := getSampleChunkData(total)
tmp.Truncate(tmp.Len() - chunkInfoSize)
out := putSampleChunkData(tmp)
if out.Len() != total {
t.Fatal("ChunkStage writes a wrong total length", out.Len())
}
chunk1 := out.Next(total)
if !bytes.Equal(chunk1, rawData[:total]) {
t.Fatal("ChunkStage writes mismatched chunk data")
}
if tmp.Len() != 0 {
t.Fatal("ChunkStage reads wrong length of data")
}
}