From ddc7cf835efd435ffda134e633f41303bc306296 Mon Sep 17 00:00:00 2001 From: "Frederick F. Kautz IV" Date: Sat, 28 Feb 2015 16:09:52 -0800 Subject: [PATCH] Donut now tests amount written should match expected amount to write --- pkg/storage/donut/v1/donut.go | 6 +++++- pkg/storage/donut/v1/donut_test.go | 6 ++++++ 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/pkg/storage/donut/v1/donut.go b/pkg/storage/donut/v1/donut.go index 03ce85962..4ddf4a994 100644 --- a/pkg/storage/donut/v1/donut.go +++ b/pkg/storage/donut/v1/donut.go @@ -19,6 +19,7 @@ package v1 import ( "bytes" "encoding/binary" + "errors" "io" "github.com/minio-io/minio/pkg/utils/checksum/crc32c" @@ -101,10 +102,13 @@ func Write(target io.Writer, reader io.Reader, length uint64) error { checksumChannel := make(chan checksumValue) go generateChecksum(sumReader, checksumChannel) teeReader := io.TeeReader(reader, sumWriter) - _, err = io.Copy(target, teeReader) + dataLength, err := io.Copy(target, teeReader) if err != nil { return err } + if uint64(dataLength) != length { + return errors.New("Specified data length and amount written mismatched") + } sumWriter.Close() dataChecksum := <-checksumChannel if dataChecksum.err != nil { diff --git a/pkg/storage/donut/v1/donut_test.go b/pkg/storage/donut/v1/donut_test.go index 7a07a12a5..dd4c40c1e 100644 --- a/pkg/storage/donut/v1/donut_test.go +++ b/pkg/storage/donut/v1/donut_test.go @@ -124,3 +124,9 @@ func (s *MySuite) TestSingleWrite(c *C) { // ensure no extra data is in the file c.Assert(testBuffer.Len(), Equals, 0) } + +func (s *MySuite) TestLengthMismatchInWrite(c *C) { + var testData bytes.Buffer + err := Write(&testData, bytes.NewBufferString("hello, world"), 5) + c.Assert(err, Not(IsNil)) +}