mirror of
https://github.com/zrepl/zrepl.git
synced 2025-08-09 07:05:47 +02:00
chunking: rewrite to handle EOF events correctly
bonus: some tests asserting the chunking protocol is adhered to
This commit is contained in:
73
util/chunking_test.go
Normal file
73
util/chunking_test.go
Normal file
@ -0,0 +1,73 @@
|
||||
package chunking
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/binary"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"io"
|
||||
"reflect"
|
||||
"testing"
|
||||
"testing/quick"
|
||||
)
|
||||
|
||||
func TestUnchunker(t *testing.T) {
|
||||
|
||||
buf := bytes.Buffer{}
|
||||
binary.Write(&buf, ChunkHeaderByteOrder, uint32(2))
|
||||
buf.WriteByte(0xca)
|
||||
buf.WriteByte(0xfe)
|
||||
binary.Write(&buf, ChunkHeaderByteOrder, uint32(0))
|
||||
buf.WriteByte(0xff) // sentinel, should not be read
|
||||
|
||||
un := NewUnchunker(&buf)
|
||||
|
||||
recv := bytes.Buffer{}
|
||||
n, err := io.Copy(&recv, un)
|
||||
assert.Nil(t, err)
|
||||
assert.Equal(t, int64(2), n)
|
||||
assert.Equal(t, []byte{0xca, 0xfe}, recv.Bytes())
|
||||
|
||||
}
|
||||
|
||||
func TestChunker(t *testing.T) {
|
||||
|
||||
buf := bytes.Buffer{}
|
||||
buf.WriteByte(0xca)
|
||||
buf.WriteByte(0xfe)
|
||||
|
||||
ch := NewChunker(&buf)
|
||||
|
||||
chunked := bytes.Buffer{}
|
||||
n, err := io.Copy(&chunked, &ch)
|
||||
assert.Nil(t, err)
|
||||
assert.Equal(t, int64(4+2+4), n)
|
||||
assert.Equal(t, []byte{0x2, 0x0, 0x0, 0x0, 0xca, 0xfe, 0x0, 0x0, 0x0, 0x0}, chunked.Bytes())
|
||||
|
||||
}
|
||||
|
||||
func TestUnchunkerUnchunksChunker(t *testing.T) {
|
||||
|
||||
f := func(b []byte) bool {
|
||||
|
||||
buf := bytes.NewBuffer(b)
|
||||
ch := NewChunker(buf)
|
||||
unch := NewUnchunker(&ch)
|
||||
var tx bytes.Buffer
|
||||
_, err := io.Copy(&tx, unch)
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
|
||||
return reflect.DeepEqual(b, tx.Bytes())
|
||||
}
|
||||
|
||||
cfg := quick.Config{
|
||||
MaxCount: 3 * int(ChunkBufSize),
|
||||
MaxCountScale: 2.0,
|
||||
}
|
||||
|
||||
if err := quick.Check(f, &cfg); err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
|
||||
}
|
Reference in New Issue
Block a user