New donut frame implemented

This commit is contained in:
Frederick F. Kautz IV
2015-02-28 15:53:55 -08:00
parent 9744efc725
commit 58a04ee831
3 changed files with 217 additions and 239 deletions
+88 -26
View File
@@ -18,10 +18,13 @@ package v1
import (
"bytes"
. "gopkg.in/check.v1"
"io/ioutil"
"os"
"crypto/sha512"
"encoding/binary"
"testing"
"github.com/minio-io/minio/pkg/utils/checksum/crc32c"
. "gopkg.in/check.v1"
)
func Test(t *testing.T) { TestingT(t) }
@@ -32,33 +35,92 @@ var _ = Suite(&MySuite{})
func (s *MySuite) TestSingleWrite(c *C) {
//var b io.ReadWriteSeeker
var o bytes.Buffer
var testBuffer bytes.Buffer
b, err := ioutil.TempFile(os.TempDir(), "minio-donut-test")
defer os.RemoveAll(b.Name())
testData := "Hello, World"
testLength := uint64(len(testData))
err := Write(&testBuffer, bytes.NewBufferString(testData), testLength)
c.Assert(err, IsNil)
donut := New(b)
gobheader := GobHeader{}
err = donut.Write(gobheader, &o)
c.Assert(err, IsNil)
blockStart := make([]byte, 4)
testBufferLength := uint64(testBuffer.Len())
//n, _ := b.Read(blockStart)
// b.Next(b.Len() - n) // jump ahead
// b.Read(blockEnd)
// read start
b.Seek(0, 0) // jump ahead
b.Read(blockStart)
blockStartCheck := []byte{'M', 'I', 'N', 'I'}
c.Assert(blockStart, DeepEquals, blockStartCheck)
// we test our crc here too
headerBytes := testBuffer.Bytes()[0:28]
expectedCrc := crc32c.Sum32(headerBytes)
// read block
// magic mini
magicMini := make([]byte, 4)
testBuffer.Read(magicMini)
c.Assert(magicMini, DeepEquals, []byte{'M', 'I', 'N', 'I'})
// read end
blockEnd := make([]byte, 4)
b.Seek(-int64(len(blockEnd)), 2) // jump ahead
b.Read(blockEnd)
blockEndCheck := []byte{'I', 'N', 'I', 'M'}
c.Assert(blockEnd, DeepEquals, blockEndCheck)
// major version
majorVersion := make([]byte, 2)
testBuffer.Read(majorVersion)
c.Assert(binary.LittleEndian.Uint16(majorVersion), DeepEquals, uint16(1))
// minor version
minorVersion := make([]byte, 2)
testBuffer.Read(minorVersion)
c.Assert(binary.LittleEndian.Uint16(minorVersion), DeepEquals, uint16(0))
// patch version
patchVersion := make([]byte, 2)
testBuffer.Read(patchVersion)
c.Assert(binary.LittleEndian.Uint16(patchVersion), DeepEquals, uint16(0))
// reserved version
reservedVersion := make([]byte, 2)
testBuffer.Read(reservedVersion)
c.Assert(binary.LittleEndian.Uint16(reservedVersion), DeepEquals, uint16(0))
// reserved
reserved := make([]byte, 8)
testBuffer.Read(reserved)
c.Assert(binary.LittleEndian.Uint64(reserved), DeepEquals, uint64(0))
// data length
length := make([]byte, 8)
testBuffer.Read(length)
c.Assert(binary.LittleEndian.Uint64(length), DeepEquals, testLength)
// test crc
bufCrc := make([]byte, 4)
testBuffer.Read(bufCrc)
c.Assert(binary.LittleEndian.Uint32(bufCrc), DeepEquals, expectedCrc)
// magic DATA
magicData := make([]byte, 4)
testBuffer.Read(magicData)
c.Assert(magicData, DeepEquals, []byte{'D', 'A', 'T', 'A'})
// data
actualData := make([]byte, int32(testLength))
testBuffer.Read(actualData)
c.Assert(string(actualData), DeepEquals, testData)
// extract footer crc32c
actualFooterCrc := make([]byte, 4)
testBuffer.Read(actualFooterCrc)
remainingBytes := testBuffer.Bytes()
remainingSum := crc32c.Sum32(remainingBytes)
c.Assert(binary.LittleEndian.Uint32(actualFooterCrc), DeepEquals, remainingSum)
// sha512
expectedSha512 := sha512.Sum512([]byte(testData))
actualSha512 := make([]byte, 64)
testBuffer.Read(actualSha512)
c.Assert(actualSha512, DeepEquals, expectedSha512[:])
// length
actualLength := make([]byte, 8)
testBuffer.Read(actualLength)
c.Assert(testBufferLength, DeepEquals, binary.LittleEndian.Uint64(actualLength))
// magic INIM
magicInim := make([]byte, 4)
testBuffer.Read(magicInim)
c.Assert(magicInim, DeepEquals, []byte{'I', 'N', 'I', 'M'})
// ensure no extra data is in the file
c.Assert(testBuffer.Len(), Equals, 0)
}