2018-10-09 23:05:42 +01:00
|
|
|
// Copyright (C) 2018 Storj Labs, Inc.
|
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
2018-10-05 14:34:15 +01:00
|
|
|
package audit
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"crypto/rand"
|
|
|
|
"errors"
|
|
|
|
"math"
|
|
|
|
"math/big"
|
|
|
|
"reflect"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"go.uber.org/zap"
|
|
|
|
"google.golang.org/grpc"
|
|
|
|
|
2018-10-09 23:05:42 +01:00
|
|
|
"storj.io/storj/pkg/auth"
|
2018-10-05 14:34:15 +01:00
|
|
|
"storj.io/storj/pkg/overlay"
|
|
|
|
"storj.io/storj/pkg/paths"
|
|
|
|
"storj.io/storj/pkg/pb"
|
|
|
|
"storj.io/storj/pkg/pointerdb"
|
|
|
|
"storj.io/storj/pkg/pointerdb/pdbclient"
|
|
|
|
"storj.io/storj/pkg/storage/meta"
|
|
|
|
"storj.io/storj/storage/redis/redisserver"
|
2018-10-09 23:05:42 +01:00
|
|
|
"storj.io/storj/storage/teststore"
|
2018-10-05 14:34:15 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2018-10-10 09:33:17 +01:00
|
|
|
ctx = context.Background()
|
|
|
|
ErrNoList = errors.New("list error: failed to get list")
|
|
|
|
ErrNoNum = errors.New("num error: failed to get num")
|
2018-10-05 14:34:15 +01:00
|
|
|
)
|
|
|
|
|
2018-10-10 09:33:17 +01:00
|
|
|
// pointerDBWrapper wraps pb.PointerDBServer to be compatible with pb.PointerDBClient
|
2018-10-05 14:34:15 +01:00
|
|
|
type pointerDBWrapper struct {
|
|
|
|
s pb.PointerDBServer
|
|
|
|
}
|
|
|
|
|
2018-10-10 09:33:17 +01:00
|
|
|
func newPointerDBWrapper(pdbs pb.PointerDBServer) pb.PointerDBClient {
|
|
|
|
return &pointerDBWrapper{pdbs}
|
|
|
|
}
|
|
|
|
|
2018-10-05 14:34:15 +01:00
|
|
|
func (pbd *pointerDBWrapper) Put(ctx context.Context, in *pb.PutRequest, opts ...grpc.CallOption) (*pb.PutResponse, error) {
|
|
|
|
return pbd.s.Put(ctx, in)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (pbd *pointerDBWrapper) Get(ctx context.Context, in *pb.GetRequest, opts ...grpc.CallOption) (*pb.GetResponse, error) {
|
|
|
|
return pbd.s.Get(ctx, in)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (pbd *pointerDBWrapper) List(ctx context.Context, in *pb.ListRequest, opts ...grpc.CallOption) (*pb.ListResponse, error) {
|
|
|
|
return pbd.s.List(ctx, in)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (pbd *pointerDBWrapper) Delete(ctx context.Context, in *pb.DeleteRequest, opts ...grpc.CallOption) (*pb.DeleteResponse, error) {
|
|
|
|
return pbd.s.Delete(ctx, in)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestAuditSegment(t *testing.T) {
|
2018-10-10 09:33:17 +01:00
|
|
|
type pathCount struct {
|
|
|
|
path paths.Path
|
|
|
|
count int
|
|
|
|
}
|
|
|
|
|
2018-10-05 14:34:15 +01:00
|
|
|
// note: to simulate better,
|
|
|
|
// change limit in library to 5 in
|
|
|
|
// list api call, default is 0 == 1000 listing
|
|
|
|
tests := []struct {
|
2018-10-09 23:05:42 +01:00
|
|
|
bm string
|
|
|
|
path paths.Path
|
2018-10-05 14:34:15 +01:00
|
|
|
}{
|
|
|
|
{
|
2018-10-09 23:05:42 +01:00
|
|
|
bm: "success-1",
|
|
|
|
path: paths.New("folder1/file1"),
|
2018-10-05 14:34:15 +01:00
|
|
|
},
|
|
|
|
{
|
2018-10-09 23:05:42 +01:00
|
|
|
bm: "success-2",
|
|
|
|
path: paths.New("foodFolder1/file1/file2"),
|
2018-10-05 14:34:15 +01:00
|
|
|
},
|
|
|
|
{
|
2018-10-09 23:05:42 +01:00
|
|
|
bm: "success-3",
|
|
|
|
path: paths.New("foodFolder1/file1/file2/foodFolder2/file3"),
|
2018-10-05 14:34:15 +01:00
|
|
|
},
|
|
|
|
{
|
2018-10-09 23:05:42 +01:00
|
|
|
bm: "success-4",
|
|
|
|
path: paths.New("projectFolder/project1.txt/"),
|
2018-10-05 14:34:15 +01:00
|
|
|
},
|
|
|
|
{
|
2018-10-09 23:05:42 +01:00
|
|
|
bm: "success-5",
|
|
|
|
path: paths.New("newProjectFolder/project2.txt"),
|
2018-10-05 14:34:15 +01:00
|
|
|
},
|
|
|
|
{
|
2018-10-09 23:05:42 +01:00
|
|
|
bm: "success-6",
|
|
|
|
path: paths.New("Pictures/image1.png"),
|
2018-10-05 14:34:15 +01:00
|
|
|
},
|
|
|
|
{
|
2018-10-09 23:05:42 +01:00
|
|
|
bm: "success-7",
|
|
|
|
path: paths.New("Pictures/Nature/mountains.png"),
|
2018-10-05 14:34:15 +01:00
|
|
|
},
|
|
|
|
{
|
2018-10-09 23:05:42 +01:00
|
|
|
bm: "success-8",
|
|
|
|
path: paths.New("Pictures/City/streets.png"),
|
2018-10-05 14:34:15 +01:00
|
|
|
},
|
|
|
|
{
|
2018-10-09 23:05:42 +01:00
|
|
|
bm: "success-9",
|
|
|
|
path: paths.New("Pictures/Animals/Dogs/dogs.png"),
|
2018-10-05 14:34:15 +01:00
|
|
|
},
|
|
|
|
{
|
2018-10-09 23:05:42 +01:00
|
|
|
bm: "success-10",
|
|
|
|
path: paths.New("Nada/ビデオ/😶"),
|
2018-10-05 14:34:15 +01:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2018-10-09 15:39:14 +01:00
|
|
|
ctx = auth.WithAPIKey(ctx, nil)
|
|
|
|
|
2018-10-10 19:25:46 +01:00
|
|
|
// PointerDB instantiation
|
2018-10-05 14:34:15 +01:00
|
|
|
db := teststore.New()
|
|
|
|
c := pointerdb.Config{MaxInlineSegmentSize: 8000}
|
2018-10-09 23:05:42 +01:00
|
|
|
|
2018-10-05 14:34:15 +01:00
|
|
|
redisAddr, cleanup, err := redisserver.Start()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2018-10-09 23:05:42 +01:00
|
|
|
|
2018-10-05 14:34:15 +01:00
|
|
|
defer cleanup()
|
|
|
|
|
|
|
|
cache, err := overlay.NewRedisOverlayCache(redisAddr, "", 1, nil)
|
|
|
|
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.NotNil(t, cache)
|
|
|
|
|
2018-10-11 15:35:55 +01:00
|
|
|
pdbw := newPointerDBWrapper(pointerdb.NewServer(db, cache, zap.NewNop(), c, nil))
|
2018-10-09 15:39:14 +01:00
|
|
|
pointers := pdbclient.New(pdbw)
|
2018-10-05 14:34:15 +01:00
|
|
|
|
|
|
|
// create a pdb client and instance of audit
|
2018-10-10 19:25:46 +01:00
|
|
|
cursor := NewCursor(pointers)
|
2018-10-05 14:34:15 +01:00
|
|
|
|
|
|
|
// put 10 paths in db
|
|
|
|
t.Run("putToDB", func(t *testing.T) {
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.bm, func(t *testing.T) {
|
|
|
|
assert1 := assert.New(t)
|
|
|
|
|
|
|
|
// create a pointer and put in db
|
2018-10-09 23:05:42 +01:00
|
|
|
putRequest := makePutRequest(tt.path)
|
2018-10-05 14:34:15 +01:00
|
|
|
|
|
|
|
// create putreq. object
|
2018-10-09 15:39:14 +01:00
|
|
|
req := &pb.PutRequest{Path: tt.path.String(), Pointer: putRequest.Pointer}
|
2018-10-05 14:34:15 +01:00
|
|
|
|
2018-10-10 19:25:46 +01:00
|
|
|
// put pointer into db
|
2018-10-05 14:34:15 +01:00
|
|
|
_, err := pdbw.Put(ctx, req)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("failed to put %v: error: %v", req.Pointer, err)
|
|
|
|
assert1.NotNil(err)
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
t.Error("cant instantiate the piece store client")
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("NextStripe", func(t *testing.T) {
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.bm, func(t *testing.T) {
|
|
|
|
assert1 := assert.New(t)
|
2018-10-10 19:25:46 +01:00
|
|
|
stripe, err := cursor.NextStripe(ctx)
|
2018-10-05 14:34:15 +01:00
|
|
|
if err != nil {
|
|
|
|
assert1.Error(err)
|
|
|
|
assert1.Nil(stripe)
|
|
|
|
}
|
|
|
|
if stripe != nil {
|
|
|
|
assert1.Nil(err)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
// test to see how random paths are
|
2018-10-10 19:25:46 +01:00
|
|
|
t.Run("probabilisticTest", func(t *testing.T) {
|
2018-10-05 14:34:15 +01:00
|
|
|
list, _, err := pointers.List(ctx, nil, nil, nil, true, 10, meta.None)
|
|
|
|
if err != nil {
|
|
|
|
t.Error(ErrNoList)
|
|
|
|
}
|
|
|
|
|
|
|
|
// get count of items picked at random
|
|
|
|
uniquePathCounted := []pathCount{}
|
|
|
|
pathCounter := []pathCount{}
|
|
|
|
|
|
|
|
// get a list of 100 paths generated from random
|
|
|
|
for i := 0; i < 100; i++ {
|
|
|
|
randomNum, err := rand.Int(rand.Reader, big.NewInt(int64(len(list))))
|
|
|
|
if err != nil {
|
2018-10-10 09:33:17 +01:00
|
|
|
t.Error(ErrNoNum)
|
2018-10-05 14:34:15 +01:00
|
|
|
}
|
|
|
|
pointerItem := list[randomNum.Int64()]
|
|
|
|
path := pointerItem.Path
|
|
|
|
val := pathCount{path: path, count: 1}
|
|
|
|
pathCounter = append(pathCounter, val)
|
|
|
|
}
|
|
|
|
|
|
|
|
// get a count for paths in list
|
|
|
|
for _, pc := range pathCounter {
|
|
|
|
skip := false
|
|
|
|
for i, up := range uniquePathCounted {
|
|
|
|
if reflect.DeepEqual(pc.path, up.path) {
|
|
|
|
up.count = up.count + 1
|
|
|
|
uniquePathCounted[i] = up
|
|
|
|
skip = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !skip {
|
|
|
|
uniquePathCounted = append(uniquePathCounted, pc)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Section: binomial test for randomness
|
|
|
|
n := float64(100) // events
|
|
|
|
p := float64(.10) // theoretical probability of getting 1/10 paths
|
|
|
|
m := n * p
|
|
|
|
s := math.Sqrt(m * (1 - p)) // binomial distribution
|
|
|
|
|
|
|
|
// if values fall outside of the critical values of test statistics (ie Z value)
|
|
|
|
// in a 2-tail test
|
|
|
|
// we can assume, 95% confidence, it's not sampling according to a 10% probability
|
|
|
|
for _, v := range uniquePathCounted {
|
|
|
|
z := (float64(v.count) - m) / s
|
|
|
|
if z <= -1.96 || z >= 1.96 {
|
|
|
|
t.Log(false)
|
|
|
|
} else {
|
|
|
|
t.Log(true)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2018-10-09 23:05:42 +01:00
|
|
|
func makePutRequest(path paths.Path) pb.PutRequest {
|
2018-10-05 14:34:15 +01:00
|
|
|
var rps []*pb.RemotePiece
|
|
|
|
rps = append(rps, &pb.RemotePiece{
|
|
|
|
PieceNum: 1,
|
|
|
|
NodeId: "testId",
|
|
|
|
})
|
|
|
|
pr := pb.PutRequest{
|
|
|
|
Path: path.String(),
|
|
|
|
Pointer: &pb.Pointer{
|
|
|
|
Type: pb.Pointer_REMOTE,
|
|
|
|
Remote: &pb.RemoteSegment{
|
|
|
|
Redundancy: &pb.RedundancyScheme{
|
|
|
|
Type: pb.RedundancyScheme_RS,
|
|
|
|
MinReq: 1,
|
|
|
|
Total: 3,
|
|
|
|
RepairThreshold: 2,
|
|
|
|
SuccessThreshold: 3,
|
|
|
|
ErasureShareSize: 2,
|
|
|
|
},
|
|
|
|
PieceId: "testId",
|
|
|
|
RemotePieces: rps,
|
|
|
|
},
|
|
|
|
Size: int64(10),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
return pr
|
|
|
|
}
|