From 83ea81d8c32209dc2b5be49b5272a703504bdabd Mon Sep 17 00:00:00 2001 From: Alexander Neumann Date: Thu, 2 Oct 2014 23:01:01 +0200 Subject: [PATCH] Fix special case with null byte input --- chunker/chunker.go | 16 +++++++++------- chunker/chunker_test.go | 16 ++++++++++++++-- 2 files changed, 23 insertions(+), 9 deletions(-) diff --git a/chunker/chunker.go b/chunker/chunker.go index 003db39fc..c317672c0 100644 --- a/chunker/chunker.go +++ b/chunker/chunker.go @@ -154,13 +154,15 @@ func (c *chunker) Next() (*Chunk, error) { if err == io.EOF && !c.closed { c.closed = true - // return current chunk - return &Chunk{ - Start: c.start, - Length: c.count, - Cut: c.digest, - Data: c.data, - }, nil + // return current chunk, if any bytes have been processed + if c.count > 0 { + return &Chunk{ + Start: c.start, + Length: c.count, + Cut: c.digest, + Data: c.data, + }, nil + } } if err != nil { diff --git a/chunker/chunker_test.go b/chunker/chunker_test.go index 8c17a57d5..a1f735c62 100644 --- a/chunker/chunker_test.go +++ b/chunker/chunker_test.go @@ -46,6 +46,14 @@ var chunks1 = []chunk{ chunk{237392, 0x00184c5825e18636}, } +// test if nullbytes are correctly split, even if length is a multiple of MinSize. +var chunks2 = []chunk{ + chunk{chunker.MinSize, 0}, + chunk{chunker.MinSize, 0}, + chunk{chunker.MinSize, 0}, + chunk{chunker.MinSize, 0}, +} + func test_with_data(t *testing.T, chunker chunker.Chunker, chunks []chunk) { for i, chunk := range chunks { c, err := chunker.Next() @@ -105,10 +113,14 @@ func get_random(seed, count int) []byte { func TestChunker(t *testing.T) { // setup data source buf := get_random(23, 32*1024*1024) - ch := chunker.New(bytes.NewReader(buf)) - test_with_data(t, ch, chunks1) + + // setup nullbyte data source + buf = bytes.Repeat([]byte{0}, len(chunks2)*chunker.MinSize) + ch = chunker.New(bytes.NewReader(buf)) + + test_with_data(t, ch, chunks2) } func BenchmarkChunker(b *testing.B) {