minio/pkg/storage/donut/v1/donut_test.go

133 lines
3.7 KiB
Go
Raw Normal View History

2015-02-18 22:46:01 -05:00
/*
* Mini Object Storage, (C) 2015 Minio, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package v1
2015-02-18 22:46:01 -05:00
import (
"bytes"
2015-02-28 18:53:55 -05:00
"crypto/sha512"
"encoding/binary"
2015-02-18 22:46:01 -05:00
"testing"
2015-02-28 18:53:55 -05:00
"github.com/minio-io/minio/pkg/utils/checksum/crc32c"
. "gopkg.in/check.v1"
2015-02-18 22:46:01 -05:00
)
func Test(t *testing.T) { TestingT(t) }
type MySuite struct{}
var _ = Suite(&MySuite{})
2015-02-27 15:39:52 -05:00
func (s *MySuite) TestSingleWrite(c *C) {
2015-02-27 15:18:08 -05:00
//var b io.ReadWriteSeeker
2015-02-28 18:53:55 -05:00
var testBuffer bytes.Buffer
2015-02-18 22:46:01 -05:00
2015-02-28 18:53:55 -05:00
testData := "Hello, World"
testLength := uint64(len(testData))
err := Write(&testBuffer, bytes.NewBufferString(testData), testLength)
c.Assert(err, IsNil)
2015-02-28 18:53:55 -05:00
testBufferLength := uint64(testBuffer.Len())
// we test our crc here too
headerBytes := testBuffer.Bytes()[0:28]
expectedCrc := crc32c.Sum32(headerBytes)
// magic mini
magicMini := make([]byte, 4)
testBuffer.Read(magicMini)
c.Assert(magicMini, DeepEquals, []byte{'M', 'I', 'N', 'I'})
// 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)
2015-02-18 22:46:01 -05:00
}
func (s *MySuite) TestLengthMismatchInWrite(c *C) {
var testData bytes.Buffer
err := Write(&testData, bytes.NewBufferString("hello, world"), 5)
c.Assert(err, Not(IsNil))
}