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
|
|
|
|
|
2019-01-18 13:54:08 +00:00
|
|
|
// TODO: use audit_test as the test package to avoid import cycles
|
|
|
|
|
2018-10-05 14:34:15 +01:00
|
|
|
import (
|
|
|
|
"crypto/rand"
|
|
|
|
"math"
|
|
|
|
"math/big"
|
|
|
|
"reflect"
|
|
|
|
"testing"
|
2019-01-15 16:08:45 +00:00
|
|
|
"time"
|
2018-10-05 14:34:15 +01:00
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
2019-01-15 16:08:45 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
2018-10-05 14:34:15 +01:00
|
|
|
|
2019-01-15 16:08:45 +00:00
|
|
|
"storj.io/storj/internal/testcontext"
|
|
|
|
"storj.io/storj/internal/testplanet"
|
2018-11-29 18:39:27 +00:00
|
|
|
"storj.io/storj/internal/teststorj"
|
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/pb"
|
|
|
|
"storj.io/storj/pkg/storage/meta"
|
2018-10-25 21:28:16 +01:00
|
|
|
"storj.io/storj/pkg/storj"
|
2018-10-05 14:34:15 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestAuditSegment(t *testing.T) {
|
2018-10-10 09:33:17 +01:00
|
|
|
type pathCount struct {
|
2018-10-25 21:28:16 +01:00
|
|
|
path storj.Path
|
2018-10-10 09:33:17 +01:00
|
|
|
count int
|
|
|
|
}
|
|
|
|
|
2019-01-15 16:08:45 +00:00
|
|
|
tctx := testcontext.New(t)
|
|
|
|
defer tctx.Cleanup()
|
|
|
|
|
|
|
|
planet, err := testplanet.New(t, 1, 4, 1)
|
|
|
|
require.NoError(t, err)
|
|
|
|
defer tctx.Check(planet.Shutdown)
|
|
|
|
|
|
|
|
planet.Start(tctx)
|
|
|
|
|
|
|
|
// we wait a second for all the nodes to complete bootstrapping off the satellite
|
|
|
|
time.Sleep(2 * time.Second)
|
2018-11-05 15:12:19 +00:00
|
|
|
|
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
|
2018-10-25 21:28:16 +01:00
|
|
|
path storj.Path
|
2018-10-05 14:34:15 +01:00
|
|
|
}{
|
|
|
|
{
|
2018-10-09 23:05:42 +01:00
|
|
|
bm: "success-1",
|
2018-10-25 21:28:16 +01:00
|
|
|
path: "folder1/file1",
|
2018-10-05 14:34:15 +01:00
|
|
|
},
|
|
|
|
{
|
2018-10-09 23:05:42 +01:00
|
|
|
bm: "success-2",
|
2018-10-25 21:28:16 +01:00
|
|
|
path: "foodFolder1/file1/file2",
|
2018-10-05 14:34:15 +01:00
|
|
|
},
|
|
|
|
{
|
2018-10-09 23:05:42 +01:00
|
|
|
bm: "success-3",
|
2018-10-25 21:28:16 +01:00
|
|
|
path: "foodFolder1/file1/file2/foodFolder2/file3",
|
2018-10-05 14:34:15 +01:00
|
|
|
},
|
|
|
|
{
|
2018-10-09 23:05:42 +01:00
|
|
|
bm: "success-4",
|
2018-10-25 21:28:16 +01:00
|
|
|
path: "projectFolder/project1.txt/",
|
2018-10-05 14:34:15 +01:00
|
|
|
},
|
|
|
|
{
|
2018-10-09 23:05:42 +01:00
|
|
|
bm: "success-5",
|
2018-10-25 21:28:16 +01:00
|
|
|
path: "newProjectFolder/project2.txt",
|
2018-10-05 14:34:15 +01:00
|
|
|
},
|
|
|
|
{
|
2018-10-09 23:05:42 +01:00
|
|
|
bm: "success-6",
|
2018-10-25 21:28:16 +01:00
|
|
|
path: "Pictures/image1.png",
|
2018-10-05 14:34:15 +01:00
|
|
|
},
|
|
|
|
{
|
2018-10-09 23:05:42 +01:00
|
|
|
bm: "success-7",
|
2018-10-25 21:28:16 +01:00
|
|
|
path: "Pictures/Nature/mountains.png",
|
2018-10-05 14:34:15 +01:00
|
|
|
},
|
|
|
|
{
|
2018-10-09 23:05:42 +01:00
|
|
|
bm: "success-8",
|
2018-10-25 21:28:16 +01:00
|
|
|
path: "Pictures/City/streets.png",
|
2018-10-05 14:34:15 +01:00
|
|
|
},
|
|
|
|
{
|
2018-10-09 23:05:42 +01:00
|
|
|
bm: "success-9",
|
2018-10-25 21:28:16 +01:00
|
|
|
path: "Pictures/Animals/Dogs/dogs.png",
|
2018-10-05 14:34:15 +01:00
|
|
|
},
|
|
|
|
{
|
2018-10-09 23:05:42 +01:00
|
|
|
bm: "success-10",
|
2018-10-25 21:28:16 +01:00
|
|
|
path: "Nada/ビデオ/😶",
|
2018-10-05 14:34:15 +01:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2019-01-15 16:08:45 +00:00
|
|
|
ctx := auth.WithAPIKey(tctx, nil)
|
2018-10-09 15:39:14 +01:00
|
|
|
|
2019-01-18 13:54:08 +00:00
|
|
|
pointers := planet.Satellites[0].Metainfo.Endpoint
|
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-25 21:28:16 +01:00
|
|
|
req := &pb.PutRequest{Path: tt.path, Pointer: putRequest.Pointer}
|
2018-10-05 14:34:15 +01:00
|
|
|
|
2018-10-10 19:25:46 +01:00
|
|
|
// put pointer into db
|
2019-01-10 16:35:18 +00:00
|
|
|
_, err := pointers.Put(ctx, req)
|
2018-10-05 14:34:15 +01:00
|
|
|
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) {
|
2019-01-10 16:35:18 +00:00
|
|
|
listRes, err := pointers.List(ctx, &pb.ListRequest{
|
|
|
|
Prefix: "",
|
|
|
|
StartAfter: "",
|
|
|
|
EndBefore: "",
|
|
|
|
Recursive: true,
|
|
|
|
Limit: 10,
|
|
|
|
MetaFlags: meta.None,
|
|
|
|
})
|
2019-01-18 13:54:08 +00:00
|
|
|
require.NoError(t, err)
|
2018-10-05 14:34:15 +01:00
|
|
|
|
2019-01-10 16:35:18 +00:00
|
|
|
list := listRes.GetItems()
|
2019-01-18 13:54:08 +00:00
|
|
|
require.Len(t, list, 10)
|
2019-01-10 16:35:18 +00:00
|
|
|
|
2018-10-05 14:34:15 +01:00
|
|
|
// 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 {
|
2019-01-18 13:54:08 +00:00
|
|
|
t.Error("num error: failed to get num")
|
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-25 21:28:16 +01:00
|
|
|
func makePutRequest(path storj.Path) pb.PutRequest {
|
2018-10-05 14:34:15 +01:00
|
|
|
var rps []*pb.RemotePiece
|
|
|
|
rps = append(rps, &pb.RemotePiece{
|
|
|
|
PieceNum: 1,
|
2018-11-29 18:39:27 +00:00
|
|
|
NodeId: teststorj.NodeIDFromString("testId"),
|
2018-10-05 14:34:15 +01:00
|
|
|
})
|
|
|
|
pr := pb.PutRequest{
|
2018-10-25 21:28:16 +01:00
|
|
|
Path: path,
|
2018-10-05 14:34:15 +01:00
|
|
|
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,
|
|
|
|
},
|
2018-11-20 17:09:35 +00:00
|
|
|
SegmentSize: int64(10),
|
2018-10-05 14:34:15 +01:00
|
|
|
},
|
|
|
|
}
|
|
|
|
return pr
|
|
|
|
}
|