Faster tests (#834)
This commit is contained in:
parent
c8a110a36d
commit
38f72df8b5
@ -1,90 +0,0 @@
|
||||
// Copyright (C) 2018 Storj Labs, Inc.
|
||||
// See LICENSE for copying information.
|
||||
|
||||
package encryption
|
||||
|
||||
import (
|
||||
"encoding/binary"
|
||||
"hash/crc32"
|
||||
|
||||
"storj.io/storj/pkg/ranger"
|
||||
)
|
||||
|
||||
const (
|
||||
crcBlockSize = 64 // this could literally be
|
||||
uint32Size = 4
|
||||
uint64Size = 8
|
||||
)
|
||||
|
||||
// crcAdder is a Transformer that is going to add a block number and a crc to
|
||||
// the end of each block
|
||||
type crcAdder struct {
|
||||
Table *crc32.Table
|
||||
}
|
||||
|
||||
func newCRCAdder(t *crc32.Table) *crcAdder {
|
||||
return &crcAdder{Table: t}
|
||||
}
|
||||
|
||||
func (c *crcAdder) InBlockSize() int { return crcBlockSize }
|
||||
func (c *crcAdder) OutBlockSize() int {
|
||||
return crcBlockSize + uint32Size + uint64Size
|
||||
}
|
||||
|
||||
func (c *crcAdder) Transform(out, in []byte, blockOffset int64) (
|
||||
[]byte, error) {
|
||||
// we're just going to take the input data, then add the block number,
|
||||
// big-endian encoded, then add the big-endian crc of the input + block
|
||||
// number.
|
||||
out = append(out, in...)
|
||||
var buf [uint64Size]byte
|
||||
binary.BigEndian.PutUint64(buf[:], uint64(blockOffset))
|
||||
out = append(out, buf[:]...)
|
||||
binary.BigEndian.PutUint32(buf[:uint32Size], crc32.Checksum(out, c.Table))
|
||||
out = append(out, buf[:uint32Size]...)
|
||||
return out, nil
|
||||
}
|
||||
|
||||
// crcChecker is a Transformer that validates a given CRC and compares the
|
||||
// block number, then removes them from the input, returning the original
|
||||
// unchecked input.
|
||||
type crcChecker struct {
|
||||
Table *crc32.Table
|
||||
}
|
||||
|
||||
func newCRCChecker(t *crc32.Table) *crcChecker {
|
||||
return &crcChecker{Table: t}
|
||||
}
|
||||
|
||||
func (c *crcChecker) InBlockSize() int {
|
||||
return crcBlockSize + uint32Size + uint64Size
|
||||
}
|
||||
|
||||
func (c *crcChecker) OutBlockSize() int { return crcBlockSize }
|
||||
|
||||
func (c *crcChecker) Transform(out, in []byte, blockOffset int64) (
|
||||
[]byte, error) {
|
||||
bs := c.OutBlockSize()
|
||||
// first check the crc
|
||||
if binary.BigEndian.Uint32(in[bs+uint64Size:bs+uint64Size+uint32Size]) !=
|
||||
crc32.Checksum(in[:bs+uint64Size], c.Table) {
|
||||
return nil, Error.New("crc check mismatch")
|
||||
}
|
||||
// then check the block offset
|
||||
if binary.BigEndian.Uint64(in[bs:bs+uint64Size]) != uint64(blockOffset) {
|
||||
return nil, Error.New("block offset mismatch")
|
||||
}
|
||||
return append(out, in[:bs]...), nil
|
||||
}
|
||||
|
||||
// addCRC is a Ranger constructor, given a specific crc table and an existing
|
||||
// un-crced Ranger
|
||||
func addCRC(data ranger.Ranger, tab *crc32.Table) (ranger.Ranger, error) {
|
||||
return Transform(data, newCRCAdder(tab))
|
||||
}
|
||||
|
||||
// checkCRC is a Ranger constructor, given a specific crc table and an existing
|
||||
// crced Ranger
|
||||
func checkCRC(data ranger.Ranger, tab *crc32.Table) (ranger.Ranger, error) {
|
||||
return Transform(data, newCRCChecker(tab))
|
||||
}
|
@ -5,16 +5,12 @@ package encryption
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"fmt"
|
||||
"hash/crc32"
|
||||
"io"
|
||||
"io/ioutil"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
|
||||
"storj.io/storj/pkg/ranger"
|
||||
)
|
||||
|
||||
func TestCalcEncompassingBlocks(t *testing.T) {
|
||||
@ -67,82 +63,6 @@ func TestCalcEncompassingBlocks(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestCRC(t *testing.T) {
|
||||
const blocks = 3
|
||||
rr, err := addCRC(ranger.ByteRanger(bytes.Repeat([]byte{0}, blocks*64)),
|
||||
crc32.IEEETable)
|
||||
if err != nil {
|
||||
t.Fatalf("unexpected: %v", err)
|
||||
}
|
||||
if rr.Size() != blocks*(64+4+8) {
|
||||
t.Fatalf("invalid crc padded size")
|
||||
}
|
||||
ctx := context.Background()
|
||||
r, err := rr.Range(ctx, 0, rr.Size())
|
||||
if err != nil {
|
||||
t.Fatalf("unexpected: %v", err)
|
||||
}
|
||||
data, err := ioutil.ReadAll(r)
|
||||
if err != nil || int64(len(data)) != rr.Size() {
|
||||
t.Fatalf("unexpected: %v", err)
|
||||
}
|
||||
|
||||
rr, err = checkCRC(ranger.ByteRanger(data), crc32.IEEETable)
|
||||
if err != nil {
|
||||
t.Fatalf("unexpected: %v", err)
|
||||
}
|
||||
|
||||
if rr.Size() != blocks*64 {
|
||||
t.Fatalf("invalid crc padded size")
|
||||
}
|
||||
|
||||
r, err = rr.Range(ctx, 0, rr.Size())
|
||||
if err != nil {
|
||||
t.Fatalf("unexpected: %v", err)
|
||||
}
|
||||
data, err = ioutil.ReadAll(r)
|
||||
if err != nil {
|
||||
t.Fatalf("unexpected: %v", err)
|
||||
}
|
||||
|
||||
if !bytes.Equal(data, bytes.Repeat([]byte{0}, blocks*64)) {
|
||||
t.Fatalf("invalid data")
|
||||
}
|
||||
}
|
||||
|
||||
func TestCRCSubranges(t *testing.T) {
|
||||
const blocks = 3
|
||||
data := bytes.Repeat([]byte{0, 1, 2}, blocks*64)
|
||||
internal, err := addCRC(ranger.ByteRanger(data), crc32.IEEETable)
|
||||
if err != nil {
|
||||
t.Fatalf("unexpected: %v", err)
|
||||
}
|
||||
external, err := checkCRC(internal, crc32.IEEETable)
|
||||
if err != nil {
|
||||
t.Fatalf("unexpected: %v", err)
|
||||
}
|
||||
if external.Size() != int64(len(data)) {
|
||||
t.Fatalf("wrong size")
|
||||
}
|
||||
|
||||
ctx := context.Background()
|
||||
for i := 0; i < len(data); i++ {
|
||||
for j := i; j < len(data); j++ {
|
||||
r, err := external.Range(ctx, int64(i), int64(j-i))
|
||||
if err != nil {
|
||||
t.Fatalf("unexpected: %v", err)
|
||||
}
|
||||
read, err := ioutil.ReadAll(r)
|
||||
if err != nil {
|
||||
t.Fatalf("unexpected: %v", err)
|
||||
}
|
||||
if !bytes.Equal(read, data[i:j]) {
|
||||
t.Fatalf("bad subrange")
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
type nopTransformer struct {
|
||||
blockSize int
|
||||
}
|
||||
|
@ -4,39 +4,30 @@
|
||||
package kademlia_test
|
||||
|
||||
import (
|
||||
"context"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
|
||||
"storj.io/storj/internal/testcontext"
|
||||
"storj.io/storj/internal/testplanet"
|
||||
"storj.io/storj/pkg/provider"
|
||||
)
|
||||
|
||||
func newTestIdentity() (*provider.FullIdentity, error) {
|
||||
fid, err := provider.NewFullIdentity(context.Background(), 12, 4)
|
||||
return fid, err
|
||||
}
|
||||
|
||||
func TestLookupNodes(t *testing.T) {
|
||||
ctx := testcontext.New(t)
|
||||
defer ctx.Cleanup()
|
||||
|
||||
planet, err := testplanet.New(t, 1, 30, 0)
|
||||
planet, err := testplanet.New(t, 1, 8, 0)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer ctx.Check(planet.Shutdown)
|
||||
|
||||
planet.Start(ctx)
|
||||
|
||||
k := planet.Satellites[0].Kademlia
|
||||
err = k.Bootstrap(ctx)
|
||||
assert.NoError(t, err)
|
||||
|
||||
id, err := newTestIdentity()
|
||||
assert.NoError(t, err)
|
||||
assert.NotNil(t, id)
|
||||
|
||||
seen := k.Seen()
|
||||
assert.NotEqual(t, len(seen), 0)
|
||||
assert.NotNil(t, seen)
|
||||
|
Loading…
Reference in New Issue
Block a user