storj/pkg/audit/verifier_test.go

156 lines
3.9 KiB
Go
Raw Normal View History

2019-01-24 20:15:10 +00:00
// Copyright (C) 2019 Storj Labs, Inc.
2018-10-09 22:10:37 +01:00
// See LICENSE for copying information.
package audit
2018-10-09 22:10:37 +01:00
import (
"context"
"crypto/rand"
2018-10-09 22:10:37 +01:00
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
2018-10-09 22:10:37 +01:00
"github.com/vivint/infectious"
"storj.io/storj/pkg/pb"
"storj.io/storj/pkg/pkcrypto"
"storj.io/storj/pkg/storj"
2018-10-09 22:10:37 +01:00
)
func TestFailingAudit(t *testing.T) {
const (
required = 8
total = 14
)
f, err := infectious.NewFEC(required, total)
require.NoError(t, err)
2018-10-09 22:10:37 +01:00
shares := make([]infectious.Share, total)
output := func(s infectious.Share) {
shares[s.Number] = s.DeepCopy()
}
// the data to encode must be padded to a multiple of required, hence the
// underscores.
err = f.Encode([]byte("hello, world! __"), output)
require.NoError(t, err)
2018-10-09 22:10:37 +01:00
modifiedShares := make([]infectious.Share, len(shares))
for i := range shares {
modifiedShares[i] = shares[i].DeepCopy()
}
modifiedShares[0].Data[1] = '!'
modifiedShares[2].Data[0] = '#'
modifiedShares[3].Data[1] = '!'
modifiedShares[4].Data[0] = 'b'
badPieceNums := []int{0, 2, 3, 4}
ctx := context.Background()
auditPkgShares := make(map[int]Share, len(modifiedShares))
2018-10-09 22:10:37 +01:00
for i := range modifiedShares {
auditPkgShares[modifiedShares[i].Number] = Share{
PieceNum: modifiedShares[i].Number,
Data: append([]byte(nil), modifiedShares[i].Data...),
2018-11-28 07:33:17 +00:00
}
2018-10-09 22:10:37 +01:00
}
pieceNums, correctedShares, err := auditShares(ctx, 8, 14, auditPkgShares)
2018-10-09 22:10:37 +01:00
if err != nil {
panic(err)
}
2018-10-09 22:10:37 +01:00
for i, num := range pieceNums {
if num != badPieceNums[i] {
t.Fatal("expected nums in pieceNums to be same as in badPieceNums")
}
}
require.Equal(t, shares, correctedShares)
2018-10-09 22:10:37 +01:00
}
func TestNotEnoughShares(t *testing.T) {
const (
required = 8
total = 14
)
f, err := infectious.NewFEC(required, total)
require.NoError(t, err)
2018-10-09 22:10:37 +01:00
shares := make([]infectious.Share, total)
output := func(s infectious.Share) {
shares[s.Number] = s.DeepCopy()
}
// the data to encode must be padded to a multiple of required, hence the
// underscores.
err = f.Encode([]byte("hello, world! __"), output)
require.NoError(t, err)
2018-10-09 22:10:37 +01:00
ctx := context.Background()
auditPkgShares := make(map[int]Share, len(shares))
2018-10-09 22:10:37 +01:00
for i := range shares {
auditPkgShares[shares[i].Number] = Share{
PieceNum: shares[i].Number,
Data: append([]byte(nil), shares[i].Data...),
2018-11-28 07:33:17 +00:00
}
2018-10-09 22:10:37 +01:00
}
_, _, err = auditShares(ctx, 20, 40, auditPkgShares)
require.Contains(t, err.Error(), "infectious: must specify at least the number of required shares")
2018-10-09 22:10:37 +01:00
}
func TestCreatePendingAudits(t *testing.T) {
const (
required = 8
total = 14
)
f, err := infectious.NewFEC(required, total)
require.NoError(t, err)
shares := make([]infectious.Share, total)
output := func(s infectious.Share) {
shares[s.Number] = s.DeepCopy()
}
// the data to encode must be padded to a multiple of required, hence the
// underscores.
err = f.Encode([]byte("hello, world! __"), output)
require.NoError(t, err)
var testNodeID storj.NodeID
_, err = rand.Read(testNodeID[:])
require.NoError(t, err)
ctx := context.Background()
contained := make(map[int]storj.NodeID)
contained[1] = testNodeID
stripe := Stripe{
Index: 3,
Segment: &pb.Pointer{
Type: pb.Pointer_REMOTE,
Remote: &pb.RemoteSegment{
RootPieceId: storj.NewPieceID(),
Redundancy: &pb.RedundancyScheme{
MinReq: 8,
Total: 14,
ErasureShareSize: int32(len(shares[0].Data)),
},
},
},
SegmentPath: "test/path",
}
pending, err := createPendingAudits(ctx, contained, shares, &stripe)
require.NoError(t, err)
require.Equal(t, 1, len(pending))
assert.Equal(t, testNodeID, pending[0].NodeID)
assert.Equal(t, stripe.Segment.Remote.RootPieceId, pending[0].PieceID)
assert.Equal(t, stripe.Index, pending[0].StripeIndex)
assert.Equal(t, stripe.Segment.Remote.Redundancy.ErasureShareSize, pending[0].ShareSize)
assert.Equal(t, pkcrypto.SHA256Hash(shares[1].Data), pending[0].ExpectedShareHash)
assert.EqualValues(t, 0, pending[0].ReverifyCount)
}