예제 #1
0
func TestByteOrder(t *testing.T) {
	defer test.New(t)
	s := new(Superblock)
	currentByteOrder := s.GetByteOrder()
	wrongByteOrder := LittleEndian
	if wrongByteOrder == currentByteOrder {
		wrongByteOrder = BigEndian
	}

	b := make([]byte, s.Size())
	currentByteOrder.Binary().PutUint64(b, uint64(currentByteOrder))
	currentByteOrder.Binary().PutUint64(b[8:], 1)
	test.Nil(s.Decode(b))

	test.Equal(s.Decode(b[:16]), ErrSbInvalid)
	test.Equal(s.Decode(b[:15]), ErrSbInvalidLen)

	binary.LittleEndian.PutUint64(b[8:], 0)
	test.Equal(s.Decode(b), ErrSbInvalidVersion)

	binary.LittleEndian.PutUint64(b, uint64(wrongByteOrder))
	test.Equal(s.Decode(b), ErrByteOrderNotEqual)

	LittleEndian.String()
	BigEndian.String()
	ByteOrder(-1).String()
	wrongByteOrder.Binary()
}
예제 #2
0
func TestCheckSizeV1(t *testing.T) {
	defer test.New(t)
	s := SuperblockV1{}
	size := int(unsafe.Sizeof(s))
	test.Equal(size, ReserveBlockSize)

	size = int(unsafe.Sizeof(s) - unsafe.Sizeof(s.IndirectAddr))
	test.Equal(size, MetaSize)

	size = int(unsafe.Sizeof(InodeIndex{}))
	test.Equal(size, 32)

	maxRecordNum := len(s.IndirectAddr) * BlockSize / size
	test.Equal(maxRecordNum < 1<<20, false)
	test.Equal(maxRecordNum > 5<<20, false, fmt.Errorf("%v", maxRecordNum))
}
예제 #3
0
func TestEncodeDecode(t *testing.T) {
	defer test.New(t)
	s := new(Superblock)
	s.SuperblockV1 = new(SuperblockV1)
	s.Version = 1
	s.CheckPoint = 2
	s.BlockBit = 3
	s.IndirectAddr[0] = int64(qrand.RandInt(1024))
	s.IndirectAddr[BlockSize-1] = 5

	target := s.Encode()

	s2 := new(Superblock)
	test.Nil(s2.Decode(target))
	test.Equal(s.SuperblockV1, s2.SuperblockV1)
	test.Equal(s.IndirectAddr[0], s2.IndirectAddr[0])
}
예제 #4
0
파일: volume_test.go 프로젝트: allmad/madq
func TestVolume(t *testing.T) {
	defer test.New(t)
	v, err := NewVolume("hello")
	test.Nil(err)
	v.Open("hello")

	_, err = NewVolume("!hello")
	test.Equal(err, ErrInvalidVolmueName)
}
예제 #5
0
파일: reader_test.go 프로젝트: allmad/madq
func TestReaderWriter(t *testing.T) {
	defer test.New(t)

	var n int
	f, err := os.Create(root)
	test.Nil(err)

	w := NewWriter(f, 1)
	offset, _ := w.Seek(-1, 1)
	test.NotEqual(offset, 0)

	_, err = w.Write([]byte("caoo"))
	test.Nil(err)
	defer w.Close()
	// |caoo

	_, err = w.Seek(0, 2)
	test.Equal(err, ErrSeekNotSupport)

	_, err = w.Seek(1, 0)
	test.Nil(err)

	n, err = w.Write([]byte("j"))
	test.Equals(err, nil, n, 1)
	// cj|oo

	r := NewReader(f, 0)
	defer r.Close()

	_, err = r.Seek(1, 1)
	test.Nil(err)
	// c|joo

	_, err = r.Seek(0, 2)
	test.Equal(err, ErrSeekNotSupport)

	buf := make([]byte, 3)
	n, err = r.Read(buf)
	test.Equals(n, 3, err, nil, buf, []byte("joo"))
	// cjoo|

	_, err = r.Seek(0, 0)
	test.Nil(err)
}
예제 #6
0
파일: block_test.go 프로젝트: allmad/madq
func TestBlockException(t *testing.T) {
	defer test.New(t)

	_, err := New(root, 0, 33)
	test.NotNil(err)

	_, err = New("/notperm", 0, 11)
	test.NotNil(err)

	f, err := New(root, 0, 10)
	test.Nil(err)

	err = f.Delete(false)
	test.Nil(err)

	_, err = f.ReadAt(nil, -1)
	test.Equal(err, ErrInvalidOffset)
	test.Equal(f.Close(), true)
	test.Equal(f.Close(), false)
	test.Equal(f.Delete(false), nil)
	_, err = f.WriteAt(nil, 1)
	test.Equal(err, ErrBlockClosed)
	_, err = f.ReadAt(nil, 1)
	test.Equal(err, ErrBlockClosed)
}
예제 #7
0
파일: block_test.go 프로젝트: allmad/madq
func TestBlock(t *testing.T) {
	defer test.New(t)

	f, err := New(root, 0, 2)
	test.Nil(err)
	test.Nil(f.Delete(false))
	defer f.Close()

	buf := bytes.Repeat([]byte("ha"), 1024)
	n, err := f.WriteAt(buf, 1)
	test.Nil(err)
	test.Equal(n, len(buf))

	buf2 := make([]byte, len(buf))
	n, err = f.ReadAt(buf2, 0)
	test.Nil(err)
	test.Equal(n, len(buf2))
	test.Equal(buf[:len(buf)-1], buf2[1:])

	n, err = f.ReadAt([]byte(" "), 1024*2+1)
	test.Equals(n, 0, err, io.EOF)

	n, err = f.ReadAt([]byte("  "), 0)
	test.Equals(n, 2, err, nil)

	n, err = f.ReadAt([]byte("  "), 0)
	test.Equals(n, 2, err, nil)

	os.RemoveAll(root)
	n, err = f.ReadAt([]byte("  "), 4)
	test.Equals(n, 0)
	test.CheckError(err, test.StrNotSuchFile)

	n, err = f.WriteAt([]byte("  "), 4)
	test.Equals(n, 0)
	test.CheckError(err, test.StrNotSuchFile)
}